Ejemplo n.º 1
0
def tag_image(source, dest, tag, font, fontsize, x, y, width, height, aspectx, aspecty, red, green, blue, bold=False, italic=False):
    """docstring for tag_image"""

    app = QApplication.instance()
    
    pixmap = QPixmap(source)

    color = QColor(red,green,blue)
    font = QFont(font)
    font.setPixelSize(int(fontsize*pixmap.height()))
    font.setItalic(italic)
    font.setBold(bold)

    painter = QPainter(pixmap)
    painter.setPen(color)
    painter.setFont(font);
    painter.drawText(x*pixmap.width(),y*pixmap.height(), tag)
    painter.end()

    # Resize and save
    return pixmap.toImage().scaled(width*aspectx, height*aspecty, Qt.KeepAspectRatioByExpanding, Qt.SmoothTransformation).scaled(width, height, Qt.IgnoreAspectRatio, Qt.SmoothTransformation).save(dest)
Ejemplo n.º 2
0
 def PlainText(painter,widget):
     painter.setPen(widget.textColor())
     
     font = QFont()
     font.setPixelSize(widget.fontSize())
     
     font.setOverline(widget.overline())
     font.setUnderline(widget.underlined())
     font.setItalic(widget.italic())
     font.setStrikeOut(widget.strikeOut())
     
     font.setWeight(widget.fontWeight())
     font.setFamily(widget.fontFamily())
     font.setCapitalization( QFont.Capitalization(widget.textTransform()) )
     
     font.setStretch(widget.fontStretch())
     font.setLetterSpacing(QFont.SpacingType(widget.letterSpacingType()), widget.letterSpacing())
     font.setWordSpacing(widget.wordSpacing())
     
     painter.setFont(font)
     
     painter.drawText(0,0,100,100,widget.textAlignment(),widget.text())
Ejemplo n.º 3
0
    def setObjectText(self, strng):
        """
        TOWRITE

        :param `strng`: TOWRITE
        :type `strng`: QString
        """
        self.objText = strng
        textPath = QPainterPath()
        font = QFont()
        font.setFamily(self.objTextFont)
        font.setPointSizeF(self.objTextSize)
        font.setBold(self.objTextBold)
        font.setItalic(self.objTextItalic)
        font.setUnderline(self.objTextUnderline)
        font.setStrikeOut(self.objTextStrikeOut)
        font.setOverline(self.objTextOverline)
        textPath.addText(0., 0., font, strng)

        # Translate the path based on the justification.
        jRect = textPath.boundingRect()  # QRectF
        if self.objTextJustify == "Left": textPath.translate(-jRect.left(), 0)
        elif self.objTextJustify == "Center":
            textPath.translate(-jRect.center().x(), 0)
        elif self.objTextJustify == "Right":
            textPath.translate(-jRect.right(), 0)
        elif self.objTextJustify == "Aligned":
            pass  # TODO: TextSingleObject Aligned Justification
        elif self.objTextJustify == "Middle":
            textPath.translate(-jRect.center())
        elif self.objTextJustify == "Fit":
            pass  # TODO: TextSingleObject Fit Justification
        elif self.objTextJustify == "Top Left":
            textPath.translate(-jRect.topLeft())
        elif self.objTextJustify == "Top Center":
            textPath.translate(-jRect.center().x(), -jRect.top())
        elif self.objTextJustify == "Top Right":
            textPath.translate(-jRect.topRight())
        elif self.objTextJustify == "Middle Left":
            textPath.translate(-jRect.left(), -jRect.top() / 2.0)
        elif self.objTextJustify == "Middle Center":
            textPath.translate(-jRect.center().x(), -jRect.top() / 2.0)
        elif self.objTextJustify == "Middle Right":
            textPath.translate(-jRect.right(), -jRect.top() / 2.0)
        elif self.objTextJustify == "Bottom Left":
            textPath.translate(-jRect.bottomLeft())
        elif self.objTextJustify == "Bottom Center":
            textPath.translate(-jRect.center().x(), -jRect.bottom())
        elif self.objTextJustify == "Bottom Right":
            textPath.translate(-jRect.bottomRight())

        # Backward or Upside Down.
        if self.objTextBackward or self.objTextUpsideDown:

            horiz = 1.0  # qreal
            vert = 1.0  # qreal
            if self.objTextBackward:
                horiz = -1.0
            if self.objTextUpsideDown:
                vert = -1.0

            flippedPath = QPainterPath()

            element = QPainterPath.Element
            P2 = QPainterPath.Element
            P3 = QPainterPath.Element
            P4 = QPainterPath.Element
            for i in range(0, textPath.elementCount(
            )):  # for(int i = 0; i < textPath.elementCount(); ++i)

                element = textPath.elementAt(i)
                if element.isMoveTo():
                    flippedPath.moveTo(horiz * element.x, vert * element.y)

                elif element.isLineTo():
                    flippedPath.lineTo(horiz * element.x, vert * element.y)

                elif element.isCurveTo():
                    # start point P1 is not needed
                    P2 = textPath.elementAt(i)  # control point
                    P3 = textPath.elementAt(i + 1)  # control point
                    P4 = textPath.elementAt(i + 2)  # end point

                    flippedPath.cubicTo(horiz * P2.x, vert * P2.y,
                                        horiz * P3.x, vert * P3.y,
                                        horiz * P4.x, vert * P4.y)

            objTextPath = flippedPath

        else:
            objTextPath = textPath

        # Add the grip point to the shape path.
        gripPath = objTextPath  # QPainterPath
        gripPath.connectPath(objTextPath)
        gripPath.addRect(-0.00000001, -0.00000001, 0.00000002, 0.00000002)
        self.setObjectPath(gripPath)
