GridViewer.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. '''
  2. ** By Cro-Ki l@b, 2017 **
  3. '''
  4. import sys
  5. from PyQt5.Qt import Qt
  6. from PyQt5.QtCore import QPointF
  7. from PyQt5.QtWidgets import QMainWindow, \
  8. QApplication, QGraphicsScene, QGraphicsView
  9. import ipdb # until I find another way to print traceback with pyqt5
  10. from pypog.grid_objects import SquareGrid
  11. try:
  12. from gridviewer.GridViewerCell import GridViewerCell
  13. from gridviewer.qt_viewer import Ui_window
  14. except ImportError:
  15. from GridViewerCell import GridViewerCell
  16. from qt_viewer import Ui_window
  17. class GridViewer(QMainWindow):
  18. def __init__(self):
  19. super (GridViewer, self).__init__()
  20. self.cells = {}
  21. self.selection = []
  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.5, 0.5)
  29. self.ui.view.centerOn(QPointF(0, 0))
  30. self.ui.view.setDragMode(QGraphicsView.NoDrag)
  31. self.ui.btn_new_grid.clicked.connect(self.new_grid_dialog)
  32. self.ui.btn_run.clicked.connect(self.run_f)
  33. self.ui.btn_list_view.clicked.connect(self.list_view_dialog)
  34. self.ui.btn_zoom_plus.clicked.connect(self.zoom_plus)
  35. self.ui.btn_zoom_minus.clicked.connect(self.zoom_minus)
  36. self.ui.chk_displayCoords.toggled.connect(self.update_cell_labels)
  37. self.make_grid(SquareGrid(30, 30))
  38. def make_grid(self, grid):
  39. QApplication.setOverrideCursor(Qt.WaitCursor)
  40. self.cells = {}
  41. self.selection = []
  42. self._scene.clear()
  43. if len(grid) > 10000:
  44. self.ui.chk_displayCoords.setChecked(False)
  45. for x, y in grid:
  46. cell = GridViewerCell(self, x, y)
  47. cell.generate(grid.geometry.graphicsitem(x, y), show_label=self.ui.chk_displayCoords.isChecked())
  48. self._scene.addItem(cell)
  49. self.cells[(x, y)] = cell
  50. self.ui.view.centerOn(QPointF(0, 0))
  51. QApplication.restoreOverrideCursor()
  52. def add_to_selection(self, x, y):
  53. self.selection.append((x, y))
  54. def remove_from_selection(self, x, y):
  55. self.selection.remove((x, y))
  56. def update_selected_cells(self, new_selection):
  57. QApplication.setOverrideCursor(Qt.WaitCursor)
  58. for x, y in tuple(self.selection):
  59. self.cells[(x, y)].unselect()
  60. for x, y in new_selection:
  61. self.cells[(x, y)].select()
  62. QApplication.restoreOverrideCursor()
  63. def update_cell_labels(self):
  64. for cell in self.cells.values():
  65. cell.show_label(bool(self.ui.chk_displayCoords.isChecked()))
  66. def zoom_plus(self):
  67. self.ui.view.scale(1.1, 1.1)
  68. def zoom_minus(self):
  69. self.ui.view.scale(0.9, 0.9)
  70. def new_grid_dialog(self):
  71. pass
  72. def list_view_dialog(self):
  73. pass
  74. def run_f(self):
  75. pass
  76. if __name__ == "__main__":
  77. app = QApplication(sys.argv)
  78. gv = GridViewer()
  79. gv.show()
  80. r = app.exec_()
  81. exit(r)