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"
23 #include <kiconloader.h>
24 #include <kiconeffect.h>
27 #include <QPaintEvent>
32 SelectionToggle::SelectionToggle(QWidget
* parent
) :
33 QAbstractButton(parent
),
39 setFocusPolicy(Qt::NoFocus
);
40 parent
->installEventFilter(this);
42 setIconOverlay(isChecked());
43 connect(this, SIGNAL(toggled(bool)),
44 this, SLOT(setIconOverlay(bool)));
47 SelectionToggle::~SelectionToggle()
51 QSize
SelectionToggle::sizeHint() const
56 void SelectionToggle::reset()
62 void SelectionToggle::setUrl(const KUrl
& url
)
70 KUrl
SelectionToggle::url() const
75 void SelectionToggle::setVisible(bool visible
)
77 QAbstractButton::setVisible(visible
);
86 bool SelectionToggle::eventFilter(QObject
* obj
, QEvent
* event
)
88 if ((obj
== parent()) && (event
->type() == QEvent::Leave
)) {
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
101 if (m_fadingTimeLine
!= 0) {
102 m_fadingTimeLine
->stop();
108 void SelectionToggle::leaveEvent(QEvent
* event
)
110 QAbstractButton::leaveEvent(event
);
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
138 KIconEffect iconEffect
;
139 QPixmap activeIcon
= iconEffect
.apply(m_icon
, KIconLoader::Desktop
, KIconLoader::ActiveState
);
140 painter
.drawPixmap(0, 0, activeIcon
);
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
);
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();
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
);
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();
188 void SelectionToggle::stopFading()
190 if (m_fadingTimeLine
!= 0) {
191 m_fadingTimeLine
->stop();
192 delete m_fadingTimeLine
;
193 m_fadingTimeLine
= 0;
198 #include "selectiontoggle.moc"