本文整理汇总了C++中QTextLine::naturalTextWidth方法的典型用法代码示例。如果您正苦于以下问题:C++ QTextLine::naturalTextWidth方法的具体用法?C++ QTextLine::naturalTextWidth怎么用?C++ QTextLine::naturalTextWidth使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QTextLine
的用法示例。
在下文中一共展示了QTextLine::naturalTextWidth方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: drawTextLayout
void TextLabel::drawTextLayout(QPainter *painter, const QTextLayout &layout, const QRect &rect)
{
if (rect.width() < 1 || rect.height() < 1) {
return;
}
QPixmap pixmap(rect.size());
pixmap.fill(Qt::transparent);
QPainter p(&pixmap);
p.setPen(painter->pen());
// Create the alpha gradient for the fade out effect
QLinearGradient alphaGradient(0, 0, 1, 0);
alphaGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
if (layout.textOption().textDirection() == Qt::LeftToRight) {
alphaGradient.setColorAt(0, QColor(0, 0, 0, 255));
alphaGradient.setColorAt(1, QColor(0, 0, 0, 0));
} else {
alphaGradient.setColorAt(0, QColor(0, 0, 0, 0));
alphaGradient.setColorAt(1, QColor(0, 0, 0, 255));
}
QFontMetrics fm(layout.font());
int textHeight = layout.lineCount() * fm.lineSpacing();
QPointF position(0, (rect.height() - textHeight) / 2);
QList<QRect> fadeRects;
int fadeWidth = 30;
// Draw each line in the layout
for (int i = 0; i < layout.lineCount(); i++) {
QTextLine line = layout.lineAt(i);
line.draw(&p, position);
// Add a fade out rect to the list if the line is too long
if (line.naturalTextWidth() > rect.width())
{
int x = int(qMin(line.naturalTextWidth(), (qreal)pixmap.width())) - fadeWidth;
int y = int(line.position().y() + position.y());
QRect r = QStyle::visualRect(layout.textOption().textDirection(), pixmap.rect(),
QRect(x, y, fadeWidth, int(line.height())));
fadeRects.append(r);
}
}
// Reduce the alpha in each fade out rect using the alpha gradient
if (!fadeRects.isEmpty()) {
p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
foreach (const QRect &rect, fadeRects) {
p.fillRect(rect, alphaGradient);
}
示例2: layoutText
void TextLabel::layoutText(QTextLayout &layout, const QString &text, const QSize &constraints)
{
QFontMetrics metrics(layout.font());
int leading = metrics.leading();
int height = 0;
int maxWidth = constraints.width();
int widthUsed = 0;
int lineSpacing = metrics.lineSpacing();
QTextLine line;
layout.setText(text);
layout.beginLayout();
while ((line = layout.createLine()).isValid()) {
height += leading;
// Make the last line that will fit infinitely long.
// drawTextLayout() will handle this by fading the line out
// if it won't fit in the constraints.
if (height + 2 * lineSpacing > constraints.height()) {
line.setPosition(QPoint(0, height));
break;
}
line.setLineWidth(maxWidth);
line.setPosition(QPoint(0, height));
height += int(line.height());
widthUsed = int(qMax(qreal(widthUsed), line.naturalTextWidth()));
}
layout.endLayout();
}
示例3: setContent
void setContent(const ToolTipContent &data)
{
QString html;
if (!data.mainText().isEmpty()) {
html.append("<div><b>" + data.mainText() + "</b></div>");
}
html.append(data.subText());
m_anchor.clear();
m_document->clear();
data.registerResources(m_document);
if (!html.isEmpty()) {
m_document->setHtml("<p>" + html + "</p>");
}
m_document->adjustSize();
m_haloRects.clear();
QTextLayout *layout = m_document->begin().layout();
//layout->setPosition(QPointF(textRect.x(), textBoundingRect->y()));
QTextLine line;
for (int i = 0; i < layout->lineCount(); ++i) {
line = layout->lineAt(i);
// Add halo rect only when a non empty line is found
if (line.naturalTextWidth()) {
m_haloRects.append(line.naturalTextRect().translated(layout->position().toPoint()).toRect().translated(m_margin, m_margin));
}
}
update();
}
示例4: qMax
void KTextDocumentLayout::Private::adjustSize()
{
if (parent->resizeMethod() == KTextDocument::NoResize)
return;
if (parent->shapes().isEmpty())
return;
// Limit auto-resizing to the first shape only (there won't be more
// with auto-resizing turned on, unless specifically set)
KShape *shape = parent->shapes().first();
// Determine the maximum width of all text lines
qreal width = 0;
for (QTextBlock block = parent->document()->begin(); block.isValid(); block = block.next()) {
// The block layout's wrap mode must be QTextOption::NoWrap, thus the line count
// of a valid block must be 1 (otherwise this resizing scheme wouldn't work)
Q_ASSERT(block.layout()->lineCount() == 1);
QTextLine line = block.layout()->lineAt(0);
width = qMax(width, line.naturalTextWidth());
}
// Use position and height of last text line to calculate height
QTextLine line = parent->document()->lastBlock().layout()->lineAt(0);
qreal height = line.position().y() + line.height();
shape->setSize(QSizeF(width, height));
}
示例5: descriptionHeight
int JobItem::descriptionHeight(const QStyleOptionViewItem & option)
{
int textTotalWidth = 0;
int lineWidth = option.rect.width() - 2 * m_itemPadding;
QString description = m_job->description().simplified();
QTextLayout descriptionLayout(description, m_descriptionFont, m_paintDevice);
descriptionLayout.beginLayout();
for (int i = 0; i < m_descriptionLineCount - 1; ++i)
{
QTextLine line = descriptionLayout.createLine();
if (!line.isValid())
{
// There is no text left to be inserted into the layout.
break;
}
line.setLineWidth(lineWidth);
textTotalWidth += line.naturalTextWidth();
}
descriptionLayout.endLayout();
// Add space for last visible line.
textTotalWidth += lineWidth;
m_descriptionText = m_descriptionFontMetrics.elidedText(description,
Qt::ElideRight,
textTotalWidth);
m_descriptionRect =
m_descriptionFontMetrics.boundingRect(0,
0,
option.rect.width() - 2 * m_itemPadding,
0,
descriptionFlags(),
m_descriptionText);
return m_descriptionRect.height();
}
示例6: paint_QTextLayout
void paint_QTextLayout(QPainter &p, bool useCache)
{
static bool first = true;
static QTextLayout *textLayout[lines];
if (first) {
for (int i = 0; i < lines; ++i) {
textLayout[i] = new QTextLayout(strings[i]);
int leading = p.fontMetrics().leading();
qreal height = 0;
qreal widthUsed = 0;
textLayout[i]->setCacheEnabled(useCache);
textLayout[i]->beginLayout();
while (1) {
QTextLine line = textLayout[i]->createLine();
if (!line.isValid())
break;
line.setLineWidth(lineWidth);
height += leading;
line.setPosition(QPointF(0, height));
height += line.height();
widthUsed = qMax(widthUsed, line.naturalTextWidth());
}
textLayout[i]->endLayout();
}
first = false;
}
for (int i = 0; i < count; ++i) {
for (int j = 0; j < lines; ++j) {
textLayout[j]->draw(&p, QPoint(0, j*spacing));
}
}
}
示例7: blockWidth
qreal TextDocumentLayout::blockWidth(const QTextBlock &block)
{
QTextLayout *layout = block.layout();
if (!layout->lineCount())
return 0; // only for layouted blocks
qreal blockWidth = 0;
for (int i = 0; i < layout->lineCount(); ++i) {
QTextLine line = layout->lineAt(i);
blockWidth = qMax(line.naturalTextWidth() + 8, blockWidth);
}
return blockWidth;
}
示例8: floatWidthForComplexText
float Font::floatWidthForComplexText(const TextRun& run, HashSet<const SimpleFontData*>*) const
{
if (!run.length())
return 0;
const QString string = fixSpacing(qstring(run));
QTextLayout layout(string, font());
QTextLine line = setupLayout(&layout, run);
int w = int(line.naturalTextWidth());
// WebKit expects us to ignore word spacing on the first character (as opposed to what Qt does)
if (treatAsSpace(run[0]))
w -= m_wordSpacing;
return w + run.padding();
}
示例9: setupLayout
static QTextLine setupLayout(QTextLayout* layout, const TextRun& style)
{
int flags = style.rtl() ? Qt::TextForceRightToLeft : Qt::TextForceLeftToRight;
if (style.padding())
flags |= Qt::TextJustificationForced;
layout->setFlags(flags);
layout->beginLayout();
QTextLine line = layout->createLine();
line.setLineWidth(INT_MAX/256);
if (style.padding())
line.setLineWidth(line.naturalTextWidth() + style.padding());
layout->endLayout();
return line;
}
示例10: viewItemTextLayout
// Origin: Qt
static void viewItemTextLayout ( QTextLayout &textLayout, int lineWidth, qreal &height, qreal &widthUsed )
{
height = 0;
widthUsed = 0;
textLayout.beginLayout();
while ( true )
{
QTextLine line = textLayout.createLine();
if ( !line.isValid() )
break;
line.setLineWidth ( lineWidth );
line.setPosition ( QPointF ( 0, height ) );
height += line.height();
widthUsed = qMax ( widthUsed, line.naturalTextWidth() );
}
textLayout.endLayout();
}
示例11: doTextLayout
// copied from QItemDelegate for drawDisplay
QSizeF doTextLayout(QTextLayout *textLayout, int lineWidth)
{
qreal height = 0;
qreal widthUsed = 0;
textLayout->beginLayout();
while (true) {
QTextLine line = textLayout->createLine();
if (!line.isValid())
break;
line.setLineWidth(lineWidth);
line.setPosition(QPointF(0, height));
height += line.height();
widthUsed = qMax(widthUsed, line.naturalTextWidth());
}
textLayout->endLayout();
return QSizeF(widthUsed, height);
}
示例12: paintEvent
void CodeEditor::paintEvent(QPaintEvent* event)
{
QPlainTextEdit::paintEvent(event);
QPainter painter(viewport());
painter.setPen(Qt::darkGray);
QTextBlock block = firstVisibleBlock();
QRectF rect;
do {
if (!block.isVisible())
continue;
rect = blockBoundingGeometry(block).translated(contentOffset());
QTextLine line = block.layout()->lineAt(0);
if (config->whitespaces) {
QString txt = block.text();
for (int i = 0; i < txt.length(); i++) {
// rect.x() <- учитывая горизонтальный скролинг
QPoint point(rect.x() + line.cursorToX(i), rect.y() + line.ascent());
if (txt[i] == ' ')
painter.drawText(point, QChar(0x00b7));
else if (txt[i] == '\t')
painter.drawText(point, QChar(0x21b9));
}
}
int state = block.userState();
if (!(state & Error) && state & Folded) {
QRect collapseRect(rect.x() + line.rect().x() + line.naturalTextWidth() + FONTWIDTH * 2,
rect.y() + 2, FONTWIDTH * 6, line.height() - 4);
painter.drawText(collapseRect, Qt::AlignCenter, state & Comment ? "...;" : "...)");
painter.drawRoundedRect(collapseRect, 4, 6);
}
} while ((block = block.next()).isValid() && rect.y() < viewport()->height());
}
示例13: layoutText
static int layoutText(QTextLayout *layout, int maxWidth) {
qreal height = 0;
int textWidth = 0;
layout->beginLayout();
while (true) {
QTextLine line = layout->createLine();
if (!line.isValid()) {
break;
}
line.setLineWidth(maxWidth);
line.setPosition(QPointF(0, height));
height += line.height();
textWidth = qMax(textWidth, qRound(line.naturalTextWidth() + 0.5));
}
layout->endLayout();
return textWidth;
}
示例14: doTextLayout
QSizeF QItemDelegatePrivate::doTextLayout(int lineWidth) const
{
qreal height = 0;
qreal widthUsed = 0;
textLayout.beginLayout();
while (true) {
QTextLine line = textLayout.createLine();
if (!line.isValid())
break;
line.setLineWidth(lineWidth);
line.setPosition(QPointF(0, height));
height += line.height();
widthUsed = qMax(widthUsed, line.naturalTextWidth());
}
textLayout.endLayout();
return QSizeF(widthUsed, height);
}
示例15: mouseMoveEvent
void CodeEditor::mouseMoveEvent(QMouseEvent* event)
{
QTextBlock block = findBlockByY(event->pos().y());
QRect collapseRect;
if (block.isValid()) {
QRectF rect = blockBoundingGeometry(block).translated(contentOffset());
QTextLine line = block.layout()->lineAt(0);
collapseRect = QRect(rect.x() + line.rect().x() + line.naturalTextWidth() + FONTWIDTH * 2,
rect.y() + 2, FONTWIDTH * 6, line.height() - 4);
}
int state = block.userState();
if (!(state & Error) && state & Folded && collapseRect.contains(event->pos())) {
pointedBlock = block;
viewport()->setCursor(Qt::PointingHandCursor);
QString str;
while ((block = block.next()).isValid() && !block.isVisible()) {
if (str.count() > 1)
str += "\n";
if (block.blockNumber() - pointedBlock.blockNumber() > 50) {
str += "..."; // "\n...";
break;
}
str += block.text();
}
QToolTip::showText(event->globalPos(), str, this);
} else {
pointedBlock = QTextBlock();
viewport()->setCursor(Qt::IBeamCursor);
}
QPlainTextEdit::mouseMoveEvent(event);
}