]>
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"
22 #include <kglobalsettings.h>
24 #include <kiconloader.h>
25 #include <kiconeffect.h>
28 #include <QPaintEvent>
33 SelectionToggle::SelectionToggle(QWidget
* parent
) :
34 QAbstractButton(parent
),
40 setFocusPolicy(Qt::NoFocus
);
41 parent
->installEventFilter(this);
43 setIconOverlay(isChecked());
44 connect(this, SIGNAL(toggled(bool)),
45 this, SLOT(setIconOverlay(bool)));
48 SelectionToggle::~SelectionToggle()
52 QSize
SelectionToggle::sizeHint() const
57 void SelectionToggle::reset()
63 void SelectionToggle::setUrl(const KUrl
& url
)
71 KUrl
SelectionToggle::url() const
76 void SelectionToggle::setVisible(bool visible
)
78 QAbstractButton::setVisible(visible
);
87 bool SelectionToggle::eventFilter(QObject
* obj
, QEvent
* event
)
89 if ((obj
== parent()) && (event
->type() == QEvent::Leave
)) {
92 return QAbstractButton::eventFilter(obj
, event
);
95 void SelectionToggle::enterEvent(QEvent
* event
)
97 QAbstractButton::enterEvent(event
);
99 // if the mouse cursor is above the selection toggle, display
100 // it immediately without fading timer
102 if (m_fadingTimeLine
!= 0) {
103 m_fadingTimeLine
->stop();
109 void SelectionToggle::leaveEvent(QEvent
* event
)
111 QAbstractButton::leaveEvent(event
);
116 void SelectionToggle::paintEvent(QPaintEvent
* event
)
118 QPainter
painter(this);
119 painter
.setClipRect(event
->rect());
120 painter
.setRenderHint(QPainter::Antialiasing
);
122 // draw an alpha blended circle as background
123 const QPalette
& palette
= parentWidget()->palette();
125 const QBrush
& backgroundBrush
= palette
.brush(QPalette::Normal
, QPalette::Window
);
126 QColor background
= backgroundBrush
.color();
127 background
.setAlpha(m_fadingValue
/ 2);
128 painter
.setBrush(background
);
130 const QBrush
& foregroundBrush
= palette
.brush(QPalette::Normal
, QPalette::WindowText
);
131 QColor foreground
= foregroundBrush
.color();
132 foreground
.setAlpha(m_fadingValue
/ 4);
133 painter
.setPen(foreground
);
135 painter
.drawEllipse(0, 0, width(), height());
137 // draw the icon overlay
139 KIconEffect iconEffect
;
140 QPixmap activeIcon
= iconEffect
.apply(m_icon
, KIconLoader::Desktop
, KIconLoader::ActiveState
);
141 painter
.drawPixmap(0, 0, activeIcon
);
143 if (m_fadingValue
< 255) {
144 // apply an alpha mask respecting the fading value to the icon
145 QPixmap icon
= m_icon
;
146 QPixmap
alphaMask(icon
.width(), icon
.height());
147 const QColor
color(m_fadingValue
, m_fadingValue
, m_fadingValue
);
148 alphaMask
.fill(color
);
149 icon
.setAlphaChannel(alphaMask
);
150 painter
.drawPixmap(0, 0, icon
);
152 // no fading is required
153 painter
.drawPixmap(0, 0, m_icon
);
158 void SelectionToggle::setFadingValue(int value
)
160 m_fadingValue
= value
;
161 if (m_fadingValue
>= 255) {
162 Q_ASSERT(m_fadingTimeLine
!= 0);
163 m_fadingTimeLine
->stop();
168 void SelectionToggle::setIconOverlay(bool checked
)
170 const char* icon
= checked
? "list-remove" : "list-add";
171 m_icon
= KIconLoader::global()->loadIcon(icon
,
172 KIconLoader::NoGroup
,
173 KIconLoader::SizeSmall
);
177 void SelectionToggle::startFading()
179 Q_ASSERT(m_fadingTimeLine
== 0);
181 const bool animate
= KGlobalSettings::graphicEffectsLevel() & KGlobalSettings::SimpleAnimationEffects
;
182 const int duration
= animate
? 1500 : 1;
184 m_fadingTimeLine
= new QTimeLine(duration
, this);
185 connect(m_fadingTimeLine
, SIGNAL(frameChanged(int)),
186 this, SLOT(setFadingValue(int)));
187 m_fadingTimeLine
->setFrameRange(0, 255);
188 m_fadingTimeLine
->start();
192 void SelectionToggle::stopFading()
194 if (m_fadingTimeLine
!= 0) {
195 m_fadingTimeLine
->stop();
196 delete m_fadingTimeLine
;
197 m_fadingTimeLine
= 0;
202 #include "selectiontoggle.moc"