delay a few things on startup, such as setting the visibility mode, which ensures...
[personal-kdebase.git] / apps / dolphin / src / selectiontoggle.cpp
blobb9b79def01af8f2834936e2e85909ccac59d03c7
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 <kglobalsettings.h>
23 #include <kicon.h>
24 #include <kiconloader.h>
25 #include <kiconeffect.h>
26 #include <klocale.h>
28 #include <QPainter>
29 #include <QPaintEvent>
30 #include <QRect>
31 #include <QTimer>
32 #include <QTimeLine>
34 SelectionToggle::SelectionToggle(QWidget* parent) :
35 QAbstractButton(parent),
36 m_isHovered(false),
37 m_fadingValue(0),
38 m_icon(),
39 m_fadingTimeLine(0)
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()));
51 SelectionToggle::~SelectionToggle()
55 QSize SelectionToggle::sizeHint() const
57 return QSize(16, 16);
60 void SelectionToggle::reset()
62 m_url = KUrl();
63 hide();
66 void SelectionToggle::setUrl(const KUrl& url)
68 m_url = url;
69 if (!url.isEmpty()) {
70 startFading();
74 KUrl SelectionToggle::url() const
76 return m_url;
79 void SelectionToggle::setVisible(bool visible)
81 QAbstractButton::setVisible(visible);
83 stopFading();
84 if (visible) {
85 startFading();
90 bool SelectionToggle::eventFilter(QObject* obj, QEvent* event)
92 if ((obj == parent()) && (event->type() == QEvent::Leave)) {
93 hide();
95 return QAbstractButton::eventFilter(obj, event);
98 void SelectionToggle::enterEvent(QEvent* event)
100 QAbstractButton::enterEvent(event);
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();
108 m_fadingValue = 255;
109 setToolTip(isChecked() ? i18nc("@info:tooltip", "Deselect Item") :
110 i18nc("@info:tooltip", "Select Item"));
111 update();
114 void SelectionToggle::leaveEvent(QEvent* event)
116 QAbstractButton::leaveEvent(event);
117 m_isHovered = false;
118 update();
121 void SelectionToggle::paintEvent(QPaintEvent* event)
123 QPainter painter(this);
124 painter.setClipRect(event->rect());
125 painter.setRenderHint(QPainter::Antialiasing);
127 // draw an alpha blended circle as background
128 const QPalette& palette = parentWidget()->palette();
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);
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);
140 painter.drawEllipse(0, 0, width(), height());
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);
163 void SelectionToggle::setFadingValue(int value)
165 m_fadingValue = value;
166 if (m_fadingValue >= 255) {
167 Q_ASSERT(m_fadingTimeLine != 0);
168 m_fadingTimeLine->stop();
170 update();
173 void SelectionToggle::setIconOverlay(bool checked)
175 const char* icon = checked ? "list-remove" : "list-add";
176 m_icon = KIconLoader::global()->loadIcon(icon,
177 KIconLoader::NoGroup,
178 KIconLoader::SizeSmall);
179 update();
182 void SelectionToggle::refreshIcon()
184 setIconOverlay(isChecked());
187 void SelectionToggle::startFading()
189 Q_ASSERT(m_fadingTimeLine == 0);
191 const bool animate = KGlobalSettings::graphicEffectsLevel() & KGlobalSettings::SimpleAnimationEffects;
192 const int duration = animate ? 600 : 1;
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;
202 void SelectionToggle::stopFading()
204 if (m_fadingTimeLine != 0) {
205 m_fadingTimeLine->stop();
206 delete m_fadingTimeLine;
207 m_fadingTimeLine = 0;
209 m_fadingValue = 0;
212 #include "selectiontoggle.moc"