]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistwidget.cpp
Move drawing of textbackground to KItemListWidget
[dolphin.git] / src / kitemviews / kitemlistwidget.cpp
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * Based on the Itemviews NG project from Trolltech Labs: *
5 * http://qt.gitorious.org/qt-labs/itemviews-ng *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
21 ***************************************************************************/
22
23 #include "kitemlistwidget.h"
24
25 #include "kitemlistview.h"
26 #include "kitemmodelbase.h"
27
28 #include <KDebug>
29
30 #include <QPainter>
31 #include <QPropertyAnimation>
32 #include <QStyle>
33 #include <QStyleOption>
34
35 KItemListWidget::KItemListWidget(QGraphicsItem* parent) :
36 QGraphicsWidget(parent, 0),
37 m_index(-1),
38 m_selected(false),
39 m_current(false),
40 m_hovered(false),
41 m_data(),
42 m_visibleRoles(),
43 m_visibleRolesSizes(),
44 m_styleOption(),
45 m_hoverOpacity(0),
46 m_hoverCache(0),
47 m_hoverAnimation(0)
48 {
49 }
50
51 KItemListWidget::~KItemListWidget()
52 {
53 clearCache();
54 }
55
56 void KItemListWidget::setIndex(int index)
57 {
58 if (m_index != index) {
59 if (m_hoverAnimation) {
60 m_hoverAnimation->stop();
61 m_hoverOpacity = 0;
62 }
63 clearCache();
64
65 m_index = index;
66 }
67 }
68
69 int KItemListWidget::index() const
70 {
71 return m_index;
72 }
73
74 void KItemListWidget::setData(const QHash<QByteArray, QVariant>& data,
75 const QSet<QByteArray>& roles)
76 {
77 clearCache();
78 if (roles.isEmpty()) {
79 m_data = data;
80 dataChanged(m_data);
81 } else {
82 foreach (const QByteArray& role, roles) {
83 m_data[role] = data[role];
84 }
85 dataChanged(m_data, roles);
86 }
87 }
88
89 QHash<QByteArray, QVariant> KItemListWidget::data() const
90 {
91 return m_data;
92 }
93
94 void KItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
95 {
96 Q_UNUSED(option);
97
98 painter->setRenderHint(QPainter::Antialiasing);
99
100 const QRect iconBounds = iconBoundingRect().toRect();
101 if (m_selected) {
102 QStyleOptionViewItemV4 viewItemOption;
103 viewItemOption.initFrom(widget);
104 viewItemOption.rect = iconBounds;
105 viewItemOption.state = QStyle::State_Enabled | QStyle::State_Selected | QStyle::State_Item;
106 viewItemOption.viewItemPosition = QStyleOptionViewItemV4::OnlyOne;
107 widget->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &viewItemOption, painter, widget);
108
109 drawTextBackground(painter);
110 }
111
112 if (isCurrent()) {
113 drawFocusIndicator(painter);
114 }
115
116 if (m_hoverOpacity <= 0.0) {
117 return;
118 }
119
120 if (!m_hoverCache) {
121 // Initialize the m_hoverCache pixmap to improve the drawing performance
122 // when fading the hover background
123 m_hoverCache = new QPixmap(iconBounds.size());
124 m_hoverCache->fill(Qt::transparent);
125
126 QPainter pixmapPainter(m_hoverCache);
127
128 QStyleOptionViewItemV4 viewItemOption;
129 viewItemOption.initFrom(widget);
130 viewItemOption.rect = QRect(0, 0, iconBounds.width(), iconBounds.height());
131 viewItemOption.state = QStyle::State_Enabled | QStyle::State_MouseOver | QStyle::State_Item;
132 viewItemOption.viewItemPosition = QStyleOptionViewItemV4::OnlyOne;
133
134 widget->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &viewItemOption, &pixmapPainter, widget);
135 }
136
137 const qreal opacity = painter->opacity();
138 painter->setOpacity(m_hoverOpacity * opacity);
139 painter->drawPixmap(iconBounds.topLeft(), *m_hoverCache);
140 drawTextBackground(painter);
141 painter->setOpacity(opacity);
142 }
143
144 void KItemListWidget::setVisibleRoles(const QHash<QByteArray, int>& roles)
145 {
146 const QHash<QByteArray, int> previousRoles = m_visibleRoles;
147 m_visibleRoles = roles;
148 visibleRolesChanged(roles, previousRoles);
149 }
150
151 QHash<QByteArray, int> KItemListWidget::visibleRoles() const
152 {
153 return m_visibleRoles;
154 }
155
156 void KItemListWidget::setVisibleRolesSizes(const QHash<QByteArray, QSizeF> rolesSizes)
157 {
158 const QHash<QByteArray, QSizeF> previousRolesSizes = m_visibleRolesSizes;
159 m_visibleRolesSizes = rolesSizes;
160 visibleRolesSizesChanged(rolesSizes, previousRolesSizes);
161 }
162
163 QHash<QByteArray, QSizeF> KItemListWidget::visibleRolesSizes() const
164 {
165 return m_visibleRolesSizes;
166 }
167
168 void KItemListWidget::setStyleOption(const KItemListStyleOption& option)
169 {
170 const KItemListStyleOption previous = m_styleOption;
171 clearCache();
172 m_styleOption = option;
173
174 styleOptionChanged(option, previous);
175 }
176
177 const KItemListStyleOption& KItemListWidget::styleOption() const
178 {
179 return m_styleOption;
180 }
181
182 void KItemListWidget::setSelected(bool selected)
183 {
184 if (m_selected != selected) {
185 m_selected = selected;
186 selectedChanged(selected);
187 update();
188 }
189 }
190
191 bool KItemListWidget::isSelected() const
192 {
193 return m_selected;
194 }
195
196 void KItemListWidget::setCurrent(bool current)
197 {
198 if (m_current != current) {
199 m_current = current;
200 currentChanged(current);
201 update();
202 }
203 }
204
205 bool KItemListWidget::isCurrent() const
206 {
207 return m_current;
208 }
209
210 void KItemListWidget::setHovered(bool hovered)
211 {
212 if (hovered == m_hovered) {
213 return;
214 }
215
216 m_hovered = hovered;
217
218 if (!m_hoverAnimation) {
219 m_hoverAnimation = new QPropertyAnimation(this, "hoverOpacity", this);
220 m_hoverAnimation->setDuration(200);
221 }
222 m_hoverAnimation->stop();
223
224 if (hovered) {
225 m_hoverAnimation->setEndValue(1.0);
226 } else {
227 m_hoverAnimation->setEndValue(0.0);
228 }
229
230 m_hoverAnimation->start();
231
232 hoveredChanged(hovered);
233
234 update();
235 }
236
237 bool KItemListWidget::isHovered() const
238 {
239 return m_hovered;
240 }
241
242 bool KItemListWidget::contains(const QPointF& point) const
243 {
244 if (!QGraphicsWidget::contains(point)) {
245 return false;
246 }
247
248 return iconBoundingRect().contains(point) ||
249 textBoundingRect().contains(point) ||
250 expansionToggleRect().contains(point) ||
251 selectionToggleRect().contains(point);
252 }
253
254 QRectF KItemListWidget::selectionToggleRect() const
255 {
256 return QRectF();
257 }
258
259 QRectF KItemListWidget::expansionToggleRect() const
260 {
261 return QRectF();
262 }
263
264 void KItemListWidget::dataChanged(const QHash<QByteArray, QVariant>& current,
265 const QSet<QByteArray>& roles)
266 {
267 Q_UNUSED(current);
268 Q_UNUSED(roles);
269 update();
270 }
271
272 void KItemListWidget::visibleRolesChanged(const QHash<QByteArray, int>& current,
273 const QHash<QByteArray, int>& previous)
274 {
275 Q_UNUSED(current);
276 Q_UNUSED(previous);
277 update();
278 }
279
280 void KItemListWidget::visibleRolesSizesChanged(const QHash<QByteArray, QSizeF>& current,
281 const QHash<QByteArray, QSizeF>& previous)
282 {
283 Q_UNUSED(current);
284 Q_UNUSED(previous);
285 update();
286 }
287
288 void KItemListWidget::styleOptionChanged(const KItemListStyleOption& current,
289 const KItemListStyleOption& previous)
290 {
291 Q_UNUSED(current);
292 Q_UNUSED(previous);
293 update();
294 }
295
296 void KItemListWidget::currentChanged(bool current)
297 {
298 Q_UNUSED(current);
299 }
300
301 void KItemListWidget::selectedChanged(bool selected)
302 {
303 Q_UNUSED(selected);
304 }
305
306 void KItemListWidget::hoveredChanged(bool hovered)
307 {
308 Q_UNUSED(hovered);
309 }
310
311 void KItemListWidget::resizeEvent(QGraphicsSceneResizeEvent* event)
312 {
313 QGraphicsWidget::resizeEvent(event);
314 clearCache();
315 }
316
317 qreal KItemListWidget::hoverOpacity() const
318 {
319 return m_hoverOpacity;
320 }
321
322 void KItemListWidget::setHoverOpacity(qreal opacity)
323 {
324 m_hoverOpacity = opacity;
325 update();
326 }
327
328 void KItemListWidget::clearCache()
329 {
330 delete m_hoverCache;
331 m_hoverCache = 0;
332 }
333
334 void KItemListWidget::drawFocusIndicator(QPainter* painter)
335 {
336 // Ideally style()->drawPrimitive(QStyle::PE_FrameFocusRect...)
337 // should be used, but Oxygen only draws indicators within classes
338 // derived from QAbstractItemView or Q3ListView. As a workaround
339 // the indicator is drawn manually. Code copied from oxygenstyle.cpp
340 // Copyright ( C ) 2009-2010 Hugo Pereira Da Costa <hugo@oxygen-icons.org>
341 // TODO: Clarify with Oxygen maintainers how to proceed with this.
342
343 const KItemListStyleOption& option = styleOption();
344 const QPalette palette = option.palette;
345 const QRect rect = textBoundingRect().toRect().adjusted(0, 0, 0, -1);
346
347 QLinearGradient gradient(rect.bottomLeft(), rect.bottomRight());
348 gradient.setColorAt(0.0, Qt::transparent);
349 gradient.setColorAt(1.0, Qt::transparent);
350 gradient.setColorAt(0.2, palette.color(QPalette::Text));
351 gradient.setColorAt(0.8, palette.color(QPalette::Text));
352
353 painter->setRenderHint(QPainter::Antialiasing, false);
354 painter->setPen(QPen(gradient, 1));
355 painter->drawLine(rect.bottomLeft(), rect.bottomRight());
356 painter->setRenderHint(QPainter::Antialiasing, true);
357 }
358
359 void KItemListWidget::drawTextBackground(QPainter* painter)
360 {
361 const qreal opacity = painter->opacity();
362
363 QRectF textBounds = textBoundingRect();
364 const qreal marginDiff = m_styleOption.margin / 2;
365 textBounds.adjust(marginDiff, marginDiff, -marginDiff, -marginDiff);
366 painter->setOpacity(opacity * 0.1);
367 painter->setPen(Qt::NoPen);
368 painter->setBrush(m_styleOption.palette.text());
369 painter->drawRoundedRect(textBounds, 4, 4);
370
371 painter->setOpacity(opacity);
372 }
373
374 #include "kitemlistwidget.moc"