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

Last change on this file since 15 was 15, checked in by Bayard Gildas, 9 years ago

sources reformatted with flair-format-dir script

File size: 3.8 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) : QScrollBar(parent) { init(); }
10
11ScrollBar::ScrollBar(Qt::Orientation o, QWidget *parent)
12 : QScrollBar(o, parent) {
13 init();
14}
15
16ScrollBar::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
24void 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
35void ScrollBar::setInverted(bool inverted) {
36 if (d_inverted != inverted) {
37 d_inverted = inverted;
38 moveSlider(minSliderValue(), maxSliderValue());
39 }
40}
41
42bool ScrollBar::isInverted(void) const { return d_inverted; }
43
44void 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
53void 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
79float ScrollBar::minBaseValue(void) const { return d_minBase; }
80
81float ScrollBar::maxBaseValue(void) const { return d_maxBase; }
82
83void 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
93float ScrollBar::minSliderValue(void) const {
94 float min, dummy;
95 sliderRange(value(), min, dummy);
96
97 return min;
98}
99
100float ScrollBar::maxSliderValue(void) const {
101 float max, dummy;
102 sliderRange(value(), dummy, max);
103
104 return max;
105}
106
107int ScrollBar::mapToTick(float v) const {
108 return (int)((v - d_minBase) / (d_maxBase - d_minBase) * d_baseTicks);
109}
110
111float ScrollBar::mapFromTick(int tick) const {
112 return d_minBase + (d_maxBase - d_minBase) * tick / d_baseTicks;
113}
114
115void ScrollBar::catchValueChanged(int value) {
116 float min, max;
117 sliderRange(value, min, max);
118 Q_EMIT valueChanged(orientation(), min, max);
119}
120
121void ScrollBar::catchSliderMoved(int value) {
122 float min, max;
123 sliderRange(value, min, max);
124 Q_EMIT sliderMoved(orientation(), min, max);
125}
126
127int 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}
Note: See TracBrowser for help on using the repository browser.