GridViewer.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 gridviewer.GridDialogBox import GridDialogBox
  11. from gridviewer.ListViewDialog import ListViewDialog
  12. from pypog.grid_objects import SquareGrid
  13. try:
  14. from gridviewer.GridViewerCell import GridViewerCell
  15. from gridviewer.qt_viewer import Ui_window
  16. except ImportError:
  17. from GridViewerCell import GridViewerCell
  18. from qt_viewer import Ui_window
  19. class GridViewer(QMainWindow):
  20. def __init__(self):
  21. super (GridViewer, self).__init__()
  22. self.cells = {}
  23. self.selection = []
  24. self.createWidgets()
  25. def createWidgets(self):
  26. self.ui = Ui_window()
  27. self.ui.setupUi(self)
  28. self._scene = QGraphicsScene()
  29. self.ui.view.setScene(self._scene)
  30. self.ui.view.scale(0.5, 0.5)
  31. self.ui.view.centerOn(QPointF(0, 0))
  32. self.ui.view.setDragMode(QGraphicsView.NoDrag)
  33. self.ui.btn_new_grid.clicked.connect(self.new_grid_dialog)
  34. self.ui.btn_run.clicked.connect(self.run_f)
  35. self.ui.btn_list_view.clicked.connect(self.list_view_dialog)
  36. self.ui.btn_zoom_plus.clicked.connect(self.zoom_plus)
  37. self.ui.btn_zoom_minus.clicked.connect(self.zoom_minus)
  38. self.ui.chk_displayCoords.toggled.connect(self.update_cell_labels)
  39. self.make_grid(SquareGrid(30, 30))
  40. def make_grid(self, grid):
  41. QApplication.setOverrideCursor(Qt.WaitCursor)
  42. self.grid = grid
  43. self.cells = {}
  44. self.selection = []
  45. self._scene.clear()
  46. if len(grid) > 10000:
  47. self.ui.chk_displayCoords.setChecked(False)
  48. for x, y in grid:
  49. cell = GridViewerCell(self, x, y)
  50. cell.generate(grid.geometry.graphicsitem(x, y), show_label=self.ui.chk_displayCoords.isChecked())
  51. self._scene.addItem(cell)
  52. self.cells[(x, y)] = cell
  53. self.ui.view.centerOn(QPointF(0, 0))
  54. QApplication.restoreOverrideCursor()
  55. def add_to_selection(self, x, y):
  56. self.selection.append((x, y))
  57. def remove_from_selection(self, x, y):
  58. self.selection.remove((x, y))
  59. def update_selected_cells(self, new_selection):
  60. if not new_selection != self.selection:
  61. return
  62. QApplication.setOverrideCursor(Qt.WaitCursor)
  63. for x, y in tuple(self.selection):
  64. self.cells[(x, y)].unselect()
  65. for x, y in new_selection:
  66. if (x, y) in self.grid:
  67. self.cells[(x, y)].select()
  68. QApplication.restoreOverrideCursor()
  69. def update_cell_labels(self):
  70. for cell in self.cells.values():
  71. cell.show_label(bool(self.ui.chk_displayCoords.isChecked()))
  72. def zoom_plus(self):
  73. self.ui.view.scale(1.1, 1.1)
  74. def zoom_minus(self):
  75. self.ui.view.scale(0.9, 0.9)
  76. def new_grid_dialog(self):
  77. grid = GridDialogBox.get()
  78. self.make_grid(grid)
  79. def list_view_dialog(self):
  80. new_lst = ListViewDialog(self.selection).exec_()
  81. self.update_selected_cells(new_lst)
  82. def run_f(self):
  83. pass
  84. if __name__ == "__main__":
  85. app = QApplication(sys.argv)
  86. gv = GridViewer()
  87. gv.show()
  88. r = app.exec_()
  89. exit(r)