]>
cloud.milkyroute.net Git - dolphin.git/blob - src/selectiontoggle.cpp
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 parent
->installEventFilter(this);
41 setIconOverlay(isChecked());
42 connect(this, SIGNAL(toggled(bool)),
43 this, SLOT(setIconOverlay(bool)));
46 SelectionToggle::~SelectionToggle()
50 QSize
SelectionToggle::sizeHint() const
55 void SelectionToggle::reset()
61 void SelectionToggle::setUrl(const KUrl
& url
)
69 KUrl
SelectionToggle::url() const
74 void SelectionToggle::setVisible(bool visible
)
76 QAbstractButton::setVisible(visible
);
85 bool SelectionToggle::eventFilter(QObject
* obj
, QEvent
* event
)
87 if ((obj
== parent()) && (event
->type() == QEvent::Leave
)) {
90 return QAbstractButton::eventFilter(obj
, event
);
93 void SelectionToggle::enterEvent(QEvent
* event
)
95 QAbstractButton::enterEvent(event
);
97 // if the mouse cursor is above the selection toggle, display
98 // it immediately without fading timer
100 if (m_fadingTimeLine
!= 0) {
101 m_fadingTimeLine
->stop();
107 void SelectionToggle::leaveEvent(QEvent
* event
)
109 QAbstractButton::leaveEvent(event
);
114 void SelectionToggle::paintEvent(QPaintEvent
* event
)
116 QPainter
painter(this);
117 painter
.setClipRect(event
->rect());
118 painter
.setRenderHint(QPainter::Antialiasing
);
120 // draw an alpha blended circle as background
121 const QPalette
& palette
= parentWidget()->palette();
123 const QBrush
& backgroundBrush
= palette
.brush(QPalette::Normal
, QPalette::Window
);
124 QColor background
= backgroundBrush
.color();
125 background
.setAlpha(m_fadingValue
/ 2);
126 painter
.setBrush(background
);
128 const QBrush
& foregroundBrush
= palette
.brush(QPalette::Normal
, QPalette::WindowText
);
129 QColor foreground
= foregroundBrush
.color();
130 foreground
.setAlpha(m_fadingValue
/ 4);
131 painter
.setPen(foreground
);
133 painter
.drawEllipse(0, 0, width(), height());
135 // draw the icon overlay
137 KIconEffect iconEffect
;
138 QPixmap activeIcon
= iconEffect
.apply(m_icon
, KIconLoader::Desktop
, KIconLoader::ActiveState
);
139 painter
.drawPixmap(0, 0, activeIcon
);
141 if (m_fadingValue
< 255) {
142 // apply an alpha mask respecting the fading value to the icon
143 QPixmap icon
= m_icon
;
144 QPixmap
alphaMask(icon
.width(), icon
.height());
145 const QColor
color(m_fadingValue
, m_fadingValue
, m_fadingValue
);
146 alphaMask
.fill(color
);
147 icon
.setAlphaChannel(alphaMask
);
148 painter
.drawPixmap(0, 0, icon
);
150 // no fading is required
151 painter
.drawPixmap(0, 0, m_icon
);
156 void SelectionToggle::setFadingValue(int value
)
158 m_fadingValue
= value
;
159 if (m_fadingValue
>= 255) {
160 Q_ASSERT(m_fadingTimeLine
!= 0);
161 m_fadingTimeLine
->stop();
166 void SelectionToggle::setIconOverlay(bool checked
)
168 const char* icon
= checked
? "list-remove" : "list-add";
169 m_icon
= KIconLoader::global()->loadIcon(icon
,
170 KIconLoader::NoGroup
,
171 KIconLoader::SizeSmall
);
175 void SelectionToggle::startFading()
177 Q_ASSERT(m_fadingTimeLine
== 0);
179 m_fadingTimeLine
= new QTimeLine(1500, this);
180 connect(m_fadingTimeLine
, SIGNAL(frameChanged(int)),
181 this, SLOT(setFadingValue(int)));
182 m_fadingTimeLine
->setFrameRange(0, 255);
183 m_fadingTimeLine
->start();
187 void SelectionToggle::stopFading()
189 if (m_fadingTimeLine
!= 0) {
190 m_fadingTimeLine
->stop();
191 delete m_fadingTimeLine
;
192 m_fadingTimeLine
= 0;
197 #include "selectiontoggle.moc"