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

Last change on this file since 10 was 10, checked in by Sanahuja Guillaume, 6 years ago

lic

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