本文整理汇总了C++中QTextLayout类的典型用法代码示例。如果您正苦于以下问题:C++ QTextLayout类的具体用法?C++ QTextLayout怎么用?C++ QTextLayout使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了QTextLayout类的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: while
void QSyntaxHighlighterPrivate::applyFormatChanges()
{
QTextLayout *layout = currentBlock.layout();
QList<QTextLayout::FormatRange> ranges = layout->additionalFormats();
const int preeditAreaStart = layout->preeditAreaPosition();
const int preeditAreaLength = layout->preeditAreaText().length();
QList<QTextLayout::FormatRange>::Iterator it = ranges.begin();
while (it != ranges.end()) {
if (it->start >= preeditAreaStart
&& it->start + it->length <= preeditAreaStart + preeditAreaLength)
++it;
else
it = ranges.erase(it);
}
QTextCharFormat emptyFormat;
QTextLayout::FormatRange r;
r.start = r.length = -1;
int i = 0;
while (i < formatChanges.count()) {
while (i < formatChanges.count() && formatChanges.at(i) == emptyFormat)
++i;
if (i >= formatChanges.count())
break;
r.start = i;
r.format = formatChanges.at(i);
while (i < formatChanges.count() && formatChanges.at(i) == r.format)
++i;
if (i >= formatChanges.count())
break;
r.length = i - r.start;
if (r.start >= preeditAreaStart) {
r.start += preeditAreaLength;
} else if (r.start + r.length >= preeditAreaStart) {
r.length += preeditAreaLength;
}
ranges << r;
r.start = r.length = -1;
}
if (r.start != -1) {
r.length = formatChanges.count() - r.start;
if (r.start >= preeditAreaStart) {
r.start += preeditAreaLength;
} else if (r.start + r.length >= preeditAreaStart) {
r.length += preeditAreaLength;
}
ranges << r;
}
layout->setAdditionalFormats(ranges);
}
示例2: initForNewTest
void TestDocumentLayout::testNumberedList()
{
initForNewTest("Base\nListItem1\nListItem2\nListItem3\nListItem4\nListItem5\nListItem6\nListItem6\nListItem7\nListItem8\nListItem9\nListItem10\nListItem11\nListItem12\n");
KoParagraphStyle style;
m_styleManager->add(&style);
QTextBlock block = m_doc->begin();
style.applyStyle(block);
block = block.next();
KoListStyle listStyle;
KoListLevelProperties llp;
llp.setStyle(KoListStyle::DecimalItem);
listStyle.setLevelProperties(llp);
style.setListStyle(&listStyle);
QTextList *previous = 0;
int i;
for (i = 1; i <= 9; i++) {
QVERIFY(block.isValid());
// qDebug() << "->" << block.text();
style.applyStyle(block);
QTextList *textList = block.textList();
QVERIFY(textList);
if (previous == 0) {
previous = textList;
} else {
QCOMPARE(textList, previous);
}
QCOMPARE(textList->format().intProperty(QTextListFormat::ListStyle), (int)(KoListStyle::DecimalItem));
block = block.next();
}
m_layout->layout();
QTextLayout *blockLayout = m_block.layout();
QCOMPARE(blockLayout->lineAt(0).x(), 0.0);
QTextBlock blok = m_doc->begin().next();
qreal indent = blok.layout()->lineAt(0).x();
QVERIFY(indent > 0.0);
for (i = 1; i <= 9; ++i) {
// qDebug() << "=>" << blok.text();
QTextList *textList = blok.textList();
QVERIFY(textList);
QCOMPARE(blok.layout()->lineAt(0).x(), indent); // all the same indent.
blok = blok.next();
}
// now make number of listitems be more than 10, so we use 2 digits.
for (i = 9; i <= 12; ++i) {
QVERIFY(block.isValid());
style.applyStyle(block);
// qDebug() << "->" << block.text();
block = block.next();
}
m_layout->layout();
blockLayout = m_block.layout();
QCOMPARE(blockLayout->lineAt(0).x(), 0.0);
blok = m_doc->begin().next();
qreal indent2 = blok.layout()->lineAt(0).x();
QVERIFY(indent2 > indent); // since it takes an extra digit
for (i = 2; i <= 12; ++i) {
// qDebug() << "=>" << blok.text();
QCOMPARE(blok.layout()->lineAt(0).x(), indent2); // all the same indent.
blok = blok.next();
}
// now to make sure the text is actually properly set.
block = m_doc->begin().next();
i = 1;
while (block.isValid() && i < 13) {
KoTextBlockData *data = dynamic_cast<KoTextBlockData *>(block.userData());
QVERIFY(data);
QCOMPARE(data->counterText(), QString::number(i++));
block = block.next();
}
llp.setListItemSuffix(".");
llp.setStartValue(4);
listStyle.setLevelProperties(llp);
QTextCursor cursor(m_doc);
cursor.setPosition(10); // listItem1
QTextBlockFormat format = cursor.blockFormat();
format.setProperty(KoParagraphStyle::ListStartValue, 4);
cursor.setBlockFormat(format);
cursor.setPosition(40); // listItem4
format = cursor.blockFormat();
format.setProperty(KoParagraphStyle::ListStartValue, 12);
cursor.setBlockFormat(format);
// at this point we start numbering at 4. Have 4, 5, 6, 12, 13, 14, 15 etc
m_layout->layout();
// now to make sur the text is actually properly set.
block = m_doc->begin().next();
i = 4;
while (block.isValid() && i < 22) {
if (i == 7) {
//.........这里部分代码省略.........
示例3: qDebug
void HGMarkdownHighlighter::highlight()
{
if (cached_elements == NULL) {
qDebug() << "cached_elements is NULL";
return;
}
if (highlightingStyles == NULL)
this->setDefaultStyles();
this->clearFormatting();
for (int i = 0; i < highlightingStyles->size(); i++)
{
HighlightingStyle style = highlightingStyles->at(i);
pmh_element *elem_cursor = cached_elements[style.type];
while (elem_cursor != NULL)
{
if (elem_cursor->end <= elem_cursor->pos) {
elem_cursor = elem_cursor->next;
continue;
}
// "The QTextLayout object can only be modified from the
// documentChanged implementation of a QAbstractTextDocumentLayout
// subclass. Any changes applied from the outside cause undefined
// behavior." -- we are breaking this rule here. There might be
// a better (more correct) way to do this.
int startBlockNum = document->findBlock(elem_cursor->pos).blockNumber();
int endBlockNum = document->findBlock(elem_cursor->end).blockNumber();
for (int j = startBlockNum; j <= endBlockNum; j++)
{
QTextBlock block = document->findBlockByNumber(j);
QTextLayout *layout = block.layout();
QList<QTextLayout::FormatRange> list = layout->additionalFormats();
int blockpos = block.position();
QTextLayout::FormatRange r;
r.format = style.format;
if (j == startBlockNum) {
r.start = elem_cursor->pos - blockpos;
r.length = (startBlockNum == endBlockNum)
? elem_cursor->end - elem_cursor->pos
: block.length() - r.start;
} else if (j == endBlockNum) {
r.start = 0;
r.length = elem_cursor->end - blockpos;
} else {
r.start = 0;
r.length = block.length();
}
list.append(r);
layout->setAdditionalFormats(list);
}
elem_cursor = elem_cursor->next;
}
}
document->markContentsDirty(0, document->characterCount());
}
示例4: while
void DTPatientListDelegate::drawHightlightText(QPainter *painter,
QString text,
QString substring,
QRect pos,
Qt::CaseSensitivity cs,
bool boldFont,
QColor highlight) const
{
if (!painter || (text.length() == 0))
return;
painter->save();
QVector<QTextLayout::FormatRange> selections;
QTextLayout textLayout;
textLayout.setText(text);
if (substring.length() != 0)
{
int idx = -1;
int start = 0;
while (1)
{
idx = text.indexOf(substring, start,cs);
if (idx == -1)
break;
QTextLayout::FormatRange* range = new QTextLayout::FormatRange;
range->start = idx;
range->length = substring.length();
range->format.setBackground(QBrush(QColor(highlight)));
selections.append(*range);
start += substring.length();
}
}
if (boldFont)
{
QFont f = textLayout.font();
f.setBold(true);
textLayout.setFont(f);
}
int leading = painter->fontMetrics().leading();
qreal height = 0;
textLayout.beginLayout();
while (1) {
QTextLine line = textLayout.createLine();
if (!line.isValid())
break;
line.setLineWidth(pos.width());
height += leading;
line.setPosition(QPointF(0, height));
height += line.height();
}
textLayout.endLayout();
textLayout.draw(painter, pos.topLeft(), selections);
painter->restore();
}
示例5: document
void TextDocumentLayout::layoutBlock(const QTextBlock &block)
{
QTextDocument *doc = document();
qreal margin = doc->documentMargin();
qreal blockMaximumWidth = 0;
qreal height = 0;
QTextLayout *tl = block.layout();
QTextOption option = doc->defaultTextOption();
tl->setTextOption(option);
int extraMargin = 0;
if (option.flags() & QTextOption::AddSpaceForLineAndParagraphSeparators) {
QFontMetrics fm(block.charFormat().font());
extraMargin += fm.width(QChar(0x21B5));
}
tl->beginLayout();
qreal availableWidth = d->width;
if (availableWidth <= 0) {
availableWidth = qreal(INT_MAX); // similar to text edit with pageSize.width == 0
}
availableWidth -= 2*margin + extraMargin;
qreal indentMargin = 0;
while (1) {
QTextLine line = tl->createLine();
if (!line.isValid())
break;
line.setLeadingIncluded(true);
line.setLineWidth(availableWidth - indentMargin);
line.setPosition(QPointF(margin + indentMargin, height));
if(!height) //enter only in the first iteration
{
indentMargin = indentWidth(block);
}
height += line.height();
blockMaximumWidth = qMax(blockMaximumWidth, line.naturalTextWidth() + 2*margin);
}
tl->endLayout();
int previousLineCount = doc->lineCount();
const_cast<QTextBlock&>(block).setLineCount(block.isVisible() ? tl->lineCount() : 0);
int lineCount = doc->lineCount();
bool emitDocumentSizeChanged = previousLineCount != lineCount;
if (blockMaximumWidth > d->maximumWidth) {
// new longest line
d->maximumWidth = blockMaximumWidth;
d->maximumWidthBlockNumber = block.blockNumber();
emitDocumentSizeChanged = true;
} else if (block.blockNumber() == d->maximumWidthBlockNumber && blockMaximumWidth < d->maximumWidth) {
// longest line shrinking
QTextBlock b = doc->firstBlock();
d->maximumWidth = 0;
QTextBlock maximumBlock;
while (b.isValid()) {
qreal blockMaximumWidth = blockWidth(b);
if (blockMaximumWidth > d->maximumWidth) {
d->maximumWidth = blockMaximumWidth;
maximumBlock = b;
}
b = b.next();
}
if (maximumBlock.isValid()) {
d->maximumWidthBlockNumber = maximumBlock.blockNumber();
emitDocumentSizeChanged = true;
}
}
if (emitDocumentSizeChanged)// && !d->blockDocumentSizeChanged)
emit documentSizeChanged(documentSize());
emit updateBlock(block);
}
示例6: QPainterPath
QPainterPath TextEditorOverlay::createSelectionPath(const QTextCursor &begin, const QTextCursor &end,
const QRect &clip)
{
if (begin.isNull() || end.isNull() || begin.position() > end.position())
return QPainterPath();
QPointF offset = m_editor->contentOffset();
QRect viewportRect = rect();
QTextDocument *document = m_editor->document();
if (m_editor->blockBoundingGeometry(begin.block()).translated(offset).top() > clip.bottom() + 10
|| m_editor->blockBoundingGeometry(end.block()).translated(offset).bottom() < clip.top() - 10
)
return QPainterPath(); // nothing of the selection is visible
QTextBlock block = begin.block();
if (block.blockNumber() < m_editor->firstVisibleBlock().blockNumber() - 4)
block = m_editor->document()->findBlockByNumber(m_editor->firstVisibleBlock().blockNumber() - 4);
bool inSelection = false;
QVector<QRectF> selection;
if (begin.position() == end.position()) {
// special case empty selections
const QRectF blockGeometry = m_editor->blockBoundingGeometry(block);
QTextLayout *blockLayout = block.layout();
int pos = begin.position() - begin.block().position();
QTextLine line = blockLayout->lineForTextPosition(pos);
QRectF lineRect = line.naturalTextRect();
int x = line.cursorToX(pos);
lineRect.setLeft(x - m_borderWidth);
lineRect.setRight(x + m_borderWidth);
selection += lineRect.translated(blockGeometry.topLeft());
} else {
for (; block.isValid() && block.blockNumber() <= end.blockNumber(); block = block.next()) {
if (! block.isVisible())
continue;
const QRectF blockGeometry = m_editor->blockBoundingGeometry(block);
QTextLayout *blockLayout = block.layout();
QTextLine line = blockLayout->lineAt(0);
bool firstOrLastBlock = false;
int beginChar = 0;
if (!inSelection) {
if (block == begin.block()) {
beginChar = begin.positionInBlock();
line = blockLayout->lineForTextPosition(beginChar);
firstOrLastBlock = true;
}
inSelection = true;
} else {
// while (beginChar < block.length() && document->characterAt(block.position() + beginChar).isSpace())
// ++beginChar;
// if (beginChar == block.length())
// beginChar = 0;
}
int lastLine = blockLayout->lineCount()-1;
int endChar = -1;
if (block == end.block()) {
endChar = end.positionInBlock();
lastLine = blockLayout->lineForTextPosition(endChar).lineNumber();
inSelection = false;
firstOrLastBlock = true;
} else {
endChar = block.length();
while (endChar > beginChar && document->characterAt(block.position() + endChar - 1).isSpace())
--endChar;
}
QRectF lineRect = line.naturalTextRect();
if (beginChar < endChar) {
lineRect.setLeft(line.cursorToX(beginChar));
if (line.lineNumber() == lastLine)
lineRect.setRight(line.cursorToX(endChar));
selection += lineRect.translated(blockGeometry.topLeft());
for (int lineIndex = line.lineNumber()+1; lineIndex <= lastLine; ++lineIndex) {
line = blockLayout->lineAt(lineIndex);
lineRect = line.naturalTextRect();
if (lineIndex == lastLine)
lineRect.setRight(line.cursorToX(endChar));
selection += lineRect.translated(blockGeometry.topLeft());
}
} else { // empty lines
const int emptyLineSelectionSize = 16;
if (!firstOrLastBlock && !selection.isEmpty()) { // middle
lineRect.setLeft(selection.last().left());
} else if (inSelection) { // first line
lineRect.setLeft(line.cursorToX(beginChar));
} else { // last line
if (endChar == 0)
break;
lineRect.setLeft(line.cursorToX(endChar) - emptyLineSelectionSize);
}
//.........这里部分代码省略.........
示例7: drawDisplay
// copied from QItemDelegate to be able to add the 'format' parameter
void HighlightingItemDelegate::drawDisplay(QPainter *painter,
const QStyleOptionViewItem &option,
const QRect &rect, const QString &text,
const QVector<QTextLayout::FormatRange> &format) const
{
QPalette::ColorGroup cg = option.state & QStyle::State_Enabled
? QPalette::Normal : QPalette::Disabled;
if (cg == QPalette::Normal && !(option.state & QStyle::State_Active))
cg = QPalette::Inactive;
if (option.state & QStyle::State_Selected) {
painter->fillRect(rect, option.palette.brush(cg, QPalette::Highlight));
painter->setPen(option.palette.color(cg, QPalette::HighlightedText));
} else {
painter->setPen(option.palette.color(cg, QPalette::Text));
}
if (text.isEmpty())
return;
if (option.state & QStyle::State_Editing) {
painter->save();
painter->setPen(option.palette.color(cg, QPalette::Text));
painter->drawRect(rect.adjusted(0, 0, -1, -1));
painter->restore();
}
const QStyleOptionViewItem opt = option;
const QWidget *widget = option.widget;
QStyle *style = widget ? widget->style() : QApplication::style();
const int textMargin = style->pixelMetric(QStyle::PM_FocusFrameHMargin, nullptr, widget) + 1;
QRect textRect = rect.adjusted(textMargin, 0, -textMargin, 0); // remove width padding
const bool wrapText = opt.features & QStyleOptionViewItem::WrapText;
QTextOption textOption;
textOption.setWrapMode(wrapText ? QTextOption::WordWrap : QTextOption::ManualWrap);
textOption.setTextDirection(option.direction);
textOption.setAlignment(QStyle::visualAlignment(option.direction, option.displayAlignment));
QTextLayout textLayout;
textLayout.setTextOption(textOption);
textLayout.setFont(option.font);
textLayout.setText(replaceNewLine(text));
QSizeF textLayoutSize = doTextLayout(&textLayout, textRect.width());
if (textRect.width() < textLayoutSize.width()
|| textRect.height() < textLayoutSize.height()) {
QString elided;
int start = 0;
int end = text.indexOf(QChar::LineSeparator, start);
if (end == -1) {
elided += option.fontMetrics.elidedText(text, option.textElideMode, textRect.width());
} else {
while (end != -1) {
elided += option.fontMetrics.elidedText(text.mid(start, end - start),
option.textElideMode, textRect.width());
elided += QChar::LineSeparator;
start = end + 1;
end = text.indexOf(QChar::LineSeparator, start);
}
// let's add the last line (after the last QChar::LineSeparator)
elided += option.fontMetrics.elidedText(text.mid(start),
option.textElideMode, textRect.width());
}
textLayout.setText(elided);
textLayoutSize = doTextLayout(&textLayout, textRect.width());
}
const QSize layoutSize(textRect.width(), int(textLayoutSize.height()));
const QRect layoutRect = QStyle::alignedRect(option.direction, option.displayAlignment,
layoutSize, textRect);
// if we still overflow even after eliding the text, enable clipping
if (!hasClipping() && (textRect.width() < textLayoutSize.width()
|| textRect.height() < textLayoutSize.height())) {
painter->save();
painter->setClipRect(layoutRect);
textLayout.draw(painter, layoutRect.topLeft(), format, layoutRect);
painter->restore();
} else {
textLayout.draw(painter, layoutRect.topLeft(), format, layoutRect);
}
}
示例8: initStyleOption
//.........这里部分代码省略.........
textHighlightRect.adjust ( 0,iconSize + 5,0,0 );
// draw background
{
QSize textSize = viewItemTextSize ( &opt );
QPalette::ColorGroup cg;
QStyleOptionViewItemV4 opt2(opt);
if((opt.widget && opt.widget->isEnabled()) || (opt.state & QStyle::State_Enabled))
{
if(! ( opt.state & QStyle::State_Active ))
cg = QPalette::Inactive;
else
cg = QPalette::Normal;
}
else
{
cg = QPalette::Disabled;
}
opt2.palette.setCurrentColorGroup(cg);
// fill in background, if any
if ( opt.backgroundBrush.style() != Qt::NoBrush )
{
QPointF oldBO = painter->brushOrigin();
painter->setBrushOrigin ( opt.rect.topLeft() );
painter->fillRect ( opt.rect, opt.backgroundBrush );
painter->setBrushOrigin ( oldBO );
}
if ( opt.showDecorationSelected )
{
drawSelectionRect(painter,opt2, opt.rect);
drawFocusRect(painter,opt2, opt.rect);
//painter->fillRect ( opt.rect, opt.palette.brush ( cg, QPalette::Highlight ) );
}
else
{
//if ( opt.state & QStyle::State_Selected )
{
//QRect textRect = subElementRect ( QStyle::SE_ItemViewItemText, opt, opt.widget );
//painter->fillRect ( textHighlightRect, opt.palette.brush ( cg, QPalette::Highlight ) );
drawSelectionRect(painter,opt2, textHighlightRect);
drawFocusRect(painter,opt2, textHighlightRect);
}
}
}
// draw the icon
{
QIcon::Mode mode = QIcon::Normal;
if ( ! ( opt.state & QStyle::State_Enabled ) )
mode = QIcon::Disabled;
else if ( opt.state & QStyle::State_Selected )
mode = QIcon::Selected;
QIcon::State state = opt.state & QStyle::State_Open ? QIcon::On : QIcon::Off;
iconbox.setHeight ( iconSize );
opt.icon.paint ( painter, iconbox, Qt::AlignCenter, mode, state );
}
// set the text colors
QPalette::ColorGroup cg = opt.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled;
if ( cg == QPalette::Normal && ! ( opt.state & QStyle::State_Active ) )
cg = QPalette::Inactive;
if ( opt.state & QStyle::State_Selected )
{
painter->setPen ( opt.palette.color ( cg, QPalette::HighlightedText ) );
}
else
{
painter->setPen ( opt.palette.color ( cg, QPalette::Text ) );
}
// draw the text
QTextOption textOption;
textOption.setWrapMode ( QTextOption::WrapAtWordBoundaryOrAnywhere );
textOption.setTextDirection ( opt.direction );
textOption.setAlignment ( QStyle::visualAlignment ( opt.direction, opt.displayAlignment ) );
QTextLayout textLayout;
textLayout.setTextOption ( textOption );
textLayout.setFont ( opt.font );
textLayout.setText ( opt.text );
qreal width, height;
viewItemTextLayout ( textLayout, iconbox.width(), height, width );
const int lineCount = textLayout.lineCount();
const QRect layoutRect = QStyle::alignedRect ( opt.direction, opt.displayAlignment, QSize ( iconbox.width(), int ( height ) ), textRect );
const QPointF position = layoutRect.topLeft();
for ( int i = 0; i < lineCount; ++i )
{
const QTextLine line = textLayout.lineAt ( i );
line.draw ( painter, position );
}
painter->restore();
}
示例9: pageSize
void Text::layout()
{
#if 0
QSizeF pageSize(-1.0, 1000000);
setPos(0.0, 0.0);
if (parent() && _layoutToParentWidth) {
pageSize.setWidth(parent()->width());
if (parent()->type() == HBOX || parent()->type() == VBOX || parent()->type() == TBOX) {
Box* box = static_cast<Box*>(parent());
rxpos() += box->leftMargin() * DPMM;
rypos() += box->topMargin() * DPMM;
// pageSize.setHeight(box->height() - (box->topMargin() + box->bottomMargin()) * DPMM);
pageSize.setWidth(box->width() - (box->leftMargin() + box->rightMargin()) * DPMM);
}
}
QTextOption to = _doc->defaultTextOption();
to.setUseDesignMetrics(true);
to.setWrapMode(pageSize.width() <= 0.0 ? QTextOption::NoWrap : QTextOption::WrapAtWordBoundaryOrAnywhere);
_doc->setDefaultTextOption(to);
if (pageSize.width() <= 0.0)
_doc->setTextWidth(_doc->idealWidth());
else
_doc->setPageSize(pageSize);
if (hasFrame()) {
frame = QRectF();
for (QTextBlock tb = _doc->begin(); tb.isValid(); tb = tb.next()) {
QTextLayout* tl = tb.layout();
int n = tl->lineCount();
for (int i = 0; i < n; ++i)
// frame |= tl->lineAt(0).naturalTextRect().translated(tl->position());
frame |= tl->lineAt(0).rect().translated(tl->position());
}
if (circle()) {
if (frame.width() > frame.height()) {
frame.setY(frame.y() + (frame.width() - frame.height()) * -.5);
frame.setHeight(frame.width());
}
else {
frame.setX(frame.x() + (frame.height() - frame.width()) * -.5);
frame.setWidth(frame.height());
}
}
qreal w = (paddingWidth() + frameWidth() * .5) * DPMM;
frame.adjust(-w, -w, w, w);
w = frameWidth() * DPMM;
_bbox = frame.adjusted(-w, -w, w, w);
}
else {
_bbox = QRectF(QPointF(0.0, 0.0), _doc->size()); //_doc->documentLayout()->frameBoundingRect(_doc->rootFrame());
}
_doc->setModified(false);
style().layout(this); // process alignment
if ((style().align() & ALIGN_VCENTER) && (subtype() == TEXT_TEXTLINE)) {
// special case: vertically centered text with TextLine needs to
// take into account the line width
TextLineSegment* tls = (TextLineSegment*)parent();
TextLine* tl = (TextLine*)(tls->line());
qreal textlineLineWidth = point(tl->lineWidth());
rypos() -= textlineLineWidth * .5;
}
if (parent() == 0)
return;
if (parent()->type() == SEGMENT) {
Segment* s = static_cast<Segment*>(parent());
rypos() += s ? s->measure()->system()->staff(staffIdx())->y() : 0.0;
}
#endif
Font f = style().font(spatium());
qreal asc, desc, leading;
qreal w = textMetrics(f.family(), _text, f.size(), &asc, &desc, &leading);
// printf("text(%s) asc %f desc %f leading %f w %f\n", qPrintable(_text), asc, desc, leading, w);
_lineHeight = asc + desc;
_lineSpacing = _lineHeight + leading;
_baseLine = asc;
setbbox(QRectF(0.0, -asc, w, _lineHeight));
#if 0
if (parent() && _layoutToParentWidth) {
qreal wi = parent()->width();
qreal ph = parent()->height();
qreal x;
qreal y = pos.y();
if (align() & ALIGN_HCENTER)
x = (wi - w) * .5;
else if (align() & ALIGN_RIGHT)
x = wi - w;
else
x = 0.0;
if (align() & ALIGN_VCENTER)
y = (ph - asc) * .5;
else if (align() & ALIGN_BOTTOM)
y = ph - asc;
else
y = asc;
//.........这里部分代码省略.........
示例10: painter
void BaseEditor::paintEvent(QPaintEvent *e)
{
//copy from QPlainTextEditor
QPainter painter(viewport());
Q_ASSERT(qobject_cast<QPlainTextDocumentLayout*>(document()->documentLayout()));
QPointF offset(contentOffset());
QRect er = e->rect();
QRect viewportRect = viewport()->rect();
bool editable = !isReadOnly();
QTextBlock block = firstVisibleBlock();
qreal maximumWidth = document()->documentLayout()->documentSize().width();
//margin
qreal lineX = 0;
if (conf->isDisplayRightColumnMargin()) {
// Don't use QFontMetricsF::averageCharWidth here, due to it returning
// a fractional size even when this is not supported by the platform.
lineX = QFontMetricsF(document()->defaultFont()).width(QLatin1Char('X')) * conf->getRightMarginColumn() + offset.x() + 4;
if (lineX < viewportRect.width()) {
const QBrush background = QBrush(QColor(239, 239, 239));
painter.fillRect(QRectF(lineX, er.top(), viewportRect.width() - lineX, er.height()),
background);
const QColor col = (palette().base().color().value() > 128) ? Qt::black : Qt::white;
const QPen pen = painter.pen();
painter.setPen(blendColors(background.isOpaque() ? background.color() : palette().base().color(),
col, 32));
painter.drawLine(QPointF(lineX, er.top()), QPointF(lineX, er.bottom()));
painter.setPen(pen);
}
}
// Set a brush origin so that the WaveUnderline knows where the wave started
painter.setBrushOrigin(offset);
// keep right margin clean from full-width selection
int maxX = offset.x() + qMax((qreal)viewportRect.width(), maximumWidth)
- document()->documentMargin();
er.setRight(qMin(er.right(), maxX));
painter.setClipRect(er);
QAbstractTextDocumentLayout::PaintContext context = getPaintContext();
while (block.isValid()) {
QRectF r = blockBoundingRect(block).translated(offset);
QTextLayout *layout = block.layout();
if (!block.isVisible()) {
offset.ry() += r.height();
block = block.next();
continue;
}
if (r.bottom() >= er.top() && r.top() <= er.bottom()) {
QTextBlockFormat blockFormat = block.blockFormat();
QBrush bg = blockFormat.background();
if (bg != Qt::NoBrush) {
QRectF contentsRect = r;
contentsRect.setWidth(qMax(r.width(), maximumWidth));
fillBackground(&painter, contentsRect, bg);
}
QVector<QTextLayout::FormatRange> selections;
int blpos = block.position();
int bllen = block.length();
for (int i = 0; i < context.selections.size(); ++i) {
const QAbstractTextDocumentLayout::Selection &range = context.selections.at(i);
const int selStart = range.cursor.selectionStart() - blpos;
const int selEnd = range.cursor.selectionEnd() - blpos;
if (selStart < bllen && selEnd > 0
&& selEnd > selStart) {
QTextLayout::FormatRange o;
o.start = selStart;
o.length = selEnd - selStart;
o.format = range.format;
selections.append(o);
} else if (!range.cursor.hasSelection() && range.format.hasProperty(QTextFormat::FullWidthSelection)
&& block.contains(range.cursor.position())) {
// for full width selections we don't require an actual selection, just
// a position to specify the line. that's more convenience in usage.
QTextLayout::FormatRange o;
QTextLine l = layout->lineForTextPosition(range.cursor.position() - blpos);
o.start = l.textStart();
o.length = l.textLength();
if (o.start + o.length == bllen - 1)
++o.length; // include newline
o.format = range.format;
selections.append(o);
}
}
//.........这里部分代码省略.........
示例11: firstVisibleBlock
void ScCodeEditor::blinkCode( const QTextCursor & c )
{
if( !c.document() || !c.hasSelection() ) return;
Settings::Manager *settings = Main::settings();
QTextCharFormat evalCodeTextFormat = settings->getThemeVal("evaluatedCode");
QTextDocument *doc = c.document();
int startPos = c.selectionStart();
int endPos = c.selectionEnd();
QTextBlock startBlock = doc->findBlock(startPos);
QTextBlock endBlock = doc->findBlock(endPos);
startPos -= startBlock.position();
endPos -= endBlock.position();
// Get the bounds of visible blocks within the cursor's selection:
QTextBlock block = firstVisibleBlock();
int idx = block.blockNumber();
int sidx = startBlock.blockNumber();
QTextBlock firstBlock, lastBlock;
firstBlock = lastBlock = block;
QRectF geom = blockBoundingGeometry(block).translated(contentOffset());
qreal top = geom.top();
qreal bottom = top;
qreal width=0;
while(block.isValid() && bottom < viewport()->rect().height())
{
if(block.isVisible())
{
QTextLayout *l = block.layout();
QRectF r = l->boundingRect();
bottom += r.height();
if(idx < sidx) {
// Block not within the selection. Will skip it.
top = bottom;
}
else {
// Block within the selection.
width = qMax(width, l->maximumWidth() + r.left());
}
}
if(block == endBlock) break;
block = block.next();
++idx;
if(top == bottom)
firstBlock = block;
}
lastBlock = block;
if(bottom == top) {
//qDebug("no visible block.");
return;
}
// Construct a pixmap to render the code on:
QPixmap pix( QSize(qCeil(width), qCeil(bottom - top)) );
pix.fill(QColor(0,0,0,0));
// Render the visible blocks:
QPainter painter(&pix);
QVector<QTextLayout::FormatRange> selections;
block = firstBlock;
int y=0;
while( block.isValid() )
{
if (block.isVisible())
{
QRectF blockRect = block.layout()->boundingRect();
// Use extra char formatting to hide code outside of selection
// and modify the appearance of selected code:
QTextLayout::FormatRange range;
selections.clear();
int start = 0;
if(block == startBlock) {
range.start = 0;
range.length = startPos;
range.format.setForeground(QColor(0,0,0,0));
range.format.setBackground(Qt::NoBrush);
selections.append(range);
start = startPos;
}
range.start = start;
range.length = (block == endBlock ? endPos : block.length() - 1) - range.start;
range.format = evalCodeTextFormat;
selections.append(range);
//.........这里部分代码省略.........
示例12: document
void MarkdownHighlighter::resultReady(pmh_element **elements)
{
QTextBlock block = document()->firstBlock();
while (block.isValid()) {
block.layout()->clearAdditionalFormats();
block = block.next();
}
if (!elements) {
qDebug() << "elements is null";
return;
}
// QTextDocument::characterCount returns a value one higher than the
// actual character count.
// See: https://bugreports.qt.nokia.com//browse/QTBUG-4841
// document->toPlainText().length() would give us the correct value
// but it's probably too slow.
unsigned long max_offset = document()->characterCount() - 1;
for (int i = 0; i < highlightingStyles.size(); i++)
{
HighlightingStyle style = highlightingStyles.at(i);
pmh_element *elem_cursor = elements[style.type];
while (elem_cursor != NULL)
{
unsigned long pos = elem_cursor->pos;
unsigned long end = elem_cursor->end;
if (end <= pos || max_offset < pos)
{
elem_cursor = elem_cursor->next;
continue;
}
if (max_offset < end)
end = max_offset;
// "The QTextLayout object can only be modified from the
// documentChanged implementation of a QAbstractTextDocumentLayout
// subclass. Any changes applied from the outside cause undefined
// behavior." -- we are breaking this rule here. There might be
// a better (more correct) way to do this.
int startBlockNum = document()->findBlock(pos).blockNumber();
int endBlockNum = document()->findBlock(end).blockNumber();
for (int j = startBlockNum; j <= endBlockNum; j++)
{
QTextBlock block = document()->findBlockByNumber(j);
QTextLayout *layout = block.layout();
QList<QTextLayout::FormatRange> list = layout->additionalFormats();
int blockpos = block.position();
QTextLayout::FormatRange r;
r.format = style.format;
if (/*_makeLinksClickable
&&*/ (elem_cursor->type == pmh_LINK
|| elem_cursor->type == pmh_AUTO_LINK_URL
|| elem_cursor->type == pmh_AUTO_LINK_EMAIL
|| elem_cursor->type == pmh_REFERENCE)
&& elem_cursor->address != NULL)
{
QString address(elem_cursor->address);
if (elem_cursor->type == pmh_AUTO_LINK_EMAIL && !address.startsWith("mailto:"))
address = "mailto:" + address;
QTextCharFormat linkFormat(r.format);
linkFormat.setAnchor(true);
linkFormat.setAnchorHref(address);
linkFormat.setToolTip(address);
r.format = linkFormat;
}
if (j == startBlockNum) {
r.start = pos - blockpos;
r.length = (startBlockNum == endBlockNum)
? end - pos
: block.length() - r.start;
} else if (j == endBlockNum) {
r.start = 0;
r.length = end - blockpos;
} else {
r.start = 0;
r.length = block.length();
}
list.append(r);
layout->setAdditionalFormats(list);
}
elem_cursor = elem_cursor->next;
}
}
document()->markContentsDirty(0, document()->characterCount());
pmh_free_elements(elements);
}
示例13: previous
int CharBreakIteratorQt::previous() {
if (currentPos == 0)
return -1;
currentPos = layout.previousCursorPosition(currentPos);
return currentPos;
}
示例14: next
int CharBreakIteratorQt::next() {
if (currentPos == length)
return -1;
currentPos = layout.nextCursorPosition(currentPos);
return currentPos;
}