]> cloud.milkyroute.net Git - dolphin.git/blob - src/selectiontoggle.cpp
Blend in a toggle button when hovering items. This allows selecting items without...
[dolphin.git] / src / selectiontoggle.cpp
1 /***************************************************************************
2 * Copyright (C) 2008 by Peter Penz <peter.penz@gmx.at> *
3 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20 #include "selectiontoggle.h"
21
22 #include <kicon.h>
23 #include <kiconloader.h>
24 #include <kiconeffect.h>
25
26 #include <QPainter>
27 #include <QPaintEvent>
28 #include <QRect>
29 #include <QTimer>
30
31 #include <kdebug.h>
32
33 SelectionToggle::SelectionToggle(QWidget* parent) :
34 QAbstractButton(parent),
35 m_showIcon(false),
36 m_isHovered(false),
37 m_icon(),
38 m_timer(0)
39 {
40 parent->installEventFilter(this);
41 resize(sizeHint());
42 m_icon = KIconLoader::global()->loadIcon("dialog-ok",
43 KIconLoader::NoGroup,
44 KIconLoader::SizeSmall);
45 m_timer = new QTimer(this);
46 connect(m_timer, SIGNAL(timeout()),
47 this, SLOT(showIcon()));
48 }
49
50 SelectionToggle::~SelectionToggle()
51 {
52 }
53
54 QSize SelectionToggle::sizeHint() const
55 {
56 return QSize(16, 16);
57 }
58
59 void SelectionToggle::setVisible(bool visible)
60 {
61 QAbstractButton::setVisible(visible);
62 if (visible) {
63 m_timer->start(1000);
64 } else {
65 m_timer->stop();
66 m_showIcon = false;
67 }
68 }
69
70 bool SelectionToggle::eventFilter(QObject* obj, QEvent* event)
71 {
72 if ((obj == parent()) && (event->type() == QEvent::Leave)) {
73 hide();
74 }
75 return QAbstractButton::eventFilter(obj, event);
76 }
77
78 void SelectionToggle::enterEvent(QEvent* event)
79 {
80 QAbstractButton::enterEvent(event);
81 m_isHovered = true;
82 m_showIcon = true;
83 update();
84 }
85
86 void SelectionToggle::leaveEvent(QEvent* event)
87 {
88 QAbstractButton::leaveEvent(event);
89 m_isHovered = false;
90 update();
91 }
92
93 void SelectionToggle::paintEvent(QPaintEvent* event)
94 {
95 QPainter painter(this);
96 painter.setClipRect(event->rect());
97
98 if (m_isHovered) {
99 KIconEffect iconEffect;
100 QPixmap activeIcon = iconEffect.apply(m_icon, KIconLoader::Desktop, KIconLoader::ActiveState);
101 painter.drawPixmap(0, 0, activeIcon);
102 } else if (m_showIcon) {
103 painter.drawPixmap(0, 0, m_icon);
104 }
105 }
106
107 void SelectionToggle::showIcon()
108 {
109 m_showIcon = true;
110 update();
111 }
112
113 #include "selectiontoggle.moc"