本文整理汇总了Python中AnyQt.QtGui.QFontMetrics类的典型用法代码示例。如果您正苦于以下问题:Python QFontMetrics类的具体用法?Python QFontMetrics怎么用?Python QFontMetrics使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了QFontMetrics类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: paint
def paint(self, painter, option, widget=None):
rect = self.rect()
if self.isSelected():
option.state ^= QStyle.State_Selected
font = self.document().defaultFont()
painter.setFont(font)
if self.parent:
draw_text = self.node_inst.description
if self.parent.x() > self.x(): # node is to the left
fm = QFontMetrics(font)
x = rect.width() / 2 - fm.width(draw_text) - 4
else:
x = rect.width() / 2 + 4
painter.drawText(QPointF(x, -self.line_descent - 1), draw_text)
painter.save()
painter.setBrush(self.backgroundBrush)
painter.setPen(QPen(Qt.black, 3 if self.isSelected() else 0))
adjrect = rect.adjusted(-3, 0, 0, 0)
if not self.node_inst.children:
painter.drawRoundedRect(adjrect, 4, 4)
else:
painter.drawRect(adjrect)
painter.restore()
painter.setClipRect(rect)
return QGraphicsTextItem.paint(self, painter, option, widget)
示例2: splash_screen
def splash_screen():
"""
"""
pm = QPixmap(
pkg_resources.resource_filename(
__name__, "icons/orange-splash-screen.png")
)
version = QCoreApplication.applicationVersion()
size = 21 if len(version) < 5 else 16
font = QFont("Helvetica")
font.setPixelSize(size)
font.setBold(True)
font.setItalic(True)
font.setLetterSpacing(QFont.AbsoluteSpacing, 2)
metrics = QFontMetrics(font)
br = metrics.boundingRect(version).adjusted(-5, 0, 5, 0)
br.moveCenter(QPoint(436, 224))
p = QPainter(pm)
p.setRenderHint(QPainter.Antialiasing)
p.setRenderHint(QPainter.TextAntialiasing)
p.setFont(font)
p.setPen(QColor("#231F20"))
p.drawText(br, Qt.AlignCenter, version)
p.end()
return pm, QRect(88, 193, 200, 20)
示例3: __naturalsh
def __naturalsh(self):
fm = QFontMetrics(self.font())
spacing = self.__spacing
N = len(self.__items)
width = max((fm.width(text) for text in self.__items),
default=0)
height = N * fm.height() + (N - 1) * spacing
return QSizeF(width, height)
示例4: __textLayout
def __textLayout(self):
fm = QFontMetrics(self.font())
text = self.defaultAction().text()
words = deque(text.split())
lines = []
curr_line = ""
curr_line_word_count = 0
option = QStyleOptionToolButton()
option.initFrom(self)
margin = self.style().pixelMetric(QStyle.PM_ButtonMargin, option, self)
width = self.width() - 2 * margin
while words:
w = words.popleft()
if curr_line_word_count:
line_extended = " ".join([curr_line, w])
else:
line_extended = w
line_w = fm.boundingRect(line_extended).width()
if line_w >= width:
if curr_line_word_count == 0 or len(lines) == 1:
# A single word that is too long must be elided.
# Also if the text overflows 2 lines
# Warning: hardcoded max lines
curr_line = fm.elidedText(line_extended, Qt.ElideRight,
width)
curr_line = curr_line
else:
# Put the word back
words.appendleft(w)
lines.append(curr_line)
curr_line = ""
curr_line_word_count = 0
if len(lines) == 2:
break
else:
curr_line = line_extended
curr_line_word_count += 1
if curr_line:
lines.append(curr_line)
text = "\n".join(lines)
text = text.replace('&', '&&') # Need escaped ampersand to show
self.__text = text
示例5: update_contents
def update_contents(self):
self.prepareGeometryChange()
self.setTextWidth(-1)
self.setTextWidth(self.document().idealWidth())
self.droplet.setPos(self.rect().center().x(), self.rect().height())
self.droplet.setVisible(bool(self.branches))
fm = QFontMetrics(self.document().defaultFont())
attr = self.node_inst.attr
self.attr_text_w = fm.width(attr.name if attr else "")
self.attr_text_h = fm.lineSpacing()
self.line_descent = fm.descent()
if self.pie is not None:
self.pie.setPos(self.rect().right(), self.rect().center().y())
示例6: defaultTextGeometry
def defaultTextGeometry(self, point):
"""
Return the default text geometry. Used in case the user single
clicked in the scene.
"""
font = self.annotation_item.font()
metrics = QFontMetrics(font)
spacing = metrics.lineSpacing()
margin = self.annotation_item.document().documentMargin()
rect = QRectF(QPointF(point.x(), point.y() - spacing - margin),
QSizeF(150, spacing + 2 * margin))
return rect
示例7: __init__
def __init__(self, model, node_inst, parent=None):
super().__init__(parent)
self.model = model
self.node_inst = node_inst
fm = QFontMetrics(self.document().defaultFont())
attr = node_inst.attr
self.attr_text_w = fm.width(attr.name if attr else "")
self.attr_text_h = fm.lineSpacing()
self.line_descent = fm.descent()
self._rect = None
if model.domain.class_var.is_discrete:
self.pie = PieChart(node_inst.value, 8, self)
else:
self.pie = None
示例8: __layout
def __layout(self):
crect = self.contentsRect()
spacing = self.__spacing
N = len(self.__items)
if not N:
return
fm = QFontMetrics(self.font())
naturalheight = fm.height()
th = (crect.height() - (N - 1) * spacing) / N
if th > naturalheight and N > 1:
th = naturalheight
spacing = (crect.height() - N * th) / (N - 1)
for i, item in enumerate(self.__textitems):
item.setPos(crect.left(), crect.top() + i * (th + spacing))
示例9: set_widget_value
def set_widget_value(widget, value):
if isinstance(widget, QLineEdit):
widget.setText(value)
elif isinstance(widget, QPlainTextEdit):
return widget.setPlainText(value)
elif isinstance(widget, QLabel):
if widget.openExternalLinks():
widget.setToolTip(value)
metrics = QFontMetrics(widget.font())
display = metrics.elidedText(value, Qt.ElideRight, widget.width())
value = urllib.parse.unquote(value) # Because setText() escapes percent-encoded values and corrupts them
value = '<a href="{value}">{display}</a>'.format(**locals())
widget.setText(value)
elif isinstance(widget, QCheckBox):
return widget.setChecked(value not in (False, '0', '', 'False'))
else:
raise RuntimeError()
示例10: __init__
def __init__(self, tree_adapter, node_inst, parent=None):
super().__init__(parent)
self.tree_adapter = tree_adapter
self.model = self.tree_adapter.model
self.node_inst = node_inst
fm = QFontMetrics(self.document().defaultFont())
attr = self.tree_adapter.attribute(node_inst)
self.attr_text_w = fm.width(attr.name if attr else "")
self.attr_text_h = fm.lineSpacing()
self.line_descent = fm.descent()
self._rect = None
if self.model.domain.class_var.is_discrete:
self.pie = PieChart(self.tree_adapter.get_distribution(node_inst)[0], 8, self)
else:
self.pie = None
示例11: paintEvent
def paintEvent(self, event):
QLineEdit.paintEvent(self, event)
if not self.text() and self.placeholderText() and \
not self.hasFocus():
p = QStylePainter(self)
font = self.font()
metrics = QFontMetrics(font)
p.setFont(font)
color = self.palette().color(QPalette.Mid)
p.setPen(color)
left, top, right, bottom = self.getTextMargins()
contents = self.contentsRect()
contents = contents.adjusted(left, top, -right, -bottom)
text = metrics.elidedText(self.placeholderText(),
Qt.ElideMiddle,
contents.width())
p.drawText(contents, Qt.AlignLeft | Qt.AlignVCenter, text)
示例12: splash_screen
def splash_screen():
# type: () -> Tuple[QPixmap, QRect]
"""
Return a splash screen pixmap and an text area within it.
The text area is used for displaying text messages during application
startup.
The default implementation returns a bland rectangle splash screen.
Returns
-------
t : Tuple[QPixmap, QRect]
A QPixmap and a rect area within it.
"""
path = pkg_resources.resource_filename(
__name__, "icons/orange-canvas-core-splash.svg")
pm = QPixmap(path)
version = QCoreApplication.applicationVersion()
if version:
version_parsed = LooseVersion(version)
version_comp = version_parsed.version
version = ".".join(map(str, version_comp[:2]))
size = 21 if len(version) < 5 else 16
font = QFont()
font.setPixelSize(size)
font.setBold(True)
font.setItalic(True)
font.setLetterSpacing(QFont.AbsoluteSpacing, 2)
metrics = QFontMetrics(font)
br = metrics.boundingRect(version).adjusted(-5, 0, 5, 0)
br.moveBottomRight(QPoint(pm.width() - 15, pm.height() - 15))
p = QPainter(pm)
p.setRenderHint(QPainter.Antialiasing)
p.setRenderHint(QPainter.TextAntialiasing)
p.setFont(font)
p.setPen(QColor("#231F20"))
p.drawText(br, Qt.AlignCenter, version)
p.end()
textarea = QRect(15, 15, 170, 20)
return pm, textarea
示例13: _update_points_labels
def _update_points_labels(self):
if self.plotdata.points is None:
return
for point_label in self.plotdata.point_labels:
self.graph.plot_widget.removeItem(point_label)
self.plotdata.point_labels = []
sx, sy = self.graph.view_box.viewPixelSize()
for row in self.plotdata.points:
ti = TextItem()
metrics = QFontMetrics(ti.textItem.font())
text_width = ((RANGE.width())/2. - np.abs(row[0])) / sx
name = row[2].name
ti.setText(name)
ti.setTextWidth(text_width)
ti.setColor(QColor(0, 0, 0))
br = ti.boundingRect()
width = metrics.width(name) if metrics.width(name) < br.width() else br.width()
width = sx * (width + 5)
height = sy * br.height()
ti.setPos(row[0] - (row[0] < 0) * width, row[1] + (row[1] > 0) * height)
self.plotdata.point_labels.append(ti)
self.graph.plot_widget.addItem(ti)
示例14: set_text
def set_text(self):
metrics = QFontMetrics(self.font())
self.setText(metrics.elidedText(self.desc_text, Qt.ElideRight,
self.width() - 15))
示例15: __paintEventNoStyle
def __paintEventNoStyle(self):
p = QPainter(self)
opt = QStyleOptionToolButton()
self.initStyleOption(opt)
fm = QFontMetrics(opt.font)
palette = opt.palette
# highlight brush is used as the background for the icon and background
# when the tab is expanded and as mouse hover color (lighter).
brush_highlight = palette.highlight()
foregroundrole = QPalette.ButtonText
if opt.state & QStyle.State_Sunken:
# State 'down' pressed during a mouse press (slightly darker).
background_brush = brush_darker(brush_highlight, 110)
foregroundrole = QPalette.HighlightedText
elif opt.state & QStyle.State_MouseOver:
background_brush = brush_darker(brush_highlight, 95)
foregroundrole = QPalette.HighlightedText
elif opt.state & QStyle.State_On:
background_brush = brush_highlight
foregroundrole = QPalette.HighlightedText
else:
# The default button brush.
background_brush = palette.button()
rect = opt.rect
icon_area_rect = QRect(rect)
icon_area_rect.setRight(int(icon_area_rect.height() * 1.26))
text_rect = QRect(rect)
text_rect.setLeft(icon_area_rect.right() + 10)
# Background (TODO: Should the tab button have native
# toolbutton shape, drawn using PE_PanelButtonTool or even
# QToolBox tab shape)
# Default outline pen
pen = QPen(palette.color(QPalette.Mid))
p.save()
p.setPen(Qt.NoPen)
p.setBrush(QBrush(background_brush))
p.drawRect(rect)
# Draw the background behind the icon if the background_brush
# is different.
if not opt.state & QStyle.State_On:
p.setBrush(brush_highlight)
p.drawRect(icon_area_rect)
# Line between the icon and text
p.setPen(pen)
p.drawLine(icon_area_rect.topRight(),
icon_area_rect.bottomRight())
if opt.state & QStyle.State_HasFocus:
# Set the focus frame pen and draw the border
pen = QPen(QColor(FOCUS_OUTLINE_COLOR))
p.setPen(pen)
p.setBrush(Qt.NoBrush)
# Adjust for pen
rect = rect.adjusted(0, 0, -1, -1)
p.drawRect(rect)
else:
p.setPen(pen)
# Draw the top/bottom border
if self.position == QStyleOptionToolBox.OnlyOneTab or \
self.position == QStyleOptionToolBox.Beginning or \
self.selected & \
QStyleOptionToolBox.PreviousIsSelected:
p.drawLine(rect.topLeft(), rect.topRight())
p.drawLine(rect.bottomLeft(), rect.bottomRight())
p.restore()
p.save()
text = fm.elidedText(opt.text, Qt.ElideRight, text_rect.width())
p.setPen(QPen(palette.color(foregroundrole)))
p.setFont(opt.font)
p.drawText(text_rect,
int(Qt.AlignVCenter | Qt.AlignLeft) | \
int(Qt.TextSingleLine),
text)
if not opt.icon.isNull():
if opt.state & QStyle.State_Enabled:
mode = QIcon.Normal
else:
mode = QIcon.Disabled
if opt.state & QStyle.State_On:
state = QIcon.On
else:
state = QIcon.Off
icon_area_rect = icon_area_rect
icon_rect = QRect(QPoint(0, 0), opt.iconSize)
#.........这里部分代码省略.........