Image Component Library (ICL)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
AbstractPlotWidget.h
Go to the documentation of this file.
00001 /********************************************************************
00002 **                Image Component Library (ICL)                    **
00003 **                                                                 **
00004 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld          **
00005 **                         Neuroinformatics Group                  **
00006 ** Website: www.iclcv.org and                                      **
00007 **          http://opensource.cit-ec.de/projects/icl               **
00008 **                                                                 **
00009 ** File   : ICLQt/src/ICLQt/AbstractPlotWidget.h                   **
00010 ** Module : ICLQt                                                  **
00011 ** Authors: Christof Elbrechter                                    **
00012 **                                                                 **
00013 **                                                                 **
00014 ** GNU LESSER GENERAL PUBLIC LICENSE                               **
00015 ** This file may be used under the terms of the GNU Lesser General **
00016 ** Public License version 3.0 as published by the                  **
00017 **                                                                 **
00018 ** Free Software Foundation and appearing in the file LICENSE.GPL  **
00019 ** included in the packaging of this file.  Please review the      **
00020 ** following information to ensure the license requirements will   **
00021 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt                **
00022 **                                                                 **
00023 ** The development of this software was supported by the           **
00024 ** Excellence Cluster EXC 277 Cognitive Interaction Technology.    **
00025 ** The Excellence Cluster EXC 277 is a grant of the Deutsche       **
00026 ** Forschungsgemeinschaft (DFG) in the context of the German       **
00027 ** Excellence Initiative.                                          **
00028 **                                                                 **
00029 ********************************************************************/
00030 
00031 #pragma once
00032 
00033 #include <ICLMath/DynMatrix.h>
00034 #include <ICLUtils/Range.h>
00035 #include <ICLUtils/Rect32f.h>
00036 #include <ICLUtils/Configurable.h>
00037 #include <ICLQt/MouseEvent.h>
00038 #include <ICLQt/MouseHandler.h>
00039 
00040 #include <ICLQt/ThreadedUpdatableWidget.h>
00041 
00042 #include <QtGui/QPen>
00043 #include <QtGui/QBrush>
00044 #include <QtGui/QPainter>
00045 
00046 namespace icl{
00047   namespace qt{
00048     
00049     
00051 
00085     class AbstractPlotWidget : public ThreadedUpdatableWidget, public utils::Configurable{
00086       struct Data; 
00087       Data *data;  
00088   
00090       void property_changed(const Property &);
00091   
00092       public:
00093       
00094       
00096 
00102       void install(MouseHandler *h);
00103 
00105 
00106       void uninstall(MouseHandler *h);
00107       
00109       enum PenType{
00110         X_AXIS_PEN,    
00111         Y_AXIS_PEN,    
00112         X_TIC_PEN,     
00113         Y_TIC_PEN,     
00114         X_LABEL_PEN,   
00115         Y_LABEL_PEN,   
00116         X_GRID_PEN,    
00117         Y_GRID_PEN,    
00118         AXIS_NAME_PEN, 
00119         NUM_PEN_TYPES
00120       };
00121   
00122       
00124       AbstractPlotWidget(QWidget *parent=0);
00125   
00127       ~AbstractPlotWidget();
00128       
00130       virtual void paintEvent(QPaintEvent *evt);
00131   
00133       virtual void keyPressEvent(QKeyEvent *event);
00134       
00136       void renderTo(QPainter &p);
00137   
00139       void setBackground(const QBrush &bgBrush);
00140   
00142       void setPen(PenType p, const QPen &pen);  
00143       
00145 
00146       struct Pen{
00148 
00151 
00170         Pen(const QPen &linePen=Qt::NoPen, 
00171             const QPen &symbolPen=Qt::NoPen,
00172             char symbol= ' ',
00173             int symbolSize=5,
00174             const QBrush &fillBrush=Qt::NoBrush):
00175         linePen(linePen),symbolPen(symbolPen),symbol(symbol),symbolSize(symbolSize),fillBrush(fillBrush){}
00176         QPen linePen;     
00177         QPen symbolPen;   
00178         char symbol;      
00179         int symbolSize;   
00180         QBrush fillBrush; 
00181       };
00183       typedef utils::SmartPtr<Pen> PenPtr;
00184   
00185   
00187 
00188       inline void render() { updateFromOtherThread(); }
00189   
00191 
00192       virtual utils::Rect32f getDataViewPort() const;
00193       
00195 
00203       void setDataViewPort(const utils::Rect32f &viewPort);
00204   
00206 
00209       void setDataViewPort(const utils::Range32f &xrange, const utils::Range32f &yrange);
00210   
00212       void lock() const;
00213   
00215       void unlock() const;
00216   
00218 
00249       void addAnnotations(const char type,const float *data, int num=1, 
00250                           const QPen &linePen = QColor(255,0,0),
00251                           const QBrush &brush = Qt::NoBrush,
00252                           const std::string &text="", const std::string &textDelim=",");
00253   
00255       void clearAnnotations();
00256       
00258 
00259       virtual void clear() { clearAnnotations(); }
00260       
00262       typedef utils::Function<float,float,float> bgFunction;
00263 
00265 
00275       void setBackgroundFunction(bgFunction f);
00276       
00278 
00281       void updateBackgroundFunction();
00282       
00284       void removeBackgroundFunction();
00285   
00286       protected:
00287       
00289       bool isZoomed() const;
00290       
00292       virtual void mouseDoubleClickEvent(QMouseEvent *event);
00293   
00295       virtual void mouseMoveEvent(QMouseEvent *event);
00296   
00298       virtual void mousePressEvent(QMouseEvent *event);
00299   
00301       virtual void mouseReleaseEvent(QMouseEvent *event);
00302   
00304       virtual void enterEvent(QEvent *event);
00305   
00307       virtual void leaveEvent(QEvent *event);
00308   
00310 
00311       virtual bool drawData(QPainter &p) = 0;
00312       
00314       virtual void drawLegend(QPainter &p,const utils::Rect &where, bool horizontal);
00315   
00316   
00318       void drawDefaultLedgend(QPainter &p,const utils::Rect &where, bool horizontal, 
00319                               const std::vector<std::string> &rowNames,
00320                               const std::vector<PenPtr> &pens);
00321   
00323 
00326       template<char s> static inline void draw_symbol(QPainter &p,int size, float x, float y){ 
00327         ERROR_LOG("undefined given symbol ID"); 
00328       }
00329       
00331       float winToDrawX(int winX) const;
00332   
00334       float winToDrawY(int winY) const;
00335   
00337       utils::Point32f winToDraw(const utils::Point &p) const;
00338       
00340       int drawToWinX(float drawX) const;
00341   
00343       int drawToWinY(float drawY) const;
00344   
00346       utils::Point drawToWin(const utils::Point32f &p) const;
00347   
00349       utils::Rect32f getDynamicDataViewPort() const;
00350       
00352       struct Locker{
00353         const AbstractPlotWidget *w; 
00354 
00355         inline Locker(const AbstractPlotWidget *w):w(w){ this->w->lock(); }
00357         inline Locker(const AbstractPlotWidget &w):w(&w){ this->w->lock();}
00359         ~Locker() { w->unlock(); }
00360       };
00361     }; 
00362     
00364     // (specialized) note, points have not size: use circle for larger round points ...
00365     template<> inline void AbstractPlotWidget::draw_symbol<'.'>(QPainter &p,int, float x, float y){ 
00366       //    p.drawPoint(QPoint(x,y));
00367       p.drawLine(QPoint(x,y),QPoint(x,y));
00368     }
00369     
00370     template<> inline void AbstractPlotWidget::draw_symbol<'x'>(QPainter &p,int size, float x, float y){ 
00371       p.drawLine(QPoint(x-size,y-size),QPoint(x+size,y+size));
00372       p.drawLine(QPoint(x-size,y+size),QPoint(x+size,y-size));
00373     }
00374   
00375     template<> inline void AbstractPlotWidget::draw_symbol<'+'>(QPainter &p,int size, float x, float y){ 
00376       p.drawLine(QPoint(x,y-size),QPoint(x,y+size));
00377       p.drawLine(QPoint(x-size,y),QPoint(x+size,y));
00378     }
00379   
00380     template<> inline void AbstractPlotWidget::draw_symbol<'*'>(QPainter &p,int size, float x, float y){ 
00381       draw_symbol<'+'>(p,(size*2)/3,x,y);    
00382       draw_symbol<'x'>(p,size,x,y);
00383     }
00384   
00386     template<> inline void AbstractPlotWidget::draw_symbol<'o'>(QPainter &p,int size, float x, float y){ 
00387       p.drawEllipse(QRect(x-size,y-size,2*size,2*size));
00388     }
00389   
00390     template<> inline void AbstractPlotWidget::draw_symbol<'s'>(QPainter &p,int size, float x, float y){ 
00391       p.drawRect(QRect(x-size,y-size,2*size,2*size));
00392     }
00393   
00394     template<> inline void AbstractPlotWidget::draw_symbol<'t'>(QPainter &p,int size, float x, float y){ 
00395       const QPoint e[3] = { QPoint(x,y-size),QPoint(x+size,y+size),QPoint(x-size,y+size) };
00396       p.drawConvexPolygon(e,3);
00397     }
00398   
00399     template<> inline void AbstractPlotWidget::draw_symbol<'d'>(QPainter &p,int size, float x, float y){ 
00400       const QPoint e[4] = { QPoint(x,y-size),QPoint(x+size,y),QPoint(x,y+size), QPoint(x-size,y) };
00401       p.drawConvexPolygon(e,4);
00402     }
00404   } // namespace qt
00405 }
00406 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines