]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kitemlistwidget.cpp
Improve group-header layout
[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_alternatingBackgroundColors(false),
42 m_data(),
43 m_visibleRoles(),
44 m_visibleRolesSizes(),
45 m_styleOption(),
46 m_hoverOpacity(0),
47 m_hoverCache(0),
48 m_hoverAnimation(0)
49 {
50 }
51
52 KItemListWidget::~KItemListWidget()
53 {
54 clearHoverCache();
55 }
56
57 void KItemListWidget::setIndex(int index)
58 {
59 if (m_index != index) {
60 if (m_hoverAnimation) {
61 m_hoverAnimation->stop();
62 m_hoverOpacity = 0;
63 }
64 clearHoverCache();
65
66 m_index = index;
67 }
68 }
69
70 int KItemListWidget::index() const
71 {
72 return m_index;
73 }
74
75 void KItemListWidget::setData(const QHash<QByteArray, QVariant>& data,
76 const QSet<QByteArray>& roles)
77 {
78 clearHoverCache();
79 if (roles.isEmpty()) {
80 m_data = data;
81 dataChanged(m_data);
82 } else {
83 foreach (const QByteArray& role, roles) {
84 m_data[role] = data[role];
85 }
86 dataChanged(m_data, roles);
87 }
88 }
89
90 QHash<QByteArray, QVariant> KItemListWidget::data() const
91 {
92 return m_data;
93 }
94
95 void KItemListWidget::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
96 {
97 Q_UNUSED(option);
98
99 painter->setRenderHint(QPainter::Antialiasing);
100
101 if (m_alternatingBackgroundColors && (m_index & 0x1)) {
102 const QColor backgroundColor = m_styleOption.palette.color(QPalette::AlternateBase);
103 const QRectF backgroundRect(0, 0, size().width(), size().height());
104 painter->fillRect(backgroundRect, backgroundColor);
105 }
106
107 const QRect iconBounds = iconRect().toRect();
108 if (m_selected) {
109 QStyleOptionViewItemV4 viewItemOption;
110 viewItemOption.initFrom(widget);
111 viewItemOption.rect = iconBounds;
112 viewItemOption.state = QStyle::State_Enabled | QStyle::State_Selected | QStyle::State_Item;
113 viewItemOption.viewItemPosition = QStyleOptionViewItemV4::OnlyOne;
114 widget->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &viewItemOption, painter, widget);
115
116 drawTextBackground(painter);
117 }
118
119 if (isCurrent()) {
120 QStyleOptionViewItemV4 viewItemOption;
121 viewItemOption.initFrom(widget);
122 viewItemOption.rect = textRect().toRect();
123 viewItemOption.state = QStyle::State_Enabled | QStyle::State_Item;
124 viewItemOption.viewItemPosition = QStyleOptionViewItemV4::OnlyOne;
125 style()->drawPrimitive(QStyle::PE_FrameFocusRect, &viewItemOption, painter, widget);
126 }
127
128 if (m_hoverOpacity <= 0.0) {
129 return;
130 }
131
132 if (!m_hoverCache) {
133 // Initialize the m_hoverCache pixmap to improve the drawing performance
134 // when fading the hover background
135 m_hoverCache = new QPixmap(iconBounds.size());
136 m_hoverCache->fill(Qt::transparent);
137
138 QPainter pixmapPainter(m_hoverCache);
139
140 QStyleOptionViewItemV4 viewItemOption;
141 viewItemOption.initFrom(widget);
142 viewItemOption.rect = QRect(0, 0, iconBounds.width(), iconBounds.height());
143 viewItemOption.state = QStyle::State_Enabled | QStyle::State_MouseOver | QStyle::State_Item;
144 viewItemOption.viewItemPosition = QStyleOptionViewItemV4::OnlyOne;
145
146 widget->style()->drawPrimitive(QStyle::PE_PanelItemViewItem, &viewItemOption, &pixmapPainter, widget);
147 }
148
149 const qreal opacity = painter->opacity();
150 painter->setOpacity(m_hoverOpacity * opacity);
151 painter->drawPixmap(iconBounds.topLeft(), *m_hoverCache);
152 drawTextBackground(painter);
153 painter->setOpacity(opacity);
154 }
155
156 void KItemListWidget::setVisibleRoles(const QList<QByteArray>& roles)
157 {
158 const QList<QByteArray> previousRoles = m_visibleRoles;
159 m_visibleRoles = roles;
160 visibleRolesChanged(roles, previousRoles);
161 }
162
163 QList<QByteArray> KItemListWidget::visibleRoles() const
164 {
165 return m_visibleRoles;
166 }
167
168 void KItemListWidget::setVisibleRolesSizes(const QHash<QByteArray, QSizeF> rolesSizes)
169 {
170 const QHash<QByteArray, QSizeF> previousRolesSizes = m_visibleRolesSizes;
171 m_visibleRolesSizes = rolesSizes;
172 visibleRolesSizesChanged(rolesSizes, previousRolesSizes);
173 }
174
175 QHash<QByteArray, QSizeF> KItemListWidget::visibleRolesSizes() const
176 {
177 return m_visibleRolesSizes;
178 }
179
180 void KItemListWidget::setStyleOption(const KItemListStyleOption& option)
181 {
182 const KItemListStyleOption previous = m_styleOption;
183 clearHoverCache();
184 m_styleOption = option;
185
186 styleOptionChanged(option, previous);
187 }
188
189 const KItemListStyleOption& KItemListWidget::styleOption() const
190 {
191 return m_styleOption;
192 }
193
194 void KItemListWidget::setSelected(bool selected)
195 {
196 if (m_selected != selected) {
197 m_selected = selected;
198 selectedChanged(selected);
199 update();
200 }
201 }
202
203 bool KItemListWidget::isSelected() const
204 {
205 return m_selected;
206 }
207
208 void KItemListWidget::setCurrent(bool current)
209 {
210 if (m_current != current) {
211 m_current = current;
212 currentChanged(current);
213 update();
214 }
215 }
216
217 bool KItemListWidget::isCurrent() const
218 {
219 return m_current;
220 }
221
222 void KItemListWidget::setHovered(bool hovered)
223 {
224 if (hovered == m_hovered) {
225 return;
226 }
227
228 m_hovered = hovered;
229
230 if (!m_hoverAnimation) {
231 m_hoverAnimation = new QPropertyAnimation(this, "hoverOpacity", this);
232 m_hoverAnimation->setDuration(200);
233 }
234 m_hoverAnimation->stop();
235
236 if (hovered) {
237 m_hoverAnimation->setEndValue(1.0);
238 } else {
239 m_hoverAnimation->setEndValue(0.0);
240 }
241
242 m_hoverAnimation->start();
243
244 hoveredChanged(hovered);
245
246 update();
247 }
248
249 bool KItemListWidget::isHovered() const
250 {
251 return m_hovered;
252 }
253
254 void KItemListWidget::setAlternatingBackgroundColors(bool enable)
255 {
256 if (m_alternatingBackgroundColors != enable) {
257 m_alternatingBackgroundColors = enable;
258 alternatingBackgroundColorsChanged(enable);
259 update();
260 }
261 }
262
263 bool KItemListWidget::alternatingBackgroundColors() const
264 {
265 return m_alternatingBackgroundColors;
266 }
267
268 bool KItemListWidget::contains(const QPointF& point) const
269 {
270 if (!QGraphicsWidget::contains(point)) {
271 return false;
272 }
273
274 return iconRect().contains(point) ||
275 textRect().contains(point) ||
276 expansionToggleRect().contains(point) ||
277 selectionToggleRect().contains(point);
278 }
279
280 QRectF KItemListWidget::selectionToggleRect() const
281 {
282 return QRectF();
283 }
284
285 QRectF KItemListWidget::expansionToggleRect() const
286 {
287 return QRectF();
288 }
289
290 void KItemListWidget::dataChanged(const QHash<QByteArray, QVariant>& current,
291 const QSet<QByteArray>& roles)
292 {
293 Q_UNUSED(current);
294 Q_UNUSED(roles);
295 update();
296 }
297
298 void KItemListWidget::visibleRolesChanged(const QList<QByteArray>& current,
299 const QList<QByteArray>& previous)
300 {
301 Q_UNUSED(current);
302 Q_UNUSED(previous);
303 update();
304 }
305
306 void KItemListWidget::visibleRolesSizesChanged(const QHash<QByteArray, QSizeF>& current,
307 const QHash<QByteArray, QSizeF>& previous)
308 {
309 Q_UNUSED(current);
310 Q_UNUSED(previous);
311 update();
312 }
313
314 void KItemListWidget::styleOptionChanged(const KItemListStyleOption& current,
315 const KItemListStyleOption& previous)
316 {
317 Q_UNUSED(current);
318 Q_UNUSED(previous);
319 update();
320 }
321
322 void KItemListWidget::currentChanged(bool current)
323 {
324 Q_UNUSED(current);
325 }
326
327 void KItemListWidget::selectedChanged(bool selected)
328 {
329 Q_UNUSED(selected);
330 }
331
332 void KItemListWidget::hoveredChanged(bool hovered)
333 {
334 Q_UNUSED(hovered);
335 }
336
337 void KItemListWidget::alternatingBackgroundColorsChanged(bool enabled)
338 {
339 Q_UNUSED(enabled);
340 }
341
342 void KItemListWidget::resizeEvent(QGraphicsSceneResizeEvent* event)
343 {
344 QGraphicsWidget::resizeEvent(event);
345 clearHoverCache();
346 }
347
348 qreal KItemListWidget::hoverOpacity() const
349 {
350 return m_hoverOpacity;
351 }
352
353 void KItemListWidget::setHoverOpacity(qreal opacity)
354 {
355 m_hoverOpacity = opacity;
356 update();
357 }
358
359 void KItemListWidget::clearHoverCache()
360 {
361 delete m_hoverCache;
362 m_hoverCache = 0;
363 }
364
365 void KItemListWidget::drawTextBackground(QPainter* painter)
366 {
367 const qreal opacity = painter->opacity();
368
369 QRectF textBounds = textRect();
370 const qreal marginDiff = m_styleOption.margin / 2;
371 textBounds.adjust(marginDiff, marginDiff, -marginDiff, -marginDiff);
372 painter->setOpacity(opacity * 0.1);
373 painter->setPen(Qt::NoPen);
374 painter->setBrush(m_styleOption.palette.text());
375 painter->drawRoundedRect(textBounds, 4, 4);
376
377 painter->setOpacity(opacity);
378 }
379
380 #include "kitemlistwidget.moc"