/
scrollareas.cpp
413 lines (359 loc) · 15 KB
/
scrollareas.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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
* Virtuality Style for Qt4 and Qt5
* Copyright 2009-2014 by Thomas Lübking <thomas.luebking@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
*
* This program 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 General Public License for more details
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <QAbstractScrollArea>
#include <QApplication>
#include <QScrollBar>
#include <QTimer>
#include <QtDebug>
#include "draw.h"
#include "animator/hover.h"
#include "animator/hovercomplex.h"
#define MAX_STEPS Animator::Hover::maxSteps()
static uchar gs_scrollAreaState = 0;
enum ScrollAreaStates { ComboDropDown = 1<<0, Hovered = 1<<1, HasFocus = 1<<2 };
inline static void
detectScrollAreaStates(const QWidget* slider)
{
gs_scrollAreaState &= ~(Hovered|HasFocus);
if (!slider) {
gs_scrollAreaState |= Hovered;
return;
}
QWidget *scrollWidget = const_cast<QWidget*>(slider);
if (!scrollWidget->isEnabled())
return;
while (scrollWidget && !(qobject_cast<QAbstractScrollArea*>(scrollWidget) || Animator::Hover::managesArea(scrollWidget)))
scrollWidget = const_cast<QWidget*>(scrollWidget->parentWidget());
if (scrollWidget) {
if (scrollWidget->hasFocus())
gs_scrollAreaState |= HasFocus;
if (scrollWidget->underMouse()) {
if (scrollWidget->geometry().y() < -400000) { // khtmlview and some other widget set off a render dummy
gs_scrollAreaState |= Hovered; // by -500000px, leading to a weird global position
} else {
QPoint tl = scrollWidget->mapToGlobal(QPoint(0,0));
QRegion scrollArea(tl.x(), tl.y(), scrollWidget->width(), scrollWidget->height());
QList<QAbstractScrollArea*> scrollChilds = scrollWidget->findChildren<QAbstractScrollArea*>();
for (int i = 0; i < scrollChilds.size(); ++i) {
if (scrollChilds[i]->testAttribute(Qt::WA_TransparentForMouseEvents))
continue;
if (scrollChilds[i]->geometry().y() < -400000)
continue; // see above ... D'OHHH
int d = 0;
if (QScrollBar *bar = scrollChilds[i]->verticalScrollBar())
d += bar->maximum() - bar->minimum();
if (QScrollBar *bar = scrollChilds[i]->horizontalScrollBar())
d += bar->maximum() - bar->minimum();
if (!d)
continue;
QPoint tl = scrollChilds[i]->mapToGlobal(QPoint(0,0));
scrollArea -= QRegion(tl.x(), tl.y(), scrollChilds[i]->width(), scrollChilds[i]->height());
}
if (scrollArea.contains(QCursor::pos()))
gs_scrollAreaState |= Hovered;
}
}
} else {
gs_scrollAreaState |= Hovered;
}
}
#define PAINT_ELEMENT(_E_)\
if (scrollbar->subControls & SC_ScrollBar##_E_)\
{\
optCopy.rect = scrollbar->rect;\
optCopy.state = saveFlags;\
optCopy.rect = subControlRect(CC_ScrollBar, &optCopy, SC_ScrollBar##_E_, widget);\
if (optCopy.rect.isValid())\
{\
if (!(scrollbar->activeSubControls & SC_ScrollBar##_E_))\
optCopy.state &= ~(State_Sunken | State_MouseOver);\
if (info && (info->fades[Animator::In] & SC_ScrollBar##_E_ ||\
info->fades[Animator::Out] & SC_ScrollBar##_E_))\
complexStep = info->step(SC_ScrollBar##_E_);\
else \
complexStep = 0; \
drawScrollBar##_E_(&optCopy, cPainter, widget);\
}\
}//
static int complexStep, widgetStep;
static QPixmap *scrollBgCache = 0;
static QTimer cacheCleaner;
const static QWidget *cachedScroller = 0;
static QPainter *cPainter = 0;
void
Style::clearScrollbarCache()
{
cacheCleaner.stop(); cachedScroller = 0L;
delete scrollBgCache; scrollBgCache = 0L;
}
enum SA_Flags { WebKit = 1, ComboBox = 2 };
static const QWidget *last_widget = 0;
static int last_flags = 0;
static void updateLastWidget( const QWidget *widget )
{
if (widget != last_widget)
{
last_widget = widget;
last_flags = 0;
if (widget->inherits("QWebView"))
last_flags |= WebKit;
else if (widget->testAttribute(Qt::WA_OpaquePaintEvent) &&
widget->parentWidget() &&
widget->parentWidget()->parentWidget() &&
widget->parentWidget()->parentWidget()->inherits("QComboBoxListView"))
last_flags |= ComboBox;
}
}
void
Style::drawScrollAreaCorner(const QStyleOption *option, QPainter *painter, const QWidget *widget) const
{
// ouchhh...!
if (widget)
{
updateLastWidget(widget);
if (last_flags & WebKit)
erase(option, painter, widget);
}
}
void
Style::drawScrollBar(const QStyleOptionComplex *option, QPainter *painter, const QWidget *widget) const
{
ASSURE_OPTION(scrollbar, Slider);
SAVE_PAINTER(Pen|Brush|Alias);
cPainter = painter;
bool useCache = false, needsPaint = true;
bool isWebKit = false; // ouchhh... needs some specials
if (widget)
{
updateLastWidget(widget);
if ((isWebKit = last_flags & WebKit))
{
const bool isFrame = RECT.height() == widget->height() || RECT.height() == widget->height() - RECT.width() ||
RECT.width() == widget->width() || RECT.width() == widget->width() - RECT.height();
if (isFrame)
last_flags &= ~ComboBox;
else
last_flags |= ComboBox;
}
}
// we paint the slider bg ourselves, as otherwise a frame repaint would be
// triggered (for no sense)
if (!widget) { // fallback ===========
#if QT_VERSION >= 0x050000
if (!scrollbar->styleObject ||
scrollbar->styleObject->isWidgetType()) // not required for graphicsitems and quickstyleitems
#endif
painter->fillRect(RECT, FCOLOR(Window));
} else if (isWebKit || widget->testAttribute(Qt::WA_OpaquePaintEvent))
{ /// fake a transparent bg (real transparency leads to frame22 painting overhead)
// i.e. we erase the bg with the window background or any autofilled element between
if ( last_flags & ComboBox )
{ /// catch combobox dropdowns ==========
painter->fillRect(RECT, PAL.brush(QPalette::Base));
gs_scrollAreaState |= ComboDropDown;
}
else
{ /// default scrollbar ===============
gs_scrollAreaState &= ~ComboDropDown;
if (option->state & State_Sunken)
{ /// use caching for sliding scrollers to gain speed
useCache = true;
if (widget != cachedScroller)
{ // update cache
cachedScroller = widget;
delete scrollBgCache; scrollBgCache = 0L;
}
if (!scrollBgCache || scrollBgCache->size() != RECT.size())
{ // we need a new cache pixmap
cacheCleaner.disconnect();
connect(&cacheCleaner, SIGNAL(timeout()), this, SLOT(clearScrollbarCache()));
delete scrollBgCache;
scrollBgCache = new QPixmap(RECT.size());
cPainter = new QPainter(scrollBgCache);
}
else
needsPaint = false;
}
if (needsPaint)
{
QPoint offset(0,0);
if (isWebKit && (option->state & QStyle::State_Horizontal))
offset.setY(RECT.height() - widget->height());
erase(option, cPainter, widget, &offset);
}
}
}
// =================
//BEGIN real scrollbar painting -
// Make a copy here and reset it for each primitive.
QStyleOptionSlider optCopy = *scrollbar;
if (isWebKit)
optCopy.palette = QApplication::palette();
State saveFlags = optCopy.state;
if (scrollbar->minimum == scrollbar->maximum)
saveFlags &= ~State_Enabled; // there'd be nothing to scroll anyway...
/// hover animation =================
detectScrollAreaStates(widget);
if (isWebKit && widget && widget->hasFocus()) {
gs_scrollAreaState |= HasFocus;
}
if (scrollbar->activeSubControls & SC_ScrollBarSlider) {
widgetStep = 0; gs_scrollAreaState |= Hovered;
} else {
widgetStep = Animator::Hover::step(widget);
if (isWebKit)
gs_scrollAreaState &= ~Hovered;
}
SubControls hoverControls = scrollbar->activeSubControls &
(SC_ScrollBarSubLine | SC_ScrollBarAddLine | SC_ScrollBarSlider);
const Animator::ComplexInfo *info = isWebKit ? 0L : Animator::HoverComplex::info(widget, hoverControls);
/// ================
if (cPainter != painter) // unwrap cache painter
{ cPainter->end(); delete cPainter; cPainter = painter; }
/// Background and groove have been painted
if (useCache) //flush the cache
{ cacheCleaner.start(1000);
painter->drawPixmap(RECT.topLeft(), *scrollBgCache);
}
if (config.slider.buttons)
{ // nasty useless "click-to-scroll-one-single-line" buttons
PAINT_ELEMENT(SubLine);
PAINT_ELEMENT(AddLine);
}
if ((saveFlags & State_Enabled) && (scrollbar->subControls & SC_ScrollBarSlider))
{
optCopy.rect = scrollbar->rect;
optCopy.state = saveFlags;
optCopy.rect = subControlRect(CC_ScrollBar, &optCopy, SC_ScrollBarSlider, widget);
optCopy.rect.adjust(F(2),F(2),-F(2),-F(2));
if (optCopy.rect.isValid())
{
if (!(scrollbar->activeSubControls & SC_ScrollBarSlider))
optCopy.state &= ~(State_Sunken | State_MouseOver);
if (scrollbar->state & State_HasFocus)
optCopy.state |= (State_Sunken | State_MouseOver);
if (info && ( (info->fades[Animator::In] & SC_ScrollBarSlider) ||
(info->fades[Animator::Out] & SC_ScrollBarSlider) ))
complexStep = info->step(SC_ScrollBarSlider);
else
complexStep = 0;
drawScrollBarSlider(&optCopy, cPainter, widget);
}
}
gs_scrollAreaState = 0;
widgetStep = complexStep = 0;
RESTORE_PAINTER
}
#undef PAINT_ELEMENT
void
Style::drawScrollBarButton(const QStyleOption *option, QPainter *painter, const QWidget*, bool up) const
{
ASSURE_OPTION(opt, Slider);
SAVE_PAINTER(Pen|Brush|Alias);
const Navi::Direction dir = (option->state & QStyle::State_Horizontal) ? (up?Navi::W:Navi::E) : (up?Navi::N:Navi::S);
if (gs_scrollAreaState & ComboDropDown) { // gets a classic triangular look and is allways shown
OPT_HOVER
painter->setPen(Qt::NoPen);
painter->setBrush(hover ? FCOLOR(Text) : FX::blend(FCOLOR(Base), FCOLOR(Text)));
// const int dx = RECT.width()/4, dy = RECT.height()/4;
// QRect rect = RECT.adjusted(dx, dy, -dx, -dy);
drawArrow(dir, RECT, painter);
RESTORE_PAINTER
return;
}
if (!config.slider.buttons)
return;
OPT_ENABLED OPT_HOVER
bool alive = isEnabled && // visually inactivate if an extreme position is reached
((up && opt->sliderValue > opt->minimum) || (!up && opt->sliderValue < opt->maximum));
hover = hover && alive;
const int step = !alive ? 0 : ((hover && !complexStep) ? MAX_STEPS : complexStep);
const QColor c = FX::blend(FCOLOR(Window), FCOLOR(WindowText), MAX_STEPS-step, MAX_STEPS);
painter->setRenderHint(QPainter::Antialiasing);
painter->setPen(Qt::NoPen);
painter->setBrush(c);
painter->drawEllipse(RECT.adjusted(F(1),F(1),-F(1),-F(1)));
RESTORE_PAINTER
}
void
Style::drawScrollBarSlider(const QStyleOption *option, QPainter *painter, const QWidget *widget) const
{
OPT_SUNKEN OPT_ENABLED OPT_HOVER
const bool horizontal = option->state & QStyle::State_Horizontal ||
// at least opera doesn't propagate this
RECT.width() > RECT.height();
if (gs_scrollAreaState & ComboDropDown) { //gets a special slimmer and simpler look
SAVE_PAINTER(Pen|Brush|Alias);
QRect r = RECT;
if (horizontal) {
const int d = RECT.height()/3;
r.adjust(F(2), d, -F(2), -d);
} else {
const int d = RECT.width()/3;
r.adjust(d, F(2), -d, -F(2));
}
painter->setPen(Qt::NoPen);
painter->setRenderHint(QPainter::Antialiasing);
if (sunken || (hover && !complexStep))
complexStep = MAX_STEPS;
painter->setBrush(FX::blend(FCOLOR(Base), FCOLOR(Text), MAX_STEPS-complexStep, complexStep+1));
painter->drawRoundedRect(r, F(4), F(4));
RESTORE_PAINTER
return;
}
if (!isEnabled)
return;
// --> we need to paint a slider
SAVE_PAINTER(Pen|Brush|Alias);
// COLOR: the hover indicator (inside area)
#define SCROLL_COLOR(_X_) (widgetStep ? FX::blend( bgC, fgC, MAX_STEPS - _X_, _X_) : bgC)
if ((gs_scrollAreaState & Hovered) && !widgetStep)
widgetStep = MAX_STEPS;
QColor c, bgC = FCOLOR(Window),
fgC = (gs_scrollAreaState & HasFocus) ? FCOLOR(Highlight) : FCOLOR(WindowText);
if ( widget && widget->isActiveWindow() ) {
if (!widgetStep)
widgetStep = 2;
else if (!(hover || (gs_scrollAreaState & Hovered)))
widgetStep = qMax(2, widgetStep);
}
if (sunken) {
c = SCROLL_COLOR(MAX_STEPS);
complexStep = MAX_STEPS;
} else if (complexStep) {
c = FX::blend(bgC, SCROLL_COLOR(widgetStep));
c = FX::blend(c, SCROLL_COLOR(complexStep), MAX_STEPS-complexStep, complexStep);
} else if (hover) {
complexStep = MAX_STEPS; c = SCROLL_COLOR(MAX_STEPS);
} else if (widgetStep) {
c = FX::blend(bgC, SCROLL_COLOR(widgetStep));
} else {
c = bgC;
}
c.setAlpha(255); // bg could be transparent, i don't want scrollers translucent, though.
#undef SCROLL_COLOR
painter->setPen(Qt::NoPen);
painter->setBrush(c);
painter->setRenderHint(QPainter::Antialiasing, true);
const int radius = qMin(RECT.width(), RECT.height()) / 2;
painter->drawRoundedRect(RECT, radius, radius);
painter->setRenderHint(QPainter::Antialiasing, false);
RESTORE_PAINTER
}
// case CE_ScrollBarFirst: // Scroll bar first line indicator (i.e., home).
// case CE_ScrollBarLast: // Scroll bar last line indicator (i.e., end).