فهرست منبع

replaced hack by isinstance, nicer drawing for edges

Lucas Heer 7 سال پیش
والد
کامیت
896b043aea
4فایلهای تغییر یافته به همراه20 افزوده شده و 15 حذف شده
  1. 1 4
      sketchUI/exm_scene.py
  2. 12 6
      sketchUI/graphics_edge_item.py
  3. 6 1
      sketchUI/graphics_node_item.py
  4. 1 4
      sketchUI/im_scene.py

+ 1 - 4
sketchUI/exm_scene.py

@@ -160,11 +160,8 @@ class SketchScene(QGraphicsScene):
                 if not item:
                     return
                 for item in self.items():
-                    try:
-                        item.__hack__()  # hack to check if item is of edge type
+                    if isinstance(item, GraphicsEdgeItem):
                         item.redraw()
-                    except AttributeError:
-                        continue
 
             elif self._mode == Mode.CONNECT:
                 item = self.itemAt(event.scenePos(), QTransform())

+ 12 - 6
sketchUI/graphics_edge_item.py

@@ -1,24 +1,30 @@
 """
 Edge representation for Qt: Same as a LineItem, but stores items it is connected to
+as well as the modelverse edge id.
 """
 
 from PyQt5.QtWidgets import QGraphicsItem, QGraphicsLineItem
-
+from sketchUI.graphics_node_item import GraphicsNodeItem
 
 class GraphicsEdgeItem(QGraphicsLineItem):
     def __init__(self, from_item, to_item, edge_id=None):
+        #type: (GraphicsNodeItem, GraphicsNodeItem) -> None
         QGraphicsLineItem.__init__(self)
         self.from_item = from_item
         self.to_item = to_item
         self.edge_id = edge_id
         self.setFlag(QGraphicsItem.ItemIsMovable, False)
-        self.setFlag(QGraphicsItem.ItemIsSelectable, False)
+        self.setFlag(QGraphicsItem.ItemIsSelectable, True)
+
+        self.setZValue(-1.0)
 
     def redraw(self):
         from_pos = self.from_item.scenePos()
+        from_brect = self.from_item.boundingRect()
         to_pos = self.to_item.scenePos()
-        self.setLine(from_pos.x(), from_pos.y(), to_pos.x(), to_pos.y())
+        to_brec = self.to_item.boundingRect()
 
-    # hack to work around failing isinstance check due to import chaos
-    def __hack__(self):
-        return None
+        self.setLine(from_pos.x() + int(from_brect.width()/2.0),
+                     from_pos.y() + int(from_brect.height()/2.0),
+                     to_pos.x() + int(to_brec.width()/2.0),
+                     to_pos.y() + int(to_brec.height()/2.0))

+ 6 - 1
sketchUI/graphics_node_item.py

@@ -6,7 +6,7 @@ an image file or a set of primitives.
 
 from enum import Enum
 from PyQt5.QtWidgets import QGraphicsItem, QStyleOptionGraphicsItem, QWidget
-from PyQt5.Qt import QRectF, QPainter, QFont, Qt
+from PyQt5.Qt import QRectF, QPainter, QFont, Qt, QBrush, QColor
 
 
 class IconType(Enum):
@@ -25,6 +25,8 @@ class GraphicsNodeItem(QGraphicsItem):
         self._font = QFont()
         self._font.setPixelSize(12)
 
+        self.setZValue(1.0)
+
     def set_type(self, node_type):
         self._type = node_type
         self.update(self.boundingRect())
@@ -44,10 +46,13 @@ class GraphicsNodeItem(QGraphicsItem):
         # type: (QPainter, QStyleOptionGraphicsItem, QWidget) -> None
         # draw type text and bounding rectangle
         painter.setFont(self._font)
+
+        painter.setBrush(QBrush(Qt.white))
         painter.drawRect(self.boundingRect())
 
         text_box = QRectF(0, 100, 120, 20)
         painter.drawRect(text_box)
+        painter.setBrush(QBrush(QColor(0, 0, 0, 0)))
         painter.drawText(text_box, Qt.AlignCenter | Qt.TextDontClip, self._type)
 
         # draw concrete syntax

+ 1 - 4
sketchUI/im_scene.py

@@ -72,11 +72,8 @@ class CustomScene(QGraphicsScene):
             if not item:
                 return
             for item in self.items():
-                try:
-                    item.__hack__()  # hack to check if item is of edge type
+                if isinstance(item, GraphicsEdgeItem):
                     item.redraw()
-                except AttributeError:
-                    continue
 
         QGraphicsScene.mouseReleaseEvent(self, event)