File: sliders.cpp

package info (click to toggle)
qwt5 5.2.2-3
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 13,340 kB
  • sloc: cpp: 32,645; makefile: 23; sh: 4
file content (201 lines) | stat: -rw-r--r-- 5,475 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <math.h>
#include <qapplication.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qwt_slider.h>
#include <qwt_scale_engine.h>
#include <qwt_scale_map.h>
#include "sliders.h"

class Layout: public QBoxLayout
{
public:
    Layout(Qt::Orientation o, QWidget *parent = NULL):
#if QT_VERSION < 0x040000
        QBoxLayout(parent, QBoxLayout::LeftToRight)
#else
        QBoxLayout(QBoxLayout::LeftToRight, parent)
#endif
    {
        if ( o == Qt::Vertical )
            setDirection(QBoxLayout::TopToBottom);

        setSpacing(20);
        setMargin(0);
    }
};

Slider::Slider(QWidget *parent, int sliderType):
    QWidget(parent)
{
    d_slider = createSlider(this, sliderType);

#if QT_VERSION < 0x040000
    int alignment = Qt::AlignCenter;
#else
    QFlags<Qt::AlignmentFlag> alignment;
#endif
    switch(d_slider->scalePosition())
    {
        case QwtSlider::NoScale:
            if ( d_slider->orientation() == Qt::Horizontal )
                alignment = Qt::AlignHCenter | Qt::AlignTop;
            else
                alignment = Qt::AlignVCenter | Qt::AlignLeft;
            break;
        case QwtSlider::LeftScale:
            alignment = Qt::AlignVCenter | Qt::AlignRight;
            break;
        case QwtSlider::RightScale:
            alignment = Qt::AlignVCenter | Qt::AlignLeft;
            break;
        case QwtSlider::TopScale:
            alignment = Qt::AlignHCenter | Qt::AlignBottom;
            break;
        case QwtSlider::BottomScale:
            alignment = Qt::AlignHCenter | Qt::AlignTop;
            break;
    }

    d_label = new QLabel("0", this);
    d_label->setAlignment(alignment);
    d_label->setFixedWidth(d_label->fontMetrics().width("10000.9"));

    connect(d_slider, SIGNAL(valueChanged(double)), SLOT(setNum(double)));

    QBoxLayout *layout;
    if ( d_slider->orientation() == Qt::Horizontal )
        layout = new QHBoxLayout(this);
    else
        layout = new QVBoxLayout(this);

    layout->addWidget(d_slider);
    layout->addWidget(d_label);
}

QwtSlider *Slider::createSlider(QWidget *parent, int sliderType) const
{
    QwtSlider *slider = NULL;

    switch(sliderType)
    {
        case 0:
        {
            slider = new QwtSlider(parent, 
                Qt::Horizontal, QwtSlider::TopScale, QwtSlider::BgTrough);
            slider->setThumbWidth(10);
            slider->setRange(-10.0, 10.0, 1.0, 0); // paging disabled
            break;
        }
        case 1:
        {
            slider = new QwtSlider(parent, 
                Qt::Horizontal, QwtSlider::NoScale, QwtSlider::BgBoth);
            slider->setRange(0.0, 1.0, 0.01, 5);
            break;
        }
        case 2:
        {
            slider = new QwtSlider(parent, 
                Qt::Horizontal, QwtSlider::BottomScale, QwtSlider::BgSlot);
            slider->setThumbWidth(25);
            slider->setThumbLength(12);
            slider->setRange(1000.0, 3000.0, 10.0, 10);
            break;
        }
        case 3:
        {
            slider = new QwtSlider(parent, 
                Qt::Vertical, QwtSlider::LeftScale, QwtSlider::BgSlot);
            slider->setRange(0.0, 100.0, 1.0, 5);
            slider->setScaleMaxMinor(5);
            break;
        }
        case 4:
        {
            slider = new QwtSlider(parent, 
                Qt::Vertical, QwtSlider::NoScale, QwtSlider::BgTrough);
            slider->setRange(0.0,100.0,1.0, 10);
            break;
        }
        case 5:
        {
            slider = new QwtSlider(parent, 
                Qt::Vertical, QwtSlider::RightScale, QwtSlider::BgBoth);
            slider->setScaleEngine(new QwtLog10ScaleEngine);
            slider->setThumbWidth(20);
            slider->setBorderWidth(1);
            slider->setRange(0.0, 4.0, 0.01);
            slider->setScale(1.0, 1.0e4);
            slider->setScaleMaxMinor(10);
            break;
        }
    }

    return slider;
}

void Slider::setNum(double v)
{
    if ( d_slider->scaleMap().transformation()->type() ==
        QwtScaleTransformation::Log10 )
    {
        v = pow(10.0, v);
    }

    QString text;
    text.setNum(v, 'f', 1);

    d_label->setText(text);
}

SliderDemo::SliderDemo(QWidget *p): 
    QWidget(p)
{
    int i;

    Layout *hSliderLayout = new Layout(Qt::Vertical);
    for ( i = 0; i < 3; i++ )
        hSliderLayout->addWidget(new Slider(this, i));
    hSliderLayout->addStretch();

    Layout *vSliderLayout = new Layout(Qt::Horizontal);
    for ( ; i < 6; i++ )
        vSliderLayout->addWidget(new Slider(this, i));

    QLabel *vTitle = new QLabel("Vertical Sliders", this);
    vTitle->setFont(QFont("Helvetica", 14, QFont::Bold));
    vTitle->setAlignment(Qt::AlignHCenter);

    Layout *layout1 = new Layout(Qt::Vertical);
    layout1->addWidget(vTitle, 0);
    layout1->addLayout(vSliderLayout, 10);

    QLabel *hTitle = new QLabel("Horizontal Sliders", this);
    hTitle->setFont(vTitle->font());
    hTitle->setAlignment(Qt::AlignHCenter);

    Layout *layout2 = new Layout(Qt::Vertical);
    layout2->addWidget(hTitle, 0);
    layout2->addLayout(hSliderLayout, 10);

    Layout *mainLayout = new Layout(Qt::Horizontal, this);
    mainLayout->addLayout(layout1);
    mainLayout->addLayout(layout2, 10);
}

int main (int argc, char **argv)
{
    QApplication a(argc, argv);

    QApplication::setFont(QFont("Helvetica",10));

    SliderDemo w;

#if QT_VERSION < 0x040000
    a.setMainWidget(&w);
#endif
    w.show();

    return a.exec();
}