def mousePressEvent(self, event):
        """Handles mouse press event"""
        button = event.button()
        if button not in [Qt.LeftButton, Qt.RightButton]:
            self.clearSelection()
            event.accept()
            return

        if button == Qt.RightButton:
            item = self.itemAt(event.scenePos(), QTransform())
            logicalItem = self.__getLogicalItem(item)
            if logicalItem is None:
                # Not a selectable item or out of the items
                self.clearSelection()
            elif not logicalItem.isSelected():
                self.clearSelection()
                logicalItem.setSelected(True)

            # Bring up a context menu
            self.onContextMenu(event)
        else:
            # Here: this is LMB
            self.__createRubberBand(event)

        event.accept()
        return
Beispiel #2
0
    def mousePressEvent(self, event):
        """The default mouse behavior of the QT library is sometimes
           inconsistent. For example, selecting of the items is done on
           mousePressEvent however adding items is done on mouseReleaseEvent.
           The second thing is that the QT library does not support
           hierarchical relationships between the items.
           The third thing is the selection proxy which is not supported either.
           So the whole mouse[Press,Release]Event members are overridden
        """
        item = self.itemAt(event.scenePos(), QTransform())
        logicalItem = self.__getLogicalItem(item)

        button = event.button()
        if button not in [Qt.LeftButton, Qt.RightButton]:
            self.clearSelection()
            event.accept()
            return

        if button == Qt.RightButton:
            if logicalItem is None:
                # Not a selectable item or out of the items
                self.clearSelection()
            elif not logicalItem.isSelected():
                self.clearSelection()
                logicalItem.setSelected(True)

            # Bring up a context menu
            self.onContextMenu(event)
            event.accept()
            return

        # Here: this is LMB
        self.__createRubberBand(event)

        if logicalItem is None:
            self.clearSelection()
            event.accept()
            return

        modifiers = event.modifiers()
        if modifiers == Qt.NoModifier:
            self.clearSelection()
            logicalItem.setSelected(True)
            event.accept()
            return

        if modifiers == Qt.ControlModifier:
            if logicalItem.isSelected():
                logicalItem.setSelected(False)
                event.accept()
                return
            # Item is not selected and should be added or ignored
            self.addToSelection(logicalItem)
            event.accept()
            return

        # The alt modifier works for the whole app window on
        # Ubuntu, so it cannot really be used...
        if modifiers == Qt.ShiftModifier:
            self.clearSelection()

            # Here: add comments
            if self.isOpenGroupItem(item):
                self.addToSelection(item)
            else:
                for item in self.findItemsForRef(logicalItem.ref):
                    self.addToSelection(item)
            event.accept()
            return

        event.accept()
    def mouseReleaseEvent(self, event):
        """Handles the mouse release event"""
        button = event.button()

        if button not in [Qt.LeftButton, Qt.RightButton]:
            event.accept()
            return

        if button == Qt.RightButton:
            event.accept()
            return

        # Here: left mouse button
        if self.__isRubberBandVisible():
            # Detect intersections
            self.clearSelection()
            for item in self.items():
                if not isinstance(item, CellElement):
                    continue
                if item.isProxyItem():
                    continue
                if item.scopedItem():
                    if item.subKind not in [
                            ScopeCellElement.DECLARATION,
                            ScopeCellElement.COMMENT,
                            ScopeCellElement.DOCSTRING
                    ]:
                        continue

                    if item.subKind == ScopeCellElement.DECLARATION:
                        item = item.getTopLeftItem()

                # The call must be done on item. (not on rubberBand.)
                if item.collidesWithItem(self.rubberBand,
                                         Qt.ContainsItemBoundingRect):
                    self.addToSelection(item)
        else:
            item = self.itemAt(event.scenePos(), QTransform())
            logicalItem = self.__getLogicalItem(item)

            if logicalItem is None:
                self.clearSelection()
            else:
                modifiers = event.modifiers()
                if modifiers == Qt.NoModifier:
                    if logicalItem.kind in [CellElement.INDEPENDENT_DOC,
                                            CellElement.LEADING_DOC,
                                            CellElement.ABOVE_DOC] and \
                       item.isProxyItem():
                        if logicalItem.cmlRef.link is None:
                            self.clearSelection()
                            logicalItem.setSelected(True)
                        else:
                            logicalItem.mouseClickLinkIcon()
                    else:
                        self.clearSelection()
                        logicalItem.setSelected(True)
                elif modifiers == Qt.ControlModifier:
                    if logicalItem.isSelected():
                        logicalItem.setSelected(False)
                    else:
                        # Item is not selected and should be added or ignored
                        self.addToSelection(logicalItem)
                # The alt modifier works for the whole app window on
                # Ubuntu, so it cannot really be used...
                elif modifiers == Qt.ShiftModifier:
                    self.clearSelection()

                    # Here: add comments
                    if self.isOpenGroupItem(item):
                        self.addToSelection(item)
                    else:
                        for itemForRef in self.findItemsForRef(
                                logicalItem.ref):
                            self.addToSelection(itemForRef)

        self.__destroyRubberBand()
        event.accept()