Przeglądaj źródła

Cache sur les emprises

olivier.massot 7 lat temu
rodzic
commit
ddd85abeeb
1 zmienionych plików z 55 dodań i 33 usunięć
  1. 55 33
      Viewer.py

+ 55 - 33
Viewer.py

@@ -7,7 +7,7 @@ from PyQt5.Qt import Qt, QEvent, QGraphicsScene, QPointF, QFileDialog, \
     QApplication, QMessageBox, QTreeWidgetItem, \
     QGraphicsTextItem, QGraphicsItem, QGraphicsRectItem, \
     QBrush, QColor, QGraphicsLineItem, QLineF, \
-    QPen, QPainter, QSvgGenerator, QSize, QRect
+    QPen, QPainter, QSvgGenerator, QSize, QRect, QGraphicsItemGroup
 from PyQt5.QtWidgets import QMainWindow, QGraphicsView
 from path import Path
 
@@ -31,45 +31,50 @@ v_spacing = 120
 cell_width = 150
 cell_spacing = 25
 
-class GraphicsObject(QGraphicsTextItem):
+class GraphicsObject(QGraphicsItemGroup):
     items = []
     def __init__(self, obj, parent=None):
-        super(GraphicsObject, self).__init__("", parent=parent)
+        super(GraphicsObject, self).__init__(parent=parent)
         self.obj = obj
-        self._text = "[{}] <b>{}</b>".format(obj.type_, obj.nom)
+        self._text = "[{}]<br/><b>{}</b>".format(obj.type_, obj.nom)
         self.links = []
         self._x = 0
         self._y = 0
-        self.setHtml(self._text)
 
-        self.setTextWidth(cell_width)
         self.setFlag(QGraphicsItem.ItemIsMovable, True)
         self.setFlag(QGraphicsItem.ItemIsSelectable, True)
         self.setFlag(QGraphicsItem.ItemIsFocusable, True)
 
-        self._addRect()
-        self._addAnchors()
-        GraphicsObject.items.append(self)
+        self.label = QGraphicsTextItem()
+        self.label.setHtml(self._text)
+        self.label.setTextWidth(cell_width)
+        self.label.setZValue(2)
+        self.addToGroup(self.label)
 
-    def _addRect(self):
-        pen = QPen(palette[self.obj.type_])
-        self.rect = QGraphicsRectItem(self.boundingRect(), parent=self)
+        pen = QPen(palette[self.obj.type_].darker(150))
+        pen.setWidth(2)
+        self.rect = QGraphicsRectItem(self.label.boundingRect())
         self.rect.setPen(pen)
+        self.rect.setBrush(palette[self.obj.type_].lighter(150))
+        self.rect.setZValue(0)
+        self.addToGroup(self.rect)
+
+        self.topAnchorCoords = ((self.boundingRect().topLeft().x() + self.boundingRect().topRight().x() / 2), self.boundingRect().topLeft().y())
+        self.bottomAnchorCoords = ((self.boundingRect().topLeft().x() + self.boundingRect().topRight().x() / 2), self.boundingRect().bottomLeft().y())
+        self.top_anchor = QGraphicsRectItem(self.topAnchorCoords[0] - 3, self.topAnchorCoords[1] - 3, 6, 6)
+        self.bottom_anchor = QGraphicsRectItem(self.bottomAnchorCoords[0] - 3, self.bottomAnchorCoords[1] - 3, 6, 6)
+        for anchor in (self.top_anchor, self.bottom_anchor):
+            anchor.setBrush(QBrush(QColor(255, 153, 51)))
+            anchor.setZValue(1)
+            self.addToGroup(anchor)
+
+        GraphicsObject.items.append(self)
 
     def topAnchorCenter(self):
-        return self.mapToScene(QPointF((self.boundingRect().topLeft().x() + self.boundingRect().topRight().x() / 2), self.boundingRect().topLeft().y()))
+        return self.mapToScene(QPointF(*self.topAnchorCoords))
 
     def bottomAnchorCenter(self):
-        return self.mapToScene(QPointF((self.boundingRect().topLeft().x() + self.boundingRect().topRight().x() / 2), self.boundingRect().bottomLeft().y()))
-
-    def _addAnchors(self):
-        x = (self.boundingRect().topLeft().x() + self.boundingRect().topRight().x() / 2)
-        ytop = self.boundingRect().topLeft().y()
-        ybottom = self.boundingRect().bottomLeft().y()
-        self.top_anchor = QGraphicsRectItem(x - 3, ytop - 3, 6, 6, parent=self)
-        self.bottom_anchor = QGraphicsRectItem(x - 3, ybottom - 3, 6, 6, parent=self)
-        self.top_anchor.setBrush(QBrush(QColor(255, 153, 51)))
-        self.bottom_anchor.setBrush(QBrush(QColor(255, 153, 51)))
+        return self.mapToScene(QPointF(*self.bottomAnchorCoords))
 
     def update(self):
         if self.pos() is not QPointF(self._x, self._y):
