kdeui Library API Documentation

kdialog.cpp

00001 /*  This file is part of the KDE Libraries
00002  *  Copyright (C) 1998 Thomas Tanghus (tanghus@earthling.net)
00003  *  Additions 1999-2000 by Espen Sand (espen@kde.org)
00004  *
00005  *  This library is free software; you can redistribute it and/or
00006  *  modify it under the terms of the GNU Library General Public
00007  *  License as published by the Free Software Foundation; either
00008  *  version 2 of the License, or (at your option) any later version.
00009  *
00010  *  This library is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  *  Library General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU Library General Public License
00016  *  along with this library; see the file COPYING.LIB.  If not, write to
00017  *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018  *  Boston, MA 02111-1307, USA.
00019  */
00020 
00021 #include <kconfig.h>
00022 #include <kapplication.h>
00023 #include <kdialog.h>
00024 #include <kdebug.h>
00025 #include <kstaticdeleter.h>
00026 
00027 #include <qlayout.h>
00028 #include <qobjectlist.h>
00029 #include <qguardedptr.h>
00030 #include <qlineedit.h>
00031 #include <qvaluelist.h>
00032 #include <qtimer.h>
00033 #include <qcursor.h>
00034 
00035 #include "config.h"
00036 #if defined Q_WS_X11 && ! defined K_WS_QTONLY
00037 #include <netwm.h> 
00038 #endif
00039 
00040 int KDialog::mMarginSize = 11;
00041 int KDialog::mSpacingSize = 6;
00042 
00043 template class QPtrList<QLayoutItem>;
00044 
00045 KDialog::KDialog(QWidget *parent, const char *name, bool modal, WFlags f)
00046   : QDialog(parent, name, modal, f)
00047 {
00048 }
00049 
00050 //
00051 // Grab QDialogs keypresses if non-modal.
00052 //
00053 void KDialog::keyPressEvent(QKeyEvent *e)
00054 {
00055   if ( e->state() == 0 )
00056   {
00057     switch ( e->key() )
00058     {
00059       case Key_Escape:
00060       case Key_Enter:
00061       case Key_Return:
00062       {
00063         if(testWFlags(WType_Dialog | WShowModal))
00064     {
00065           QDialog::keyPressEvent(e);
00066     }
00067         else
00068         {
00069       e->ignore();
00070         }
00071       }
00072       break;
00073       default:
00074     e->ignore();
00075     return;
00076     }
00077   }
00078   else
00079   {
00080       // accept the dialog when Ctrl-Return is pressed
00081       if ( e->state() == ControlButton &&
00082            (e->key() == Key_Return || e->key() == Key_Enter) )
00083       {
00084           e->accept();
00085           accept();
00086       }
00087       else
00088       {
00089           e->ignore();
00090       }
00091   }
00092 }
00093 
00094 
00095 int KDialog::marginHint()
00096 {
00097   return( mMarginSize );
00098 }
00099 
00100 
00101 int KDialog::spacingHint()
00102 {
00103   return( mSpacingSize );
00104 }
00105 
00106 // KDE4: Remove me
00107 void KDialog::polish()
00108 {
00109   QDialog::polish();
00110 }
00111 
00112 
00113 void KDialog::setCaption( const QString &_caption )
00114 {
00115   QString caption = kapp ? kapp->makeStdCaption( _caption ) : _caption;
00116   setPlainCaption( caption );
00117 }
00118 
00119 
00120 void KDialog::setPlainCaption( const QString &caption )
00121 {
00122   QDialog::setCaption( caption );
00123 
00124 #if defined Q_WS_X11 && ! defined K_WS_QTONLY
00125 //#ifdef Q_WS_X11 //FIXME(E) Implement for Qt/E
00126   NETWinInfo info( qt_xdisplay(), winId(), qt_xrootwin(), 0 );
00127   info.setName( caption.utf8().data() );
00128 #endif
00129 }
00130 
00131 
00132 void KDialog::resizeLayout( QWidget *w, int margin, int spacing )
00133 {
00134   if( w->layout() )
00135   {
00136     resizeLayout( w->layout(), margin, spacing );
00137   }
00138 
00139   if( w->children() )
00140   {
00141     QObjectList *l = (QObjectList*)w->children(); // silence please
00142     for( uint i=0; i < l->count(); i++ )
00143     {
00144       QObject *o = l->at(i);
00145       if( o->isWidgetType() )
00146       {
00147     resizeLayout( (QWidget*)o, margin, spacing );
00148       }
00149     }
00150   }
00151 }
00152 
00153 
00154 void KDialog::resizeLayout( QLayoutItem *lay, int margin, int spacing )
00155 {
00156   QLayoutIterator it = lay->iterator();
00157   QLayoutItem *child;
00158   while ( (child = it.current() ) )
00159   {
00160     resizeLayout( child, margin, spacing );
00161     ++it;
00162   }
00163   if( lay->layout() != 0 )
00164   {
00165     lay->layout()->setMargin( margin );
00166     lay->layout()->setSpacing( spacing );
00167   }
00168 }
00169 
00170 static QRect screenRect( QWidget *w, int screen )
00171 {
00172   QDesktopWidget *desktop = QApplication::desktop();
00173   KConfig gc("kdeglobals", false, false);
00174   gc.setGroup("Windows");
00175   if (desktop->isVirtualDesktop() &&
00176       gc.readBoolEntry("XineramaEnabled", true) &&
00177       gc.readBoolEntry("XineramaPlacementEnabled", true)) {
00178     if ( screen < 0 || screen >= desktop->numScreens() ) {
00179       if ( screen == -1 ) {
00180         screen = desktop->primaryScreen();
00181       } else if ( screen == -3 ) {
00182         screen = desktop->screenNumber( QCursor::pos() );
00183       } else {
00184         screen = desktop->screenNumber( w );
00185       }
00186     }
00187     return desktop->screenGeometry(screen);
00188   } else {
00189     return desktop->geometry();
00190   }
00191 }
00192 
00193 void KDialog::centerOnScreen( QWidget *w, int screen )
00194 {
00195   if ( !w )
00196     return;
00197   QRect r = screenRect( w, screen );
00198 
00199   w->move( r.center().x() - w->width()/2,
00200            r.center().y() - w->height()/2 );
00201 }
00202 
00203 bool KDialog::avoidArea( QWidget *w, const QRect& area, int screen )
00204 {
00205   if ( !w )
00206     return false;
00207   QRect fg = w->frameGeometry();
00208   if ( !fg.intersects( area ) )
00209     return true; // nothing to do.
00210 
00211   QRect scr = screenRect( w, screen );
00212   QRect avoid( area ); // let's add some margin
00213   avoid.moveBy( -5, -5 );
00214   avoid.rRight() += 10;
00215   avoid.rBottom() += 10;
00216 
00217   if ( QMAX( fg.top(), avoid.top() ) <= QMIN( fg.bottom(), avoid.bottom() ) )
00218   {
00219     // We need to move the widget up or down
00220     int spaceAbove = QMAX(0, avoid.top() - scr.top());
00221     int spaceBelow = QMAX(0, scr.bottom() - avoid.bottom());
00222     if ( spaceAbove > spaceBelow ) // where's the biggest side?
00223       if ( fg.height() <= spaceAbove ) // big enough?
00224         fg.setY( avoid.top() - fg.height() );
00225       else
00226         return false;
00227     else
00228       if ( fg.height() <= spaceBelow ) // big enough?
00229         fg.setY( avoid.bottom() );
00230       else
00231         return false;
00232   }
00233 
00234   if ( QMAX( fg.left(), avoid.left() ) <= QMIN( fg.right(), avoid.right() ) )
00235   {
00236     // We need to move the widget left or right
00237     int spaceLeft = QMAX(0, avoid.left() - scr.left());
00238     int spaceRight = QMAX(0, scr.right() - avoid.right());
00239     if ( spaceLeft > spaceRight ) // where's the biggest side?
00240       if ( fg.width() <= spaceLeft ) // big enough?
00241         fg.setX( avoid.left() - fg.width() );
00242       else
00243         return false;
00244     else
00245       if ( fg.width() <= spaceRight ) // big enough?
00246         fg.setX( avoid.right() );
00247       else
00248         return false;
00249   }
00250   //kdDebug() << "Moving window to " << fg.x() << "," << fg.y() << endl;
00251   w->move(fg.x(), fg.y());
00252   return true;
00253 }
00254 
00255 class KDialogQueuePrivate
00256 {
00257 public:
00258   QValueList< QGuardedPtr<QDialog> > queue;
00259   bool busy;
00260 };
00261 
00262 static KStaticDeleter<KDialogQueue> ksdkdq;
00263 
00264 KDialogQueue *KDialogQueue::_self=0;
00265 
00266 KDialogQueue* KDialogQueue::self()
00267 {
00268    if (!_self)
00269       _self = ksdkdq.setObject(_self, new KDialogQueue);
00270    return _self;
00271 }
00272 
00273 KDialogQueue::KDialogQueue()
00274 {
00275    d = new KDialogQueuePrivate;
00276    d->busy = false;
00277 }
00278 
00279 KDialogQueue::~KDialogQueue()
00280 {
00281    delete d; d = 0;
00282    _self = 0;
00283 }
00284 
00285 // static
00286 void KDialogQueue::queueDialog(QDialog *dialog)
00287 {
00288    KDialogQueue *_this = self();
00289    _this->d->queue.append(dialog);
00290    QTimer::singleShot(0, _this, SLOT(slotShowQueuedDialog()));
00291 }
00292 
00293 void KDialogQueue::slotShowQueuedDialog()
00294 {
00295    if (d->busy)
00296       return;
00297    QDialog *dialog;
00298    do {
00299       if(!d->queue.count())
00300          return;
00301       dialog = d->queue.first();
00302       d->queue.remove(d->queue.begin());
00303    }
00304    while(!dialog);
00305 
00306    d->busy = true;
00307    dialog->exec();
00308    d->busy = false;
00309    delete dialog;
00310 
00311    if (d->queue.count())
00312       QTimer::singleShot(20, this, SLOT(slotShowQueuedDialog()));
00313    else
00314       ksdkdq.destructObject(); // Suicide.
00315 }
00316 
00317 void KDialog::virtual_hook( int, void* )
00318 { /*BASE::virtual_hook( id, data );*/ }
00319 
00320 #include "kdialog.moc"
KDE Logo
This file is part of the documentation for kdeui Library Version 3.3.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Sep 23 17:11:53 2004 by doxygen 1.3.8-20040913 written by Dimitri van Heesch, © 1997-2003