Exemplo n.º 1
0
    def rendered_size(self, text):
        if self.font is None:
            return 0, 0

        fm = QFontMetrics(self.font)
        rect = fm.boundingRect(text)
        return rect.width(), rect.height()
Exemplo n.º 2
0
    def render(self, text, _, color):
        if self.font is None:
            return "", (0, 0)

        fm = QFontMetrics(self.font)
        rect = fm.boundingRect(text)
        im = QImage(
            rect.x() + rect.width(),
            rect.height(),
            QImage.Format_ARGB32_Premultiplied,
        )
        im.fill(QColor(0, 0, 0, 0))
        painter = QPainter()
        painter.begin(im)
        painter.setPen(QPen(QColor(*color)))
        painter.setFont(self.font)
        painter.drawText(QPoint(0 - rect.x(), 0 - rect.y()), text)
        painter.end()

        bits = im.bits()
        try:
            pixels = bits.tobytes()
        except AttributeError:
            bits.setsize(im.byteCount())
            pixels = bytes(bits)
        return pixels, (rect.x() + rect.width(), rect.height())
Exemplo n.º 3
0
 def measure_text(self, text):
     font = self._qwidget.font()
     metrics = QFontMetrics(font)
     return metrics.width(text), metrics.height()
Exemplo n.º 4
0
 def measure_text(self, text):
     widget = getattr(self, "_widget", self)
     font = widget.font()
     metrics = QFontMetrics(font)
     return metrics.width(text), metrics.height()
Exemplo n.º 5
0
 def measure_text(self, text):
     font = self.widget().font()
     metrics = QFontMetrics(font)
     return metrics.width(text), metrics.height()
Exemplo n.º 6
0
    def update_style(self):
        # There seems to be an issue with specifying padding-top and
        # padding-bottom for a QComboBox. The internal pop menu also gets
        # padding added, resulting in ugly white borders at the top/bottom of
        # the popup, and there seems to be no way to remove this. So instead,
        # we calculate minimum height based on font height manually.
        theme = get_theme(self)
        padding = theme.choice_padding()
        if not padding:
            # Indicates that we do not want custom styling
            return
        fontmetrics = QFontMetrics(self._qwidget.font())
        fontheight = fontmetrics.height()
        # print(fontheight)
        # FIXME: Assumed
        border = 4
        min_height = fontheight + padding.top + padding.bottom + border
        self.set_min_height(min_height)

        padding = theme.textfield_padding()
        # Padding top/bottom does not work before it does not influence the
        # parent (ComboBox) height.
        self._qwidget.lineEdit().setStyleSheet(f"""
            QLineEdit {{
                /*
                background: #ffff00;
                border: 2px solid #00ff00;
                color: red;

                background: #ffff00;
                color: red;
                */
                /* padding-top: {padding.top}px; */
                padding-right: {padding.right}px;
                padding-left: {padding.left}px;
                /* padding-bottom: {padding.bottom}px; */
            }}
        """)
        self._qwidget.setStyleSheet(f"""
            QComboBox {{
                /*
                border: 2px solid #ffff00;
                */
            }}
            /*
            QComboBox QAbstractItemView {{
                border: 2px solid darkgray;
                selection-background-color: lightgray;
                padding-top: 10px;
                height: 40px;
            }}
            QComboBox QAbstractItemView::item {{
                border: 2px solid darkgray;
                selection-background-color: lightgray;
                padding-top: 10px;
                height: 40px;
            }}
            QComboBox QListView::item {{
                border: 2px solid darkgray;
                selection-background-color: lightgray;
                padding-top: 10px;
                height: 40px;
            }}
            */
        """)