GridViewer.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. '''
  2. Created on 26 nov. 2016
  3. @author: olinox
  4. '''
  5. if __name__ == "__main__":
  6. import os, sys
  7. pypog_path = (os.path.abspath("..\\..\\"))
  8. sys.path.append(pypog_path)
  9. from PyQt5.QtCore import QPointF
  10. from PyQt5.QtGui import QPolygonF
  11. from PyQt5.QtWidgets import QMainWindow, QGraphicsPolygonItem, QGraphicsItem, \
  12. QApplication, QGraphicsScene
  13. from core import geometry
  14. from core.graphic.cells import polygon
  15. from tests.gridviewer.main import Ui_window
  16. # from PyQt5.Qt import QMainWindow, QApplication, QPolygonF, QGraphicsPolygonItem, \
  17. # QGraphicsItem, QPointF
  18. class GridViewer(QMainWindow):
  19. def __init__(self):
  20. super (GridViewer, self).__init__()
  21. self._polygons = {}
  22. self.createWidgets()
  23. def createWidgets(self):
  24. self.ui = Ui_window()
  25. self.ui.setupUi(self)
  26. self._scene = QGraphicsScene()
  27. self.ui.view.setScene(self._scene)
  28. self.ui.view.scale(0.25, 0.25)
  29. self.ui.view.centerOn(QPointF(0,0))
  30. self.ui.view.setDragMode(1)
  31. self.ui.btn_make.clicked.connect(self.make_grid)
  32. def make_grid(self):
  33. shape = geometry.HEX if self.ui.opt_hex.isChecked() else geometry.SQUARE
  34. width = self.ui.spb_width.value()
  35. height = self.ui.spb_height.value()
  36. kx = 1 if shape == geometry.SQUARE else 0.866
  37. margin = 240
  38. cell_height = 120
  39. self._scene.clear()
  40. self._scene.setSceneRect(0 - margin, 0 - margin, (kx * cell_height * (width + 2)) + margin, (cell_height * (height + 2)) + margin)
  41. for x in range(width):
  42. for y in range(height):
  43. points = [QPointF(xp, yp) for xp, yp in polygon(shape, x, y)]
  44. qpolygon = QPolygonF( points )
  45. graphic_polygon = QGraphicsPolygonItem(qpolygon)
  46. self._scene.addItem(graphic_polygon)
  47. graphic_polygon.setFlag(QGraphicsItem.ItemIsFocusable)
  48. self._polygons[(x, y)] = graphic_polygon
  49. if __name__ == "__main__":
  50. app = QApplication(sys.argv)
  51. gv = GridViewer()
  52. gv.show()
  53. r = app.exec_()
  54. exit(r)