Python PyQt5.QtWidgets.QGraphicsItem() Examples

The following are 23 code examples of PyQt5.QtWidgets.QGraphicsItem(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module PyQt5.QtWidgets , or try the search function .
Example #1
Source File: qt.py    From imperialism-remake with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, parent):
        """
        Create property animations, sets the opacity to zero initially.

        :param parent:
        """
        super().__init__()
        if isinstance(parent, QtWidgets.QGraphicsItem):
            # create opacity effect
            self.effect = QtWidgets.QGraphicsOpacityEffect()
            self.effect.setOpacity(0)
            parent.setGraphicsEffect(self.effect)
            self.fade = QtCore.QPropertyAnimation(self.effect, 'opacity'.encode())  # encode is utf-8 by default
        elif isinstance(parent, QtWidgets.QWidget):
            parent.setWindowOpacity(0)
            self.fade = QtCore.QPropertyAnimation(parent, 'windowOpacity'.encode())  # encode is utf-8 by default
        else:
            raise RuntimeError('Type of parameter must be QtWidgets.QGraphicsItem or QtWidgets.QWidget.')

        # set start and stop value
        self.fade.setStartValue(0)
        self.fade.setEndValue(1)
        self.fade.finished.connect(self.finished)

        self.forward = True 
Example #2
Source File: cad_viewer.py    From ezdxf with MIT License 6 votes vote down vote up
def _on_element_selected(self, element: Optional[qw.QGraphicsItem], mouse_pos: qc.QPointF):
        text = f'mouse position: {mouse_pos.x():.4f}, {mouse_pos.y():.4f}\n'
        if element is None:
            text += 'No element selected'
        else:
            dxf_entity = element.data(CorrespondingDXFEntity)
            if dxf_entity is None:
                text += 'No data'
            else:
                text += f'Current Entity: {dxf_entity}\nLayer: {dxf_entity.dxf.layer}\n\nDXF Attributes:\n'
                for key, value in dxf_entity.dxf.all_existing_dxf_attribs().items():
                    text += f'- {key}: {value}\n'

                dxf_entity_stack = element.data(CorrespondingDXFEntityStack)
                if dxf_entity_stack:
                    text += '\nParents:\n'
                    for entity in reversed(dxf_entity_stack):
                        text += f'- {entity}\n'

        self.info.setPlainText(text) 
Example #3
Source File: items.py    From Miyamoto with GNU General Public License v3.0 6 votes vote down vote up
def itemChange(self, change, value):
        """
        Handle movement
        """
        if change == QtWidgets.QGraphicsItem.ItemPositionChange:
            if self.scene() is None: return value

            updRect = QtCore.QRectF(
                self.x() + self.aux.x(),
                self.y() + self.aux.y(),
                self.aux.BoundingRect.width(),
                self.aux.BoundingRect.height(),
            )
            self.scene().update(updRect)

        return super().itemChange(change, value) 
Example #4
Source File: spritelib.py    From Miyamoto with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, parent, imageObj):
        """
        Generic constructor for auxiliary zone items
        """
        super().__init__(parent)
        self.parent = parent
        self.imageObj = imageObj
        self.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable, False)
        self.setFlag(QtWidgets.QGraphicsItem.ItemIsSelectable, False)
        self.setFlag(QtWidgets.QGraphicsItem.ItemStacksBehindParent, False)
        self.setParentItem(parent)
        self.hover = False

        if parent is not None:
            parent.aux.add(self)

        self.BoundingRect = QtCore.QRectF(0, 0, TileWidth, TileWidth) 
Example #5
Source File: CodeUIEdgeItem.py    From CodeAtlasSublime with Eclipse Public License 1.0 5 votes vote down vote up
def __init__(self, srcUniqueName, tarUniqueName, edgeData = {}, parent = None, scene = None):
		super(CodeUIEdgeItem, self).__init__(parent)
		self.setFlag(QtWidgets.QGraphicsItem.ItemIsSelectable)
		self.setAcceptHoverEvents(True)
		self.srcUniqueName = srcUniqueName
		self.tarUniqueName = tarUniqueName
		self.setZValue(-1)
		self.path = None
		self.pathShape = None
		self.curve = None
		self.pathPnt = None

		self.file = ''
		self.line = -1
		self.column = -1
		dbRef = edgeData.get('dbRef', None)
		if dbRef:
			self.file = dbRef.file().longname()
			self.line = dbRef.line()
			self.column = dbRef.column()

		self.isHover = False

		# (number, point)
		self.orderData = None
		self.buildPath()
		self.isConnectedToFocusNode = False
		self.schemeColorList = []
		self.customEdge = edgeData.get('customEdge', False)
		self.isCandidate = False 
