GridViewer.py 3.6 KB

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