]>
cloud.milkyroute.net Git - dolphin.git/blob - src/views/selectiontoggle.cpp
40e0cfc10e61889656287a67e2f90c19d23579fc
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>
24 #include <KIconLoader>
25 #include <KIconEffect>
28 #include <QApplication>
30 #include <QPaintEvent>
37 SelectionToggle::SelectionToggle(QWidget
* parent
) :
38 QAbstractButton(parent
),
40 m_leftMouseButtonPressed(false),
46 setFocusPolicy(Qt::NoFocus
);
47 parent
->installEventFilter(this);
49 setIconOverlay(isChecked());
50 connect(this, SIGNAL(toggled(bool)),
51 this, SLOT(setIconOverlay(bool)));
52 connect(KGlobalSettings::self(), SIGNAL(iconChanged(int)),
53 this, SLOT(refreshIcon()));
56 SelectionToggle::~SelectionToggle()
60 QSize
SelectionToggle::sizeHint() const
65 void SelectionToggle::reset()
71 void SelectionToggle::setUrl(const KUrl
& url
)
79 void SelectionToggle::setMargin(int margin
)
81 if (margin
!= m_margin
) {
87 int SelectionToggle::margin() const
92 KUrl
SelectionToggle::url() const
97 void SelectionToggle::setVisible(bool visible
)
99 QAbstractButton::setVisible(visible
);
108 bool SelectionToggle::eventFilter(QObject
* obj
, QEvent
* event
)
110 if ((obj
== parent()) && (event
->type() == QEvent::MouseMove
) && m_leftMouseButtonPressed
) {
111 // Don't forward mouse move events to the viewport,
112 // otherwise a rubberband selection will be shown when
113 // clicking on the selection toggle and moving the mouse
114 // above the viewport.
118 return QAbstractButton::eventFilter(obj
, event
);
121 void SelectionToggle::enterEvent(QEvent
* event
)
123 QAbstractButton::enterEvent(event
);
125 // if the mouse cursor is above the selection toggle, display
126 // it immediately without fading timer
128 if (m_fadingTimeLine
!= 0) {
129 m_fadingTimeLine
->stop();
132 setToolTip(isChecked() ? i18nc("@info:tooltip", "Deselect Item") :
133 i18nc("@info:tooltip", "Select Item"));
137 void SelectionToggle::leaveEvent(QEvent
* event
)
139 QAbstractButton::leaveEvent(event
);
145 void SelectionToggle::mousePressEvent(QMouseEvent
* event
)
147 QAbstractButton::mousePressEvent(event
);
148 m_leftMouseButtonPressed
= (event
->buttons() & Qt::LeftButton
);
151 void SelectionToggle::mouseReleaseEvent(QMouseEvent
* event
)
153 QAbstractButton::mouseReleaseEvent(event
);
154 m_leftMouseButtonPressed
= (event
->buttons() & Qt::LeftButton
);
157 void SelectionToggle::resizeEvent(QResizeEvent
* event
)
159 QAbstractButton::resizeEvent(event
);
160 setIconOverlay(isChecked());
163 void SelectionToggle::paintEvent(QPaintEvent
* event
)
165 QPainter
painter(this);
166 painter
.setClipRect(event
->rect());
168 // draw the icon overlay
169 const QPoint
pos(m_margin
, m_margin
);
171 KIconEffect
*iconEffect
= KIconLoader::global()->iconEffect();
172 QPixmap activeIcon
= iconEffect
->apply(m_icon
, KIconLoader::Desktop
, KIconLoader::ActiveState
);
173 painter
.drawPixmap(pos
, activeIcon
);
175 if (m_fadingValue
< 255) {
176 // apply an alpha mask respecting the fading value to the icon
177 QPixmap icon
= m_icon
;
178 QPixmap
alphaMask(icon
.width(), icon
.height());
179 const QColor
color(m_fadingValue
, m_fadingValue
, m_fadingValue
);
180 alphaMask
.fill(color
);
181 icon
.setAlphaChannel(alphaMask
);
182 painter
.drawPixmap(pos
, icon
);
184 // no fading is required
185 painter
.drawPixmap(pos
, m_icon
);
191 void SelectionToggle::setFadingValue(int value
)
193 m_fadingValue
= value
;
194 if (m_fadingValue
>= 255) {
195 Q_ASSERT(m_fadingTimeLine
!= 0);
196 m_fadingTimeLine
->stop();
201 void SelectionToggle::setIconOverlay(bool checked
)
203 const char* icon
= checked
? "list-remove" : "list-add";
204 const int size
= qMin(width() - 2 * m_margin
, height() - 2 * m_margin
);
205 m_icon
= KIconLoader::global()->loadIcon(icon
,
206 KIconLoader::NoGroup
,
211 void SelectionToggle::refreshIcon()
213 setIconOverlay(isChecked());
216 void SelectionToggle::startFading()
218 Q_ASSERT(m_fadingTimeLine
== 0);
220 const bool animate
= KGlobalSettings::graphicEffectsLevel() & KGlobalSettings::SimpleAnimationEffects
;
221 const int duration
= animate
? 600 : 1;
223 m_fadingTimeLine
= new QTimeLine(duration
, this);
224 connect(m_fadingTimeLine
, SIGNAL(frameChanged(int)),
225 this, SLOT(setFadingValue(int)));
226 m_fadingTimeLine
->setFrameRange(0, 255);
227 m_fadingTimeLine
->start();
231 void SelectionToggle::stopFading()
233 if (m_fadingTimeLine
!= 0) {
234 m_fadingTimeLine
->stop();
235 delete m_fadingTimeLine
;
236 m_fadingTimeLine
= 0;
241 #include "selectiontoggle.moc"