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