]> cloud.milkyroute.net Git - dolphin.git/blob - src/selectiontoggle.cpp
* the WA_Hover flag is set by KFileItemDelegate automatically now
[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 #include <QTimeLine>
31
32 SelectionToggle::SelectionToggle(QWidget* parent) :
33 QAbstractButton(parent),
34 m_isHovered(false),
35 m_fadingValue(0),
36 m_icon(),
37 m_fadingTimeLine(0)
38 {
39 setFocusPolicy(Qt::NoFocus);
40 parent->installEventFilter(this);
41 resize(sizeHint());
42 setIconOverlay(isChecked());
43 connect(this, SIGNAL(toggled(bool)),
44 this, SLOT(setIconOverlay(bool)));
45 }
46
47 SelectionToggle::~SelectionToggle()
48 {
49 }
50
51 QSize SelectionToggle::sizeHint() const
52 {
53 return QSize(16, 16);
54 }
55
56 void SelectionToggle::reset()
57 {
58 m_url = KUrl();
59 hide();
60 }
61
62 void SelectionToggle::setUrl(const KUrl& url)
63 {
64 m_url = url;
65 if (!url.isEmpty()) {
66 startFading();
67 }
68 }
69
70 KUrl SelectionToggle::url() const
71 {
72 return m_url;
73 }
74
75 void SelectionToggle::setVisible(bool visible)
76 {
77 QAbstractButton::setVisible(visible);
78
79 stopFading();
80 if (visible) {
81 startFading();
82 }
83
84 }
85
86 bool SelectionToggle::eventFilter(QObject* obj, QEvent* event)
87 {
88 if ((obj == parent()) && (event->type() == QEvent::Leave)) {
89 hide();
90 }
91 return QAbstractButton::eventFilter(obj, event);
92 }
93
94 void SelectionToggle::enterEvent(QEvent* event)
95 {
96 QAbstractButton::enterEvent(event);
97
98 // if the mouse cursor is above the selection toggle, display
99 // it immediately without fading timer
100 m_isHovered = true;
101 if (m_fadingTimeLine != 0) {
102 m_fadingTimeLine->stop();
103 }
104 m_fadingValue = 255;
105 update();
106 }
107
108 void SelectionToggle::leaveEvent(QEvent* event)
109 {
110 QAbstractButton::leaveEvent(event);
111 m_isHovered = false;
112 update();
113 }
114
115 void SelectionToggle::paintEvent(QPaintEvent* event)
116 {
117 QPainter painter(this);
118 painter.setClipRect(event->rect());
119 painter.setRenderHint(QPainter::Antialiasing);
120
121 // draw an alpha blended circle as background
122 const QPalette& palette = parentWidget()->palette();
123
124 const QBrush& backgroundBrush = palette.brush(QPalette::Normal, QPalette::Window);
125 QColor background = backgroundBrush.color();
126 background.setAlpha(m_fadingValue / 2);
127 painter.setBrush(background);
128
129 const QBrush& foregroundBrush = palette.brush(QPalette::Normal, QPalette::WindowText);
130 QColor foreground = foregroundBrush.color();
131 foreground.setAlpha(m_fadingValue / 4);
132 painter.setPen(foreground);
133
134 painter.drawEllipse(0, 0, width(), height());
135
136 // draw the icon overlay
137 if (m_isHovered) {
138 KIconEffect iconEffect;
139 QPixmap activeIcon = iconEffect.apply(m_icon, KIconLoader::Desktop, KIconLoader::ActiveState);
140 painter.drawPixmap(0, 0, activeIcon);
141 } else {
142 if (m_fadingValue < 255) {
143 // apply an alpha mask respecting the fading value to the icon
144 QPixmap icon = m_icon;
145 QPixmap alphaMask(icon.width(), icon.height());
146 const QColor color(m_fadingValue, m_fadingValue, m_fadingValue);
147 alphaMask.fill(color);
148 icon.setAlphaChannel(alphaMask);
149 painter.drawPixmap(0, 0, icon);
150 } else {
151 // no fading is required
152 painter.drawPixmap(0, 0, m_icon);
153 }
154 }
155 }
156
157 void SelectionToggle::setFadingValue(int value)
158 {
159 m_fadingValue = value;
160 if (m_fadingValue >= 255) {
161 Q_ASSERT(m_fadingTimeLine != 0);
162 m_fadingTimeLine->stop();
163 }
164 update();
165 }
166
167 void SelectionToggle::setIconOverlay(bool checked)
168 {
169 const char* icon = checked ? "list-remove" : "list-add";
170 m_icon = KIconLoader::global()->loadIcon(icon,
171 KIconLoader::NoGroup,
172 KIconLoader::SizeSmall);
173 update();
174 }
175
176 void SelectionToggle::startFading()
177 {
178 Q_ASSERT(m_fadingTimeLine == 0);
179
180 m_fadingTimeLine = new QTimeLine(1500, this);
181 connect(m_fadingTimeLine, SIGNAL(frameChanged(int)),
182 this, SLOT(setFadingValue(int)));
183 m_fadingTimeLine->setFrameRange(0, 255);
184 m_fadingTimeLine->start();
185 m_fadingValue = 0;
186 }
187
188 void SelectionToggle::stopFading()
189 {
190 if (m_fadingTimeLine != 0) {
191 m_fadingTimeLine->stop();
192 delete m_fadingTimeLine;
193 m_fadingTimeLine = 0;
194 }
195 m_fadingValue = 0;
196 }
197
198 #include "selectiontoggle.moc"