]> cloud.milkyroute.net Git - dolphin.git/blob - src/selectiontoggle.cpp
- Fix crash found while investigating https://bugs.kde.org/show_bug.cgi?id=170927
[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()) && (event->type() == QEvent::Leave)) {
93 hide();
94 }
95 return QAbstractButton::eventFilter(obj, event);
96 }
97
98 void SelectionToggle::enterEvent(QEvent* event)
99 {
100 QAbstractButton::enterEvent(event);
101
102 // if the mouse cursor is above the selection toggle, display
103 // it immediately without fading timer
104 m_isHovered = true;
105 if (m_fadingTimeLine != 0) {
106 m_fadingTimeLine->stop();
107 }
108 m_fadingValue = 255;
109 setToolTip(isChecked() ? i18nc("@info:tooltip", "Deselect Item") :
110 i18nc("@info:tooltip", "Select Item"));
111 update();
112 }
113
114 void SelectionToggle::leaveEvent(QEvent* event)
115 {
116 QAbstractButton::leaveEvent(event);
117 m_isHovered = false;
118 update();
119 }
120
121 void SelectionToggle::paintEvent(QPaintEvent* event)
122 {
123 QPainter painter(this);
124 painter.setClipRect(event->rect());
125 painter.setRenderHint(QPainter::Antialiasing);
126
127 // draw an alpha blended circle as background
128 const QPalette& palette = parentWidget()->palette();
129
130 const QBrush& backgroundBrush = palette.brush(QPalette::Normal, QPalette::Window);
131 QColor background = backgroundBrush.color();
132 background.setAlpha(m_fadingValue / 2);
133 painter.setBrush(background);
134
135 const QBrush& foregroundBrush = palette.brush(QPalette::Normal, QPalette::WindowText);
136 QColor foreground = foregroundBrush.color();
137 foreground.setAlpha(m_fadingValue / 4);
138 painter.setPen(foreground);
139
140 painter.drawEllipse(0, 0, width(), height());
141
142 // draw the icon overlay
143 if (m_isHovered) {
144 KIconEffect iconEffect;
145 QPixmap activeIcon = iconEffect.apply(m_icon, KIconLoader::Desktop, KIconLoader::ActiveState);
146 painter.drawPixmap(0, 0, activeIcon);
147 } else {
148 if (m_fadingValue < 255) {
149 // apply an alpha mask respecting the fading value to the icon
150 QPixmap icon = m_icon;
151 QPixmap alphaMask(icon.width(), icon.height());
152 const QColor color(m_fadingValue, m_fadingValue, m_fadingValue);
153 alphaMask.fill(color);
154 icon.setAlphaChannel(alphaMask);
155 painter.drawPixmap(0, 0, icon);
156 } else {
157 // no fading is required
158 painter.drawPixmap(0, 0, m_icon);
159 }
160 }
161 }
162
163 void SelectionToggle::setFadingValue(int value)
164 {
165 m_fadingValue = value;
166 if (m_fadingValue >= 255) {
167 Q_ASSERT(m_fadingTimeLine != 0);
168 m_fadingTimeLine->stop();
169 }
170 update();
171 }
172
173 void SelectionToggle::setIconOverlay(bool checked)
174 {
175 const char* icon = checked ? "list-remove" : "list-add";
176 m_icon = KIconLoader::global()->loadIcon(icon,
177 KIconLoader::NoGroup,
178 KIconLoader::SizeSmall);
179 update();
180 }
181
182 void SelectionToggle::refreshIcon()
183 {
184 setIconOverlay(isChecked());
185 }
186
187 void SelectionToggle::startFading()
188 {
189 Q_ASSERT(m_fadingTimeLine == 0);
190
191 const bool animate = KGlobalSettings::graphicEffectsLevel() & KGlobalSettings::SimpleAnimationEffects;
192 const int duration = animate ? 600 : 1;
193
194 m_fadingTimeLine = new QTimeLine(duration, this);
195 connect(m_fadingTimeLine, SIGNAL(frameChanged(int)),
196 this, SLOT(setFadingValue(int)));
197 m_fadingTimeLine->setFrameRange(0, 255);
198 m_fadingTimeLine->start();
199 m_fadingValue = 0;
200 }
201
202 void SelectionToggle::stopFading()
203 {
204 if (m_fadingTimeLine != 0) {
205 m_fadingTimeLine->stop();
206 delete m_fadingTimeLine;
207 m_fadingTimeLine = 0;
208 }
209 m_fadingValue = 0;
210 }
211
212 #include "selectiontoggle.moc"