| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- '''
- ** By Cro-Ki l@b, 2017 **
- '''
- if __name__ == "__main__":
- import os, sys
- pypog_path = (os.path.abspath(".."))
- sys.path.append(pypog_path)
- import time
- from PyQt5.QtCore import QPointF, QMimeData
- from PyQt5.QtWidgets import QMainWindow, \
- QApplication, QGraphicsScene, QGraphicsView, QMessageBox
- import ipdb # until I find another way to print traceback with pyqt5
- from gridviewer.GridViewerCell import GridViewerCell
- from gridviewer.viewer import Ui_window
- from pypog import geometry
- class GridViewer(QMainWindow):
- def __init__(self):
- super (GridViewer, self).__init__()
- self.cells = {}
- self.selection = []
- self.createWidgets()
- def createWidgets(self):
- self.ui = Ui_window()
- self.ui.setupUi(self)
- self._scene = QGraphicsScene()
- self.ui.view.setScene(self._scene)
- self.ui.view.scale(0.5, 0.5)
- self.ui.view.centerOn(QPointF(0, 0))
- self.ui.view.setDragMode(QGraphicsView.NoDrag)
- self.ui.txt_coords.setPlainText("[]")
- self.ui.btn_make.clicked.connect(self.make_grid)
- self.ui.btn_updateSelection.clicked.connect(self.update_selected_cells)
- self.ui.btn_toClipboard.clicked.connect(self.to_clipboard)
- self.ui.btn_zoom_plus.clicked.connect(self.zoom_plus)
- self.ui.btn_zoom_minus.clicked.connect(self.zoom_minus)
- self.ui.txt_stdin.returnPressed.connect(self.run_stdin)
- self.ui.chk_displayCoords.toggled.connect(self.update_cell_labels)
- self.make_grid()
- def make_grid(self):
- self.selection = []
- shape = geometry.FLAT_HEX if self.ui.opt_hex.isChecked() else geometry.SQUARE
- width = self.ui.spb_width.value()
- height = self.ui.spb_height.value()
- kx = 1 if shape == geometry.SQUARE else 0.866
- margin = 240
- cell_height = 120
- self._scene.clear()
- self._scene.setSceneRect(0 - margin, 0 - margin, (kx * cell_height * (width + 2)) + margin, (cell_height * (height + 2)) + margin)
- for x in range(width):
- for y in range(height):
- cell = GridViewerCell(self, x, y)
- cell.generate(shape)
- self._scene.addItem(cell)
- self.cells[(x, y)] = cell
- def add_to_selection(self, x, y):
- self.selection.append((x, y))
- self.ui.txt_coords.setText(str(self.selection))
- def remove_from_selection(self, x, y):
- self.selection.remove((x, y))
- self.ui.txt_coords.setText(str(self.selection))
- def update_selected_cells(self):
- try:
- new_selection = list(eval(self.ui.txt_coords.toPlainText()))
- except SyntaxError:
- QMessageBox.warning(self, "Error", "Invalid string")
- return
- for x, y in tuple(self.selection):
- self.cells[(x, y)].unselect()
- for x, y in new_selection:
- self.cells[(x, y)].select()
- def to_clipboard(self):
- data = QMimeData()
- data.setText(self.ui.txt_coords.toPlainText())
- app.clipboard().setMimeData(data)
- def update_cell_labels(self):
- for cell in self.cells.values():
- cell.show_label(bool(self.ui.chk_displayCoords.isChecked()))
- def zoom_plus(self):
- self.ui.view.scale(1.1, 1.1)
- def zoom_minus(self):
- self.ui.view.scale(0.9, 0.9)
- def run_stdin(self):
- stdin = self.ui.txt_stdin.text()
- try:
- t0 = time.time()
- result = eval(stdin)
- self.ui.txt_coords.setText(str(result))
- self.update_selected_cells()
- self.ui.txt_stdout.setText("{} ms.".format(1000 * (time.time() - t0)))
- except Exception as e:
- self.ui.txt_stdout.setText("{} : {}".format(e.__class__name__, e))
- if __name__ == "__main__":
- app = QApplication(sys.argv)
- gv = GridViewer()
- gv.show()
- r = app.exec_()
- exit(r)
|