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