Settings: unify duplicated code
[lxde/liblxqt.git] / lxqthtmldelegate.cpp
CommitLineData
3e7b0203
PL
1/* BEGIN_COMMON_COPYRIGHT_HEADER
2 * (c)LGPL2+
3 *
4 * LXQt - a lightweight, Qt based, desktop toolset
5 * http://lxqt.org
6 *
7 * Copyright: 2010-2011 Razor team
8 * Authors:
9 * Alexander Sokoloff <sokoloff.a@gmail.com>
10 * Paulo Lieuthier <paulolieuthier@gmail.com>
11 *
12 * This program or library is free software; you can redistribute it
13 * and/or modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
16 *
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General
23 * Public License along with this library; if not, write to the
24 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 * Boston, MA 02110-1301 USA
26 *
27 * END_COMMON_COPYRIGHT_HEADER */
28
29#include "lxqthtmldelegate.h"
30#include <QAbstractTextDocumentLayout>
31#include <QTextDocument>
32
f05ba5af 33using namespace LXQt;
3e7b0203
PL
34
35
36HtmlDelegate::HtmlDelegate(const QSize iconSize, QObject* parent) :
37 QStyledItemDelegate(parent),
38 mIconSize(iconSize)
39{
40}
41
42HtmlDelegate::~HtmlDelegate()
43{
44}
45
46/************************************************
47
48 ************************************************/
49void HtmlDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
50{
51 if (!index.isValid())
52 return;
53
7055162c 54 QStyleOptionViewItem options = option;
3e7b0203 55 initStyleOption(&options, index);
470b2c8e 56 const bool is_right_to_left = Qt::RightToLeft == options.direction;
3e7b0203
PL
57
58 painter->save();
59
60 QTextDocument doc;
61 doc.setHtml(options.text);
62 QIcon icon = options.icon;
63
c24f8b85 64 options.text = QString();
3e7b0203
PL
65 options.icon = QIcon();
66
67 // icon size
68 QSize iconSize = icon.actualSize(mIconSize);
69 QRect iconRect = QRect(8, 8, iconSize.width(), iconSize.height());
470b2c8e
PK
70 if (is_right_to_left)
71 {
72 iconRect.moveLeft(options.rect.left() + options.rect.right() - iconRect.x() - iconRect.width() + 1);
73 }
3e7b0203
PL
74
75 // set doc size
470b2c8e 76 doc.setTextWidth(options.rect.width() - iconRect.width() - 8);
3e7b0203
PL
77
78 options.widget->style()->drawControl(QStyle::CE_ItemViewItem, &options, painter);
79
80 // paint icon
81 painter->translate(options.rect.left(), options.rect.top());
82 icon.paint(painter, iconRect);
83
470b2c8e
PK
84 if (!is_right_to_left)
85 {
86 // shift text right to make icon visible
87 painter->translate(iconRect.right() + 8, 0);
88 }
89 QRect clip(0, 0, options.rect.width() - iconRect.width() - 8, options.rect.height());
3e7b0203
PL
90 painter->setClipRect(clip);
91
92 // set text color to red for selected item
93 QAbstractTextDocumentLayout::PaintContext ctx;
94 if (option.state & QStyle::State_Selected)
95 {
96 QPalette::ColorGroup colorGroup = (option.state & QStyle::State_Active) ? QPalette::Active : QPalette::Inactive;
97 ctx.palette.setColor(QPalette::Text, option.palette.color(colorGroup, QPalette::HighlightedText));
98 }
99
100 ctx.clip = clip;
101 doc.documentLayout()->draw(painter, ctx);
102
103 painter->restore();
104}
105
106
107/************************************************
108
109 ************************************************/
110QSize HtmlDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
111{
7055162c 112 QStyleOptionViewItem options = option;
3e7b0203
PL
113 initStyleOption(&options, index);
114
115 QSize iconSize = options.icon.actualSize(mIconSize);
116 QRect iconRect = QRect(8, 8, iconSize.width(), iconSize.height());
117
118 QTextDocument doc;
119 doc.setHtml(options.text);
120 doc.setTextWidth(options.rect.width() - iconRect.right() - 8);
121 return QSize(options.rect.width(), doc.size().height() + 8);
122}