Ejemplo n.º 1
0
 def handleAtPos(self, pos):
     """Is there an interactive handle where the mouse is?"""
     if not self.complete:
         path = QPainterPath()
         path.addEllipse(self.data['points'][-1], self.radius, self.radius)
         return path.contains(pos)
Ejemplo n.º 2
0
class PlugItem(QGraphicsPathItem):
    """Graphical wrapper around the engine Plug class."""

    bodyW = 30
    """The width of the body of plugs."""
    pinW = 10
    """The width of the pin part of plugs."""

    def __init__(self, plug):
        super(PlugItem, self).__init__()
        self.data = plug
        """The real info. The class PlugItem is just a graphical container
        around it. data is saved / loaded to / from file.
        """
        self.showName = False
        """Is the name of the item shown on screen?"""
        self.setFlag(QGraphicsItem.ItemIsMovable)
        self.setFlag(QGraphicsItem.ItemIsSelectable)
        self.setFlag(QGraphicsItem.ItemSendsGeometryChanges)
        self.setAcceptsHoverEvents(True)
        self.setPen(QPen(QBrush(QColor(QColor('black'))), 2))
        # This path is needed at each mouse over event, to check if
        # the mouse is over a pin. We save it as an instance field,
        # rather than recreate it at each event.
        self.pinPath = QPainterPath()
        if self.data.isInput:
            self.pinPath.addEllipse(
                self.bodyW + self.pinW / 2, self.bodyW / 2,
                self.pinW, self.pinW)
        else:
            self.pinPath.addEllipse(
                self.pinW / 2, self.bodyW / 2, self.pinW, self.pinW)
        f = QFont('Times', 12, 75)
        # Name and value text labels.
        self.name = QGraphicsSimpleTextItem(self)
        # that won't rotate when the PlugItem is rotated by the user.
        self.name.setFlag(QGraphicsItem.ItemIgnoresTransformations)
        self.name.setText(self.data.name)
        self.name.setFont(f)
        self.value = QGraphicsSimpleTextItem(self)
        self.value.setFlag(QGraphicsItem.ItemIgnoresTransformations)
        # Or else value would get the clicks, instead of the PlugItem.
        self.value.setFlag(QGraphicsItem.ItemStacksBehindParent)
        self.value.setFont(f)
        self.setupPaint()

    def handleAtPos(self, pos):
        """Is there an interactive handle where the mouse is?
        Also return the Plug under this handle.
        """
        return self.data if self.pinPath.contains(pos) else None

    def itemChange(self, change, value):
        """Warning view it will soon have to correct pos."""
        if change == QGraphicsItem.ItemPositionHasChanged:
            # Restart till we stop moving.
            self.scene().views()[0].timer.start()
        return QGraphicsItem.itemChange(self, change, value)

    def setAndUpdate(self):
        """Change the undelying plug's value, and force updates items."""
        self.data.set(not self.data.value)
        for i in self.scene().items():
            if isinstance(i, PlugItem) or isinstance(i, WireItem):
                i.setupPaint()

    def setNameVisibility(self, isVisible):
        """Shows/Hide the item name in the graphical view."""
        self.showName = isVisible
        self.setupPaint()

    def setupPaint(self):
        """Offscreen rather than onscreen redraw (few changes)."""
        path = QPainterPath()
        if self.data.isInput:
            path.addEllipse(
                self.pinW / 2, self.pinW / 2, self.bodyW, self.bodyW)
        else:
            path.addRect(
                3 * self.pinW / 2 + 1, self.pinW / 2, self.bodyW, self.bodyW)
        path.addPath(self.pinPath)
        self.setPath(path)
        self.name.setVisible(self.showName)
        self.name.setText(self.data.name)
        br = self.mapToScene(self.boundingRect())
        w = self.boundingRect().width()
        h = self.boundingRect().height()
        realX = min([i.x() for i in br])
        realY = min([i.y() for i in br])
        self.name.setPos(self.mapFromScene(
            realX, realY + (w if self.rotation() % 180 else h) + 1))
        self.value.setText(
            str(int(self.data.value)) if self.data.value is not None else 'E')
        self.value.setPos(self.mapFromScene(realX + w / 3, realY + h / 3))
        self.value.setBrush(QColor('green' if self.data.value else 'red'))
        self.update()       # Force onscreen redraw after changes.