Back to index

salome-gui  6.5.0
Plot2d_ViewFrame.h
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 #ifndef PLOT2D_VIEWFRAME_H
00024 #define PLOT2D_VIEWFRAME_H
00025 
00026 #include "Plot2d.h"
00027 #include "Plot2d_Curve.h"
00028 #include "Plot2d_AnalyticalCurve.h"
00029 #include "Plot2d_NormalizeAlgorithm.h"
00030 
00031 #include <QWidget>
00032 #include <QMultiHash>
00033 #include <QMap>
00034 #include <QList>
00035 #include <QPainter>
00036 #include <qwt_symbol.h>
00037 #include <qwt_scale_draw.h>
00038 #include <qwt_plot_marker.h>
00039 #include <qwt_plot_picker.h>
00040 
00041 #include <iostream>
00042 #include <ostream>
00043 
00044 class Plot2d_Plot2d;
00045 class Plot2d_Prs;
00046 class Plot2d_Curve;
00047 class Plot2d_Object;
00048 class QCustomEvent;
00049 class QwtPlotItem;
00050 class QwtPlotCurve;
00051 class QwtPlotGrid;
00052 class QwtPlotZoomer;
00053 class Plot2d_AxisScaleDraw;
00054 class Plot2d_QwtPlotPicker;
00055 
00056 typedef QMultiHash<QwtPlotCurve*, Plot2d_Curve*>  CurveDict;
00057 typedef QMultiHash<QwtPlotItem*,  Plot2d_Object*> ObjectDict;
00058 
00059 class PLOT2D_EXPORT Plot2d_ViewFrame : public QWidget, public Plot2d_CurveContainer
00060 { 
00061   Q_OBJECT
00062   
00063   enum { NoOpId, FitAreaId, ZoomId, PanId, GlPanId, DumpId, 
00064         ModeXLinearId, ModeXLogarithmicId, ModeYLinearId, ModeYLogarithmicId,
00065         LegendId, CurvePointsId, CurveLinesId, CurveSplinesId };
00066 public:
00067   /* Construction/destruction */
00068   Plot2d_ViewFrame( QWidget*, const QString& = "" );
00069   virtual ~Plot2d_ViewFrame();
00070 
00071   enum ObjectType { MainTitle, XTitle, YTitle, Y2Title, XAxis, YAxis, Y2Axis };
00072 
00073   QWidget*       getViewWidget();
00074 
00075   /* display */
00076   virtual void   DisplayAll();
00077   virtual void   EraseAll();
00078   void           Repaint();
00079 
00080   void           Display( const Plot2d_Prs* );
00081   void           Erase( const Plot2d_Prs*, const bool = false );
00082   Plot2d_Prs*    CreatePrs( const char* = 0 );
00083 
00084   virtual bool   eventFilter( QObject*, QEvent* );
00085 
00086   /* operations */
00087   void           updateTitles();
00088   void           setTitle( const QString& );
00089   QString        getTitle() const;
00090 
00091   /* addition MultiX */
00092   void createCurveTooltips( Plot2d_Curve *curve,
00093                             Plot2d_QwtPlotPicker *picker);
00094 
00095   void displayPlot2dCurveList( QList< QList<Plot2d_Curve*> > sysCoCurveList,
00096                                Plot2d_QwtPlotPicker*         picker,
00097                                bool                          displayLegend);
00098   
00099   void displayPlot2dCurveList( QList<Plot2d_Curve*>  curveList,
00100                                                 int  groupsize,
00101                                Plot2d_QwtPlotPicker* picker,
00102                                                bool  displayLegend);
00103   
00104   Plot2d_Curve* createPlot2dCurve( QString & title,
00105                                    QString & unit,
00106                                    QList<double> & xList,
00107                                    QList<double> & yList,
00108                                    QList<QString> & tooltipList,
00109                                    Plot2d::LineType lineKind,
00110                                    int lineWidth,
00111                                    QColor & lineColor,
00112                                    QwtSymbol::Style markerKind,
00113                                    Plot2d_QwtPlotPicker* picker,
00114                                    bool toDraw,
00115                                    bool displayLegend=true);
00116 
00117   QColor getPlot2dCurveColor( Plot2d_Curve* plot2dCurve);
00118 
00119   void createSegment( double *X, double *Y, int nbPoint,
00120                       Qt::PenStyle lineKind,
00121                       int lineWidth,
00122                       QColor & lineColor,
00123                       QwtSymbol::Style markerKind);
00124 
00125   /* curves operations [ obsolete ] */
00126   void           displayCurve( Plot2d_Curve*, bool = false );
00127   void           displayCurves( const curveList&, bool = false );
00128   void           eraseCurve( Plot2d_Curve*, bool = false );
00129   void           eraseCurves( const curveList&, bool = false );
00130   int            getCurves( curveList& ) const;
00131   CurveDict      getCurves() const;
00132   void           updateCurve( Plot2d_Curve*, bool = false );
00133   void           processFiltering(bool = false);
00134 
00135   /* objects operations */
00136   void           displayObject( Plot2d_Object*, bool = false );
00137   void           displayObjects( const objectList&, bool = false );
00138   void           eraseObject( Plot2d_Object*, bool = false );
00139   void           eraseObjects( const objectList&, bool = false );
00140   int            getObjects( objectList& ) const;
00141   bool           isVisible( Plot2d_Object* ) const;
00142   void           updateObject( Plot2d_Object*, bool = false );
00143 
00144   void           updateLegend( const Plot2d_Prs* );
00145   void           updateLegend();
00146   void           fitAll();
00147   void           fitArea( const QRect& );
00148   void           fitData( const int, const double, const double,
00149                        const double, const double,
00150                        const double = 0, const double = 0 );
00151   
00152   void           getFitRanges( double&, double&, double&, double&,
00153                             double&, double&);
00154   
00155   void           getFitRangeByCurves( double&, double&, double&, double&,
00156                                   double&, double& );
00157 
00158   void              addAnalyticalCurve( Plot2d_AnalyticalCurve* );
00159   void              removeAnalyticalCurve( Plot2d_AnalyticalCurve* );
00160   void              updateAnalyticalCurve( Plot2d_AnalyticalCurve*, bool = false );
00161   void              updateAnalyticalCurves();
00162   void              deselectAnalyticalCurves();  
00163   void              deselectObjects();    
00164 
00165   AnalyticalCurveList getAnalyticalCurves() const;
00166   Plot2d_AnalyticalCurve* getAnalyticalCurve(QwtPlotItem *);
00167 
00168   /* view parameters */
00169   void           copyPreferences( Plot2d_ViewFrame* );
00170   void           setCurveType( int, bool = true );
00171   int            getCurveType() const;
00172   void           setCurveTitle( Plot2d_Curve*, const QString& );
00173   void           setObjectTitle( Plot2d_Object*, const QString& );
00174   void           showLegend( bool, bool = true );
00175   void           setLegendPos( int );
00176   int            getLegendPos() const;
00177   void           setLegendFont( const QFont& );
00178   QFont          getLegendFont() const;
00179   void           setLegendFontColor( const QColor& );
00180   QColor         getLegendFontColor() const;
00181   void           setMarkerSize( const int, bool = true  );
00182   int            getMarkerSize() const;
00183   void           setBackgroundColor( const QColor& );
00184   QColor         backgroundColor() const;
00185   void           setXGrid( bool, const int, bool, const int, bool = true );
00186   void           setYGrid( bool, const int, bool, const int,
00187                         bool, const int, bool, const int, bool = true );
00188   void           setTitle( bool, const QString&, ObjectType, bool = true );
00189   QString        getTitle( ObjectType ) const;
00190 
00191   void           setFont( const QFont&, ObjectType, bool = true );
00192   void           setHorScaleMode( const int, bool = true );
00193   int            getHorScaleMode() const;
00194   void           setVerScaleMode( const int, bool = true );
00195   int            getVerScaleMode() const;
00196   void           setNormLMaxMode( bool, bool = true);
00197   bool           getNormLMaxMode()const;
00198   void           setNormLMinMode( bool, bool = true);
00199   bool           getNormLMinMode()const;
00200   void           setNormRMaxMode( bool, bool = true);
00201   bool           getNormRMaxMode()const;
00202   void           setNormRMinMode( bool, bool = true);
00203   bool           getNormRMinMode()const;
00204 
00205 
00206   bool           isModeHorLinear();
00207   bool           isModeVerLinear();
00208   bool           isNormLMaxMode();
00209   bool           isNormLMinMode();
00210   bool           isNormRMaxMode();
00211   bool           isNormRMinMode();
00212 
00213   bool           isLegendShow() const;
00214 
00215   // Protection against QwtCurve::drawLines() bug in Qwt 0.4.x: 
00216   // it crashes if switched to X/Y logarithmic mode, when one or more points have
00217   // non-positive X/Y coordinate
00218   bool           isXLogEnabled() const;
00219   bool           isYLogEnabled() const;
00220   void           setEnableAxis( QwtPlot::Axis, bool );
00221 
00222   virtual bool   print( const QString&, const QString& ) const;
00223   void           printPlot( QPainter*, const QRect&,
00224                          const QwtPlotPrintFilter& = QwtPlotPrintFilter() ) const;
00225 
00226   QString        getVisualParameters();
00227   void           setVisualParameters( const QString& );
00228 
00229   void           incrementalPan ( const int, const int );
00230   void           incrementalZoom( const int, const int );
00231 
00232   QwtPlotCanvas* getPlotCanvas() const;
00233   Plot2d_Curve*  getClosestCurve( QPoint, double&, int& ) const;
00234   
00235   Plot2d_Object* getPlotObject( QwtPlotItem* ) const;
00236   QwtPlotItem*   getPlotObject( Plot2d_Object* ) const;
00237   QwtPlotCurve*  getPlotCurve( Plot2d_Curve* ) const;
00238   Plot2d_Plot2d* getPlot() const { return myPlot; }
00239 
00240   void           updatePlotItem(Plot2d_Object*, QwtPlotItem*);
00241 protected:
00242   int            testOperation( const QMouseEvent& );
00243   void           readPreferences();
00244   void           writePreferences();
00245   QString        getInfo( const QPoint& );
00246   virtual void   wheelEvent( QWheelEvent* );
00247   bool           hasPlotCurve( Plot2d_Curve* ) const;
00248   void           setCurveType( QwtPlotCurve*, int );
00249   bool           hasPlotObject( Plot2d_Object* ) const;
00250   QString        getXmlVisualParameters();
00251   bool           setXmlVisualParameters(const QString&);
00252 
00253 
00254   
00255   
00256 
00257 public slots:
00258   void           onViewPan(); 
00259   void           onViewZoom();
00260   void           onViewFitAll();
00261   void           onViewFitArea();
00262   void           onViewGlobalPan(); 
00263   void           onSettings();
00264   void           onAnalyticalCurve();
00265   void           onFitData();
00266   void           onChangeBackground();
00267   void           onPanLeft();
00268   void           onPanRight();
00269   void           onPanUp();
00270   void           onPanDown();
00271   void           onZoomIn();
00272   void           onZoomOut();
00273 
00274 protected:
00275   virtual void   customEvent( QEvent* );
00276   void           plotMousePressed( const QMouseEvent& );
00277   bool           plotMouseMoved( const QMouseEvent& );
00278   void           plotMouseReleased( const QMouseEvent& );
00279 
00280 signals:
00281   void           vpModeHorChanged();
00282   void           vpModeVerChanged();
00283   void           vpNormLModeChanged();
00284   void           vpNormRModeChanged();
00285   void           vpCurveChanged();
00286   void           contextMenuRequested( QContextMenuEvent* );
00287   void           legendClicked( QwtPlotItem* );
00288 
00289 protected:
00290   Plot2d_Plot2d*      myPlot;
00291   int                 myOperation;
00292   QPoint              myPnt;
00293 
00294   int                 myCurveType;
00295   bool                myShowLegend;
00296   int                 myLegendPos;
00297   QFont               myLegendFont;
00298   QColor              myLegendColor;
00299   int                 myMarkerSize;
00300   QColor              myBackground;
00301   QString             myTitle, myXTitle, myYTitle, myY2Title;
00302   bool                myTitleEnabled, myXTitleEnabled, myYTitleEnabled, myY2TitleEnabled;
00303   bool                myXGridMajorEnabled, myYGridMajorEnabled, myY2GridMajorEnabled;
00304   bool                myXGridMinorEnabled, myYGridMinorEnabled, myY2GridMinorEnabled;
00305   int                 myXGridMaxMajor, myYGridMaxMajor, myY2GridMaxMajor;
00306   int                 myXGridMaxMinor, myYGridMaxMinor, myY2GridMaxMinor;
00307   int                 myXMode, myYMode;
00308   bool                myNormLMin, myNormLMax, myNormRMin, myNormRMax;
00309   double              myXDistance, myYDistance, myYDistance2;
00310   bool                mySecondY;
00311   ObjectDict          myObjects;
00312   AnalyticalCurveList myAnalyticalCurves;
00313   Plot2d_NormalizeAlgorithm* myLNormAlgo;
00314   Plot2d_NormalizeAlgorithm* myRNormAlgo;
00315   bool                myIsDefTitle;
00316  private:
00317   // List of intermittent segments to connect curves
00318   // (cf displayPlot2dCurveList() and createSegment(() )
00319   QList<QwtPlotCurve*> myIntermittentSegmentList;
00320 };
00321 
00322 class Plot2d_Plot2d : public QwtPlot 
00323 {
00324   Q_OBJECT
00325 public:
00326   Plot2d_Plot2d( QWidget* );
00327   virtual ~Plot2d_Plot2d();
00328 
00329   void           setLogScale( int, bool );
00330 
00331   void           replot();
00332   QwtLegend*     getLegend();
00333   QSize          sizeHint() const;
00334   QSize          minimumSizeHint() const;
00335   void           defaultPicker();
00336   void           setPickerMousePattern( int, int = Qt::NoButton );
00337 
00338   void createMarkerAndTooltip( QwtSymbol symbol,
00339                                double    X,
00340                                double    Y,
00341                                QString & tooltip,
00342                                Plot2d_QwtPlotPicker *picker);
00343 
00344   bool           polished() const;
00345   QwtPlotGrid*   grid() const;
00346   QwtPlotZoomer* zoomer() const;
00347 
00348   virtual void   updateYAxisIdentifiers();
00349   
00350   // Methods to deal with axes ticks
00351 
00352   void createAxisScaleDraw();
00353   void applyTicks();
00354   void unactivAxisScaleDraw( int numcall);
00355 
00356   void displayXTicksAndLabels(
00357          double XLeftmargin, double XRightMargin,
00358          const QList< QPair< QString, QMap<double,QString> > > & devicesPosLabelTicks);
00359 
00360   void createSeparationLine( double Xpos);
00361                                          
00362 
00363 public slots:
00364   virtual void   polish();
00365 
00366 protected slots:
00367   void           onScaleDivChanged();
00368 
00369 protected:
00370   QwtPlotGrid*   myGrid;
00371   QList<QColor>  myColors;
00372   bool           myIsPolished;
00373   QwtPlotZoomer* myPlotZoomer;
00374   Plot2d_AxisScaleDraw* myScaleDraw;
00375 };
00376 
00377 class Plot2d_ScaleDraw: public QwtScaleDraw
00378 {
00379 public:
00380   Plot2d_ScaleDraw( char f = 'g', int prec = 6 );
00381   Plot2d_ScaleDraw( const QwtScaleDraw& scaleDraw, char f = 'g', int prec = 6 );
00382 
00383   virtual QwtText label( double value ) const;
00384 
00385   int precision() const { return myPrecision; }
00386 
00387 private:
00388   char myFormat;
00389   int  myPrecision;
00390 };
00391 
00392 /* Definition of X axis graduations
00393  */
00394 class Plot2d_AxisScaleDraw: public QwtScaleDraw
00395 {
00396 public:
00397   static const QString DEVICE_FONT;
00398   static const int     DEVICE_FONT_SIZE;
00399   static const int     DEVICE_BY;
00400 
00401   Plot2d_AxisScaleDraw(Plot2d_Plot2d* plot);
00402 
00403   virtual ~Plot2d_AxisScaleDraw();
00404 
00405   void unactivTicksDrawing( int numcall);
00406 
00407   virtual void draw( QPainter * painter, const QPalette & palette) const;
00408 
00409   virtual QwtText label(double value) const;
00410 
00411   void setLabelTick(double value, QString label, bool isDevice = false);
00412     
00413   void setTicks(const QList<double> aTicks);
00414 
00415   void setInterval(double lowerBound, double upperBound);
00416 
00417   void applyTicks();
00418 
00419 protected:
00420 
00421   void drawLabel( QPainter* painter, double value) const;
00422     
00423   void drawTick( QPainter* painter, double value, int len) const;
00424 
00425 private:
00426   bool myActivTicksDrawing;   // true => activate drawing ticks (with draw() )
00427   int  myNumTicksDrawingCall; // call number to ticks drawing
00428 
00429   // Ticks list to display on X axis
00430   QMap<double, QString> myLabelX;  // position, label
00431   //
00432   QList<double> myTicks;  // positions
00433     
00434   // Systems names to display under X axis
00435   QMap<double, QString> myLabelDevice;
00436     
00437   Plot2d_Plot2d* myPlot;  // Drawing zone QwtPlot
00438 
00439   double myLowerBound;
00440   double myUpperBound;
00441 };
00442 
00443 
00444 
00445 
00446 /* Management of tooltips associated with markers for curves points or others points
00447  */
00448 class Plot2d_QwtPlotPicker : public QwtPlotPicker
00449 {
00450 public:
00451   static const double BOUND_HV_SIZE;
00452   
00453   Plot2d_QwtPlotPicker( int            xAxis,
00454                         int            yAxis,
00455                         int            selectionFlags,
00456                         RubberBand     rubberBand,
00457                         DisplayMode    trackerMode,
00458                         QwtPlotCanvas *canvas);
00459     
00460   Plot2d_QwtPlotPicker( int  xAxis,
00461                         int  yAxis,
00462                         QwtPlotCanvas *canvas);
00463     
00464   virtual ~Plot2d_QwtPlotPicker();
00465     
00466   QList<QwtPlotMarker*>             pMarkers;         // points markers
00467   QMap<QwtPlotMarker*, QwtText>  pMarkersToolTip;  // associations (marker,tooltip)
00468 
00469 protected:
00470 
00471   virtual QwtText trackerText( const QwtDoublePoint & pos ) const;
00472 
00473 };
00474 
00475 #endif