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