source: flair-src/trunk/tools/FlairGCS/src/Scrollbar.cpp@ 9

Last change on this file since 9 was 9, checked in by Sanahuja Guillaume, 7 years ago

gcs

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