GridViewer.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. '''
  2. ** By Cro-Ki l@b, 2017 **
  3. '''
  4. from timeit import timeit
  5. from PyQt5.Qt import Qt, QEvent
  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. self.ui.view.viewport().installEventFilter(self)
  49. def eventFilter(self, obj, event):
  50. if event.type() == QEvent.Wheel:
  51. if event.angleDelta().y() > 0:
  52. self.zoom_plus()
  53. elif event.angleDelta().y() < 0:
  54. self.zoom_minus()
  55. return True
  56. return False
  57. def make_grid(self, grid):
  58. QApplication.setOverrideCursor(Qt.WaitCursor)
  59. self.grid = grid
  60. self.cells = {}
  61. self.selection = []
  62. self._scene.clear()
  63. if len(grid) > 10000:
  64. self.ui.chk_displayCoords.setChecked(False)
  65. for x, y in grid:
  66. cell = GridViewerCell(self, x, y)
  67. cell.generate(grid.geometry.graphicsitem(x, y), show_label=self.ui.chk_displayCoords.isChecked())
  68. self._scene.addItem(cell)
  69. self.cells[(x, y)] = cell
  70. self.ui.view.centerOn(QPointF(0, 0))
  71. self.grid = grid
  72. QApplication.restoreOverrideCursor()
  73. def add_to_selection(self, x, y):
  74. self.selection.append((x, y))
  75. def remove_from_selection(self, x, y):
  76. self.selection.remove((x, y))
  77. def update_selected_cells(self, new_selection):
  78. if not new_selection != self.selection:
  79. return
  80. QApplication.setOverrideCursor(Qt.WaitCursor)
  81. for x, y in tuple(self.selection):
  82. self.cells[(x, y)].unselect()
  83. for x, y in new_selection:
  84. if (x, y) in self.grid:
  85. self.cells[(x, y)].select()
  86. QApplication.restoreOverrideCursor()
  87. def update_cell_labels(self):
  88. for cell in self.cells.values():
  89. cell.show_label(bool(self.ui.chk_displayCoords.isChecked()))
  90. def zoom_plus(self):
  91. self.ui.view.scale(1.1, 1.1)
  92. def zoom_minus(self):
  93. self.ui.view.scale(0.9, 0.9)
  94. def new_grid_dialog(self):
  95. grid = GridDialogBox.get()
  96. self.make_grid(grid)
  97. def list_view_dialog(self):
  98. new_lst = ListViewDialog(self.selection).exec_()
  99. self.update_selected_cells(new_lst)
  100. def job_names(self):
  101. with open("jobs.yml", "r") as f:
  102. jobs = yaml.load(f)
  103. return jobs.keys()
  104. def run_selected_job(self):
  105. self.job_index = 0
  106. self.job_results = self.run_job(self.ui.cb_jobs.currentText())
  107. self.update_stack_job()
  108. def update_stack_job(self):
  109. if not self.job_results:
  110. self.ui.stack_job.setCurrentIndex(0)
  111. return
  112. self.ui.stack_job.setCurrentIndex(1)
  113. self.ui.lbl_job_number.setText("Test {} / {}".format(self.job_index + 1, len(self.job_results)))
  114. gridstr, callstr, result, ittime = self.job_results[self.job_index]
  115. new_grid = eval(gridstr)
  116. if not (new_grid.__class__ == self.grid.__class__ and
  117. new_grid.width == self.grid.width and
  118. new_grid.height == self.grid.height):
  119. self.make_grid(new_grid)
  120. self.ui.txt_job_run.setText(callstr)
  121. self.update_selected_cells(result)
  122. saved = self.saved_result_for(callstr)
  123. if saved:
  124. 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]))
  125. else:
  126. self.ui.lbl_job_exectime.setText("Exec. in {0:.2f} ms.".format(ittime))
  127. def job_next(self):
  128. if self.job_index < (len(self.job_results) - 1):
  129. self.job_index += 1
  130. self.update_stack_job()
  131. def job_previous(self):
  132. if self.job_index > 0:
  133. self.job_index -= 1
  134. self.update_stack_job()
  135. def run_job(self, job_name):
  136. with open("jobs.yml", "r") as f:
  137. jobs = yaml.load(f)
  138. callstrings = [(gridstr, "{}.{}".format(gridstr, funcstr)) for gridstr, calls in jobs[job_name].items() for funcstr in calls]
  139. return [(gridstr, callstr, eval(callstr), self.ittime(callstr)) for gridstr, callstr in callstrings]
  140. def ittime(self, callstr):
  141. """ returns the execution time in milli-seconds
  142. callstr has to be a string
  143. (ex: 'time.sleep(1)', which will return 1000)
  144. """
  145. number, t = 1, 0
  146. while t < 10 ** 8:
  147. t = timeit(lambda: eval(callstr), number=number)
  148. if t >= 0.001:
  149. return 1000 * t / number
  150. number *= 10
  151. else:
  152. return -1
  153. def saved_results(self):
  154. try:
  155. with open("results.yml", "r") as f:
  156. data = yaml.load(f)
  157. return dict(data)
  158. except (FileNotFoundError, TypeError):
  159. return {}
  160. def saved_result_for(self, callstr):
  161. try:
  162. return tuple(self.saved_results()[callstr])
  163. except (TypeError, KeyError):
  164. return None
  165. def job_validate(self):
  166. gridstr, callstr, result, ittime = self.job_results[self.job_index]
  167. data = self.saved_results()
  168. data[callstr] = [gridstr, callstr, str(result), ittime]
  169. with open("results.yml", "w+") as f:
  170. yaml.dump(data, f)
  171. self.update_stack_job()