forked from jonathanverner/comment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgwenview_splittercollapser.cpp
268 lines (207 loc) · 5.87 KB
/
gwenview_splittercollapser.cpp
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
// vim: set tabstop=4 shiftwidth=4 noexpandtab:
/*
Gwenview: an image viewer
Copyright 2009-2010 Aurélien Gâteau <[email protected]>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) version 3, or any
later version accepted by the membership of KDE e.V. (or its
successor approved by the membership of KDE e.V.), which shall
act as a proxy defined in Section 6 of version 3 of the license.
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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Self
#include "gwenview_splittercollapser.h"
// Qt
#include <QApplication>
#include <QEvent>
#include <QMouseEvent>
#include <QSplitter>
#include <QStyleOptionToolButton>
#include <QStylePainter>
#include <QTimeLine>
// Local
namespace Gwenview {
enum Direction {
LTR = 1 << 0,
RTL = 1 << 1,
Vertical = 1 << 2,
TTB = Vertical + (1 << 0),
BTT = Vertical + (1 << 1)
};
const int TIMELINE_DURATION = 500;
const qreal MINIMUM_OPACITY = 0.3;
struct ArrowTypes {
ArrowTypes() {}
ArrowTypes(Qt::ArrowType t1, Qt::ArrowType t2)
: visible(t1), notVisible(t2) {}
Qt::ArrowType visible,
notVisible;
Qt::ArrowType get(bool isVisible) {
return isVisible ? visible : notVisible;
}
};
struct SplitterCollapserPrivate {
SplitterCollapser* q;
QSplitter* mSplitter;
QWidget* mWidget;
Direction mDirection;
QTimeLine* mOpacityTimeLine;
bool isVertical() const {
return mDirection & Vertical;
}
void updatePosition() {
int x, y;
QRect widgetRect = mWidget->geometry();
int splitterWidth = mSplitter->width();
int handleWidth = mSplitter->handleWidth();
int width = q->width();
if (!isVertical()) {
// FIXME: Make this configurable
y = 60;
if (mDirection == LTR) {
if (mWidget->isVisible()) {
x = widgetRect.right() + handleWidth;
} else {
x = 0;
}
} else { // RTL
if (mWidget->isVisible()) {
x = widgetRect.left() - handleWidth - width;
} else {
x = splitterWidth - handleWidth - width;
}
}
} else {
// FIXME
x = 0;
y = 0;
}
q->move(x, y);
}
void updateArrow() {
static QMap<Direction, ArrowTypes> arrowForDirection;
if (arrowForDirection.isEmpty()) {
arrowForDirection[LTR] = ArrowTypes(Qt::LeftArrow, Qt::RightArrow);
arrowForDirection[RTL] = ArrowTypes(Qt::RightArrow, Qt::LeftArrow);
arrowForDirection[TTB] = ArrowTypes(Qt::UpArrow, Qt::DownArrow);
arrowForDirection[BTT] = ArrowTypes(Qt::DownArrow, Qt::UpArrow);
}
q->setArrowType(arrowForDirection[mDirection].get(mWidget->isVisible()));
}
void fadeIn() {
mOpacityTimeLine->setDirection(QTimeLine::Forward);
startTimeLine();
}
void fadeOut() {
mOpacityTimeLine->setDirection(QTimeLine::Backward);
startTimeLine();
}
void startTimeLine() {
if (mOpacityTimeLine->state() != QTimeLine::Running) {
mOpacityTimeLine->start();
}
}
};
SplitterCollapser::SplitterCollapser(QSplitter* splitter, QWidget* widget)
: QToolButton()
, d(new SplitterCollapserPrivate) {
d->q = this;
// We do not want our collapser to be added as a regular widget in the
// splitter!
setAttribute(Qt::WA_NoChildEventsForParent);
// Get QHoverEvents
setAttribute(Qt::WA_Hover);
// We do not want to get the focused, otherwise user might trigger
// it while pressing space to browse through images
setFocusPolicy(Qt::NoFocus);
d->mOpacityTimeLine = new QTimeLine(TIMELINE_DURATION, this);
d->mOpacityTimeLine->setFrameRange(int(MINIMUM_OPACITY * 1000), 1000);
connect(d->mOpacityTimeLine, SIGNAL(valueChanged(qreal)),
SLOT(update()));
d->mWidget = widget;
d->mWidget->installEventFilter(this);
d->mSplitter = splitter;
setParent(d->mSplitter);
if (splitter->indexOf(widget) < splitter->count() / 2) {
d->mDirection = LTR;
} else {
d->mDirection = RTL;
}
if (splitter->orientation() == Qt::Vertical) {
// FIXME: Ugly!
d->mDirection = static_cast<Direction>(int(d->mDirection) + int(TTB));
}
connect(this, SIGNAL(clicked()), SLOT(slotClicked()));
d->updatePosition();
show();
}
SplitterCollapser::~SplitterCollapser() {
delete d;
}
bool SplitterCollapser::eventFilter(QObject*, QEvent* event) {
switch (event->type()) {
case QEvent::Resize:
d->updatePosition();
break;
case QEvent::Show:
case QEvent::Hide:
d->updatePosition();
d->updateArrow();
break;
default:
break;
}
return false;
}
QSize SplitterCollapser::sizeHint() const {
int extent = style()->pixelMetric(QStyle::PM_ScrollBarExtent);
QSize sh(extent * 3 / 4, extent * 240 / 100);
if (d->isVertical()) {
sh.transpose();
}
return sh;
}
void SplitterCollapser::slotClicked() {
d->mWidget->setVisible(!d->mWidget->isVisible());
}
bool SplitterCollapser::event(QEvent* event) {
switch (event->type()) {
case QEvent::HoverEnter:
d->fadeIn();
break;
case QEvent::HoverLeave:
d->fadeOut();
break;
default:
break;
}
return QToolButton::event(event);
}
void SplitterCollapser::paintEvent(QPaintEvent*) {
QStylePainter painter(this);
qreal opacity = d->mOpacityTimeLine->currentFrame() / 1000.;
painter.setOpacity(opacity);
QStyleOptionToolButton opt;
initStyleOption(&opt);
if (d->mDirection == LTR) {
opt.rect.setLeft(-width());
} else {
opt.rect.setWidth(width() * 2);
}
painter.drawPrimitive(QStyle::PE_PanelButtonTool, opt);
QStyleOptionToolButton opt2;
initStyleOption(&opt2);
painter.drawControl(QStyle::CE_ToolButtonLabel, opt2);
}
void SplitterCollapser::showEvent(QShowEvent*) {
d->updateArrow();
}
} // namespace
#include "gwenview_splittercollapser.moc"