1 | // %flair:license{
|
---|
2 | // This file is part of the Flair framework distributed under the
|
---|
3 | // CECILL-C License, Version 1.0.
|
---|
4 | // %flair:license}
|
---|
5 | #include <qstyle.h>
|
---|
6 | #include <qstyleoption.h>
|
---|
7 | #include "Scrollbar.h"
|
---|
8 |
|
---|
9 | ScrollBar::ScrollBar(QWidget *parent) : QScrollBar(parent) { init(); }
|
---|
10 |
|
---|
11 | ScrollBar::ScrollBar(Qt::Orientation o, QWidget *parent)
|
---|
12 | : QScrollBar(o, parent) {
|
---|
13 | init();
|
---|
14 | }
|
---|
15 |
|
---|
16 | ScrollBar::ScrollBar(float minBase, float maxBase, Qt::Orientation o,
|
---|
17 | QWidget *parent)
|
---|
18 | : QScrollBar(o, parent) {
|
---|
19 | init();
|
---|
20 | setBase(minBase, maxBase);
|
---|
21 | moveSlider(minBase, maxBase);
|
---|
22 | }
|
---|
23 |
|
---|
24 | void ScrollBar::init(void) {
|
---|
25 | d_inverted = orientation() == Qt::Vertical;
|
---|
26 | d_baseTicks = 1000000;
|
---|
27 | d_minBase = 0.0;
|
---|
28 | d_maxBase = 1.0;
|
---|
29 | moveSlider(d_minBase, d_maxBase);
|
---|
30 |
|
---|
31 | connect(this, SIGNAL(sliderMoved(int)), SLOT(catchSliderMoved(int)));
|
---|
32 | connect(this, SIGNAL(valueChanged(int)), SLOT(catchValueChanged(int)));
|
---|
33 | }
|
---|
34 |
|
---|
35 | void ScrollBar::setInverted(bool inverted) {
|
---|
36 | if (d_inverted != inverted) {
|
---|
37 | d_inverted = inverted;
|
---|
38 | moveSlider(minSliderValue(), maxSliderValue());
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | bool ScrollBar::isInverted(void) const { return d_inverted; }
|
---|
43 |
|
---|
44 | void ScrollBar::setBase(float min, float max) {
|
---|
45 | if (min != d_minBase || max != d_maxBase) {
|
---|
46 | d_minBase = min;
|
---|
47 | d_maxBase = max;
|
---|
48 |
|
---|
49 | moveSlider(minSliderValue(), maxSliderValue());
|
---|
50 | }
|
---|
51 | }
|
---|
52 |
|
---|
53 | void ScrollBar::moveSlider(float min, float max) {
|
---|
54 | const int sliderTicks =
|
---|
55 | qRound((max - min) / (d_maxBase - d_minBase) * d_baseTicks);
|
---|
56 |
|
---|
57 | // setRange initiates a valueChanged of the scrollbars
|
---|
58 | // in some situations. So we block
|
---|
59 | // and unblock the signals.
|
---|
60 |
|
---|
61 | blockSignals(true);
|
---|
62 |
|
---|
63 | setRange(sliderTicks / 2, d_baseTicks - sliderTicks / 2);
|
---|
64 | int steps = sliderTicks / 200;
|
---|
65 | if (steps <= 0)
|
---|
66 | steps = 1;
|
---|
67 |
|
---|
68 | setSingleStep(steps);
|
---|
69 | setPageStep(sliderTicks);
|
---|
70 |
|
---|
71 | int tick = mapToTick(min + (max - min) / 2);
|
---|
72 | if (isInverted())
|
---|
73 | tick = d_baseTicks - tick;
|
---|
74 |
|
---|
75 | setSliderPosition(tick);
|
---|
76 | blockSignals(false);
|
---|
77 | }
|
---|
78 |
|
---|
79 | float ScrollBar::minBaseValue(void) const { return d_minBase; }
|
---|
80 |
|
---|
81 | float ScrollBar::maxBaseValue(void) const { return d_maxBase; }
|
---|
82 |
|
---|
83 | void ScrollBar::sliderRange(int value, float &min, float &max) const {
|
---|
84 | if (isInverted())
|
---|
85 | value = d_baseTicks - value;
|
---|
86 |
|
---|
87 | const int visibleTicks = pageStep();
|
---|
88 |
|
---|
89 | min = mapFromTick(value - visibleTicks / 2);
|
---|
90 | max = mapFromTick(value + visibleTicks / 2);
|
---|
91 | }
|
---|
92 |
|
---|
93 | float ScrollBar::minSliderValue(void) const {
|
---|
94 | float min, dummy;
|
---|
95 | sliderRange(value(), min, dummy);
|
---|
96 |
|
---|
97 | return min;
|
---|
98 | }
|
---|
99 |
|
---|
100 | float ScrollBar::maxSliderValue(void) const {
|
---|
101 | float max, dummy;
|
---|
102 | sliderRange(value(), dummy, max);
|
---|
103 |
|
---|
104 | return max;
|
---|
105 | }
|
---|
106 |
|
---|
107 | int ScrollBar::mapToTick(float v) const {
|
---|
108 | return (int)((v - d_minBase) / (d_maxBase - d_minBase) * d_baseTicks);
|
---|
109 | }
|
---|
110 |
|
---|
111 | float ScrollBar::mapFromTick(int tick) const {
|
---|
112 | return d_minBase + (d_maxBase - d_minBase) * tick / d_baseTicks;
|
---|
113 | }
|
---|
114 |
|
---|
115 | void ScrollBar::catchValueChanged(int value) {
|
---|
116 | float min, max;
|
---|
117 | sliderRange(value, min, max);
|
---|
118 | Q_EMIT valueChanged(orientation(), min, max);
|
---|
119 | }
|
---|
120 |
|
---|
121 | void ScrollBar::catchSliderMoved(int value) {
|
---|
122 | float min, max;
|
---|
123 | sliderRange(value, min, max);
|
---|
124 | Q_EMIT sliderMoved(orientation(), min, max);
|
---|
125 | }
|
---|
126 |
|
---|
127 | int ScrollBar::extent(void) const {
|
---|
128 | QStyleOptionSlider opt;
|
---|
129 | opt.init(this);
|
---|
130 | opt.subControls = QStyle::SC_None;
|
---|
131 | opt.activeSubControls = QStyle::SC_None;
|
---|
132 | opt.orientation = orientation();
|
---|
133 | opt.minimum = minimum();
|
---|
134 | opt.maximum = maximum();
|
---|
135 | opt.sliderPosition = sliderPosition();
|
---|
136 | opt.sliderValue = value();
|
---|
137 | opt.singleStep = singleStep();
|
---|
138 | opt.pageStep = pageStep();
|
---|
139 | opt.upsideDown = invertedAppearance();
|
---|
140 | if (orientation() == Qt::Horizontal)
|
---|
141 | opt.state |= QStyle::State_Horizontal;
|
---|
142 | return style()->pixelMetric(QStyle::PM_ScrollBarExtent, &opt, this);
|
---|
143 | }
|
---|