def strRect(s, fontMetrics: QtGui.QFontMetrics): #baseWidth = fontMetrics.boundingRect(s).width() baseWidth = fontMetrics.horizontalAdvance(s) sWidth = baseWidth # - fontMetrics.leftBearing(s[0]) #sWidth -= fontMetrics.rightBearing(s[-1]) r = QtCore.QRect(0, 0, sWidth, fontMetrics.height()) return r
def updateEditorGeometry(self, editor, option, index): super().updateEditorGeometry(editor, option, index) item = index.model().item_from_index(index) if item.item_type == "scenario active": size = option.rect.size() fm = QFontMetrics(index.data(Qt.FontRole)) dx = fm.horizontalAdvance("active:") size.setWidth(size.width() - dx) editor.set_base_size(size) editor.set_base_offset(QPoint(dx, 0)) editor.update_geometry()
def paintEvent(self, event): sineTable = [ 0, 38, 71, 92, 100, 92, 71, 38, 0, -38, -71, -92, -100, -92, -71, -38 ] metrics = QFontMetrics(self.font()) x = (self.width() - metrics.horizontalAdvance(self.text)) / 2 y = (self.height() + metrics.ascent() - metrics.descent()) / 2 color = QColor() painter = QPainter(self) for i in range(len(self.text)): index = (self.step + i) % 16 color.setHsv((15 - index) * 16, 255, 191) painter.setPen(color) painter.drawText(x, y - ((sineTable[index] * metrics.height()) / 400), str(self.text[i])) x += metrics.horizontalAdvance(self.text[i])
def check_if_name_need_resize_column_to_fit_content(self): new_column_width = 0 for i in range(self.rowCount()): column_font = self.cellWidget(i, self.column_ids["Name"]).font() column_font_metrics = QFontMetrics(column_font) new_column_width = max( new_column_width, column_font_metrics.horizontalAdvance( self.data[i].video_name_with_spaces)) if new_column_width > self.columnWidth(self.column_ids["Name"]): self.resize_name_column_to_fit_content()
def __init__(self, view, label, callback): super().__init__(label) self.callback = callback self.view = view font_name = Settings().get_string('ui.font.name') font_size = Settings().get_integer('ui.font.size') button_font = QFont(font_name, font_size) fm = QFontMetrics(button_font) self.setFont(button_font) self.setFixedWidth(fm.horizontalAdvance(label) + 10) QObject.connect(self, SIGNAL('clicked()'), self.callback)
def update_row_size(self): new_column_width = self.tableHeader.width() self.table.horizontalHeader().setSectionResizeMode( QHeaderView.Interactive) for i in range(self.table.rowCount()): column_font = self.table.item(i, 0).font() column_font_metrics = QFontMetrics(column_font) new_column_width = max( new_column_width, column_font_metrics.horizontalAdvance( self.table.item(i, 0).text())) new_column_width += 15 self.table.setColumnWidth(0, new_column_width)
def __init__(self, title, parent=None): super().__init__(parent) widget = QWidget() layout = QHBoxLayout(widget) layout.setContentsMargins(self.H_MARGIN, self.V_MARGIN, self.H_MARGIN, self.V_MARGIN) layout.setSpacing(self.V_MARGIN) label = QLabel(title, widget) fm = QFontMetrics(label.font()) label.setFixedWidth(fm.horizontalAdvance(title)) self._add_line(widget, layout) layout.addWidget(label) self._add_line(widget, layout) self.setDefaultWidget(widget)
def resize_name_column_to_fit_content(self): # Resize Name Column Only new_column_width = 0 for i in range(self.rowCount()): column_font = self.cellWidget(i, self.column_ids["Name"]).font() column_font_metrics = QFontMetrics(column_font) new_column_width = max( new_column_width, column_font_metrics.horizontalAdvance( self.data[i].video_name_with_spaces)) if new_column_width != 0: self.setColumnWidth(self.column_ids["Name"], new_column_width) for i in range(self.rowCount()): self.data[i].video_name_displayed = chr( 0x200E) + self.data[i].video_name_with_spaces self.cellWidget(i, self.column_ids["Name"]).setText( self.data[i].video_name_displayed)
def __init__(self, title, parent=None): super().__init__(parent) widget = QWidget() layout = QHBoxLayout(widget) layout.setContentsMargins(self.H_MARGIN, self.V_MARGIN, self.H_MARGIN, self.V_MARGIN) layout.setSpacing(self.V_MARGIN) label = QLabel(title, widget) fm = QFontMetrics(label.font()) label.setFixedWidth(fm.horizontalAdvance(title)) lines = QFrame(widget), QFrame(widget) for line in lines: line.setFrameShape(QFrame.HLine) line.setFrameShadow(QFrame.Sunken) layout.addWidget(line) layout.insertWidget(1, label) self.setDefaultWidget(widget)
def get_text_width(self, text): """ Returns painted width of the text """ fm = QFontMetrics(self.font()) return max(20, fm.horizontalAdvance(text))
def _updateAxisTickCount(chart: QtCharts.QChart, axis: QtCharts.QAbstractAxis, newSize: QSize) -> None: """ Given an axis and the size of the view, sets the number of ticks to the best value avoiding too many overlapping labels """ # Get one label as string and the current number of ticks/labels label: str ticks: int if axis.type() == QtCharts.QAbstractAxis.AxisTypeDateTime: ticks = axis.tickCount() # current number of dates shown label = axis.min().toString(axis.format()) elif axis.type() == QtCharts.QAbstractAxis.AxisTypeBarCategory: ticks = axis.count() # number of labels label = axis.at(0) if ticks else None elif axis.type() == QtCharts.QAbstractAxis.AxisTypeCategory: ticks = axis.count() # number of labels labels = axis.categoriesLabels() label = labels[0] if labels else None else: return # Axis type not supported if not label: # No labels set return # Decide which dimension is relevant for resizing margins = chart.margins() # layoutMargins: (left, top, right, bottom) layoutMargins: Tuple[float, ...] = chart.layout().getContentsMargins() if layoutMargins: layoutMargins = tuple( [i if i is not None else 0.0 for i in layoutMargins]) offset: int = 0 if axis.orientation() == Qt.Horizontal: if margins: offset += margins.left() + margins.right() if layoutMargins: offset += layoutMargins[0] + layoutMargins[2] # 'length' is the available space for displaying labels, without margins and the space # between every label length = newSize.width() - offset - (ticks * 10) else: if margins: offset += margins.top() + margins.bottom() if layoutMargins: offset += layoutMargins[1] + layoutMargins[3] length = newSize.height() - offset - (ticks * 10) # Compute the optimal width of the label (in pixel) metrics = QFontMetrics(axis.labelsFont()) optimalWidth: int = metrics.horizontalAdvance( label) * 1.9 # not precise, 1.9 is to fix it # Deal with every type separately if axis.type() == QtCharts.QAbstractAxis.AxisTypeDateTime: # Determine optimal number of ticks to avoid much overlapping newTicks = int(length / optimalWidth) - 1 axis.setTickCount(newTicks) elif axis.type() == QtCharts.QAbstractAxis.AxisTypeCategory or axis.type() == \ QtCharts.QAbstractAxis.AxisTypeBarCategory: labelSpace: float = length / (ticks * 2) if labelSpace < optimalWidth: deg = min([ 90, np.degrees(np.arccos(labelSpace / optimalWidth) * 1.1) ]) axis.setLabelsAngle(deg) else: axis.setLabelsAngle(0)
def sizeChanged(self, newSize): fm = QFontMetrics(self.font()) self.setMaximumWidth(max(min([fm.horizontalAdvance(self.toPlainText())+10, newSize.toSize().width()+10]), 40)) if self.width() < self.document().textWidth(): self.document().setTextWidth(self.width()) self.setFixedHeight(newSize.height()+2)