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