void TestSpellCheck::testFetchMoreText() { MySpellCheck checker; QTextDocument doc; QString text("some simple text\na second parag with more text\n"); doc.setPlainText(text); checker.startRun(&doc, 0, text.size()); QTextBlock block = doc.begin(); QCOMPARE(checker.publicFetchMoreText(), block.text()); block = block.next(); QVERIFY(block.isValid()); QCOMPARE(checker.publicFetchMoreText(), block.text()); QTextCursor cursor(&doc); QTextCharFormat cf; cf.setProperty(KoCharacterStyle::Language, QVariant("pl")); cursor.setPosition(4, QTextCursor::KeepAnchor); cursor.mergeCharFormat(cf); checker.startRun(&doc, 0, text.size()); block = doc.begin(); QCOMPARE(checker.publicFetchMoreText(), QString("some")); QCOMPARE(checker.publicFetchMoreText(), block.text().mid(4)); block = block.next(); QVERIFY(block.isValid()); QCOMPARE(checker.publicFetchMoreText(), block.text()); // add some more cursor.setPosition(block.position() + 2); cursor.movePosition(QTextCursor::NextWord, QTextCursor::KeepAnchor); // 'second' int position2 = cursor.anchor(); int position3 = cursor.position(); cf.setProperty(KoCharacterStyle::Language, QVariant("br")); cursor.mergeCharFormat(cf); cursor.movePosition(QTextCursor::NextWord, QTextCursor::MoveAnchor, 2); cursor.movePosition(QTextCursor::NextWord, QTextCursor::KeepAnchor); // 'with' cf.setProperty(KoCharacterStyle::Language, QVariant("en")); cursor.mergeCharFormat(cf); checker.startRun(&doc, 0, text.size()); checker.setDefaultLanguage("en_NZ"); block = doc.begin(); QCOMPARE(checker.publicFetchMoreText(), QString("some")); QCOMPARE(checker.publicFetchMoreText(), block.text().mid(4)); block = block.next(); QVERIFY(block.isValid()); QCOMPARE(checker.publicFetchMoreText(), block.text().left(2)); QCOMPARE(checker.publicFetchMoreText(), text.mid(position2, position3 - position2)); QCOMPARE(checker.publicFetchMoreText(), text.mid(position3).trimmed()); // start a check in the middle of a block checker.startRun(&doc, 4, 19); block = doc.begin(); QCOMPARE(checker.publicFetchMoreText(), block.text().mid(4)); block = block.next(); QVERIFY(block.isValid()); QCOMPARE(checker.publicFetchMoreText(), block.text().left(2)); }
QTextBlock QTextDocumentProto::begin() const { QTextDocument *item = qscriptvalue_cast<QTextDocument*>(thisObject()); if (item) return item->begin(); return QTextBlock(); }
void tst_QTextFormat::testTabsUsed() { QTextDocument doc; QTextCursor cursor(&doc); QList<QTextOption::Tab> tabs; QTextBlockFormat format; QTextOption::Tab tab; tab.position = 100; tabs.append(tab); format.setTabPositions(tabs); cursor.mergeBlockFormat(format); cursor.insertText("foo\tbar"); //doc.setPageSize(QSizeF(200, 200)); doc.documentLayout()->pageCount(); // force layout; QTextBlock block = doc.begin(); QTextLayout *layout = block.layout(); QVERIFY(layout); QCOMPARE(layout->lineCount(), 1); QTextLine line = layout->lineAt(0); QCOMPARE(line.cursorToX(4), 100.); QTextOption option = layout->textOption(); QCOMPARE(option.tabs().count(), tabs.count()); }
int GenericCodeEditor::findAll( const QRegExp &expr, QTextDocument::FindFlags options ) { mSearchSelections.clear(); if(expr.isEmpty()) { this->updateExtraSelections(); return 0; } QTextEdit::ExtraSelection selection; selection.format = mSearchResultTextFormat; QTextDocument *doc = QPlainTextEdit::document(); QTextBlock block = doc->begin(); QTextCursor cursor; while (block.isValid()) { int blockPos = block.position(); int offset = 0; while(findInBlock(doc, block, expr, offset, options, cursor)) { offset = cursor.selectionEnd() - blockPos; if (cursor.hasSelection()) { selection.cursor = cursor; mSearchSelections.append(selection); } else offset += 1; } block = block.next(); } this->updateExtraSelections(); return mSearchSelections.count(); }
void PluginListDelegate::drawDisplay(QPainter* painter, const QStyleOptionViewItem &option, const QRect &rect, const QString &text) const { QTextDocument textDocument; textDocument.setHtml(text); QTextLayout textLayout(textDocument.begin()); textLayout.setFont(option.font); const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin, 0) + 1; QRect textRect = rect.adjusted(textMargin, 0, -textMargin, 0); // remove width padding textLayout.beginLayout(); qreal height = 0; while (1) { QTextLine line = textLayout.createLine(); if (!line.isValid()) { break; } line.setLineWidth(textRect.width()); height += 3; line.setPosition(QPoint(0, height)); height += line.height(); } textLayout.endLayout(); textLayout.draw(painter, QPointF(textRect.left(), textRect.top())); }
void SchematicName::focusInEvent(QFocusEvent *fe) { QGraphicsTextItem::focusInEvent(fe); qApp->installEventFilter(this); QTextDocument *doc = document(); QTextCursor cursor(doc->begin()); cursor.select(QTextCursor::Document); setTextCursor(cursor); }
// Set the alignment of all blocks to the given alignment void setAlignment(Qt::Alignment align) { QTextDocument* textDocument = document(); for (QTextBlock it = textDocument->begin(); it != textDocument->end(); it = it.next()) { QTextCursor cur(it); QTextBlockFormat format = cur.blockFormat(); format.setAlignment(align); cur.setBlockFormat(format); } }
QString Dialog::toStringFromDocument() { QTextDocument *doc = message()->document(); QString txt; for (QTextBlock bl = doc->begin(); bl != doc->end(); bl = bl.next()) if (bl.isValid()) { for (QTextBlock::iterator it = bl.begin(); !it.atEnd(); ++it) { QTextFragment fragm = it.fragment(); if (fragm.isValid() && fragm.charFormat().isImageFormat()) { QString imgName = fragm.charFormat().toImageFormat().name(); txt += imgName; } else if (fragm.isValid()) txt += fragm.text(); } if (bl != doc->begin()) txt += "\n"; } int i = (int)txt.size() - 1; while (i >= 0 && (txt[i] == ' ' || txt[i] == '\n')) --i; txt.remove(i + 1, txt.size() - i - 1); return txt; }
void TextItem::setInternalAlignment(Qt::Alignment align) { // Store alignment alignment = align; // Set the alignment of all blocks to the given alignment QTextDocument* textDocument = textItem->document(); for (QTextBlock it = textDocument->begin(); it != textDocument->end(); it = it.next()) { QTextCursor cur(it); QTextBlockFormat format = cur.blockFormat(); format.setAlignment(align); cur.setBlockFormat(format); } }
int main(int argv, char **args) { QString contentString("One\nTwp\nThree"); QTextDocument *doc = new QTextDocument(contentString); //! [0] for (QTextBlock it = doc->begin(); it != doc->end(); it = it.next()) cout << it.text().toStdString() << endl; //! [0] return 0; }
void LineNumberWidget::paintEvent( QPaintEvent* /*e*/ ) { int max=0; int l=0; QPainter painter( this ); painter.setFont(numfont); const QFontMetrics fm(numfont); int yOffset = m_editor->verticalScrollBar()->value(); QTextDocument *doc = m_editor->document(); int i = 1; QTextBlock p = doc->begin(); QString numtext; const QBrush oldbrush=painter.brush(); QPen oldpen(QColor("#136872")); oldpen.setStyle(Qt::SolidLine); painter.setPen(oldpen); painter.drawLine(width()-2,0,width()-2,height()); while ( p.isValid() ) { QPointF point = p.layout()->position(); if ( point.y() + 20 - yOffset < 0 ) { i++; p = p.next(); continue; } if ( (int)(point.y()) - yOffset > height() ) break; for (int j = 0; j < 3; ++j) { if (m_editor->UserBookmark[j]==i) { const QBrush brush(QColor("#1B8EA6")); painter.fillRect(2, (int)(point.y()) - yOffset,fm.width("0")+6,fm.lineSpacing(), brush); const QPen pen(QColor("#FFFFFF")); painter.setPen(pen); painter.drawText(4, (int)(point.y()) - yOffset,width()-4,fm.lineSpacing(),Qt::AlignLeft | Qt::AlignTop,QString::number(j+1)); } } painter.setPen(oldpen); numtext=QString::number(i); painter.drawText(0, (int)(point.y()) - yOffset,width()-4,fm.lineSpacing(),Qt::AlignRight | Qt::AlignTop,numtext); l= fm.width(numtext)+18+fm.width("0"); if (l>max) max=l; i++; p = p.next(); } if (i>=10000) setFixedWidth(max); painter.end(); }
void KPrPlaceholderTextStrategy::saveOdf( KoShapeSavingContext & context ) { if (m_textShape) { KoTextShapeData *shapeData = qobject_cast<KoTextShapeData*>(m_textShape->userData()); if (shapeData) { KoStyleManager *styleManager = KoTextDocument(shapeData->document()).styleManager(); if (styleManager) { QTextDocument *document = shapeData->document(); QTextBlock block = document->begin(); QString styleName = KoTextWriter::saveParagraphStyle(block, styleManager, context); context.xmlWriter().addAttribute("draw:text-style-name", styleName); } } } KPrPlaceholderStrategy::saveOdf( context ); }
void TextOutput::updateExistsTextColor() { if (!m_existsTimer.hasExpired(1000)) return; QTextDocument* doc = document(); for (QTextBlock it = doc->begin(); it != doc->end(); it = it.next()) { QTextCursor cur(it); cur.select(QTextCursor::BlockUnderCursor); QTextCharFormat f(cur.charFormat()); QColor color(f.foreground().color()); color.setAlpha(128); f.setForeground(color); cur.setCharFormat(f); } }
bool CVProjectDialog::getInput(Core::CVProject& proj) { proj.name = _name->text(); proj.path = _path->text() + QDir::separator() + proj.name; proj.type = _type->currentIndex() == 0 ? Core::CVProject::PHOTOGRAMMETRY : Core::CVProject::LIDAR; proj.refPath = _conf->text(); if (proj.type == Core::CVProject::PHOTOGRAMMETRY) { proj.scale = _scale->currentText(); } proj.datum = _epsg->currentText().toInt(); QTextDocument* doc = _note->document(); QStringList l; for (QTextBlock it = doc->begin(); it != doc->end(); it = it.next()) { l << it.text(); } proj.notes = l.join("\n"); return !proj.name.isEmpty() && !proj.path.isEmpty() && !proj.refPath.isEmpty(); }
QString LiteEditorWidget::cursorToHtml(QTextCursor cursor) const { QTextDocument *tempDocument = new QTextDocument; QTextCursor tempCursor(tempDocument); tempCursor.insertFragment(cursor.selection()); // Apply the additional formats set by the syntax highlighter QTextBlock start = document()->findBlock(cursor.selectionStart()); QTextBlock end = document()->findBlock(cursor.selectionEnd()); end = end.next(); const int selectionStart = cursor.selectionStart(); const int endOfDocument = tempDocument->characterCount() - 1; for (QTextBlock current = start; current.isValid() && current != end; current = current.next()) { const QTextLayout *layout = current.layout(); foreach (const QTextLayout::FormatRange &range, layout->additionalFormats()) { const int start = current.position() + range.start - selectionStart; const int end = start + range.length; if (end <= 0 || start >= endOfDocument) continue; tempCursor.setPosition(qMax(start, 0)); tempCursor.setPosition(qMin(end, endOfDocument), QTextCursor::KeepAnchor); tempCursor.setCharFormat(range.format); } } // Reset the user states since they are not interesting for (QTextBlock block = tempDocument->begin(); block.isValid(); block = block.next()) block.setUserState(-1); // Make sure the text appears pre-formatted tempCursor.setPosition(0); tempCursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor); QTextBlockFormat blockFormat = tempCursor.blockFormat(); blockFormat.setNonBreakableLines(true); tempCursor.setBlockFormat(blockFormat); QString html = tempCursor.selection().toHtml();//("utf-8"); html.replace("\t","    "); delete tempDocument; return html; }
void PluginListDelegate::drawDisplay(QPainter* painter, const QStyleOptionViewItem &option, const QRect &rect, const QString &text) 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)); } QTextDocument textDocument; textDocument.setHtml(text); QTextLayout textLayout(textDocument.begin()); textLayout.setFont(option.font); const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin, 0) + 1; QRect textRect = rect.adjusted(textMargin, 0, -textMargin, 0); // remove width padding textLayout.beginLayout(); qreal height = 0; QTextLine line = textLayout.createLine(); while (line.isValid()) { line.setLineWidth(textRect.width()); height += 3; line.setPosition(QPoint(0, height)); height += line.height(); line = textLayout.createLine(); } textLayout.endLayout(); textLayout.draw(painter, QPointF(textRect.left(), textRect.top())); }
int GenericCodeEditor::replaceAll( const QRegExp &expr, const QString &replacement, QTextDocument::FindFlags options ) { mSearchSelections.clear(); updateExtraSelections(); if(expr.isEmpty()) return 0; int replacements = 0; bool caps = expr.patternSyntax() != QRegExp::FixedString; QTextDocument *doc = QPlainTextEdit::document(); QTextBlock block = doc->begin(); QTextCursor cursor; QTextCursor(doc).beginEditBlock(); while (block.isValid()) { int blockPos = block.position(); int offset = 0; while(findInBlock(doc, block, expr, offset, options, cursor)) { QString rstr = replacement; if(caps) rstr = resolvedReplacement(rstr, expr); cursor.insertText(rstr); ++replacements; offset = cursor.selectionEnd() - blockPos; } block = block.next(); } QTextCursor(doc).endEditBlock(); return replacements; }
void TestListStyle::testListStyle() { KListStyle ls; KListLevelProperties llp = ls.levelProperties(2); QCOMPARE(llp.level(), 2); llp.setStyle(KListStyle::AlphaLowerItem); KListLevelProperties llp2 = ls.levelProperties(2); QVERIFY(llp2.style() != llp.style()); ls.setLevelProperties(llp); QCOMPARE(llp.level(), 2); QCOMPARE(llp.style(), KListStyle::AlphaLowerItem); llp = ls.levelProperties(2); QCOMPARE(llp.level(), 2); QCOMPARE(llp.style(), KListStyle::AlphaLowerItem); QTextDocument doc; KTextDocument kodoc(&doc); kodoc.setStyleManager(new KStyleManager); QTextCursor cursor(&doc); cursor.insertText("foo\nbar\nBaz\n"); QTextBlock block = doc.begin(); ls.applyStyle(block, 2); QVERIFY(block.textList()); QTextList *textList = block.textList(); QTextListFormat format = textList->format(); QCOMPARE(format.intProperty(QTextListFormat::ListStyle), (int)(KListStyle::AlphaLowerItem)); block = block.next(); QVERIFY(block.isValid()); ls.applyStyle(block, 2); QVERIFY(block.textList()); QCOMPARE(block.textList(), textList); ls.applyStyle(block, 10); // should set the properties of the only one that is set, level 1 QVERIFY(block.textList()); textList = block.textList(); format = textList->format(); QCOMPARE(format.intProperty(QTextListFormat::ListStyle), (int)(KListStyle::AlphaLowerItem)); // getting a properties without setting it doesn't change the list. KListLevelProperties l4 = ls.levelProperties(4); QCOMPARE(l4.level(), 4); QCOMPARE(l4.displayLevel(), 0); // default l4.setDisplayLevel(3); QCOMPARE(l4.displayLevel(), 3); QCOMPARE(ls.hasLevelProperties(4), false); KListLevelProperties anotherL4 = ls.levelProperties(4); QCOMPARE(anotherL4.level(), 4); QCOMPARE(anotherL4.displayLevel(), 0); // default QCOMPARE(ls.hasLevelProperties(4), false); QCOMPARE(ls.hasLevelProperties(5), false); // new levels are a copy of the existing level. KListLevelProperties l5 = ls.levelProperties(5); QCOMPARE(l5.displayLevel(), 0); QCOMPARE(l5.style(), KListStyle::AlphaLowerItem); QCOMPARE(l5.indent(), 0.); }
bool GenericCodeEditor::find( const QRegExp &expr, QTextDocument::FindFlags options ) { // Although QTextDocument provides a find() method, we implement // our own, because the former one is not adequate. if(expr.isEmpty()) return true; bool backwards = options & QTextDocument::FindBackward; QTextCursor c( textCursor() ); int pos; if (c.hasSelection()) { bool matching = expr.exactMatch(c.selectedText()); if( backwards == matching ) pos = c.selectionStart(); else pos = c.selectionEnd(); } else pos = c.position(); QTextDocument *doc = QPlainTextEdit::document(); QTextBlock startBlock = doc->findBlock(pos); int startBlockOffset = pos - startBlock.position(); QTextCursor cursor; if (!backwards) { int blockOffset = startBlockOffset; QTextBlock block = startBlock; while (block.isValid()) { if (findInBlock(doc, block, expr, blockOffset, options, cursor)) break; blockOffset = 0; block = block.next(); } if(cursor.isNull()) { blockOffset = 0; block = doc->begin(); while(true) { if (findInBlock(doc, block, expr, blockOffset, options, cursor) || block == startBlock) break; block = block.next(); } } } else { int blockOffset = startBlockOffset; QTextBlock block = startBlock; while (block.isValid()) { if (findInBlock(doc, block, expr, blockOffset, options, cursor)) break; block = block.previous(); blockOffset = block.length() - 1; } if(cursor.isNull()) { block = doc->end(); while(true) { blockOffset = block.length() - 1; if (findInBlock(doc, block, expr, blockOffset, options, cursor) || block == startBlock) break; block = block.previous(); } } } if(!cursor.isNull()) { setTextCursor(cursor); return true; } else return false; }
void MainWindow::interpolateButtonClicked() { QString tString = ui->tLineEdit->text(); bool ok; double tValue = tString.toDouble(&ok); if(!ok) ; // if you're too stupid to enter a valid number, you're too stupid // to read a proper error message, so no error message here. // get the input by filtering non-valid input data QTextDocument *doc = ui->dataInput->document(); QStringList inputList; for (QTextBlock it = doc->begin(); it != doc->end(); it = it.next()) { QString line = QString::fromStdString(it.text().toStdString()); // empty lines and malformed input are skipped if(line != "" && line.contains(QRegExp("-?\\d+(\\.\\d+)?\\s+-?\\d+(\\.\\d+)?"))) inputList << line.trimmed(); else qDebug() << "Skipping line: " << line; } int number_points = inputList.size(); double data[number_points][2]; // split the lines in x and y for(int i=0; i<inputList.size(); i++) { QString point = inputList.at(i); QStringList l = point.split(QRegExp("\\s+")); // this should never happen if(l.size()<2){ qDebug() << "String couldn't be split."; qDebug() << "Can't interpolate."; return; } data[i][0] = l.at(0).toDouble(); data[i][1] = l.at(1).toDouble(); } // the output file QFile output; output.setFileName("results.dat"); output.open(QIODevice::WriteOnly | QIODevice::Text); QTextStream fout(&output); // basic matrix double Mcr[4][4] = { {-0.5, 1.5, -1.5, 0.5}, {1, -2.5, 2, -0.5}, {-0.5, 0, 0.5, 0}, {0, 1, 0, 0} }; double Gbi[4][2]; for(int i=0; i<number_points-1; i++) { // Select the four points required for the calculation. // With the min/max functions the border points are implicitely // duplicated as they wouldn't be interpolated by the algorithm // otherwise. for(int j=0; j<2; j++) { Gbi[0][j] = data[std::max(0, i-1)][j]; Gbi[1][j] = data[i][j]; Gbi[2][j] = data[std::min(number_points-1, i+1)][j]; Gbi[3][j] = data[std::min(number_points-1, i+2)][j]; } // interpolate points between two given points for(double u=0; u<1; u=u+0.001) { double T[4] = {pow(u,3), pow(u,2), u, 1}; double results[2] = {0, 0}; // interpolate one point by calculating T * Mcr * Gbi for(int bi_ctr=0; bi_ctr<2; bi_ctr++) { double sum = 0; for(int point_row=0; point_row<4; point_row++) { for(int mcr_row=0; mcr_row<4; mcr_row++) { sum += T[mcr_row] * Mcr[mcr_row][point_row] * Gbi[point_row][bi_ctr]; } } results[bi_ctr] = sum; } fout << results[0] << " " << results[1] << "\n"; // if the user entered a valid value for x then display y if(ok) { //qDebug() << "ok"; double diff = results[0] - tValue; //qDebug() << tValue << " " << results[0]; qDebug() << diff; if( diff <= 0.001 ) ui->resultLineEdit->setText(QString("%1").arg(results[1])); } } } output.close(); }
//绘制stub面板上的显示效果 只显示恶性数据竞争的那一行 void DrcDockWidget::stubDrawText() { QTextDocument *document = ui->resultStubEdit->document(); QTextBlock currentBlock = document->begin(); QTextBlock::iterator it; QTextCursor cursor = ui->resultStubEdit->textCursor(); //此处需要注意的是临时变量的生命周期!!! QString tempStr; int tempPos; while( true) { // 在修改chatformat时会改变当前Block的fragment // 所以++it的处理类似std::map中的erase操作 for (it = currentBlock.begin(); !(it.atEnd()); ) { QTextFragment currentFragment = it.fragment(); if (currentFragment.isValid()) { ++it; int pos = currentFragment.position(); QString strText = currentFragment.text(); QTextCharFormat fmt; QFont font; font.setBold(true); fmt.setFont(font); fmt.setForeground(QBrush(QColor(255, 0, 0))); QTextCursor helper = cursor; char str = strText.at(0).toLatin1(); if(str == 'A') { QString strPart2 = strText.split(':').at(1); if(strPart2.toInt() == 1) { helper.setPosition(tempPos); helper.setPosition(tempPos + tempStr.length(), QTextCursor::KeepAnchor); helper.setCharFormat(fmt); helper.setPosition(pos); helper.setPosition(pos + strText.length(), QTextCursor::KeepAnchor); helper.setCharFormat(fmt); } } else { tempStr = strText; tempPos = pos; } } } currentBlock = currentBlock.next(); if(!currentBlock.isValid()) break; } // 光标移动到最后, 并设置拥有焦点 QTextCursor c = ui->resultStubEdit->textCursor(); c.movePosition(QTextCursor::End, QTextCursor::MoveAnchor); ui->resultStubEdit->setTextCursor(c); ui->resultStubEdit->setFocus(Qt::MouseFocusReason); }
void TestStyles::testUnapplyStyle() { // Used to test OverlineColor style QColor testOverlineColor(255, 128, 64); KoCharacterStyle::LineWeight testOverlineWeight = KoCharacterStyle::ThickLineWeight; qreal testOverlineWidth = 1.5; // in this test we should avoid testing any of the hardcodedDefaultProperties; see KoCharacterStyle for details! KoParagraphStyle headers; headers.setOverlineColor(testOverlineColor); headers.setOverlineMode(KoCharacterStyle::ContinuousLineMode); headers.setOverlineStyle(KoCharacterStyle::DottedLine); headers.setOverlineType(KoCharacterStyle::DoubleLine); headers.setOverlineWidth(testOverlineWeight, testOverlineWidth); headers.setFontWeight(QFont::Bold); headers.setAlignment(Qt::AlignCenter); KoParagraphStyle head1; head1.setParentStyle(&headers); head1.setLeftMargin(QTextLength(QTextLength::FixedLength, 40)); QTextDocument doc; doc.setPlainText("abc"); QTextBlock block = doc.begin(); head1.applyStyle(block); QTextCursor cursor(block); QTextBlockFormat bf = cursor.blockFormat(); KoParagraphStyle bfStyle (bf, cursor.charFormat()); QCOMPARE(bf.alignment(), Qt::AlignCenter); QCOMPARE(bfStyle.leftMargin(), 40.); QTextCharFormat cf = cursor.charFormat(); QCOMPARE(cf.colorProperty(KoCharacterStyle::OverlineColor), testOverlineColor); QCOMPARE(cf.intProperty(KoCharacterStyle::OverlineMode), (int) KoCharacterStyle::ContinuousLineMode); QCOMPARE(cf.intProperty(KoCharacterStyle::OverlineStyle), (int) KoCharacterStyle::DottedLine); QCOMPARE(cf.intProperty(KoCharacterStyle::OverlineType), (int) KoCharacterStyle::DoubleLine); QCOMPARE(cf.intProperty(KoCharacterStyle::OverlineWeight), (int) testOverlineWeight); QCOMPARE(cf.doubleProperty(KoCharacterStyle::OverlineWidth), testOverlineWidth); head1.unapplyStyle(block); bf = cursor.blockFormat(); QCOMPARE(bf.hasProperty(QTextFormat::BlockAlignment), false); QCOMPARE(bf.hasProperty(QTextFormat::BlockLeftMargin), false); cf = cursor.charFormat(); QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineColor), false); QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineMode), false); QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineStyle), false); QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineType), false); QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineWeight), false); QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineWidth), false); doc.clear(); block = doc.begin(); head1.applyStyle(block); bf = cursor.blockFormat(); KoParagraphStyle bfStyle2 (bf, cursor.charFormat()); QCOMPARE(bf.alignment(), Qt::AlignCenter); QCOMPARE(bfStyle2.leftMargin(), 40.); cf = cursor.charFormat(); //QCOMPARE(cf.fontOverline(), true); QCOMPARE(cf.colorProperty(KoCharacterStyle::OverlineColor), testOverlineColor); QCOMPARE(cf.intProperty(KoCharacterStyle::OverlineMode), (int) KoCharacterStyle::ContinuousLineMode); QCOMPARE(cf.intProperty(KoCharacterStyle::OverlineStyle), (int) KoCharacterStyle::DottedLine); QCOMPARE(cf.intProperty(KoCharacterStyle::OverlineType), (int) KoCharacterStyle::DoubleLine); QCOMPARE(cf.intProperty(KoCharacterStyle::OverlineWeight), (int) testOverlineWeight); QCOMPARE(cf.doubleProperty(KoCharacterStyle::OverlineWidth), testOverlineWidth); head1.unapplyStyle(block); bf = cursor.blockFormat(); QCOMPARE(bf.hasProperty(QTextFormat::BlockAlignment), false); QCOMPARE(bf.hasProperty(QTextFormat::BlockLeftMargin), false); cf = cursor.charFormat(); //QCOMPARE(cf.hasProperty(QTextFormat::FontOverline), false); doc.setHtml("bla bla<i>italic</i>enzo"); block = doc.begin(); head1.applyStyle(block); bf = cursor.blockFormat(); KoParagraphStyle bfStyle3(bf, cursor.charFormat()); QCOMPARE(bf.alignment(), Qt::AlignCenter); QCOMPARE(bfStyle3.leftMargin(), 40.); cf = cursor.charFormat(); //QCOMPARE(cf.fontOverline(), true); QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineColor), true); QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineMode), true); QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineStyle), true); QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineType), true); QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineWeight), true); QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineWidth), true); cursor.setPosition(7); cursor.setPosition(12, QTextCursor::KeepAnchor); QTextCharFormat italic; italic.setFontItalic(true); cursor.mergeCharFormat(italic); cursor.setPosition(8); cf = cursor.charFormat(); QCOMPARE(cf.fontItalic(), true); cursor.setPosition(0); head1.unapplyStyle(block); cursor.setPosition(0); bf = cursor.blockFormat(); QCOMPARE(bf.hasProperty(QTextFormat::BlockAlignment), false); QCOMPARE(bf.hasProperty(QTextFormat::BlockLeftMargin), false); cf = cursor.charFormat(); //QCOMPARE(cf.hasProperty(QTextFormat::FontOverline), false); QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineColor), false); QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineMode), false); QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineStyle), false); QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineType), false); QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineWeight), false); QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineWidth), false); cursor.setPosition(8); cf = cursor.charFormat(); //QCOMPARE(cf.hasProperty(QTextFormat::FontOverline), false); QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineColor), false); QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineMode), false); QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineStyle), false); QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineType), false); QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineWeight), false); QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineWidth), false); QCOMPARE(cf.fontItalic(), true); cursor.setPosition(12); cf = cursor.charFormat(); //QCOMPARE(cf.hasProperty(QTextFormat::FontOverline), false); QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineColor), false); QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineMode), false); QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineStyle), false); QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineType), false); QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineWeight), false); QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineWidth), false); QCOMPARE(cf.fontItalic(), true); cursor.setPosition(13); cf = cursor.charFormat(); //QCOMPARE(cf.hasProperty(QTextFormat::FontOverline), false); QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineColor), false); QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineMode), false); QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineStyle), false); QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineType), false); QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineWeight), false); QCOMPARE(cf.hasProperty(KoCharacterStyle::OverlineWidth), false); QCOMPARE(cf.hasProperty(QTextFormat::FontWeight), false); QCOMPARE(cf.hasProperty(QTextFormat::FontItalic), false); }
//绘制klee面板上的显示效果 void DrcDockWidget::kleeDrawText() { QTextDocument *document = ui->resultKleeEdit->document(); QTextBlock currentBlock = document->begin(); QTextBlock::iterator it; QTextCursor cursor = ui->resultKleeEdit->textCursor(); while( true) { // 在修改chatformat时会改变当前Block的fragment // 所以++it的处理类似std::map中的erase操作 for (it = currentBlock.begin(); !(it.atEnd()); ) { QTextFragment currentFragment = it.fragment(); if (currentFragment.isValid()) { ++it; int pos = currentFragment.position(); QString strText = currentFragment.text(); qDebug() << "strText: " << strText; if(strText.contains(':')) { char str = strText.at(0).toLatin1(); QTextCharFormat fmt; QColor col; QFont font; font.setBold(true); fmt.setFont(font); fmt.setForeground(QBrush(QColor(255, 0, 0))); QTextCursor helper = cursor; QString strPart1 = strText.split(':').at(0); QString strPart2 = strText.split(':').at(1); switch(str) { case 'V': case 'h': case 'M': pos += strPart1.length() + 1; helper.setPosition(pos); helper.setPosition(pos + strPart2.length(), QTextCursor::KeepAnchor); fmt.setForeground(QBrush(QColor(255, 0, 0))); helper.setCharFormat(fmt); break; case 'i': case '/': pos += strPart1.length() + 1; helper.setPosition(pos); helper.setPosition(pos + strPart2.length(), QTextCursor::KeepAnchor); fmt.setForeground(QBrush(QColor(34, 139, 34))); helper.setCharFormat(fmt); break; case 'T': QString strPart3 = strText.split(':').at(2); //线程ID QString strPart4 = strPart2.split(',').at(0); pos += strPart1.length() + 1; helper.setPosition(pos); helper.setPosition(pos + strPart4.length(), QTextCursor::KeepAnchor); fmt.setForeground(QBrush(QColor(148, 0, 211))); helper.setCharFormat(fmt); pos += strPart2.length() + 1; //读写类型 helper.setPosition(pos); helper.setPosition(pos + strPart3.length(), QTextCursor::KeepAnchor); fmt.setForeground(QBrush(QColor(0, 0, 139))); helper.setCharFormat(fmt); break; } } } } currentBlock = currentBlock.next(); if(!currentBlock.isValid()) break; } // 光标移动到最后, 并设置拥有焦点 QTextCursor c = ui->resultKleeEdit->textCursor(); c.movePosition(QTextCursor::End, QTextCursor::MoveAnchor); ui->resultKleeEdit->setTextCursor(c); ui->resultKleeEdit->setFocus(Qt::MouseFocusReason); }