GridViewer.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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, Qt
  10. from PyQt5.QtWidgets import QMainWindow, \
  11. QApplication, QGraphicsScene, QGraphicsView
  12. from core import geometry
  13. from tests.gridviewer.GridViewerCell import GridViewerCell
  14. from tests.gridviewer.main import Ui_window
  15. class GridViewer(QMainWindow):
  16. def __init__(self):
  17. super (GridViewer, self).__init__()
  18. self._polygons = {}
  19. self.selection = []
  20. self.createWidgets()
  21. def createWidgets(self):
  22. self.ui = Ui_window()
  23. self.ui.setupUi(self)
  24. self._scene = QGraphicsScene()
  25. self.ui.view.setScene(self._scene)
  26. self.ui.view.scale(0.25, 0.25)
  27. self.ui.view.centerOn(QPointF(0,0))
  28. self.ui.view.setDragMode(QGraphicsView.NoDrag)
  29. self.ui.btn_make.clicked.connect(self.make_grid)
  30. self.ui.txt_coords.textChanged.connect(self.update_selected_cells)
  31. def make_grid(self):
  32. shape = geometry.HEX if self.ui.opt_hex.isChecked() else geometry.SQUARE
  33. width = self.ui.spb_width.value()
  34. height = self.ui.spb_height.value()
  35. kx = 1 if shape == geometry.SQUARE else 0.866
  36. margin = 240
  37. cell_height = 120
  38. self._scene.clear()
  39. self._scene.setSceneRect(0 - margin, 0 - margin, (kx * cell_height * (width + 2)) + margin, (cell_height * (height + 2)) + margin)
  40. for x in range(width):
  41. for y in range(height):
  42. cell = GridViewerCell(self, x, y)
  43. cell.generate(shape)
  44. self._scene.addItem(cell)
  45. self._polygons[(x, y)] = cell
  46. def add_to_selection(self, x, y):
  47. self.selection.append( (x, y) )
  48. self.ui.txt_coords.setText( str(self.selection) )
  49. def remove_from_selection(self, x, y):
  50. self.selection.remove( (x, y) )
  51. self.ui.txt_coords.setText( str(self.selection) )
  52. def update_selected_cells(self):
  53. pass
  54. if __name__ == "__main__":
  55. app = QApplication(sys.argv)
  56. gv = GridViewer()
  57. gv.show()
  58. r = app.exec_()
  59. exit(r)