TQtClientGuard.cxx

Go to the documentation of this file.
00001 // @(#)root/qt:$Id: TQtClientGuard.cxx 28205 2009-04-14 19:38:00Z brun $
00002 // Author: Valeri Fine   21/01/2002
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2004, Rene Brun and Fons Rademakers.               *
00006  * Copyright (C) 2002 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 #include <assert.h>
00013 
00014 #include "TQtClientGuard.h"
00015 #include "TGQt.h"
00016 #include <QPixmap>
00017 #include <QBitmap>
00018 
00019 //______________________________________________________________________________
00020 void TQtClientGuard::Add(QWidget *w)
00021 {
00022    // add the widget to list of the "guarded" widget
00023    fQClientGuard.prepend(w);
00024    // fprintf(stderr," TQtClientGuard::Add %d %lp %p \n", TGQt::rootwid(w), TGQt::rootwid(w),w );
00025    connect(w,SIGNAL(destroyed()),this,SLOT(Disconnect()));
00026 }
00027 //______________________________________________________________________________
00028 TQtClientWidget *TQtClientGuard::Create(QWidget* mother, const char* name, Qt::WFlags f)
00029 {
00030    // TQtClientWidget object factory
00031    TQtClientWidget *w =  new TQtClientWidget(this,mother,name,f);
00032    // w->setBackgroundMode(Qt::NoBackground);
00033    Add(w);
00034    return  w;
00035 }
00036 //______________________________________________________________________________
00037 void TQtClientGuard::Delete(QWidget *w)
00038 {
00039    // Delete and unregister the object
00040    int found = -1;
00041 #if QT_VERSION < 0x40000
00042    if (w && ( (found = fQClientGuard.find(w))>=0))
00043 #else
00044    if (w && ( (found = fQClientGuard.indexOf(w))>=0) )
00045 #endif
00046    {
00047       w->hide();
00048       Disconnect(w,found);
00049       //((TQtClientWidget *)w)->SetClosing();
00050       //w->close(true);
00051       w->deleteLater();
00052       assert( w != QWidget::mouseGrabber() );
00053    }
00054 }
00055 //______________________________________________________________________________
00056 void TQtClientGuard::Disconnect(QWidget *w, int found)
00057 {
00058    // Disconnect and unregister the object
00059    // fprintf(stderr, "TQtClientGuard::Disconnecting widget %p\n", w);
00060    if ( (found>=0) ||
00061 #if QT_VERSION < 0x40000
00062       ( w && ( (found = fQClientGuard.find(w)) >=0 ) )  ) {
00063 #else
00064       ( w && ((found = fQClientGuard.indexOf(w)) >=0 ) )  ) {
00065 #endif
00066       // ungrab the poiner just in case
00067       QWidget *grabber = QWidget::mouseGrabber();
00068 #if QT_VERSION < 0x40000
00069       fQClientGuard.remove();
00070 #else
00071       fQClientGuard.removeAt(found);
00072 #endif
00073       disconnect(w,SIGNAL(destroyed()),this,SLOT(Disconnect()));
00074       if (grabber == w && gQt->IsRegistered(w) )
00075          gVirtualX->GrabPointer(TGQt::iwid(w), 0, 0, 0, kFALSE);
00076    } else {
00077       fDeadCounter++;
00078 #ifdef QTDEBUG
00079       printf(" %d Attempt to delete the dead widget %p\n",fDeadCounter, w);
00080 #endif
00081    }
00082 }
00083 //______________________________________________________________________________
00084 void TQtClientGuard::DisconnectChildren(TQtClientWidget *w)
00085 {
00086    // Disconnect all children of the registered widget
00087    if (w) {
00088 #if QT_VERSION < 0x40000
00089       const QObjectList *childList = w->children();
00090 #else /* QT_VERSION */
00091       const QObjectList &childList = w->children();
00092 #endif /* QT_VERSION */
00093       int nChild = 0;
00094 #if QT_VERSION < 0x40000
00095       if (childList) {
00096          nChild = childList->count();
00097          QObjectListIterator next(*childList);
00098          next.toLast();
00099 #else /* QT_VERSION */
00100       if (!childList.isEmpty()) {
00101          nChild = childList.count();
00102          QListIterator<QObject *> next(childList);
00103          next.toBack();
00104 #endif /* QT_VERSION */
00105          QObject *widget = 0;
00106          // while ( (widget = *next) )
00107 #if QT_VERSION < 0x40000
00108          for (widget=next.toLast(); (widget = next.current()); --next)
00109 #else /* QT_VERSION */
00110          while( next.hasPrevious() )
00111 #endif /* QT_VERSION */
00112          {
00113 #if QT_VERSION >= 0x40000
00114             widget = next.previous();
00115 #endif /* QT_VERSION */
00116             if (dynamic_cast<TQtClientWidget*>(widget)) {
00117                DisconnectChildren((TQtClientWidget*)widget);
00118             } else {
00119                 // assert(0);// Layout here
00120             }
00121          }
00122       }
00123       Disconnect(w);
00124    }
00125 }
00126 
00127 //______________________________________________________________________________
00128 QWidget *TQtClientGuard::Find(Window_t id)
00129 {
00130    // Find the object by ROOT id
00131 
00132    // fprintf(stderr," TQtClientGuard::Find %d %lp %p\n", id, id, TGQt::wid(id));
00133    int found = -1;
00134 #if QT_VERSION < 0x40000
00135    found = fQClientGuard.find(TGQt::wid(id));
00136 #else
00137    found = fQClientGuard.indexOf(TGQt::wid(id));
00138 #endif
00139    return  found >=0 ? fQClientGuard.at(found) : 0;
00140 }
00141 // protected slots:
00142 //______________________________________________________________________________
00143 void TQtClientGuard::Disconnect()
00144 {
00145    // Disconnect object Qt slot
00146    QWidget *w = (QWidget *)sender();
00147    // fprintf(stderr, "Disconnecting  SLOT widget %p\n", w);
00148    int found = -1;
00149 #if QT_VERSION < 0x40000
00150    found = fQClientGuard.find(w);
00151 #else
00152    found = fQClientGuard.indexOf(w);
00153 #endif
00154    if ( found >= 0 ) {
00155       if ( w == QWidget::mouseGrabber())
00156          fprintf(stderr," mouse is still grabbed by the dead wigdet !!!\n");
00157 #if QT_VERSION < 0x40000
00158       fQClientGuard.remove();
00159 #else
00160       fQClientGuard.removeAt(found);
00161 #endif
00162       disconnect(w,SIGNAL(destroyed()),this,SLOT(Disconnect()));
00163    }
00164 }
00165 
00166 //______________________________________________________________________________
00167 //
00168 //      TQtPixmapGuard
00169 //______________________________________________________________________________
00170 void TQtPixmapGuard::Add(QPixmap *w)
00171 {
00172    // add the widget to list of the "guarded" widget
00173    fQClientGuard.prepend(w);
00174    SetCurrent(0);
00175    // fprintf(stderr," TQtPixmapGuard::Add %d %lp %p \n", TGQt::iwid(w), TGQt::iwid(w),w );
00176 }
00177 //______________________________________________________________________________
00178 QPixmap* TQtPixmapGuard::Create(int w, int h, const uchar *bits, bool isXbitmap)
00179 {
00180    QPixmap *p = new QBitmap(
00181          QBitmap::fromData (QSize(w,h), bits, isXbitmap ? QImage::Format_MonoLSB : QImage::Format_Mono));
00182    Add(p);
00183    return p;
00184 }
00185 //______________________________________________________________________________
00186 QPixmap* TQtPixmapGuard::Create(int width, int height, int depth)
00187                                 // , Optimization optimization)
00188 {
00189    if (depth) {/* fool the compiler with  Qt4 */ }
00190    QPixmap *w =  new QPixmap(width,height); // ,optimization);
00191    Add(w);
00192    return  w;
00193 }
00194 //______________________________________________________________________________
00195 QPixmap* TQtPixmapGuard::Create(const QString &fileName, const char *format)
00196 //, ColorMode mode)
00197 {
00198    // QPixmap object factory
00199    // Constructs a pixmap from the file fileName.
00200 
00201    QPixmap *w =  new QPixmap(fileName,format); //,mode);
00202    Add(w);
00203    return  w;
00204 }
00205 //______________________________________________________________________________
00206 QPixmap* TQtPixmapGuard::Create(const QPixmap &src)
00207 {
00208    // QPixmap object factory
00209    // Constructs a pixmap that is a copy of pixmap.
00210    QPixmap *w =  new QPixmap(src);
00211    Add(w);
00212    return  w;
00213 }
00214 
00215 //______________________________________________________________________________
00216 QBitmap* TQtPixmapGuard::Create(const QBitmap &src)
00217 {
00218   // QBitmap object factory
00219 
00220    QBitmap *w =  new QBitmap(src);
00221    Add(w);
00222    return  w;
00223 }
00224 //______________________________________________________________________________
00225 QPixmap* TQtPixmapGuard::Create (const char* xpm[])
00226 {
00227    // QPixmap object factory
00228    // Constructs a pixmap from xpm
00229    QPixmap *w =  new QPixmap(xpm);
00230    Add(w);
00231    return  w;
00232 }
00233 //______________________________________________________________________________
00234 void TQtPixmapGuard::Delete(QPixmap *w)
00235 {
00236    // Delete and unregister QPixmap
00237    if (w)
00238    {
00239       Disconnect(w);
00240       delete w;
00241    }
00242 }
00243 //______________________________________________________________________________
00244 void TQtPixmapGuard::Disconnect(QPixmap *w, int found)
00245 {
00246    // Disconnect QPixmap
00247    
00248    if (found <0) found =
00249 #if QT_VERSION < 0x40000
00250                    fQClientGuard.find(w);
00251 #else
00252                    fQClientGuard.indexOf(w);
00253 #endif
00254    if ( found >=0 ) {
00255 #if QT_VERSION < 0x40000
00256       fQClientGuard.remove();
00257 #else
00258       fQClientGuard.removeAt(found);
00259 #endif
00260    } else {
00261       fDeadCounter++;
00262 #ifdef QTDEBUG
00263       printf(" %d Attempt to delete the dead pixmap %p\n",fDeadCounter, w);
00264 #endif
00265    }
00266    SetCurrent(found);
00267 }
00268 //______________________________________________________________________________
00269 QPixmap *TQtPixmapGuard::Pixmap(Pixmap_t id, bool needBitmap)
00270 {
00271    // Find QPixmap by ROOT pixmap id
00272    QPixmap *thisPix = 0;
00273    int found = -1;
00274    if (id) {
00275 #if QT_VERSION < 0x40000
00276       found = fQClientGuard.find((QPixmap *)id);
00277       assert( (thisPix  = fQClientGuard.current()) &&  (!needBitmap || thisPix->isQBitmap()) ) ;
00278 #else
00279       found = fQClientGuard.indexOf((QPixmap *)id);
00280       thisPix = found>=0 ? fQClientGuard[found] : 0;
00281       assert( thisPix  &&  (!needBitmap || thisPix->isQBitmap()) ) ;
00282 #endif
00283    }
00284    SetCurrent(found);
00285    return thisPix;
00286 }
00287 //______________________________________________________________________________
00288 QPixmap *TQtPixmapGuard::Find(Window_t /*id*/ )
00289 {
00290    // return the current QPixmap object
00291 
00292    // fprintf(stderr," TQtPixmapGuard::Find %d %lp %p index=%d\n", id, id, TGQt::wid(id),
00293    // fQClientGuard.find(TGQt::wid(id));
00294 #if QT_VERSION < 0x40000
00295    return  fQClientGuard.current();
00296 #else
00297    return  fLastFound >=0 ? fQClientGuard[fLastFound] : 0;
00298 #endif
00299 }
00300 // protected slots:
00301 //______________________________________________________________________________
00302 void TQtPixmapGuard::Disconnect()
00303 {
00304    // Disconnect Qt slot
00305    QPixmap *w = (QPixmap *)sender();
00306    int found = -1;
00307 #if QT_VERSION < 0x40000
00308    found = fQClientGuard.find(w);
00309    if ( found >=0 )   fQClientGuard.remove();
00310 #else
00311    found = fQClientGuard.indexOf(w);
00312    if ( found >=0 )   fQClientGuard.removeAt(found);
00313 #endif
00314    SetCurrent(found);
00315 }

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