Updated Russian translation
[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
54 QStyleOptionViewItemV4 options = option;
55 initStyleOption(&options, index);
56
57 painter->save();
58
59 QTextDocument doc;
60 doc.setHtml(options.text);
61 QIcon icon = options.icon;
62
c24f8b85 63 options.text = QString();
3e7b0203
PL
64 options.icon = QIcon();
65
66 // icon size
67 QSize iconSize = icon.actualSize(mIconSize);
68 QRect iconRect = QRect(8, 8, iconSize.width(), iconSize.height());
69
70 // set doc size
71 doc.setTextWidth(options.rect.width() - iconRect.right() - 8);
72
73 options.widget->style()->drawControl(QStyle::CE_ItemViewItem, &options, painter);
74
75 // paint icon
76 painter->translate(options.rect.left(), options.rect.top());
77 icon.paint(painter, iconRect);
78
79 // shift text right to make icon visible
80 painter->translate(iconRect.right() + 8, 0);
81 QRect clip(0, 0, options.rect.width() - iconRect.right() - 8, options.rect.height());
82 painter->setClipRect(clip);
83
84 // set text color to red for selected item
85 QAbstractTextDocumentLayout::PaintContext ctx;
86 if (option.state & QStyle::State_Selected)
87 {
88 QPalette::ColorGroup colorGroup = (option.state & QStyle::State_Active) ? QPalette::Active : QPalette::Inactive;
89 ctx.palette.setColor(QPalette::Text, option.palette.color(colorGroup, QPalette::HighlightedText));
90 }
91
92 ctx.clip = clip;
93 doc.documentLayout()->draw(painter, ctx);
94
95 painter->restore();
96}
97
98
99/************************************************
100
101 ************************************************/
102QSize HtmlDelegate::sizeHint(const QStyleOptionViewItem& option, const QModelIndex& index) const
103{
104 QStyleOptionViewItemV4 options = option;
105 initStyleOption(&options, index);
106
107 QSize iconSize = options.icon.actualSize(mIconSize);
108 QRect iconRect = QRect(8, 8, iconSize.width(), iconSize.height());
109
110 QTextDocument doc;
111 doc.setHtml(options.text);
112 doc.setTextWidth(options.rect.width() - iconRect.right() - 8);
113 return QSize(options.rect.width(), doc.size().height() + 8);
114}