1 /***************************************************************************
2 * Copyright (C) 2008 by Peter Penz <peter.penz@gmx.at> *
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. *
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. *
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>
24 #include <kiconloader.h>
25 #include <kiconeffect.h>
29 #include <QPaintEvent>
34 SelectionToggle::SelectionToggle(QWidget
* parent
) :
35 QAbstractButton(parent
),
41 setFocusPolicy(Qt::NoFocus
);
42 parent
->installEventFilter(this);
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
60 void SelectionToggle::reset()
66 void SelectionToggle::setUrl(const KUrl
& url
)
74 KUrl
SelectionToggle::url() const
79 void SelectionToggle::setVisible(bool visible
)
81 QAbstractButton::setVisible(visible
);
90 bool SelectionToggle::eventFilter(QObject
* obj
, QEvent
* event
)
92 if ((obj
== parent()) && (event
->type() == QEvent::Leave
)) {
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
105 if (m_fadingTimeLine
!= 0) {
106 m_fadingTimeLine
->stop();
109 setToolTip(isChecked() ? i18nc("@info:tooltip", "Deselect Item") :
110 i18nc("@info:tooltip", "Select Item"));
114 void SelectionToggle::leaveEvent(QEvent
* event
)
116 QAbstractButton::leaveEvent(event
);
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
144 KIconEffect iconEffect
;
145 QPixmap activeIcon
= iconEffect
.apply(m_icon
, KIconLoader::Desktop
, KIconLoader::ActiveState
);
146 painter
.drawPixmap(0, 0, activeIcon
);
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
);
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();
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
);
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();
202 void SelectionToggle::stopFading()
204 if (m_fadingTimeLine
!= 0) {
205 m_fadingTimeLine
->stop();
206 delete m_fadingTimeLine
;
207 m_fadingTimeLine
= 0;
212 #include "selectiontoggle.moc"