File: mainwindow.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 (230 lines) | stat: -rw-r--r-- 6,492 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include <qlabel.h>
#include <qlayout.h>
#include <qstatusbar.h>
#include <qtoolbar.h>
#include <qtoolbutton.h>
#include <qspinbox.h>
#include <qwhatsthis.h>
#include <qpixmap.h>
#include "randomplot.h"
#include "mainwindow.h"
#include "start.xpm"
#include "clear.xpm"

class MyToolBar: public QToolBar
{
public:
    MyToolBar(MainWindow *parent):
        QToolBar(parent)
    {
    }
    void addSpacing(int spacing)
    {
        QLabel *label = new QLabel(this);
#if QT_VERSION >= 0x040000
        addWidget(label);
#endif
        label->setFixedWidth(spacing);
    }    
};

class Counter: public QWidget
{
public:
    Counter(QWidget *parent, 
            const QString &prefix, const QString &suffix,
            int min, int max, int step):
        QWidget(parent)
    {
        QHBoxLayout *layout = new QHBoxLayout(this);

        if ( !prefix.isEmpty() )
            layout->addWidget(new QLabel(prefix + " ", this));

#if QT_VERSION < 0x040000
        d_counter = new QSpinBox(min, max, step, this);
#else
        d_counter = new QSpinBox(this);
        d_counter->setRange(min, max);
        d_counter->setSingleStep(step);
#endif
        layout->addWidget(d_counter);

        if ( !suffix.isEmpty() )
            layout->addWidget(new QLabel(QString(" ") + suffix, this));
    }

    void setValue(int value) { d_counter->setValue(value); }
    int value() const { return d_counter->value(); }

private:
    QSpinBox *d_counter;
};

MainWindow::MainWindow()
{
#if QT_VERSION < 0x040000
    setDockEnabled(TornOff, true);
    setRightJustification(true);
#endif

    addToolBar(toolBar());
#ifndef QT_NO_STATUSBAR
    (void)statusBar();
#endif

    d_plot = new RandomPlot(this);
    d_plot->setMargin(4);

    setCentralWidget(d_plot);

#if QT_VERSION >= 0x040000
    connect(d_startAction, SIGNAL(toggled(bool)), this, SLOT(appendPoints(bool)));
    connect(d_clearAction, SIGNAL(triggered()), d_plot, SLOT(clear()));
#else
    connect(d_startBtn, SIGNAL(toggled(bool)), this, SLOT(appendPoints(bool)));
    connect(d_clearBtn, SIGNAL(clicked()), d_plot, SLOT(clear()));
#endif
    connect(d_plot, SIGNAL(running(bool)), this, SLOT(showRunning(bool)));

    initWhatsThis();

#if QT_VERSION >= 0x040000
    setContextMenuPolicy(Qt::NoContextMenu);
#endif
}

QToolBar *MainWindow::toolBar()
{
    MyToolBar *toolBar = new MyToolBar(this);

#if QT_VERSION >= 0x040000
    toolBar->setAllowedAreas(Qt::TopToolBarArea | Qt::BottomToolBarArea);
    setToolButtonStyle(Qt::ToolButtonTextUnderIcon);

    d_startAction = new QAction(QIcon(start_xpm), "Clear", toolBar);
    d_startAction->setCheckable(true);
    d_clearAction = new QAction(QIcon(clear_xpm), "Clear", toolBar);
    QAction *whatsThisAction = QWhatsThis::createAction(toolBar);
    whatsThisAction->setText("Help");

    toolBar->addAction(d_startAction);
    toolBar->addAction(d_clearAction);
    toolBar->addAction(whatsThisAction);

    setIconSize(QSize(22, 22));
#else
    d_startBtn = new QToolButton(toolBar);
    d_startBtn->setUsesTextLabel(true);
    d_startBtn->setPixmap(QPixmap(start_xpm));
    d_startBtn->setToggleButton(true);

    d_clearBtn = new QToolButton(toolBar);
    d_clearBtn->setUsesTextLabel(true);
    d_clearBtn->setPixmap(QPixmap(clear_xpm));
    d_clearBtn->setTextLabel("Clear", false);

    QToolButton *helpBtn = QWhatsThis::whatsThisButton(toolBar);
    helpBtn->setUsesTextLabel(true);
    helpBtn->setTextLabel("Help", false);
#endif

    QWidget *hBox = new QWidget(toolBar);

    d_randomCount = 
        new Counter(hBox, "Points", QString::null, 1, 100000, 100);
    d_randomCount->setValue(1000);

    d_timerCount = new Counter(hBox, "Delay", "ms", 0, 100000, 100);
    d_timerCount->setValue(0);

    QHBoxLayout *layout = new QHBoxLayout(hBox);
    layout->setMargin(0);
    layout->setSpacing(0);
    layout->addSpacing(10);
    layout->addWidget(new QWidget(hBox), 10); // spacer
    layout->addWidget(d_randomCount);
    layout->addSpacing(5);
    layout->addWidget(d_timerCount);

    showRunning(false);

#if QT_VERSION < 0x040000
    toolBar->setStretchableWidget(hBox);

    d_startBtn->setMinimumWidth(helpBtn->sizeHint().width() + 20);
    d_clearBtn->setMinimumWidth(helpBtn->sizeHint().width() + 20);
    helpBtn->setMinimumWidth(helpBtn->sizeHint().width() + 20);
#else
    toolBar->addWidget(hBox);
#endif

    return toolBar;
}

void MainWindow::appendPoints(bool on)
{
    if ( on )
        d_plot->append(d_timerCount->value(),
            d_randomCount->value());
    else
        d_plot->stop();
}

void MainWindow::showRunning(bool running)
{
    d_randomCount->setEnabled(!running);
    d_timerCount->setEnabled(!running);
#if QT_VERSION < 0x040000
    d_startBtn->setOn(running);
    d_startBtn->setTextLabel(running ? "Stop" : "Start", false);
#else
    d_startAction->setChecked(running);
    d_startAction->setText(running ? "Stop" : "Start");
#endif
}

void MainWindow::initWhatsThis()
{
    const char *text1 =
        "Zooming is enabled until the selected area gets "
        "too small for the significance on the axes.\n\n"
        "You can zoom in using the left mouse button.\n"
        "The middle mouse button is used to go back to the "
        "previous zoomed area.\n"
        "The right mouse button is used to unzoom completely.";

    const char *text2 =
        "Number of random points that will be generated.";

    const char *text3 =
        "Delay between the generation of two random points.";

    const char *text4 =
        "Start generation of random points.\n\n"
        "The intention of this example is to show how to implement "
        "growing curves. The points will be generated and displayed "
        "one after the other.\n"
        "To check the performance, a small delay and a large number "
        "of points are useful. To watch the curve growing, a delay "
        " > 300 ms and less points are better.\n"
        "To inspect the curve, stacked zooming is implemented using the "
        "mouse buttons on the plot.";

    const char *text5 = "Remove all points.";

#if QT_VERSION < 0x040000
    QWhatsThis::add(d_plot, text1);
    QWhatsThis::add(d_randomCount, text2);
    QWhatsThis::add(d_timerCount, text3);
    QWhatsThis::add(d_startBtn, text4);
    QWhatsThis::add(d_clearBtn, text5);
#else
    d_plot->setWhatsThis(text1);
    d_randomCount->setWhatsThis(text2);
    d_timerCount->setWhatsThis(text3);
    d_startAction->setWhatsThis(text4);
    d_clearAction->setWhatsThis(text5);
#endif
}