Example #1
0
    def resize_column(self, col: int, str_text: Optional[str]) -> None:
        """
        Resize a column.
        """

        if str_text is None:
            return

        str_text = str(str_text)

        field = self.model().metadata().indexFieldObject(col)
        if field.name() in self.widthCols_.keys():
            if self.columnWidth(col) < self.widthCols_[field.name()]:
                self.header().resizeSection(col, self.widthCols_[field.name()])
        else:
            wC = self.header().sectionSize(col)

            fm = Qt.QFontMetrics(self.header().font())
            wH = fm.horizontalAdvance(field.alias() + "W")
            if wH < wC:
                wH = wC

            wC = fm.horizontalAdvance(str_text) + fm.maxWidth()
            if wC > wH:
                self.header().resizeSection(col, wC)
                if col == 0 and self.popup_:
                    pw = self.parentWidget()
                    if pw and pw.width() < wC:
                        self.resize(wC, pw.height())
                        pw.resize(wC, pw.height())
Example #2
0
 def drawItemText(self,
                  painter,
                  rect,
                  flags,
                  pal,
                  enabled,
                  text,
                  textRole=Q.QPalette.NoRole):
     """Redefined from QStyle."""
     astr = Q.QFontMetrics(painter.font()).\
         elidedText(text, Q.Qt.ElideRight, rect.width())
     super(Style, self).drawItemText(painter, rect, flags, pal, enabled,
                                     astr, textRole)
Example #3
0
 def genBitmap(self):
     if self.bitmap is not None:
         return self.bitmap
     font = Qt.QFont()
     font.fromString(self.font)
     # for some reason the bounding rect calculated here is wrong by quite a
     # bit, so let's use twice as a rough estimation
     width = 2 * Qt.QFontMetrics(font).boundingRect(self.text).width()
     if not width:
         return Bitmap()
     image = Qt.QImage(width, HEIGHT, Qt.QImage.Format_Mono)
     image.fill(0)
     with Qt.QPainter(image) as painter:
         # no antialiasing
         painter.setRenderHints(Qt.QPainter.RenderHints())
         painter.setFont(font)
         painter.setPen(Qt.QPen(Qt.QColor('white')))
         # here we get the real width of the drawn text
         real_width = painter.drawText(0, -self.offset, width,
                                       HEIGHT + self.offset,
                                       Qt.Qt.AlignTop | Qt.Qt.AlignLeft,
                                       self.text).width()
     return Bitmap(image, real_width)