Salome HOME
Merge from V6_main 13/12/2012
[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           getFitRangeByMarkers(double&, double&, double&, double&,
158                                       double&, double& );
159
160   void              addAnalyticalCurve( Plot2d_AnalyticalCurve* );
161   void              removeAnalyticalCurve( Plot2d_AnalyticalCurve* );
162   void              updateAnalyticalCurve( Plot2d_AnalyticalCurve*, bool = false );
163   void              updateAnalyticalCurves();
164   void              deselectAnalyticalCurves(); 
165   void              deselectObjects();  
166
167   AnalyticalCurveList getAnalyticalCurves() const;
168   Plot2d_AnalyticalCurve* getAnalyticalCurve(QwtPlotItem *);
169
170   /* view parameters */
171   void           copyPreferences( Plot2d_ViewFrame* );
172   void           setCurveType( int, bool = true );
173   int            getCurveType() const;
174   void           setCurveTitle( Plot2d_Curve*, const QString& );
175   void           setObjectTitle( Plot2d_Object*, const QString& );
176   void           showLegend( bool, bool = true );
177   void           setLegendPos( int );
178   int            getLegendPos() const;
179   void           setLegendFont( const QFont& );
180   QFont          getLegendFont() const;
181   void           setLegendFontColor( const QColor& );
182   QColor         getLegendFontColor() const;
183   void           setMarkerSize( const int, bool = true  );
184   int            getMarkerSize() const;
185   void           setBackgroundColor( const QColor& );
186   QColor         backgroundColor() const;
187   void           setXGrid( bool, const int, bool, const int, bool = true );
188   void           setYGrid( bool, const int, bool, const int,
189                            bool, const int, bool, const int, bool = true );
190   void           setTitle( bool, const QString&, ObjectType, bool = true );
191   QString        getTitle( ObjectType ) const;
192
193   void           setFont( const QFont&, ObjectType, bool = true );
194   void           setHorScaleMode( const int, bool = true );
195   int            getHorScaleMode() const;
196   void           setVerScaleMode( const int, bool = true );
197   int            getVerScaleMode() const;
198   void           setNormLMaxMode( bool, bool = true);
199   bool           getNormLMaxMode()const;
200   void           setNormLMinMode( bool, bool = true);
201   bool           getNormLMinMode()const;
202   void           setNormRMaxMode( bool, bool = true);
203   bool           getNormRMaxMode()const;
204   void           setNormRMinMode( bool, bool = true);
205   bool           getNormRMinMode()const;
206
207
208   bool           isModeHorLinear();
209   bool           isModeVerLinear();
210   bool           isNormLMaxMode();
211   bool           isNormLMinMode();
212   bool           isNormRMaxMode();
213   bool           isNormRMinMode();
214
215   bool           isLegendShow() const;
216
217   // Protection against QwtCurve::drawLines() bug in Qwt 0.4.x: 
218   // it crashes if switched to X/Y logarithmic mode, when one or more points have
219   // non-positive X/Y coordinate
220   bool           isXLogEnabled() const;
221   bool           isYLogEnabled() const;
222   void           setEnableAxis( QwtPlot::Axis, bool );
223
224   virtual bool   print( const QString&, const QString& ) const;
225   void           printPlot( QPainter*, const QRect&,
226                             const QwtPlotPrintFilter& = QwtPlotPrintFilter() ) const;
227
228   QString        getVisualParameters();
229   void           setVisualParameters( const QString& );
230
231   void           incrementalPan ( const int, const int );
232   void           incrementalZoom( const int, const int );
233
234   QwtPlotCanvas* getPlotCanvas() const;
235   Plot2d_Curve*  getClosestCurve( QPoint, double&, int& ) const;
236   
237   Plot2d_Object* getPlotObject( QwtPlotItem* ) const;
238   QwtPlotItem*   getPlotObject( Plot2d_Object* ) const;
239   QwtPlotCurve*  getPlotCurve( Plot2d_Curve* ) const;
240   Plot2d_Plot2d* getPlot() const { return myPlot; }
241
242   void           updatePlotItem(Plot2d_Object*, QwtPlotItem*);
243 protected:
244   int            testOperation( const QMouseEvent& );
245   void           readPreferences();
246   void           writePreferences();
247   QString        getInfo( const QPoint& );
248   virtual void   wheelEvent( QWheelEvent* );
249   bool           hasPlotCurve( Plot2d_Curve* ) const;
250   void           setCurveType( QwtPlotCurve*, int );
251   bool           hasPlotObject( Plot2d_Object* ) const;
252   QString        getXmlVisualParameters();
253   bool           setXmlVisualParameters(const QString&);
254
255
256   
257   
258
259 public slots:
260   void           onViewPan(); 
261   void           onViewZoom();
262   void           onViewFitAll();
263   void           onViewFitArea();
264   void           onViewGlobalPan(); 
265   void           onSettings();
266   void           onAnalyticalCurve();
267   void           onFitData();
268   void           onChangeBackground();
269   void           onPanLeft();
270   void           onPanRight();
271   void           onPanUp();
272   void           onPanDown();
273   void           onZoomIn();
274   void           onZoomOut();
275
276 protected:
277   virtual void   customEvent( QEvent* );
278   void           plotMousePressed( const QMouseEvent& );
279   bool           plotMouseMoved( const QMouseEvent& );
280   void           plotMouseReleased( const QMouseEvent& );
281
282 signals:
283   void           vpModeHorChanged();
284   void           vpModeVerChanged();
285   void           vpNormLModeChanged();
286   void           vpNormRModeChanged();
287   void           vpCurveChanged();
288   void           contextMenuRequested( QContextMenuEvent* );
289   void           legendClicked( QwtPlotItem* );
290
291 protected:
292   Plot2d_Plot2d*      myPlot;
293   int                 myOperation;
294   QPoint              myPnt;
295
296   int                 myCurveType;
297   bool                myShowLegend;
298   int                 myLegendPos;
299   QFont               myLegendFont;
300   QColor              myLegendColor;
301   int                 myMarkerSize;
302   QColor              myBackground;
303   QString             myTitle, myXTitle, myYTitle, myY2Title;
304   bool                myTitleEnabled, myXTitleEnabled, myYTitleEnabled, myY2TitleEnabled;
305   bool                myXGridMajorEnabled, myYGridMajorEnabled, myY2GridMajorEnabled;
306   bool                myXGridMinorEnabled, myYGridMinorEnabled, myY2GridMinorEnabled;
307   int                 myXGridMaxMajor, myYGridMaxMajor, myY2GridMaxMajor;
308   int                 myXGridMaxMinor, myYGridMaxMinor, myY2GridMaxMinor;
309   int                 myXMode, myYMode;
310   bool                myNormLMin, myNormLMax, myNormRMin, myNormRMax;
311   double              myXDistance, myYDistance, myYDistance2;
312   bool                mySecondY;
313   ObjectDict          myObjects;
314   AnalyticalCurveList myAnalyticalCurves;
315   Plot2d_NormalizeAlgorithm* myLNormAlgo;
316   Plot2d_NormalizeAlgorithm* myRNormAlgo;
317   bool                myIsDefTitle;
318  private:
319   // List of QwtPlotCurve curves to draw (created by Plot2d_Curve::createPlotItem() )
320   QList<QwtPlotItem*> myQwtPlotCurveList;
321   
322   // List of intermittent segments to connect curves
323   QList<QwtPlotCurve*> myIntermittentSegmentList;
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