2
#include <qapplication.h>
4
#include <qtoolbutton.h>
7
#include <qstatusbar.h>
11
#include <qfiledialog.h>
12
#if QT_VERSION >= 0x040300
14
#include <qsvggenerator.h>
17
#if QT_VERSION >= 0x040000
18
#include <qprintdialog.h>
19
#include <qfileinfo.h>
21
#include <qwt_painter.h>
23
#include <qwt_counter.h>
24
#include <qwt_plot_zoomer.h>
25
#include <qwt_plot_panner.h>
29
#include "bode_plot.h"
32
class Zoomer: public QwtPlotZoomer
35
Zoomer(int xAxis, int yAxis, QwtPlotCanvas *canvas):
36
QwtPlotZoomer(xAxis, yAxis, canvas)
38
setSelectionFlags(QwtPicker::DragSelection | QwtPicker::CornerToCorner);
39
setTrackerMode(QwtPicker::AlwaysOff);
40
setRubberBand(QwtPicker::NoRubberBand);
42
// RightButton: zoom out by 1
43
// Ctrl+RightButton: zoom out to full size
45
#if QT_VERSION < 0x040000
46
setMousePattern(QwtEventPattern::MouseSelect2,
47
Qt::RightButton, Qt::ControlButton);
49
setMousePattern(QwtEventPattern::MouseSelect2,
50
Qt::RightButton, Qt::ControlModifier);
52
setMousePattern(QwtEventPattern::MouseSelect3,
57
//-----------------------------------------------------------------
59
// bode.cpp -- A demo program featuring QwtPlot and QwtCounter
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.
65
//-----------------------------------------------------------------
67
MainWin::MainWin(QWidget *parent):
70
d_plot = new BodePlot(this);
73
#if QT_VERSION >= 0x040000
74
setContextMenuPolicy(Qt::NoContextMenu);
77
d_zoomer[0] = new Zoomer( QwtPlot::xBottom, QwtPlot::yLeft,
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));
84
d_zoomer[1] = new Zoomer(QwtPlot::xTop, QwtPlot::yRight,
87
d_panner = new QwtPlotPanner(d_plot->canvas());
88
d_panner->setMouseButton(Qt::MidButton);
90
d_picker = new QwtPlotPicker(QwtPlot::xBottom, QwtPlot::yLeft,
91
QwtPicker::PointSelection | QwtPicker::DragSelection,
92
QwtPlotPicker::CrossRubberBand, QwtPicker::AlwaysOn,
94
d_picker->setRubberBandPen(QColor(Qt::green));
95
d_picker->setRubberBand(QwtPicker::CrossRubberBand);
96
d_picker->setTrackerPen(QColor(Qt::white));
98
setCentralWidget(d_plot);
100
QToolBar *toolBar = new QToolBar(this);
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);
109
btnZoom->setTextLabel("Zoom");
110
btnZoom->setPixmap(zoom_xpm);
111
btnZoom->setToggleButton(true);
112
btnZoom->setUsesTextLabel(true);
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);
121
btnPrint->setTextLabel("Print");
122
btnPrint->setPixmap(print_xpm);
123
btnPrint->setUsesTextLabel(true);
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
133
QToolButton *btnSVG = new QToolButton(toolBar);
134
btnSVG->setText("SVG");
135
btnSVG->setIcon(QIcon(print_xpm));
136
btnSVG->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
140
#if QT_VERSION >= 0x040000
141
toolBar->addWidget(btnZoom);
142
toolBar->addWidget(btnPrint);
143
#if QT_VERSION >= 0x040300
145
toolBar->addWidget(btnSVG);
149
toolBar->addSeparator();
151
QWidget *hBox = new QWidget(toolBar);
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);
159
QwtCounter *cntDamp = new QwtCounter(hBox);
160
cntDamp->setRange(0.0, 5.0, 0.01);
161
cntDamp->setValue(0.0);
163
layout->addWidget(cntDamp, 0);
165
#if QT_VERSION >= 0x040000
166
(void)toolBar->addWidget(hBox);
168
toolBar->setStretchableWidget(hBox);
172
#ifndef QT_NO_STATUSBAR
176
enableZoomMode(false);
179
connect(cntDamp, SIGNAL(valueChanged(double)),
180
d_plot, SLOT(setDamp(double)));
182
connect(btnPrint, SIGNAL(clicked()), SLOT(print()));
183
#if QT_VERSION < 0x040000
184
connect(btnSVG, SIGNAL(clicked()), SLOT(exportSVG()));
185
#elif QT_VERSION >= 0x040300
187
connect(btnSVG, SIGNAL(clicked()), SLOT(exportSVG()));
190
connect(btnZoom, SIGNAL(toggled(bool)), SLOT(enableZoomMode(bool)));
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 &)));
198
void MainWin::print()
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);
209
printer.setOutputFileName("/tmp/bode.pdf");
213
QString docName = d_plot->title().text();
214
if ( !docName.isEmpty() )
216
docName.replace (QRegExp (QString::fromLatin1 ("\n")), tr (" -- "));
217
printer.setDocName (docName);
220
printer.setCreator("Bode example");
221
printer.setOrientation(QPrinter::Landscape);
223
#if QT_VERSION >= 0x040000
224
QPrintDialog dialog(&printer);
231
QwtPlotPrintFilter filter;
232
if ( printer.colorMode() == QPrinter::GrayScale )
234
int options = QwtPlotPrintFilter::PrintAll;
235
options &= ~QwtPlotPrintFilter::PrintBackground;
236
options |= QwtPlotPrintFilter::PrintFrameWithScales;
237
filter.setOptions(options);
239
d_plot->print(printer, filter);
243
void MainWin::exportSVG()
245
QString fileName = "bode.svg";
247
#if QT_VERSION < 0x040000
249
#ifndef QT_NO_FILEDIALOG
250
fileName = QFileDialog::getSaveFileName(
251
"bode.svg", "SVG Documents (*.svg)", this);
253
if ( !fileName.isEmpty() )
255
// enable workaround for Qt3 misalignments
256
QwtPainter::setSVGMode(true);
260
QPainter p(&picture);
261
d_plot->print(&p, QRect(0, 0, 800, 600));
264
picture.save(fileName, "svg");
267
#elif QT_VERSION >= 0x040300
270
#ifndef QT_NO_FILEDIALOG
271
fileName = QFileDialog::getSaveFileName(
272
this, "Export File Name", QString(),
273
"SVG Documents (*.svg)");
275
if ( !fileName.isEmpty() )
277
QSvgGenerator generator;
278
generator.setFileName(fileName);
279
generator.setSize(QSize(800, 600));
281
d_plot->print(generator);
287
void MainWin::enableZoomMode(bool on)
289
d_panner->setEnabled(on);
291
d_zoomer[0]->setEnabled(on);
292
d_zoomer[0]->zoom(0);
294
d_zoomer[1]->setEnabled(on);
295
d_zoomer[1]->zoom(0);
297
d_picker->setEnabled(!on);
302
void MainWin::showInfo(QString text)
304
if ( text == QString::null )
306
if ( d_picker->rubberBand() )
307
text = "Cursor Pos: Press left mouse button in plot region";
309
text = "Zoom: Press mouse button and drag";
312
#ifndef QT_NO_STATUSBAR
313
#if QT_VERSION >= 0x040000
314
statusBar()->showMessage(text);
316
statusBar()->message(text);
321
void MainWin::moved(const QPoint &pos)
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())
332
void MainWin::selected(const QwtPolygon &)
337
int main (int argc, char **argv)
339
QApplication a(argc, argv);
342
#if QT_VERSION < 0x040000