1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- /*
- mephi-tasks — a client to NRNU MEPhI Redmine server
- Copyright (C) 2015 Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
- */
- #include "htmldelegate.h"
- #include <QPainter>
- #include <QTextDocument>
- #include <QAbstractTextDocumentLayout>
- HTMLDelegate::HTMLDelegate()
- {
- }
- /*
- * The next code is an adapted copy of code from:
- * http://stackoverflow.com/questions/1956542/how-to-make-item-view-render-rich-html-text-in-qt
- *
- * Authors (in order of contribution decrease):
- * * serge-gubenko,
- * * satanas,
- * * Dmitry Yu Okunev <dyokunev@ut.mephi.ru>.
- *
- * The code is licensed by "CC BY-SA 3.0 with attribution required" according
- * to "stackoverflow.com"'s footer.
- *
- */
- 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();
- }
- QSize HTMLDelegate::sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
- {
- QStyleOptionViewItemV4 options = option;
- initStyleOption ( &options, index );
- QTextDocument doc;
- doc.setHtml ( options.text );
- doc.setTextWidth ( options.rect.width() );
- return QSize ( doc.idealWidth(), doc.size().height() );
- }
|