-
-
Notifications
You must be signed in to change notification settings - Fork 557
/
Copy pathscrollarea.cpp
127 lines (102 loc) · 3.88 KB
/
scrollarea.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
// This file is a part of "grblControl" application.
// Copyright 2015 Hayrullin Denis Ravilevich
#include <QDebug>
#include <QLayout>
#include <QScrollBar>
#include <QTimer>
#include <QMouseEvent>
#include <QStyle>
#include "scrollarea.h"
ScrollArea::ScrollArea(QWidget *parent) : QScrollArea(parent)
{
m_update = false;
m_width = 0;
this->setStyleSheet("QScrollArea {border-top: 2px solid transparent; border-bottom: 2px solid transparent}\
QScrollArea[topBorder=\"true\"] {border-top: 2px solid qlineargradient(spread:pad, x1:0, y1:1, x2:0, y2:0, stop:1 #D5DFE5, stop:0 white);}\
QScrollArea[bottomBorder=\"true\"] {border-bottom: 2px solid qlineargradient(spread:pad, x1:0, y1:1, x2:0, y2:0, stop:1 #D5DFE5, stop:0 white);}");
this->verticalScrollBar()->setStyleSheet("QScrollBar:vertical {border: none; width: 2px; padding-top: 8px;}\
QScrollBar::handle:vertical {background: darkgray;}\
QScrollBar::add-line:vertical {border: none; background: none; height: 0px;}\
QScrollBar::sub-line:vertical {border: none; background: none; height: 0px;}");
connect(this->verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(onVerticalScrollBarValueChanged(int)));
}
QSize ScrollArea::sizeHint() const
{
QSize hint = widget()->sizeHint();
hint.setHeight(hint.height() + 4); // top + bottom border width
hint.setWidth(m_width);
return hint;
}
void ScrollArea::setWidget(QWidget *widget)
{
connect(static_cast<Widget*>(widget), SIGNAL(sizeChanged(QSize)), this, SLOT(onContentSizeChanged(QSize)));
QScrollArea::setWidget(widget);
}
void ScrollArea::updateMinimumWidth()
{
m_width = 0;
QList<GroupBox*> list = this->widget()->findChildren<GroupBox*>();
foreach (GroupBox *box, list) {
connect(box, SIGNAL(mouseMoved(int,int)), this, SLOT(onScroll(int,int)));
connect(box, SIGNAL(mousePressed()), this, SLOT(onPressed()));
m_width = qMax<int>(m_width, box->sizeHint().width() + box->layout()->contentsMargins().left() + box->layout()->contentsMargins().right()); // 1 * margin
}
onContentSizeChanged(QSize());
}
void ScrollArea::resizeEvent(QResizeEvent *re)
{
QScrollArea::resizeEvent(re);
updateBorders();
}
void ScrollArea::mouseMoveEvent(QMouseEvent *me)
{
QScrollArea::mouseMoveEvent(me);
if (!m_pressedPos.isNull()) {
QPoint delta = me->globalPos() - m_pressedPos;
onScroll(delta.x(), delta.y());
}
}
void ScrollArea::mousePressEvent(QMouseEvent *me)
{
QScrollArea::mousePressEvent(me);
m_pressedPos = me->globalPos();
m_pressedValue = this->verticalScrollBar()->value();
}
void ScrollArea::mouseReleaseEvent(QMouseEvent *me)
{
QScrollArea::mouseReleaseEvent(me);
m_pressedPos = QPoint();
}
void ScrollArea::onContentSizeChanged(QSize newSize)
{
this->widget()->setMinimumWidth(m_width);
this->updateGeometry();
}
void ScrollArea::onVerticalScrollBarValueChanged(int newValue)
{
updateBorders();
}
void ScrollArea::onScroll(int dx, int dy)
{
QScrollBar *bar = this->verticalScrollBar();
int delta = (double)dy / (this->sizeHint().height() - this->height()) * (bar->maximum() - bar->minimum());
bar->setValue(m_pressedValue - delta);
}
void ScrollArea::onPressed()
{
m_pressedValue = this->verticalScrollBar()->value();
}
void ScrollArea::updateBorders()
{
// Performance issue on changing stylesheet
#ifdef GLES
return;
#else
QScrollBar* bar = this->verticalScrollBar();
bool fitted = this->geometry().height() > this->widget()->sizeHint().height();
this->setProperty("topBorder", bar->value() > bar->minimum() && !fitted);
this->setProperty("bottomBorder", bar->value() < bar->maximum() && !fitted);
style()->unpolish(this);
this->ensurePolished();
#endif
}