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