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 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
|
#include <qregexp.h>
#include <qapplication.h>
#include <qtoolbar.h>
#include <qtoolbutton.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qstatusbar.h>
#include <qprinter.h>
#include <qpicture.h>
#include <qpainter.h>
#include <qfiledialog.h>
#if QT_VERSION >= 0x040300
#ifdef QT_SVG_LIB
#include <qsvggenerator.h>
#endif
#endif
#if QT_VERSION >= 0x040000
#include <qprintdialog.h>
#include <qfileinfo.h>
#else
#include <qwt_painter.h>
#endif
#include <qwt_counter.h>
#include <qwt_plot_zoomer.h>
#include <qwt_plot_panner.h>
#include <qwt_text.h>
#include <qwt_math.h>
#include "pixmaps.h"
#include "bode_plot.h"
#include "bode.h"
class Zoomer: public QwtPlotZoomer
{
public:
Zoomer(int xAxis, int yAxis, QwtPlotCanvas *canvas):
QwtPlotZoomer(xAxis, yAxis, canvas)
{
setSelectionFlags(QwtPicker::DragSelection | QwtPicker::CornerToCorner);
setTrackerMode(QwtPicker::AlwaysOff);
setRubberBand(QwtPicker::NoRubberBand);
// RightButton: zoom out by 1
// Ctrl+RightButton: zoom out to full size
#if QT_VERSION < 0x040000
setMousePattern(QwtEventPattern::MouseSelect2,
Qt::RightButton, Qt::ControlButton);
#else
setMousePattern(QwtEventPattern::MouseSelect2,
Qt::RightButton, Qt::ControlModifier);
#endif
setMousePattern(QwtEventPattern::MouseSelect3,
Qt::RightButton);
}
};
//-----------------------------------------------------------------
//
// bode.cpp -- A demo program featuring QwtPlot and QwtCounter
//
// This example demonstrates the mapping of different curves
// to different axes in a QwtPlot widget. It also shows how to
// display the cursor position and how to implement zooming.
//
//-----------------------------------------------------------------
MainWin::MainWin(QWidget *parent):
QMainWindow(parent)
{
d_plot = new BodePlot(this);
d_plot->setMargin(5);
#if QT_VERSION >= 0x040000
setContextMenuPolicy(Qt::NoContextMenu);
#endif
d_zoomer[0] = new Zoomer( QwtPlot::xBottom, QwtPlot::yLeft,
d_plot->canvas());
d_zoomer[0]->setRubberBand(QwtPicker::RectRubberBand);
d_zoomer[0]->setRubberBandPen(QColor(Qt::green));
d_zoomer[0]->setTrackerMode(QwtPicker::ActiveOnly);
d_zoomer[0]->setTrackerPen(QColor(Qt::white));
d_zoomer[1] = new Zoomer(QwtPlot::xTop, QwtPlot::yRight,
d_plot->canvas());
d_panner = new QwtPlotPanner(d_plot->canvas());
d_panner->setMouseButton(Qt::MidButton);
d_picker = new QwtPlotPicker(QwtPlot::xBottom, QwtPlot::yLeft,
QwtPicker::PointSelection | QwtPicker::DragSelection,
QwtPlotPicker::CrossRubberBand, QwtPicker::AlwaysOn,
d_plot->canvas());
d_picker->setRubberBandPen(QColor(Qt::green));
d_picker->setRubberBand(QwtPicker::CrossRubberBand);
d_picker->setTrackerPen(QColor(Qt::white));
setCentralWidget(d_plot);
QToolBar *toolBar = new QToolBar(this);
QToolButton *btnZoom = new QToolButton(toolBar);
#if QT_VERSION >= 0x040000
btnZoom->setText("Zoom");
btnZoom->setIcon(QIcon(zoom_xpm));
btnZoom->setCheckable(true);
btnZoom->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
#else
btnZoom->setTextLabel("Zoom");
btnZoom->setPixmap(zoom_xpm);
btnZoom->setToggleButton(true);
btnZoom->setUsesTextLabel(true);
#endif
QToolButton *btnPrint = new QToolButton(toolBar);
#if QT_VERSION >= 0x040000
btnPrint->setText("Print");
btnPrint->setIcon(QIcon(print_xpm));
btnPrint->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
#else
btnPrint->setTextLabel("Print");
btnPrint->setPixmap(print_xpm);
btnPrint->setUsesTextLabel(true);
#endif
#if QT_VERSION < 0x040000
QToolButton *btnSVG = new QToolButton(toolBar);
btnSVG->setTextLabel("SVG");
btnSVG->setPixmap(print_xpm);
btnSVG->setUsesTextLabel(true);
#elif QT_VERSION >= 0x040300
#ifdef QT_SVG_LIB
QToolButton *btnSVG = new QToolButton(toolBar);
btnSVG->setText("SVG");
btnSVG->setIcon(QIcon(print_xpm));
btnSVG->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
#endif
#endif
#if QT_VERSION >= 0x040000
toolBar->addWidget(btnZoom);
toolBar->addWidget(btnPrint);
#if QT_VERSION >= 0x040300
#ifdef QT_SVG_LIB
toolBar->addWidget(btnSVG);
#endif
#endif
#endif
toolBar->addSeparator();
QWidget *hBox = new QWidget(toolBar);
QHBoxLayout *layout = new QHBoxLayout(hBox);
layout->setSpacing(0);
layout->addWidget(new QWidget(hBox), 10); // spacer
layout->addWidget(new QLabel("Damping Factor", hBox), 0);
layout->addSpacing(10);
QwtCounter *cntDamp = new QwtCounter(hBox);
cntDamp->setRange(0.0, 5.0, 0.01);
cntDamp->setValue(0.0);
layout->addWidget(cntDamp, 0);
#if QT_VERSION >= 0x040000
(void)toolBar->addWidget(hBox);
#else
toolBar->setStretchableWidget(hBox);
#endif
addToolBar(toolBar);
#ifndef QT_NO_STATUSBAR
(void)statusBar();
#endif
enableZoomMode(false);
showInfo();
connect(cntDamp, SIGNAL(valueChanged(double)),
d_plot, SLOT(setDamp(double)));
connect(btnPrint, SIGNAL(clicked()), SLOT(print()));
#if QT_VERSION < 0x040000
connect(btnSVG, SIGNAL(clicked()), SLOT(exportSVG()));
#elif QT_VERSION >= 0x040300
#ifdef QT_SVG_LIB
connect(btnSVG, SIGNAL(clicked()), SLOT(exportSVG()));
#endif
#endif
connect(btnZoom, SIGNAL(toggled(bool)), SLOT(enableZoomMode(bool)));
connect(d_picker, SIGNAL(moved(const QPoint &)),
SLOT(moved(const QPoint &)));
connect(d_picker, SIGNAL(selected(const QwtPolygon &)),
SLOT(selected(const QwtPolygon &)));
}
void MainWin::print()
{
#if 1
QPrinter printer;
#else
QPrinter printer(QPrinter::HighResolution);
#if QT_VERSION < 0x040000
printer.setOutputToFile(true);
printer.setOutputFileName("/tmp/bode.ps");
printer.setColorMode(QPrinter::Color);
#else
printer.setOutputFileName("/tmp/bode.pdf");
#endif
#endif
QString docName = d_plot->title().text();
if ( !docName.isEmpty() )
{
docName.replace (QRegExp (QString::fromLatin1 ("\n")), tr (" -- "));
printer.setDocName (docName);
}
printer.setCreator("Bode example");
printer.setOrientation(QPrinter::Landscape);
#if QT_VERSION >= 0x040000
QPrintDialog dialog(&printer);
if ( dialog.exec() )
{
#else
if (printer.setup())
{
#endif
QwtPlotPrintFilter filter;
if ( printer.colorMode() == QPrinter::GrayScale )
{
int options = QwtPlotPrintFilter::PrintAll;
options &= ~QwtPlotPrintFilter::PrintBackground;
options |= QwtPlotPrintFilter::PrintFrameWithScales;
filter.setOptions(options);
}
d_plot->print(printer, filter);
}
}
void MainWin::exportSVG()
{
QString fileName = "bode.svg";
#if QT_VERSION < 0x040000
#ifndef QT_NO_FILEDIALOG
fileName = QFileDialog::getSaveFileName(
"bode.svg", "SVG Documents (*.svg)", this);
#endif
if ( !fileName.isEmpty() )
{
// enable workaround for Qt3 misalignments
QwtPainter::setSVGMode(true);
QPicture picture;
QPainter p(&picture);
d_plot->print(&p, QRect(0, 0, 800, 600));
p.end();
picture.save(fileName, "svg");
}
#elif QT_VERSION >= 0x040300
#ifdef QT_SVG_LIB
#ifndef QT_NO_FILEDIALOG
fileName = QFileDialog::getSaveFileName(
this, "Export File Name", QString(),
"SVG Documents (*.svg)");
#endif
if ( !fileName.isEmpty() )
{
QSvgGenerator generator;
generator.setFileName(fileName);
generator.setSize(QSize(800, 600));
d_plot->print(generator);
}
#endif
#endif
}
void MainWin::enableZoomMode(bool on)
{
d_panner->setEnabled(on);
d_zoomer[0]->setEnabled(on);
d_zoomer[0]->zoom(0);
d_zoomer[1]->setEnabled(on);
d_zoomer[1]->zoom(0);
d_picker->setEnabled(!on);
showInfo();
}
void MainWin::showInfo(QString text)
{
if ( text == QString::null )
{
if ( d_picker->rubberBand() )
text = "Cursor Pos: Press left mouse button in plot region";
else
text = "Zoom: Press mouse button and drag";
}
#ifndef QT_NO_STATUSBAR
#if QT_VERSION >= 0x040000
statusBar()->showMessage(text);
#else
statusBar()->message(text);
#endif
#endif
}
void MainWin::moved(const QPoint &pos)
{
QString info;
info.sprintf("Freq=%g, Ampl=%g, Phase=%g",
d_plot->invTransform(QwtPlot::xBottom, pos.x()),
d_plot->invTransform(QwtPlot::yLeft, pos.y()),
d_plot->invTransform(QwtPlot::yRight, pos.y())
);
showInfo(info);
}
void MainWin::selected(const QwtPolygon &)
{
showInfo();
}
int main (int argc, char **argv)
{
QApplication a(argc, argv);
MainWin w;
#if QT_VERSION < 0x040000
a.setMainWidget(&w);
#endif
w.resize(540,400);
w.show();
int rv = a.exec();
return rv;
}
|