Avogadro  1.1.0
/home/kitware/dashboards/avogadro/libavogadro/src/plotwidget.h
00001 /**********************************************************************
00002   PlotWidget -- 2D plotting interface
00003 
00004   Copyright (C) 2003 Jason Harris <[email protected]> (KDE)
00005   Copyright (C) 2008 David Lonie <[email protected]> (Avogadro)
00006 
00007   This file is part of the Avogadro molecular editor project.
00008   For more information, see <http://avogadro.openmolecules.net/>
00009 
00010   This file is based on KPlotWidget from the KDE library. For more
00011   information see <http://www.kde.org/>
00012 
00013   Avogadro is free software; you can redistribute it and/or modify
00014   it under the terms of the GNU General Public License as published by
00015   the Free Software Foundation; either version 2 of the License, or
00016   (at your option) any later version.
00017 
00018   Avogadro is distributed in the hope that it will be useful,
00019   but WITHOUT ANY WARRANTY; without even the implied warranty of
00020   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021   GNU General Public License for more details.
00022 
00023   You should have received a copy of the GNU General Public License
00024   along with this program; if not, write to the Free Software
00025   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00026   02110-1301, USA.
00027  **********************************************************************/
00028 
00029 #ifndef PLOTWIDGET_H
00030 #define PLOTWIDGET_H
00031 
00032 #include <avogadro/plotaxis.h>
00033 #include <avogadro/plotpoint.h>
00034 #include <avogadro/plotobject.h>
00035 
00036 #include <QtGui/QFrame>
00037 #include <QtCore/QList>
00038 
00039 namespace Avogadro {
00040 
00094     class A_EXPORT PlotWidget : public QFrame {
00095         Q_OBJECT
00096         Q_PROPERTY(int leftPadding READ leftPadding)
00097             Q_PROPERTY(int rightPadding READ rightPadding)
00098             Q_PROPERTY(int topPadding READ topPadding)
00099             Q_PROPERTY(int bottomPadding READ bottomPadding)
00100             Q_PROPERTY(QColor backgroundColor READ backgroundColor WRITE setBackgroundColor)
00101             Q_PROPERTY(QColor foregroundColor READ foregroundColor WRITE setForegroundColor)
00102             Q_PROPERTY(QColor gridColor READ gridColor WRITE setGridColor)
00103             Q_PROPERTY(bool grid READ isGridShown WRITE setShowGrid)
00104             Q_PROPERTY(bool objectToolTip READ isObjectToolTipShown WRITE setObjectToolTipShown)
00105             public:
00110             explicit PlotWidget( QWidget * parent = 0 );
00111 
00115         virtual ~PlotWidget();
00116 
00120         enum Axis
00121             {
00122                 LeftAxis = 0,  
00123                 BottomAxis,    
00124                 RightAxis,     
00125                 TopAxis        
00126             };
00127             
00131         enum Direction
00132             {
00133                 None = 0,
00134                 Left,  
00135                 Down,    
00136                 Right,     
00137                 Up        
00138             };
00139             
00143         virtual QSize minimumSizeHint() const;
00144 
00148         virtual QSize sizeHint() const;
00149 
00158         void setLimits( double x1, double x2, double y1, double y2 );
00164         void setLimits( QRectF &rect);
00165 
00172         void scaleLimits(PlotObject *po = NULL);
00173 
00185         void setDefaultLimits( double x1, double x2, double y1, double y2 );
00186 
00187         void setDefaultLimits( QRectF &rect);
00188 
00193         void unsetDefaultLimits();
00194 
00216         void setSecondaryLimits( double x1, double x2, double y1, double y2 );
00217 
00223         void clearSecondaryLimits();
00224 
00230         QRectF dataRect() const;
00231 
00237         QRectF defaultDataRect() const;
00238 
00245         QRectF secondaryDataRect() const;
00246 
00251         QRect pixRect() const;
00252 
00259         void addPlotObject( PlotObject *object );
00260 
00265         void addPlotObjects( const QList< PlotObject* >& objects );
00266 
00270         QList< PlotObject* > plotObjects() const;
00271 
00275         void removeAllPlotObjects();
00276 
00281         void resetPlotMask();
00282 
00286         void resetPlot();
00287 
00293         void replacePlotObject( int i, PlotObject *o );
00294 
00300         QColor backgroundColor() const;
00301 
00308         QColor foregroundColor() const;
00309 
00315         QColor gridColor() const;
00316 
00321         void setBackgroundColor( const QColor &bg );
00322 
00327         void setForegroundColor( const QColor &fg );
00328 
00333         void setGridColor( const QColor &gc );
00334 
00339         void setFontSize( int pointSize );
00340 
00345         void setFont( QFont font );
00346 
00350         QFont getFont();
00351 
00356         bool isGridShown() const;
00357 
00362         bool isObjectToolTipShown() const;
00363 
00368         bool antialiasing() const;
00369 
00374         void setAntialiasing( bool b );
00375 
00383         int leftPadding() const;
00384 
00391         int rightPadding() const;
00392 
00399         int topPadding() const;
00400 
00407         int bottomPadding() const;
00408 
00413         void setLeftPadding( int padding );
00414 
00419         void setRightPadding( int padding );
00420 
00425         void setTopPadding( int padding );
00426 
00431         void setBottomPadding( int padding );
00432 
00437         void setDefaultPaddings();
00438 
00446         QPointF mapToWidget( const QPointF& p ) const;
00447 
00455         QPointF mapToData( const QPointF& p ) const;
00456 
00464         QPointF mapFrameToData( const QPointF& p ) const;
00465 
00477         void maskRect( const QRectF &r, float value=1.0 );
00478 
00490         void maskAlongLine( const QPointF &p1, const QPointF &p2, float value=1.0 );
00491 
00504         void placeLabel( QPainter *painter, PlotPoint *pp );
00505 
00510         PlotAxis* axis( Axis type );
00511 
00516         const PlotAxis* axis( Axis type ) const;
00517 
00518        signals:
00524         void pointClicked(double x, double y);
00525 
00530         void pointClicked(const QList<PlotPoint*> & pl);
00531 
00536         void pointClicked(PlotPoint*);
00537 
00543         void mouseOverPoint(double x, double y);
00544 
00545        public Q_SLOTS:
00551         void setShowGrid( bool show );
00552 
00558         void setObjectToolTipShown( bool show );
00559 
00568         bool saveImage(const QString &filename, double width, double height, double dpi, bool optimizeFontSize );
00569 
00575         void selectPoint(PlotPoint* point);
00576 
00582         void selectPoints(const QList<PlotPoint*> & points);
00583 
00589         void clearAndSelectPoint(PlotPoint* point);
00590 
00596         void clearAndSelectPoints(const QList<PlotPoint*> & points);
00597 
00602         void clearSelection();
00603 
00608         void setPointFollowMouse(bool b);
00609         
00613         void setJailedInDefaults(bool b);
00614 
00615         void setLabelShiftDirection(Direction dir, float priority=2.0);
00616 
00617     protected:
00621         virtual bool event( QEvent* );
00622 
00626         virtual void mouseMoveEvent(QMouseEvent *event);
00627 
00631         virtual void mousePressEvent(QMouseEvent *event);
00632 
00636         virtual void mouseDoubleClickEvent(QMouseEvent *event);
00637 
00641         virtual void mouseReleaseEvent(QMouseEvent *event);
00642 
00646         virtual void wheelEvent(QWheelEvent *event);
00647 
00651         virtual void paintEvent( QPaintEvent* );
00652 
00656         virtual void resizeEvent( QResizeEvent* );
00657 
00664         virtual void drawAxes( QPainter *p );
00665 
00670         void setPixRect();
00671 
00677         QList<PlotPoint*> pointsUnderPoint( const QPoint& p ) const;
00678 
00683         PlotPoint* pointNearestPoint( const QPoint& p ) const;
00684 
00685     private:
00686         class Private;
00687         Private * const d;
00688 
00689         QPointF mouseClickOrigin, zoomPosF;
00690 
00691         Q_DISABLE_COPY( PlotWidget )
00692     };
00693 
00694 }
00695 
00696 #endif