GridViewer.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. '''
  2. ** By Cro-Ki l@b, 2017 **
  3. '''
  4. from PyQt5.Qt import Qt
  5. from PyQt5.QtCore import QPointF
  6. from PyQt5.QtWidgets import QMainWindow, \
  7. QApplication, QGraphicsScene, QGraphicsView
  8. from GridDialogBox import GridDialogBox
  9. from ListViewDialog import ListViewDialog
  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._init_scene()
  27. self.ui.btn_new_grid.clicked.connect(self.new_grid_dialog)
  28. self.ui.btn_list_view.clicked.connect(self.list_view_dialog)
  29. self.ui.btn_zoom_plus.clicked.connect(self.zoom_plus)
  30. self.ui.btn_zoom_minus.clicked.connect(self.zoom_minus)
  31. self.ui.chk_displayCoords.toggled.connect(self.update_cell_labels)
  32. self.make_grid(SquareGrid(30, 30))
  33. def _init_scene(self):
  34. self._scene = QGraphicsScene()
  35. self._scene.setItemIndexMethod(QGraphicsScene.BspTreeIndex)
  36. self.ui.view.setScene(self._scene)
  37. self.ui.view.scale(0.5, 0.5)
  38. self.ui.view.centerOn(QPointF(0, 0))
  39. self.ui.view.setViewportUpdateMode(QGraphicsView.BoundingRectViewportUpdate)
  40. self.ui.view.setDragMode(QGraphicsView.NoDrag)
  41. self.ui.view.setTransformationAnchor(QGraphicsView.AnchorUnderMouse)
  42. def make_grid(self, grid):
  43. QApplication.setOverrideCursor(Qt.WaitCursor)
  44. self.grid = grid
  45. self.cells = {}
  46. self.selection = []
  47. self._scene.clear()
  48. if len(grid) > 10000:
  49. self.ui.chk_displayCoords.setChecked(False)
  50. for x, y in grid:
  51. cell = GridViewerCell(self, x, y)
  52. cell.generate(grid.geometry.graphicsitem(x, y), show_label=self.ui.chk_displayCoords.isChecked())
  53. self._scene.addItem(cell)
  54. self.cells[(x, y)] = cell
  55. self.ui.view.centerOn(QPointF(0, 0))
  56. QApplication.restoreOverrideCursor()
  57. def add_to_selection(self, x, y):
  58. self.selection.append((x, y))
  59. def remove_from_selection(self, x, y):
  60. self.selection.remove((x, y))
  61. def update_selected_cells(self, new_selection):
  62. if not new_selection != self.selection:
  63. return
  64. QApplication.setOverrideCursor(Qt.WaitCursor)
  65. for x, y in tuple(self.selection):
  66. self.cells[(x, y)].unselect()
  67. for x, y in new_selection:
  68. if (x, y) in self.grid:
  69. self.cells[(x, y)].select()
  70. QApplication.restoreOverrideCursor()
  71. def update_cell_labels(self):
  72. for cell in self.cells.values():
  73. cell.show_label(bool(self.ui.chk_displayCoords.isChecked()))
  74. def zoom_plus(self):
  75. self.ui.view.scale(1.1, 1.1)
  76. def zoom_minus(self):
  77. self.ui.view.scale(0.9, 0.9)
  78. def new_grid_dialog(self):
  79. grid = GridDialogBox.get()
  80. self.make_grid(grid)
  81. def list_view_dialog(self):
  82. new_lst = ListViewDialog(self.selection).exec_()
  83. self.update_selected_cells(new_lst)
  84. def wheelEvent(self, event):
  85. if event.angleDelta().y() > 0:
  86. # self.zoom_plus()
  87. event.accept()
  88. elif event.angleDelta().y() < 0:
  89. # self.zoom_minus()
  90. event.accept()
  91. else:
  92. event.ignore()
  93. def keyPressEvent(self, event):
  94. if event.key() == Qt.Key_Left:
  95. pass
  96. elif event.key() == Qt.Key_Right:
  97. pass
  98. elif event.key() == Qt.Key_Down:
  99. pass
  100. elif event.key() == Qt.Key_Up:
  101. pass