folder colors
parent
e1fc0648ce
commit
5aa22fbfdd
@ -0,0 +1,47 @@
|
||||
#include "chtmldelegate.h"
|
||||
|
||||
#include <QPainter>
|
||||
#include <QTextDocument>
|
||||
#include <QAbstractTextDocumentLayout>
|
||||
|
||||
|
||||
void cHTMLDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
|
||||
{
|
||||
QStyleOptionViewItem 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 cHTMLDelegate::sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const
|
||||
{
|
||||
QStyleOptionViewItem options = option;
|
||||
initStyleOption(&options, index);
|
||||
|
||||
QTextDocument doc;
|
||||
doc.setHtml(options.text);
|
||||
doc.setTextWidth(options.rect.width());
|
||||
return QSize(static_cast<int>(doc.idealWidth()), static_cast<int>(doc.size().height()));
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
#ifndef CHTMLDELEGATE_H
|
||||
#define CHTMLDELEGATE_H
|
||||
|
||||
|
||||
#include <QStyledItemDelegate>
|
||||
|
||||
|
||||
class cHTMLDelegate : public QStyledItemDelegate
|
||||
{
|
||||
protected:
|
||||
void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const;
|
||||
QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const;
|
||||
};
|
||||
|
||||
#endif // CHTMLDELEGATE_H
|
||||
@ -0,0 +1,36 @@
|
||||
#include "cstyleddelegate.h"
|
||||
|
||||
#include <QPainter>
|
||||
#include <QTextDocument>
|
||||
#include <QAbstractTextDocumentLayout>
|
||||
|
||||
#include <QDebug>
|
||||
|
||||
|
||||
void cStyledDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
|
||||
{
|
||||
QStyleOptionViewItem options = option;
|
||||
initStyleOption(&options, index);
|
||||
|
||||
bool bBold = index.data(STYLEDDELEGATE_BOLD).toBool();
|
||||
bool bItalic = index.data(STYLEDDELEGATE_ITALIC).toBool();
|
||||
QColor color = index.data(STYLEDDELEGATE_COLOR).value<QColor>();
|
||||
|
||||
options.font.setBold(bBold);
|
||||
options.font.setItalic(bItalic);
|
||||
if(color.isValid())
|
||||
options.palette.setColor(QPalette::Text, color);
|
||||
|
||||
QStyledItemDelegate::paint(painter, options, index);
|
||||
}
|
||||
|
||||
//QSize cStyledDelegate::sizeHint(const QStyleOptionViewItem & option, const QModelIndex & index) const
|
||||
//{
|
||||
// QStyleOptionViewItem options = option;
|
||||
// initStyleOption(&options, index);
|
||||
|
||||
// QTextDocument doc;
|
||||
// doc.setHtml(options.text);
|
||||
// doc.setTextWidth(options.rect.width());
|
||||
// return QSize(static_cast<int>(doc.idealWidth()), static_cast<int>(doc.size().height()));
|
||||
//}
|
||||
@ -0,0 +1,20 @@
|
||||
#ifndef CSTYLEDDELEGATE_H
|
||||
#define CSTYLEDDELEGATE_H
|
||||
|
||||
|
||||
#include <QStyledItemDelegate>
|
||||
|
||||
|
||||
#define STYLEDDELEGATE_BOLD (Qt::UserRole+10)
|
||||
#define STYLEDDELEGATE_ITALIC (Qt::UserRole+11)
|
||||
#define STYLEDDELEGATE_COLOR (Qt::UserRole+12)
|
||||
|
||||
|
||||
class cStyledDelegate : public QStyledItemDelegate
|
||||
{
|
||||
protected:
|
||||
void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const;
|
||||
// QSize sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const;
|
||||
};
|
||||
|
||||
#endif // CSTYLEDDELEGATE_H
|
||||
Loading…
Reference in New Issue