1 | /*
|
---|
2 | * QSeisScrollbar.cpp
|
---|
3 | *
|
---|
4 | * taken from "realtime"-example
|
---|
5 | *
|
---|
6 | */
|
---|
7 |
|
---|
8 | // include config.h
|
---|
9 | #ifdef HAVE_CONFIG_H
|
---|
10 | #include <config.h>
|
---|
11 | #endif
|
---|
12 |
|
---|
13 | #include <qstyle.h>
|
---|
14 | #if QT_VERSION >= 0x040000
|
---|
15 | #include <qstyleoption.h>
|
---|
16 | #endif
|
---|
17 | #include "ScrollBar.hpp"
|
---|
18 |
|
---|
19 |
|
---|
20 | // have this after(!) all Qt includes
|
---|
21 | #include "CodePatterns/MemDebug.hpp"
|
---|
22 |
|
---|
23 | ScrollBar::ScrollBar(QWidget * parent):
|
---|
24 | QScrollBar(parent)
|
---|
25 | {
|
---|
26 | init();
|
---|
27 | }
|
---|
28 |
|
---|
29 | ScrollBar::ScrollBar(Qt::Orientation o,
|
---|
30 | QWidget *parent):
|
---|
31 | QScrollBar(o, parent)
|
---|
32 | {
|
---|
33 | init();
|
---|
34 | }
|
---|
35 |
|
---|
36 | ScrollBar::ScrollBar(double minBase, double maxBase,
|
---|
37 | Qt::Orientation o, QWidget *parent):
|
---|
38 | QScrollBar(o, parent)
|
---|
39 | {
|
---|
40 | init();
|
---|
41 | setBase(minBase, maxBase);
|
---|
42 | moveSlider(minBase, maxBase);
|
---|
43 | }
|
---|
44 |
|
---|
45 | void ScrollBar::init()
|
---|
46 | {
|
---|
47 | d_inverted = orientation() == Qt::Vertical;
|
---|
48 | d_baseTicks = 1000000;
|
---|
49 | d_minBase = 0.0;
|
---|
50 | d_maxBase = 1.0;
|
---|
51 | moveSlider(d_minBase, d_maxBase);
|
---|
52 |
|
---|
53 | connect(this, SIGNAL(sliderMoved(int)), SLOT(catchSliderMoved(int)));
|
---|
54 | connect(this, SIGNAL(valueChanged(int)), SLOT(catchValueChanged(int)));
|
---|
55 | }
|
---|
56 |
|
---|
57 | void ScrollBar::setInverted(bool inverted)
|
---|
58 | {
|
---|
59 | if ( d_inverted != inverted )
|
---|
60 | {
|
---|
61 | d_inverted = inverted;
|
---|
62 | moveSlider(minSliderValue(), maxSliderValue());
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | bool ScrollBar::isInverted() const
|
---|
67 | {
|
---|
68 | return d_inverted;
|
---|
69 | }
|
---|
70 |
|
---|
71 | void ScrollBar::setBase(double min, double max)
|
---|
72 | {
|
---|
73 | if ( min != d_minBase || max != d_maxBase )
|
---|
74 | {
|
---|
75 | d_minBase = min;
|
---|
76 | d_maxBase = max;
|
---|
77 |
|
---|
78 | moveSlider(minSliderValue(), maxSliderValue());
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | void ScrollBar::moveSlider(double min, double max)
|
---|
83 | {
|
---|
84 | const int sliderTicks = qRound((max - min) /
|
---|
85 | (d_maxBase - d_minBase) * d_baseTicks);
|
---|
86 |
|
---|
87 | // setRange initiates a valueChanged of the scrollbars
|
---|
88 | // in some situations. So we block
|
---|
89 | // and unblock the signals.
|
---|
90 |
|
---|
91 | blockSignals(true);
|
---|
92 |
|
---|
93 | setRange(sliderTicks / 2, d_baseTicks - sliderTicks / 2);
|
---|
94 | int steps = sliderTicks / 200;
|
---|
95 | if ( steps <= 0 )
|
---|
96 | steps = 1;
|
---|
97 |
|
---|
98 | #if QT_VERSION < 0x040000
|
---|
99 | setSteps(steps, sliderTicks);
|
---|
100 | #else
|
---|
101 | setSingleStep(steps);
|
---|
102 | setPageStep(sliderTicks);
|
---|
103 | #endif
|
---|
104 |
|
---|
105 | int tick = mapToTick(min + (max - min) / 2);
|
---|
106 | if ( isInverted() )
|
---|
107 | tick = d_baseTicks - tick;
|
---|
108 |
|
---|
109 | #if QT_VERSION < 0x040000
|
---|
110 | directSetValue(tick);
|
---|
111 | rangeChange();
|
---|
112 | #else
|
---|
113 | setSliderPosition(tick);
|
---|
114 | #endif
|
---|
115 | blockSignals(false);
|
---|
116 | }
|
---|
117 |
|
---|
118 | double ScrollBar::minBaseValue() const
|
---|
119 | {
|
---|
120 | return d_minBase;
|
---|
121 | }
|
---|
122 |
|
---|
123 | double ScrollBar::maxBaseValue() const
|
---|
124 | {
|
---|
125 | return d_maxBase;
|
---|
126 | }
|
---|
127 |
|
---|
128 | void ScrollBar::sliderRange(int value, double &min, double &max) const
|
---|
129 | {
|
---|
130 | if ( isInverted() )
|
---|
131 | value = d_baseTicks - value;
|
---|
132 |
|
---|
133 | const int visibleTicks = pageStep();
|
---|
134 |
|
---|
135 | min = mapFromTick(value - visibleTicks / 2);
|
---|
136 | max = mapFromTick(value + visibleTicks / 2);
|
---|
137 | }
|
---|
138 |
|
---|
139 | double ScrollBar::minSliderValue() const
|
---|
140 | {
|
---|
141 | double min, dummy;
|
---|
142 | sliderRange(value(), min, dummy);
|
---|
143 |
|
---|
144 | return min;
|
---|
145 | }
|
---|
146 |
|
---|
147 | double ScrollBar::maxSliderValue() const
|
---|
148 | {
|
---|
149 | double max, dummy;
|
---|
150 | sliderRange(value(), dummy, max);
|
---|
151 |
|
---|
152 | return max;
|
---|
153 | }
|
---|
154 |
|
---|
155 | int ScrollBar::mapToTick(double v) const
|
---|
156 | {
|
---|
157 | return (int) ( ( v - d_minBase) / (d_maxBase - d_minBase ) * d_baseTicks );
|
---|
158 | }
|
---|
159 |
|
---|
160 | double ScrollBar::mapFromTick(int tick) const
|
---|
161 | {
|
---|
162 | return d_minBase + ( d_maxBase - d_minBase ) * tick / d_baseTicks;
|
---|
163 | }
|
---|
164 |
|
---|
165 | void ScrollBar::catchValueChanged(int value)
|
---|
166 | {
|
---|
167 | double min, max;
|
---|
168 | sliderRange(value, min, max);
|
---|
169 | emit valueChanged(orientation(), min, max);
|
---|
170 | }
|
---|
171 |
|
---|
172 | void ScrollBar::catchSliderMoved(int value)
|
---|
173 | {
|
---|
174 | double min, max;
|
---|
175 | sliderRange(value, min, max);
|
---|
176 | emit sliderMoved(orientation(), min, max);
|
---|
177 | }
|
---|
178 |
|
---|
179 | int ScrollBar::extent() const
|
---|
180 | {
|
---|
181 | #if QT_VERSION < 0x040000
|
---|
182 | return style().pixelMetric(QStyle::PM_ScrollBarExtent, this);
|
---|
183 | #else
|
---|
184 | QStyleOptionSlider opt;
|
---|
185 | opt.init(this);
|
---|
186 | opt.subControls = QStyle::SC_None;
|
---|
187 | opt.activeSubControls = QStyle::SC_None;
|
---|
188 | opt.orientation = orientation();
|
---|
189 | opt.minimum = minimum();
|
---|
190 | opt.maximum = maximum();
|
---|
191 | opt.sliderPosition = sliderPosition();
|
---|
192 | opt.sliderValue = value();
|
---|
193 | opt.singleStep = singleStep();
|
---|
194 | opt.pageStep = pageStep();
|
---|
195 | opt.upsideDown = invertedAppearance();
|
---|
196 | if (orientation() == Qt::Horizontal)
|
---|
197 | opt.state |= QStyle::State_Horizontal;
|
---|
198 | return style()->pixelMetric(QStyle::PM_ScrollBarExtent, &opt, this);
|
---|
199 | #endif
|
---|
200 | }
|
---|