Example #1
0
class GraphicsTextWidget(QGraphicsWidget):
    def __init__(self, text, parent=None, **kwargs):
        super().__init__(parent, **kwargs)
        self.labelItem = QGraphicsTextItem(self)
        self.setHtml(text)

        self.labelItem.document().documentLayout().documentSizeChanged.connect(
            self.onLayoutChanged
        )

    def setGeometry(self, rect):
        self.prepareGeometryChange()
        super().setGeometry(rect)

    def onLayoutChanged(self, *args):
        self.updateGeometry()

    def sizeHint(self, which, constraint=QSizeF()):
        if which == Qt.MinimumSize:
            return self.labelItem.boundingRect().size()
        else:
            return self.labelItem.boundingRect().size()

    def setTextWidth(self, width):
        self.labelItem.setTextWidth(width)

    def setHtml(self, text):
        self.labelItem.setHtml(text)
def bgc_name_face(node, *args, **kargs):
    """
    This is the item generator. It must receive a node object, and
    returns a Qt4 graphics item that can be used as a node face.
    """
    # Receive an arbitrary number of arguments, in this case width and
    # Height of the faces and the information about the BGC
    width = args[0]
    height = args[1]
    # Add the popup
    interactive_face = InteractiveItem("Class : {}\nRelated MIBiG : {}\nCluster family : {}".format(args[2], args[4], args[3]), 0, 0, width, height)
    # Keep a link within the item to access node info
    interactive_face.node = node
    # Remove border around the masterItem
    interactive_face.setPen(QPen(QtCore.Qt.NoPen))
    # Add ellipse around text
    ellipse = QGraphicsEllipseItem(interactive_face.rect())
    ellipse.setParentItem(interactive_face)
    # Change ellipse color
    ellipse.setBrush(QBrush(QColor(args[6])))
    # Add node name within the ellipse
    text = QGraphicsTextItem(args[5])
    text.setTextWidth(50)
    text.setParentItem(ellipse)
    # Center text according to masterItem size
    text_width = text.boundingRect().width()
    text_height = text.boundingRect().height()
    center = interactive_face.boundingRect().center()
    text.setPos(center.x()-text_width/2, center.y()-text_height/2)
    return interactive_face
Example #3
0
class ItemUsuario(ItemMovel):
    def __init__(self, usuario, rect=QRectF(0, 0, 100, 50), parent=None):
        super().__init__(False, True, rect, parent)

        self._numConversasNaoVisualizadas = 0
        self.selected = False
        self._textItem = QGraphicsTextItem(self)
        self._textItem.setFont(self.fontUser())
        self._textItem.setPos(0, 10)
        self.setaImg = ResourceUtil.getImage('images/right.png').scaled(80, 40, aspectRatioMode=Qt.KeepAspectRatio)
        self.topoImg = ResourceUtil.getImage('images/fundo/topo.png')
        self.background = ResourceUtil.getImage('images/postit3.png')

        self.setUsuario(usuario)

    def setUsuario(self, usuario):
        self._usuario = usuario
        self.atualizaTexto()

    def getUsuario(self):
        return self._usuario

    def setNumConversasNaoVisualizadas(self, num):
        self._numConversasNaoVisualizadas = num
        self.atualizaTexto()

    def atualizaTexto(self):
        texto = "<center>{0} - {1}".format(self._usuario.getNome(), self._usuario.getIP())
        if self._numConversasNaoVisualizadas > 0:
            texto += " <b>({0})</b>".format(self._numConversasNaoVisualizadas)
        texto += "</center>"

        self._textItem.setHtml(texto)

    def setRect(self, rect):
        super().setRect(rect)
        self._textItem.setTextWidth(rect.size().width() - self.setaImg.rect().width())

    def paint(self, painter, widget, option):
        painter.setPen(Qt.NoPen)

        if self.selected:
            rect = self.boundingRect()
            rectImg = self.setaImg.rect()
            painter.drawImage(rectImg.adjusted(rect.width() - rectImg.width(), 0, rect.width() - rectImg.width(), 0), self.setaImg)

        painter.drawImage(self.boundingRect(), self.background)

        """if self.isUnderMouse():
            painter.drawImage(self.boundingRect().adjusted(0,10,0,0), self.topoImg)

        painter.save()
        painter.setPen(Qt.NoPen)
        st = Status.getInstance(self.getUsuario().getStatus())
        if st == Status.ONLINE:
            painter.setBrush(QColor(0,255,0,50))
        elif st == Status.OCUPADO:
            painter.setBrush(QColor(255,0,0,50))
        elif st == Status.AUSENTE:
            painter.setBrush(QColor(240, 226, 31, 50))

        painter.drawRoundedRect(self.getRect(), 7, 5)
        painter.restore()"""

        super().paint(painter, widget, option)

    def fontUser(self):
        font = QFont("LoveYaLikeASister")
        font.setBold(True)
        return font
