folder colors

master
Herwig Birke 7 years ago
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

@ -16,6 +16,8 @@
#include "cimage.h"
#include "cimageviewer.h"
#include "cstyleddelegate.h"
#include "ui_cmainwindow.h"
#include <QDebug>
@ -100,6 +102,8 @@ void cMainWindow::initUI()
{
ui->setupUi(this);
ui->m_lpFolderView->setItemDelegate(new cStyledDelegate);
QIcon::setThemeName("TangoMFK");
ui->m_lpFilter->setTitle(tr("Filter"));

@ -11,6 +11,7 @@
#include "common.h"
#include "ccopier.h"
#include "cstyleddelegate.h"
#include <QBuffer>
#include <QDir>
@ -120,8 +121,33 @@ QStandardItem* insertPath(QString szPath, QStandardItem* lpRootItem)
for(;path < szPathList.count();path++)
{
szPath1.append(szPathList[path]);
QStandardItem* lpNewItem = new QStandardItem(szPathList[path]);
switch(path)
{
case 0:
lpNewItem->setData(QVariant::fromValue(true), STYLEDDELEGATE_BOLD);
lpNewItem->setData(QVariant::fromValue(false), STYLEDDELEGATE_ITALIC);
break;
case 1:
lpNewItem->setData(QVariant::fromValue(false), STYLEDDELEGATE_BOLD);
lpNewItem->setData(QVariant::fromValue(false), STYLEDDELEGATE_ITALIC);
break;
case 2:
lpNewItem->setData(QVariant::fromValue(false), STYLEDDELEGATE_BOLD);
lpNewItem->setData(QVariant::fromValue(true), STYLEDDELEGATE_ITALIC);
lpNewItem->setData(QVariant::fromValue(QColor(Qt::gray)), STYLEDDELEGATE_COLOR);
break;
case 3:
lpNewItem->setData(QVariant::fromValue(false), STYLEDDELEGATE_BOLD);
lpNewItem->setData(QVariant::fromValue(false), STYLEDDELEGATE_ITALIC);
lpNewItem->setData(QVariant::fromValue(QColor(Qt::blue)), STYLEDDELEGATE_COLOR);
break;
}
lpCurRoot->appendRow(lpNewItem);
lpNewItem->setData(QVariant::fromValue(szPath1), Qt::UserRole+1);
szPath1.append("/");

@ -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

@ -79,7 +79,9 @@ SOURCES += \
ctoolboxtag.cpp \
cspoiler.cpp \
cfilterpanel.cpp \
cexportdialog.cpp
cexportdialog.cpp \
chtmldelegate.cpp \
cstyleddelegate.cpp
HEADERS += \
cmainwindow.h \
@ -107,7 +109,9 @@ HEADERS += \
ctoolboxtag.h \
cspoiler.h \
cfilterpanel.h \
cexportdialog.h
cexportdialog.h \
chtmldelegate.h \
cstyleddelegate.h
FORMS += \
cmainwindow.ui \

Loading…
Cancel
Save