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