1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
/*
* Copyright © 2009, 2010 Fredrik Höglund <fredrik@kde.org>
* Copyright © 2009 Bruno Bigras <bigras.bruno@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "actionoverlay.h"
#include "asyncfiletester.h"
#include "iconview.h"
#include <Plasma/PaintUtils>
#include <Plasma/Animator>
#include <Plasma/Svg>
#include <QPainter>
#include <QGraphicsGridLayout>
ActionIcon::ActionIcon(QGraphicsItem* parent)
: QGraphicsWidget(parent),
m_pressed(false),
m_sunken(false)
{
setAcceptHoverEvents(true);
setCacheMode(DeviceCoordinateCache);
m_icon = new Plasma::Svg(this);
m_icon->setImagePath("widgets/action-overlays");
m_icon->setContainsMultipleImages(true);
setMinimumSize(m_icon->elementSize("add-normal"));
setMaximumSize(minimumSize());
show();
}
void ActionIcon::setElement(const QString &element)
{
m_element = element;
}
void ActionIcon::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
Q_UNUSED(option)
Q_UNUSED(widget)
QString element = m_element;
if (m_sunken) {
element += "-pressed";
} else if (isUnderMouse()) {
element += "-hover";
} else {
element += "-normal";
}
m_icon->paint(painter, rect(), element);
}
void ActionIcon::mousePressEvent(QGraphicsSceneMouseEvent* event)
{
Q_UNUSED(event)
m_pressed = true;
m_sunken = true;
update();
}
void ActionIcon::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
Q_UNUSED(event)
m_pressed = false;
m_sunken = false;
if (isUnderMouse()) {
emit clicked();
}
update();
}
void ActionIcon::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
Q_UNUSED(event)
if (m_sunken != isUnderMouse()) {
m_sunken = isUnderMouse();
update();
}
}
void ActionIcon::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
Q_UNUSED(event)
emit iconHoverEnter();
update();
}
void ActionIcon::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
Q_UNUSED(event)
emit iconHoverLeave();
update();
}
// --------------------------------------------------------
ActionOverlay::ActionOverlay(AbstractItemView* parent)
: QGraphicsWidget(parent)
{
m_toggleButton = new ActionIcon(this);
m_openButton = new ActionIcon(this);
m_openButton->setElement("open");
m_showFolderButton = true;
m_showSelectionButton = true;
m_layout = new QGraphicsGridLayout(this);
m_layout->setContentsMargins(4, 4, 4, 4);
m_layout->setSpacing(1);
m_layout->addItem(m_toggleButton, 0, 0);
m_layout->addItem(m_openButton, 1, 0);
connect(parentWidget(), SIGNAL(entered(QModelIndex)), this, SLOT(entered(QModelIndex)));
connect(parentWidget(), SIGNAL(left(QModelIndex)), this, SLOT(left(QModelIndex)));
connect(parentWidget(), SIGNAL(modelChanged()), this, SLOT(modelChanged()));
connect(m_toggleButton, SIGNAL(clicked()), this, SLOT(toggleSelection()));
connect(m_openButton, SIGNAL(clicked()), this, SLOT(openPopup()));
m_hideActionOverlayIconTimer = new QTimer(this);
connect(m_hideActionOverlayIconTimer, SIGNAL(timeout()), this, SLOT(timeout()));
connect(m_toggleButton, SIGNAL(iconHoverEnter()), m_hideActionOverlayIconTimer, SLOT(stop()));
connect(m_toggleButton, SIGNAL(iconHoverLeave()), m_hideActionOverlayIconTimer, SLOT(start()));
connect(m_openButton, SIGNAL(iconHoverEnter()), m_hideActionOverlayIconTimer, SLOT(stop()));
connect(m_openButton, SIGNAL(iconHoverLeave()), m_hideActionOverlayIconTimer, SLOT(start()));
connect(parent->verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(close()));
m_hideActionOverlayIconTimer->setInterval(500);
m_hideActionOverlayIconTimer->setSingleShot(true);
fadeIn = Plasma::Animator::create(Plasma::Animator::FadeAnimation, this);
fadeIn->setProperty("startOpacity", 0);
fadeIn->setProperty("targetOpacity", 1);
fadeIn->setTargetWidget(this);
fadeOut = Plasma::Animator::create(Plasma::Animator::FadeAnimation, this);
fadeOut->setProperty("startOpacity", 1);
fadeOut->setProperty("targetOpacity", 0);
fadeOut->setTargetWidget(this);
connect(fadeOut, SIGNAL(finished()), SLOT(close()));
hide();
}
void ActionOverlay::toggleSelection()
{
AbstractItemView *view = static_cast<AbstractItemView*>(parentWidget());
QItemSelectionModel *m_selectionModel = view->selectionModel();
if (m_hoverIndex.isValid()) {
const QModelIndex oldCurrent = m_selectionModel->currentIndex();
m_selectionModel->select(m_hoverIndex, QItemSelectionModel::Toggle);
m_selectionModel->setCurrentIndex(m_hoverIndex, QItemSelectionModel::NoUpdate);
m_toggleButton->setElement(m_selectionModel->isSelected(m_hoverIndex) ? "remove" : "add");
view->markAreaDirty(view->visualRect(m_hoverIndex));
if (oldCurrent.isValid() && oldCurrent != m_hoverIndex) {
view->markAreaDirty(view->visualRect(oldCurrent));
}
}
}
void ActionOverlay::openPopup()
{
if (IconView *view = qobject_cast<IconView*>(parentWidget())) {
view->openPopup(m_hoverIndex);
}
}
QPersistentModelIndex ActionOverlay::hoverIndex()
{
return m_hoverIndex;
}
void ActionOverlay::entered(const QModelIndex &index)
{
m_hideActionOverlayIconTimer->stop();
if (index.isValid()) {
AbstractItemView *view = static_cast<AbstractItemView*>(parentWidget());
QItemSelectionModel *m_selectionModel = view->selectionModel();
m_toggleButton->setElement(m_selectionModel->isSelected(index) ? "remove" : "add");
setPos(view->mapFromViewport(view->visualRect(index)).topLeft());
show();
if (m_hoverIndex != index) {
m_toggleButton->update();
fadeOut->stop();
fadeIn->start();
}
m_hoverIndex = index;
IconView *iview = qobject_cast<IconView*>(view);
if (iview && iview->clickToViewFolders()) {
AsyncFileTester::checkIfFolder(index, this, "checkIfFolderResult");
}
}
}
void ActionOverlay::checkIfFolderResult(const QModelIndex &index, bool isFolder)
{
if (index == m_hoverIndex) {
m_openButton->setVisible(isFolder);
}
}
void ActionOverlay::left(const QModelIndex &index)
{
Q_UNUSED(index);
if (!m_hideActionOverlayIconTimer->isActive()) {
m_hideActionOverlayIconTimer->start();
}
}
void ActionOverlay::timeout()
{
// allow the animation to restart after hiding the ActionOverlayIcon even if m_hoverIndex didn't change
m_hoverIndex = QPersistentModelIndex();
if (isVisible() && (fadeOut->state() != QAbstractAnimation::Running)) {
fadeIn->stop();
fadeOut->start();
}
}
void ActionOverlay::forceHide(HideHint hint)
{
m_hideActionOverlayIconTimer->stop();
if (hint == FadeOut) {
timeout();
} else {
hide();
}
}
void ActionOverlay::rowsRemoved(const QModelIndex & parent, int start, int end)
{
Q_UNUSED(parent);
Q_UNUSED(start);
Q_UNUSED(end);
if (!m_hoverIndex.isValid()) {
hide();
}
}
void ActionOverlay::modelChanged()
{
AbstractItemView *view = static_cast<AbstractItemView*>(parentWidget());
QAbstractItemModel *mod = view->model();
connect(mod, SIGNAL(rowsRemoved(QModelIndex,int,int)), SLOT(rowsRemoved(QModelIndex,int,int)));
}
void ActionOverlay::setShowFolderButton(bool show)
{
if (m_showFolderButton != show) {
m_showFolderButton = show;
toggleShowActionButton(show, m_openButton, 1);
}
}
void ActionOverlay::setShowSelectionButton(bool show)
{
if (m_showSelectionButton!= show) {
m_showSelectionButton = show;
toggleShowActionButton(show, m_toggleButton, 0);
}
}
bool ActionOverlay::showFolderButton() const
{
return m_showFolderButton;
}
bool ActionOverlay::showSelectionButton() const
{
return m_showSelectionButton;
}
QSizeF ActionOverlay::iconSize() const
{
return m_openButton->geometry().size();
}
void ActionOverlay::toggleShowActionButton(bool show, ActionIcon* button, unsigned int pos)
{
if (show && m_layout->itemAt(pos, 0) != button) {
m_layout->addItem(button, pos, 0);
button->show();
} else if (m_layout->itemAt(pos, 0) == button) {
button->hide();
#if QT_VERSION >= 0x040800
m_layout->removeItem(button);
#else
/* find the index of the item: yeah, this is ugly... on the other hand, this works in Qt 4.7,
* and if you have a look at the 4.8 source, this is exactly what it does. */
int index = -1;
for (int i = 0; i < m_layout->count(); ++i) {
if (m_layout->itemAt(i) == button) {
index = i;
break;
}
}
Q_ASSERT(index >= 0); // the button *is* part of the layout, so we have to find it
m_layout->removeAt(index);
#endif
}
}
|