TQtWidget.h

Go to the documentation of this file.
00001 // @(#)root/qt:$Id: TQtWidget.h 33707 2010-06-02 06:27:34Z brun $
00002 // Author: Valeri Fine   21/01/2002
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2004, Rene Brun and Fons Rademakers.               *
00006  * Copyright (C) 2003 by Valeri Fine.                                    *
00007  * All rights reserved.                                                  *
00008  *                                                                       *
00009  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00010  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00011  *************************************************************************/
00012 
00013 #ifndef ROOT_TQtWidget
00014 #define ROOT_TQtWidget
00015 
00016 // Definition of TQtWidget class
00017 // "double-buffered" widget
00018 
00019 #include <assert.h>
00020 #include "Rtypes.h"
00021 #include "TCanvas.h"
00022 
00023 #ifndef __CINT__
00024 #  include <QtGui/QWidget>
00025 #  include <QtGui/QMouseEvent>
00026 #  include <QtGui/QShowEvent>
00027 #  include <QtGui/QFocusEvent>
00028 #  include <QtGui/QKeyEvent>
00029 #  include <QtGui/QResizeEvent>
00030 #  include <QtCore/QEvent>
00031 #  include <QtGui/QPaintEvent>
00032 #  include <QtGui/QPaintDevice>
00033 #  include <QtCore/QSize>
00034 #  include <QtCore/QPoint>
00035 #  include <QtCore/QPointer>
00036 #  include <QtGui/QPixmap>
00037 #  include "TQtCanvasPainter.h"
00038 #else
00039   // List of the fake classes to make RootCint happy.
00040   class QWidget;
00041   class QPixmap;
00042   class QMouseEvent;
00043   class QFocusEvent;
00044   class QCustomEvent;
00045   class QKeyEvent;
00046   class QShowEvent;
00047   class QPaintEvent;
00048   class QPaintDevice;
00049   class QResizeEvent;
00050   class QSize;  
00051   class QString;
00052   class QEvent;
00053   class QSizePolicy;
00054   class QContextMenuEvent;
00055   class QSize;
00056   class QPoint;
00057   class TQtCanvasPainter;
00058 #endif
00059   class QTimer;
00060   class TApplication;
00061 //
00062 // TQtWidget is a custom QWidget to back ROOT TCanvas.
00063 //
00064 // It can be used within Qt-based program and with Qt Designer as a "regular"
00065 // Qt QWidget to create the Qt widget wihe builtin TCanvas'
00066 //
00067 enum EEventTrackingBits {
00068        kMousePressEvent       = BIT(0), // emit signal as soon as TCanvas processed mousePressEvent       QMouseEvent
00069        kMouseMoveEvent        = BIT(1), // emit signal as soon as TCanvas processed mouseMoveEvent        QMouseEvent
00070        kMouseReleaseEvent     = BIT(2), // emit signal as soon as TCanvas processed mouseReleaseEvent     QMouseEvent
00071        kMouseDoubleClickEvent = BIT(3), // emit signal as soon as TCanvas processed mouseDoubleClickEvent QMouseEvent
00072        kKeyPressEvent         = BIT(4), // emit signal as soon as TCanvas processed keyPressEvent         QKeyEvent
00073        kEnterEvent            = BIT(5), // emit signal as soon as TCanvas processed enterEvent            QEvent
00074        kLeaveEvent            = BIT(6)  // emit signal as soon as TCanvas processed leaveEvent            QEvent
00075 };
00076 
00077 //___________________________________________________________________
00078 class TQtWidgetBuffer
00079 {
00080 private:
00081    const QWidget *fWidget;
00082    QPaintDevice  *fBuffer;
00083    bool  fIsImage;
00084 public:
00085    TQtWidgetBuffer(const QWidget *w, bool clear=false);
00086    TQtWidgetBuffer(const TQtWidgetBuffer &b);
00087    const QPaintDevice  *Buffer() const  { return fBuffer; }
00088    QPaintDevice  *Buffer()  { return fBuffer; }
00089    ~TQtWidgetBuffer();
00090    void Clear();
00091    bool PaintingActive(){ return fBuffer ? fBuffer->paintingActive() : false; }
00092    QRect Rect () const { return fWidget->rect();                }
00093    int Height () const { return fBuffer ? fBuffer->height() : 0;}
00094    int Width  () const { return fBuffer ? fBuffer->width() : 0; }
00095 };
00096 
00097 //___________________________________________________________________
00098 class  TQtWidget : public QWidget {
00099 #ifndef __CINT__   
00100  Q_OBJECT
00101  friend class TQtSynchPainting;
00102 #endif
00103 private:
00104 
00105     TQtWidget(const TQtWidget&);
00106          void operator=(const TQtWidget&);
00107    //----- Private bits, clients can only test but not change them
00108    UInt_t         fBits;       //bit field status word
00109    enum {
00110       kBitMask       = 0x00ffffff
00111    };
00112    bool fNeedStretch;
00113 #ifndef __CINT__   
00114    QPointer<TQtCanvasPainter> fCanvasDecorator;  //< The object to paint onto the TQtWidget on the top of TCanvas image
00115 #endif
00116 protected:
00117    void Init(); 
00118    void ResetCanvas() { fCanvas = 0;}
00119 
00120 public:
00121    enum {
00122       kEXITSIZEMOVE,
00123       kENTERSIZEMOVE,
00124       kFORCESIZE
00125    };
00126 #ifndef __CINT__
00127   TQtWidget( QWidget* parent, const char* name, Qt::WFlags f=0, bool embedded=TRUE);
00128   TQtWidget( QWidget* parent=0, Qt::WFlags f=0, bool embedded=TRUE);
00129 #else
00130   TQtWidget( QWidget* parent=0);
00131 #endif  
00132   virtual ~TQtWidget();
00133   void SetCanvas(TCanvas *c);
00134 //  inline TCanvas  *GetCanvas() const         { return fCanvas;}
00135   TCanvas  *GetCanvas() const;
00136   TQtWidgetBuffer  &SetBuffer();
00137   const TQtWidgetBuffer  *GetBuffer()  const;
00138   QPixmap  *GetOffScreenBuffer()  const;
00139 
00140   // overloaded methods
00141   virtual void Erase ();
00142   bool    IsDoubleBuffered() const { return fDoubleBufferOn; }
00143   void    SetDoubleBuffer(bool on=TRUE);
00144   virtual void SetSaveFormat(const char *format);
00145 
00146 protected:
00147    friend class TGQt;
00148    friend class TQtFeedBackWidget;
00149    TCanvas           *fCanvas;
00150    TQtWidgetBuffer   *fPixmapID;     // Double buffer of this widget
00151    TQtWidgetBuffer   *fPixmapScreen; // Double buffer for no-double buffer operation
00152    bool        fPaint;
00153    bool        fSizeChanged;
00154    bool        fDoubleBufferOn;
00155    bool        fEmbedded;
00156    QSize       fSizeHint;
00157    QWidget    *fWrapper;
00158    QString     fSaveFormat;
00159    bool        fInsidePaintEvent;
00160    QPoint      fOldMousePos;
00161    int         fIgnoreLeaveEnter;
00162    QTimer     *fRefreshTimer;
00163 
00164 
00165    void SetRootID(QWidget *wrapper);
00166    QWidget *GetRootID() const;
00167    virtual void EmitCanvasPainted() { emit CanvasPainted(); }
00168    TCanvas  *Canvas();
00169    bool paintFlag(bool mode=TRUE);
00170    void AdjustBufferSize();
00171 
00172    bool PaintingActive () const;
00173    void SetIgnoreLeaveEnter(int ignore=1);
00174 
00175 
00176    virtual void enterEvent       ( QEvent *      );
00177    virtual void customEvent      ( QEvent *      );
00178    virtual void contextMenuEvent ( QContextMenuEvent *);
00179    virtual void focusInEvent     ( QFocusEvent * );
00180    virtual void focusOutEvent    ( QFocusEvent * );
00181    virtual void leaveEvent       ( QEvent *      );
00182    virtual void mouseDoubleClickEvent(QMouseEvent* );
00183    virtual void mouseMoveEvent   ( QMouseEvent * );
00184    virtual void mousePressEvent  ( QMouseEvent * );
00185    virtual void mouseReleaseEvent( QMouseEvent * );
00186    virtual void keyPressEvent    ( QKeyEvent *   );
00187    virtual void keyReleaseEvent  ( QKeyEvent *   );
00188    virtual void showEvent        ( QShowEvent *  );
00189    virtual void paintEvent       ( QPaintEvent * );
00190    virtual void resizeEvent      ( QResizeEvent *);
00191    //  Layout methods:
00192    virtual void        SetSizeHint (const QSize &size);
00193 public:
00194    virtual QSize       sizeHint () const;        // returns the preferred size of the widget.
00195    virtual QSize       minimumSizeHint () const; // returns the smallest size the widget can have.
00196    virtual QSizePolicy sizePolicy () const;      // returns a QSizePolicy; a value describing the space requirements of the
00197 protected:
00198    // -- A special event handler
00199    virtual void exitSizeEvent ();
00200    virtual void stretchWidget(QResizeEvent *e);
00201    TQtCanvasPainter *CanvasDecorator();
00202 public:
00203    //----- bit manipulation (a'la TObject )
00204    void     SetBit     (UInt_t f, Bool_t set);
00205    void     SetBit     (UInt_t f);
00206    void     ResetBit   (UInt_t f);
00207    Bool_t   TestBit    (UInt_t f) const;
00208    Int_t    TestBits   (UInt_t f) const;
00209    void     InvertBit  (UInt_t f);
00210    void     EmitSignal (UInt_t f);
00211    void     EmitTestedSignal();
00212    UInt_t   GetAllBits() const;
00213    void     SetAllBits(UInt_t f);
00214    void SetCanvasDecorator( TQtCanvasPainter *decorator);
00215    
00216 public:
00217    // Static method to immitate ROOT as needed
00218    static TApplication *InitRint(Bool_t prompt=kFALSE, const char *appClassName="QtRint", int *argc=0, char **argv=0,
00219           void *options = 0, int numOptions = 0, Bool_t noLogo = kTRUE);
00220    //  Proxy methods to access the TCanvas selected TObject 
00221    //  and last processed ROOT TCanvas event
00222    Int_t             GetEvent()       const;
00223    Int_t             GetEventX()      const;
00224    Int_t             GetEventY()      const;
00225    TObject          *GetSelected()    const;
00226    Int_t             GetSelectedX()   const;
00227    Int_t             GetSelectedY()   const;
00228    TVirtualPad      *GetSelectedPad() const;
00229 
00230    //----- bit Qt signal emitting the Qt signal to track mouse movement
00231    void     EnableSignalEvents  (UInt_t f);
00232    void     DisableSignalEvents (UInt_t f);
00233    Bool_t   IsSignalEventEnabled(UInt_t f) const;
00234    
00235    static TCanvas   *Canvas(TQtWidget *widget);
00236    static TQtWidget *Canvas(const TCanvas *canvas);
00237    static TQtWidget *Canvas(Int_t id);
00238 
00239 public slots:
00240    virtual void cd();
00241    virtual void cd(int subpadnumber);
00242    void Disconnect();
00243    void Refresh();
00244    virtual bool Save(const QString &fileName) const;
00245    virtual bool Save(const char    *fileName) const;
00246    virtual bool Save(const QString &fileName,const char *format,int quality=60) const;
00247    virtual bool Save(const char    *fileName,const char *format,int quality=60) const;
00248 #ifndef __CINT__
00249 protected slots:
00250    void RefreshCB();
00251    
00252 signals:
00253    // emit the Qt signal when the double buffer of the TCamvas has been filled up
00254    void CanvasPainted();  // Signal the TCanvas has been painted onto the screen
00255    void Saved(bool ok);   // Signal the TCanvas has been saved into the file
00256    void RootEventProcessed(TObject *selected, unsigned int event, TCanvas *c);
00257 #endif
00258 
00259 #ifndef Q_MOC_RUN
00260    ClassDef(TQtWidget,0) // QWidget to back ROOT TCanvas (Can be used with Qt designer)
00261 #endif
00262 };
00263 
00264 //______________________________________________________________________________
00265 inline TCanvas  *TQtWidget::GetCanvas() const         { return fCanvas; }
00266 
00267 //______________________________________________________________________________
00268 inline const TQtWidgetBuffer  *TQtWidget::GetBuffer()  const { 
00269    //  return the current widget buffer;
00270    return IsDoubleBuffered() ? fPixmapScreen : fPixmapID;
00271 }
00272 
00273 //______________________________________________________________________________
00274 inline bool TQtWidget::PaintingActive () const {
00275   return QWidget::paintingActive() || (fPixmapID && fPixmapID->PaintingActive())
00276      || (fPixmapScreen && fPixmapScreen->PaintingActive());
00277 }
00278 //______________________________________________________________________________
00279 inline void TQtWidget::SetRootID(QWidget *wrapper) { fWrapper = wrapper;}
00280 //______________________________________________________________________________
00281 inline QWidget *TQtWidget::GetRootID() const { return fWrapper;}
00282 
00283 //______________________________________________________________________________
00284 //
00285 //  Proxy methods to access the TCanvas selected TObject
00286 //  and last processed event
00287 //______________________________________________________________________________
00288 inline Int_t        TQtWidget::GetEvent()       const { return GetCanvas()->GetEvent();       }
00289 //______________________________________________________________________________
00290 inline Int_t        TQtWidget::GetEventX()      const { return GetCanvas()->GetEventX();      }
00291 //______________________________________________________________________________
00292 inline Int_t        TQtWidget::GetEventY()      const { return GetCanvas()->GetEventY();      }
00293 //______________________________________________________________________________
00294 inline TObject     *TQtWidget::GetSelected()    const { return GetCanvas()->GetSelected();    }
00295 //______________________________________________________________________________
00296 inline Int_t        TQtWidget::GetSelectedX()   const { return GetCanvas()->GetSelectedX();   }
00297 //______________________________________________________________________________
00298 inline Int_t        TQtWidget::GetSelectedY()   const { return GetCanvas()->GetSelectedY();   }
00299 //______________________________________________________________________________
00300 inline TVirtualPad *TQtWidget::GetSelectedPad() const { return GetCanvas()->GetSelectedPad(); }
00301 
00302 //----- bit manipulation
00303 inline UInt_t TQtWidget::GetAllBits() const       { return fBits;                       }
00304 inline void   TQtWidget::SetAllBits(UInt_t f)     { fBits = f;                          }
00305 inline void   TQtWidget::SetBit(UInt_t f)         { fBits |= f & kBitMask;              }
00306 inline void   TQtWidget::ResetBit(UInt_t f)       { fBits &= ~(f & kBitMask);           }
00307 inline Bool_t TQtWidget::TestBit(UInt_t f) const  { return (Bool_t) ((fBits & f) != 0); }
00308 inline Int_t  TQtWidget::TestBits(UInt_t f) const { return (Int_t) (fBits & f);         }
00309 inline void   TQtWidget::InvertBit(UInt_t f)      { fBits ^= f & kBitMask;              }
00310 
00311 inline TQtCanvasPainter *TQtWidget::CanvasDecorator() { return fCanvasDecorator;   }
00312 inline void   TQtWidget::SetCanvasDecorator( TQtCanvasPainter *decorator) { fCanvasDecorator = decorator;}
00313    
00314 inline void   TQtWidget::EnableSignalEvents  (UInt_t f){ SetBit  (f); }
00315 inline void   TQtWidget::DisableSignalEvents (UInt_t f){ ResetBit(f); }
00316 inline Bool_t TQtWidget::IsSignalEventEnabled(UInt_t f) const { return TestBit (f); }
00317 inline void   TQtWidget::EmitSignal(UInt_t f)  {if (IsSignalEventEnabled(f)) EmitTestedSignal();}
00318 inline void   TQtWidget::SetIgnoreLeaveEnter(int ignore) { fIgnoreLeaveEnter = ignore; }
00319 
00320 #endif

Generated on Tue Jul 5 14:14:32 2011 for ROOT_528-00b_version by  doxygen 1.5.1