]>
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>
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)));
49 SelectionToggle::~SelectionToggle()
53 QSize
SelectionToggle::sizeHint() const
58 void SelectionToggle::reset()
64 void SelectionToggle::setUrl(const KUrl
& url
)
72 KUrl
SelectionToggle::url() const
77 void SelectionToggle::setVisible(bool visible
)
79 QAbstractButton::setVisible(visible
);
88 bool SelectionToggle::eventFilter(QObject
* obj
, QEvent
* event
)
90 if ((obj
== parent()) && (event
->type() == QEvent::Leave
)) {
93 return QAbstractButton::eventFilter(obj
, event
);
96 void SelectionToggle::enterEvent(QEvent
* event
)
98 QAbstractButton::enterEvent(event
);
100 // if the mouse cursor is above the selection toggle, display
101 // it immediately without fading timer
103 if (m_fadingTimeLine
!= 0) {
104 m_fadingTimeLine
->stop();
107 setToolTip(isChecked() ? i18nc("@info:tooltip", "Deselect Item") :
108 i18nc("@info:tooltip", "Select Item"));
112 void SelectionToggle::leaveEvent(QEvent
* event
)
114 QAbstractButton::leaveEvent(event
);
119 void SelectionToggle::paintEvent(QPaintEvent
* event
)
121 QPainter
painter(this);
122 painter
.setClipRect(event
->rect());
123 painter
.setRenderHint(QPainter::Antialiasing
);
125 // draw an alpha blended circle as background
126 const QPalette
& palette
= parentWidget()->palette();
128 const QBrush
& backgroundBrush
= palette
.brush(QPalette::Normal
, QPalette::Window
);
129 QColor background
= backgroundBrush
.color();
130 background
.setAlpha(m_fadingValue
/ 2);
131 painter
.setBrush(background
);
133 const QBrush
& foregroundBrush
= palette
.brush(QPalette::Normal
, QPalette::WindowText
);
134 QColor foreground
= foregroundBrush
.color();
135 foreground
.setAlpha(m_fadingValue
/ 4);
136 painter
.setPen(foreground
);
138 painter
.drawEllipse(0, 0, width(), height());
140 // draw the icon overlay
142 KIconEffect iconEffect
;
143 QPixmap activeIcon
= iconEffect
.apply(m_icon
, KIconLoader::Desktop
, KIconLoader::ActiveState
);
144 painter
.drawPixmap(0, 0, activeIcon
);
146 if (m_fadingValue
< 255) {
147 // apply an alpha mask respecting the fading value to the icon
148 QPixmap icon
= m_icon
;
149 QPixmap
alphaMask(icon
.width(), icon
.height());
150 const QColor
color(m_fadingValue
, m_fadingValue
, m_fadingValue
);
151 alphaMask
.fill(color
);
152 icon
.setAlphaChannel(alphaMask
);
153 painter
.drawPixmap(0, 0, icon
);
155 // no fading is required
156 painter
.drawPixmap(0, 0, m_icon
);
161 void SelectionToggle::setFadingValue(int value
)
163 m_fadingValue
= value
;
164 if (m_fadingValue
>= 255) {
165 Q_ASSERT(m_fadingTimeLine
!= 0);
166 m_fadingTimeLine
->stop();
171 void SelectionToggle::setIconOverlay(bool checked
)
173 const char* icon
= checked
? "list-remove" : "list-add";
174 m_icon
= KIconLoader::global()->loadIcon(icon
,
175 KIconLoader::NoGroup
,
176 KIconLoader::SizeSmall
);
180 void SelectionToggle::startFading()
182 Q_ASSERT(m_fadingTimeLine
== 0);
184 const bool animate
= KGlobalSettings::graphicEffectsLevel() & KGlobalSettings::SimpleAnimationEffects
;
185 const int duration
= animate
? 600 : 1;
187 m_fadingTimeLine
= new QTimeLine(duration
, this);
188 connect(m_fadingTimeLine
, SIGNAL(frameChanged(int)),
189 this, SLOT(setFadingValue(int)));
190 m_fadingTimeLine
->setFrameRange(0, 255);
191 m_fadingTimeLine
->start();
195 void SelectionToggle::stopFading()
197 if (m_fadingTimeLine
!= 0) {
198 m_fadingTimeLine
->stop();
199 delete m_fadingTimeLine
;
200 m_fadingTimeLine
= 0;
205 #include "selectiontoggle.moc"