]>
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>
29 #include <QPaintEvent>
34 SelectionToggle::SelectionToggle(QWidget
* parent
) :
35 QAbstractButton(parent
),
37 m_leftMouseButtonPressed(false),
43 setFocusPolicy(Qt::NoFocus
);
44 parent
->installEventFilter(this);
46 setIconOverlay(isChecked());
47 connect(this, SIGNAL(toggled(bool)),
48 this, SLOT(setIconOverlay(bool)));
49 connect(KGlobalSettings::self(), SIGNAL(iconChanged(int)),
50 this, SLOT(refreshIcon()));
53 SelectionToggle::~SelectionToggle()
57 QSize
SelectionToggle::sizeHint() const
62 void SelectionToggle::reset()
68 void SelectionToggle::setUrl(const KUrl
& url
)
76 void SelectionToggle::setMargin(int margin
)
78 if (margin
!= m_margin
) {
84 int SelectionToggle::margin() const
89 KUrl
SelectionToggle::url() const
94 void SelectionToggle::setVisible(bool visible
)
96 QAbstractButton::setVisible(visible
);
105 bool SelectionToggle::eventFilter(QObject
* obj
, QEvent
* event
)
107 if (obj
== parent()) {
108 switch (event
->type()) {
113 case QEvent::MouseMove
:
114 if (m_leftMouseButtonPressed
) {
115 // Don't forward mouse move events to the viewport,
116 // otherwise a rubberband selection will be shown when
117 // clicking on the selection toggle and moving the mouse
118 // above the viewport.
128 return QAbstractButton::eventFilter(obj
, event
);
131 void SelectionToggle::enterEvent(QEvent
* event
)
133 QAbstractButton::enterEvent(event
);
135 // if the mouse cursor is above the selection toggle, display
136 // it immediately without fading timer
138 if (m_fadingTimeLine
!= 0) {
139 m_fadingTimeLine
->stop();
142 setToolTip(isChecked() ? i18nc("@info:tooltip", "Deselect Item") :
143 i18nc("@info:tooltip", "Select Item"));
147 void SelectionToggle::leaveEvent(QEvent
* event
)
149 QAbstractButton::leaveEvent(event
);
154 void SelectionToggle::mousePressEvent(QMouseEvent
* event
)
156 QAbstractButton::mousePressEvent(event
);
157 m_leftMouseButtonPressed
= (event
->buttons() & Qt::LeftButton
);
160 void SelectionToggle::mouseReleaseEvent(QMouseEvent
* event
)
162 QAbstractButton::mouseReleaseEvent(event
);
163 m_leftMouseButtonPressed
= (event
->buttons() & Qt::LeftButton
);
166 void SelectionToggle::resizeEvent(QResizeEvent
* event
)
168 QAbstractButton::resizeEvent(event
);
169 setIconOverlay(isChecked());
172 void SelectionToggle::paintEvent(QPaintEvent
* event
)
174 QPainter
painter(this);
175 painter
.setClipRect(event
->rect());
177 // draw the icon overlay
178 const QPoint
pos(m_margin
, m_margin
);
180 KIconEffect
*iconEffect
= KIconLoader::global()->iconEffect();
181 QPixmap activeIcon
= iconEffect
->apply(m_icon
, KIconLoader::Desktop
, KIconLoader::ActiveState
);
182 painter
.drawPixmap(pos
, activeIcon
);
184 if (m_fadingValue
< 255) {
185 // apply an alpha mask respecting the fading value to the icon
186 QPixmap icon
= m_icon
;
187 QPixmap
alphaMask(icon
.width(), icon
.height());
188 const QColor
color(m_fadingValue
, m_fadingValue
, m_fadingValue
);
189 alphaMask
.fill(color
);
190 icon
.setAlphaChannel(alphaMask
);
191 painter
.drawPixmap(pos
, icon
);
193 // no fading is required
194 painter
.drawPixmap(pos
, m_icon
);
200 void SelectionToggle::setFadingValue(int value
)
202 m_fadingValue
= value
;
203 if (m_fadingValue
>= 255) {
204 Q_ASSERT(m_fadingTimeLine
!= 0);
205 m_fadingTimeLine
->stop();
210 void SelectionToggle::setIconOverlay(bool checked
)
212 const char* icon
= checked
? "list-remove" : "list-add";
213 const int size
= qMin(width() - 2 * m_margin
, height() - 2 * m_margin
);
214 m_icon
= KIconLoader::global()->loadIcon(icon
,
215 KIconLoader::NoGroup
,
220 void SelectionToggle::refreshIcon()
222 setIconOverlay(isChecked());
225 void SelectionToggle::startFading()
227 Q_ASSERT(m_fadingTimeLine
== 0);
229 const bool animate
= KGlobalSettings::graphicEffectsLevel() & KGlobalSettings::SimpleAnimationEffects
;
230 const int duration
= animate
? 600 : 1;
232 m_fadingTimeLine
= new QTimeLine(duration
, this);
233 connect(m_fadingTimeLine
, SIGNAL(frameChanged(int)),
234 this, SLOT(setFadingValue(int)));
235 m_fadingTimeLine
->setFrameRange(0, 255);
236 m_fadingTimeLine
->start();
240 void SelectionToggle::stopFading()
242 if (m_fadingTimeLine
!= 0) {
243 m_fadingTimeLine
->stop();
244 delete m_fadingTimeLine
;
245 m_fadingTimeLine
= 0;
250 #include "selectiontoggle.moc"