• Skip to content
  • Skip to link menu
KDE 4.3 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KDEUI

kdialog.cpp

Go to the documentation of this file.
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  *                      by Holger Freyther <freyther@kde.org>
00005  *            2005-2009 by Olivier Goffart (ogoffart at kde.org)
00006  *
00007  *  This library is free software; you can redistribute it and/or
00008  *  modify it under the terms of the GNU Library General Public
00009  *  License as published by the Free Software Foundation; either
00010  *  version 2 of the License, or (at your option) any later version.
00011  *
00012  *  This library is distributed in the hope that it will be useful,
00013  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  *  Library General Public License for more details.
00016  *
00017  *  You should have received a copy of the GNU Library General Public License
00018  *  along with this library; see the file COPYING.LIB.  If not, write to
00019  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00020  *  Boston, MA 02110-1301, USA.
00021  */
00022 
00023 #include "kdialog.h"
00024 #include "kdialog_p.h"
00025 #include <kdebug.h>
00026 #include "kdialogqueue_p.h"
00027 
00028 #include <config.h>
00029 
00030 #include <QApplication>
00031 #include <QDesktopWidget>
00032 #include <QDialogButtonBox>
00033 #include <QHBoxLayout>
00034 #include <QHideEvent>
00035 #include <QPointer>
00036 #include <QStyle>
00037 #include <QTimer>
00038 #include <QVBoxLayout>
00039 #include <QWhatsThis>
00040 
00041 #include <klocale.h>
00042 #include <kpushbutton.h>
00043 #include <kseparator.h>
00044 #include <kstandardguiitem.h>
00045 #include <ktoolinvocation.h>
00046 #include <kurllabel.h>
00047 #include <kwhatsthismanager_p.h>
00048 
00049 #ifdef Q_WS_X11
00050 #include <qx11info_x11.h>
00051 #include <netwm.h>
00052 #endif
00053 
00054 void KDialogPrivate::setupLayout()
00055 {
00056     Q_Q(KDialog);
00057     if (!dirty) {
00058         QMetaObject::invokeMethod( q, "queuedLayoutUpdate", Qt::QueuedConnection );
00059         dirty = true;
00060     }
00061 }
00062 
00063 void KDialogPrivate::queuedLayoutUpdate()
00064 {
00065   if (!dirty)
00066     return;
00067 
00068   dirty = false;
00069 
00070   Q_Q(KDialog);
00071 
00072   if (q->layout() && q->layout() != mTopLayout) {
00073       kWarning(240) << q->metaObject()->className() << "created with a layout; don't do that, KDialog takes care of it, use mainWidget or setMainWidget instead";
00074       delete q->layout();
00075   }
00076 
00077   delete mTopLayout;
00078 
00079   if ( mButtonOrientation == Qt::Horizontal )
00080         mTopLayout = new QVBoxLayout(q);
00081   else
00082         mTopLayout = new QHBoxLayout(q);
00083 
00084   if ( mUrlHelp )
00085     mTopLayout->addWidget( mUrlHelp, 0, Qt::AlignRight );
00086 
00087   if ( mMainWidget )
00088     mTopLayout->addWidget( mMainWidget, 10 );
00089 
00090   if ( mDetailsWidget )
00091     mTopLayout->addWidget( mDetailsWidget );
00092 
00093   if ( mActionSeparator )
00094     mTopLayout->addWidget( mActionSeparator );
00095 
00096   if ( mButtonBox ) {
00097     mButtonBox->setOrientation( mButtonOrientation );
00098     mTopLayout->addWidget( mButtonBox );
00099   }
00100 }
00101 
00102 void KDialogPrivate::setButtonFocus(QPushButton *button, bool isDefault, bool isFocus)
00103 {
00104   button->setDefault( isDefault );
00105   if ( isFocus )
00106       button->setFocus();
00107 }
00108 
00109 void KDialogPrivate::appendButton(KDialog::ButtonCode key, const KGuiItem &item)
00110 {
00111   QDialogButtonBox::ButtonRole role = QDialogButtonBox::InvalidRole;
00112   switch ( key ) {
00113     case KDialog::Help:
00114     case KDialog::Details:
00115       role = QDialogButtonBox::HelpRole;
00116       break;
00117     case KDialog::Default:
00118     case KDialog::Reset:
00119       role = QDialogButtonBox::ResetRole;
00120       break;
00121     case KDialog::Ok:
00122       role = QDialogButtonBox::AcceptRole;
00123       break;
00124     case KDialog::Apply:
00125       role = QDialogButtonBox::ApplyRole;
00126       break;
00127     case KDialog::Try:
00128     case KDialog::Yes:
00129       role = QDialogButtonBox::YesRole;
00130       break;
00131     case KDialog::Close:
00132     case KDialog::Cancel:
00133       role = QDialogButtonBox::RejectRole;
00134       break;
00135     case KDialog::No:
00136       role = QDialogButtonBox::NoRole;
00137       break;
00138     case KDialog::User1:
00139     case KDialog::User2:
00140     case KDialog::User3:
00141       role = QDialogButtonBox::ActionRole;
00142       break;
00143     case KDialog::NoDefault:
00144     default:
00145       role = QDialogButtonBox::InvalidRole;
00146       break;
00147   }
00148 
00149   if ( role == QDialogButtonBox::InvalidRole )
00150     return;
00151 
00152   KPushButton *button = new KPushButton( item );
00153   mButtonBox->addButton( button, role );
00154 
00155   mButtonList.insert( key, button );
00156   mButtonSignalMapper.setMapping( button, key );
00157 
00158     QObject::connect(button, SIGNAL(clicked()),
00159            &mButtonSignalMapper, SLOT( map() ) );
00160 }
00161 
00162 void KDialogPrivate::init(KDialog *q)
00163 {
00164     q_ptr = q;
00165     KWhatsThisManager::init();
00166 
00167     dirty = false;
00168 
00169     q->setButtons(KDialog::Ok | KDialog::Cancel);
00170     q->setDefaultButton(KDialog::Ok);
00171 
00172     q->connect(&mButtonSignalMapper, SIGNAL(mapped(int)), q, SLOT(slotButtonClicked(int)));
00173 
00174     q->setPlainCaption(KGlobal::caption()); // set appropriate initial window title for case it gets not set later
00175 }
00176 
00177 void KDialogPrivate::helpLinkClicked()
00178 {
00179     q_ptr->slotButtonClicked(KDialog::Help);
00180 }
00181 
00182 KDialog::KDialog( QWidget *parent, Qt::WFlags flags )
00183   : QDialog(parent, flags), d_ptr(new KDialogPrivate)
00184 {
00185     d_ptr->init(this);
00186 }
00187 
00188 KDialog::KDialog(KDialogPrivate &dd, QWidget *parent, Qt::WFlags flags)
00189     : QDialog(parent, flags), d_ptr(&dd)
00190 {
00191     d_ptr->init(this);
00192 }
00193 
00194 KDialog::~KDialog()
00195 {
00196     delete d_ptr;
00197 }
00198 
00199 void KDialog::setButtons( ButtonCodes buttonMask )
00200 {
00201     Q_D(KDialog);
00202   if ( d->mButtonBox ) {
00203     d->mButtonList.clear();
00204 
00205     delete d->mButtonBox;
00206     d->mButtonBox = 0;
00207   }
00208 
00209   if ( buttonMask & Cancel )
00210     buttonMask &= ~Close;
00211 
00212   if ( buttonMask & Apply )
00213     buttonMask &= ~Try;
00214 
00215   if ( buttonMask & Details )
00216     buttonMask &= ~Default;
00217 
00218   if ( buttonMask == None ) {
00219     d->setupLayout();
00220     return; // When we want no button box
00221   }
00222 
00223   d->mEscapeButton = (buttonMask & Cancel) ? Cancel : Close;
00224   d->mButtonBox = new QDialogButtonBox( this );
00225 
00226   if ( buttonMask & Help )
00227     d->appendButton( Help, KStandardGuiItem::help() );
00228   if ( buttonMask & Default )
00229     d->appendButton( Default, KStandardGuiItem::defaults() );
00230   if ( buttonMask & Reset )
00231     d->appendButton( Reset, KStandardGuiItem::reset() );
00232   if ( buttonMask & User3 )
00233     d->appendButton( User3, KGuiItem() );
00234   if ( buttonMask & User2 )
00235     d->appendButton( User2, KGuiItem() );
00236   if ( buttonMask & User1 )
00237     d->appendButton( User1, KGuiItem() );
00238   if ( buttonMask & Ok )
00239     d->appendButton( Ok, KStandardGuiItem::ok() );
00240   if ( buttonMask & Apply )
00241     d->appendButton( Apply, KStandardGuiItem::apply() );
00242   if ( buttonMask & Try )
00243     d->appendButton( Try, KGuiItem(i18n( "&Try" )) );
00244   if ( buttonMask & Cancel )
00245     d->appendButton( Cancel, KStandardGuiItem::cancel() );
00246   if ( buttonMask & Close )
00247     d->appendButton( Close, KStandardGuiItem::close() );
00248   if ( buttonMask & Yes )
00249     d->appendButton( Yes, KStandardGuiItem::yes() );
00250   if ( buttonMask & No )
00251     d->appendButton( No, KStandardGuiItem::no() );
00252   if ( buttonMask & Details ) {
00253     d->appendButton( Details, KGuiItem(QString(), "help-about") );
00254     setDetailsWidgetVisible( false );
00255   }
00256 
00257   d->setupLayout();
00258 }
00259 
00260 
00261 void KDialog::setButtonsOrientation( Qt::Orientation orientation )
00262 {
00263     Q_D(KDialog);
00264   if ( d->mButtonOrientation != orientation ) {
00265     d->mButtonOrientation = orientation;
00266 
00267     if ( d->mActionSeparator )
00268       d->mActionSeparator->setOrientation( d->mButtonOrientation );
00269 
00270     if ( d->mButtonOrientation == Qt::Vertical )
00271       enableLinkedHelp( false ); // 2000-06-18 Espen: No support for this yet.
00272   }
00273 }
00274 
00275 void KDialog::setEscapeButton( ButtonCode id )
00276 {
00277     d_func()->mEscapeButton = id;
00278 }
00279 
00280 void KDialog::setDefaultButton( ButtonCode newDefaultButton )
00281 {
00282     Q_D(KDialog);
00283     bool makeDefault = true;
00284     if (newDefaultButton == NoDefault) {
00285         // if we already have a default, let's be sure to reset
00286         // it to not be the default!
00287         newDefaultButton = defaultButton();
00288         makeDefault = false;
00289     }
00290 
00291     KPushButton *b = button(newDefaultButton);
00292     if (b) {
00293         d->setButtonFocus(b, makeDefault, false);
00294     }
00295 }
00296 
00297 KDialog::ButtonCode KDialog::defaultButton() const
00298 {
00299     Q_D(const KDialog);
00300   QHashIterator<int, KPushButton*> it( d->mButtonList );
00301   while ( it.hasNext() ) {
00302     it.next();
00303     if ( it.value()->isDefault() )
00304       return (ButtonCode)it.key();
00305   }
00306 
00307   return NoDefault;
00308 }
00309 
00310 void KDialog::setMainWidget( QWidget *widget )
00311 {
00312     Q_D(KDialog);
00313     if ( d->mMainWidget == widget )
00314         return;
00315     d->mMainWidget = widget;
00316     if (d->mMainWidget && d->mMainWidget->layout()) {
00317         // Avoid double-margin problem
00318         d->mMainWidget->layout()->setMargin(0);
00319     }
00320     d->setupLayout();
00321 }
00322 
00323 QWidget *KDialog::mainWidget()
00324 {
00325     Q_D(KDialog);
00326     if (!d->mMainWidget)
00327         setMainWidget( new QWidget(this) );
00328     return d->mMainWidget;
00329 }
00330 
00331 QSize KDialog::sizeHint() const
00332 {
00333     Q_D(const KDialog);
00334 
00335     if (!d->mMinSize.isEmpty())
00336         return d->mMinSize.expandedTo( minimumSizeHint() ) + d->mIncSize;
00337     else {
00338         if (d->dirty)
00339             const_cast<KDialogPrivate*>(d)->queuedLayoutUpdate();
00340         return QDialog::sizeHint() + d->mIncSize;
00341     }
00342 }
00343 
00344 QSize KDialog::minimumSizeHint() const
00345 {
00346     Q_D(const KDialog);
00347 
00348     if (d->dirty)
00349         const_cast<KDialogPrivate*>(d)->queuedLayoutUpdate();
00350     return QDialog::minimumSizeHint() + d->mIncSize;
00351 }
00352 
00353 //
00354 // Grab QDialogs keypresses if non-modal.
00355 //
00356 void KDialog::keyPressEvent( QKeyEvent *event )
00357 {
00358     Q_D(KDialog);
00359   if ( event->modifiers() == 0 ) {
00360     if ( event->key() == Qt::Key_F1 ) {
00361       KPushButton *button = this->button( Help );
00362 
00363       if ( button ) {
00364         button->animateClick();
00365         event->accept();
00366         return;
00367       }
00368     }
00369 
00370     if ( event->key() == Qt::Key_Escape ) {
00371       KPushButton *button = this->button( d->mEscapeButton );
00372 
00373       if ( button ) {
00374         button->animateClick();
00375         event->accept();
00376         return;
00377       }
00378 
00379     }
00380   } else if ( event->key() == Qt::Key_F1 && event->modifiers() == Qt::ShiftModifier ) {
00381     QWhatsThis::enterWhatsThisMode();
00382     event->accept();
00383     return;
00384   } else if ( event->modifiers() == Qt::ControlModifier &&
00385             ( event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter ) ) {
00386     // accept the dialog when Ctrl-Return is pressed
00387     KPushButton *button = this->button( Ok );
00388 
00389     if ( button ) {
00390       button->animateClick();
00391       event->accept();
00392       return;
00393     }
00394   }
00395 
00396   QDialog::keyPressEvent( event );
00397 }
00398 
00399 int KDialog::marginHint()
00400 {
00401     return QApplication::style()->pixelMetric( QStyle::PM_DefaultChildMargin );
00402 }
00403 
00404 int KDialog::spacingHint()
00405 {
00406     return QApplication::style()->pixelMetric( QStyle::PM_DefaultLayoutSpacing );
00407 }
00408 
00409 int KDialog::groupSpacingHint()
00410 {
00411     return QApplication::fontMetrics().lineSpacing();
00412 }
00413 
00414 QString KDialog::makeStandardCaption( const QString &userCaption,
00415                                       QWidget* window,
00416                                       CaptionFlags flags )
00417 {
00418   Q_UNUSED(window);
00419   QString caption = KGlobal::caption();
00420   QString captionString = userCaption.isEmpty() ? caption : userCaption;
00421 
00422   // If the document is modified, add '[modified]'.
00423   if (flags & ModifiedCaption)
00424       captionString += QString::fromUtf8(" [") + i18n("modified") + QString::fromUtf8("]");
00425 
00426   if ( !userCaption.isEmpty() ) {
00427       // Add the application name if:
00428       // User asked for it, it's not a duplication  and the app name (caption()) is not empty
00429       if ( flags & AppNameCaption &&
00430            !caption.isEmpty() &&
00431            !userCaption.endsWith(caption)  ) {
00432            // TODO: check to see if this is a transient/secondary window before trying to add the app name
00433            //       on platforms that need this
00434           captionString += i18nc("Document/application separator in titlebar", " – ") + caption;
00435       }
00436   }
00437 
00438   return captionString;
00439 }
00440 
00441 void KDialog::setCaption( const QString &_caption )
00442 {
00443   const QString caption = makeStandardCaption( _caption, this );
00444   setPlainCaption( caption );
00445 }
00446 
00447 void KDialog::setCaption( const QString &caption, bool modified )
00448 {
00449     CaptionFlags flags = HIGCompliantCaption;
00450 
00451     if ( modified )
00452     {
00453         flags |= ModifiedCaption;
00454     }
00455 
00456     setPlainCaption( makeStandardCaption(caption, this, flags) );
00457 }
00458 
00459 
00460 void KDialog::setPlainCaption( const QString &caption )
00461 {
00462   QDialog::setWindowTitle( caption );
00463 
00464 #ifdef Q_WS_X11
00465   NETWinInfo info( QX11Info::display(), winId(), QX11Info::appRootWindow(), 0 );
00466   info.setName( caption.toUtf8().constData() );
00467 #endif
00468 }
00469 
00470 void KDialog::resizeLayout( QWidget *widget, int margin, int spacing ) //static
00471 {
00472   if ( widget->layout() )
00473     resizeLayout( widget->layout(), margin, spacing );
00474 
00475   if ( widget->children().count() > 0 ) {
00476     const QList<QObject*> list = widget->children();
00477     foreach ( QObject *object, list ) {
00478       if ( object->isWidgetType() )
00479         resizeLayout( (QWidget*)object, margin, spacing );
00480     }
00481   }
00482 }
00483 
00484 void KDialog::resizeLayout( QLayout *layout, int margin, int spacing ) //static
00485 {
00486   QLayoutItem *child;
00487   int pos = 0;
00488 
00489   while ( (child = layout->itemAt( pos ) ) ) {
00490     if ( child->layout() )
00491       resizeLayout( child->layout(), margin, spacing );
00492 
00493     ++pos;
00494   }
00495 
00496   if ( layout->layout() ) {
00497     layout->layout()->setMargin( margin );
00498     layout->layout()->setSpacing( spacing );
00499   }
00500 }
00501 
00502 static QRect screenRect( QWidget *widget, int screen )
00503 {
00504   QDesktopWidget *desktop = QApplication::desktop();
00505   KConfig gc( "kdeglobals", KConfig::NoGlobals );
00506   KConfigGroup cg(&gc, "Windows" );
00507   if ( desktop->isVirtualDesktop() &&
00508        cg.readEntry( "XineramaEnabled", true ) &&
00509        cg.readEntry( "XineramaPlacementEnabled", true ) ) {
00510 
00511     if ( screen < 0 || screen >= desktop->numScreens() ) {
00512       if ( screen == -1 )
00513         screen = desktop->primaryScreen();
00514       else if ( screen == -3 )
00515         screen = desktop->screenNumber( QCursor::pos() );
00516       else
00517         screen = desktop->screenNumber( widget );
00518     }
00519 
00520     return desktop->availableGeometry( screen );
00521   } else
00522     return desktop->geometry();
00523 }
00524 
00525 void KDialog::centerOnScreen( QWidget *widget, int screen )
00526 {
00527   if ( !widget )
00528     return;
00529 
00530 #ifdef Q_WS_X11
00531   if( !( widget->windowFlags() & Qt::X11BypassWindowManagerHint ) && widget->windowType() != Qt::Popup
00532       && NETRootInfo( QX11Info::display(), NET::Supported ).isSupported( NET::WM2FullPlacement )) {
00533       return; // the WM can handle placement much better
00534   }
00535 #endif
00536 
00537   QRect rect = screenRect( widget, screen );
00538 
00539   widget->move( rect.center().x() - widget->width() / 2,
00540                 rect.center().y() - widget->height() / 2 );
00541 }
00542 
00543 bool KDialog::avoidArea( QWidget *widget, const QRect& area, int screen )
00544 {
00545   if ( !widget )
00546     return false;
00547 
00548   QRect fg = widget->frameGeometry();
00549   if ( !fg.intersects( area ) )
00550     return true; // nothing to do.
00551 
00552   const QRect scr = screenRect( widget, screen );
00553   QRect avoid( area ); // let's add some margin
00554   avoid.translate( -5, -5 );
00555   avoid.setRight( avoid.right() + 10 );
00556   avoid.setBottom( avoid.bottom() + 10 );
00557 
00558   if ( qMax( fg.top(), avoid.top() ) <= qMin( fg.bottom(), avoid.bottom() ) ) {
00559     // We need to move the widget up or down
00560     int spaceAbove = qMax( 0, avoid.top() - scr.top() );
00561     int spaceBelow = qMax( 0, scr.bottom() - avoid.bottom() );
00562     if ( spaceAbove > spaceBelow ) // where's the biggest side?
00563       if ( fg.height() <= spaceAbove ) // big enough?
00564         fg.setY( avoid.top() - fg.height() );
00565       else
00566         return false;
00567     else
00568       if ( fg.height() <= spaceBelow ) // big enough?
00569         fg.setY( avoid.bottom() );
00570       else
00571         return false;
00572   }
00573 
00574   if ( qMax( fg.left(), avoid.left() ) <= qMin( fg.right(), avoid.right() ) ) {
00575     // We need to move the widget left or right
00576     const int spaceLeft = qMax( 0, avoid.left() - scr.left() );
00577     const int spaceRight = qMax( 0, scr.right() - avoid.right() );
00578     if ( spaceLeft > spaceRight ) // where's the biggest side?
00579       if ( fg.width() <= spaceLeft ) // big enough?
00580         fg.setX( avoid.left() - fg.width() );
00581       else
00582         return false;
00583     else
00584       if ( fg.width() <= spaceRight ) // big enough?
00585         fg.setX( avoid.right() );
00586       else
00587         return false;
00588   }
00589 
00590   widget->move( fg.x(), fg.y() );
00591 
00592   return true;
00593 }
00594 
00595 void KDialog::showButtonSeparator( bool state )
00596 {
00597     Q_D(KDialog);
00598   if ( ( d->mActionSeparator != 0 ) == state )
00599     return;
00600   if ( state ) {
00601     if ( d->mActionSeparator )
00602       return;
00603 
00604      d->mActionSeparator = new KSeparator( this );
00605      d->mActionSeparator->setOrientation( d->mButtonOrientation );
00606   } else {
00607     delete d->mActionSeparator;
00608     d->mActionSeparator = 0;
00609   }
00610 
00611   d->setupLayout();
00612 }
00613 
00614 void KDialog::setInitialSize( const QSize &size )
00615 {
00616     d_func()->mMinSize = size;
00617   adjustSize();
00618 }
00619 
00620 void KDialog::incrementInitialSize( const QSize &size )
00621 {
00622     d_func()->mIncSize = size;
00623   adjustSize();
00624 }
00625 
00626 KPushButton *KDialog::button( ButtonCode id ) const
00627 {
00628     Q_D(const KDialog);
00629   return d->mButtonList.value( id, 0 );
00630 }
00631 
00632 void KDialog::enableButton( ButtonCode id, bool state )
00633 {
00634   KPushButton *button = this->button( id );
00635   if ( button )
00636     button->setEnabled( state );
00637 }
00638 
00639 bool KDialog::isButtonEnabled( ButtonCode id ) const
00640 {
00641   KPushButton *button = this->button( id );
00642   if ( button )
00643     return button->isEnabled();
00644 
00645   return false;
00646 }
00647 
00648 void KDialog::enableButtonOk( bool state )
00649 {
00650   enableButton( Ok, state );
00651 }
00652 
00653 void KDialog::enableButtonApply( bool state )
00654 {
00655   enableButton( Apply, state );
00656 }
00657 
00658 void KDialog::enableButtonCancel( bool state )
00659 {
00660   enableButton( Cancel, state );
00661 }
00662 
00663 void KDialog::showButton( ButtonCode id, bool state )
00664 {
00665   KPushButton *button = this->button( id );
00666   if ( button )
00667     state ? button->show() : button->hide();
00668 }
00669 
00670 void KDialog::setButtonGuiItem( ButtonCode id, const KGuiItem &item )
00671 {
00672   KPushButton *button = this->button( id );
00673   if ( !button )
00674     return;
00675 
00676   button->setGuiItem( item );
00677 }
00678 
00679 void KDialog::setButtonMenu( ButtonCode id, QMenu *menu, ButtonPopupMode popupmode)
00680 {
00681   KPushButton *button = this->button( id );
00682   if ( button ) {
00683     if (popupmode==InstantPopup)
00684       button->setMenu( menu );
00685     else
00686       button->setDelayedMenu(menu);
00687   }
00688 }
00689 
00690 void KDialog::setButtonText( ButtonCode id, const QString &text )
00691 {
00692     Q_D(KDialog);
00693   if ( !d->mSettingDetails && (id == Details) ) {
00694     d->mDetailsButtonText = text;
00695     setDetailsWidgetVisible( d->mDetailsVisible );
00696     return;
00697   }
00698 
00699   KPushButton *button = this->button( id );
00700   if ( button )
00701     button->setText( text );
00702 }
00703 
00704 QString KDialog::buttonText( ButtonCode id ) const
00705 {
00706   KPushButton *button = this->button( id );
00707   if ( button )
00708     return button->text();
00709   else
00710     return QString();
00711 }
00712 
00713 void KDialog::setButtonIcon( ButtonCode id, const KIcon &icon )
00714 {
00715   KPushButton *button = this->button( id );
00716   if ( button )
00717     button->setIcon( icon );
00718 }
00719 
00720 KIcon KDialog::buttonIcon( ButtonCode id ) const
00721 {
00722   KPushButton *button = this->button( id );
00723   if ( button )
00724     return KIcon(button->icon());
00725   else
00726     return KIcon();
00727 }
00728 
00729 void KDialog::setButtonToolTip( ButtonCode id, const QString &text )
00730 {
00731   KPushButton *button = this->button( id );
00732   if ( button ) {
00733     if ( text.isEmpty() )
00734       button->setToolTip( QString() );
00735     else
00736       button->setToolTip( text );
00737   }
00738 }
00739 
00740 QString KDialog::buttonToolTip( ButtonCode id ) const
00741 {
00742   KPushButton *button = this->button( id );
00743   if ( button )
00744     return button->toolTip();
00745   else
00746     return QString();
00747 }
00748 
00749 void KDialog::setButtonWhatsThis( ButtonCode id, const QString &text )
00750 {
00751   KPushButton *button = this->button( id );
00752   if ( button ) {
00753     if ( text.isEmpty() )
00754       button->setWhatsThis( QString() );
00755     else
00756       button->setWhatsThis( text );
00757   }
00758 }
00759 
00760 QString KDialog::buttonWhatsThis( ButtonCode id ) const
00761 {
00762   KPushButton *button = this->button( id );
00763   if ( button )
00764     return button->whatsThis();
00765   else
00766     return QString();
00767 }
00768 
00769 void KDialog::setButtonFocus( ButtonCode id )
00770 {
00771   KPushButton *button = this->button( id );
00772   if ( button )
00773         d_func()->setButtonFocus(button, button->isDefault(), true);
00774 }
00775 
00776 void KDialog::setDetailsWidget( QWidget *detailsWidget )
00777 {
00778     Q_D(KDialog);
00779   if ( d->mDetailsWidget == detailsWidget )
00780     return;
00781   delete d->mDetailsWidget;
00782   d->mDetailsWidget = detailsWidget;
00783 
00784   if ( d->mDetailsWidget->parentWidget() != this )
00785     d->mDetailsWidget->setParent( this );
00786 
00787   d->mDetailsWidget->hide();
00788   d->setupLayout();
00789 
00790   if ( !d->mSettingDetails )
00791     setDetailsWidgetVisible( d->mDetailsVisible );
00792 }
00793 
00794 bool KDialog::isDetailsWidgetVisible() const
00795 {
00796     return d_func()->mDetailsVisible;
00797 }
00798 
00799 void KDialog::setDetailsWidgetVisible( bool visible )
00800 {
00801     Q_D(KDialog);
00802   if ( d->mDetailsButtonText.isEmpty() )
00803     d->mDetailsButtonText = i18n( "&Details" );
00804 
00805   d->mSettingDetails = true;
00806   d->mDetailsVisible = visible;
00807   if ( d->mDetailsVisible ) {
00808     emit aboutToShowDetails();
00809     setButtonText( Details, d->mDetailsButtonText + " <<" );
00810     if ( d->mDetailsWidget ) {
00811       if ( layout() )
00812         layout()->setEnabled( false );
00813 
00814       d->mDetailsWidget->show();
00815 
00816       adjustSize();
00817 
00818       if ( layout() ) {
00819         layout()->activate();
00820         layout()->setEnabled( true );
00821       }
00822     }
00823   } else {
00824     setButtonText( Details, d->mDetailsButtonText + " >>" );
00825     if ( d->mDetailsWidget )
00826       d->mDetailsWidget->hide();
00827 
00828     if ( layout() )
00829       layout()->activate();
00830 
00831     adjustSize();
00832   }
00833 
00834   d->mSettingDetails = false;
00835 }
00836 
00837 void KDialog::delayedDestruct()
00838 {
00839   if ( isVisible() )
00840     hide();
00841 
00842   deleteLater();
00843 }
00844 
00845 
00846 void KDialog::slotButtonClicked( int button )
00847 {
00848     Q_D(KDialog);
00849   emit buttonClicked( static_cast<KDialog::ButtonCode>(button) );
00850 
00851   switch( button ) {
00852     case Ok:
00853       emit okClicked();
00854       accept();
00855       break;
00856     case Apply:
00857         emit applyClicked();
00858       break;
00859     case Try:
00860       emit tryClicked();
00861       break;
00862     case User3:
00863       emit user3Clicked();
00864       break;
00865     case User2:
00866         emit user2Clicked();
00867       break;
00868     case User1:
00869         emit user1Clicked();
00870         break;
00871     case Yes:
00872       emit yesClicked();
00873       done( Yes );
00874       break;
00875     case No:
00876         emit noClicked();
00877       done( No );
00878       break;
00879     case Cancel:
00880       emit cancelClicked();
00881       reject();
00882       break;
00883     case Close:
00884       emit closeClicked();
00885       close();
00886       break;
00887     case Help:
00888       emit helpClicked();
00889       if ( !d->mAnchor.isEmpty() || !d->mHelpApp.isEmpty() )
00890         KToolInvocation::invokeHelp( d->mAnchor, d->mHelpApp );
00891       break;
00892     case Default:
00893       emit defaultClicked();
00894       break;
00895     case Reset:
00896       emit resetClicked();
00897       break;
00898     case Details:
00899       setDetailsWidgetVisible( !d->mDetailsVisible );
00900       break;
00901   }
00902 }
00903 
00904 void KDialog::enableLinkedHelp( bool state )
00905 {
00906     Q_D(KDialog);
00907     if ( ( d->mUrlHelp != 0 ) == state )
00908         return;
00909     if ( state ) {
00910         if ( d->mUrlHelp )
00911             return;
00912 
00913         d->mUrlHelp = new KUrlLabel( this );
00914         d->mUrlHelp->setText( helpLinkText() );
00915         d->mUrlHelp->setFloatEnabled( true );
00916         d->mUrlHelp->setUnderline( true );
00917         d->mUrlHelp->setMinimumHeight( fontMetrics().height() + marginHint() );
00918         connect( d->mUrlHelp, SIGNAL(leftClickedUrl()), SLOT(helpLinkClicked()) );
00919 
00920         d->mUrlHelp->show();
00921     } else {
00922         delete d->mUrlHelp;
00923         d->mUrlHelp = 0;
00924     }
00925 
00926     d->setupLayout();
00927 }
00928 
00929 
00930 void KDialog::setHelp( const QString &anchor, const QString &appname )
00931 {
00932     Q_D(KDialog);
00933   d->mAnchor  = anchor;
00934   d->mHelpApp = appname;
00935 }
00936 
00937 
00938 void KDialog::setHelpLinkText( const QString &text )
00939 {
00940     Q_D(KDialog);
00941   d->mHelpLinkText = text;
00942   if ( d->mUrlHelp )
00943     d->mUrlHelp->setText( helpLinkText() );
00944 }
00945 
00946 QString KDialog::helpLinkText() const
00947 {
00948     Q_D(const KDialog);
00949   return ( d->mHelpLinkText.isEmpty() ? i18n( "Get help..." ) : d->mHelpLinkText );
00950 }
00951 
00952 void KDialog::updateGeometry()
00953 {
00954 }
00955 
00956 void KDialog::hideEvent( QHideEvent *event )
00957 {
00958   emit hidden();
00959 
00960   if ( !event->spontaneous() )
00961     emit finished();
00962 }
00963 
00964 void KDialog::closeEvent( QCloseEvent *event )
00965 {
00966     Q_D(KDialog);
00967   KPushButton *button = this->button( d->mEscapeButton );
00968   if ( button && !isHidden() )
00969     button->animateClick();
00970   else
00971     QDialog::closeEvent( event );
00972 }
00973 
00974 void KDialog::restoreDialogSize( const KConfigGroup& cfg )
00975 {
00976   int width, height;
00977   int scnum = QApplication::desktop()->screenNumber( parentWidget() );
00978   QRect desk = QApplication::desktop()->screenGeometry( scnum );
00979 
00980   width = sizeHint().width();
00981   height = sizeHint().height();
00982 
00983   width = cfg.readEntry( QString::fromLatin1( "Width %1" ).arg( desk.width() ), width );
00984   height = cfg.readEntry( QString::fromLatin1( "Height %1" ).arg( desk.height() ), height );
00985 
00986   resize( width, height );
00987 }
00988 
00989 void KDialog::saveDialogSize( KConfigGroup& config, KConfigGroup::WriteConfigFlags options ) const
00990 {
00991    int scnum = QApplication::desktop()->screenNumber( parentWidget() );
00992    QRect desk = QApplication::desktop()->screenGeometry( scnum );
00993 
00994    const QSize sizeToSave = size();
00995 
00996    config.writeEntry( QString::fromLatin1("Width %1").arg( desk.width() ), sizeToSave.width(), options );
00997    config.writeEntry( QString::fromLatin1("Height %1").arg( desk.height() ), sizeToSave.height(), options );
00998 }
00999 
01000 
01001 class KDialogQueue::Private
01002 {
01003   public:
01004     Private(KDialogQueue *q): q(q) {}
01005 
01006     void slotShowQueuedDialog();
01007 
01008     KDialogQueue *q;
01009     QList< QPointer<QDialog> > queue;
01010     bool busy;
01011 
01012 };
01013 
01014 KDialogQueue* KDialogQueue::self()
01015 {
01016   K_GLOBAL_STATIC(KDialogQueue, _self)
01017   return _self;
01018 }
01019 
01020 KDialogQueue::KDialogQueue()
01021   : d( new Private(this) )
01022 {
01023   d->busy = false;
01024 }
01025 
01026 KDialogQueue::~KDialogQueue()
01027 {
01028   delete d;
01029 }
01030 
01031 // static
01032 void KDialogQueue::queueDialog( QDialog *dialog )
01033 {
01034   KDialogQueue *_this = self();
01035   _this->d->queue.append( dialog );
01036 
01037   QTimer::singleShot( 0, _this, SLOT( slotShowQueuedDialog() ) );
01038 }
01039 
01040 void KDialogQueue::Private::slotShowQueuedDialog()
01041 {
01042   if ( busy )
01043     return;
01044 
01045   QDialog *dialog;
01046   do {
01047     if ( queue.isEmpty() )
01048       return;
01049     dialog = queue.first();
01050     queue.pop_front();
01051   } while( !dialog );
01052 
01053   busy = true;
01054   dialog->exec();
01055   busy = false;
01056   delete dialog;
01057 
01058   if ( !queue.isEmpty() )
01059     QTimer::singleShot( 20, q, SLOT( slotShowQueuedDialog() ) );
01060 }
01061 
01062 #include "kdialog.moc"
01063 #include "kdialogqueue_p.moc"

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal