]>
cloud.milkyroute.net Git - dolphin.git/blob - src/views/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 <QApplication>
30 #include <QPaintEvent>
35 SelectionToggle::SelectionToggle(QWidget
* parent
) :
36 QAbstractButton(parent
),
38 m_leftMouseButtonPressed(false),
39 m_appliedArrowCursor(false),
45 setFocusPolicy(Qt::NoFocus
);
46 parent
->installEventFilter(this);
48 setIconOverlay(isChecked());
49 connect(this, SIGNAL(toggled(bool)),
50 this, SLOT(setIconOverlay(bool)));
51 connect(KGlobalSettings::self(), SIGNAL(iconChanged(int)),
52 this, SLOT(refreshIcon()));
55 SelectionToggle::~SelectionToggle()
59 QSize
SelectionToggle::sizeHint() const
64 void SelectionToggle::reset()
70 void SelectionToggle::setUrl(const KUrl
& url
)
78 void SelectionToggle::setMargin(int margin
)
80 if (margin
!= m_margin
) {
86 int SelectionToggle::margin() const
91 KUrl
SelectionToggle::url() const
96 void SelectionToggle::setVisible(bool visible
)
98 QAbstractButton::setVisible(visible
);
107 bool SelectionToggle::eventFilter(QObject
* obj
, QEvent
* event
)
109 if (obj
== parent()) {
110 switch (event
->type()) {
115 case QEvent::MouseMove
:
116 if (m_leftMouseButtonPressed
) {
117 // Don't forward mouse move events to the viewport,
118 // otherwise a rubberband selection will be shown when
119 // clicking on the selection toggle and moving the mouse
120 // above the viewport.
130 return QAbstractButton::eventFilter(obj
, event
);
133 void SelectionToggle::enterEvent(QEvent
* event
)
135 QAbstractButton::enterEvent(event
);
137 if (!m_appliedArrowCursor
) {
138 QApplication::setOverrideCursor(QCursor(Qt::ArrowCursor
));
139 m_appliedArrowCursor
= true;
142 // if the mouse cursor is above the selection toggle, display
143 // it immediately without fading timer
145 if (m_fadingTimeLine
!= 0) {
146 m_fadingTimeLine
->stop();
149 setToolTip(isChecked() ? i18nc("@info:tooltip", "Deselect Item") :
150 i18nc("@info:tooltip", "Select Item"));
154 void SelectionToggle::leaveEvent(QEvent
* event
)
156 QAbstractButton::leaveEvent(event
);
158 if (m_appliedArrowCursor
) {
159 QApplication::restoreOverrideCursor();
160 m_appliedArrowCursor
= false;
167 void SelectionToggle::mousePressEvent(QMouseEvent
* event
)
169 QAbstractButton::mousePressEvent(event
);
170 m_leftMouseButtonPressed
= (event
->buttons() & Qt::LeftButton
);
173 void SelectionToggle::mouseReleaseEvent(QMouseEvent
* event
)
175 QAbstractButton::mouseReleaseEvent(event
);
176 m_leftMouseButtonPressed
= (event
->buttons() & Qt::LeftButton
);
179 void SelectionToggle::resizeEvent(QResizeEvent
* event
)
181 QAbstractButton::resizeEvent(event
);
182 setIconOverlay(isChecked());
185 void SelectionToggle::paintEvent(QPaintEvent
* event
)
187 QPainter
painter(this);
188 painter
.setClipRect(event
->rect());
190 // draw the icon overlay
191 const QPoint
pos(m_margin
, m_margin
);
193 KIconEffect
*iconEffect
= KIconLoader::global()->iconEffect();
194 QPixmap activeIcon
= iconEffect
->apply(m_icon
, KIconLoader::Desktop
, KIconLoader::ActiveState
);
195 painter
.drawPixmap(pos
, activeIcon
);
197 if (m_fadingValue
< 255) {
198 // apply an alpha mask respecting the fading value to the icon
199 QPixmap icon
= m_icon
;
200 QPixmap
alphaMask(icon
.width(), icon
.height());
201 const QColor
color(m_fadingValue
, m_fadingValue
, m_fadingValue
);
202 alphaMask
.fill(color
);
203 icon
.setAlphaChannel(alphaMask
);
204 painter
.drawPixmap(pos
, icon
);
206 // no fading is required
207 painter
.drawPixmap(pos
, m_icon
);
213 void SelectionToggle::setFadingValue(int value
)
215 m_fadingValue
= value
;
216 if (m_fadingValue
>= 255) {
217 Q_ASSERT(m_fadingTimeLine
!= 0);
218 m_fadingTimeLine
->stop();
223 void SelectionToggle::setIconOverlay(bool checked
)
225 const char* icon
= checked
? "list-remove" : "list-add";
226 const int size
= qMin(width() - 2 * m_margin
, height() - 2 * m_margin
);
227 m_icon
= KIconLoader::global()->loadIcon(icon
,
228 KIconLoader::NoGroup
,
233 void SelectionToggle::refreshIcon()
235 setIconOverlay(isChecked());
238 void SelectionToggle::startFading()
240 Q_ASSERT(m_fadingTimeLine
== 0);
242 const bool animate
= KGlobalSettings::graphicEffectsLevel() & KGlobalSettings::SimpleAnimationEffects
;
243 const int duration
= animate
? 600 : 1;
245 m_fadingTimeLine
= new QTimeLine(duration
, this);
246 connect(m_fadingTimeLine
, SIGNAL(frameChanged(int)),
247 this, SLOT(setFadingValue(int)));
248 m_fadingTimeLine
->setFrameRange(0, 255);
249 m_fadingTimeLine
->start();
253 void SelectionToggle::stopFading()
255 if (m_fadingTimeLine
!= 0) {
256 m_fadingTimeLine
->stop();
257 delete m_fadingTimeLine
;
258 m_fadingTimeLine
= 0;
263 #include "selectiontoggle.moc"