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