Viewer.py 16 KB

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