Salome HOME
f1732b97662d7678e1c8649bb0ad07a01a92efcb
[modules/gui.git] / src / Plot2d / Plot2d_ViewFrame.h
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #ifndef PLOT2D_VIEWFRAME_H
24 #define PLOT2D_VIEWFRAME_H
25
26 #include "Plot2d.h"
27 #include "Plot2d_Curve.h"
28 #ifndef NO_ANALYTICAL_CURVES
29 #include "Plot2d_AnalyticalCurve.h"
30 #endif
31 #include "Plot2d_NormalizeAlgorithm.h"
32
33 #include <QWidget>
34 #include <QMultiHash>
35 #include <QMap>
36 #include <QList>
37 #include <QPainter>
38 #include <qwt_symbol.h>
39 #include <qwt_scale_draw.h>
40 #include <qwt_plot_marker.h>
41 #include <qwt_plot_picker.h>
42
43 #include <iostream>
44 #include <ostream>
45
46 class Plot2d_Plot2d;
47 class Plot2d_Prs;
48 class Plot2d_Curve;
49 class Plot2d_Object;
50 class QCustomEvent;
51 class QwtPlotItem;
52 class QwtPlotCurve;
53 class QwtPlotGrid;
54 class QwtPlotZoomer;
55 class Plot2d_AxisScaleDraw;
56 class Plot2d_QwtPlotPicker;
57
58 typedef QMultiHash<QwtPlotCurve*, Plot2d_Curve*>  CurveDict;
59 typedef QMultiHash<QwtPlotItem*,  Plot2d_Object*> ObjectDict;
60
61 class PLOT2D_EXPORT Plot2d_ViewFrame : public QWidget
62 #ifndef NO_ANALYTICAL_CURVES
63   , public Plot2d_CurveContainer
64 #endif
65 {
66   Q_OBJECT
67
68   enum { NoOpId, FitAreaId, ZoomId, PanId, GlPanId, DumpId,
69          ModeXLinearId, ModeXLogarithmicId, ModeYLinearId, ModeYLogarithmicId,
70          LegendId, CurvePointsId, CurveLinesId, CurveSplinesId };
71 public:
72   /* Construction/destruction */
73   Plot2d_ViewFrame( QWidget*, const QString& = "" );
74   virtual ~Plot2d_ViewFrame();
75
76   void Init();
77
78   enum ObjectType { MainTitle, XTitle, YTitle, Y2Title, XAxis, YAxis, Y2Axis };
79
80   QWidget*       getViewWidget();
81
82   /* display */
83   virtual void   DisplayAll();
84   virtual void   EraseAll();
85   void           Repaint();
86
87   void           Display( const Plot2d_Prs* );
88   void           Erase( const Plot2d_Prs*, const bool = false );
89   Plot2d_Prs*    CreatePrs( const char* = 0 );
90
91   virtual bool   eventFilter( QObject*, QEvent* );
92
93   /* operations */
94   void           updateTitles( const bool = true );
95   void           setTitle( const QString& );
96   QString        getTitle() const;
97
98   QVector< QVector<QwtPlotCurve *> > displayPlot2dCurveList( const QList< QList<Plot2d_Curve*> >& sysCoCurveList,
99                                                              bool                                 displayLegend,
100                                                              const QList< QList<bool> >&          sides);
101
102   QVector< QVector<QwtPlotCurve *> > displayPlot2dCurveList( const QList<Plot2d_Curve*>&  curveList,
103                                                              int  groupsize,
104                                                              bool  displayLegend,
105                                                              const QList< bool >& sides);
106
107   Plot2d_Curve* createPlot2dCurve( QString & title,
108                                    QString & unit,
109                                    QList<double> & xList,
110                                    QList<double> & yList,
111                                    QList<QString> & tooltipList,
112                                    Plot2d::LineType lineKind,
113                                    int lineWidth,
114                                    QColor & lineColor,
115                                    QwtSymbol::Style markerKind,
116                                    Plot2d_QwtPlotPicker* picker,
117                                    bool toDraw,
118                                    bool displayLegend=true);
119
120   QColor getPlot2dCurveColor( Plot2d_Curve* plot2dCurve);
121
122   QwtPlotCurve *createSegment( double *X, double *Y, int nbPoint,
123                                Qt::PenStyle lineKind,
124                                int lineWidth,
125                                QColor & lineColor,
126                                QwtSymbol::Style markerKind,
127                                bool side=false);
128
129   /* curves operations [ obsolete ] */
130   void           displayCurve( Plot2d_Curve*, bool = false );
131   void           displayCurves( const curveList&, bool = false );
132   void           eraseCurve( Plot2d_Curve*, bool = false );
133   void           eraseCurves( const curveList&, bool = false );
134   int            getCurves( curveList& ) const;
135   CurveDict      getCurves() const;
136   void           updateCurve( Plot2d_Curve*, bool = false );
137   void           processFiltering(bool = false);
138
139   /* objects operations */
140   QwtPlotItem*   displayObject( Plot2d_Object*, bool = false );
141   void           displayObjects( const objectList&, bool = false );
142   void           eraseObject( Plot2d_Object*, bool = false );
143   void           eraseObjects( const objectList&, bool = false );
144   void           eraseBasicObject( QwtPlotItem*, bool = false );
145   void           eraseBasicObjects( const QList<QwtPlotItem*> &, bool = false );
146   int            getObjects( objectList& ) const;
147   bool           isVisible( Plot2d_Object* ) const;
148   void           updateObject( Plot2d_Object*, bool = false );
149
150   void           updateLegend( const Plot2d_Prs* );
151   void           updateLegend();
152   void           fitAll();
153   void           fitArea( const QRect& );
154   void           fitData( const int, const double, const double,
155                           const double, const double,
156                           const double = 0, const double = 0 );
157
158   void           getFitRanges( double&, double&, double&, double&,
159                                double&, double&);
160
161   void           getFitRangeByCurves( double&, double&, double&, double&,
162                                       double&, double& );
163
164   void           getFitRangeByMarkers(double&, double&, double&, double&,
165                                       double&, double& );
166
167 #ifndef NO_ANALYTICAL_CURVES
168   void              addAnalyticalCurve( Plot2d_AnalyticalCurve* );
169   void              removeAnalyticalCurve( Plot2d_AnalyticalCurve* );
170   void              updateAnalyticalCurve( Plot2d_AnalyticalCurve*, bool = false );
171   void              updateAnalyticalCurves();
172   void              deselectAnalyticalCurves();
173
174   AnalyticalCurveList getAnalyticalCurves() const;
175   Plot2d_AnalyticalCurve* getAnalyticalCurve(QwtPlotItem *);
176 #endif
177
178   void              deselectObjects();
179
180   /* view parameters */
181   void           copyPreferences( Plot2d_ViewFrame* );
182   void           setCurveType( int, bool = true );
183   int            getCurveType() const;
184   void           setCurveTitle( Plot2d_Curve*, const QString& );
185   void           setObjectTitle( Plot2d_Object*, const QString& );
186   void           showLegend( bool, bool = true );
187   void           setLegendPos( int );
188   int            getLegendPos() const;
189   void           setLegendFont( const QFont& );
190   QFont          getLegendFont() const;
191   void           setLegendFontColor( const QColor& );
192   QColor         getLegendFontColor() const;
193   void           setMarkerSize( const int, bool = true  );
194   int            getMarkerSize() const;
195   virtual void   setBackgroundColor( const QColor& );
196   QColor         backgroundColor() const;
197   void           setXGrid( bool, const int, bool, const int, bool = true );
198   void           setYGrid( bool, const int, bool, const int,
199                            bool, const int, bool, const int, bool = true );
200   void           setTitle( bool, const QString&, ObjectType, bool = true );
201   QString        getTitle( ObjectType ) const;
202
203   void           setFont( const QFont&, ObjectType, bool = true );
204   void           setHorScaleMode( const int, bool = true );
205   int            getHorScaleMode() const;
206   void           setVerScaleMode( const int, bool = true );
207   int            getVerScaleMode() const;
208   void           setNormLMaxMode( bool, bool = true);
209   bool           getNormLMaxMode()const;
210   void           setNormLMinMode( bool, bool = true);
211   bool           getNormLMinMode()const;
212   void           setNormRMaxMode( bool, bool = true);
213   bool           getNormRMaxMode()const;
214   void           setNormRMinMode( bool, bool = true);
215   bool           getNormRMinMode()const;
216
217
218   bool           isModeHorLinear();
219   bool           isModeVerLinear();
220   bool           isNormLMaxMode();
221   bool           isNormLMinMode();
222   bool           isNormRMaxMode();
223   bool           isNormRMinMode();
224
225   bool           isLegendShow() const;
226
227   // Protection against QwtCurve::drawLines() bug in Qwt 0.4.x:
228   // it crashes if switched to X/Y logarithmic mode, when one or more points have
229   // non-positive X/Y coordinate
230   bool           isXLogEnabled() const;
231   bool           isYLogEnabled() const;
232   void           setEnableAxis( QwtPlot::Axis, bool );
233
234   virtual bool   print( const QString&, const QString& ) const;
235   void           printPlot( QPainter*, const QRect&,
236                             const QwtPlotPrintFilter& = QwtPlotPrintFilter() ) const;
237
238   QString        getVisualParameters();
239   void           setVisualParameters( const QString& );
240
241   void           incrementalPan ( const int, const int );
242   void           incrementalZoom( const int, const int );
243
244   QwtPlotCanvas* getPlotCanvas() const;
245   Plot2d_Curve*  getClosestCurve( QPoint, double&, int& ) const;
246
247   Plot2d_Object* getPlotObject( QwtPlotItem* ) const;
248   QwtPlotItem*   getPlotObject( Plot2d_Object* ) const;
249   QwtPlotCurve*  getPlotCurve( Plot2d_Curve* ) const;
250   Plot2d_Plot2d* getPlot() const { return myPlot; }
251
252   void           updatePlotItem(Plot2d_Object*, QwtPlotItem*);
253 protected:
254   int            testOperation( const QMouseEvent& );
255   virtual void   readPreferences();
256   virtual void   writePreferences();
257   QString        getInfo( const QPoint& );
258   virtual void   wheelEvent( QWheelEvent* );
259   bool           hasPlotCurve( Plot2d_Curve* ) const;
260   void           setCurveType( QwtPlotCurve*, int );
261   bool           hasPlotObject( Plot2d_Object* ) const;
262   QString        getXmlVisualParameters();
263   bool           setXmlVisualParameters(const QString&);
264
265 public slots:
266   void           onViewPan();
267   void           onViewZoom();
268   void           onViewFitAll();
269   void           onViewFitArea();
270   void           onViewGlobalPan();
271   void           onSettings();
272 #ifndef NO_ANALYTICAL_CURVES
273   void           onAnalyticalCurve();
274 #endif
275   void           onFitData();
276   void           onChangeBackground();
277   void           onPanLeft();
278   void           onPanRight();
279   void           onPanUp();
280   void           onPanDown();
281   void           onZoomIn();
282   void           onZoomOut();
283
284 protected:
285   virtual void   customEvent( QEvent* );
286   void           plotMousePressed( const QMouseEvent& );
287   bool           plotMouseMoved( const QMouseEvent& );
288   void           plotMouseReleased( const QMouseEvent& );
289
290 signals:
291   void           vpModeHorChanged();
292   void           vpModeVerChanged();
293   void           vpNormLModeChanged();
294   void           vpNormRModeChanged();
295   void           vpCurveChanged();
296   void           contextMenuRequested( QContextMenuEvent* );
297   void           legendClicked( QwtPlotItem* );
298
299 protected:
300   Plot2d_Plot2d*      myPlot;
301   int                 myOperation;
302   QPoint              myPnt;
303
304   int                 myCurveType;
305   bool                myShowLegend;
306   int                 myLegendPos;
307   QFont               myLegendFont;
308   QColor              myLegendColor;
309   int                 myMarkerSize;
310   QColor              myBackground;
311   QString             myTitle, myXTitle, myYTitle, myY2Title;
312   bool                myTitleEnabled, myXTitleEnabled, myYTitleEnabled, myY2TitleEnabled;
313   bool                myXGridMajorEnabled, myYGridMajorEnabled, myY2GridMajorEnabled;
314   bool                myXGridMinorEnabled, myYGridMinorEnabled, myY2GridMinorEnabled;
315   int                 myXGridMaxMajor, myYGridMaxMajor, myY2GridMaxMajor;
316   int                 myXGridMaxMinor, myYGridMaxMinor, myY2GridMaxMinor;
317   int                 myXMode, myYMode;
318   bool                myNormLMin, myNormLMax, myNormRMin, myNormRMax;
319   double              myXDistance, myYDistance, myYDistance2;
320   bool                mySecondY;
321   ObjectDict          myObjects;
322 #ifndef NO_ANALYTICAL_CURVES
323   AnalyticalCurveList myAnalyticalCurves;
324 #endif
325   Plot2d_NormalizeAlgorithm* myLNormAlgo;
326   Plot2d_NormalizeAlgorithm* myRNormAlgo;
327   bool                myIsDefTitle;
328  private:
329   // List of QwtPlotCurve curves to draw (created by Plot2d_Curve::createPlotItem() )
330   QList<QwtPlotItem*> myQwtPlotCurveList;
331
332   // List of intermittent segments to connect curves
333   QList<QwtPlotCurve*> myIntermittentSegmentList;
334
335   // List of curves Plot2d_Curve
336   QList<Plot2d_Curve*> myPlot2dCurveList;
337 };
338
339 class Plot2d_Plot2d : public QwtPlot
340 {
341   Q_OBJECT
342 public:
343   Plot2d_Plot2d( QWidget* );
344   virtual ~Plot2d_Plot2d();
345
346   void           setLogScale( int, bool );
347
348   void           replot();
349   QwtLegend*     getLegend();
350   QSize          sizeHint() const;
351   QSize          minimumSizeHint() const;
352   void           defaultPicker();
353   void           setPickerMousePattern( int, int = Qt::NoButton );
354   void           setPicker( Plot2d_QwtPlotPicker *picker);
355   Plot2d_QwtPlotPicker* getPicker() { return myPicker; }
356   Plot2d_AxisScaleDraw* getScaleDraw() { return myScaleDraw; }
357   QList<QwtPlotMarker*> getSeparationLineList() { return mySeparationLineList; }
358   void clearSeparationLineList();
359   QwtPlotMarker *createMarkerAndTooltip( QwtSymbol symbol,
360                                double    X,
361                                double    Y,
362                                QString & tooltip,
363                                Plot2d_QwtPlotPicker *picker);
364
365   bool           polished() const;
366   QwtPlotGrid*   grid() const;
367   QwtPlotZoomer* zoomer() const;
368
369   virtual void   updateYAxisIdentifiers();
370
371   // Methods to deal with axes ticks
372
373   void createAxisScaleDraw();
374   void applyTicks();
375   void unactivAxisScaleDraw( int numcall);
376
377   void displayXTicksAndLabels(
378          double XLeftmargin, double XRightMargin,
379          const QList< QPair< QString, QMap<double,QString> > > & devicesPosLabelTicks);
380
381   void createSeparationLine( double Xpos);
382
383
384 public slots:
385   virtual void   polish();
386
387 protected slots:
388   void           onScaleDivChanged();
389
390 protected:
391   QwtPlotGrid*   myGrid;
392   QList<QColor>  myColors;
393   bool           myIsPolished;
394   QwtPlotZoomer* myPlotZoomer;
395   Plot2d_AxisScaleDraw* myScaleDraw;
396   // The point picker associated with the graphic view
397   Plot2d_QwtPlotPicker *myPicker;
398 private:
399   // List of verticals segments between two curves
400   QList<QwtPlotMarker*> mySeparationLineList;
401 };
402
403 class Plot2d_ScaleDraw: public QwtScaleDraw
404 {
405 public:
406   Plot2d_ScaleDraw( char f = 'g', int prec = 6 );
407   Plot2d_ScaleDraw( const QwtScaleDraw& scaleDraw, char f = 'g', int prec = 6 );
408
409   virtual QwtText label( double value ) const;
410
411   int precision() const { return myPrecision; }
412
413 private:
414   char myFormat;
415   int  myPrecision;
416 };
417
418 class Plot2d_YScaleDraw: public QwtScaleDraw
419 {
420 public:
421   Plot2d_YScaleDraw();
422
423   virtual QwtText label( double value ) const;
424 };
425
426 /* Definition of X axis graduations
427  */
428 class Plot2d_AxisScaleDraw: public QwtScaleDraw
429 {
430 public:
431   static const QString DEVICE_FONT;
432   static const int     DEVICE_FONT_SIZE;
433   static const int     DEVICE_BY;
434
435   Plot2d_AxisScaleDraw(Plot2d_Plot2d* plot);
436
437   virtual ~Plot2d_AxisScaleDraw();
438
439   void unactivTicksDrawing( int numcall);
440
441   virtual void draw( QPainter * painter, const QPalette & palette) const;
442
443   virtual QwtText label(double value) const;
444
445   void setLabelTick(double value, QString label, bool isDevice = false);
446
447   void setTicks(const QList<double> aTicks);
448
449   void setInterval(double lowerBound, double upperBound);
450
451   void applyTicks();
452
453 protected:
454
455   void drawLabel( QPainter* painter, double value) const;
456
457   void drawTick( QPainter* painter, double value, int len) const;
458
459 private:
460   bool myActivTicksDrawing;   // true => activate drawing ticks (with draw() )
461   int  myNumTicksDrawingCall; // call number to ticks drawing
462
463   // Ticks list to display on X axis
464   QMap<double, QString> myLabelX;  // position, label
465   //
466   QList<double> myTicks;  // positions
467
468   // Systems names to display under X axis
469   QMap<double, QString> myLabelDevice;
470
471   Plot2d_Plot2d* myPlot;  // Drawing zone QwtPlot
472
473   double myLowerBound;
474   double myUpperBound;
475 };
476
477
478
479
480 /* Management of tooltips associated with markers for curves points or others points
481  */
482 class Plot2d_QwtPlotPicker : public QwtPlotPicker
483 {
484 public:
485   static const double BOUND_HV_SIZE;
486
487   Plot2d_QwtPlotPicker( int            xAxis,
488                         int            yAxis,
489                         int            selectionFlags,
490                         RubberBand     rubberBand,
491                         DisplayMode    trackerMode,
492                         QwtPlotCanvas *canvas);
493
494   Plot2d_QwtPlotPicker( int  xAxis,
495                         int  yAxis,
496                         QwtPlotCanvas *canvas);
497
498   virtual ~Plot2d_QwtPlotPicker();
499
500   QList<QwtPlotMarker*>             pMarkers;         // points markers
501   QMap<QwtPlotMarker*, QwtText>  pMarkersToolTip;  // associations (marker,tooltip)
502
503 protected:
504
505   virtual QwtText trackerText( const QwtDoublePoint & pos ) const;
506
507 };
508
509 #endif