Ejemplo n.º 4
0
class Bar(QToolBar):

    clicked = Signal(str)

    def __init__(self, orientation=Qt.Horizontal, parent=None):
        super().__init__(parent)
        self.setOrientation(orientation)
        policy = QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed,
                             QSizePolicy.ButtonBox)
        policy.setHeightForWidth(True)
        policy.setWidthForHeight(True)
        self.setSizePolicy(policy)
        self.setFloatable(False)
        self.setToolTip("""<p><b>Goto Letter</b></p>
<p>Left-click a letter to navigate to the first entry starting with that
letter.</p>
<p>Right-click a letter to navigate to the first entry starting with the
most recently left-clicked letter, plus any subsequent right-clicked
letters, and this letter.</p>
<p>(One left-click or a fifth right-click clears the previous letters.)</p>
<p>For example, left-click <i>S</i> to go to the first “s” entry. Then
right-click <i>A</i> to go to the first “sa” entry, then right-click
<i>T</i> to go to the first “sat” entry.</p>""")
        self.timer = QTimer(self)
        self.timer.setSingleShot(True)
        self.timer.timeout.connect(self.onTimeout)
        size = self.font().pointSizeF()
        strategy = QFont.StyleStrategy(QFont.PreferOutline
                                       | QFont.PreferAntialias
                                       | QFont.PreferQuality)
        self.mainFont = QFont("courier new")
        self.mainFont.setFixedPitch(True)
        self.mainFont.setStyleStrategy(strategy)
        self.mainFont.setStyleHint(QFont.Monospace)
        self.mainFont.setPointSizeF(size * 1.5)
        self.mainFont.setBold(True)
        self.mainFont.setItalic(False)
        self.setFont(self.mainFont)
        self.smallFont = QFont("helvetica")
        self.smallFont.setStyleStrategy(strategy)
        self.smallFont.setStyleHint(QFont.Times)
        self.smallFont.setPointSizeF(size * 1.25)
        self.smallFont.setBold(False)
        self.smallFont.setItalic(False)
        self.letters = string.ascii_uppercase
        self.word = None

    def sizeHint(self):
        return self.minimumSizeHint()

    def minimumSizeHint(self):
        count = len(self.letters)
        widthAll = self.fontMetrics().width(self.letters) * H_GAP_PC
        widthW = self.fontMetrics().width("WW")
        heightOne = self.fontMetrics().height()
        if self.orientation == Qt.Vertical:
            return QSize(widthW, (heightOne * count) + HANDLE_OFFSET)
        return QSize(widthAll + HANDLE_OFFSET, heightOne * V_GAP_PC)

    def paintEvent(self, event):
        super().paintEvent(event)
        letterPen = QPen(Qt.darkGreen if self.isEnabled() else Qt.lightGray)
        wordPen = QPen(Qt.darkCyan if self.isEnabled() else Qt.lightGray)
        painter = QPainter(self)
        painter.setRenderHints(QPainter.TextAntialiasing)
        painter.setPen(letterPen)
        heightOne = self.fontMetrics().height()
        widthOne = self.fontMetrics().width("W")
        if self.orientation == Qt.Vertical:
            y = HANDLE_OFFSET
            for c in self.letters:
                rect = QRectF(X_OFFSET, y, widthOne, heightOne)
                painter.drawText(rect, Qt.AlignCenter, c)
                y += heightOne
            if self.word is not None:
                painter.setFont(self.smallFont)
                painter.setPen(wordPen)
                rect = self.rect().adjusted(0, HANDLE_OFFSET, -widthOne / 3, 0)
                painter.drawText(rect,
                                 Qt.AlignTop | Qt.AlignRight | Qt.TextWordWrap,
                                 "\n".join(self.word.lower()))
                painter.setPen(letterPen)
                painter.setFont(self.mainFont)
        else:
            widthOne *= H_GAP_PC
            x = HANDLE_OFFSET
            for c in self.letters:
                rect = QRectF(x, 0, widthOne, heightOne * V_GAP_PC)
                painter.drawText(rect, Qt.AlignCenter, c)
                x += widthOne
            if self.word is not None:
                painter.setFont(self.smallFont)
                painter.setPen(wordPen)
                rect = self.rect().adjusted(HANDLE_OFFSET, 0, 0, 0)
                painter.drawText(rect, Qt.AlignBottom | Qt.AlignLeft,
                                 self.word.lower())
                painter.setPen(letterPen)
                painter.setFont(self.mainFont)

    def mousePressEvent(self, event):
        if self.orientation == Qt.Vertical:
            if event.y() < HANDLE_OFFSET:
                event.ignore()
                return
            heightOne = self.fontMetrics().height()
            i = clamp(0, int((event.y() - HANDLE_OFFSET) / heightOne),
                      len(self.letters) - 1)
        else:
            if event.x() < HANDLE_OFFSET:
                event.ignore()
                return
            widthOne = self.fontMetrics().width("W") * H_GAP_PC
            i = clamp(0, int((event.x() - HANDLE_OFFSET) / widthOne),
                      len(self.letters) - 1)
        event.accept()
        word = self.letters[i]
        if event.button() == Qt.LeftButton or self.word is None:
            self.word = word
        elif event.button() == Qt.RightButton:
            if len(self.word) >= 4:
                self.word = word
            elif self.word is not None:
                self.word += word
        if self.word is not None:
            self.clicked.emit(self.word.lower())
            self.timer.start(10000)
        self.update()

    def contextMenuEvent(self, event):
        if ((self.orientation == Qt.Vertical and event.y() < HANDLE_OFFSET) or
            (self.orientation == Qt.Horizontal and event.x() < HANDLE_OFFSET)):
            event.ignore()
        else:
            event.accept()

    def setOrientation(self, orientation):
        self.orientation = orientation
        self.update()

    def heightForWidth(self, width):
        count = len(self.letters)
        heightOne = self.fontMetrics().height()
        widthAll = self.fontMetrics().width(self.letters) * H_GAP_PC
        if widthAll > width:  # Needs to be vertical
            return heightOne * count
        return heightOne * V_GAP_PC  # Needs to be horizontal

    def onTimeout(self):
        self.timer.stop()
        if self.word is not None:
            self.word = None
            self.update()
    def setObjectText(self, strng):
        """
        TOWRITE

        :param `strng`: TOWRITE
        :type `strng`: QString
        """
        self.objText = strng
        textPath = QPainterPath()
        font = QFont()
        font.setFamily(self.objTextFont)
        font.setPointSizeF(self.objTextSize)
        font.setBold(self.objTextBold)
        font.setItalic(self.objTextItalic)
        font.setUnderline(self.objTextUnderline)
        font.setStrikeOut(self.objTextStrikeOut)
        font.setOverline(self.objTextOverline)
        textPath.addText(0., 0., font, strng)

        # Translate the path based on the justification.
        jRect = textPath.boundingRect()  # QRectF
        if   self.objTextJustify == "Left":          textPath.translate(-jRect.left(), 0)
        elif self.objTextJustify == "Center":        textPath.translate(-jRect.center().x(), 0)
        elif self.objTextJustify == "Right":         textPath.translate(-jRect.right(), 0)
        elif self.objTextJustify == "Aligned":       pass # TODO: TextSingleObject Aligned Justification
        elif self.objTextJustify == "Middle":        textPath.translate(-jRect.center())
        elif self.objTextJustify == "Fit":           pass # TODO: TextSingleObject Fit Justification
        elif self.objTextJustify == "Top Left":      textPath.translate(-jRect.topLeft())
        elif self.objTextJustify == "Top Center":    textPath.translate(-jRect.center().x(), -jRect.top())
        elif self.objTextJustify == "Top Right":     textPath.translate(-jRect.topRight())
        elif self.objTextJustify == "Middle Left":   textPath.translate(-jRect.left(), -jRect.top()/2.0)
        elif self.objTextJustify == "Middle Center": textPath.translate(-jRect.center().x(), -jRect.top()/2.0)
        elif self.objTextJustify == "Middle Right":  textPath.translate(-jRect.right(), -jRect.top()/2.0)
        elif self.objTextJustify == "Bottom Left":   textPath.translate(-jRect.bottomLeft())
        elif self.objTextJustify == "Bottom Center": textPath.translate(-jRect.center().x(), -jRect.bottom())
        elif self.objTextJustify == "Bottom Right":  textPath.translate(-jRect.bottomRight())

        # Backward or Upside Down.
        if self.objTextBackward or self.objTextUpsideDown:

            horiz = 1.0  # qreal
            vert = 1.0   # qreal
            if self.objTextBackward:
                horiz = -1.0
            if self.objTextUpsideDown:
                vert = -1.0

            flippedPath = QPainterPath()

            element = QPainterPath.Element
            P2      = QPainterPath.Element
            P3      = QPainterPath.Element
            P4      = QPainterPath.Element
            for i in range(0, textPath.elementCount()):  # for(int i = 0; i < textPath.elementCount(); ++i)

                element = textPath.elementAt(i)
                if element.isMoveTo():
                    flippedPath.moveTo(horiz * element.x, vert * element.y)

                elif element.isLineTo():
                    flippedPath.lineTo(horiz * element.x, vert * element.y)

                elif element.isCurveTo():
                                                    # start point P1 is not needed
                    P2 = textPath.elementAt(i)      # control point
                    P3 = textPath.elementAt(i + 1)  # control point
                    P4 = textPath.elementAt(i + 2)  # end point

                    flippedPath.cubicTo(horiz * P2.x, vert * P2.y,
                                        horiz * P3.x, vert * P3.y,
                                        horiz * P4.x, vert * P4.y)

            objTextPath = flippedPath

        else:
            objTextPath = textPath

        # Add the grip point to the shape path.
        gripPath = objTextPath  # QPainterPath
        gripPath.connectPath(objTextPath)
        gripPath.addRect(-0.00000001, -0.00000001, 0.00000002, 0.00000002)
        self.setObjectPath(gripPath)