Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / apps / dolphin / src / selectiontoggle.cpp
blobfdae03e2b3b80e89292030a2e7f81fa58d9ffa31
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 ***************************************************************************/
20 #include "selectiontoggle.h"
22 #include <kicon.h>
23 #include <kiconloader.h>
24 #include <kiconeffect.h>
26 #include <QPainter>
27 #include <QPaintEvent>
28 #include <QRect>
29 #include <QTimer>
30 #include <QTimeLine>
32 SelectionToggle::SelectionToggle(QWidget* parent) :
33 QAbstractButton(parent),
34 m_isHovered(false),
35 m_fadingValue(0),
36 m_icon(),
37 m_fadingTimeLine(0)
39 setFocusPolicy(Qt::NoFocus);
40 parent->installEventFilter(this);
41 resize(sizeHint());
42 setIconOverlay(isChecked());
43 connect(this, SIGNAL(toggled(bool)),
44 this, SLOT(setIconOverlay(bool)));
47 SelectionToggle::~SelectionToggle()
51 QSize SelectionToggle::sizeHint() const
53 return QSize(16, 16);
56 void SelectionToggle::reset()
58 m_url = KUrl();
59 hide();
62 void SelectionToggle::setUrl(const KUrl& url)
64 m_url = url;
65 if (!url.isEmpty()) {
66 startFading();
70 KUrl SelectionToggle::url() const
72 return m_url;
75 void SelectionToggle::setVisible(bool visible)
77 QAbstractButton::setVisible(visible);
79 stopFading();
80 if (visible) {
81 startFading();
86 bool SelectionToggle::eventFilter(QObject* obj, QEvent* event)
88 if ((obj == parent()) && (event->type() == QEvent::Leave)) {
89 hide();
91 return QAbstractButton::eventFilter(obj, event);
94 void SelectionToggle::enterEvent(QEvent* event)
96 QAbstractButton::enterEvent(event);
98 // if the mouse cursor is above the selection toggle, display
99 // it immediately without fading timer
100 m_isHovered = true;
101 if (m_fadingTimeLine != 0) {
102 m_fadingTimeLine->stop();
104 m_fadingValue = 255;
105 update();
108 void SelectionToggle::leaveEvent(QEvent* event)
110 QAbstractButton::leaveEvent(event);
111 m_isHovered = false;
112 update();
115 void SelectionToggle::paintEvent(QPaintEvent* event)
117 QPainter painter(this);
118 painter.setClipRect(event->rect());
119 painter.setRenderHint(QPainter::Antialiasing);
121 // draw an alpha blended circle as background
122 const QPalette& palette = parentWidget()->palette();
124 const QBrush& backgroundBrush = palette.brush(QPalette::Normal, QPalette::Window);
125 QColor background = backgroundBrush.color();
126 background.setAlpha(m_fadingValue / 2);
127 painter.setBrush(background);
129 const QBrush& foregroundBrush = palette.brush(QPalette::Normal, QPalette::WindowText);
130 QColor foreground = foregroundBrush.color();
131 foreground.setAlpha(m_fadingValue / 4);
132 painter.setPen(foreground);
134 painter.drawEllipse(0, 0, width(), height());
136 // draw the icon overlay
137 if (m_isHovered) {
138 KIconEffect iconEffect;
139 QPixmap activeIcon = iconEffect.apply(m_icon, KIconLoader::Desktop, KIconLoader::ActiveState);
140 painter.drawPixmap(0, 0, activeIcon);
141 } else {
142 if (m_fadingValue < 255) {
143 // apply an alpha mask respecting the fading value to the icon
144 QPixmap icon = m_icon;
145 QPixmap alphaMask(icon.width(), icon.height());
146 const QColor color(m_fadingValue, m_fadingValue, m_fadingValue);
147 alphaMask.fill(color);
148 icon.setAlphaChannel(alphaMask);
149 painter.drawPixmap(0, 0, icon);
150 } else {
151 // no fading is required
152 painter.drawPixmap(0, 0, m_icon);
157 void SelectionToggle::setFadingValue(int value)
159 m_fadingValue = value;
160 if (m_fadingValue >= 255) {
161 Q_ASSERT(m_fadingTimeLine != 0);
162 m_fadingTimeLine->stop();
164 update();
167 void SelectionToggle::setIconOverlay(bool checked)
169 const char* icon = checked ? "list-remove" : "list-add";
170 m_icon = KIconLoader::global()->loadIcon(icon,
171 KIconLoader::NoGroup,
172 KIconLoader::SizeSmall);
173 update();
176 void SelectionToggle::startFading()
178 Q_ASSERT(m_fadingTimeLine == 0);
180 m_fadingTimeLine = new QTimeLine(1500, this);
181 connect(m_fadingTimeLine, SIGNAL(frameChanged(int)),
182 this, SLOT(setFadingValue(int)));
183 m_fadingTimeLine->setFrameRange(0, 255);
184 m_fadingTimeLine->start();
185 m_fadingValue = 0;
188 void SelectionToggle::stopFading()
190 if (m_fadingTimeLine != 0) {
191 m_fadingTimeLine->stop();
192 delete m_fadingTimeLine;
193 m_fadingTimeLine = 0;
195 m_fadingValue = 0;
198 #include "selectiontoggle.moc"