ROOT logo
// @(#)root/qt:$Id: TQtWidget.h 33707 2010-06-02 06:27:34Z brun $
// Author: Valeri Fine   21/01/2002

/*************************************************************************
 * Copyright (C) 1995-2004, Rene Brun and Fons Rademakers.               *
 * Copyright (C) 2003 by Valeri Fine.                                    *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#ifndef ROOT_TQtWidget
#define ROOT_TQtWidget

// Definition of TQtWidget class
// "double-buffered" widget

#include <assert.h>
#include "Rtypes.h"
#include "TCanvas.h"

#ifndef __CINT__
#  include <QtGui/QWidget>
#  include <QtGui/QMouseEvent>
#  include <QtGui/QShowEvent>
#  include <QtGui/QFocusEvent>
#  include <QtGui/QKeyEvent>
#  include <QtGui/QResizeEvent>
#  include <QtCore/QEvent>
#  include <QtGui/QPaintEvent>
#  include <QtGui/QPaintDevice>
#  include <QtCore/QSize>
#  include <QtCore/QPoint>
#  include <QtCore/QPointer>
#  include <QtGui/QPixmap>
#  include "TQtCanvasPainter.h"
#else
  // List of the fake classes to make RootCint happy.
  class QWidget;
  class QPixmap;
  class QMouseEvent;
  class QFocusEvent;
  class QCustomEvent;
  class QKeyEvent;
  class QShowEvent;
  class QPaintEvent;
  class QPaintDevice;
  class QResizeEvent;
  class QSize;  
  class QString;
  class QEvent;
  class QSizePolicy;
  class QContextMenuEvent;
  class QSize;
  class QPoint;
  class TQtCanvasPainter;
#endif
  class QTimer;
  class TApplication;
//
// TQtWidget is a custom QWidget to back ROOT TCanvas.
//
// It can be used within Qt-based program and with Qt Designer as a "regular"
// Qt QWidget to create the Qt widget wihe builtin TCanvas'
//
enum EEventTrackingBits {
       kMousePressEvent       = BIT(0), // emit signal as soon as TCanvas processed mousePressEvent       QMouseEvent
       kMouseMoveEvent        = BIT(1), // emit signal as soon as TCanvas processed mouseMoveEvent        QMouseEvent
       kMouseReleaseEvent     = BIT(2), // emit signal as soon as TCanvas processed mouseReleaseEvent     QMouseEvent
       kMouseDoubleClickEvent = BIT(3), // emit signal as soon as TCanvas processed mouseDoubleClickEvent QMouseEvent
       kKeyPressEvent         = BIT(4), // emit signal as soon as TCanvas processed keyPressEvent         QKeyEvent
       kEnterEvent            = BIT(5), // emit signal as soon as TCanvas processed enterEvent            QEvent
       kLeaveEvent            = BIT(6)  // emit signal as soon as TCanvas processed leaveEvent            QEvent
};

//___________________________________________________________________
class TQtWidgetBuffer
{
private:
   const QWidget *fWidget;
   QPaintDevice  *fBuffer;
   bool  fIsImage;
public:
   TQtWidgetBuffer(const QWidget *w, bool clear=false);
   TQtWidgetBuffer(const TQtWidgetBuffer &b);
   const QPaintDevice  *Buffer() const  { return fBuffer; }
   QPaintDevice  *Buffer()  { return fBuffer; }
   ~TQtWidgetBuffer();
   void Clear();
   bool PaintingActive(){ return fBuffer ? fBuffer->paintingActive() : false; }
   QRect Rect () const { return fWidget->rect();                }
   int Height () const { return fBuffer ? fBuffer->height() : 0;}
   int Width  () const { return fBuffer ? fBuffer->width() : 0; }
};

//___________________________________________________________________
class  TQtWidget : public QWidget {
#ifndef __CINT__   
 Q_OBJECT
 friend class TQtSynchPainting;
#endif
private:

    TQtWidget(const TQtWidget&);
	 void operator=(const TQtWidget&);
   //----- Private bits, clients can only test but not change them
   UInt_t         fBits;       //bit field status word
   enum {
      kBitMask       = 0x00ffffff
   };
   bool fNeedStretch;
#ifndef __CINT__   
   QPointer<TQtCanvasPainter> fCanvasDecorator;  //< The object to paint onto the TQtWidget on the top of TCanvas image
#endif
protected:
   void Init(); 
   void ResetCanvas() { fCanvas = 0;}

public:
   enum {
      kEXITSIZEMOVE,
      kENTERSIZEMOVE,
      kFORCESIZE
   };
#ifndef __CINT__
  TQtWidget( QWidget* parent, const char* name, Qt::WFlags f=0, bool embedded=TRUE);
  TQtWidget( QWidget* parent=0, Qt::WFlags f=0, bool embedded=TRUE);
#else
  TQtWidget( QWidget* parent=0);
#endif  
  virtual ~TQtWidget();
  void SetCanvas(TCanvas *c);
//  inline TCanvas  *GetCanvas() const         { return fCanvas;}
  TCanvas  *GetCanvas() const;
  TQtWidgetBuffer  &SetBuffer();
  const TQtWidgetBuffer  *GetBuffer()  const;
  QPixmap  *GetOffScreenBuffer()  const;

  // overloaded methods
  virtual void Erase ();
  bool    IsDoubleBuffered() const { return fDoubleBufferOn; }
  void    SetDoubleBuffer(bool on=TRUE);
  virtual void SetSaveFormat(const char *format);

protected:
   friend class TGQt;
   friend class TQtFeedBackWidget;
   TCanvas           *fCanvas;
   TQtWidgetBuffer   *fPixmapID;     // Double buffer of this widget
   TQtWidgetBuffer   *fPixmapScreen; // Double buffer for no-double buffer operation
   bool        fPaint;
   bool        fSizeChanged;
   bool        fDoubleBufferOn;
   bool        fEmbedded;
   QSize       fSizeHint;
   QWidget    *fWrapper;
   QString     fSaveFormat;
   bool        fInsidePaintEvent;
   QPoint      fOldMousePos;
   int         fIgnoreLeaveEnter;
   QTimer     *fRefreshTimer;


   void SetRootID(QWidget *wrapper);
   QWidget *GetRootID() const;
   virtual void EmitCanvasPainted() { emit CanvasPainted(); }
   TCanvas  *Canvas();
   bool paintFlag(bool mode=TRUE);
   void AdjustBufferSize();

   bool PaintingActive () const;
   void SetIgnoreLeaveEnter(int ignore=1);


   virtual void enterEvent       ( QEvent *      );
   virtual void customEvent      ( QEvent *      );
   virtual void contextMenuEvent ( QContextMenuEvent *);
   virtual void focusInEvent     ( QFocusEvent * );
   virtual void focusOutEvent    ( QFocusEvent * );
   virtual void leaveEvent       ( QEvent *      );
   virtual void mouseDoubleClickEvent(QMouseEvent* );
   virtual void mouseMoveEvent   ( QMouseEvent * );
   virtual void mousePressEvent  ( QMouseEvent * );
   virtual void mouseReleaseEvent( QMouseEvent * );
   virtual void keyPressEvent    ( QKeyEvent *   );
   virtual void keyReleaseEvent  ( QKeyEvent *   );
   virtual void showEvent        ( QShowEvent *  );
   virtual void paintEvent       ( QPaintEvent * );
   virtual void resizeEvent      ( QResizeEvent *);
   //  Layout methods:
   virtual void        SetSizeHint (const QSize &size);
public:
   virtual QSize       sizeHint () const;        // returns the preferred size of the widget.
   virtual QSize       minimumSizeHint () const; // returns the smallest size the widget can have.
   virtual QSizePolicy sizePolicy () const;      // returns a QSizePolicy; a value describing the space requirements of the
protected:
   // -- A special event handler
   virtual void exitSizeEvent ();
   virtual void stretchWidget(QResizeEvent *e);
   TQtCanvasPainter *CanvasDecorator();
public:
   //----- bit manipulation (a'la TObject )
   void     SetBit     (UInt_t f, Bool_t set);
   void     SetBit     (UInt_t f);
   void     ResetBit   (UInt_t f);
   Bool_t   TestBit    (UInt_t f) const;
   Int_t    TestBits   (UInt_t f) const;
   void     InvertBit  (UInt_t f);
   void     EmitSignal (UInt_t f);
   void     EmitTestedSignal();
   UInt_t   GetAllBits() const;
   void     SetAllBits(UInt_t f);
   void SetCanvasDecorator( TQtCanvasPainter *decorator);
   
public:
   // Static method to immitate ROOT as needed
   static TApplication *InitRint(Bool_t prompt=kFALSE, const char *appClassName="QtRint", int *argc=0, char **argv=0,
          void *options = 0, int numOptions = 0, Bool_t noLogo = kTRUE);
   //  Proxy methods to access the TCanvas selected TObject 
   //  and last processed ROOT TCanvas event
   Int_t             GetEvent()       const;
   Int_t             GetEventX()      const;
   Int_t             GetEventY()      const;
   TObject          *GetSelected()    const;
   Int_t             GetSelectedX()   const;
   Int_t             GetSelectedY()   const;
   TVirtualPad      *GetSelectedPad() const;

   //----- bit Qt signal emitting the Qt signal to track mouse movement
   void     EnableSignalEvents  (UInt_t f);
   void     DisableSignalEvents (UInt_t f);
   Bool_t   IsSignalEventEnabled(UInt_t f) const;
   
   static TCanvas   *Canvas(TQtWidget *widget);
   static TQtWidget *Canvas(const TCanvas *canvas);
   static TQtWidget *Canvas(Int_t id);

public slots:
   virtual void cd();
   virtual void cd(int subpadnumber);
   void Disconnect();
   void Refresh();
   virtual bool Save(const QString &fileName) const;
   virtual bool Save(const char    *fileName) const;
   virtual bool Save(const QString &fileName,const char *format,int quality=60) const;
   virtual bool Save(const char    *fileName,const char *format,int quality=60) const;
#ifndef __CINT__
protected slots:
   void RefreshCB();
   
signals:
   // emit the Qt signal when the double buffer of the TCamvas has been filled up
   void CanvasPainted();  // Signal the TCanvas has been painted onto the screen
   void Saved(bool ok);   // Signal the TCanvas has been saved into the file
   void RootEventProcessed(TObject *selected, unsigned int event, TCanvas *c);
#endif

#ifndef Q_MOC_RUN
   ClassDef(TQtWidget,0) // QWidget to back ROOT TCanvas (Can be used with Qt designer)
#endif
};

//______________________________________________________________________________
inline TCanvas  *TQtWidget::GetCanvas() const         { return fCanvas; }

//______________________________________________________________________________
inline const TQtWidgetBuffer  *TQtWidget::GetBuffer()  const { 
   //  return the current widget buffer;
   return IsDoubleBuffered() ? fPixmapScreen : fPixmapID;
}

//______________________________________________________________________________
inline bool TQtWidget::PaintingActive () const {
  return QWidget::paintingActive() || (fPixmapID && fPixmapID->PaintingActive())
     || (fPixmapScreen && fPixmapScreen->PaintingActive());
}
//______________________________________________________________________________
inline void TQtWidget::SetRootID(QWidget *wrapper) { fWrapper = wrapper;}
//______________________________________________________________________________
inline QWidget *TQtWidget::GetRootID() const { return fWrapper;}

//______________________________________________________________________________
//
//  Proxy methods to access the TCanvas selected TObject
//  and last processed event
//______________________________________________________________________________
inline Int_t        TQtWidget::GetEvent()       const { return GetCanvas()->GetEvent();       }
//______________________________________________________________________________
inline Int_t        TQtWidget::GetEventX()      const { return GetCanvas()->GetEventX();      }
//______________________________________________________________________________
inline Int_t        TQtWidget::GetEventY()      const { return GetCanvas()->GetEventY();      }
//______________________________________________________________________________
inline TObject     *TQtWidget::GetSelected()    const { return GetCanvas()->GetSelected();    }
//______________________________________________________________________________
inline Int_t        TQtWidget::GetSelectedX()   const { return GetCanvas()->GetSelectedX();   }
//______________________________________________________________________________
inline Int_t        TQtWidget::GetSelectedY()   const { return GetCanvas()->GetSelectedY();   }
//______________________________________________________________________________
inline TVirtualPad *TQtWidget::GetSelectedPad() const { return GetCanvas()->GetSelectedPad(); }

//----- bit manipulation
inline UInt_t TQtWidget::GetAllBits() const       { return fBits;                       }
inline void   TQtWidget::SetAllBits(UInt_t f)     { fBits = f;                          }
inline void   TQtWidget::SetBit(UInt_t f)         { fBits |= f & kBitMask;              }
inline void   TQtWidget::ResetBit(UInt_t f)       { fBits &= ~(f & kBitMask);           }
inline Bool_t TQtWidget::TestBit(UInt_t f) const  { return (Bool_t) ((fBits & f) != 0); }
inline Int_t  TQtWidget::TestBits(UInt_t f) const { return (Int_t) (fBits & f);         }
inline void   TQtWidget::InvertBit(UInt_t f)      { fBits ^= f & kBitMask;              }

inline TQtCanvasPainter *TQtWidget::CanvasDecorator() { return fCanvasDecorator;   }
inline void   TQtWidget::SetCanvasDecorator( TQtCanvasPainter *decorator) { fCanvasDecorator = decorator;}
   
inline void   TQtWidget::EnableSignalEvents  (UInt_t f){ SetBit  (f); }
inline void   TQtWidget::DisableSignalEvents (UInt_t f){ ResetBit(f); }
inline Bool_t TQtWidget::IsSignalEventEnabled(UInt_t f) const { return TestBit (f); }
inline void   TQtWidget::EmitSignal(UInt_t f)  {if (IsSignalEventEnabled(f)) EmitTestedSignal();}
inline void   TQtWidget::SetIgnoreLeaveEnter(int ignore) { fIgnoreLeaveEnter = ignore; }

#endif
 TQtWidget.h:1
 TQtWidget.h:2
 TQtWidget.h:3
 TQtWidget.h:4
 TQtWidget.h:5
 TQtWidget.h:6
 TQtWidget.h:7
 TQtWidget.h:8
 TQtWidget.h:9
 TQtWidget.h:10
 TQtWidget.h:11
 TQtWidget.h:12
 TQtWidget.h:13
 TQtWidget.h:14
 TQtWidget.h:15
 TQtWidget.h:16
 TQtWidget.h:17
 TQtWidget.h:18
 TQtWidget.h:19
 TQtWidget.h:20
 TQtWidget.h:21
 TQtWidget.h:22
 TQtWidget.h:23
 TQtWidget.h:24
 TQtWidget.h:25
 TQtWidget.h:26
 TQtWidget.h:27
 TQtWidget.h:28
 TQtWidget.h:29
 TQtWidget.h:30
 TQtWidget.h:31
 TQtWidget.h:32
 TQtWidget.h:33
 TQtWidget.h:34
 TQtWidget.h:35
 TQtWidget.h:36
 TQtWidget.h:37
 TQtWidget.h:38
 TQtWidget.h:39
 TQtWidget.h:40
 TQtWidget.h:41
 TQtWidget.h:42
 TQtWidget.h:43
 TQtWidget.h:44
 TQtWidget.h:45
 TQtWidget.h:46
 TQtWidget.h:47
 TQtWidget.h:48
 TQtWidget.h:49
 TQtWidget.h:50
 TQtWidget.h:51
 TQtWidget.h:52
 TQtWidget.h:53
 TQtWidget.h:54
 TQtWidget.h:55
 TQtWidget.h:56
 TQtWidget.h:57
 TQtWidget.h:58
 TQtWidget.h:59
 TQtWidget.h:60
 TQtWidget.h:61
 TQtWidget.h:62
 TQtWidget.h:63
 TQtWidget.h:64
 TQtWidget.h:65
 TQtWidget.h:66
 TQtWidget.h:67
 TQtWidget.h:68
 TQtWidget.h:69
 TQtWidget.h:70
 TQtWidget.h:71
 TQtWidget.h:72
 TQtWidget.h:73
 TQtWidget.h:74
 TQtWidget.h:75
 TQtWidget.h:76
 TQtWidget.h:77
 TQtWidget.h:78
 TQtWidget.h:79
 TQtWidget.h:80
 TQtWidget.h:81
 TQtWidget.h:82
 TQtWidget.h:83
 TQtWidget.h:84
 TQtWidget.h:85
 TQtWidget.h:86
 TQtWidget.h:87
 TQtWidget.h:88
 TQtWidget.h:89
 TQtWidget.h:90
 TQtWidget.h:91
 TQtWidget.h:92
 TQtWidget.h:93
 TQtWidget.h:94
 TQtWidget.h:95
 TQtWidget.h:96
 TQtWidget.h:97
 TQtWidget.h:98
 TQtWidget.h:99
 TQtWidget.h:100
 TQtWidget.h:101
 TQtWidget.h:102
 TQtWidget.h:103
 TQtWidget.h:104
 TQtWidget.h:105
 TQtWidget.h:106
 TQtWidget.h:107
 TQtWidget.h:108
 TQtWidget.h:109
 TQtWidget.h:110
 TQtWidget.h:111
 TQtWidget.h:112
 TQtWidget.h:113
 TQtWidget.h:114
 TQtWidget.h:115
 TQtWidget.h:116
 TQtWidget.h:117
 TQtWidget.h:118
 TQtWidget.h:119
 TQtWidget.h:120
 TQtWidget.h:121
 TQtWidget.h:122
 TQtWidget.h:123
 TQtWidget.h:124
 TQtWidget.h:125
 TQtWidget.h:126
 TQtWidget.h:127
 TQtWidget.h:128
 TQtWidget.h:129
 TQtWidget.h:130
 TQtWidget.h:131
 TQtWidget.h:132
 TQtWidget.h:133
 TQtWidget.h:134
 TQtWidget.h:135
 TQtWidget.h:136
 TQtWidget.h:137
 TQtWidget.h:138
 TQtWidget.h:139
 TQtWidget.h:140
 TQtWidget.h:141
 TQtWidget.h:142
 TQtWidget.h:143
 TQtWidget.h:144
 TQtWidget.h:145
 TQtWidget.h:146
 TQtWidget.h:147
 TQtWidget.h:148
 TQtWidget.h:149
 TQtWidget.h:150
 TQtWidget.h:151
 TQtWidget.h:152
 TQtWidget.h:153
 TQtWidget.h:154
 TQtWidget.h:155
 TQtWidget.h:156
 TQtWidget.h:157
 TQtWidget.h:158
 TQtWidget.h:159
 TQtWidget.h:160
 TQtWidget.h:161
 TQtWidget.h:162
 TQtWidget.h:163
 TQtWidget.h:164
 TQtWidget.h:165
 TQtWidget.h:166
 TQtWidget.h:167
 TQtWidget.h:168
 TQtWidget.h:169
 TQtWidget.h:170
 TQtWidget.h:171
 TQtWidget.h:172
 TQtWidget.h:173
 TQtWidget.h:174
 TQtWidget.h:175
 TQtWidget.h:176
 TQtWidget.h:177
 TQtWidget.h:178
 TQtWidget.h:179
 TQtWidget.h:180
 TQtWidget.h:181
 TQtWidget.h:182
 TQtWidget.h:183
 TQtWidget.h:184
 TQtWidget.h:185
 TQtWidget.h:186
 TQtWidget.h:187
 TQtWidget.h:188
 TQtWidget.h:189
 TQtWidget.h:190
 TQtWidget.h:191
 TQtWidget.h:192
 TQtWidget.h:193
 TQtWidget.h:194
 TQtWidget.h:195
 TQtWidget.h:196
 TQtWidget.h:197
 TQtWidget.h:198
 TQtWidget.h:199
 TQtWidget.h:200
 TQtWidget.h:201
 TQtWidget.h:202
 TQtWidget.h:203
 TQtWidget.h:204
 TQtWidget.h:205
 TQtWidget.h:206
 TQtWidget.h:207
 TQtWidget.h:208
 TQtWidget.h:209
 TQtWidget.h:210
 TQtWidget.h:211
 TQtWidget.h:212
 TQtWidget.h:213
 TQtWidget.h:214
 TQtWidget.h:215
 TQtWidget.h:216
 TQtWidget.h:217
 TQtWidget.h:218
 TQtWidget.h:219
 TQtWidget.h:220
 TQtWidget.h:221
 TQtWidget.h:222
 TQtWidget.h:223
 TQtWidget.h:224
 TQtWidget.h:225
 TQtWidget.h:226
 TQtWidget.h:227
 TQtWidget.h:228
 TQtWidget.h:229
 TQtWidget.h:230
 TQtWidget.h:231
 TQtWidget.h:232
 TQtWidget.h:233
 TQtWidget.h:234
 TQtWidget.h:235
 TQtWidget.h:236
 TQtWidget.h:237
 TQtWidget.h:238
 TQtWidget.h:239
 TQtWidget.h:240
 TQtWidget.h:241
 TQtWidget.h:242
 TQtWidget.h:243
 TQtWidget.h:244
 TQtWidget.h:245
 TQtWidget.h:246
 TQtWidget.h:247
 TQtWidget.h:248
 TQtWidget.h:249
 TQtWidget.h:250
 TQtWidget.h:251
 TQtWidget.h:252
 TQtWidget.h:253
 TQtWidget.h:254
 TQtWidget.h:255
 TQtWidget.h:256
 TQtWidget.h:257
 TQtWidget.h:258
 TQtWidget.h:259
 TQtWidget.h:260
 TQtWidget.h:261
 TQtWidget.h:262
 TQtWidget.h:263
 TQtWidget.h:264
 TQtWidget.h:265
 TQtWidget.h:266
 TQtWidget.h:267
 TQtWidget.h:268
 TQtWidget.h:269
 TQtWidget.h:270
 TQtWidget.h:271
 TQtWidget.h:272
 TQtWidget.h:273
 TQtWidget.h:274
 TQtWidget.h:275
 TQtWidget.h:276
 TQtWidget.h:277
 TQtWidget.h:278
 TQtWidget.h:279
 TQtWidget.h:280
 TQtWidget.h:281
 TQtWidget.h:282
 TQtWidget.h:283
 TQtWidget.h:284
 TQtWidget.h:285
 TQtWidget.h:286
 TQtWidget.h:287
 TQtWidget.h:288
 TQtWidget.h:289
 TQtWidget.h:290
 TQtWidget.h:291
 TQtWidget.h:292
 TQtWidget.h:293
 TQtWidget.h:294
 TQtWidget.h:295
 TQtWidget.h:296
 TQtWidget.h:297
 TQtWidget.h:298
 TQtWidget.h:299
 TQtWidget.h:300
 TQtWidget.h:301
 TQtWidget.h:302
 TQtWidget.h:303
 TQtWidget.h:304
 TQtWidget.h:305
 TQtWidget.h:306
 TQtWidget.h:307
 TQtWidget.h:308
 TQtWidget.h:309
 TQtWidget.h:310
 TQtWidget.h:311
 TQtWidget.h:312
 TQtWidget.h:313
 TQtWidget.h:314
 TQtWidget.h:315
 TQtWidget.h:316
 TQtWidget.h:317
 TQtWidget.h:318
 TQtWidget.h:319
 TQtWidget.h:320