Пример #1
0
class HooverBar(QGraphicsRectItem):
    def __init__(self, *args):
        super(HooverBar, self).__init__(*args)
        self.setAcceptHoverEvents(True)
        self.gi = None
        self.description = [""]
        self.base_font = QFont()
        self.setPen(QPen(Qt.transparent))

    def hoverEnterEvent(self, event):
        super(HooverBar, self).hoverEnterEvent(event)

        if self.gi == None:
            self.gi = QGraphicsRectItem(0, 0, 100, 100)
            self.gi.setBrush(QBrush(QColor(0, 64, 0, 192)))
            self.gi.setPen(QPen(Qt.transparent))
            self.gi.setPos(event.scenePos().x() + 20,
                           event.scenePos().y() + 20)

            x = y = 10
            w = 0
            for t in self.description:
                description = QGraphicsSimpleTextItem()
                description.setFont(self.base_font)
                description.setBrush(QBrush(Qt.white))
                description.setText(t)
                description.setParentItem(self.gi)
                description.setPos(x, y)
                y += description.boundingRect().height()
                w = max(w, description.boundingRect().width())
            y += x
            w += 2 * x

            self.gi.setRect(0, 0, w, y)
            self.scene().addItem(self.gi)

    def hoverMoveEvent(self, event):
        super(HooverBar, self).hoverMoveEvent(event)

        # if self.gi:
        #     mainlog.debug("hoverMoveEvent GI pos={}-{}".format(self.gi.pos().x(),self.gi.pos().y()))
        # mainlog.debug("hoverMoveEvent pos={}-{}".format(event.scenePos().x(),event.scenePos().y()))
        if self.gi:
            self.gi.setPos(event.scenePos().x() + 20,
                           event.scenePos().y() + 20)

    def hoverLeaveEvent(self, event):
        super(HooverBar, self).hoverLeaveEvent(event)

        if self.gi:

            # QtDoc : Removes the item item and all its children from the scene.
            #         The ownership of item is passed on to the caller

            self.gi.setParentItem(None)
            # self.scene().removeItem(self.gi)
            self.gi = None
Пример #2
0
class HooverBar(QGraphicsRectItem):
    def __init__(self, x, y, w, h, hoover_text, doubleclick_callback_widget,
                 order_part_id):
        """ doubleclick_callback_widget : we will call the set_on_orderpart method
        of that object if a doubleclik happens here.
        """

        super(HooverBar, self).__init__(x, y, w, h)
        self.setAcceptHoverEvents(True)
        self.gi = None
        self.hoover_text = hoover_text
        self.description = None
        self.doubleclick_callback_widget = doubleclick_callback_widget
        self.order_part_id = order_part_id

        self.base_font = QFont()
        if configuration.font_select:
            self.base_font.setPointSize(self.base_font.pointSize() * 2)

    def hoverEnterEvent(self, event):  # QGraphicsSceneHoverEvent * event )
        # BUG I had a crash running this, I suspect ownership issues regarding
        # graphics items...

        global configuration
        #mainlog.debug("hoverEnterEvent pos={}-{}".format(event.scenePos().x(),event.scenePos().y()))
        # mainlog.debug("hoverEnterEvent data={}".format(self.data))
        super(HooverBar, self).hoverEnterEvent(event)

        if self.gi == None:
            if self.hoover_text:
                self.gi = QGraphicsRectItem(0, 0, 100, 100)
                self.gi.setBrush(QBrush(QColor(0, 64, 0, 192)))
                self.gi.setPen(QPen(Qt.transparent))
                self.gi.setPos(event.scenePos().x() + 20,
                               event.scenePos().y() + 20)

                # txt = [ "" ]

                # txt = [ u"{} {}".format(self.data.production_file.order_part.human_identifier,
                #                        date_to_dmy(self.data.production_file.order_part.deadline)),
                #         nstr(self.data.production_file.order_part.description),
                #         nstr(self.data.description),
                #         _("{}x{}={}h, rest:{}h").format(self.data.production_file.order_part.qty,
                #                                         nice_round(self.data.planned_hours),
                #                                         nice_round(self.data.production_file.order_part.qty*self.data.planned_hours),
                #                                         nice_round(self.data.planned_hours*self.data.production_file.order_part.qty - self.data.done_hours))]

                x = y = 10
                w = 0
                for t in self.hoover_text:
                    description = QGraphicsSimpleTextItem()
                    description.setFont(self.base_font)
                    description.setBrush(QBrush(Qt.white))
                    description.setText(t)
                    description.setParentItem(self.gi)
                    description.setPos(x, y)
                    y += description.boundingRect().height()
                    w = max(w, description.boundingRect().width())
                y += x
                w += 2 * x

                # description.setHtml(u"|{}| <b>{}</b><br/>{}<br>{}x{}={}h".format(
                #         self.data.production_file.order_part.human_identifier,
                #         self.data.production_file.order_part.description,
                #         self.data.description,
                #         self.data.production_file.order_part.qty, self.data.planned_hours, self.data.production_file.order_part.qty*self.data.planned_hours))

                # description.setDefaultTextColor(Qt.white)
                # br = description.boundingRect()

                self.gi.setRect(0, 0, w, y)
                self.scene().addItem(self.gi)
                #mainlog.debug("hoverEnterEvent GI={}".format(self.gi))

        # mainlog.debug("hoverEnterEvent Done")

    def hoverMoveEvent(self, event):
        super(HooverBar, self).hoverMoveEvent(event)

        # if self.gi:
        #     mainlog.debug("hoverMoveEvent GI pos={}-{}".format(self.gi.pos().x(),self.gi.pos().y()))
        # mainlog.debug("hoverMoveEvent pos={}-{}".format(event.scenePos().x(),event.scenePos().y()))
        if self.gi:
            self.gi.setPos(event.scenePos().x() + 20,
                           event.scenePos().y() + 20)

    def hoverLeaveEvent(self, event):
        super(HooverBar, self).hoverLeaveEvent(event)

        if self.gi:
            # mainlog.debug("hoverLeaveEvent GI={}".format(self.gi))
            # self.gi.setActive(False)
            # self.gi.setVisible(False)
            # return

            if self.description:
                # I do this so that I can handle the destruction of description
                # graphics item myself (i.e. description won't be deleted
                # as a children of self.gi)
                self.description.setParentItem(None)
                self.description = None  # and delete...

            # QtDoc : Removes the item item and all its children from the scene.
            #         The ownership of item is passed on to the caller

            self.gi.setParentItem(None)
            # self.scene().removeItem(self.gi)
            self.gi = None
            # mainlog.debug("hoverLeaveEvent -- done")

    # order_part_double_clicked = Signal(int)

    def mouseDoubleClickEvent(self,
                              event):  # QGraphicsSceneHoverEvent * event )
        # mainlog.debug("mouseDoubleClickEvent {}".format(self.data))
        super(HooverBar, self).mouseDoubleClickEvent(event)
        self.doubleclick_callback_widget.set_on_order_part(self.order_part_id)