Example #6
Source File: multiplot.py    From kite with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self):
        QtWidgets.QGraphicsRectItem.__init__(self, self.cursor)
        self.setPen(self.pen)
        self.setZValue(1e9)
        self.setFlag(QtWidgets.QGraphicsItem.ItemIgnoresTransformations) 
Example #7
Source File: SymbolUIItem.py    From CodeAtlasSublime with Eclipse Public License 1.0 5 votes vote down vote up
def __init__(self, node, parent = None, scene = None):
		super(SymbolUIItem, self).__init__(parent, scene)
		isIgnore = node and node.getKind() == node.KIND_UNKNOWN
		self.setFlag(QtWidgets.QGraphicsItem.ItemIsSelectable, not isIgnore)
		self.setFlag(QtWidgets.QGraphicsItem.ItemIsFocusable, not isIgnore)
		self.setAcceptDrops(True);
		self.setAcceptHoverEvents(True)
		self.node = node
		self.path = None
		self.rect = None
		self.isHover = False
		self.theta = (0,0)
		self.radius= (0,0)
		self.txtRadius = 0
		self.setToolTip("%s:%s" % (node.name, node.getKindName()))
		self.txtPos = None

		if not SymbolUIItem.COLOR_DICT:
			SymbolUIItem.COLOR_DICT = \
				{self.node.KIND_FUNCTION: QtGui.QColor(190,228,73),
				 self.node.KIND_VARIABLE: QtGui.QColor(255,198,217),
				 self.node.KIND_CLASS:    QtGui.QColor(154,177,209),
				 self.node.KIND_NAMESPACE: QtGui.QColor(154,225,209),
				 self.node.KIND_UNKNOWN: QtGui.QColor(195,195,195),
				 }

		self.setFlag(QtWidgets.QGraphicsItem.ItemIsSelectable)
		self.setFlag(QtWidgets.QGraphicsItem.ItemIsFocusable) 
Example #8
Source File: pyqt_backend.py    From ezdxf with MIT License 5 votes vote down vote up
def _set_item_data(self, item: qw.QGraphicsItem) -> None:
        item.setData(CorrespondingDXFEntity, self.current_entity)
        item.setData(CorrespondingDXFEntityStack, self.current_entity_stack) 
Example #9
Source File: cad_viewer.py    From ezdxf with MIT License 5 votes vote down vote up
def __init__(self):
        super().__init__()
        self._current_item: Optional[qw.QGraphicsItem] = None 
Example #10
Source File: image_viewer_scene.py    From CvStudio with MIT License 5 votes vote down vote up
def removeItem(self, item: QGraphicsItem) -> None:
        super(ImageViewerScene, self).removeItem(item)
        if isinstance(item, EditableItem):
            self.itemDeleted.emit(item) 
Example #11
Source File: image_viewer_scene.py    From CvStudio with MIT License 5 votes vote down vote up
def addItem(self, item: QGraphicsItem) -> None:
        super(ImageViewerScene, self).addItem(item)
        if isinstance(item, EditableItem):
            self.itemAdded.emit(item) 
Example #12
Source File: common.py    From eddy with GNU General Public License v3.0 5 votes vote down vote up
def setPos(self, *__args):
        """
        Set the item position.
        QtWidgets.QGraphicsItem.setPos(QtCore.QPointF)
        QtWidgets.QGraphicsItem.setPos(float, float)
        """
        if len(__args) == 1:
            pos = __args[0]
        elif len(__args) == 2:
            pos = QtCore.QPointF(__args[0], __args[1])
        else:
            raise TypeError('too many arguments; expected {0}, got {1}'.format(2, len(__args)))
        super().setPos(pos - QtCore.QPointF(self.width() / 2, self.height() / 2)) 
Example #13
Source File: spritelib.py    From Miyamoto with GNU General Public License v3.0 5 votes vote down vote up
def setIsBehindLocation(self, behind):
        """
        This allows you to choose whether the auiliary item will display
        behind the zone or in front of it. Default is for the item to
        be in front of the location.
        """
        self.setFlag(QtWidgets.QGraphicsItem.ItemStacksBehindParent, behind) 
