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