GridViewer.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. '''
  2. ** By Cro-Ki l@b, 2017 **
  3. '''
  4. from timeit import timeit
  5. from PyQt5.Qt import Qt
  6. from PyQt5.QtCore import QPointF
  7. from PyQt5.QtWidgets import QMainWindow, \
  8. QApplication, QGraphicsScene, QGraphicsView
  9. import yaml
  10. from GridDialogBox import GridDialogBox
  11. from GridViewerCell import GridViewerCell
  12. from ListViewDialog import ListViewDialog
  13. from pypog.grid_objects import SquareGrid, FHexGrid
  14. from qt_viewer import Ui_window
  15. class GridViewer(QMainWindow):
  16. def __init__(self):
  17. super (GridViewer, self).__init__()
  18. self.cells = {}
  19. self.selection = []
  20. self.job_index = 0
  21. self.job_results = []
  22. self.createWidgets()
  23. def createWidgets(self):
  24. self.ui = Ui_window()
  25. self.ui.setupUi(self)
  26. self._init_scene()
  27. self.ui.btn_new_grid.clicked.connect(self.new_grid_dialog)
  28. self.ui.btn_list_view.clicked.connect(self.list_view_dialog)
  29. self.ui.btn_zoom_plus.clicked.connect(self.zoom_plus)
  30. self.ui.btn_zoom_minus.clicked.connect(self.zoom_minus)
  31. self.ui.chk_displayCoords.toggled.connect(self.update_cell_labels)
  32. self.ui.cb_jobs.insertItems(0, self.job_names())
  33. self.update_stack_job()
  34. self.ui.btn_run_job.clicked.connect(self.run_selected_job)
  35. self.ui.btn_job_next.clicked.connect(self.job_next)
  36. self.ui.btn_job_previous.clicked.connect(self.job_previous)
  37. self.ui.btn_job_validate.clicked.connect(self.job_validate)
  38. self.make_grid(SquareGrid(30, 30))
  39. def _init_scene(self):
  40. self._scene = QGraphicsScene()
  41. self._scene.setItemIndexMethod(QGraphicsScene.BspTreeIndex)
  42. self.ui.view.setScene(self._scene)
  43. self.ui.view.scale(0.5, 0.5)
  44. self.ui.view.centerOn(QPointF(0, 0))
  45. self.ui.view.setViewportUpdateMode(QGraphicsView.BoundingRectViewportUpdate)
  46. self.ui.view.setDragMode(QGraphicsView.NoDrag)
  47. self.ui.view.setTransformationAnchor(QGraphicsView.AnchorUnderMouse)
  48. def make_grid(self, grid):
  49. QApplication.setOverrideCursor(Qt.WaitCursor)
  50. self.grid = grid
  51. self.cells = {}
  52. self.selection = []
  53. self._scene.clear()
  54. if len(grid) > 10000:
  55. self.ui.chk_displayCoords.setChecked(False)
  56. for x, y in grid:
  57. cell = GridViewerCell(self, x, y)
  58. cell.generate(grid.geometry.graphicsitem(x, y), show_label=self.ui.chk_displayCoords.isChecked())
  59. self._scene.addItem(cell)
  60. self.cells[(x, y)] = cell
  61. self.ui.view.centerOn(QPointF(0, 0))
  62. self.grid = grid
  63. QApplication.restoreOverrideCursor()
  64. def add_to_selection(self, x, y):
  65. self.selection.append((x, y))
  66. def remove_from_selection(self, x, y):
  67. self.selection.remove((x, y))
  68. def update_selected_cells(self, new_selection):
  69. if not new_selection != self.selection:
  70. return
  71. QApplication.setOverrideCursor(Qt.WaitCursor)
  72. for x, y in tuple(self.selection):
  73. self.cells[(x, y)].unselect()
  74. for x, y in new_selection:
  75. if (x, y) in self.grid:
  76. self.cells[(x, y)].select()
  77. QApplication.restoreOverrideCursor()
  78. def update_cell_labels(self):
  79. for cell in self.cells.values():
  80. cell.show_label(bool(self.ui.chk_displayCoords.isChecked()))
  81. def zoom_plus(self):
  82. self.ui.view.scale(1.1, 1.1)
  83. def zoom_minus(self):
  84. self.ui.view.scale(0.9, 0.9)
  85. def new_grid_dialog(self):
  86. grid = GridDialogBox.get()
  87. self.make_grid(grid)
  88. def list_view_dialog(self):
  89. new_lst = ListViewDialog(self.selection).exec_()
  90. self.update_selected_cells(new_lst)
  91. def wheelEvent(self, event):
  92. if event.angleDelta().y() > 0:
  93. # self.zoom_plus()
  94. event.accept()
  95. elif event.angleDelta().y() < 0:
  96. # self.zoom_minus()
  97. event.accept()
  98. else:
  99. event.ignore()
  100. def keyPressEvent(self, event):
  101. if event.key() == Qt.Key_Left:
  102. pass
  103. elif event.key() == Qt.Key_Right:
  104. pass
  105. elif event.key() == Qt.Key_Down:
  106. pass
  107. elif event.key() == Qt.Key_Up:
  108. pass
  109. def job_names(self):
  110. with open("jobs.yml", "r") as f:
  111. jobs = yaml.load(f)
  112. return jobs.keys()
  113. def run_selected_job(self):
  114. self.job_index = 0
  115. self.job_results = self.run_job(self.ui.cb_jobs.currentText())
  116. self.update_stack_job()
  117. def update_stack_job(self):
  118. if not self.job_results:
  119. self.ui.stack_job.setCurrentIndex(0)
  120. return
  121. self.ui.stack_job.setCurrentIndex(1)
  122. self.ui.lbl_job_number.setText("Test {} / {}".format(self.job_index + 1, len(self.job_results)))
  123. gridstr, callstr, result, ittime = self.job_results[self.job_index]
  124. new_grid = eval(gridstr)
  125. if not (new_grid.__class__ == self.grid.__class__ and
  126. new_grid.width == self.grid.width and
  127. new_grid.height == self.grid.height):
  128. self.make_grid(new_grid)
  129. self.ui.txt_job_run.setText(callstr)
  130. self.update_selected_cells(result)
  131. saved = self.saved_result_for(callstr)
  132. if saved:
  133. self.ui.lbl_job_exectime.setText("Exec. in {0:.2f} ms. / Saved: {1:.2f} ms. / Same result: {2:}".format(ittime, saved[3], str(result) == saved[2]))
  134. else:
  135. self.ui.lbl_job_exectime.setText("Exec. in {0:.2f} ms.".format(ittime))
  136. def job_next(self):
  137. if self.job_index < (len(self.job_results) - 1):
  138. self.job_index += 1
  139. self.update_stack_job()
  140. def job_previous(self):
  141. if self.job_index > 0:
  142. self.job_index -= 1
  143. self.update_stack_job()
  144. def run_job(self, job_name):
  145. with open("jobs.yml", "r") as f:
  146. jobs = yaml.load(f)
  147. callstrings = [(gridstr, "{}.{}".format(gridstr, funcstr)) for gridstr, calls in jobs[job_name].items() for funcstr in calls]
  148. return [(gridstr, callstr, eval(callstr), self.ittime(callstr)) for gridstr, callstr in callstrings]
  149. def ittime(self, callstr):
  150. """ returns the execution time in milli-seconds
  151. callstr has to be a string
  152. (ex: 'time.sleep(1)', which will return 1000)
  153. """
  154. number, t = 1, 0
  155. while t < 10 ** 8:
  156. t = timeit(lambda: eval(callstr), number=number)
  157. if t >= 0.001:
  158. return 1000 * t / number
  159. number *= 10
  160. else:
  161. return -1
  162. def saved_results(self):
  163. try:
  164. with open("results.yml", "r") as f:
  165. data = yaml.load(f)
  166. return dict(data)
  167. except (FileNotFoundError, TypeError):
  168. return {}
  169. def saved_result_for(self, callstr):
  170. try:
  171. return tuple(self.saved_results()[callstr])
  172. except (TypeError, KeyError):
  173. return None
  174. def job_validate(self):
  175. gridstr, callstr, result, ittime = self.job_results[self.job_index]
  176. data = self.saved_results()
  177. data[callstr] = [gridstr, callstr, str(result), ittime]
  178. with open("results.yml", "w+") as f:
  179. yaml.dump(data, f)
  180. self.update_stack_job()