Example #14
Source File: spritelib.py    From Miyamoto with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, parent, imageObj):
        """
        Generic constructor for auxiliary items
        """
        super().__init__(parent)
        self.parent = parent
        self.imageObj = imageObj
        self.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable, False)
        self.setFlag(QtWidgets.QGraphicsItem.ItemIsSelectable, False)
        self.setFlag(QtWidgets.QGraphicsItem.ItemStacksBehindParent, False)
        self.setParentItem(parent)
        self.hover = False
        self.BoundingRect = QtCore.QRectF(0, 0, TileWidth, TileWidth) 
Example #15
Source File: spritelib.py    From Miyamoto with GNU General Public License v3.0 5 votes vote down vote up
def setIsBehindZone(self, behind):
        """
        This allows you to choose whether the auiliary item will display
        behind the zone or in front of it. Default is for the item to
        be in front of the zone.
        """
        self.setFlag(QtWidgets.QGraphicsItem.ItemStacksBehindParent, behind) 
Example #16
Source File: spritelib.py    From Miyamoto with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, parent):
        """
        Generic constructor for auxiliary items
        """
        super().__init__(parent)
        self.parent = parent
        self.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable, False)
        self.setFlag(QtWidgets.QGraphicsItem.ItemIsSelectable, False)
        self.setFlag(QtWidgets.QGraphicsItem.ItemStacksBehindParent, True)
        self.setParentItem(parent)
        self.hover = False

        self.BoundingRect = QtCore.QRectF(0, 0, TileWidth, TileWidth) 
Example #17
Source File: items.py    From Miyamoto with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, parent):
            """
            Initializes the auxiliary entrance thing
            """
            super().__init__(parent)
            self.parent = parent
            self.setFlag(QtWidgets.QGraphicsItem.ItemIsMovable, False)
            self.setFlag(QtWidgets.QGraphicsItem.ItemIsSelectable, False)
            self.setFlag(QtWidgets.QGraphicsItem.ItemStacksBehindParent, True)
            self.setParentItem(parent)
            self.hover = False 
Example #18
Source File: items.py    From Miyamoto with GNU General Public License v3.0 5 votes vote down vote up
def itemChange(self, change, value):
        """
        Avoids snapping for zones
        """
        return QtWidgets.QGraphicsItem.itemChange(self, change, value) 
Example #19
Source File: qt.py    From imperialism-remake with GNU General Public License v3.0 5 votes vote down vote up
def add_item(self, item: QtWidgets.QGraphicsItem):
        """
        Adds an item to the content list. Should be

        :param item:
        """
        if not isinstance(item, QtWidgets.QGraphicsItem):
            raise RuntimeError('Expected instance of QGraphicsItem!')
        self._content.add(item) 
Example #20
Source File: multiplot.py    From kite with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, parent):
        QtWidgets.QGraphicsItem.__init__(self, parent=parent)

        self.p1 = QtCore.QPointF()
        self.p2 = QtCore.QPointF()
        self.line = QtCore.QLineF(self.p1, self.p2)

        self.setOrientation(0., 0.)
        self.setZValue(10000) 
Example #21
Source File: items.py    From Miyamoto with GNU General Public License v3.0 4 votes vote down vote up
def itemChange(self, change, value):
        """
        Makes sure positions don't go out of bounds and updates them as necessary
        """

        if change == QtWidgets.QGraphicsItem.ItemPositionChange:
            scene = self.scene()
            if scene is None: return value

            # snap to 24x24
            newpos = value
            newpos.setX(int((newpos.x() + globals.TileWidth / 2) / globals.TileWidth) * globals.TileWidth)
            newpos.setY(int((newpos.y() + globals.TileWidth / 2) / globals.TileWidth) * globals.TileWidth)
            x = newpos.x()
            y = newpos.y()

            # don't let it get out of the boundaries
            if x < 0: newpos.setX(0)
            if x > globals.TileWidth * 1023: newpos.setX(globals.TileWidth * 1023)
            if y < 0: newpos.setY(0)
            if y > globals.TileWidth * 511: newpos.setY(globals.TileWidth * 511)

            # update the data
            x = int(newpos.x() / globals.TileWidth)
            y = int(newpos.y() / globals.TileWidth)
            if x != self.objx or y != self.objy:
                self.LevelRect.moveTo(x, y)

                oldx = self.objx
                oldy = self.objy
                self.objx = x
                self.objy = y
                self.UpdateSearchDatabase()
                if self.positionChanged is not None:
                    self.positionChanged(self, oldx, oldy, x, y)

                SetDirty()

                # updRect = QtCore.QRectF(self.x(), self.y(), self.BoundingRect.width(), self.BoundingRect.height())
                # scene.invalidate(updRect)

                scene.invalidate(self.x(), self.y(), self.width * globals.TileWidth, self.height * globals.TileWidth,
                                 QtWidgets.QGraphicsScene.BackgroundLayer)
                # scene.invalidate(newpos.x(), newpos.y(), self.width * globals.TileWidth, self.height * globals.TileWidth, QtWidgets.QGraphicsScene.BackgroundLayer)

            return newpos

        return QtWidgets.QGraphicsItem.itemChange(self, change, value) 
