~ubuntu-branches/ubuntu/oneiric/qwt/oneiric-proposed

« back to all changes in this revision

Viewing changes to qwt-5.1.1/examples/bode/bode.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Fathi Boudra
  • Date: 2009-04-12 23:25:58 UTC
  • mfrom: (1.1.4 upstream) (2.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090412232558-3bl06x785yr8xm8u
Tags: 5.1.2-1
* New upstream release.
* Bump compat/debhelper to 7.
* Bump Standards-Version to 3.8.1. No changes needed.
* Invert Maintainers and Uploaders field.
* Fix lintian warnings:
  - dh_clean _k deprecated.
  - missing dependency on libc.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <qregexp.h>
2
 
#include <qapplication.h>
3
 
#include <qtoolbar.h>
4
 
#include <qtoolbutton.h>
5
 
#include <qlabel.h>
6
 
#include <qlayout.h>
7
 
#include <qstatusbar.h>
8
 
#include <qprinter.h>
9
 
#include <qpicture.h>
10
 
#include <qpainter.h>
11
 
#include <qfiledialog.h>
12
 
#if QT_VERSION >= 0x040300
13
 
#ifdef QT_SVG_LIB
14
 
#include <qsvggenerator.h>
15
 
#endif
16
 
#endif
17
 
#if QT_VERSION >= 0x040000
18
 
#include <qprintdialog.h>
19
 
#include <qfileinfo.h>
20
 
#else
21
 
#include <qwt_painter.h>
22
 
#endif
23
 
#include <qwt_counter.h>
24
 
#include <qwt_plot_zoomer.h>
25
 
#include <qwt_plot_panner.h>
26
 
#include <qwt_text.h>
27
 
#include <qwt_math.h>
28
 
#include "pixmaps.h"
29
 
#include "bode_plot.h"
30
 
#include "bode.h"
31
 
 
32
 
class Zoomer: public QwtPlotZoomer
33
 
{
34
 
public:
35
 
    Zoomer(int xAxis, int yAxis, QwtPlotCanvas *canvas):
36
 
        QwtPlotZoomer(xAxis, yAxis, canvas)
37
 
    {
38
 
        setSelectionFlags(QwtPicker::DragSelection | QwtPicker::CornerToCorner);
39
 
        setTrackerMode(QwtPicker::AlwaysOff);
40
 
        setRubberBand(QwtPicker::NoRubberBand);
41
 
 
42
 
        // RightButton: zoom out by 1
43
 
        // Ctrl+RightButton: zoom out to full size
44
 
 
45
 
#if QT_VERSION < 0x040000
46
 
        setMousePattern(QwtEventPattern::MouseSelect2,
47
 
            Qt::RightButton, Qt::ControlButton);
48
 
#else
49
 
        setMousePattern(QwtEventPattern::MouseSelect2,
50
 
            Qt::RightButton, Qt::ControlModifier);
51
 
#endif
52
 
        setMousePattern(QwtEventPattern::MouseSelect3,
53
 
            Qt::RightButton);
54
 
    }
55
 
};
56
 
 
57
 
//-----------------------------------------------------------------
58
 
//
59
 
//      bode.cpp -- A demo program featuring QwtPlot and QwtCounter
60
 
//
61
 
//      This example demonstrates the mapping of different curves
62
 
//      to different axes in a QwtPlot widget. It also shows how to
63
 
//      display the cursor position and how to implement zooming.
64
 
//
65
 
//-----------------------------------------------------------------
66
 
 
67
 
MainWin::MainWin(QWidget *parent): 
68
 
    QMainWindow(parent)
69
 
{
70
 
    d_plot = new BodePlot(this);
71
 
    d_plot->setMargin(5);
72
 
 
73
 
#if QT_VERSION >= 0x040000
74
 
    setContextMenuPolicy(Qt::NoContextMenu);
75
 
#endif
76
 
 
77
 
    d_zoomer[0] = new Zoomer( QwtPlot::xBottom, QwtPlot::yLeft, 
78
 
        d_plot->canvas());
79
 
    d_zoomer[0]->setRubberBand(QwtPicker::RectRubberBand);
80
 
    d_zoomer[0]->setRubberBandPen(QColor(Qt::green));
81
 
    d_zoomer[0]->setTrackerMode(QwtPicker::ActiveOnly);
82
 
    d_zoomer[0]->setTrackerPen(QColor(Qt::white));
83
 
 
84
 
    d_zoomer[1] = new Zoomer(QwtPlot::xTop, QwtPlot::yRight,
85
 
         d_plot->canvas());
86
 
    
87
 
    d_panner = new QwtPlotPanner(d_plot->canvas());
88
 
    d_panner->setMouseButton(Qt::MidButton);
89
 
 
90
 
    d_picker = new QwtPlotPicker(QwtPlot::xBottom, QwtPlot::yLeft,
91
 
        QwtPicker::PointSelection | QwtPicker::DragSelection, 
92
 
        QwtPlotPicker::CrossRubberBand, QwtPicker::AlwaysOn, 
93
 
        d_plot->canvas());
94
 
    d_picker->setRubberBandPen(QColor(Qt::green));
95
 
    d_picker->setRubberBand(QwtPicker::CrossRubberBand);
96
 
    d_picker->setTrackerPen(QColor(Qt::white));
97
 
 
98
 
    setCentralWidget(d_plot);
99
 
 
100
 
    QToolBar *toolBar = new QToolBar(this);
101
 
 
102
 
    QToolButton *btnZoom = new QToolButton(toolBar);
103
 
#if QT_VERSION >= 0x040000
104
 
    btnZoom->setText("Zoom");
105
 
    btnZoom->setIcon(QIcon(zoom_xpm));
106
 
    btnZoom->setCheckable(true);
107
 
    btnZoom->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
108
 
#else
109
 
    btnZoom->setTextLabel("Zoom");
110
 
    btnZoom->setPixmap(zoom_xpm);
111
 
    btnZoom->setToggleButton(true);
112
 
    btnZoom->setUsesTextLabel(true);
113
 
#endif
114
 
 
115
 
    QToolButton *btnPrint = new QToolButton(toolBar);
116
 
#if QT_VERSION >= 0x040000
117
 
    btnPrint->setText("Print");
118
 
    btnPrint->setIcon(QIcon(print_xpm));
119
 
    btnPrint->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
120
 
#else
121
 
    btnPrint->setTextLabel("Print");
122
 
    btnPrint->setPixmap(print_xpm);
123
 
    btnPrint->setUsesTextLabel(true);
124
 
#endif
125
 
 
126
 
#if QT_VERSION < 0x040000 
127
 
    QToolButton *btnSVG = new QToolButton(toolBar);
128
 
    btnSVG->setTextLabel("SVG");
129
 
    btnSVG->setPixmap(print_xpm);
130
 
    btnSVG->setUsesTextLabel(true);
131
 
#elif QT_VERSION >= 0x040300
132
 
#ifdef QT_SVG_LIB
133
 
    QToolButton *btnSVG = new QToolButton(toolBar);
134
 
    btnSVG->setText("SVG");
135
 
    btnSVG->setIcon(QIcon(print_xpm));
136
 
    btnSVG->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
137
 
#endif
138
 
#endif
139
 
 
140
 
#if QT_VERSION >= 0x040000
141
 
    toolBar->addWidget(btnZoom);
142
 
    toolBar->addWidget(btnPrint);
143
 
#if QT_VERSION >= 0x040300
144
 
#ifdef QT_SVG_LIB
145
 
    toolBar->addWidget(btnSVG);
146
 
#endif
147
 
#endif
148
 
#endif
149
 
    toolBar->addSeparator();
150
 
 
151
 
    QWidget *hBox = new QWidget(toolBar);
152
 
 
153
 
    QHBoxLayout *layout = new QHBoxLayout(hBox);
154
 
    layout->setSpacing(0);
155
 
    layout->addWidget(new QWidget(hBox), 10); // spacer
156
 
    layout->addWidget(new QLabel("Damping Factor", hBox), 0);
157
 
    layout->addSpacing(10);
158
 
 
159
 
    QwtCounter *cntDamp = new QwtCounter(hBox);
160
 
    cntDamp->setRange(0.0, 5.0, 0.01);
161
 
    cntDamp->setValue(0.0);
162
 
    
163
 
    layout->addWidget(cntDamp, 0);
164
 
 
165
 
#if QT_VERSION >= 0x040000
166
 
    (void)toolBar->addWidget(hBox);
167
 
#else
168
 
    toolBar->setStretchableWidget(hBox);
169
 
#endif
170
 
 
171
 
    addToolBar(toolBar);
172
 
#ifndef QT_NO_STATUSBAR
173
 
    (void)statusBar();
174
 
#endif
175
 
 
176
 
    enableZoomMode(false);
177
 
    showInfo();
178
 
 
179
 
    connect(cntDamp, SIGNAL(valueChanged(double)), 
180
 
        d_plot, SLOT(setDamp(double))); 
181
 
 
182
 
    connect(btnPrint, SIGNAL(clicked()), SLOT(print()));
183
 
#if QT_VERSION < 0x040000 
184
 
    connect(btnSVG, SIGNAL(clicked()), SLOT(exportSVG()));
185
 
#elif QT_VERSION >= 0x040300
186
 
#ifdef QT_SVG_LIB
187
 
    connect(btnSVG, SIGNAL(clicked()), SLOT(exportSVG()));
188
 
#endif
189
 
#endif
190
 
    connect(btnZoom, SIGNAL(toggled(bool)), SLOT(enableZoomMode(bool)));
191
 
 
192
 
    connect(d_picker, SIGNAL(moved(const QPoint &)),
193
 
            SLOT(moved(const QPoint &)));
194
 
    connect(d_picker, SIGNAL(selected(const QwtPolygon &)),
195
 
            SLOT(selected(const QwtPolygon &)));
196
 
}
197
 
 
198
 
void MainWin::print()
199
 
{
200
 
#if 0
201
 
    QPrinter printer;
202
 
#else
203
 
    QPrinter printer(QPrinter::HighResolution);
204
 
#if QT_VERSION < 0x040000
205
 
    printer.setOutputToFile(true);
206
 
    printer.setOutputFileName("/tmp/bode.ps");
207
 
    printer.setColorMode(QPrinter::Color);
208
 
#else
209
 
    printer.setOutputFileName("/tmp/bode.pdf");
210
 
#endif
211
 
#endif
212
 
 
213
 
    QString docName = d_plot->title().text();
214
 
    if ( !docName.isEmpty() )
215
 
    {
216
 
        docName.replace (QRegExp (QString::fromLatin1 ("\n")), tr (" -- "));
217
 
        printer.setDocName (docName);
218
 
    }
219
 
 
220
 
    printer.setCreator("Bode example");
221
 
    printer.setOrientation(QPrinter::Landscape);
222
 
 
223
 
#if QT_VERSION >= 0x040000
224
 
    QPrintDialog dialog(&printer);
225
 
    if ( dialog.exec() )
226
 
    {
227
 
#else
228
 
    if (printer.setup())
229
 
    {
230
 
#endif
231
 
        QwtPlotPrintFilter filter;
232
 
        if ( printer.colorMode() == QPrinter::GrayScale )
233
 
        {
234
 
            int options = QwtPlotPrintFilter::PrintAll;
235
 
            options &= ~QwtPlotPrintFilter::PrintBackground;
236
 
            options |= QwtPlotPrintFilter::PrintFrameWithScales;
237
 
            filter.setOptions(options);
238
 
        }
239
 
        d_plot->print(printer, filter);
240
 
    }
241
 
}
242
 
 
243
 
void MainWin::exportSVG()
244
 
{
245
 
    QString fileName = "bode.svg";
246
 
 
247
 
#if QT_VERSION < 0x040000
248
 
 
249
 
#ifndef QT_NO_FILEDIALOG
250
 
    fileName = QFileDialog::getSaveFileName(
251
 
        "bode.svg", "SVG Documents (*.svg)", this);
252
 
#endif
253
 
    if ( !fileName.isEmpty() )
254
 
    {
255
 
        // enable workaround for Qt3 misalignments
256
 
        QwtPainter::setSVGMode(true); 
257
 
 
258
 
        QPicture picture;
259
 
 
260
 
        QPainter p(&picture);
261
 
        d_plot->print(&p, QRect(0, 0, 800, 600));
262
 
        p.end();
263
 
 
264
 
        picture.save(fileName, "svg");
265
 
    }
266
 
 
267
 
#elif QT_VERSION >= 0x040300
268
 
 
269
 
#ifdef QT_SVG_LIB
270
 
#ifndef QT_NO_FILEDIALOG
271
 
    fileName = QFileDialog::getSaveFileName(
272
 
        this, "Export File Name", QString(),
273
 
        "SVG Documents (*.svg)");
274
 
#endif
275
 
    if ( !fileName.isEmpty() )
276
 
    {
277
 
        QSvgGenerator generator;
278
 
        generator.setFileName(fileName);
279
 
        generator.setSize(QSize(800, 600));
280
 
 
281
 
        d_plot->print(generator);
282
 
    }
283
 
#endif
284
 
#endif
285
 
}
286
 
 
287
 
void MainWin::enableZoomMode(bool on)
288
 
{
289
 
    d_panner->setEnabled(on);
290
 
 
291
 
    d_zoomer[0]->setEnabled(on);
292
 
    d_zoomer[0]->zoom(0);
293
 
 
294
 
    d_zoomer[1]->setEnabled(on);
295
 
    d_zoomer[1]->zoom(0);
296
 
 
297
 
    d_picker->setEnabled(!on);
298
 
 
299
 
    showInfo();
300
 
}
301
 
 
302
 
void MainWin::showInfo(QString text)
303
 
{
304
 
    if ( text == QString::null )
305
 
    {
306
 
        if ( d_picker->rubberBand() )
307
 
            text = "Cursor Pos: Press left mouse button in plot region";
308
 
        else
309
 
            text = "Zoom: Press mouse button and drag";
310
 
    }
311
 
 
312
 
#ifndef QT_NO_STATUSBAR
313
 
#if QT_VERSION >= 0x040000
314
 
    statusBar()->showMessage(text);
315
 
#else
316
 
    statusBar()->message(text);
317
 
#endif
318
 
#endif
319
 
}
320
 
 
321
 
void MainWin::moved(const QPoint &pos)
322
 
{
323
 
    QString info;
324
 
    info.sprintf("Freq=%g, Ampl=%g, Phase=%g",
325
 
        d_plot->invTransform(QwtPlot::xBottom, pos.x()),
326
 
        d_plot->invTransform(QwtPlot::yLeft, pos.y()),
327
 
        d_plot->invTransform(QwtPlot::yRight, pos.y())
328
 
    );
329
 
    showInfo(info);
330
 
}
331
 
 
332
 
void MainWin::selected(const QwtPolygon &)
333
 
{
334
 
    showInfo();
335
 
}
336
 
 
337
 
int main (int argc, char **argv)
338
 
{
339
 
    QApplication a(argc, argv);
340
 
 
341
 
    MainWin w;
342
 
#if QT_VERSION < 0x040000
343
 
    a.setMainWidget(&w);
344
 
#endif
345
 
    w.resize(540,400);
346
 
    w.show();
347
 
 
348
 
    int rv = a.exec();
349
 
    return rv;
350
 
}