Bug Summary

File:OMOptim/OMOptim/build/../GUI/Plots/MOOptPlot.h
Warning:line 497, column 20
Value stored to 'picker' during its initialization is never read

Annotated Source Code

[?] Use j/k keys for keyboard navigation

1// $Id$
2/**
3 * This file is part of OpenModelica.
4 *
5 * Copyright (c) 1998-CurrentYear, Open Source Modelica Consortium (OSMC),
6 * c/o Linköpings universitet, Department of Computer and Information Science,
7 * SE-58183 Linköping, Sweden.
8 *
9 * All rights reserved.
10 *
11 * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
12 * THIS OSMC PUBLIC LICENSE (OSMC-PL).
13 * ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE
14 * OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3, ACCORDING TO RECIPIENTS CHOICE.
15 *
16 * The OpenModelica software and the Open Source Modelica
17 * Consortium (OSMC) Public License (OSMC-PL) are obtained
18 * from OSMC, either from the above address,
19 * from the URLs: http://www.ida.liu.se/projects/OpenModelica or
20 * http://www.openmodelica.org, and in the OpenModelica distribution.
21 * GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
22 *
23 * This program is distributed WITHOUT ANY WARRANTY; without
24 * even the implied warranty of MERCHANTABILITY or FITNESS
25 * FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
26 * IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
27 *
28 * See the full OSMC Public License conditions for more details.
29 *
30 * Main contributor 2010, Hubert Thierot, CEP - ARMINES (France)
31
32 @file MOOptPlot.h
33 @brief Comments for file documentation.
34 @author Hubert Thieriot, hubert.thieriot@mines-paristech.fr
35 Company : CEP - ARMINES (France)
36 http://www-cep.ensmp.fr/english/
37 @version
38
39*/
40
41#ifndef _MINOPTPLOT_H
42#define _MINOPTPLOT_H
43
44#include <qwt_plot.h>
45#include <qwt_painter.h>
46#include <qwt_plot_canvas.h>
47#include <qwt_plot_marker.h>
48#include <qwt_plot_zoomer.h>
49#include <qwt_plot_curve.h>
50#include <qwt_scale_widget.h>
51#include <qwt_legend.h>
52#include <qwt_symbol.h>
53#include <qwt_scale_draw.h>
54#include <qwt_math.h>
55#include <qwt_dial.h>
56#include <qpaintengine.h>
57#include <qwt_plot_grid.h>
58#if QWT_VERSION0x060200 >= 0x060000
59#include "qwt_plot_renderer.h"
60#endif
61
62#include <QPainter>
63#include <QEvent>
64#include <QApplication>
65#include <QPaintEvent>
66#include <QMenu>
67#include <QClipboard>
68#include "Variable.h"
69#include "Plots/MyQwtPlotPicker.h"
70#include "InfoSender.h"
71
72class MOOptPlot : public QwtPlot
73{
74 Q_OBJECTpublic: clang diagnostic push clang diagnostic ignored "-Winconsistent-missing-override"
static const QMetaObject staticMetaObject; virtual const QMetaObject
*metaObject() const; virtual void *qt_metacast(const char *)
; virtual int qt_metacall(QMetaObject::Call, int, void **); static
inline QString tr(const char *s, const char *c = nullptr, int
n = -1) { return staticMetaObject.tr(s, c, n); } static inline
QString trUtf8(const char *s, const char *c = nullptr, int n
= -1) { return staticMetaObject.tr(s, c, n); } private: __attribute__
((visibility("hidden"))) static void qt_static_metacall(QObject
*, QMetaObject::Call, int, void **); clang diagnostic pop struct
QPrivateSignal {};
75public:
76 inline MOOptPlot(void);
77 ~MOOptPlot(void){}
78 inline void refresh(int);
79#if QWT_VERSION0x060200 >= 0x060000
80 inline int getNearestPointIndex(QwtPlotCurve *,const QPointF &);
81#else
82 inline int getNearestPointIndex(QwtPlotCurve *,const QwtDoublePoint &);
83#endif
84 inline void setSelectionPoint(int,bool _add);
85 inline void setSelectionPoints(QList<int>);
86 inline QList<int> getSelectedPoints();
87
88 inline void setShownPoint(int,bool _add);
89 inline void setShownPoints(QList<int>);
90 inline QList<int> getShownPoints();
91
92 template<typename OptVarObjResultX, typename OptVarObjResultY>
93 void setXYVar(OptVarObjResultX*,OptVarObjResultY*);
94
95
96#if QWT_VERSION0x060200 < 0x060000
97 inline virtual void drawItems (QPainter *painter, const QRect &rect,
98 const QwtScaleMap map[axisCnt], const QwtPlotPrintFilter &pfilter) const;
99#endif
100 inline void drawFrame(QPainter *p, const QRect& rect);
101 inline virtual void paintEvent(QPaintEvent *e);
102
103private:
104
105 QString _xVarName,_yVarName;
106 std::vector<double> _xData,_yData;
107
108
109 QList<int> _selectedPoints;
110 QList<int> _shownPoints;
111
112 QwtPlotZoomer *_zoomer;
113 QwtPlotCurve *_curve1;
114 QwtPlotCurve *_curve2;
115 MyQwtPlotPicker *_picker1;
116
117 QAction* _actionCopy;
118 QAction* _actionRefresh;
119
120public slots:
121 inline void onClicked(const QPointF & pos);
122 inline void onExtSelectionChanged(QList<int> &);
123 inline void onPickerAppended (const QPoint &pos);
124 inline void popUpMenu(const QPoint &pos);
125 inline void onCopyAsked();
126 inline void onRefreshAsked();
127
128signalspublic :
129 inline void selectionChanged(QList<int> &);
130};
131
132
133MOOptPlot::MOOptPlot()
134{
135 //_curve1 initialization
136 _curve1 = new QwtPlotCurve();
137 QwtSymbol sym1;
138 sym1.setStyle(QwtSymbol::Ellipse);
139 sym1.setSize(6);
140 sym1.setPen(QPen(Qt::red));
141 sym1.setBrush(QBrush(Qt::red));
142#if QWT_VERSION0x060200 >= 0x060000
143 _curve1->setSymbol(&sym1);
144#else
145 _curve1->setSymbol(sym1);
146#endif
147 _curve1->setStyle(QwtPlotCurve::NoCurve);
148 _curve1->setItemAttribute(QwtPlotItem::AutoScale,true);
149 _curve1->attach(this);
150
151 //_curve2 initialization
152 _curve2 = new QwtPlotCurve();
153 QwtSymbol sym2;
154 sym2.setStyle(QwtSymbol::Ellipse);
155 sym2.setSize(15);
156 sym2.setPen(QPen(Qt::blue));
157 sym2.setBrush(QBrush(Qt::blue));
158#if QWT_VERSION0x060200 >= 0x060000
159 _curve2->setSymbol(&sym2);
160#else
161 _curve2->setSymbol(sym2);
162#endif
163 _curve2->setStyle(QwtPlotCurve::NoCurve);
164 _curve2->setItemAttribute(QwtPlotItem::AutoScale,true);
165 _curve2->attach(this);
166
167 _selectedPoints.clear();
168
169 // Style
170
171 // Font
172 QFont axisFont(QApplication::font().family(),8,QFont::Normal);
173 axisFont.setStyleStrategy(QFont::PreferAntialias);
174 this->canvas()->setFont(axisFont);
175 this->setAxisFont(QwtPlot::xBottom,axisFont);
176 this->setAxisFont(QwtPlot::yLeft,axisFont);
177
178 // box
179 QwtPlotCanvas *pPlotCanvas = static_cast<QwtPlotCanvas*>(this->canvas());
180 pPlotCanvas->setFrameStyle(QFrame::NoFrame);
181 this->setFrameStyle(QFrame::StyledPanel);
182
183 // Colors
184 QColor background = QColor(Qt::white);
185 background.setAlpha(255);
186 QPalette palette;
187 palette.setColor(QPalette::Window, background);
188 this->setPalette(palette);
189 this->setCanvasBackground(background);
190
191
192 // Zommer
193 _zoomer = new QwtPlotZoomer(canvas());
194 _zoomer->setMousePattern(QwtEventPattern::MouseSelect1, Qt::MidButton);
195 _zoomer->setMousePattern(QwtEventPattern::MouseSelect2, Qt::NoButton);
196 _zoomer->setMousePattern(QwtEventPattern::MouseSelect3, Qt::NoButton);
197
198 // Picker
199 _picker1 = new MyQwtPlotPicker(canvas());
200 _picker1->setTrackerMode(QwtPicker::AlwaysOff);
201#if QWT_VERSION0x060200 >= 0x060000
202 /*! @todo Subclass QwtPickerMachine and provide a transition function to enable selection flags.
203 */
204 connect(_picker1, SIGNAL(selected(QPointF))"2""selected(QPointF)",this, SLOT(onClicked(QPointF))"1""onClicked(QPointF)");
205#else
206 _picker1->setSelectionFlags(QwtPicker::PointSelection | QwtPicker::RectSelection );
207 connect(_picker1, SIGNAL(selected(const QwtDoublePoint &))"2""selected(const QwtDoublePoint &)",this, SLOT(onClicked(const QPointF &))"1""onClicked(const QPointF &)");
208#endif
209 // connect(_picker1, SIGNAL(selected(const QwtDoubleRect &rect)),this, SLOT(onPickerSelected(const QwtDoubleRect &rect)));
210 // connect(_picker1, SIGNAL(appended (const QPoint &pos)),this, SLOT(onPickerAppended(const QPoint &pos)));
211
212
213 // action
214 _actionCopy = new QAction("Copy", this);
215 connect(_actionCopy,SIGNAL(triggered())"2""triggered()",this, SLOT(onCopyAsked())"1""onCopyAsked()");
216 _actionCopy->setShortcut(QKeySequence::Copy);
217 _actionCopy->setShortcutContext(Qt::WidgetShortcut);
218 this->addAction(_actionCopy);
219
220 _actionRefresh = new QAction("Refresh", this);
221 connect(_actionRefresh,SIGNAL(triggered())"2""triggered()",this, SLOT(onRefreshAsked())"1""onRefreshAsked()");
222 _actionRefresh->setShortcut(QKeySequence::Refresh);
223 _actionRefresh->setShortcutContext(Qt::WidgetShortcut);
224 this->addAction(_actionRefresh);
225
226
227 // context menu
228 this->setContextMenuPolicy(Qt::CustomContextMenu);
229 connect(this,SIGNAL(customContextMenuRequested(const QPoint & ))"2""customContextMenuRequested(const QPoint & )",this,SLOT(popUpMenu(const QPoint &))"1""popUpMenu(const QPoint &)");
230
231 /*this->setCanvasBackground(QColor(Qt::white));
232 QPalette _pal = this->palette();
233 _pal.setColor(QPalette::Window,Qt::white);
234 _pal.setColor(QPalette::Background,Qt::white);
235 this->setPalette(_pal);
236 this->canvas()->setPalette(_pal);
237
238 this->setBackgroundRole(QPalette::Background);*/
239
240
241 // grid
242 QwtPlotGrid *grid = new QwtPlotGrid;
243 grid->enableXMin(false);
244 grid->enableYMin(false);
245#if QWT_VERSION0x060200 >= 0x060100
246 grid->setMajorPen(QPen(Qt::lightGray, 0, Qt::SolidLine));
247 grid->setMinorPen(QPen(Qt::lightGray, 0 , Qt::SolidLine));
248#else
249 grid->setMajPen(QPen(Qt::lightGray, 0, Qt::SolidLine));
250 grid->setMinPen(QPen(Qt::lightGray, 0 , Qt::SolidLine));
251#endif
252 grid->attach(this);
253
254 // margins
255 this->setContentsMargins(5,5,5,5);
256 //this->canvas()->setContentsMargins(5,5,5,5);
257
258}
259
260void MOOptPlot::setSelectionPoint(int selectedPoint, bool add)
261{
262 if(add)
263 {
264 if(!_selectedPoints.contains(selectedPoint))
265 {
266 _selectedPoints.push_back(selectedPoint);
267 refresh(2);
268 }
269 }
270 else
271 {
272 if((_selectedPoints.size()!=1)||(selectedPoint != _selectedPoints.at(0)))
273 {
274 _selectedPoints.clear();
275 if(selectedPoint!=-1)
276 _selectedPoints.push_back(selectedPoint);
277
278 refresh(2);
279 }
280 }
281}
282
283void MOOptPlot::setShownPoint(int shownPoint, bool add)
284{
285 if(add)
286 {
287 if(!_shownPoints.contains(shownPoint))
288 {
289 _shownPoints.push_back(shownPoint);
290 refresh(2);
291 }
292 }
293 else
294 {
295 if((_shownPoints.size()!=1)||(shownPoint != _shownPoints.at(0)))
296 {
297 _shownPoints.clear();
298 _shownPoints.push_back(shownPoint);
299 refresh(2);
300 }
301 }
302}
303
304
305void MOOptPlot::setSelectionPoints(QList<int> selectedPoints)
306{
307 if(_selectedPoints != selectedPoints)
308 {
309 _selectedPoints = selectedPoints;
310 refresh(2);
311 }
312}
313
314void MOOptPlot::setShownPoints(QList<int> shownPoints)
315{
316 _shownPoints = shownPoints;
317 // remove selected that are not shown anymore
318 int i=0;
319 while(i<_selectedPoints.size())
320 {
321 if(!_shownPoints.contains(_selectedPoints.at(i)))
322 _selectedPoints.removeAt(i);
323 else
324 i++;
325 }
326 refresh(0);
327}
328
329QList<int> MOOptPlot::getSelectedPoints()
330{
331 return _selectedPoints;
332}
333
334QList<int> MOOptPlot::getShownPoints()
335{
336 return _shownPoints;
337}
338
339void MOOptPlot::refresh(int iCurve = 0)
340{
341 // iCurve = 0 -> refresh both curves
342 // iCurve = 1 -> refresh _curve1 only
343 // iCurve = 2 -> refresh _curve2 only
344
345 double* dataX;
346 double* dataY;
347 int iPoint;
348
349 if((iCurve==0)||(iCurve==1))
350 {
351 //Plot _curve1
352 int nbPoints = _shownPoints.size();
353
354 dataX = new double[nbPoints];
355 dataY = new double[nbPoints];
356
357 for (int i=0;i<nbPoints;i++)
358 {
359 iPoint = _shownPoints.at(i);
360 if(iPoint<std::min(_xData.size(),_yData.size()))
361 {
362 dataX[i]=_xData.at(_shownPoints.at(i));
363 dataY[i]=_yData.at(_shownPoints.at(i));
364 }
365 else
366 {
367 QString msg;
368 msg.sprintf("MOOptPlot : tried to reach point number %d but data.length = %d",
369 iPoint,std::min(_xData.size(),_yData.size()));
370 InfoSender::instance()->debug(msg);
371 }
372 }
373#if QWT_VERSION0x060200 >= 0x060000
374 _curve1->setSamples(dataX,dataY,nbPoints);
375#else
376 _curve1->setData(dataX,dataY,nbPoints);
377#endif
378 _curve1->setItemAttribute(QwtPlotItem::AutoScale,true);
379 delete[] dataX;
380 delete[] dataY;
381 }
382
383 if((iCurve==0)||(iCurve==2))
384 {
385 if(_selectedPoints.size()>0)
386 {
387 dataX = new double[_selectedPoints.size()];
388 dataY = new double[_selectedPoints.size()];
389
390 for(int i=0;i<_selectedPoints.size();i++)
391 {
392 iPoint = _selectedPoints.at(i);
393 if(iPoint<std::min(_xData.size(),_yData.size()))
394 {
395 dataX[i]=_xData.at(_selectedPoints.at(i));
396 dataY[i]=_yData.at(_selectedPoints.at(i));
397 }
398 else
399 {
400 QString msg;
401 msg.sprintf("MOOptPlot : tried to reach point number %d but data.length = %d",
402 iPoint,std::min(_xData.size(),_yData.size()));
403 InfoSender::instance()->debug(msg);
404 }
405 }
406#if QWT_VERSION0x060200 >= 0x060000
407 _curve2->setSamples(dataX,dataY,_selectedPoints.size());
408#else
409 _curve2->setData(dataX,dataY,_selectedPoints.size());
410#endif
411 _curve2->setItemAttribute(QwtPlotItem::AutoScale,true);
412 _curve2->show();
413 delete[] dataX;
414 delete[] dataY;
415 }
416 else
417 {
418 _curve2->hide();
419 }
420 }
421 replot();
422 if((iCurve==0)||(iCurve==1))
423 _zoomer->setZoomBase();
424
425}
426
427template<typename OptVarObjResultX, typename OptVarObjResultY>
428void MOOptPlot::setXYVar(OptVarObjResultX* varX,OptVarObjResultY* varY)
429{
430 _xVarName = varX->name();
431 _yVarName = varY->name();
432
433 _xData = varX->finalValuesAtScan(0).toStdVector();
434 _yData = varY->finalValuesAtScan(0).toStdVector();
435
436
437 // check that selected and shown points index are compatible with new values
438 int index=0;
439 int nbPoints = _xData.size();
440 while(index<_shownPoints.size())
441 {
442 if(_shownPoints.at(index)>=nbPoints)
443 _shownPoints.removeAt(index);
444 else
445 index++;
446 }
447
448 index=0;
449 while(index<_selectedPoints.size())
450 {
451 if(_selectedPoints.at(index)>=nbPoints)
452 _selectedPoints.removeAt(index);
453 else
454 index++;
455 }
456
457
458 refresh();
459}
460
461#if QWT_VERSION0x060200 >= 0x060000
462int MOOptPlot::getNearestPointIndex(QwtPlotCurve * curve,const QPointF & point)
463#else
464int MOOptPlot::getNearestPointIndex(QwtPlotCurve * curve,const QwtDoublePoint & point)
465#endif
466{
467
468 double minDist=0;
469 double curDist=0;
470
471 int xPos = transform(QwtPlot::xBottom,point.x());
472 int yPos = transform(QwtPlot::yLeft,point.y());
473
474 int xPointPos;
475 int yPointPos;
476
477 int indexMin = -1;
478
479 for(int iShown=0;iShown<_shownPoints.size();iShown++)
480 {
481 xPointPos = transform(QwtPlot::xBottom,_xData.at(_shownPoints.at(iShown)));
482 yPointPos = transform(QwtPlot::yLeft,_yData.at(_shownPoints.at(iShown)));
483
484 curDist = sqrt(pow((double)(yPointPos-yPos),2)+pow((double)(xPointPos-xPos),2));
485 if((curDist<minDist)|| (iShown==0))
486 {
487 indexMin = _shownPoints.at(iShown);
488 minDist = curDist;
489 }
490 }
491 return indexMin;
492}
493
494void MOOptPlot::onClicked(const QPointF & pos)
495{
496
497 QwtPlotPicker *picker = qobject_cast<QwtPlotPicker *>(sender());
Value stored to 'picker' during its initialization is never read
498
499 int provSelectedPoint = getNearestPointIndex(_curve1,pos);
500
501 setSelectionPoint(provSelectedPoint,_picker1->ctrlPressed);
502
503 // emit signal to synchronize
504 QList<int> list = getSelectedPoints();
505 emit selectionChanged(list);
506}
507
508#if QWT_VERSION0x060200 < 0x060000
509void MOOptPlot::drawItems (QPainter *painter, const QRect &rect,
510 const QwtScaleMap map[axisCnt], const QwtPlotPrintFilter &pfilter) const
511{
512
513 painter->save();
514
515 const QwtPlotItemList& itmList = itemList();
516 for ( QwtPlotItemIterator it = itmList.begin();
517 it != itmList.end(); ++it )
518 {
519 QwtPlotItem *item = *it;
520 if ( item && item->isVisible() )
521 {
522 if ( !(pfilter.options() & QwtPlotPrintFilter::PrintGrid)
523 && item->rtti() == QwtPlotItem::Rtti_PlotGrid )
524 {
525 continue;
526 }
527
528#if QT_VERSION((5<<16)|(9<<8)|(5)) >= 0x040000
529 painter->setRenderHint(QPainter::Antialiasing,true);
530#endif
531
532 item->draw(painter,
533 map[item->xAxis()], map[item->yAxis()],
534 rect);
535 }
536 }
537
538 painter->restore();
539}
540#endif
541
542void MOOptPlot::drawFrame(QPainter *p, const QRect& rect)
543{
544 p->save();
545
546 int lw = 1;
547 QRect r = rect.adjusted(lw, lw, -lw - 1, -lw - 1);
548
549 p->fillRect(r,palette().color(QPalette::Window));
550 p->setBrush(QBrush(Qt::white));
551 QwtPainter::drawRect(p, r);
552
553 p->restore();
554}
555
556void MOOptPlot::paintEvent(QPaintEvent *e)
557{
558 QPainter p(this);
559 drawFrame(&p, rect());
560 e->accept();
561}
562
563void MOOptPlot::onExtSelectionChanged(QList<int> & newSel)
564{
565 if(_selectedPoints != newSel)
566 {
567 setSelectionPoints(newSel);
568 }
569}
570
571
572//*********************
573// Picker
574//*********************
575void MOOptPlot::onPickerAppended (const QPoint &pos)
576{
577 bool ctrlPressed = _picker1->ctrlPressed;
578}
579
580
581void MOOptPlot::popUpMenu(const QPoint &pos)
582{
583 QMenu *menu = new QMenu;
584 menu->insertAction(NULL__null,_actionCopy);
585 menu->insertAction(NULL__null,_actionRefresh);
586
587 /*
588 menu->addAction(QString("Copy"), this, SLOT(onCopyAsked()), QKeySequence::Copy);
589 menu->addAction(QString("Replot"), this, SLOT(onRefreshAsked()));*/
590 menu->exec(this->mapToGlobal(pos));
591
592}
593
594void MOOptPlot::onRefreshAsked()
595{
596 qDebugQMessageLogger(nullptr, 0, nullptr).debug("replot");
597 replot();
598}
599
600void MOOptPlot::onCopyAsked()
601{
602 QImage image;
603 // Print the plot to an image
604#if QWT_VERSION0x060200 < 0x060000
605 print( image );
606#else
607 QwtPlotRenderer plotRenderer;
608 plotRenderer.renderTo(this, image);
609#endif
610
611 // Set the clilpboard image
612 QClipboard * clipboard =
613 QApplication::clipboard();
614 clipboard->setImage(image);
615
616
617 // create text
618 QString csv;
619 QwtPlotCurve* curve;
620 QString separator = "\t";
621 QList<QwtPlotCurve*> curves;
622 curves << _curve1 << _curve2;
623 for(int i=0;i<curves.size();i++)
624 {
625 curve = curves.at(i);
626 QString yTitle = this->axisTitle(QwtPlot::yLeft).text();
627 QString xTitle = this->axisTitle(QwtPlot::xBottom).text();
628 csv+= xTitle + separator + yTitle + "\n";
629#if QWT_VERSION0x060200 >= 0x060000
630 for(unsigned int j=0;j<curve->data()->size();j++)
631 {
632 csv+= QString::number(curve->sample(j).x()) + separator + QString::number(curve->sample(j).y()) + "\n";
633 csv+= "\n \n";
634 }
635#else
636 for(unsigned int j=0;j<curve->data().size();j++)
637 {
638 csv+= QString::number(curve->x(j)) + separator + QString::number(curve->y(j)) + "\n";
639 csv+= "\n \n";
640 }
641#endif
642 }
643 clipboard->setText(csv);
644}
645
646
647
648#endif