Example #22
Source File: qt.py    From imperialism-remake with GNU General Public License v3.0 4 votes vote down vote up
def make_GraphicsItem_draggable(parent):
    """
    Takes a QtWidgets.QGraphicsItem and adds signals for dragging the object around. For this the item must have the
    ItemIsMovable and ItemSendsScenePositionChanges flags set. Only use it when really needed because there is
    some performance hit attached.
    """

    # noinspection PyPep8Naming
    class DraggableGraphicsItem(parent, QtCore.QObject):
        """
        Draggable GraphicsItem.
        """
        changed = QtCore.pyqtSignal(object)

        def __init__(self, *args, **kwargs):
            """
            By default QGraphicsItems are not movable and also do not emit signals when the position is changed for
            performance reasons. We need to turn this on.
            """
            parent.__init__(self, *args, **kwargs)
            self.parent = parent
            QtCore.QObject.__init__(self)

            self.setFlags(QtWidgets.QGraphicsItem.ItemIsMovable
                          | QtWidgets.QGraphicsItem.ItemSendsScenePositionChanges)

        def itemChange(self, change, value):  # noqa: N802
            """
            Catch all item position changes and emit the changed signal with the value (which will be the position).

            :param change:
            :param value:
            """
            if change == QtWidgets.QGraphicsItem.ItemPositionChange:
                self.changed.emit(value)

            return parent.itemChange(self, change, value)

    return DraggableGraphicsItem


# Some classes we need (just to make the naming clear), Name will be used in Stylesheet selectors

#: QToolBar made draggable 
Example #23
Source File: qt.py    From imperialism-remake with GNU General Public License v3.0 4 votes vote down vote up
def make_GraphicsItem_clickable(parent):
    """
    Takes a QtWidgets.QGraphicsItem and adds signals for entering, leaving and clicking on the item. For this the item
    must have setAcceptHoverEvents and it must also inherit from QObject to have signals. Only use it when really
    needed because there is some performance hit attached.
    """

    # class ClickableGraphicsItem(parent, QtCore.QObject):
    # noinspection PyPep8Naming
    class ClickableGraphicsItem(parent):
        """
            Clickable GraphicsItem
        """

        def __init__(self, *args, **kwargs):
            """
            QGraphicsItems by default do not accept hover events or accept mouse buttons (for performance reasons).
            So we need to turn both on.
            """
            parent.__init__(self, *args, **kwargs)
#           QtCore.QObject.__init__(self)
            self.parent = parent
            self.setAcceptHoverEvents(True)
            self.setAcceptedMouseButtons(QtCore.Qt.LeftButton)
            self.signaller = ClickableGraphicsItemSignaller()

        def hoverEnterEvent(self, event):  # noqa: N802
            """
            Emit the entered signal after default handling.

            :param event:
            """
            self.parent.hoverEnterEvent(self, event)
            self.signaller.entered.emit(event)

        def hoverLeaveEvent(self, event):  # noqa: N802
            """
            Emit the left signal after default handling.

            :param event:
            """
            self.parent.hoverLeaveEvent(self, event)
            self.signaller.left.emit(event)

        def mousePressEvent(self, event):  # noqa: N802
            """
            Emit the clicked signal after default handling.

            :param event:
            """
            self.parent.mousePressEvent(self, event)
            self.signaller.clicked.emit(event)

    return ClickableGraphicsItem


# noinspection PyPep8Naming