]> cloud.milkyroute.net Git - dolphin.git/blob - src/selectiontoggle.cpp
fixed crash when cancelling the "apply view properties" dialog
[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 m_icon = KIconLoader::global()->loadIcon("dialog-ok",
42 KIconLoader::NoGroup,
43 KIconLoader::SizeSmall);
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_item = KFileItem();
58 hide();
59 }
60
61 void SelectionToggle::setFileItem(const KFileItem& item)
62 {
63 m_item = item;
64 if (!item.isNull()) {
65 startFading();
66 }
67 }
68
69 KFileItem SelectionToggle::fileItem() const
70 {
71 return m_item;
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
119 if (m_isHovered) {
120 KIconEffect iconEffect;
121 QPixmap activeIcon = iconEffect.apply(m_icon, KIconLoader::Desktop, KIconLoader::ActiveState);
122 painter.drawPixmap(0, 0, activeIcon);
123 } else {
124 if (m_fadingValue < 255) {
125 // apply an alpha mask respecting the fading value to the icon
126 QPixmap icon = m_icon;
127 QPixmap alphaMask(icon.width(), icon.height());
128 const QColor color(m_fadingValue, m_fadingValue, m_fadingValue);
129 alphaMask.fill(color);
130 icon.setAlphaChannel(alphaMask);
131 painter.drawPixmap(0, 0, icon);
132 } else {
133 // no fading is required
134 painter.drawPixmap(0, 0, m_icon);
135 }
136 }
137 }
138
139 void SelectionToggle::setFadingValue(int value)
140 {
141 m_fadingValue = value;
142 if (m_fadingValue >= 255) {
143 Q_ASSERT(m_fadingTimeLine != 0);
144 m_fadingTimeLine->stop();
145 }
146 update();
147 }
148
149 void SelectionToggle::startFading()
150 {
151 Q_ASSERT(m_fadingTimeLine == 0);
152
153 m_fadingTimeLine = new QTimeLine(2000, this);
154 connect(m_fadingTimeLine, SIGNAL(frameChanged(int)),
155 this, SLOT(setFadingValue(int)));
156 m_fadingTimeLine->setFrameRange(0, 255);
157 m_fadingTimeLine->start();
158 m_fadingValue = 0;
159 }
160
161 void SelectionToggle::stopFading()
162 {
163 if (m_fadingTimeLine != 0) {
164 m_fadingTimeLine->stop();
165 delete m_fadingTimeLine;
166 m_fadingTimeLine = 0;
167 }
168 m_fadingValue = 0;
169 }
170
171 #include "selectiontoggle.moc"