Example #1
1
void HtmlDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,const QModelIndex &index) const
{
    QStyleOptionViewItemV4 optionV4(option);
    initStyleOption(&optionV4, index);
    QStyle *style = optionV4.widget ? optionV4.widget->style() : QApplication::style();
    style->drawPrimitive(QStyle::PE_PanelItemViewItem, &optionV4, painter, optionV4.widget);

    QTextDocument doc;
    QString text = optionV4.text;
    doc.setHtml(text);
    doc.setPageSize( option.rect.size() );

    /// Painting item without text
    optionV4.text.clear();
    style->drawControl(QStyle::CE_ItemViewItem, &optionV4, painter);

    QAbstractTextDocumentLayout::PaintContext ctx;

    // Highlighting text if item is selected
    if (optionV4.state & QStyle::State_Selected)
        ctx.palette.setColor(QPalette::Text, optionV4.palette.color(QPalette::Active, QPalette::HighlightedText));

    QRect textRect = option.rect;
    painter->save();
    painter->translate(textRect.topLeft());
    painter->setClipRect(textRect.translated(-textRect.topLeft()));
    doc.documentLayout()->draw(painter, ctx);
    painter->restore();
}
void
SqlSearchDelegate::paint(QPainter *painter,
                      const QStyleOptionViewItem &option,
                      const QModelIndex &index) const
{
    QStyleOptionViewItemV4 optionV4 = option;
    initStyleOption(&optionV4, index);

    QStyle *style = optionV4.widget? optionV4.widget->style() : QApplication::style();

    QTextDocument doc;
    doc.setHtml(optionV4.text);
    optionV4.text = QString();
    style->drawControl(QStyle::CE_ItemViewItem, &optionV4, painter);

    QAbstractTextDocumentLayout::PaintContext ctx;
    QPalette::ColorGroup cg = (option.state & QStyle::State_Enabled) ?
                (option.state & QStyle::State_Active) ? QPalette::Normal : QPalette::Inactive : QPalette::Disabled;

    if (optionV4.state & QStyle::State_Selected)
        ctx.palette.setColor(QPalette::Text, optionV4.palette.color(cg, QPalette::HighlightedText));
    else
        ctx.palette.setColor(QPalette::Text, optionV4.palette.color(cg, QPalette::WindowText));

    QRect textRect = style->subElementRect(QStyle::SE_ItemViewItemText, &optionV4);
    painter->save();
    painter->translate(textRect.topLeft());
    painter->setClipRect(textRect.translated(-textRect.topLeft()));
    doc.documentLayout()->draw(painter, ctx);
    painter->restore();
}
void EventItemDelegate::paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
    painter->save();

    QStyleOptionViewItemV4 opt = option;
    initStyleOption(&opt, index);

    QVariant value = index.data();
    QBrush bgBrush = qvariant_cast<QBrush>(index.data(Qt::BackgroundRole));
    QBrush fgBrush = qvariant_cast<QBrush>(index.data(Qt::ForegroundRole));
    painter->setClipRect( opt.rect );
    painter->setBackgroundMode(Qt::OpaqueMode);
    painter->setBackground(Qt::transparent);
    painter->setBrush(bgBrush);

    if (bgBrush.style() != Qt::NoBrush) {
        QPen bgPen;
        bgPen.setColor(bgBrush.color().darker(250));
        bgPen.setStyle(Qt::SolidLine);
        bgPen.setWidth(1);
        painter->setPen(bgPen);
        painter->drawRoundedRect(opt.rect.x(), opt.rect.y(), opt.rect.width() - bgPen.width(), opt.rect.height() - bgPen.width(), 3.0, 3.0);
    }

    QTextDocument doc;
    doc.setDocumentMargin(3);
    doc.setDefaultStyleSheet("* {color: " + fgBrush.color().name() + ";}");
    doc.setHtml("<html><qt></head><meta name=\"qrichtext\" content=\"1\" />" + displayText(value, QLocale::system()) + "</qt></html>");
    QAbstractTextDocumentLayout::PaintContext context;
    doc.setPageSize( opt.rect.size());
    painter->translate(opt.rect.x(), opt.rect.y());
    doc.documentLayout()->draw(painter, context);
    painter->restore();
}
void KPrPlaceholderTextStrategy::paint( QPainter & painter, const KoViewConverter &converter, const QRectF & rect, KoShapePaintingContext &paintcontext)
{
    if ( m_textShape ) {
        painter.save();
        m_textShape->setSize( rect.size() );
        // this code is needed to make sure the text of the textshape is layouted before it is painted
        KoTextShapeData * shapeData = qobject_cast<KoTextShapeData*>( m_textShape->userData() );
        QTextDocument * document = shapeData->document();
        KoTextDocumentLayout * lay = qobject_cast<KoTextDocumentLayout*>( document->documentLayout() );
        if ( lay ) {
            lay->layout();
        }
        m_textShape->paint( painter, converter, paintcontext);

        KoShape::applyConversion( painter, converter );
        QPen pen(Qt::gray, 0);
        //pen.setStyle( Qt::DashLine ); // endless loop
        painter.setPen( pen );
        painter.drawRect( rect );
        painter.restore();
    }
    else {
        KPrPlaceholderStrategy::paint( painter, converter, rect, paintcontext);
    }
}
Example #5
0
void MessageViewDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QStyleOptionViewItemV4 optionV4 = option;
    initStyleOption(&optionV4, index);

    QStyle *style = optionV4.widget? optionV4.widget->style() : QApplication::style();

    QTextDocument doc;
    QString align(index.data(MessageModel::TypeRole) == 1 ? "left" : "right");
    QString html = "<p align=\"" + align + "\" style=\"color:black;\">" + index.data(MessageModel::HTMLRole).toString() + "</p>";
    doc.setHtml(html);

    /// Painting item without text
    optionV4.text = QString();
    style->drawControl(QStyle::CE_ItemViewItem, &optionV4, painter);

    QAbstractTextDocumentLayout::PaintContext ctx;

    // Highlighting text if item is selected
    if (optionV4.state & QStyle::State_Selected)
        ctx.palette.setColor(QPalette::Text, optionV4.palette.color(QPalette::Active, QPalette::HighlightedText));

    QRect textRect = style->subElementRect(QStyle::SE_ItemViewItemText, &optionV4);
    doc.setTextWidth( textRect.width() );
    painter->save();
    painter->translate(textRect.topLeft());
    painter->setClipRect(textRect.translated(-textRect.topLeft()));
    doc.documentLayout()->draw(painter, ctx);
    painter->restore();
}
Example #6
0
/*!
  Draw a text document into a rectangle

  \param painter Painter
  \param rect Traget rectangle
  \param flags Alignments/Text flags, see QPainter::drawText()
  \param text Text document
*/
void QwtPainter::drawSimpleRichText( QPainter *painter, const QRectF &rect,
    int flags, const QTextDocument &text )
{
    QTextDocument *txt = text.clone();

    painter->save();

    painter->setFont( txt->defaultFont() );
    qwtUnscaleFont( painter );

    txt->setDefaultFont( painter->font() );
    txt->setPageSize( QSizeF( rect.width(), QWIDGETSIZE_MAX ) );

    QAbstractTextDocumentLayout* layout = txt->documentLayout();

    const double height = layout->documentSize().height();
    double y = rect.y();
    if ( flags & Qt::AlignBottom )
        y += ( rect.height() - height );
    else if ( flags & Qt::AlignVCenter )
        y += ( rect.height() - height ) / 2;

    QAbstractTextDocumentLayout::PaintContext context;
    context.palette.setColor( QPalette::Text, painter->pen().color() );

    painter->translate( rect.x(), y );
    layout->draw( painter, context );

    painter->restore();
    delete txt;
}
Example #7
0
void QwtPainter::drawSimpleRichText(QPainter *painter, const QRect &rect,
    int flags, QTextDocument &text)
{
    const QRect scaledRect = d_metricsMap.layoutToDevice(rect, painter);
    text.setPageSize(QSize(scaledRect.width(), QWIDGETSIZE_MAX));

    QAbstractTextDocumentLayout* layout = text.documentLayout();

    const int height = qRound(layout->documentSize().height());
    int y = scaledRect.y();
    if (flags & Qt::AlignBottom)
        y += (scaledRect.height() - height);
    else if (flags & Qt::AlignVCenter)
        y += (scaledRect.height() - height)/2;

    QAbstractTextDocumentLayout::PaintContext context;
    context.palette.setColor(QPalette::Text, painter->pen().color());

    painter->save();

    painter->translate(scaledRect.x(), y);
    layout->draw(painter, context);

    painter->restore();
}
QSize KviTalIconAndRichTextItemDelegate::sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const
{
	QString szText = index.data(Qt::DisplayRole).toString();
	QTextDocument doc;
	doc.setHtml(szText);
	doc.setDefaultFont(option.font);
	doc.setTextWidth(((QListWidget *)parent())->viewport()->width() - LVI_AFTER_ICON - LVI_BORDER);
	int iHeight = doc.documentLayout()->documentSize().toSize().height();

	//qDebug("Size hint (%d,%d)",((QListWidget *)parent())->minimumWidth(), iHeight + (2 * LVI_BORDER));

	int iIconWidth = m_oIconSize.width() + (2 * LVI_BORDER);
	int iIconHeight = m_oIconSize.height() + (2 * LVI_BORDER);

	int w = ((QListWidget *)parent())->minimumWidth();
	if(w < iIconWidth)
		w = iIconWidth;
	if(w < m_oMinimumSize.width())
		w = m_oMinimumSize.width();
	int h = iHeight + (2 * LVI_BORDER);
	if(h < iIconHeight)
		h = iIconHeight;
	if(h < m_oMinimumSize.height())
		h = m_oMinimumSize.height();

	return { w, h };
}
Example #9
0
void ExpandingLabel::allTextVisible() {
	QTextDocument *doc = document();
	doc->setTextWidth(width());
	int height = doc->documentLayout()->documentSize().toSize().height();
	setStyleSheet("border: 0px; background-color: transparent; margin-top: 0px; margin-bottom: 0px;");
	setFixedHeight(height);
}
Example #10
0
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());

}
Example #11
0
//-----------------------------------------------------------------------------
int ctkFittedTextBrowser::heightForWidth(int _width) const
{
  QTextDocument* doc = this->document();
  qreal savedWidth = doc->textWidth();
  
  // Fudge factor. This is the difference between the frame and the 
  // viewport.
  int fudge = 2 * this->frameWidth();
  
  // Do the calculation assuming no scrollbars
  doc->setTextWidth(_width - fudge);
  int noScrollbarHeight =
    doc->documentLayout()->documentSize().height() + fudge;
  
  // (If noScrollbarHeight is greater than the maximum height we'll be
  // allowed, then there will be scrollbars, and the actual required
  // height will be even higher. But since in this case we've already
  // hit the maximum height, it doesn't matter that we underestimate.)
  
  // Get minimum height (even if string is empty): one line of text
  int _minimumHeight = QFontMetrics(doc->defaultFont()).lineSpacing() + fudge;
  int ret = qMax(noScrollbarHeight, _minimumHeight);

  doc->setTextWidth(savedWidth);
  return ret;
}
Example #12
0
void TestChangeTrackedDelete::testPrefixMerge()
{
    TextTool *textTool = new TextTool(new MockCanvas);
    KoTextEditor *textEditor = textTool->textEditor();
    QVERIFY(textEditor);
    QTextDocument *document = textEditor->document();
    KTextDocumentLayout *layout = qobject_cast<KTextDocumentLayout*>(document->documentLayout());
    QTextCursor *cursor = textEditor->cursor();
    cursor->insertText("Hello World");
    cursor->setPosition(3);
    ChangeTrackedDeleteCommand *delCommand = new ChangeTrackedDeleteCommand(ChangeTrackedDeleteCommand::NextChar, textTool);
    textEditor->addCommand(delCommand);
    QCOMPARE(document->characterAt(3).unicode(), (ushort)(QChar::ObjectReplacementCharacter));
    cursor->setPosition(4);
    delCommand = new ChangeTrackedDeleteCommand(ChangeTrackedDeleteCommand::NextChar, textTool);
    textEditor->addCommand(delCommand);
    // This is wierd. Without this loop present the succeeding call to inlineTextObject returs NULL. Why ??????
    for (int i=0; i<document->characterCount(); i++) {
        cursor->setPosition(i);
    }
    cursor->setPosition(4);
    KDeleteChangeMarker *testMarker = dynamic_cast<KDeleteChangeMarker*>(layout->inlineTextObjectManager()->inlineTextObject(*cursor));
    QTextDocumentFragment deleteData =  KTextDocument(document).changeTracker()->elementById(testMarker->changeId())->deleteData();
    QCOMPARE(deleteData.toPlainText(), QString("lo"));
    delete textTool;
}
Example #13
0
void HTMLDelegate::paint(QPainter* painter, const QStyleOptionViewItem & option, const QModelIndex &index) const
{
    QStyleOptionViewItemV4 options = option;
    initStyleOption(&options, index);

    painter->save();

    QTextDocument doc;
    doc.setHtml(options.text);

    options.text = "";
    options.widget->style()->drawControl(QStyle::CE_ItemViewItem, &options, painter);

    // shift text right to make icon visible
    QSize iconSize = options.icon.actualSize(options.rect.size());
    painter->translate(options.rect.left()+iconSize.width(), options.rect.top());
    QRect clip(0, 0, options.rect.width()+iconSize.width(), options.rect.height());

    //doc.drawContents(painter, clip);

    painter->setClipRect(clip);
    QAbstractTextDocumentLayout::PaintContext ctx;
    // set text color to red for selected item
    if (option.state & QStyle::State_Selected)
        ctx.palette.setColor(QPalette::Text, QColor("red"));
    ctx.clip = clip;
    doc.documentLayout()->draw(painter, ctx);

    painter->restore();
}
Example #14
0
void SPHtmlDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
    QStyleOptionViewItemV4 options = option;
    initStyleOption(&options, index);

    QTextDocument doc;
    //doc.setDocumentMargin(0);
    doc.setHtml(index.data().toString());

    painter->save();
    options.text = QString();  // clear old text
    options.widget->style()->drawControl(QStyle::CE_ItemViewItem, &options, painter, options.widget);

    // draw area
    QRect clip = options.widget->style()->subElementRect(QStyle::SE_ItemViewItemText, &options);
    QFontMetrics metrics(options.font);
    if (index.flags() == Qt::NoItemFlags) {
        painter->setBrush(QBrush(QWidget().palette().color(QPalette::Base)));
        painter->setPen(QWidget().palette().color(QPalette::Base));
        painter->drawRect(QRect(clip.topLeft() - QPoint(20, metrics.descent()), clip.bottomRight()));
        painter->translate(clip.topLeft() - QPoint(20, metrics.descent()));
    }
    else {
        painter->translate(clip.topLeft() - QPoint(0, metrics.descent()));
    }
    QAbstractTextDocumentLayout::PaintContext pcontext;
    doc.documentLayout()->draw(painter, pcontext);

    painter->restore();
}
Example #15
0
void ChannelsDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opt,
						  const QModelIndex &index) const
{
	QVariant data = index.data();
	if (data.canConvert<QTextDocument *>()) {
		QTextDocument *textDocument = data.value<QTextDocument *>();
		QStyleOptionViewItemV4 option(opt);
		QStyle *style = option.widget ? option.widget->style() : QApplication::style();
		painter->save();
		// Draw background.
		style->drawPrimitive(QStyle::PE_PanelItemViewItem, &option, painter, option.widget);
		// Draw text using QTextDocument.
		int pad = 1;
		QRect textRect = option.rect.adjusted(pad, pad, -pad, -pad);
		QRect clipRect(0, 0, textRect.width(), textRect.height());
		painter->translate(textRect.x(), textRect.y());
		painter->setClipRect(clipRect);
		QAbstractTextDocumentLayout::PaintContext ctx;
		ctx.palette = option.palette;
		ctx.clip = QRect(clipRect);
		textDocument->documentLayout()->draw(painter, ctx);
		painter->restore();
	} else {
		QStyledItemDelegate::paint(painter, opt, index);
	}
}
Example #16
0
QAbstractTextDocumentLayout *QTextDocumentProto::documentLayout() const
{
  QTextDocument *item = qscriptvalue_cast<QTextDocument*>(thisObject());
  if (item)
    return item->documentLayout();
  return 0;
}
void QgsWelcomePageItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem & option, const QModelIndex &index ) const
{
  painter->save();

  QTextDocument doc;
  QPixmap icon = qvariant_cast<QPixmap>( index.data( Qt::DecorationRole ) );

  QAbstractTextDocumentLayout::PaintContext ctx;
  QStyleOptionViewItemV4 optionV4 = option;

  QColor color;
  if ( option.state & QStyle::State_Selected && option.state & QStyle::State_HasFocus )
  {
    color = QColor( 255, 255, 255, 60 );
    ctx.palette.setColor( QPalette::Text, optionV4.palette.color( QPalette::Active, QPalette::HighlightedText ) );

    QStyle *style = QApplication::style();
    style->drawPrimitive( QStyle::PE_PanelItemViewItem, &option, painter, nullptr );
  }
  else if ( option.state & QStyle::State_Enabled )
  {
    color = QColor( 100, 100, 100, 30 );
    ctx.palette.setColor( QPalette::Text, optionV4.palette.color( QPalette::Active, QPalette::Text ) );

    QStyle *style = QApplication::style();
    style->drawPrimitive( QStyle::PE_PanelItemViewItem, &option, painter, nullptr );
  }
  else
  {
    color = QColor( 100, 100, 100, 30 );
    ctx.palette.setColor( QPalette::Text, optionV4.palette.color( QPalette::Disabled, QPalette::Text ) );
  }

  painter->setRenderHint( QPainter::Antialiasing );
  painter->setPen( QColor( 0, 0, 0, 0 ) );
  painter->setBrush( QBrush( color ) );
  painter->drawRoundedRect( option.rect.left() + 5, option.rect.top() + 5, option.rect.width() - 10, option.rect.height() - 10, 8, 8 );

  int titleSize = QApplication::fontMetrics().height() * 1.1;
  int textSize = titleSize * 0.85;

  doc.setHtml( QStringLiteral( "<div style='font-size:%1px;'><span style='font-size:%2px;font-weight:bold;'>%3</span><br>%4<br>%5</div>" ).arg( textSize ).arg( titleSize )
               .arg( index.data( QgsWelcomePageItemsModel::TitleRole ).toString(),
                     index.data( QgsWelcomePageItemsModel::PathRole ).toString(),
                     index.data( QgsWelcomePageItemsModel::CrsRole ).toString() ) );
  doc.setTextWidth( option.rect.width() - ( !icon.isNull() ? icon.width() + 35 : 35 ) );

  if ( !icon.isNull() )
  {
    painter->drawPixmap( option.rect.left() + 10, option.rect.top()  + 10, icon );
  }

  painter->translate( option.rect.left() + ( !icon.isNull() ? icon.width() + 25 : 15 ), option.rect.top() + 15 );
  ctx.clip = QRect( 0, 0, option.rect.width() - ( !icon.isNull() ? icon.width() - 35 : 25 ), option.rect.height() - 25 );
  doc.documentLayout()->draw( painter, ctx );

  painter->restore();
}
Example #18
0
QSize RichTextRenderer::findNaturalSize(int atWidth)
{
	QTextDocument doc;
	if(atWidth > 0)
		doc.setTextWidth(atWidth);
	if (Qt::mightBeRichText(html()))
		doc.setHtml(html());
	else
		doc.setPlainText(html());
	
	QSize firstSize = doc.documentLayout()->documentSize().toSize();
	QSize checkSize = firstSize;
	
// 	qDebug() << "RichTextRenderer::findNaturalSize: atWidth:"<<atWidth<<", firstSize:"<<firstSize;
	
	#define RUNAWAY_LIMIT 500
	
	int counter = 0;
	int deInc = 10;
	while(checkSize.height() == firstSize.height() &&
	      checkSize.height() > 0 &&
	      counter < RUNAWAY_LIMIT)
	{
		int w = checkSize.width() - deInc;
		doc.setTextWidth(w);
		checkSize = doc.documentLayout()->documentSize().toSize();
		
// 		qDebug() << "RichTextRenderer::findNaturalSize: w:"<<w<<", checkSize:"<<checkSize<<", counter:"<<counter;
		counter ++;
	}
	
	if(checkSize.width() != firstSize.width())
	{
		int w = checkSize.width() + deInc;
		doc.setTextWidth(w);
		checkSize = doc.documentLayout()->documentSize().toSize();
// 		qDebug() << "RichTextRenderer::findNaturalSize: Final Size: w:"<<w<<", checkSize:"<<checkSize;
		return checkSize;
	}
	else
	{
// 		qDebug() << "RichTextRenderer::findNaturalSize: No Change, firstSize:"<<checkSize;
		return firstSize;
	}
}
Example #19
0
void TestChangeTrackedDelete::testListDelete()
{
    TextTool *textTool = new TextTool(new MockCanvas);
    KoTextEditor *textEditor = textTool->textEditor();
    QVERIFY(textEditor);
    QTextDocument *document = textEditor->document();
    KTextDocumentLayout *layout = qobject_cast<KTextDocumentLayout*>(document->documentLayout());
    QTextCursor *cursor = textEditor->cursor();
    insertSampleList(document);

    cursor->setPosition(16);
    cursor->setPosition(152, QTextCursor::KeepAnchor);
    ChangeTrackedDeleteCommand *delCommand = new ChangeTrackedDeleteCommand(ChangeTrackedDeleteCommand::NextChar, textTool);
    textEditor->addCommand(delCommand);
    QCOMPARE(document->characterAt(16).unicode(), (ushort)(QChar::ObjectReplacementCharacter));

    // This is wierd. Without this loop present the succeeding call to inlineTextObject returs NULL. Why ??????
    for (int i=0; i<document->characterCount(); i++) {
        cursor->setPosition(i);
    }

    cursor->setPosition(17);
    KDeleteChangeMarker *testMarker = dynamic_cast<KDeleteChangeMarker*>(layout->inlineTextObjectManager()->inlineTextObject(*cursor));
    QTextDocumentFragment deleteData =  KTextDocument(document).changeTracker()->elementById(testMarker->changeId())->deleteData();

    QTextDocument deleteDocument;
    QTextCursor deleteCursor(&deleteDocument);

    deleteCursor.insertFragment(deleteData);
    bool listFound = false;

    for (int i=0; i < deleteDocument.characterCount(); i++) {
        deleteCursor.setPosition(i);
        if (deleteCursor.currentList()) {
            listFound = true;
            continue;
        }
    }

    QVERIFY(listFound == true);
    QTextList *deletedList = deleteCursor.currentList();
    bool deletedListStatus = deletedList->format().boolProperty(KDeleteChangeMarker::DeletedList);
    QVERIFY (deletedListStatus == true);
    bool deletedListItemStatus;
    deletedListItemStatus  = deletedList->item(0).blockFormat().boolProperty(KDeleteChangeMarker::DeletedListItem);
    QVERIFY(deletedListItemStatus == true);
    deletedListItemStatus  = deletedList->item(1).blockFormat().boolProperty(KDeleteChangeMarker::DeletedListItem);
    QVERIFY(deletedListItemStatus == true);
    deletedListItemStatus  = deletedList->item(2).blockFormat().boolProperty(KDeleteChangeMarker::DeletedListItem);
    QVERIFY(deletedListItemStatus == true);
    deletedListItemStatus  = deletedList->item(3).blockFormat().boolProperty(KDeleteChangeMarker::DeletedListItem);
    QVERIFY(deletedListItemStatus == true);
    deletedListItemStatus  = deletedList->item(4).blockFormat().boolProperty(KDeleteChangeMarker::DeletedListItem);
    QVERIFY(deletedListItemStatus == true);
    delete textTool;
}
Example #20
0
void HtmlDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
    QStyleOptionViewItem opt = option;
    initStyleOption(&opt, index);

    QStyle* style = opt.widget ? opt.widget->style() : QApplication::style();
    QAbstractItemView* parent = this->parent();

    const int top = opt.rect.top();
    const int height = opt.rect.height();
    int left = opt.rect.left();
    int width = opt.rect.width(); // the width of the item excluding the arrows but including the checkbox and icons
    if (opt.features & QStyleOptionViewItem::HasCheckIndicator) {
        const int padding = checkboxWidth(style, parent, opt);
        left += padding;
        width -= padding;
    }
    if (opt.features & QStyleOptionViewItem::HasDecoration) {
        const int padding = iconWidth(style, parent, opt);
        left += padding;
        width -= padding;
    }

    QTextDocument doc;
    // doc.setDefaultFont(QFont("Roboto", 9));
    doc.setDefaultStyleSheet("ul { margin-left: -36px; } ol { margin-left: -28px; }");

    doc.setTextWidth(width);
    doc.setHtml(opt.text);

    painter->save();
    opt.text = ""; // draw the item but with empty text
    style->drawControl(QStyle::CE_ItemViewItem, &opt, painter, opt.widget); // the 4th argument is required for QStyle to determine the style used to paint the control

    painter->translate(left, top);
    QRect clip(0, 0, width, height);
    painter->setClipRect(clip);

    QAbstractTextDocumentLayout::PaintContext context;
    context.clip = clip;
    const QColor textColor = this->textColor(index);
    context.palette.setColor(QPalette::AlternateBase, Qt::yellow);
    if (textColor != QColor(Qt::black)) {
        context.palette.setColor(QPalette::Text, textColor);
    }

    doc.documentLayout()->draw(painter, context);
    painter->restore();
}
Example #21
0
int htmlLineCount( QFont font, QString text, int width) {

    QTextDocument displayDoc;

    setUpDisplayDoc(displayDoc,font);
    displayDoc.setHtml(text);
    displayDoc.setTextWidth(-1);

    displayDoc.setPageSize(QSizeF(width, QWIDGETSIZE_MAX));
    QFontMetrics fm(font);
    int fontHeight = fm.height();
    int lineHeight = displayDoc.documentLayout()->documentSize().height();
    return lineHeight/fontHeight;

}
/*!
  Draw a text document into a rectangle

  \param painter Painter
  \param rect Traget rectangle
  \param flags Alignments/Text flags, see QPainter::drawText()
  \param text Text document
*/
void QwtPainter::drawSimpleRichText( QPainter *painter, const QRectF &rect,
    int flags, const QTextDocument &text )
{
    QTextDocument *txt = text.clone();

    painter->save();

    QRectF unscaledRect = rect;

    if ( painter->font().pixelSize() < 0 )
    {
        const QSize res = qwtScreenResolution();

        const QPaintDevice *pd = painter->device();
        if ( pd->logicalDpiX() != res.width() ||
            pd->logicalDpiY() != res.height() )
        {
            QTransform transform;
            transform.scale( res.width() / double( pd->logicalDpiX() ),
                res.height() / double( pd->logicalDpiY() ));

            painter->setWorldTransform( transform, true );
            unscaledRect = transform.inverted().mapRect(rect);
        }
    }  

    txt->setDefaultFont( painter->font() );
    txt->setPageSize( QSizeF( unscaledRect.width(), QWIDGETSIZE_MAX ) );

    QAbstractTextDocumentLayout* layout = txt->documentLayout();

    const double height = layout->documentSize().height();
    double y = unscaledRect.y();
    if ( flags & Qt::AlignBottom )
        y += ( unscaledRect.height() - height );
    else if ( flags & Qt::AlignVCenter )
        y += ( unscaledRect.height() - height ) / 2;

    QAbstractTextDocumentLayout::PaintContext context;
    context.palette.setColor( QPalette::Text, painter->pen().color() );

    painter->translate( unscaledRect.x(), y );
    layout->draw( painter, context );

    painter->restore();
    delete txt;
}
Example #23
0
void QgsWelcomePageItemDelegate::paint( QPainter* painter, const QStyleOptionViewItem & option, const QModelIndex &index ) const
{
  painter->save();

  QTextDocument doc;
  QAbstractTextDocumentLayout::PaintContext ctx;

  QColor color;
  if ( option.state & QStyle::State_Selected )
  {
    color = QColor( 255, 255, 255, 60 );
    QStyle *style = QApplication::style();
    style->drawPrimitive( QStyle::PE_PanelItemViewItem, &option, painter, NULL );
  }
  else if ( option.state & QStyle::State_Enabled )
  {
    color = QColor( 100, 100, 100, 30 );
  }
  else
  {
    color = QColor( 100, 100, 100, 30 );
    ctx.palette.setColor( QPalette::Text, QColor( 150, 150, 150, 255 ) );
  }

  painter->setRenderHint( QPainter::Antialiasing );
  painter->setPen( QColor( 0, 0, 0, 0 ) );
  painter->setBrush( QBrush( color ) );
  painter->drawRoundedRect( option.rect.left() + 5, option.rect.top() + 5, option.rect.width() - 10, option.rect.height() - 10, 8, 8 );

  doc.setHtml( QString( "<span style='font-size:18px;font-weight:bold;'>%1</span><br>%2<br>%3" ).arg( index.data( QgsWelcomePageItemsModel::TitleRole ).toString() ).arg( index.data( QgsWelcomePageItemsModel::PathRole ).toString() ).arg( index.data( QgsWelcomePageItemsModel::CrsRole ).toString() ) );
  doc.setTextWidth( 800 );

  QPixmap icon = qvariant_cast<QPixmap>( index.data( Qt::DecorationRole ) );
  if ( !icon.isNull() )
  {
    painter->drawPixmap( option.rect.left() + 10, option.rect.top()  + 10, icon );
  }

  painter->translate( option.rect.left() + ( !icon.isNull() ? icon.width() + 25 : 15 ), option.rect.top() + 15 );
  ctx.clip = QRect( 0, 0, option.rect.width() - ( !icon.isNull() ? icon.width() - 35 : 25 ), option.rect.height() - 25 );
  doc.documentLayout()->draw( painter, ctx );

  painter->restore();
}
Example #24
0
void HtmlDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
    if (!index.isValid())
        return;

    QStyleOptionViewItemV4 options = option;
    initStyleOption(&options, index);

    painter->save();

    QTextDocument doc;
    doc.setHtml(options.text);
    QIcon icon = options.icon;

    options.text = "";
    options.icon = QIcon();
    options.widget->style()->drawControl(QStyle::CE_ItemViewItem, &options, painter);

    // Draw icon ................................
    QSize iconSize = icon.actualSize(mIconSize);
    painter->translate(options.rect.left(), options.rect.top());
    QRect iconRect = QRect(4, 4, iconSize.width(), iconSize.height());

    icon.paint(painter, iconRect);

    doc.setTextWidth(options.rect.width() - iconRect.right() - 10);

    // shift text right to make icon visible
    painter->translate(iconRect.right() + 8, 0);
    QRect clip(0, 0, options.rect.width()-iconRect.right()- 10, options.rect.height());


    painter->setClipRect(clip);
    QAbstractTextDocumentLayout::PaintContext ctx;
    // set text color to red for selected item
    if (option.state & QStyle::State_Selected)
    {
        ctx.palette.setColor(QPalette::Text, option.palette.color(QPalette::HighlightedText));
    }
    ctx.clip = clip;
    doc.documentLayout()->draw(painter, ctx);

    painter->restore();
}
void
PlaylistLargeItemDelegate::drawRichText( QPainter* painter, const QRect& rect, int flags, QTextDocument& text ) const
{
    text.setPageSize( QSize( rect.width(), QWIDGETSIZE_MAX ) );
    QAbstractTextDocumentLayout* layout = text.documentLayout();

    const int height = qRound( layout->documentSize().height() );
    int y = rect.y();
    if ( flags & Qt::AlignBottom )
        y += ( rect.height() - height );
    else if ( flags & Qt::AlignVCenter )
        y += ( rect.height() - height ) / 2;

    QAbstractTextDocumentLayout::PaintContext context;
    context.palette.setColor( QPalette::Text, painter->pen().color() );

    painter->save();
    painter->translate( rect.x(), y );
    layout->draw( painter, context );
    painter->restore();
}
Example #26
0
void TabWidget::paintEvent(QPaintEvent* e) {
	QTabWidget::paintEvent(e);
	QPainter p(this);
	if(isActive) {
		p.fillRect(e->rect(), QColor(32, 128, 255, 96));
	}

	float width = e->rect().size().width()/2;
	QTextDocument doc;

	doc.documentLayout()->setPaintDevice(p.device());
	doc.setHtml("<center><font size='10'><b><u>Open a document<u></b></font></center><font size='6'><ul><li>File menu > Open (Ctrl + O)</li><li>Drag and drop files here</li><li>If any, selecting file from the open documents list</li><li>Selecting file from the drop box in the header.</li></ul></font>");
	//doc.setTextWidth(width/2);
	p.setPen(palette().foreground().color());

	QRectF rct = e->rect();
	rct.setWidth(width);
	p.translate(rct.size().width()/2, rct.size().height()/2);
	doc.drawContents(&p, rct);

}
Example #27
0
QSize CustomLabel::sizeHint() const
{
#ifdef DEBUG_CUSTOMLABEL
    QSize sh = QLabel::sizeHint();
    //sh.setWidth(sh.width() + 1);
    //sh.setHeight(sh.height() + 4);
    qDebug() << "for text:" << text();
    qDebug() << "  size hint:" << sh;

    QTextDocument *doc = textDocument();
    sh = doc->documentLayout()->documentSize().toSize();
    qDebug() << "  doc size:" << sh;

    sh += QSize(doc->documentMargin(), doc->documentMargin());
    qDebug() << "  doc size with margin:" << sh;

    return sh;
#else
    return QLabel::sizeHint();
#endif
}
void IMHistoryItemPainter::paint(QPainter *painter, const QStyleOptionViewItem &option, EditMode mode) const
{
//    if (mode == Editable) {
//        painter->setBrush(option.palette.highlight());
//    } else {
        painter->setBrush(option.palette.foreground());
//    }
    if (option.state & QStyle::State_Selected) {
        painter->fillRect(option.rect, option.palette.highlight());
    }

    painter->save();

    QTextDocument doc;
    doc.setHtml(itemText);
    QAbstractTextDocumentLayout::PaintContext context;
    doc.setPageSize(option.rect.size());
    painter->translate(option.rect.x(), option.rect.y());
    doc.documentLayout()->draw(painter, context);

    painter->restore();
}
void ExpandingLabel::allTextVisible() {
	QTextDocument *doc = document();
	doc->setTextWidth(width());
	int height = doc->documentLayout()->documentSize().toSize().height();
	setFixedHeight(height);
}
Example #30
0
void QStaticTextPrivate::paintText(const QPointF &topLeftPosition, QPainter *p)
{
    bool preferRichText = textFormat == Qt::RichText
                          || (textFormat == Qt::AutoText && Qt::mightBeRichText(text));

    if (!preferRichText) {
        QTextLayout textLayout;
        textLayout.setText(text);
        textLayout.setFont(font);
        textLayout.setTextOption(textOption);

        qreal leading = QFontMetricsF(font).leading();
        qreal height = -leading;

        textLayout.beginLayout();
        while (1) {
            QTextLine line = textLayout.createLine();
            if (!line.isValid())
                break;

            if (textWidth >= 0.0)
                line.setLineWidth(textWidth);
            height += leading;
            line.setPosition(QPointF(0.0, height));
            height += line.height();
        }
        textLayout.endLayout();

        actualSize = textLayout.boundingRect().size();
        textLayout.draw(p, topLeftPosition);
    } else {
        QTextDocument document;
#ifndef QT_NO_CSSPARSER
        QColor color = p->pen().color();
        document.setDefaultStyleSheet(QString::fromLatin1("body { color: #%1%2%3 }")
                                      .arg(QString::number(color.red(), 16), 2, QLatin1Char('0'))
                                      .arg(QString::number(color.green(), 16), 2, QLatin1Char('0'))
                                      .arg(QString::number(color.blue(), 16), 2, QLatin1Char('0')));
#endif
        document.setDefaultFont(font);
        document.setDocumentMargin(0.0);        
#ifndef QT_NO_TEXTHTMLPARSER
        document.setHtml(text);
#else
        document.setPlainText(text);
#endif
        if (textWidth >= 0.0)
            document.setTextWidth(textWidth);
        else
            document.adjustSize();
        document.setDefaultTextOption(textOption);

        p->save();
        p->translate(topLeftPosition);
        QAbstractTextDocumentLayout::PaintContext ctx;
        ctx.palette.setColor(QPalette::Text, p->pen().color());
        document.documentLayout()->draw(p, ctx);
        p->restore();

        if (textWidth >= 0.0)
            document.adjustSize(); // Find optimal size

        actualSize = document.size();
    }
}