GridViewer.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. '''
  2. ** By Cro-Ki l@b, 2017 **
  3. '''
  4. from PyQt5.QtCore import QPointF, QMimeData
  5. from PyQt5.QtWidgets import QMainWindow, \
  6. QApplication, QGraphicsScene, QGraphicsView, QMessageBox
  7. import ipdb # until I find another way to print traceback with pyqt5
  8. from gridviewer.GridViewerCell import GridViewerCell
  9. from gridviewer.viewer import Ui_window
  10. from pypog import geometry
  11. if __name__ == "__main__":
  12. import os, sys
  13. pypog_path = (os.path.abspath("..\\..\\"))
  14. sys.path.append(pypog_path)
  15. class GridViewer(QMainWindow):
  16. def __init__(self):
  17. super (GridViewer, self).__init__()
  18. self.cells = {}
  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.5, 0.5)
  27. self.ui.view.centerOn(QPointF(0, 0))
  28. self.ui.view.setDragMode(QGraphicsView.NoDrag)
  29. self.ui.txt_coords.setPlainText("[]")
  30. self.ui.btn_make.clicked.connect(self.make_grid)
  31. self.ui.btn_updateSelection.clicked.connect(self.update_selected_cells)
  32. self.ui.btn_toClipboard.clicked.connect(self.to_clipboard)
  33. self.ui.btn_zoom_plus.clicked.connect(self.zoom_plus)
  34. self.ui.btn_zoom_minus.clicked.connect(self.zoom_minus)
  35. self.ui.chk_displayCoords.toggled.connect(self.update_cell_labels)
  36. self.make_grid()
  37. def make_grid(self):
  38. self.selection = []
  39. shape = geometry.FLAT_HEX if self.ui.opt_hex.isChecked() else geometry.SQUARE
  40. width = self.ui.spb_width.value()
  41. height = self.ui.spb_height.value()
  42. kx = 1 if shape == geometry.SQUARE else 0.866
  43. margin = 240
  44. cell_height = 120
  45. self._scene.clear()
  46. self._scene.setSceneRect(0 - margin, 0 - margin, (kx * cell_height * (width + 2)) + margin, (cell_height * (height + 2)) + margin)
  47. for x in range(width):
  48. for y in range(height):
  49. cell = GridViewerCell(self, x, y)
  50. cell.generate(shape)
  51. self._scene.addItem(cell)
  52. self.cells[(x, y)] = cell
  53. def add_to_selection(self, x, y):
  54. self.selection.append((x, y))
  55. self.ui.txt_coords.setText(str(self.selection))
  56. def remove_from_selection(self, x, y):
  57. self.selection.remove((x, y))
  58. self.ui.txt_coords.setText(str(self.selection))
  59. def update_selected_cells(self):
  60. try:
  61. new_selection = list(eval(self.ui.txt_coords.toPlainText()))
  62. except SyntaxError:
  63. QMessageBox.warning(self, "Error", "Invalid string")
  64. return
  65. for x, y in tuple(self.selection):
  66. self.cells[(x, y)].unselect()
  67. for x, y in new_selection:
  68. self.cells[(x, y)].select()
  69. def to_clipboard(self):
  70. data = QMimeData()
  71. data.setText(self.ui.txt_coords.toPlainText())
  72. app.clipboard().setMimeData(data)
  73. def update_cell_labels(self):
  74. for cell in self.cells.values():
  75. cell.show_label(bool(self.ui.chk_displayCoords.isChecked()))
  76. def zoom_plus(self):
  77. self.ui.view.scale(1.1, 1.1)
  78. def zoom_minus(self):
  79. self.ui.view.scale(0.9, 0.9)
  80. if __name__ == "__main__":
  81. app = QApplication(sys.argv)
  82. gv = GridViewer()
  83. gv.show()
  84. r = app.exec_()
  85. exit(r)