def update_complete_list(self, lines=None, extra=None): self.clear() if lines or extra: self.show_me() if lines: for i in [x for x in lines if not x.name == 'mro']: item = QListWidgetItem(i.name) item.setData(32, i) self.addItem(item) if extra: font = self.font() font.setItalic(True) font.setPointSize(font.pointSize() * 0.8) for e in extra: item = QListWidgetItem(e.name) item.setData(32, e) item.setFont(font) self.addItem(item) font = QFont('monospace', self.line_height, False) fm = QFontMetrics(font) width = fm.width(' ') * max([len(x.name) for x in lines or extra]) + 40 self.resize(max(250, width), 250) self.setCurrentRow(0) else: self.hide_me()
def paint_background(self, painter, option, index): """ Overrides base paint_background icon function Draw the background for the item :param painter: QPainter :param option: QStyleOptionViewItem :param index: QModelIndex """ super(GroupDataItemView, self).paint_background(painter, option, index) painter.setPen(QPen(Qt.NoPen)) visual_rect = self.visualRect(option) text = self.name() metrics = QFontMetrics(self._font) text_width = metrics.width(text) padding = (25 * self.dpi()) visual_rect.setX(text_width + padding) visual_rect.setY(visual_rect.y() + (visual_rect.height() / 2)) visual_rect.setHeight(2 * self.dpi()) visual_rect.setWidth(visual_rect.width() - padding) color = QColor(self.text_color().red(), self.text_color().green(), self.text_color().blue(), 10) painter.setBrush(QBrush(color)) painter.drawRect(visual_rect)
def __init__(self, controller, desktop=None, parent=None): super(ScriptEditor, self).__init__(parent) self._controller = controller self._font_size = 12 self._completer = completers.ScriptCompleter(parent=parent, editor=self) self._desktop = desktop self._parent = parent self._syntax_highlighter = None self._use_jedi = True font = QFont(consts.FONT_NAME) font.setStyleHint(consts.FONT_STYLE) font.setFixedPitch(True) self.setFont(font) self.document().setDefaultFont( QFont(consts.FONT_NAME, consts.MIN_FONT_SIZE, consts.FONT_STYLE)) metrics = QFontMetrics(self.document().defaultFont()) self.setTabStopWidth(consts.TAB_STOP * metrics.width(' ')) self.setAcceptDrops(True) self.setWordWrapMode(QTextOption.NoWrap) shortcut = QShortcut(QKeySequence('Ctrl+S'), self) shortcut.activated.connect(self._test) if self._controller.settings: self.apply_highlighter(self._controller.settings.get('theme')) self.change_font_size(True)
def setText(self, text): self.m_text = text metrics = QFontMetrics(self.m_font) self.m_textRect = QRectF( metrics.boundingRect(QRect(0, 0, 150, 150), Qt.AlignLeft, self.m_text)) self.m_textRect.translate(5, 5) self.prepareGeometryChange() self.m_rect = self.m_textRect.adjusted(-5, -5, 5, 5)
def resizeEvent(self, event): if self._elided: has_icon = self.icon() and not self.icon().isNull() if has_icon: font_metrics = QFontMetrics(self.font()) elided = font_metrics.elidedText(self._text, Qt.ElideMiddle, self.width() - 30) super(BaseButton, self).setText(elided) super(BaseButton, self).resizeEvent(event)
def text_width(self, column): """ Returns the text width of the given column :param column: int :return: int """ text = self.text(column) font = self.font(column) metrics = QFontMetrics(font) text_width = metrics.width(text) return text_width
def resizeColumnToContents(self, column): """ Overrides base QTreeWidget resizeColumnToContents function Resize the given column to the data of that column :param column: int or str """ width = 0 for item in self.items(): text = item.text(column) font = item.font(column) metrics = QFontMetrics(font) text_width = metrics.width(text) + item.padding() width = max(width, text_width) self.setColumnWidth(column, width)
def __init__(self, parent=None): super(OutputConsole, self).__init__(parent) self.setWordWrapMode(QTextOption.NoWrap) font = QFont('Courier') font.setStyleHint(QFont.Monospace) font.setFixedPitch(True) self.setFont(font) self._font_size = 14 self.document().setDefaultFont( QFont('Courier', self._font_size, QFont.Monospace)) metrics = QFontMetrics(self.document().defaultFont()) self.setTabStopWidth(4 * metrics.width(' ')) self.setMouseTracking(True) self.setReadOnly(True) self.textChanged.connect(self._on_text_changed)
def set(self, U=None, vmin=None, vmax=None): # normalize U fm = QFontMetrics(self.font()) self.vmin = vmin if vmin is not None else (np.min(U) if U is not None else 0.) self.vmax = vmax if vmax is not None else (np.max(U) if U is not None else 1.) difference = abs(self.vmin - self.vmax) if difference == 0: precision = 3 else: precision = m.log(max(abs(self.vmin), abs(self.vmax)) / difference, 10) + 1 precision = int(min(max(precision, 3), 8)) self.vmin_str = format(('{:.' + str(precision) + '}').format(self.vmin)) self.vmax_str = format(('{:.' + str(precision) + '}').format(self.vmax)) self.vmin_width = fm.width(self.vmin_str) self.vmax_width = fm.width(self.vmax_str) self.text_height = fm.height() * 1.5 self.text_ascent = fm.ascent() * 1.5 self.text_descent = fm.descent() * 1.5 self.setMinimumSize(max(self.vmin_width, self.vmax_width) + 20, 300) self.update()
def set(self, U=None, vmin=None, vmax=None): # normalize U fm = QFontMetrics(self.font()) self.vmin = vmin if vmin is not None else ( np.min(U) if U is not None else 0.) self.vmax = vmax if vmax is not None else ( np.max(U) if U is not None else 1.) difference = abs(self.vmin - self.vmax) if difference == 0: precision = 3 else: precision = m.log( max(abs(self.vmin), abs(self.vmax)) / difference, 10) + 1 precision = int(min(max(precision, 3), 8)) self.vmin_str = format( ('{:.' + str(precision) + '}').format(self.vmin)) self.vmax_str = format( ('{:.' + str(precision) + '}').format(self.vmax)) self.vmin_width = fm.width(self.vmin_str) self.vmax_width = fm.width(self.vmax_str) self.text_height = fm.height() * 1.5 self.text_ascent = fm.ascent() * 1.5 self.text_descent = fm.descent() * 1.5 self.setMinimumSize( max(self.vmin_width, self.vmax_width) + 20, 300) self.update()
def paint(self, painter, option, index): painter.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform) model = index.model() view = self.parent() if view.hasFocus() and option.state & QStyle.State_MouseOver: painter.setPen(Qt.NoPen) painter.setBrush(Qt.gray) painter.drawRoundedRect(option.rect.adjusted(1, 1, -1, -1), self._ICON_MARGIN, self._ICON_MARGIN) pixmap = model.data(index, Qt.DecorationRole).pixmap(view.iconSize()) pm_rect = QRect( option.rect.topLeft() + QPoint(self._ICON_MARGIN + 1, self._ICON_MARGIN + 1), view.iconSize() - QSize(self._ICON_MARGIN * 2, self._ICON_MARGIN * 2)) painter.drawPixmap(pm_rect, pixmap) if option.state & QStyle.State_Selected: painter.setPen( QPen(Qt.red, 1.0, Qt.SolidLine, Qt.SquareCap, Qt.RoundJoin)) painter.setBrush(Qt.NoBrush) painter.drawRect(option.rect.adjusted(2, 2, -2, -2)) font = view.font() fm = QFontMetrics(font) text = os.path.splitext( os.path.basename(model.data(index, Qt.DisplayRole)))[0] text = fm.elidedText(text, Qt.ElideRight, view.iconSize().width() - 4) text_opt = QTextOption() text_opt.setAlignment(Qt.AlignHCenter) txt_rect = QRectF(QPointF(pm_rect.bottomLeft() + QPoint(0, 1)), QPointF(option.rect.bottomRight() - QPoint(4, 3))) painter.save() painter.setPen(Qt.NoPen) painter.setBrush(QColor(22, 22, 22, 220)) painter.drawRoundedRect(txt_rect.adjusted(-2, -2, 2, 2), 2, 2) painter.restore() painter.setPen(self.parent().palette().color(QPalette.WindowText)) painter.drawText(txt_rect, text, text_opt) font.setPointSize(8) fm = QFontMetrics(font) item = model.itemFromIndex(index) size_text = '%d x %d' % (item.size.width(), item.size.height()) size_rect = fm.boundingRect(option.rect, Qt.AlignLeft | Qt.AlignTop, size_text) size_rect.translate(4, 4) painter.save() painter.setPen(Qt.NoPen) painter.setBrush(QColor(22, 22, 22, 220)) painter.drawRoundedRect(size_rect.adjusted(-2, -2, 2, 2), 2, 2) painter.restore() painter.setFont(font) painter.drawText(size_rect, size_text)
def __init__(self, *args, **kwargs): self._style = kwargs.pop('button_style', None) self._pad = kwargs.pop('icon_padding', 0) self._min_size = kwargs.pop('min_size', 8) self._radius = kwargs.pop('radius', 5) self._icon = kwargs.pop('icon', None) QPushButton.__init__(self, *args, **kwargs) animation.BaseAnimObject.__init__(self) self._font_metrics = QFontMetrics(self.font()) self._border_width = kwargs.get('border_width') if self._icon: self.setIcon(self._icon)
def _paint_text(self, painter, option, column): """ Internal function used to paint the text :param painter: QPainter :param option: QStyleOption :param column: int """ if self.viewer().is_icon_view(): text = self.item.full_name() if self.item else self.name() else: label = self.label_from_column(column) text = self.display_text(label) color = self.text_color() is_selected = option.state & QStyle.State_Selected if is_selected: color = self.text_selected_color() visual_rect = self.visual_rect(option) width = visual_rect.width() height = visual_rect.height() padding = self.padding() x = padding / 2 y = padding / 2 visual_rect.translate(x, y) visual_rect.setWidth(width - padding) visual_rect.setHeight(height - padding) font = self.font(column) align = self.textAlignment(column) metrics = QFontMetrics(font) text_width = 1 if text: text_width = metrics.width(text) if text_width > visual_rect.width() - padding: visual_width = visual_rect.width() text = metrics.elidedText(text, Qt.ElideMiddle, visual_width) align = Qt.AlignLeft align = align | Qt.AlignVCenter rect = QRect(visual_rect) if self.viewer().is_icon_view(): if self.is_label_over_item() or self.is_label_under_item(): padding = 8 if padding < 8 else padding height = metrics.height() + (padding / 2) y = (rect.y() + rect.height()) - height rect.setY(y) rect.setHeight(height) if self.is_label_over_item(): color2 = self.viewer().background_color().toRgb() color2.setAlpha(200) painter.setPen(Qt.NoPen) painter.setBrush(QBrush(color2)) painter.drawRect(rect) pen = QPen(color) painter.setPen(pen) painter.setFont(font) painter.drawText(rect, align, text)