QXRD  0.11.16
qcepplot.cpp
Go to the documentation of this file.
1 #include "qcepplot.h"
2 
3 #include <QPrinter>
4 #include <QPrintDialog>
5 #include <QMenu>
6 #include <QContextMenuEvent>
7 
8 #include <qwt_symbol.h>
9 #include <qwt_picker_machine.h>
10 #include <qwt_plot_renderer.h>
11 #include <qwt_scale_engine.h>
12 
13 #include "qcepplotzoomer.h"
14 #include "qcepplotmeasurer.h"
15 #include "qcepplotsettings.h"
16 #include "qcepapplication.h"
17 
18 QcepPlot::QcepPlot(QWidget *parent)
19  : QwtPlot(parent),
21  m_Legend(NULL),
22  m_Zoomer(NULL),
23  m_Panner(NULL),
24  m_Magnifier(NULL),
25  m_Measurer(NULL)
26 {
27  for (int i=0; i<QwtPlot::axisCnt; i++) {
28  m_IsLog[i] = 0;
29  }
30 }
31 
33 {
34 }
35 
37 {
38  m_PlotSettings = settings;
39 
40  setCanvasBackground(QColor(Qt::white));
41 
42  m_Zoomer = new QcepPlotZoomer(canvas(), this);
43  m_Zoomer -> setStateMachine(new QwtPickerDragRectMachine());
44  m_Zoomer -> setTrackerMode(QwtPicker::AlwaysOn);
45  m_Zoomer -> setRubberBand(QwtPicker::RectRubberBand);
46 
47  m_Zoomer -> setMousePattern(QwtEventPattern::MouseSelect2,
48  Qt::LeftButton, Qt::ControlModifier | Qt::ShiftModifier);
49  m_Zoomer -> setMousePattern(QwtEventPattern::MouseSelect3,
50  Qt::LeftButton, Qt::ControlModifier);
51 
52  m_Zoomer -> setEnabled(true);
53 
54  m_Legend = new QwtLegend(this);
55  m_Legend -> setDefaultItemMode(QwtLegendData::Checkable);
56 
57  m_Panner = new QwtPlotPanner(canvas());
58  m_Panner -> setEnabled(true);
59  m_Panner -> setMouseButton(Qt::MidButton);
60  m_Panner -> setAxisEnabled(QwtPlot::yRight, false);
61 
62  m_Magnifier = new QwtPlotMagnifier(canvas());
63  m_Magnifier -> setEnabled(true);
64  m_Magnifier -> setMouseButton(Qt::NoButton);
65  m_Magnifier -> setAxisEnabled(QwtPlot::yRight, false);
66 
67  m_Measurer = new QcepPlotMeasurer(canvas(), this);
68  m_Measurer -> setEnabled(false);
69 
70  setAxisLabelRotation(QwtPlot::yLeft, -90);
71  setAxisLabelAlignment(QwtPlot::yLeft, Qt::AlignVCenter);
72 
73  autoScale();
74 
76 
77  if (set) {
78  connect(set->prop_XAxisLog(), SIGNAL(valueChanged(bool,int)), this, SLOT(setXAxisLog(bool)));
79  connect(set->prop_YAxisLog(), SIGNAL(valueChanged(bool,int)), this, SLOT(setYAxisLog(bool)));
80  connect(set->prop_X2AxisLog(), SIGNAL(valueChanged(bool,int)), this, SLOT(setX2AxisLog(bool)));
81  connect(set->prop_Y2AxisLog(), SIGNAL(valueChanged(bool,int)), this, SLOT(setY2AxisLog(bool)));
82 
83  setXAxisLog(set->get_XAxisLog());
84  setYAxisLog(set->get_YAxisLog());
85  setX2AxisLog(set->get_X2AxisLog());
86  setY2AxisLog(set->get_Y2AxisLog());
87 
88  if (m_Legend) {
89  int legendPos = set->get_LegendPosition();
90 
91  if (legendPos >= 0) {
92  insertLegend(m_Legend, (QwtPlot::LegendPosition) legendPos);
93  }
94  }
95  }
96 
97  if (m_Legend) {
98  connect(m_Legend, SIGNAL(clicked(const QVariant &,int)), this, SLOT(onLegendClicked(const QVariant&, int)));
99  connect(m_Legend, SIGNAL(checked(const QVariant &,bool,int)), this, SLOT(onLegendChecked(const QVariant&, bool, int)));
100  }
101 }
102 
103 void QcepPlot::setPlotCurveStyle(int index, QwtPlotCurve *curve)
104 {
105  const int nColors = 10;
106  const int nSymbols = 4;
107  const int nStyles = 3;
108 
109  int colorIndex = index % nColors;
110  int symbolIndex = (index / nColors) % nSymbols;
111  int styleIndex = (index / (nColors * nSymbols)) % nStyles;
112 
113  QPen pen;
114  QwtSymbol *symb = new QwtSymbol();
115  QBrush brush;
116 
117  switch (colorIndex) {
118  case 0:
119  pen = QPen(QColor(255,0,0)); // red
120  break;
121  case 1:
122  pen = QPen(QColor(255,170,0)); // Orange
123  break;
124  case 2:
125  pen = QPen(QColor(255,232,137)); // Yellow
126  break;
127  case 3:
128  pen = QPen(QColor(0,255,0)); // Green
129  break;
130  case 4:
131  pen = QPen(QColor(0,170,0)); // Dk green
132  break;
133  case 5:
134  pen = QPen(QColor(0,255,255)); // Cyan
135  break;
136  case 6:
137  pen = QPen(QColor(0,170,255)); // Sea blue
138  break;
139  case 7:
140  pen = QPen(QColor(0,0,255)); // Blue
141  break;
142  case 8:
143  pen = QPen(QColor(145,0,255)); // Violet
144  break;
145  case 9:
146  pen = QPen(QColor(255,0,255)); // Magenta
147  break;
148  }
149 
150  switch (styleIndex) {
151  case 0:
152  pen.setStyle(Qt::SolidLine);
153  break;
154  case 1:
155  pen.setStyle(Qt::DashLine);
156  break;
157  case 2:
158  pen.setStyle(Qt::DotLine);
159  break;
160  case 3:
161  pen.setStyle(Qt::DashDotLine);
162  break;
163  }
164 
165  symb->setPen(pen);
166  symb->setBrush(QBrush(pen.color()));
167  symb->setSize(5,5);
168 
169  switch (symbolIndex) {
170  case 0:
171  symb->setStyle(QwtSymbol::Ellipse);
172  break;
173  case 1:
174  symb->setStyle(QwtSymbol::Rect);
175  break;
176  case 2:
177  symb->setStyle(QwtSymbol::Triangle);
178  break;
179  case 3:
180  symb->setStyle(QwtSymbol::DTriangle);
181  break;
182  }
183 
184  curve -> setPen(pen);
185  curve -> setSymbol(symb);
186 }
187 
189 {
190  setAxisAutoScale(QwtPlot::yLeft);
191  setAxisAutoScale(QwtPlot::xBottom);
192 
193  m_Zoomer -> setZoomBase();
194 
195 //
196 // replot();
197 }
198 
200 {
201  QPrinter printer( QPrinter::HighResolution );
202 
203  QString docName = this->title().text();
204  if ( !docName.isEmpty() )
205  {
206  docName.replace ( QRegExp ( QString::fromLatin1 ( "\n" ) ), tr ( " -- " ) );
207  printer.setDocName ( docName );
208  }
209 
210 // printer.setCreator( "Bode example" );
211  printer.setOrientation( QPrinter::Landscape );
212 
213  QPrintDialog dialog( &printer );
214 
215  if ( dialog.exec() ) {
216  QwtPlotRenderer renderer;
217 
218  if ( printer.colorMode() == QPrinter::GrayScale ) {
219  renderer.setDiscardFlag( QwtPlotRenderer::DiscardBackground );
220  renderer.setDiscardFlag( QwtPlotRenderer::DiscardCanvasBackground );
221  renderer.setDiscardFlag( QwtPlotRenderer::DiscardCanvasFrame );
222 // renderer.setLayoutFlag( QwtPlotRenderer::FrameWithScales );
223  }
224 
225  double gw = this->width();
226  double gh = this->height();
227  double pw = printer.width();
228  double ph = printer.height();
229  double scal = qMin( pw/gw, ph/gh);
230 
231  pw = gw*scal;
232  ph = gh*scal;
233 
234  QRectF rect(0,0, pw,ph);
235 
236  QPainter p(&printer);
237 
238  renderer.render( this, &p, rect);
239  }
240 }
241 
243 {
244  m_Zoomer -> zoom(1);
245 }
246 
248 {
249  m_Zoomer -> zoom(-1);
250 }
251 
253 {
254  if (m_Zoomer && m_Zoomer -> zoomRectIndex() == 0) {
255  m_Zoomer -> setZoomBase();
256  }
257 
258  replot();
259 }
260 
262 {
263  m_Zoomer -> setEnabled(true);
264  m_Measurer -> setEnabled(false);
265 }
266 
268 {
269  m_Zoomer -> setEnabled(false);
270  m_Measurer -> setEnabled(true);
271 }
272 
273 void QcepPlot::onLegendClicked(const QVariant &itemInfo, int index)
274 {
275 // if (g_Application) {
276 // g_Application->printMessage(tr("QxrdPlot::onLegendClicked(%1,%2)").arg(itemInfo.toString()).arg(index));
277 // }
278 }
279 
280 void QcepPlot::onLegendChecked(const QVariant &itemInfo, bool on, int index)
281 {
282 // if (g_Application) {
283 // g_Application->printMessage(tr("QxrdPlot::onLegendChecked(%1,%2,%3)").arg(itemInfo.toString()).arg(on).arg(index));
284 // }
285 
286  QwtPlotItem *item = infoToItem(itemInfo);
287 
288  if (item) {
289  QwtPlotCurve *pc = dynamic_cast<QwtPlotCurve*>(item);
290 
291  if (pc) {
292  QPen pen = pc->pen();
293  const QwtSymbol *oldsym = pc->symbol();
294  QwtSymbol *sym = NULL;
295 
296  if (oldsym) {
297  sym = new QwtSymbol(oldsym->style(), oldsym->brush(), oldsym->pen(), oldsym->size());
298  }
299 
300  if (on) {
301  pen.setWidth(3);
302  if (sym) {
303  sym->setSize(9,9);
304  }
305  } else {
306  pen.setWidth(1);
307  if (sym) {
308  sym->setSize(5,5);
309  }
310  }
311  pc->setPen(pen);
312  if (sym) {
313  pc->setSymbol(sym);
314  }
315  }
316 
317  replot();
318  }
319 }
320 
321 void QcepPlot::setXAxisLog(bool isLog)
322 {
323  setLogAxis(QwtPlot::xBottom, isLog);
324 }
325 
326 void QcepPlot::setYAxisLog(bool isLog)
327 {
328  setLogAxis(QwtPlot::yLeft, isLog);
329 }
330 
331 void QcepPlot::setX2AxisLog(bool isLog)
332 {
333  setLogAxis(QwtPlot::xTop, isLog);
334 }
335 
336 void QcepPlot::setY2AxisLog(bool isLog)
337 {
338  setLogAxis(QwtPlot::yRight, isLog);
339 }
340 
341 void QcepPlot::setLogAxis(int axis, int isLog)
342 {
343  if (axis >= 0 && axis < QwtPlot::axisCnt) {
344  m_IsLog[axis] = isLog;
345 
346  if (isLog) {
347  setAxisScaleEngine(axis, new QwtLogScaleEngine);
348  } else {
349  setAxisScaleEngine(axis, new QwtLinearScaleEngine);
350  }
351 
352  replot();
353  }
354 }
355 
356 void QcepPlot::contextMenuEvent(QContextMenuEvent *event)
357 {
358  QMenu plotMenu(NULL, NULL);
359 
360  QAction *xLog = plotMenu.addAction("Log X Axis");
361  QAction *yLog = plotMenu.addAction("Log Y Axis");
362  QAction *auSc = plotMenu.addAction("Autoscale");
363  QAction *prGr = plotMenu.addAction("Print Graph...");
364 
366 
367  if (set) {
368  xLog->setCheckable(true);
369  yLog->setCheckable(true);
370  xLog->setChecked(set->get_XAxisLog());
371  yLog->setChecked(set->get_YAxisLog());
372 
373  QAction *action = plotMenu.exec(event->globalPos());
374 
375  if (action == xLog) {
376  set->toggle_XAxisLog();
377  } else if (action == yLog) {
378  set->toggle_YAxisLog();
379  } else if (action == auSc) {
380  autoScale();
381  } else if (action == prGr) {
382  printGraph();
383  }
384  }
385 
386  event->accept();
387 }
388 
389 int QcepPlot::logAxis(int axis)
390 {
391  return m_IsLog[axis];
392 }
393 
394 QwtText QcepPlot::trackerTextF(const QPointF &pos)
395 {
397 
398  if (set) {
399  set->set_XMouse(pos.x());
400  set->set_YMouse(pos.y());
401  }
402 
403  return tr("%1, %2").arg(pos.x()).arg(pos.y());
404 }
QwtPlotMagnifier * m_Magnifier
Definition: qcepplot.h:61
void contextMenuEvent(QContextMenuEvent *event)
Definition: qcepplot.cpp:356
virtual ~QcepPlot()
Definition: qcepplot.cpp:32
QWeakPointer< QcepPlotSettings > QcepPlotSettingsWPtr
QcepPlot(QWidget *parent=0)
Definition: qcepplot.cpp:18
void setPlotCurveStyle(int index, QwtPlotCurve *curve)
Definition: qcepplot.cpp:103
QcepPlotSettingsWPtr m_PlotSettings
Definition: qcepplot.h:57
void setY2AxisLog(bool isLog)
Definition: qcepplot.cpp:336
QwtLegend * m_Legend
Definition: qcepplot.h:58
QwtPlotPanner * m_Panner
Definition: qcepplot.h:60
virtual void autoScale()
Definition: qcepplot.cpp:188
int m_IsLog[QwtPlot::axisCnt]
Definition: qcepplot.h:64
void setLogAxis(int axis, int isLog)
Definition: qcepplot.cpp:341
QwtPlotZoomer * m_Zoomer
Definition: qcepplot.h:59
virtual void onLegendClicked(const QVariant &itemInfo, int index)
Definition: qcepplot.cpp:273
void printGraph()
Definition: qcepplot.cpp:199
void enableMeasuring()
Definition: qcepplot.cpp:267
void init(QcepPlotSettingsWPtr settings)
Definition: qcepplot.cpp:36
int logAxis(int axis)
Definition: qcepplot.cpp:389
virtual QwtText trackerTextF(const QPointF &pos)
Definition: qcepplot.cpp:394
QSharedPointer< QcepPlotSettings > QcepPlotSettingsPtr
virtual void onLegendChecked(const QVariant &itemInfo, bool on, int index)
Definition: qcepplot.cpp:280
void setX2AxisLog(bool isLog)
Definition: qcepplot.cpp:331
void setYAxisLog(bool isLog)
Definition: qcepplot.cpp:326
void zoomIn()
Definition: qcepplot.cpp:242
QcepPlotMeasurerPtr m_Measurer
Definition: qcepplot.h:62
void setXAxisLog(bool isLog)
Definition: qcepplot.cpp:321
void updateZoomer()
Definition: qcepplot.cpp:252
void enableZooming()
Definition: qcepplot.cpp:261
void zoomOut()
Definition: qcepplot.cpp:247