GridViewer.py 3.9 KB

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