]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/private/kitemlistselectiontoggle.cpp
Merge remote-tracking branch 'origin/KDE/4.11' into KDE/4.12
[dolphin.git] / src / kitemviews / private / kitemlistselectiontoggle.cpp
1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include "kitemlistselectiontoggle.h"
21
22 #include <KIconEffect>
23 #include <KIconLoader>
24 #include <QPainter>
25
26 #include <KDebug>
27
28 KItemListSelectionToggle::KItemListSelectionToggle(QGraphicsItem* parent) :
29 QGraphicsWidget(parent, 0),
30 m_checked(false),
31 m_hovered(false)
32 {
33 setAcceptHoverEvents(true);
34 }
35
36 KItemListSelectionToggle::~KItemListSelectionToggle()
37 {
38 }
39
40 void KItemListSelectionToggle::setChecked(bool checked)
41 {
42 if (m_checked != checked) {
43 m_checked = checked;
44 m_pixmap = QPixmap();
45 update();
46 }
47 }
48
49 bool KItemListSelectionToggle::isChecked() const
50 {
51 return m_checked;
52 }
53
54 void KItemListSelectionToggle::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
55 {
56 Q_UNUSED(option);
57 Q_UNUSED(widget);
58
59 if (m_pixmap.isNull()) {
60 updatePixmap();
61 }
62
63 const qreal x = (size().width() - qreal(m_pixmap.width())) / 2;
64 const qreal y = (size().height() - qreal(m_pixmap.height())) / 2;
65 painter->drawPixmap(x, y, m_pixmap);
66 }
67
68 void KItemListSelectionToggle::hoverEnterEvent(QGraphicsSceneHoverEvent* event)
69 {
70 QGraphicsWidget::hoverEnterEvent(event);
71 m_hovered = true;
72 m_pixmap = QPixmap();
73 }
74
75 void KItemListSelectionToggle::hoverLeaveEvent(QGraphicsSceneHoverEvent* event)
76 {
77 QGraphicsWidget::hoverLeaveEvent(event);
78 m_hovered = false;
79 m_pixmap = QPixmap();
80 }
81
82 void KItemListSelectionToggle::resizeEvent(QGraphicsSceneResizeEvent* event)
83 {
84 QGraphicsWidget::resizeEvent(event);
85
86 if (!m_pixmap.isNull()) {
87 const int pixmapSize = m_pixmap.size().width(); // Pixmap width is always equal pixmap height
88
89 if (pixmapSize != iconSize()) {
90 // If the required icon size is different from the actual pixmap size,
91 // overwrite the m_pixmap with an empty pixmap and reload the new
92 // icon on next re-painting.
93 m_pixmap = QPixmap();
94 }
95 }
96 }
97
98 void KItemListSelectionToggle::updatePixmap()
99 {
100 const char* icon = m_checked ? "list-remove" : "list-add";
101 m_pixmap = KIconLoader::global()->loadIcon(QLatin1String(icon), KIconLoader::NoGroup, iconSize());
102
103 if (m_hovered) {
104 KIconLoader::global()->iconEffect()->apply(m_pixmap, KIconLoader::Desktop, KIconLoader::ActiveState);
105 }
106 }
107
108 int KItemListSelectionToggle::iconSize() const
109 {
110 const int iconSize = qMin(size().width(), size().height());
111
112 if (iconSize < KIconLoader::SizeSmallMedium) {
113 return KIconLoader::SizeSmall;
114 } else if (iconSize < KIconLoader::SizeMedium) {
115 return KIconLoader::SizeSmallMedium;
116 } else if (iconSize < KIconLoader::SizeLarge) {
117 return KIconLoader::SizeMedium;
118 } else if (iconSize < KIconLoader::SizeHuge) {
119 return KIconLoader::SizeLarge;
120 } else if (iconSize < KIconLoader::SizeEnormous) {
121 return KIconLoader::SizeHuge;
122 }
123
124 return iconSize;
125 }
126
127 #include "kitemlistselectiontoggle.moc"