Example #4
0
File: Canvas1.py Project: iras/JADE
class CanvasProps (QGraphicsItem):


    def __init__(self, parent=None, scene=None):
        
        QGraphicsItem.__init__ (self)
        
        self.parent = parent
        self.helper = self.parent.getHelper()
        
        #self.setFlags (QGraphicsItem.ItemIsSelectable)
        self.setAcceptsHoverEvents (True)
        
        self.pen_color = QPen (Qt.black, 2)
        
        self.color = QColor (Qt.white).dark (150)
        
        # init Canvas Animation Tweening
        self.timeline = QTimeLine (200)
        self.timeline.setFrameRange (0, 100)
        self.anim = QGraphicsItemAnimation ()
        self.anim.setItem (self)
        self.anim.setTimeLine (self.timeline)
        self.helper.connect (self.timeline, SIGNAL("finished()"), self.moveFurtherUp)
        self.anim_active = False
        
        #self._nodename = QGraphicsTextItem ('text '+str(self.node_id), self)
        self._nodename = QGraphicsTextItem ('', self)
        self._nodename.setPos (QPointF (18, 20))
        self._nodename.setDefaultTextColor (QColor (Qt.white).light (255))
        self._nodename.setFont (QFont ("Helvetica", 11, QFont.Bold, False))
        self._nodename.setTextWidth(120)
        self._nodename.setToolTip (self._nodename.toPlainText ())
        #self._nodename.setHtml("<h2 align=\"center\">hello</h2><h2 align=\"center\">world 1234345345</h2>123");
        
        self.props_list = []
        self.props_textItem_value_list = []
        
        self.FACTOR = 4.0
        
        self._canvas_height = 0
    
    def boundingRect (self): return QRectF (0, 0, 122, 150)
    
    def shape (self):
        
        path = QPainterPath ()
        path.addRect (0, 0, 122, 20)
        return path
    
    def paint (self, painter, option, unused_widget):
        
        if option.state & QStyle.State_Selected:
            fillColor = self.color.dark (100)
        else:
            fillColor = self.color
        
        if option.state & QStyle.State_MouseOver:
            fillColor = fillColor.light (120)
        
        if option.levelOfDetail < 0.2:
            
            if option.levelOfDetail < 0.125:
                painter.fillRect (QRectF (0, 0, 110, 70), fillColor)
                return
            
            painter.setPen   (QPen (Qt.black, 0))
            painter.setBrush (fillColor)
            painter.drawRect (0, 0, 120, 20)
            return
        
        oldPen = painter.pen ()
        pen = oldPen
        width = 0
        
        if option.state & QStyle.State_Selected:
            width += 2
        
        pen.setWidth (width)
        if option.state & QStyle.State_Sunken:
            level = 120
        else:
            level = 100
        
        painter.setBrush (QBrush (fillColor.dark (level)))
        #painter.drawRoundRect (QRect (0, 0, 80, 34+self.height), 20)
        painter.drawRect (QRect (0, 20, 120, 30+9*self._canvas_height))
    
    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    
    def addProp (self, prop_name, prop_value):
        
        i = len (self.props_list)
        self.props_list.append  (QGraphicsTextItem (prop_name + ' : ', self))
        self.props_textItem_value_list.append (CustomFloatingText (prop_value, self))
        
        # (1) adding the prop's name.
        self.props_list[i].setPos (QPointF (7, 35+i*10))
        self.props_list[i].setDefaultTextColor (QColor (Qt.white).light (255))
        self.props_list[i].setFont (QFont ("Helvetica", 9, QFont.StyleItalic, False))
        self.props_list[i].setTextWidth (55)
        self.props_list[i].setToolTip (self.props_list[i].toPlainText ())
        
        # (2) adding the prop's value.
        self.props_textItem_value_list[i].setTextInteractionFlags (Qt.TextEditable)
        self.props_textItem_value_list[i].setPos (QPointF (55, 35+i*10))
        self.props_textItem_value_list[i].setDefaultTextColor (QColor (Qt.white).light (255))
        self.props_textItem_value_list[i].setFont (QFont ("Helvetica", 9, QFont.StyleNormal, False))
        self.props_textItem_value_list[i].setTextWidth (55)
        
        receiver = lambda value: self.parent.listenToChangedPropsValues (prop_name, value)
        self.helper.connect (self.props_textItem_value_list[i], SIGNAL ("textChanged(QString)"), receiver)
    
    def getProps (self):
        
        tmp_list = []
        
        l = len (self.props_list)
        for i in range (0, l):
            tmp_list[i] = [self.props_list[i].toPlainText(), self.props_textItem_value_list[i].toPlainText()]
        
        return tmp_list
    
    def setTitle (self, title): self._nodename.setPlainText (title)
    
    def setCanvasHeightInUnits (self, ch): self._canvas_height = ch
    
    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    
    def moveDown (self, canvas_height_in_units):
        
        self.anim_active  = True
        self.upwards_flag = False
        
        self.canvas_height = canvas_height_in_units
        
        self.timeline.stop ()
        
        self.anim.setPosAt (0, QPointF(0, 1+(self.canvas_height+1)*self.FACTOR))
        self.anim.setPosAt (1, QPointF(0, 1+(self.canvas_height+2)*self.FACTOR))
        
        self.timeline.start ()
        self.update ()
    
    def moveUp (self, canvas_height_in_units):
        
        if self.anim_active == False:
            
            self.anim_active  = True
            self.upwards_flag = True
            
            self.canvas_height = canvas_height_in_units
            
            self.timeline.stop ()
            
            self.anim.setPosAt (0, QPointF(0, 1+(self.canvas_height+1)*self.FACTOR))
            self.anim.setPosAt (1, QPointF(0, 1+(self.canvas_height)  *self.FACTOR))
            
            self.timeline.start ()
            self.update ()
    
    # this method double-checks whether the canvas needs to be further up as a
    # result of receiving other asynchronous "delete link" SIGNALs while moving up.
    def moveFurtherUp (self):
        
        self.anim_active = False
        
        if self.upwards_flag==True:
            
            if self.parent.getMaxLen() < self.canvas_height:
                self.moveUp (self.canvas_height-1)
    
    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    
    def hoverEnterEvent (self, e):
        
        self._nodename.setToolTip (self._nodename.toPlainText ())
        QGraphicsItem.hoverEnterEvent (self, e)
    
    '''