@@ -88,6 +93,8 @@ class GraphicsRootObject(GraphicsObject):
         self.level = 0
         self.deps = []
         self.refs = []
+        self._dep_emprise = 0
+        self._ref_emprise = 0
 
     def xleft(self):
         return 0
@@ -102,10 +109,14 @@ class GraphicsRootObject(GraphicsObject):
         return max([self.dep_emprise(), self.ref_emprise()])
 
     def dep_emprise(self):
-        return sum([d.dep_emprise() for d in self.deps]) if self.deps else (cell_width + 2 * cell_spacing)
+        if not self._dep_emprise:
+            self._dep_emprise = sum([d.dep_emprise() for d in self.deps]) if self.deps else (cell_width + 2 * cell_spacing)
+        return self._dep_emprise
 
     def ref_emprise(self):
-        return sum([r.ref_emprise() for r in self.refs]) if self.refs else (cell_width + 2 * cell_spacing)
+        if not self._ref_emprise:
+            self._ref_emprise = sum([r.ref_emprise() for r in self.refs]) if self.refs else (cell_width + 2 * cell_spacing)
+        return self._ref_emprise
 
 class GraphicsDepObject(GraphicsObject):
     def __init__(self, obj, parentItem, parent=None):
@@ -115,6 +126,7 @@ class GraphicsDepObject(GraphicsObject):
         if parentItem:
             parentItem.deps.append(self)
         self.level = parentItem.level + 1
+        self._dep_emprise = 0
 
     def xleft(self):
         x0 = sum([n.dep_emprise() for n in self.parentItem.deps[0:self.parentItem.deps.index(self)]])
@@ -122,14 +134,14 @@ class GraphicsDepObject(GraphicsObject):
 
     def compute_coords(self):
         x0 = sum([n.dep_emprise() for n in self.parentItem.deps[0:self.parentItem.deps.index(self)]])
-
         x = self.parentItem.xleft() + (0.5 * self.dep_emprise() - (cell_width / 2 + cell_spacing)) + x0
         y = v_spacing * self.level
-
         return x, y
 
     def dep_emprise(self):
-        return sum([d.dep_emprise() for d in self.deps]) if self.deps else (cell_width + 2 * cell_spacing)
+        if not self._dep_emprise:
+            self._dep_emprise = sum([d.dep_emprise() for d in self.deps]) if self.deps else (cell_width + 2 * cell_spacing)
+        return self._dep_emprise
 
 class GraphicsRefObject(GraphicsObject):
     def __init__(self, obj, childItem, parent=None):
@@ -139,6 +151,7 @@ class GraphicsRefObject(GraphicsObject):
         if childItem:
             childItem.refs.append(self)
         self.level = childItem.level - 1
+        self._ref_emprise = 0
 
     def xleft(self):
         x0 = sum([n.ref_emprise() for n in self.childItem.refs[0:self.childItem.refs.index(self)]])
@@ -149,9 +162,9 @@ class GraphicsRefObject(GraphicsObject):
         return self.childItem.xleft() + (0.5 * self.ref_emprise() - (cell_width / 2 + cell_spacing)) + x0, v_spacing * self.level
 
     def ref_emprise(self):
-        return sum([d.ref_emprise() for d in self.refs]) if self.refs else (cell_width + 2 * cell_spacing)
-
-
+        if not self._ref_emprise:
+            self._ref_emprise = sum([d.ref_emprise() for d in self.refs]) if self.refs else (cell_width + 2 * cell_spacing)
+        return self._ref_emprise
 
 
 class GraphicsLink(QGraphicsLineItem):
@@ -258,7 +271,6 @@ class Viewer(QMainWindow):
     def run(self):
 
         source_dir = QFileDialog.getExistingDirectory(self, "Sélectionner le répertoire des sources", "", QFileDialog.ShowDirsOnly)
-#         source_dir = r"C:\dev\access\AGRHum\source"
 
         self.ui.progressBar.setVisible(True)
         self.ui.lbl_repertoire.setText(source_dir)
@@ -347,7 +359,7 @@ class Viewer(QMainWindow):
         self.maj_scene_with(obj)
 
     def test(self):
-        a = AccessObject("queries", "testA", "")
+        a = AccessObject("queries", "testA_", "")
         b = AccessObject("queries", "testB", "")
         c = AccessObject("tables", "testC", "")
         d = AccessObject("tables", "testD", "")
@@ -368,3 +380,13 @@ class Viewer(QMainWindow):
         a.refs = [j]
 
         self.maj_scene_with(a)
+
+    def keyPressEvent(self, e):
+        if e.key() == Qt.Key_Control:
+            self.ui.view.setDragMode(QGraphicsView.ScrollHandDrag)
+            e.accept()
+
+    def keyReleaseEvent(self, e):
+        if e.key() == Qt.Key_Control:
+            self.ui.view.setDragMode(QGraphicsView.RubberBandDrag)
+