void MRichTextEdit::textBgColor() { QColor col = QColorDialog::getColor(f_textedit->textBackgroundColor(), this); QTextCursor cursor = f_textedit->textCursor(); if (!cursor.hasSelection()) { cursor.select(QTextCursor::WordUnderCursor); } QTextCharFormat fmt = cursor.charFormat(); if (col.isValid()) { fmt.setBackground(col); } else { fmt.clearBackground(); } cursor.setCharFormat(fmt); f_textedit->setCurrentCharFormat(fmt); bgColorChanged(col); }
void GrepOutputDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const { // there is no function in QString to left-trim. A call to remove this this regexp does the job static const QRegExp leftspaces("^\\s*", Qt::CaseSensitive, QRegExp::RegExp); // rich text component const GrepOutputModel *model = dynamic_cast<const GrepOutputModel *>(index.model()); const GrepOutputItem *item = dynamic_cast<const GrepOutputItem *>(model->itemFromIndex(index)); QStyleOptionViewItemV4 options = option; initStyleOption(&options, index); // building item representation QTextDocument doc; QTextCursor cur(&doc); QPalette::ColorGroup cg = options.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled; QPalette::ColorRole cr = options.state & QStyle::State_Selected ? QPalette::HighlightedText : QPalette::Text; QTextCharFormat fmt = cur.charFormat(); fmt.setFont(options.font); if(item && item->isText()) { // Use custom manual highlighting const KDevelop::SimpleRange rng = item->change()->m_range; // the line number appears grayed fmt.setForeground(options.palette.brush(QPalette::Disabled, cr)); cur.insertText(i18n("Line %1: ",item->lineNumber()), fmt); // switch to normal color fmt.setForeground(options.palette.brush(cg, cr)); cur.insertText(item->text().left(rng.start.column).remove(leftspaces), fmt); fmt.setFontWeight(QFont::Bold); // Blend the highlighted background color // For some reason, it is extremely slow to use alpha-blending directly here QColor bgHighlight = blendColor(option.palette.brush(QPalette::Highlight).color(), option.palette.brush(QPalette::Base).color(), 0.3); fmt.setBackground(bgHighlight); cur.insertText(item->text().mid(rng.start.column, rng.end.column - rng.start.column), fmt); fmt.clearBackground(); fmt.setFontWeight(QFont::Normal); cur.insertText(item->text().right(item->text().length() - rng.end.column), fmt); }else{ QString text; if(item) text = item->text(); else text = index.data().toString(); // Simply insert the text as html. We use this for the titles. doc.setHtml(text); } painter->save(); options.text = QString(); // text will be drawn separately options.widget->style()->drawControl(QStyle::CE_ItemViewItem, &options, painter, options.widget); // set correct draw area QRect clip = options.widget->style()->subElementRect(QStyle::SE_ItemViewItemText, &options); QFontMetrics metrics(options.font); painter->translate(clip.topLeft() - QPoint(0, metrics.descent())); // We disable the clipping for now, as it leads to strange clipping errors // clip.setTopLeft(QPoint(0,0)); // painter->setClipRect(clip); QAbstractTextDocumentLayout::PaintContext ctx; // ctx.clip = clip; painter->setBackground(Qt::transparent); doc.documentLayout()->draw(painter, ctx); painter->restore(); }
void XmlNoteReader::readContent() { QTextCursor cursor(document_->rootFrame()); QTextCharFormat format; bool linkFormat = false; Q_UNUSED(linkFormat); while (!atEnd()) { TokenType token = readNext(); switch(token) { // read the text between the formatting elements case Characters: { // this commented out code does not work with formatted text and multiple links // TODO a QTextDocumentFragment must be created via QTextCursor::selected, the fragment must be exported to html // the link applied and reinserted via QTextCursor::insertHtml // if(linkFormat) // { // qDebug("link inserted"); // cursor.insertHtml("<a href=\"" + text().toString() + "\">" + text().toString() + "</a>"); // } // else cursor.insertText(text().toString(),format); break; } // read elements <bold> <italic> and set the formatting case StartElement: { if(name() == "bold") format.setFontWeight(QFont::Bold); if(name() == "italic") format.setFontItalic(true); if(name() == "underline") format.setUnderlineStyle(QTextCharFormat::SingleUnderline); if(name() == "strikethrough" || name() == "strikeout") // only strikethrough is written, but strikeout is also allowed for reading format.setFontStrikeOut(true); if(name() == "highlight") format.setBackground(QColor(255,255,0)); if(qualifiedName() == "size:small") format.setFontPointSize(QApplication::font().pointSize()* 0.8f); if(qualifiedName() == "size:large") format.setFontPointSize(QApplication::font().pointSize()*1.4f); if(qualifiedName() == "size:huge") format.setFontPointSize(QApplication::font().pointSize()*1.6f); if(name() == "monospace") format.setFontFamily("Monospace"); if(qualifiedName() == "link:url") linkFormat = true; break; } // unset formatting case EndElement: { if(name() == "note-content") // end of note content, exit this method return; if(name() == "bold") format.setFontWeight(QFont::Normal); if(name() == "italic") format.setFontItalic(false); if(name() == "underline") format.setUnderlineStyle(QTextCharFormat::NoUnderline); if(name() == "strikethrough" || name() == "strikeout") // only strikethrough is written, but strikeout is also allowed for reading format.setFontStrikeOut(false); if(name() == "highlight") format.clearBackground(); if(qualifiedName() == "size:small" || qualifiedName() == "size:large" || qualifiedName() == "size:huge") // mutual exclusive options format.setFontPointSize(QApplication::font().pointSizeF()); if(name() == "monospace") format.setFontFamily(QApplication::font().family()); if(name() == "link:url") linkFormat = false; // ignore id break; } default:; // suppress compiler warnings } if (QXmlStreamReader::hasError()) { qDebug("XmlNoteReader::read failed: Error reading xml content"); return; } } }