]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/selectiontoggle.cpp
Use a pointing-hand cursor when hovering items as discussed on http://lists.kde.org...
[dolphin.git] / src / views / selectiontoggle.cpp
1 /***************************************************************************
2 * Copyright (C) 2008 by Peter Penz <peter.penz@gmx.at> *
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 "selectiontoggle.h"
21
22 #include <kglobalsettings.h>
23 #include <kicon.h>
24 #include <kiconloader.h>
25 #include <kiconeffect.h>
26 #include <klocale.h>
27
28 #include <QApplication>
29 #include <QPainter>
30 #include <QPaintEvent>
31 #include <QRect>
32 #include <QTimer>
33 #include <QTimeLine>
34
35 SelectionToggle::SelectionToggle(QWidget* parent) :
36 QAbstractButton(parent),
37 m_isHovered(false),
38 m_leftMouseButtonPressed(false),
39 m_appliedArrowCursor(false),
40 m_fadingValue(0),
41 m_margin(0),
42 m_icon(),
43 m_fadingTimeLine(0)
44 {
45 setFocusPolicy(Qt::NoFocus);
46 parent->installEventFilter(this);
47 resize(sizeHint());
48 setIconOverlay(isChecked());
49 connect(this, SIGNAL(toggled(bool)),
50 this, SLOT(setIconOverlay(bool)));
51 connect(KGlobalSettings::self(), SIGNAL(iconChanged(int)),
52 this, SLOT(refreshIcon()));
53 }
54
55 SelectionToggle::~SelectionToggle()
56 {
57 }
58
59 QSize SelectionToggle::sizeHint() const
60 {
61 return QSize(16, 16);
62 }
63
64 void SelectionToggle::reset()
65 {
66 m_url = KUrl();
67 hide();
68 }
69
70 void SelectionToggle::setUrl(const KUrl& url)
71 {
72 m_url = url;
73 if (!url.isEmpty()) {
74 startFading();
75 }
76 }
77
78 void SelectionToggle::setMargin(int margin)
79 {
80 if (margin != m_margin) {
81 m_margin = margin;
82 update();
83 }
84 }
85
86 int SelectionToggle::margin() const
87 {
88 return m_margin;
89 }
90
91 KUrl SelectionToggle::url() const
92 {
93 return m_url;
94 }
95
96 void SelectionToggle::setVisible(bool visible)
97 {
98 QAbstractButton::setVisible(visible);
99
100 stopFading();
101 if (visible) {
102 startFading();
103 }
104
105 }
106
107 bool SelectionToggle::eventFilter(QObject* obj, QEvent* event)
108 {
109 if (obj == parent()) {
110 switch (event->type()) {
111 case QEvent::Leave:
112 hide();
113 break;
114
115 case QEvent::MouseMove:
116 if (m_leftMouseButtonPressed) {
117 // Don't forward mouse move events to the viewport,
118 // otherwise a rubberband selection will be shown when
119 // clicking on the selection toggle and moving the mouse
120 // above the viewport.
121 return true;
122 }
123 break;
124
125 default:
126 break;
127 }
128 }
129
130 return QAbstractButton::eventFilter(obj, event);
131 }
132
133 void SelectionToggle::enterEvent(QEvent* event)
134 {
135 QAbstractButton::enterEvent(event);
136
137 if (!m_appliedArrowCursor) {
138 QApplication::setOverrideCursor(QCursor(Qt::ArrowCursor));
139 m_appliedArrowCursor = true;
140 }
141
142 // if the mouse cursor is above the selection toggle, display
143 // it immediately without fading timer
144 m_isHovered = true;
145 if (m_fadingTimeLine != 0) {
146 m_fadingTimeLine->stop();
147 }
148 m_fadingValue = 255;
149 setToolTip(isChecked() ? i18nc("@info:tooltip", "Deselect Item") :
150 i18nc("@info:tooltip", "Select Item"));
151 update();
152 }
153
154 void SelectionToggle::leaveEvent(QEvent* event)
155 {
156 QAbstractButton::leaveEvent(event);
157
158 if (m_appliedArrowCursor) {
159 QApplication::restoreOverrideCursor();
160 m_appliedArrowCursor = false;
161 }
162
163 m_isHovered = false;
164 update();
165 }
166
167 void SelectionToggle::mousePressEvent(QMouseEvent* event)
168 {
169 QAbstractButton::mousePressEvent(event);
170 m_leftMouseButtonPressed = (event->buttons() & Qt::LeftButton);
171 }
172
173 void SelectionToggle::mouseReleaseEvent(QMouseEvent* event)
174 {
175 QAbstractButton::mouseReleaseEvent(event);
176 m_leftMouseButtonPressed = (event->buttons() & Qt::LeftButton);
177 }
178
179 void SelectionToggle::resizeEvent(QResizeEvent* event)
180 {
181 QAbstractButton::resizeEvent(event);
182 setIconOverlay(isChecked());
183 }
184
185 void SelectionToggle::paintEvent(QPaintEvent* event)
186 {
187 QPainter painter(this);
188 painter.setClipRect(event->rect());
189
190 // draw the icon overlay
191 const QPoint pos(m_margin, m_margin);
192 if (m_isHovered) {
193 KIconEffect *iconEffect = KIconLoader::global()->iconEffect();
194 QPixmap activeIcon = iconEffect->apply(m_icon, KIconLoader::Desktop, KIconLoader::ActiveState);
195 painter.drawPixmap(pos, activeIcon);
196 } else {
197 if (m_fadingValue < 255) {
198 // apply an alpha mask respecting the fading value to the icon
199 QPixmap icon = m_icon;
200 QPixmap alphaMask(icon.width(), icon.height());
201 const QColor color(m_fadingValue, m_fadingValue, m_fadingValue);
202 alphaMask.fill(color);
203 icon.setAlphaChannel(alphaMask);
204 painter.drawPixmap(pos, icon);
205 } else {
206 // no fading is required
207 painter.drawPixmap(pos, m_icon);
208 }
209 }
210
211 }
212
213 void SelectionToggle::setFadingValue(int value)
214 {
215 m_fadingValue = value;
216 if (m_fadingValue >= 255) {
217 Q_ASSERT(m_fadingTimeLine != 0);
218 m_fadingTimeLine->stop();
219 }
220 update();
221 }
222
223 void SelectionToggle::setIconOverlay(bool checked)
224 {
225 const char* icon = checked ? "list-remove" : "list-add";
226 const int size = qMin(width() - 2 * m_margin, height() - 2 * m_margin);
227 m_icon = KIconLoader::global()->loadIcon(icon,
228 KIconLoader::NoGroup,
229 size);
230 update();
231 }
232
233 void SelectionToggle::refreshIcon()
234 {
235 setIconOverlay(isChecked());
236 }
237
238 void SelectionToggle::startFading()
239 {
240 Q_ASSERT(m_fadingTimeLine == 0);
241
242 const bool animate = KGlobalSettings::graphicEffectsLevel() & KGlobalSettings::SimpleAnimationEffects;
243 const int duration = animate ? 600 : 1;
244
245 m_fadingTimeLine = new QTimeLine(duration, this);
246 connect(m_fadingTimeLine, SIGNAL(frameChanged(int)),
247 this, SLOT(setFadingValue(int)));
248 m_fadingTimeLine->setFrameRange(0, 255);
249 m_fadingTimeLine->start();
250 m_fadingValue = 0;
251 }
252
253 void SelectionToggle::stopFading()
254 {
255 if (m_fadingTimeLine != 0) {
256 m_fadingTimeLine->stop();
257 delete m_fadingTimeLine;
258 m_fadingTimeLine = 0;
259 }
260 m_fadingValue = 0;
261 }
262
263 #include "selectiontoggle.moc"