Viewer.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. '''
  2. '''
  3. from PyQt5 import uic
  4. from PyQt5.Qt import Qt, QEvent, QGraphicsScene, QPointF, QFileDialog, \
  5. QApplication, QMessageBox, QTreeWidgetItem, \
  6. QGraphicsTextItem, QGraphicsItem, QGraphicsRectItem, \
  7. QBrush, QColor, QGraphicsLineItem, QLineF, \
  8. QPen, QPainter, QSvgGenerator, QSize, QRect, QGraphicsItemGroup, \
  9. QGraphicsColorizeEffect, QFont
  10. from PyQt5.QtWidgets import QMainWindow, QGraphicsView
  11. from path import Path
  12. import core
  13. Ui_window, _ = uic.loadUiType(Path(__file__).parent / r'qt_viewer.ui')
  14. palette = {
  15. "Table": QColor(240, 240, 20),
  16. "Query": QColor(210, 50, 210),
  17. "Module": QColor(220, 10, 33),
  18. "Relation": QColor(122, 50, 209),
  19. "Report": QColor(25, 10, 220),
  20. "Form": QColor(10, 180, 220),
  21. "Macro": QColor(40, 220, 10),
  22. }
  23. v_spacing = 120
  24. cell_width = 150
  25. cell_spacing = 25
  26. class GraphicsObject(QGraphicsItemGroup):
  27. items = []
  28. def __init__(self, obj, parent=None):
  29. super(GraphicsObject, self).__init__(parent=parent)
  30. self.obj = obj
  31. self.links = []
  32. self._x = 0
  33. self._y = 0
  34. self.setFlag(QGraphicsItem.ItemIsMovable, True)
  35. self.setFlag(QGraphicsItem.ItemIsSelectable, True)
  36. self.setFlag(QGraphicsItem.ItemIsFocusable, True)
  37. self.setAcceptHoverEvents(True)
  38. self.label = QGraphicsTextItem()
  39. self.label.setTextWidth(cell_width)
  40. self.label.setZValue(2)
  41. self.setText()
  42. self.addToGroup(self.label)
  43. pen = QPen(palette[self.obj.type_].darker(150))
  44. pen.setWidth(2)
  45. self.rect = QGraphicsRectItem(self.label.boundingRect())
  46. self.rect.setPen(pen)
  47. self.rect.setBrush(palette[self.obj.type_].lighter(150))
  48. self.rect.setZValue(0)
  49. self.addToGroup(self.rect)
  50. self.topAnchorCoords = ((self.boundingRect().topLeft().x() + self.boundingRect().topRight().x() / 2), self.boundingRect().topLeft().y())
  51. self.bottomAnchorCoords = ((self.boundingRect().topLeft().x() + self.boundingRect().topRight().x() / 2), self.boundingRect().bottomLeft().y())
  52. self.top_anchor = QGraphicsRectItem(self.topAnchorCoords[0] - 3, self.topAnchorCoords[1] - 3, 6, 6)
  53. self.bottom_anchor = QGraphicsRectItem(self.bottomAnchorCoords[0] - 3, self.bottomAnchorCoords[1] - 3, 6, 6)
  54. for anchor in (self.top_anchor, self.bottom_anchor):
  55. anchor.setBrush(QBrush(QColor(255, 153, 51)))
  56. anchor.setZValue(1)
  57. self.addToGroup(anchor)
  58. effect = QGraphicsColorizeEffect()
  59. effect.setEnabled(False)
  60. self.rect.setGraphicsEffect(effect)
  61. GraphicsObject.items.append(self)
  62. def setText(self):
  63. self.label.setHtml(self.html())
  64. def html(self):
  65. return "[{}]<br/><b>{}</b>".format(self.obj.type_,
  66. self.obj.nom)
  67. def topAnchorCenter(self):
  68. return self.mapToScene(QPointF(*self.topAnchorCoords))
  69. def bottomAnchorCenter(self):
  70. return self.mapToScene(QPointF(*self.bottomAnchorCoords))
  71. def update(self):
  72. if self.pos() is not QPointF(self._x, self._y):
  73. self.setPos(QPointF(self._x, self._y))
  74. for l in self.links:
  75. l.update()
  76. def paint(self, *args, **kwargs):
  77. super(GraphicsObject, self).paint(*args, **kwargs)
  78. self._x, self._y = self.pos().x(), self.pos().y()
  79. self.update()
  80. def setShining(self, active):
  81. self.rect.graphicsEffect().setEnabled(active)
  82. def hoverEnterEvent(self, *args, **kwargs):
  83. self.setShining(True)
  84. def hoverLeaveEvent(self, *args, **kwargs):
  85. self.setShining(False)
  86. class GraphicsRootObject(GraphicsObject):
  87. def __init__(self, obj, parent=None):
  88. super(GraphicsRootObject, self).__init__(obj, parent=parent)
  89. self.level = 0
  90. self.deps = []
  91. self.refs = []
  92. self._dep_emprise = 0
  93. self._ref_emprise = 0
  94. pen = QPen(QColor("red"))
  95. pen.setWidth(2)
  96. self.rect.setPen(pen)
  97. def xleft(self):
  98. return 0
  99. def compute_coords(self):
  100. return 0, 0
  101. def dep_emprise(self):
  102. if not self._dep_emprise:
  103. self._dep_emprise = sum([d.dep_emprise() for d in self.deps]) if self.deps else (cell_width + 2 * cell_spacing)
  104. return self._dep_emprise
  105. def ref_emprise(self):
  106. if not self._ref_emprise:
  107. self._ref_emprise = sum([r.ref_emprise() for r in self.refs]) if self.refs else (cell_width + 2 * cell_spacing)
  108. return self._ref_emprise
  109. class GraphicsDepObject(GraphicsObject):
  110. def __init__(self, obj, parentItem, parent=None):
  111. super(GraphicsDepObject, self).__init__(obj, parent=parent)
  112. self.deps = []
  113. self.parentItem = parentItem
  114. if parentItem:
  115. parentItem.deps.append(self)
  116. self.level = parentItem.level + 1
  117. self._dep_emprise = 0
  118. def xleft(self):
  119. x0 = sum([n.dep_emprise() for n in self.parentItem.deps[0:self.parentItem.deps.index(self)]])
  120. return self.parentItem.xleft() + x0
  121. def compute_coords(self):
  122. x0 = sum([n.dep_emprise() for n in self.parentItem.deps[0:self.parentItem.deps.index(self)]])
  123. x = self.parentItem.xleft() + (0.5 * self.dep_emprise() - (cell_width / 2 + cell_spacing)) + x0
  124. y = v_spacing * self.level
  125. return x, y
  126. def dep_emprise(self):
  127. if not self._dep_emprise:
  128. self._dep_emprise = sum([d.dep_emprise() for d in self.deps]) if self.deps else (cell_width + 2 * cell_spacing)
  129. return self._dep_emprise
  130. class GraphicsCloneDepObject(GraphicsDepObject):
  131. def __init__(self, obj, childItem, parent=None):
  132. super(GraphicsCloneDepObject, self).__init__(obj, childItem, parent)
  133. self.bottom_anchor.setBrush(QColor("red"))
  134. self.clone_of = next((item for item in GraphicsObject.items if item.obj == obj
  135. and type(item) is GraphicsDepObject
  136. or type(item) is GraphicsRefObject))
  137. def html(self):
  138. return super(GraphicsCloneDepObject, self).html() + "<br/>(!) Clône"
  139. @property
  140. def deps(self):
  141. return []
  142. @deps.setter
  143. def deps(self, value):
  144. pass
  145. def hoverEnterEvent(self, *args, **kwargs):
  146. self.setShining(True)
  147. self.clone_of.setShining(True)
  148. def hoverLeaveEvent(self, *args, **kwargs):
  149. self.setShining(False)
  150. self.clone_of.setShining(False)
  151. class GraphicsRefObject(GraphicsObject):
  152. def __init__(self, obj, childItem, parent=None):
  153. super(GraphicsRefObject, self).__init__(obj, parent=parent)
  154. self.refs = []
  155. self.childItem = childItem
  156. if childItem:
  157. childItem.refs.append(self)
  158. self.level = childItem.level - 1
  159. self._ref_emprise = 0
  160. def xleft(self):
  161. x0 = sum([n.ref_emprise() for n in self.childItem.refs[0:self.childItem.refs.index(self)]])
  162. return self.childItem.xleft() + x0
  163. def compute_coords(self):
  164. x0 = sum([n.ref_emprise() for n in self.childItem.refs[0:self.childItem.refs.index(self)]])
  165. return self.childItem.xleft() + (0.5 * self.ref_emprise() - (cell_width / 2 + cell_spacing)) + x0, v_spacing * self.level
  166. def ref_emprise(self):
  167. if not self._ref_emprise:
  168. self._ref_emprise = sum([d.ref_emprise() for d in self.refs]) if self.refs else (cell_width + 2 * cell_spacing)
  169. return self._ref_emprise
  170. class GraphicsCloneRefObject(GraphicsRefObject):
  171. def __init__(self, obj, childItem, parent=None):
  172. super(GraphicsCloneRefObject, self).__init__(obj, childItem, parent)
  173. self.top_anchor.setBrush(QColor("red"))
  174. self.clone_of = next((item for item in GraphicsObject.items if item.obj == obj
  175. and type(item) is GraphicsDepObject
  176. or type(item) is GraphicsRefObject))
  177. def html(self):
  178. return super(GraphicsCloneRefObject, self).html() + "<br/>(!) Clône"
  179. @property
  180. def refs(self):
  181. return []
  182. @refs.setter
  183. def refs(self, value):
  184. pass
  185. def hoverEnterEvent(self, *args, **kwargs):
  186. self.clone_of.setShining(True)
  187. def hoverLeaveEvent(self, *args, **kwargs):
  188. self.clone_of.setShining(False)
  189. class GraphicsLink(QGraphicsLineItem):
  190. items = []
  191. def __init__(self, topGraphicsObject, bottomGraphicsObject, parent=None):
  192. self.topGraphicsObject = topGraphicsObject
  193. self.topGraphicsObject.links.append(self)
  194. self.bottomGraphicsObject = bottomGraphicsObject
  195. self.bottomGraphicsObject.links.append(self)
  196. super(QGraphicsLineItem, self).__init__(parent=parent)
  197. self.update()
  198. def update(self):
  199. line = QLineF(self.mapToScene(self.topGraphicsObject.bottomAnchorCenter()), self.mapToScene(self.bottomGraphicsObject.topAnchorCenter()))
  200. self.setLine(line)
  201. class Viewer(QMainWindow):
  202. def __init__(self):
  203. super (Viewer, self).__init__()
  204. self.createWidgets()
  205. def createWidgets(self):
  206. self.ui = Ui_window()
  207. self.ui.setupUi(self)
  208. self.ui.progressBar.setVisible(False)
  209. self.ui.stackedWidget.setCurrentIndex(0)
  210. self.ui.btn_test.clicked.connect(self.test)
  211. self.ui.btn_select.clicked.connect(self.selectSourceDir)
  212. self.ui.btn_zoom_plus.clicked.connect(self.zoom_plus)
  213. self.ui.btn_zoom_minus.clicked.connect(self.zoom_minus)
  214. self.ui.btn_zoom_view.clicked.connect(self.fit_in_view)
  215. self.ui.btn_svg.clicked.connect(self.to_png)
  216. self.ui.treeWidget.itemClicked.connect(self.treeItemSelected)
  217. self._title = "<unknown>"
  218. core.Analyse.report = self.update_progression
  219. self.ui.view.setViewportUpdateMode(QGraphicsView.BoundingRectViewportUpdate)
  220. self.ui.view.setTransformationAnchor(QGraphicsView.AnchorUnderMouse)
  221. self.ui.view.viewport().installEventFilter(self)
  222. self._scene = QGraphicsScene()
  223. self._scene.setItemIndexMethod(QGraphicsScene.BspTreeIndex)
  224. self.ui.view.setScene(self._scene)
  225. helpLabel = QGraphicsTextItem()
  226. helpLabel.setPlainText("Sélectionnez le répertoire contenant les sources de la base à analyser,\npuis sélectionnez dans la liste de droite un objet à partir duquel afficher les dépendances...")
  227. helpLabel.setTextWidth(600)
  228. font = QFont()
  229. font.setItalic(True)
  230. font.setWeight(63)
  231. helpLabel.setFont(font)
  232. self._scene.addItem(helpLabel)
  233. def eventFilter(self, _, event):
  234. if event.type() == QEvent.Wheel:
  235. if event.angleDelta().y() > 0:
  236. self.zoom_plus()
  237. elif event.angleDelta().y() < 0:
  238. self.zoom_minus()
  239. return True
  240. return False
  241. def fit_in_view(self):
  242. rect = self._scene.itemsBoundingRect()
  243. rect.adjust(-50, -50, 50, 50)
  244. self._scene.setSceneRect(rect)
  245. self.ui.view.fitInView(self._scene.sceneRect(), Qt.KeepAspectRatio)
  246. def zoom_plus(self):
  247. self.ui.view.scale(1.2, 1.2)
  248. def zoom_minus(self):
  249. self.ui.view.scale(0.8, 0.8)
  250. def to_png(self):
  251. fileName, _ = QFileDialog.getSaveFileName(self, "Save File", "", "Svg File (*.svg)")
  252. if not fileName:
  253. return
  254. gen = QSvgGenerator()
  255. gen.setFileName(fileName)
  256. gen.setSize(QSize(1000, 1000))
  257. gen.setViewBox(QRect(0, 0, 1000, 1000))
  258. gen.setTitle("Access Analyser")
  259. gen.setDescription("Access Analyser Report for {}".format(self._title))
  260. painter = QPainter(gen)
  261. self._scene.render(painter)
  262. painter.end()
  263. def update_progression(self, i, total, msg=""):
  264. self.ui.progressBar.setMaximum(total)
  265. self.ui.progressBar.setValue(i)
  266. if msg:
  267. self.ui.txtPanel.append(msg)
  268. QApplication.processEvents()
  269. def selectSourceDir(self):
  270. source_dir = QFileDialog.getExistingDirectory(self, "Sélectionner le répertoire des sources", "", QFileDialog.ShowDirsOnly)
  271. if not source_dir:
  272. return
  273. self.run(source_dir)
  274. def run(self, source_dir):
  275. source_dir = Path(source_dir)
  276. self._title = source_dir
  277. self.ui.progressBar.setVisible(True)
  278. self.ui.lbl_repertoire.setText(source_dir)
  279. self.ui.txtPanel.clear()
  280. self.ui.treeWidget.clear()
  281. if self.ui.radioRefsOnly.isChecked():
  282. mode = core.REFS_ONLY
  283. elif self.ui.radioDepsOnly.isChecked():
  284. mode = core.DEPS_ONLY
  285. else:
  286. mode = core.DEPS_AND_REFS
  287. print(mode)
  288. QApplication.setOverrideCursor(Qt.WaitCursor)
  289. core.Analyse.run(source_dir, mode)
  290. QApplication.restoreOverrideCursor()
  291. if core.Analyse.duplicated_names:
  292. QMessageBox.warning(self, "Risque d'instabilités", "Attention! Des doublons ont été trouvés dans les noms des objets suivants:\n{}".format(", ".join(core.Analyse.duplicated_names)))
  293. QMessageBox.information(self, "test", "{} objets chargés".format(len(core.Analyse.objects)))
  294. self.ui.progressBar.setVisible(False)
  295. self.ui.stackedWidget.setCurrentIndex(1)
  296. self.ui.txtPanel.clear()
  297. topitem = QTreeWidgetItem()
  298. topitem.setText(0, "Objets")
  299. self.ui.treeWidget.addTopLevelItem(topitem)
  300. groupes = {}
  301. for index, obj in enumerate(core.Analyse.objects):
  302. if not obj.type_ in groupes:
  303. item = QTreeWidgetItem()
  304. item.setText(0, obj.type_)
  305. groupes[obj.type_] = item
  306. topitem.addChild(item)
  307. item = QTreeWidgetItem()
  308. item.setText(0, obj.nom)
  309. item.setData(1, 0, index)
  310. groupes[obj.type_].addChild(item)
  311. self.ui.treeWidget.setColumnHidden(1, True)
  312. self.ui.treeWidget.expandToDepth(1)
  313. def clear_scene(self):
  314. self._scene.clear()
  315. GraphicsObject.items = []
  316. def maj_scene_with(self, root_object):
  317. self.clear_scene()
  318. root = GraphicsRootObject(root_object)
  319. self._scene.addItem(root)
  320. def _addDeps(go):
  321. for dep in go.obj.deps:
  322. if dep not in [go.obj for go in GraphicsObject.items]:
  323. gdep = GraphicsDepObject(dep, go)
  324. else:
  325. gdep = GraphicsCloneDepObject(dep, go)
  326. self._scene.addItem(gdep)
  327. link = GraphicsLink(go, gdep)
  328. self._scene.addItem(link)
  329. if dep.deps:
  330. _addDeps(gdep)
  331. _addDeps(root)
  332. def _addRefs(go):
  333. for ref in go.obj.refs:
  334. if ref not in [go.obj for go in GraphicsObject.items]:
  335. gref = GraphicsRefObject(ref, go)
  336. else:
  337. gref = GraphicsCloneRefObject(ref, go)
  338. self._scene.addItem(gref)
  339. link = GraphicsLink(gref, go)
  340. self._scene.addItem(link)
  341. if ref.refs:
  342. _addRefs(gref)
  343. _addRefs(root)
  344. for item in GraphicsObject.items:
  345. item._x, item._y = item.compute_coords()
  346. if isinstance(item, GraphicsDepObject):
  347. item._x -= (root.dep_emprise() - (cell_width + 2 * cell_spacing)) / 2
  348. if isinstance(item, GraphicsRefObject):
  349. item._x -= (root.ref_emprise() - (cell_width + 2 * cell_spacing)) / 2
  350. item.update()
  351. self.ui.btn_save.setEnabled(True)
  352. self.fit_in_view()
  353. def treeItemSelected(self, item):
  354. index = item.data(1, 0)
  355. if not index:
  356. return
  357. obj = core.Analyse.objects[index]
  358. self.maj_scene_with(obj)
  359. def test(self):
  360. self.run(Path(__file__).parent / r"test\source")
  361. def keyPressEvent(self, e):
  362. if e.key() == Qt.Key_Control:
  363. self.ui.view.setDragMode(QGraphicsView.ScrollHandDrag)
  364. e.accept()
  365. def keyReleaseEvent(self, e):
  366. if e.key() == Qt.Key_Control:
  367. self.ui.view.setDragMode(QGraphicsView.RubberBandDrag)