]>
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
),
37 m_leftMouseButtonPressed(false),
42 setFocusPolicy(Qt::NoFocus
);
43 parent
->installEventFilter(this);
45 setIconOverlay(isChecked());
46 connect(this, SIGNAL(toggled(bool)),
47 this, SLOT(setIconOverlay(bool)));
48 connect(KGlobalSettings::self(), SIGNAL(iconChanged(int)),
49 this, SLOT(refreshIcon()));
52 SelectionToggle::~SelectionToggle()
56 QSize
SelectionToggle::sizeHint() const
61 void SelectionToggle::reset()
67 void SelectionToggle::setUrl(const KUrl
& url
)
75 KUrl
SelectionToggle::url() const
80 void SelectionToggle::setVisible(bool visible
)
82 QAbstractButton::setVisible(visible
);
91 bool SelectionToggle::eventFilter(QObject
* obj
, QEvent
* event
)
93 if (obj
== parent()) {
94 switch (event
->type()) {
99 case QEvent::MouseMove
:
100 if (m_leftMouseButtonPressed
) {
101 // Don't forward mouse move events to the viewport,
102 // otherwise a rubberband selection will be shown when
103 // clicking on the selection toggle and moving the mouse
104 // above the viewport.
114 return QAbstractButton::eventFilter(obj
, event
);
117 void SelectionToggle::enterEvent(QEvent
* event
)
119 QAbstractButton::enterEvent(event
);
121 // if the mouse cursor is above the selection toggle, display
122 // it immediately without fading timer
124 if (m_fadingTimeLine
!= 0) {
125 m_fadingTimeLine
->stop();
128 setToolTip(isChecked() ? i18nc("@info:tooltip", "Deselect Item") :
129 i18nc("@info:tooltip", "Select Item"));
133 void SelectionToggle::leaveEvent(QEvent
* event
)
135 QAbstractButton::leaveEvent(event
);
140 void SelectionToggle::mousePressEvent(QMouseEvent
* event
)
142 QAbstractButton::mousePressEvent(event
);
143 m_leftMouseButtonPressed
= (event
->buttons() & Qt::LeftButton
);
146 void SelectionToggle::mouseReleaseEvent(QMouseEvent
* event
)
148 QAbstractButton::mouseReleaseEvent(event
);
149 m_leftMouseButtonPressed
= (event
->buttons() & Qt::LeftButton
);
152 void SelectionToggle::resizeEvent(QResizeEvent
* event
)
154 QAbstractButton::resizeEvent(event
);
155 setIconOverlay(isChecked());
158 void SelectionToggle::paintEvent(QPaintEvent
* event
)
160 QPainter
painter(this);
161 painter
.setClipRect(event
->rect());
163 // draw the icon overlay
165 KIconEffect iconEffect
;
166 QPixmap activeIcon
= iconEffect
.apply(m_icon
, KIconLoader::Desktop
, KIconLoader::ActiveState
);
167 painter
.drawPixmap(0, 0, activeIcon
);
169 if (m_fadingValue
< 255) {
170 // apply an alpha mask respecting the fading value to the icon
171 QPixmap icon
= m_icon
;
172 QPixmap
alphaMask(icon
.width(), icon
.height());
173 const QColor
color(m_fadingValue
, m_fadingValue
, m_fadingValue
);
174 alphaMask
.fill(color
);
175 icon
.setAlphaChannel(alphaMask
);
176 painter
.drawPixmap(0, 0, icon
);
178 // no fading is required
179 painter
.drawPixmap(0, 0, m_icon
);
184 void SelectionToggle::setFadingValue(int value
)
186 m_fadingValue
= value
;
187 if (m_fadingValue
>= 255) {
188 Q_ASSERT(m_fadingTimeLine
!= 0);
189 m_fadingTimeLine
->stop();
194 void SelectionToggle::setIconOverlay(bool checked
)
196 const char* icon
= checked
? "list-remove" : "list-add";
197 m_icon
= KIconLoader::global()->loadIcon(icon
,
198 KIconLoader::NoGroup
,
199 qMin(width(), height()));
203 void SelectionToggle::refreshIcon()
205 setIconOverlay(isChecked());
208 void SelectionToggle::startFading()
210 Q_ASSERT(m_fadingTimeLine
== 0);
212 const bool animate
= KGlobalSettings::graphicEffectsLevel() & KGlobalSettings::SimpleAnimationEffects
;
213 const int duration
= animate
? 600 : 1;
215 m_fadingTimeLine
= new QTimeLine(duration
, this);
216 connect(m_fadingTimeLine
, SIGNAL(frameChanged(int)),
217 this, SLOT(setFadingValue(int)));
218 m_fadingTimeLine
->setFrameRange(0, 255);
219 m_fadingTimeLine
->start();
223 void SelectionToggle::stopFading()
225 if (m_fadingTimeLine
!= 0) {
226 m_fadingTimeLine
->stop();
227 delete m_fadingTimeLine
;
228 m_fadingTimeLine
= 0;
233 #include "selectiontoggle.moc"