]> cloud.milkyroute.net Git - dolphin.git/blob - src/selectiontoggle.cpp
Disable additional properties per default as discussed with Sebastian TrĂ¼g.
[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
163 // draw the icon overlay
164 if (m_isHovered) {
165 KIconEffect iconEffect;
166 QPixmap activeIcon = iconEffect.apply(m_icon, KIconLoader::Desktop, KIconLoader::ActiveState);
167 painter.drawPixmap(0, 0, activeIcon);
168 } else {
169 if (m_fadingValue < 255) {
170 // apply an alpha mask respecting the fading value to the icon
171 QPixmap icon = m_icon;
172 QPixmap alphaMask(icon.width(), icon.height());
173 const QColor color(m_fadingValue, m_fadingValue, m_fadingValue);
174 alphaMask.fill(color);
175 icon.setAlphaChannel(alphaMask);
176 painter.drawPixmap(0, 0, icon);
177 } else {
178 // no fading is required
179 painter.drawPixmap(0, 0, m_icon);
180 }
181 }
182 }
183
184 void SelectionToggle::setFadingValue(int value)
185 {
186 m_fadingValue = value;
187 if (m_fadingValue >= 255) {
188 Q_ASSERT(m_fadingTimeLine != 0);
189 m_fadingTimeLine->stop();
190 }
191 update();
192 }
193
194 void SelectionToggle::setIconOverlay(bool checked)
195 {
196 const char* icon = checked ? "list-remove" : "list-add";
197 m_icon = KIconLoader::global()->loadIcon(icon,
198 KIconLoader::NoGroup,
199 qMin(width(), height()));
200 update();
201 }
202
203 void SelectionToggle::refreshIcon()
204 {
205 setIconOverlay(isChecked());
206 }
207
208 void SelectionToggle::startFading()
209 {
210 Q_ASSERT(m_fadingTimeLine == 0);
211
212 const bool animate = KGlobalSettings::graphicEffectsLevel() & KGlobalSettings::SimpleAnimationEffects;
213 const int duration = animate ? 600 : 1;
214
215 m_fadingTimeLine = new QTimeLine(duration, this);
216 connect(m_fadingTimeLine, SIGNAL(frameChanged(int)),
217 this, SLOT(setFadingValue(int)));
218 m_fadingTimeLine->setFrameRange(0, 255);
219 m_fadingTimeLine->start();
220 m_fadingValue = 0;
221 }
222
223 void SelectionToggle::stopFading()
224 {
225 if (m_fadingTimeLine != 0) {
226 m_fadingTimeLine->stop();
227 delete m_fadingTimeLine;
228 m_fadingTimeLine = 0;
229 }
230 m_fadingValue = 0;
231 }
232
233 #include "selectiontoggle.moc"