KDEUI
kmessagebox.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 1999 Waldo Bastian (bastian@kde.org) 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; version 2 00007 of the License. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include "kmessagebox.h" 00021 00022 #include <QtCore/QPointer> 00023 #include <QtGui/QCheckBox> 00024 #include <QtGui/QGroupBox> 00025 #include <QtGui/QLabel> 00026 #include <QtGui/QLayout> 00027 #include <QtGui/QListWidget> 00028 #include <QtGui/QScrollArea> 00029 #include <QtGui/QScrollBar> 00030 #include <QtGui/QTextDocumentFragment> 00031 00032 #include <kapplication.h> 00033 #include <kconfig.h> 00034 #include <kdialog.h> 00035 #include <kdialogqueue_p.h> 00036 #include <kglobalsettings.h> 00037 #include <klocale.h> 00038 #include <knotification.h> 00039 #include <kiconloader.h> 00040 #include <kconfiggroup.h> 00041 #include <ktextedit.h> 00042 #include <ksqueezedtextlabel.h> 00043 #include <kwindowsystem.h> 00044 00045 // Some i18n filters, that standard button texts are piped through 00046 // (the new KGuiItem object with filtered text is created from the old one). 00047 00048 // i18n: Filter for the Yes-button text in standard message dialogs, 00049 // after the message caption/text have been translated. 00050 #define I18N_FILTER_BUTTON_YES(src, dst) \ 00051 KGuiItem dst(src); \ 00052 dst.setText( i18nc( "@action:button filter-yes", "%1", src.text() ) ); 00053 00054 // i18n: Filter for the No-button text in standard message dialogs, 00055 // after the message caption/text have been translated. 00056 #define I18N_FILTER_BUTTON_NO(src, dst) \ 00057 KGuiItem dst(src); \ 00058 dst.setText( i18nc( "@action:button filter-no", "%1", src.text() ) ); 00059 00060 // i18n: Filter for the Continue-button text in standard message dialogs, 00061 // after the message caption/text have been translated. 00062 #define I18N_FILTER_BUTTON_CONTINUE(src, dst) \ 00063 KGuiItem dst(src); \ 00064 dst.setText( i18nc( "@action:button filter-continue", "%1", src.text() ) ); 00065 00066 // i18n: Filter for the Cancel-button text in standard message dialogs, 00067 // after the message caption/text have been translated. 00068 #define I18N_FILTER_BUTTON_CANCEL(src, dst) \ 00069 KGuiItem dst(src); \ 00070 dst.setText( i18nc( "@action:button filter-cancel", "%1", src.text() ) ); 00071 00072 // i18n: Called after the button texts in standard message dialogs 00073 // have been filtered by the messages above. Not visible to user. 00074 #define I18N_POST_BUTTON_FILTER \ 00075 i18nc( "@action:button post-filter", "." ); 00076 00077 static bool KMessageBox_queue = false; 00078 KConfig* KMessageBox_againConfig = 0; 00079 00080 00081 static QIcon themedMessageBoxIcon(QMessageBox::Icon icon) 00082 { 00083 QString icon_name; 00084 00085 switch (icon) { 00086 case QMessageBox::NoIcon: 00087 return QIcon(); 00088 break; 00089 case QMessageBox::Information: 00090 icon_name = "dialog-information"; 00091 break; 00092 case QMessageBox::Warning: 00093 icon_name = "dialog-warning"; 00094 break; 00095 case QMessageBox::Critical: 00096 icon_name = "dialog-error"; 00097 break; 00098 default: 00099 break; 00100 } 00101 00102 QIcon ret = KIconLoader::global()->loadIcon(icon_name, KIconLoader::NoGroup, KIconLoader::SizeHuge, KIconLoader::DefaultState, QStringList(), 0, true); 00103 00104 if (ret.isNull()) { 00105 return QMessageBox::standardIcon(icon); 00106 } else { 00107 return ret; 00108 } 00109 } 00110 00111 static void sendNotification( QString message, //krazy:exclude=passbyvalue 00112 const QStringList& strlist, 00113 QMessageBox::Icon icon, 00114 WId parent_id ) 00115 { 00116 // create the message for KNotify 00117 QString messageType; 00118 switch (icon) { 00119 case QMessageBox::Warning: 00120 messageType = "messageWarning"; 00121 break; 00122 case QMessageBox::Critical: 00123 messageType = "messageCritical"; 00124 break; 00125 case QMessageBox::Question: 00126 messageType = "messageQuestion"; 00127 break; 00128 default: 00129 messageType = "messageInformation"; 00130 break; 00131 } 00132 00133 if ( !strlist.isEmpty() ) { 00134 for ( QStringList::ConstIterator it = strlist.begin(); it != strlist.end(); ++it ) { 00135 message += '\n' + *it; 00136 } 00137 } 00138 00139 if ( !message.isEmpty() ) { 00140 KNotification::event( messageType, message, QPixmap(), QWidget::find( parent_id ), 00141 KNotification::DefaultEvent | KNotification::CloseOnTimeout ); 00142 } 00143 } 00144 00145 00146 int KMessageBox::createKMessageBox(KDialog *dialog, QMessageBox::Icon icon, 00147 const QString &text, const QStringList &strlist, 00148 const QString &ask, bool *checkboxReturn, 00149 Options options, const QString &details) 00150 { 00151 return createKMessageBox(dialog, themedMessageBoxIcon(icon), text, strlist, 00152 ask, checkboxReturn, options, details, icon); 00153 } 00154 00155 00156 int KMessageBox::createKMessageBox(KDialog *dialog, const QIcon &icon, 00157 const QString &text, const QStringList &strlist, 00158 const QString &ask, bool *checkboxReturn, Options options, 00159 const QString &details, QMessageBox::Icon notifyType) 00160 { 00161 QWidget *mainWidget = new QWidget(dialog); 00162 QVBoxLayout *mainLayout = new QVBoxLayout(mainWidget); 00163 mainLayout->setSpacing(KDialog::spacingHint() * 2); // provide extra spacing 00164 mainLayout->setMargin(0); 00165 00166 QHBoxLayout *hLayout = new QHBoxLayout(); 00167 hLayout->setMargin(0); 00168 hLayout->setSpacing(-1); // use default spacing 00169 mainLayout->addLayout(hLayout,5); 00170 00171 QLabel *iconLabel = new QLabel(mainWidget); 00172 00173 if (!icon.isNull()) { 00174 QStyleOption option; 00175 option.initFrom(mainWidget); 00176 iconLabel->setPixmap(icon.pixmap(mainWidget->style()->pixelMetric(QStyle::PM_MessageBoxIconSize, &option, mainWidget))); 00177 } 00178 00179 QVBoxLayout *iconLayout = new QVBoxLayout(); 00180 iconLayout->addStretch(1); 00181 iconLayout->addWidget(iconLabel); 00182 iconLayout->addStretch(5); 00183 00184 hLayout->addLayout(iconLayout,0); 00185 hLayout->addSpacing(KDialog::spacingHint()); 00186 00187 QLabel *messageLabel = new QLabel(text, mainWidget); 00188 messageLabel->setOpenExternalLinks(options & KMessageBox::AllowLink); 00189 Qt::TextInteractionFlags flags = Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard; 00190 if (options & KMessageBox::AllowLink) { 00191 flags |= Qt::LinksAccessibleByMouse | Qt::LinksAccessibleByKeyboard; 00192 } 00193 messageLabel->setTextInteractionFlags(flags); 00194 00195 QRect desktop = KGlobalSettings::desktopGeometry(dialog); 00196 bool usingSqueezedTextLabel = false; 00197 if (messageLabel->sizeHint().width() > desktop.width() * 0.5) { 00198 // enable automatic wrapping of messages which are longer than 50% of screen width 00199 messageLabel->setWordWrap(true); 00200 // display a text widget with scrollbar if still too wide 00201 usingSqueezedTextLabel = messageLabel->sizeHint().width() > desktop.width() * 0.85; 00202 if (usingSqueezedTextLabel) 00203 { 00204 delete messageLabel; 00205 messageLabel = new KSqueezedTextLabel(text, mainWidget); 00206 messageLabel->setOpenExternalLinks(options & KMessageBox::AllowLink); 00207 messageLabel->setTextInteractionFlags(flags); 00208 } 00209 } 00210 00211 QPalette messagePal(messageLabel->palette()); 00212 messagePal.setColor(QPalette::Window, Qt::transparent); 00213 messageLabel->setPalette(messagePal); 00214 00215 00216 bool usingScrollArea=desktop.height() / 3 < messageLabel->sizeHint().height(); 00217 if (usingScrollArea) 00218 { 00219 QScrollArea* messageScrollArea = new QScrollArea(mainWidget); 00220 messageScrollArea->setWidget(messageLabel); 00221 messageScrollArea->setFrameShape(QFrame::NoFrame); 00222 messageScrollArea->setWidgetResizable(true); 00223 QPalette scrollPal(messageScrollArea->palette()); 00224 scrollPal.setColor(QPalette::Window, Qt::transparent); 00225 messageScrollArea->viewport()->setPalette(scrollPal); 00226 hLayout->addWidget(messageScrollArea,5); 00227 } 00228 else 00229 hLayout->addWidget(messageLabel,5); 00230 00231 00232 const bool usingListWidget=!strlist.isEmpty(); 00233 if (usingListWidget) { 00234 // enable automatic wrapping since the listwidget has already a good initial width 00235 messageLabel->setWordWrap(true); 00236 QListWidget *listWidget = new QListWidget(mainWidget); 00237 listWidget->addItems(strlist); 00238 00239 QStyleOptionViewItem styleOption; 00240 styleOption.initFrom(listWidget); 00241 QFontMetrics fm(styleOption.font); 00242 int w = listWidget->width(); 00243 Q_FOREACH(const QString &str, strlist) { 00244 w = qMax(w, fm.width(str)); 00245 } 00246 const int borderWidth = listWidget->width() - listWidget->viewport()->width() + listWidget->verticalScrollBar()->height(); 00247 w += borderWidth; 00248 if (w > desktop.width() * 0.85) { // limit listWidget size to 85% of screen width 00249 w = qRound(desktop.width() * 0.85); 00250 } 00251 listWidget->setMinimumWidth(w); 00252 00253 mainLayout->addWidget(listWidget,usingScrollArea?10:50); 00254 listWidget->setSelectionMode(QListWidget::NoSelection); 00255 messageLabel->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Minimum); 00256 } 00257 else if (!usingScrollArea) 00258 mainLayout->addStretch(15); 00259 00260 00261 QPointer<QCheckBox> checkbox = 0; 00262 if (!ask.isEmpty()) { 00263 checkbox = new QCheckBox(ask, mainWidget); 00264 mainLayout->addWidget(checkbox); 00265 if (checkboxReturn) { 00266 checkbox->setChecked(*checkboxReturn); 00267 } 00268 } 00269 00270 if (!details.isEmpty()) { 00271 QGroupBox *detailsGroup = new QGroupBox(i18n("Details")); 00272 QVBoxLayout *detailsLayout = new QVBoxLayout(detailsGroup); 00273 if (details.length() < 512) { 00274 QLabel *detailsLabel = new QLabel(details); 00275 detailsLabel->setOpenExternalLinks(options & KMessageBox::AllowLink); 00276 Qt::TextInteractionFlags flags = Qt::TextSelectableByMouse | Qt::TextSelectableByKeyboard; 00277 if ( options & KMessageBox::AllowLink ) 00278 flags |= Qt::LinksAccessibleByMouse | Qt::LinksAccessibleByKeyboard;; 00279 detailsLabel->setTextInteractionFlags(flags); 00280 detailsLabel->setWordWrap(true); 00281 detailsLayout->addWidget(detailsLabel,50); 00282 } else { 00283 KTextEdit *detailTextEdit = new KTextEdit(details); 00284 detailTextEdit->setReadOnly(true); 00285 detailTextEdit->setMinimumHeight(detailTextEdit->fontMetrics().lineSpacing() * 11); 00286 detailsLayout->addWidget(detailTextEdit,50); 00287 } 00288 if (!usingListWidget) 00289 mainLayout->setStretchFactor(hLayout,10); 00290 dialog->setDetailsWidget(detailsGroup); 00291 } 00292 00293 dialog->setMainWidget(mainWidget); 00294 if (!usingListWidget && !usingScrollArea && !usingSqueezedTextLabel && details.isEmpty()) 00295 dialog->setFixedSize(dialog->sizeHint() + QSize( 10, 10 )); 00296 else if (!details.isEmpty() && dialog->minimumHeight()<iconLabel->sizeHint().height()*2)//strange bug... 00297 { 00298 if (!usingScrollArea) 00299 dialog->setMinimumSize(300,qMax(150,qMax(iconLabel->sizeHint().height(),messageLabel->sizeHint().height()))); 00300 else 00301 dialog->setMinimumSize(300,qMax(150,iconLabel->sizeHint().height())); 00302 } 00303 00304 00305 if ((options & KMessageBox::Dangerous)) { 00306 if (dialog->isButtonEnabled(KDialog::Cancel)) 00307 dialog->setDefaultButton(KDialog::Cancel); 00308 else if (dialog->isButtonEnabled(KDialog::No)) 00309 dialog->setDefaultButton(KDialog::No); 00310 } 00311 00312 KDialog::ButtonCode defaultCode = dialog->defaultButton(); 00313 if (defaultCode != KDialog::NoDefault) { 00314 dialog->setButtonFocus(defaultCode); 00315 } 00316 00317 #ifndef Q_WS_WIN // FIXME problems with KNotify on Windows 00318 if ((options & KMessageBox::Notify)) { 00319 sendNotification(text, strlist, notifyType, dialog->topLevelWidget()->winId()); 00320 } 00321 #endif 00322 00323 if (KMessageBox_queue) { 00324 KDialogQueue::queueDialog(dialog); 00325 return KMessageBox::Cancel; // We have to return something. 00326 } 00327 00328 if ((options & KMessageBox::NoExec)) { 00329 return KMessageBox::Cancel; // We have to return something. 00330 } 00331 00332 // We use a QPointer because the dialog may get deleted 00333 // during exec() if the parent of the dialog gets deleted. 00334 // In that case the QPointer will reset to 0. 00335 QPointer<KDialog> guardedDialog = dialog; 00336 00337 const int result = guardedDialog->exec(); 00338 if (checkbox && checkboxReturn) { 00339 *checkboxReturn = checkbox->isChecked(); 00340 } 00341 00342 delete (KDialog *) guardedDialog; 00343 return result; 00344 } 00345 00346 int KMessageBox::questionYesNo(QWidget *parent, const QString &text, 00347 const QString &caption, 00348 const KGuiItem &buttonYes, 00349 const KGuiItem &buttonNo, 00350 const QString &dontAskAgainName, 00351 Options options) 00352 { 00353 return questionYesNoList(parent, text, QStringList(), caption, 00354 buttonYes, buttonNo, dontAskAgainName, options); 00355 } 00356 00357 int KMessageBox::questionYesNoWId(WId parent_id, const QString &text, 00358 const QString &caption, 00359 const KGuiItem &buttonYes, 00360 const KGuiItem &buttonNo, 00361 const QString &dontAskAgainName, 00362 Options options) 00363 { 00364 return questionYesNoListWId(parent_id, text, QStringList(), caption, 00365 buttonYes, buttonNo, dontAskAgainName, options); 00366 } 00367 00368 bool KMessageBox::shouldBeShownYesNo(const QString &dontShowAgainName, 00369 ButtonCode &result) 00370 { 00371 if ( dontShowAgainName.isEmpty() ) { 00372 return true; 00373 } 00374 KConfigGroup cg( KMessageBox_againConfig ? KMessageBox_againConfig : KGlobal::config().data(), "Notification Messages" ); 00375 const QString dontAsk = cg.readEntry(dontShowAgainName, QString()).toLower(); 00376 if (dontAsk == "yes" || dontAsk == "true") { 00377 result = Yes; 00378 return false; 00379 } 00380 if (dontAsk == "no" || dontAsk == "false") { 00381 result = No; 00382 return false; 00383 } 00384 return true; 00385 } 00386 00387 bool KMessageBox::shouldBeShownContinue(const QString &dontShowAgainName) 00388 { 00389 if ( dontShowAgainName.isEmpty() ) { 00390 return true; 00391 } 00392 KConfigGroup cg( KMessageBox_againConfig ? KMessageBox_againConfig : KGlobal::config().data(), "Notification Messages" ); 00393 return cg.readEntry(dontShowAgainName, true); 00394 } 00395 00396 void KMessageBox::saveDontShowAgainYesNo(const QString &dontShowAgainName, 00397 ButtonCode result) 00398 { 00399 if ( dontShowAgainName.isEmpty() ) { 00400 return; 00401 } 00402 KConfigGroup::WriteConfigFlags flags = KConfig::Persistent; 00403 if (dontShowAgainName[0] == ':') { 00404 flags |= KConfigGroup::Global; 00405 } 00406 KConfigGroup cg( KMessageBox_againConfig? KMessageBox_againConfig : KGlobal::config().data(), "Notification Messages" ); 00407 cg.writeEntry( dontShowAgainName, result==Yes, flags ); 00408 cg.sync(); 00409 } 00410 00411 void KMessageBox::saveDontShowAgainContinue(const QString &dontShowAgainName) 00412 { 00413 if ( dontShowAgainName.isEmpty() ) { 00414 return; 00415 } 00416 KConfigGroup::WriteConfigFlags flags = KConfigGroup::Persistent; 00417 if (dontShowAgainName[0] == ':') { 00418 flags |= KConfigGroup::Global; 00419 } 00420 KConfigGroup cg( KMessageBox_againConfig? KMessageBox_againConfig: KGlobal::config().data(), "Notification Messages" ); 00421 cg.writeEntry( dontShowAgainName, false, flags ); 00422 cg.sync(); 00423 } 00424 00425 void KMessageBox::setDontShowAskAgainConfig(KConfig* cfg) 00426 { 00427 KMessageBox_againConfig = cfg; 00428 } 00429 00430 int KMessageBox::questionYesNoList(QWidget *parent, const QString &text, 00431 const QStringList &strlist, 00432 const QString &caption, 00433 const KGuiItem &buttonYes, 00434 const KGuiItem &buttonNo, 00435 const QString &dontAskAgainName, 00436 Options options) 00437 { // in order to avoid code duplication, convert to WId, it will be converted back 00438 return questionYesNoListWId( parent ? parent->effectiveWinId() : 0, text, strlist, 00439 caption, buttonYes, buttonNo, dontAskAgainName, options ); 00440 } 00441 00442 int KMessageBox::questionYesNoListWId(WId parent_id, const QString &text, 00443 const QStringList &strlist, 00444 const QString &caption, 00445 const KGuiItem &buttonYes_, 00446 const KGuiItem &buttonNo_, 00447 const QString &dontAskAgainName, 00448 Options options) 00449 { 00450 ButtonCode res; 00451 if ( !shouldBeShownYesNo(dontAskAgainName, res) ) { 00452 return res; 00453 } 00454 00455 I18N_FILTER_BUTTON_YES(buttonYes_, buttonYes) 00456 I18N_FILTER_BUTTON_NO(buttonNo_, buttonNo) 00457 I18N_POST_BUTTON_FILTER 00458 00459 QWidget* parent = QWidget::find( parent_id ); 00460 KDialog *dialog = new KDialog(parent, Qt::Dialog); 00461 dialog->setCaption( caption.isEmpty() ? i18n("Question") : caption ); 00462 dialog->setButtons( KDialog::Yes | KDialog::No ); 00463 dialog->setObjectName( "questionYesNo" ); 00464 dialog->setModal( true ); 00465 dialog->setButtonGuiItem( KDialog::Yes, buttonYes ); 00466 dialog->setButtonGuiItem( KDialog::No, buttonNo ); 00467 dialog->setDefaultButton( KDialog::Yes ); 00468 dialog->setEscapeButton( KDialog::No ); 00469 if ( options & PlainCaption ) { 00470 dialog->setPlainCaption( caption ); 00471 } 00472 if ( parent == NULL && parent_id ) { 00473 KWindowSystem::setMainWindow( dialog, parent_id ); 00474 } 00475 00476 bool checkboxResult = false; 00477 const int result = createKMessageBox(dialog, QMessageBox::Information, text, strlist, 00478 dontAskAgainName.isEmpty() ? QString() : i18n("Do not ask again"), 00479 &checkboxResult, options); 00480 res = (result==KDialog::Yes ? Yes : No); 00481 00482 if (checkboxResult) { 00483 saveDontShowAgainYesNo(dontAskAgainName, res); 00484 } 00485 return res; 00486 } 00487 00488 int KMessageBox::questionYesNoCancel(QWidget *parent, 00489 const QString &text, 00490 const QString &caption, 00491 const KGuiItem &buttonYes, 00492 const KGuiItem &buttonNo, 00493 const KGuiItem &buttonCancel, 00494 const QString &dontAskAgainName, 00495 Options options) 00496 { 00497 return questionYesNoCancelWId( parent ? parent->effectiveWinId() : 0, text, caption, buttonYes, buttonNo, buttonCancel, 00498 dontAskAgainName, options ); 00499 } 00500 00501 int KMessageBox::questionYesNoCancelWId(WId parent_id, 00502 const QString &text, 00503 const QString &caption, 00504 const KGuiItem &buttonYes_, 00505 const KGuiItem &buttonNo_, 00506 const KGuiItem &buttonCancel_, 00507 const QString &dontAskAgainName, 00508 Options options) 00509 { 00510 ButtonCode res; 00511 if ( !shouldBeShownYesNo(dontAskAgainName, res) ) { 00512 return res; 00513 } 00514 00515 I18N_FILTER_BUTTON_YES(buttonYes_, buttonYes) 00516 I18N_FILTER_BUTTON_NO(buttonNo_, buttonNo) 00517 I18N_FILTER_BUTTON_CANCEL(buttonCancel_, buttonCancel) 00518 I18N_POST_BUTTON_FILTER 00519 00520 QWidget* parent = QWidget::find( parent_id ); 00521 KDialog *dialog= new KDialog(parent, Qt::Dialog); 00522 dialog->setCaption( caption.isEmpty() ? i18n("Question") : caption ); 00523 dialog->setButtons( KDialog::Yes | KDialog::No | KDialog::Cancel ); 00524 dialog->setObjectName( "questionYesNoCancel" ); 00525 dialog->setModal( true ); 00526 dialog->setButtonGuiItem( KDialog::Yes, buttonYes ); 00527 dialog->setButtonGuiItem( KDialog::No, buttonNo ); 00528 dialog->setButtonGuiItem( KDialog::Cancel, buttonCancel ); 00529 dialog->setDefaultButton( KDialog::Yes ); 00530 if ( options & PlainCaption ) { 00531 dialog->setPlainCaption( caption ); 00532 } 00533 if ( parent == NULL && parent_id ) { 00534 KWindowSystem::setMainWindow( dialog, parent_id ); 00535 } 00536 00537 bool checkboxResult = false; 00538 const int result = createKMessageBox(dialog, QMessageBox::Information, 00539 text, QStringList(), 00540 dontAskAgainName.isEmpty() ? QString() : i18n("Do not ask again"), 00541 &checkboxResult, options); 00542 00543 if ( result == KDialog::Yes ) { 00544 res = Yes; 00545 } else if ( result == KDialog::No ) { 00546 res = No; 00547 } else { 00548 return Cancel; 00549 } 00550 00551 if (checkboxResult) { 00552 saveDontShowAgainYesNo(dontAskAgainName, res); 00553 } 00554 return res; 00555 } 00556 00557 int KMessageBox::warningYesNo(QWidget *parent, const QString &text, 00558 const QString &caption, 00559 const KGuiItem &buttonYes, 00560 const KGuiItem &buttonNo, 00561 const QString &dontAskAgainName, 00562 Options options) 00563 { 00564 return warningYesNoList(parent, text, QStringList(), caption, 00565 buttonYes, buttonNo, dontAskAgainName, options); 00566 } 00567 00568 int KMessageBox::warningYesNoWId(WId parent_id, const QString &text, 00569 const QString &caption, 00570 const KGuiItem &buttonYes, 00571 const KGuiItem &buttonNo, 00572 const QString &dontAskAgainName, 00573 Options options) 00574 { 00575 return warningYesNoListWId(parent_id, text, QStringList(), caption, 00576 buttonYes, buttonNo, dontAskAgainName, options); 00577 } 00578 00579 int KMessageBox::warningYesNoList(QWidget *parent, const QString &text, 00580 const QStringList &strlist, 00581 const QString &caption, 00582 const KGuiItem &buttonYes, 00583 const KGuiItem &buttonNo, 00584 const QString &dontAskAgainName, 00585 Options options) 00586 { 00587 return warningYesNoListWId( parent ? parent->effectiveWinId() : 0, text, strlist, caption, 00588 buttonYes, buttonNo, dontAskAgainName, options ); 00589 } 00590 00591 int KMessageBox::warningYesNoListWId(WId parent_id, const QString &text, 00592 const QStringList &strlist, 00593 const QString &caption, 00594 const KGuiItem &buttonYes_, 00595 const KGuiItem &buttonNo_, 00596 const QString &dontAskAgainName, 00597 Options options) 00598 { 00599 ButtonCode res; 00600 if ( !shouldBeShownYesNo(dontAskAgainName, res) ) { 00601 return res; 00602 } 00603 00604 I18N_FILTER_BUTTON_YES(buttonYes_, buttonYes) 00605 I18N_FILTER_BUTTON_NO(buttonNo_, buttonNo) 00606 I18N_POST_BUTTON_FILTER 00607 00608 QWidget* parent = QWidget::find( parent_id ); 00609 KDialog *dialog = new KDialog(parent, Qt::Dialog); 00610 dialog->setCaption( caption.isEmpty() ? i18n("Warning") : caption ); 00611 dialog->setButtons( KDialog::Yes | KDialog::No ); 00612 dialog->setObjectName( "warningYesNoList" ); 00613 dialog->setModal( true ); 00614 dialog->setButtonGuiItem( KDialog::Yes, buttonYes ); 00615 dialog->setButtonGuiItem( KDialog::No, buttonNo ); 00616 dialog->setDefaultButton( KDialog::No ); 00617 dialog->setEscapeButton( KDialog::No ); 00618 if ( options & PlainCaption ) { 00619 dialog->setPlainCaption( caption ); 00620 } 00621 if ( parent == NULL && parent_id ) { 00622 KWindowSystem::setMainWindow( dialog, parent_id ); 00623 } 00624 00625 bool checkboxResult = false; 00626 const int result = createKMessageBox(dialog, QMessageBox::Warning, text, strlist, 00627 dontAskAgainName.isEmpty() ? QString() : i18n("Do not ask again"), 00628 &checkboxResult, options); 00629 res = (result==KDialog::Yes ? Yes : No); 00630 00631 if (checkboxResult) { 00632 saveDontShowAgainYesNo(dontAskAgainName, res); 00633 } 00634 return res; 00635 } 00636 00637 int KMessageBox::warningContinueCancel(QWidget *parent, 00638 const QString &text, 00639 const QString &caption, 00640 const KGuiItem &buttonContinue, 00641 const KGuiItem &buttonCancel, 00642 const QString &dontAskAgainName, 00643 Options options) 00644 { 00645 return warningContinueCancelList(parent, text, QStringList(), caption, 00646 buttonContinue, buttonCancel, dontAskAgainName, options); 00647 } 00648 00649 int KMessageBox::warningContinueCancelWId(WId parent_id, 00650 const QString &text, 00651 const QString &caption, 00652 const KGuiItem &buttonContinue, 00653 const KGuiItem &buttonCancel, 00654 const QString &dontAskAgainName, 00655 Options options) 00656 { 00657 return warningContinueCancelListWId(parent_id, text, QStringList(), caption, 00658 buttonContinue, buttonCancel, dontAskAgainName, options); 00659 } 00660 00661 int KMessageBox::warningContinueCancelList(QWidget *parent, const QString &text, 00662 const QStringList &strlist, 00663 const QString &caption, 00664 const KGuiItem &buttonContinue, 00665 const KGuiItem &buttonCancel, 00666 const QString &dontAskAgainName, 00667 Options options) 00668 { 00669 return warningContinueCancelListWId( parent ? parent->effectiveWinId() : 0, text, strlist, 00670 caption, buttonContinue, buttonCancel, dontAskAgainName, options ); 00671 } 00672 00673 int KMessageBox::warningContinueCancelListWId(WId parent_id, const QString &text, 00674 const QStringList &strlist, 00675 const QString &caption, 00676 const KGuiItem &buttonContinue_, 00677 const KGuiItem &buttonCancel_, 00678 const QString &dontAskAgainName, 00679 Options options) 00680 { 00681 if ( !shouldBeShownContinue(dontAskAgainName) ) 00682 return Continue; 00683 00684 I18N_FILTER_BUTTON_CONTINUE(buttonContinue_, buttonContinue) 00685 I18N_FILTER_BUTTON_CANCEL(buttonCancel_, buttonCancel) 00686 I18N_POST_BUTTON_FILTER 00687 00688 QWidget* parent = QWidget::find( parent_id ); 00689 KDialog *dialog = new KDialog(parent, Qt::Dialog); 00690 dialog->setCaption( caption.isEmpty() ? i18n("Warning") : caption ); 00691 dialog->setButtons( KDialog::Yes | KDialog::No ); 00692 dialog->setObjectName( "warningYesNo" ); 00693 dialog->setModal( true ); 00694 dialog->setButtonGuiItem( KDialog::Yes, buttonContinue ); 00695 dialog->setButtonGuiItem( KDialog::No, buttonCancel ); 00696 dialog->setDefaultButton( KDialog::Yes ); 00697 dialog->setEscapeButton( KDialog::No ); 00698 if ( options & PlainCaption ) { 00699 dialog->setPlainCaption( caption ); 00700 } 00701 if ( parent == NULL && parent_id ) { 00702 KWindowSystem::setMainWindow( dialog, parent_id ); 00703 } 00704 00705 bool checkboxResult = false; 00706 const int result = createKMessageBox(dialog, QMessageBox::Warning, text, strlist, 00707 dontAskAgainName.isEmpty() ? QString() : i18n("Do not ask again"), 00708 &checkboxResult, options); 00709 00710 if ( result != KDialog::Yes ) { 00711 return Cancel; 00712 } 00713 if (checkboxResult) { 00714 saveDontShowAgainContinue(dontAskAgainName); 00715 } 00716 return Continue; 00717 } 00718 00719 int KMessageBox::warningYesNoCancel(QWidget *parent, const QString &text, 00720 const QString &caption, 00721 const KGuiItem &buttonYes, 00722 const KGuiItem &buttonNo, 00723 const KGuiItem &buttonCancel, 00724 const QString &dontAskAgainName, 00725 Options options) 00726 { 00727 return warningYesNoCancelList(parent, text, QStringList(), caption, 00728 buttonYes, buttonNo, buttonCancel, dontAskAgainName, options); 00729 } 00730 00731 int KMessageBox::warningYesNoCancelWId(WId parent_id, const QString &text, 00732 const QString &caption, 00733 const KGuiItem &buttonYes, 00734 const KGuiItem &buttonNo, 00735 const KGuiItem &buttonCancel, 00736 const QString &dontAskAgainName, 00737 Options options) 00738 { 00739 return warningYesNoCancelListWId(parent_id, text, QStringList(), caption, 00740 buttonYes, buttonNo, buttonCancel, dontAskAgainName, options); 00741 } 00742 00743 int KMessageBox::warningYesNoCancelList(QWidget *parent, const QString &text, 00744 const QStringList &strlist, 00745 const QString &caption, 00746 const KGuiItem &buttonYes, 00747 const KGuiItem &buttonNo, 00748 const KGuiItem &buttonCancel, 00749 const QString &dontAskAgainName, 00750 Options options) 00751 { 00752 return warningYesNoCancelListWId( parent ? parent->effectiveWinId() : 0, text, strlist, 00753 caption, buttonYes, buttonNo, buttonCancel, dontAskAgainName, options ); 00754 } 00755 00756 int KMessageBox::warningYesNoCancelListWId(WId parent_id, const QString &text, 00757 const QStringList &strlist, 00758 const QString &caption, 00759 const KGuiItem &buttonYes_, 00760 const KGuiItem &buttonNo_, 00761 const KGuiItem &buttonCancel_, 00762 const QString &dontAskAgainName, 00763 Options options) 00764 { 00765 ButtonCode res; 00766 if ( !shouldBeShownYesNo(dontAskAgainName, res) ) { 00767 return res; 00768 } 00769 00770 I18N_FILTER_BUTTON_YES(buttonYes_, buttonYes) 00771 I18N_FILTER_BUTTON_NO(buttonNo_, buttonNo) 00772 I18N_FILTER_BUTTON_CANCEL(buttonCancel_, buttonCancel) 00773 I18N_POST_BUTTON_FILTER 00774 00775 QWidget* parent = QWidget::find( parent_id ); 00776 KDialog *dialog = new KDialog(parent, Qt::Dialog); 00777 dialog->setCaption( caption.isEmpty() ? i18n("Warning") : caption ); 00778 dialog->setButtons( KDialog::Yes | KDialog::No | KDialog::Cancel ); 00779 dialog->setObjectName( "warningYesNoCancel" ); 00780 dialog->setModal( true ); 00781 dialog->setButtonGuiItem( KDialog::Yes, buttonYes ); 00782 dialog->setButtonGuiItem( KDialog::No, buttonNo ); 00783 dialog->setButtonGuiItem( KDialog::Cancel, buttonCancel ); 00784 dialog->setDefaultButton( KDialog::Yes ); 00785 if ( options & PlainCaption ) { 00786 dialog->setPlainCaption( caption ); 00787 } 00788 if ( parent == NULL && parent_id ) { 00789 KWindowSystem::setMainWindow( dialog, parent_id ); 00790 } 00791 00792 bool checkboxResult = false; 00793 const int result = createKMessageBox(dialog, QMessageBox::Warning, text, strlist, 00794 dontAskAgainName.isEmpty() ? QString() : i18n("Do not ask again"), 00795 &checkboxResult, options); 00796 00797 if ( result == KDialog::Yes ) { 00798 res = Yes; 00799 } else if ( result == KDialog::No ) { 00800 res = No; 00801 } else { 00802 return Cancel; 00803 } 00804 00805 if (checkboxResult) { 00806 saveDontShowAgainYesNo(dontAskAgainName, res); 00807 } 00808 return res; 00809 } 00810 00811 void KMessageBox::error(QWidget *parent, const QString &text, 00812 const QString &caption, Options options) 00813 { 00814 return errorListWId( parent ? parent->effectiveWinId() : 0, text, QStringList(), caption, options ); 00815 } 00816 00817 void KMessageBox::errorWId(WId parent_id, const QString &text, 00818 const QString &caption, Options options) 00819 { 00820 errorListWId( parent_id, text, QStringList(), caption, options ); 00821 } 00822 00823 void KMessageBox::errorList(QWidget *parent, const QString &text, const QStringList &strlist, 00824 const QString &caption, Options options) 00825 { 00826 return errorListWId( parent ? parent->effectiveWinId() : 0, text, strlist, caption, options ); 00827 } 00828 00829 void KMessageBox::errorListWId(WId parent_id, const QString &text, const QStringList &strlist, 00830 const QString &caption, Options options) 00831 { 00832 QWidget* parent = QWidget::find( parent_id ); 00833 KDialog *dialog = new KDialog(parent, Qt::Dialog); 00834 dialog->setCaption( caption.isEmpty() ? i18n("Error") : caption ); 00835 dialog->setButtons( KDialog::Ok ); 00836 dialog->setObjectName( "error" ); 00837 dialog->setModal( true ); 00838 dialog->setDefaultButton( KDialog::Ok ); 00839 dialog->setEscapeButton( KDialog::Ok ); 00840 if ( options & PlainCaption ) { 00841 dialog->setPlainCaption( caption ); 00842 } 00843 if ( parent == NULL && parent_id ) { 00844 KWindowSystem::setMainWindow( dialog, parent_id ); 00845 } 00846 00847 createKMessageBox(dialog, QMessageBox::Critical, text, strlist, QString(), 0, options); 00848 } 00849 00850 void 00851 KMessageBox::detailedError(QWidget *parent, const QString &text, 00852 const QString &details, 00853 const QString &caption, Options options) 00854 { 00855 return detailedErrorWId( parent ? parent->effectiveWinId() : 0, text, details, caption, options ); 00856 } 00857 00858 void KMessageBox::detailedErrorWId(WId parent_id, const QString &text, 00859 const QString &details, 00860 const QString &caption, Options options) 00861 { 00862 QWidget* parent = QWidget::find( parent_id ); 00863 KDialog *dialog = new KDialog(parent, Qt::Dialog); 00864 dialog->setCaption( caption.isEmpty() ? i18n("Error") : caption ); 00865 dialog->setButtons( KDialog::Ok | KDialog::Details ); 00866 dialog->setObjectName( "error" ); 00867 dialog->setModal( true ); 00868 dialog->setDefaultButton( KDialog::Ok ); 00869 dialog->setEscapeButton( KDialog::Ok ); 00870 if( options & PlainCaption ) { 00871 dialog->setPlainCaption( caption ); 00872 } 00873 if ( parent == NULL && parent_id ) { 00874 KWindowSystem::setMainWindow( dialog, parent_id ); 00875 } 00876 00877 createKMessageBox(dialog, QMessageBox::Critical, text, QStringList(), QString(), 0, options, details); 00878 } 00879 00880 void KMessageBox::queuedDetailedError(QWidget *parent, const QString &text, 00881 const QString &details, 00882 const QString &caption) 00883 { 00884 return queuedDetailedErrorWId( parent ? parent->effectiveWinId() : 0, text, details, caption ); 00885 } 00886 00887 void KMessageBox::queuedDetailedErrorWId(WId parent_id, const QString &text, 00888 const QString &details, 00889 const QString &caption) 00890 { 00891 KMessageBox_queue = true; 00892 (void) detailedErrorWId(parent_id, text, details, caption); 00893 KMessageBox_queue = false; 00894 } 00895 00896 00897 void KMessageBox::sorry(QWidget *parent, const QString &text, 00898 const QString &caption, Options options) 00899 { 00900 return sorryWId( parent ? parent->effectiveWinId() : 0, text, caption, options ); 00901 } 00902 00903 void KMessageBox::sorryWId(WId parent_id, const QString &text, 00904 const QString &caption, Options options) 00905 { 00906 QWidget* parent = QWidget::find( parent_id ); 00907 KDialog *dialog = new KDialog(parent, Qt::Dialog); 00908 dialog->setCaption( caption.isEmpty() ? i18n("Sorry") : caption ); 00909 dialog->setButtons( KDialog::Ok ); 00910 dialog->setObjectName( "sorry" ); 00911 dialog->setModal( true ); 00912 dialog->setDefaultButton( KDialog::Ok ); 00913 dialog->setEscapeButton( KDialog::Ok ); 00914 if ( options & PlainCaption ) { 00915 dialog->setPlainCaption( caption ); 00916 } 00917 if ( parent == NULL && parent_id ) { 00918 KWindowSystem::setMainWindow( dialog, parent_id ); 00919 } 00920 00921 createKMessageBox(dialog, QMessageBox::Warning, text, QStringList(), QString(), 0, options); 00922 } 00923 00924 void KMessageBox::detailedSorry(QWidget *parent, const QString &text, 00925 const QString &details, 00926 const QString &caption, Options options) 00927 { 00928 return detailedSorryWId( parent ? parent->effectiveWinId() : 0, text, details, caption, options ); 00929 } 00930 00931 void KMessageBox::detailedSorryWId(WId parent_id, const QString &text, 00932 const QString &details, 00933 const QString &caption, Options options) 00934 { 00935 QWidget* parent = QWidget::find( parent_id ); 00936 KDialog *dialog = new KDialog(parent, Qt::Dialog); 00937 dialog->setCaption( caption.isEmpty() ? i18n("Sorry") : caption ); 00938 dialog->setButtons( KDialog::Ok | KDialog::Details ); 00939 dialog->setObjectName( "sorry" ); 00940 dialog->setModal( true ); 00941 dialog->setDefaultButton( KDialog::Ok ); 00942 dialog->setEscapeButton( KDialog::Ok ); 00943 if ( options & PlainCaption ) { 00944 dialog->setPlainCaption( caption ); 00945 } 00946 if ( parent == NULL && parent_id ) { 00947 KWindowSystem::setMainWindow( dialog, parent_id ); 00948 } 00949 00950 createKMessageBox(dialog, QMessageBox::Warning, text, QStringList(), QString(), 0, options, details); 00951 } 00952 00953 void KMessageBox::information(QWidget *parent,const QString &text, 00954 const QString &caption, const QString &dontShowAgainName, Options options) 00955 { 00956 informationList(parent, text, QStringList(), caption, dontShowAgainName, options); 00957 } 00958 00959 void KMessageBox::informationWId(WId parent_id,const QString &text, 00960 const QString &caption, const QString &dontShowAgainName, Options options) 00961 { 00962 informationListWId(parent_id, text, QStringList(), caption, dontShowAgainName, options); 00963 } 00964 00965 void KMessageBox::informationList(QWidget *parent,const QString &text, const QStringList & strlist, 00966 const QString &caption, const QString &dontShowAgainName, Options options) 00967 { 00968 return informationListWId( parent ? parent->effectiveWinId() : 0, text, strlist, caption, 00969 dontShowAgainName, options ); 00970 } 00971 00972 void KMessageBox::informationListWId(WId parent_id,const QString &text, const QStringList & strlist, 00973 const QString &caption, const QString &dontShowAgainName, Options options) 00974 { 00975 if ( !shouldBeShownContinue(dontShowAgainName) ) { 00976 return; 00977 } 00978 00979 QWidget* parent = QWidget::find( parent_id ); 00980 KDialog *dialog = new KDialog(parent, Qt::Dialog); 00981 dialog->setCaption( caption.isEmpty() ? i18n("Information") : caption ); 00982 dialog->setButtons( KDialog::Ok ); 00983 dialog->setObjectName( "information" ); 00984 dialog->setModal( true ); 00985 dialog->setDefaultButton( KDialog::Ok ); 00986 dialog->setEscapeButton( KDialog::Ok); 00987 if ( options & PlainCaption ) { 00988 dialog->setPlainCaption( caption ); 00989 } 00990 if ( parent == NULL && parent_id ) { 00991 KWindowSystem::setMainWindow( dialog, parent_id ); 00992 } 00993 00994 bool checkboxResult = false; 00995 00996 createKMessageBox(dialog, QMessageBox::Information, text, strlist, 00997 dontShowAgainName.isEmpty() ? QString() : i18n("Do not show this message again"), 00998 &checkboxResult, options); 00999 01000 if (checkboxResult) { 01001 saveDontShowAgainContinue(dontShowAgainName); 01002 } 01003 } 01004 01005 void KMessageBox::enableAllMessages() 01006 { 01007 KConfig *config = KMessageBox_againConfig ? KMessageBox_againConfig : KGlobal::config().data(); 01008 if (!config->hasGroup("Notification Messages")) { 01009 return; 01010 } 01011 01012 KConfigGroup cg(config, "Notification Messages" ); 01013 01014 typedef QMap<QString, QString> configMap; 01015 01016 const configMap map = cg.entryMap(); 01017 01018 configMap::ConstIterator it; 01019 for (it = map.begin(); it != map.end(); ++it) { 01020 cg.deleteEntry( it.key() ); 01021 } 01022 } 01023 01024 void KMessageBox::enableMessage(const QString &dontShowAgainName) 01025 { 01026 KConfig *config = KMessageBox_againConfig ? KMessageBox_againConfig : KGlobal::config().data(); 01027 if (!config->hasGroup("Notification Messages")) { 01028 return; 01029 } 01030 01031 KConfigGroup cg( config, "Notification Messages" ); 01032 01033 cg.deleteEntry(dontShowAgainName); 01034 config->sync(); 01035 } 01036 01037 void KMessageBox::about(QWidget *parent, const QString &text, 01038 const QString &caption, Options options) 01039 { 01040 QString _caption = caption; 01041 if (_caption.isEmpty()) { 01042 _caption = i18n("About %1", KGlobal::caption()); 01043 } 01044 01045 KDialog *dialog = new KDialog(parent, Qt::Dialog); 01046 dialog->setCaption( caption ); 01047 dialog->setButtons( KDialog::Ok ); 01048 dialog->setObjectName( "about" ); 01049 dialog->setModal( true ); 01050 dialog->setDefaultButton( KDialog::Ok ); 01051 dialog->setEscapeButton( KDialog::Ok ); 01052 if (qApp->windowIcon().isNull()) { 01053 QPixmap ret = QMessageBox::standardIcon(QMessageBox::Information); 01054 dialog->setWindowIcon(ret); 01055 } 01056 01057 createKMessageBox(dialog, qApp->windowIcon(), text, QStringList(), QString(), 0, options); 01058 return; 01059 } 01060 01061 int KMessageBox::messageBox( QWidget *parent, DialogType type, const QString &text, 01062 const QString &caption, const KGuiItem &buttonYes, 01063 const KGuiItem &buttonNo, const KGuiItem &buttonCancel, 01064 const QString &dontShowAskAgainName, Options options ) 01065 { 01066 return messageBoxWId( parent ? parent->effectiveWinId() : 0, type, text, caption, 01067 buttonYes, buttonNo, buttonCancel, dontShowAskAgainName, options ); 01068 } 01069 01070 int KMessageBox::messageBoxWId( WId parent_id, DialogType type, const QString &text, 01071 const QString &caption, const KGuiItem &buttonYes, 01072 const KGuiItem &buttonNo, const KGuiItem &buttonCancel, 01073 const QString &dontShow, Options options ) 01074 { 01075 switch (type) { 01076 case QuestionYesNo: 01077 return KMessageBox::questionYesNoWId( parent_id, 01078 text, caption, buttonYes, buttonNo, dontShow, options ); 01079 case QuestionYesNoCancel: 01080 return KMessageBox::questionYesNoCancelWId( parent_id, 01081 text, caption, buttonYes, buttonNo, buttonCancel, dontShow, options ); 01082 case WarningYesNo: 01083 return KMessageBox::warningYesNoWId( parent_id, 01084 text, caption, buttonYes, buttonNo, dontShow, options ); 01085 case WarningContinueCancel: 01086 return KMessageBox::warningContinueCancelWId( parent_id, 01087 text, caption, KGuiItem(buttonYes.text()), buttonCancel, dontShow, options ); 01088 case WarningYesNoCancel: 01089 return KMessageBox::warningYesNoCancelWId( parent_id, 01090 text, caption, buttonYes, buttonNo, buttonCancel, dontShow, options ); 01091 case Information: 01092 KMessageBox::informationWId( parent_id, 01093 text, caption, dontShow, options ); 01094 return KMessageBox::Ok; 01095 01096 case Error: 01097 KMessageBox::errorWId( parent_id, text, caption, options ); 01098 return KMessageBox::Ok; 01099 01100 case Sorry: 01101 KMessageBox::sorryWId( parent_id, text, caption, options ); 01102 return KMessageBox::Ok; 01103 } 01104 return KMessageBox::Cancel; 01105 } 01106 01107 void KMessageBox::queuedMessageBox( QWidget *parent, DialogType type, const QString &text, const QString &caption, Options options ) 01108 { 01109 return queuedMessageBoxWId( parent ? parent->effectiveWinId() : 0, type, text, caption, options ); 01110 } 01111 01112 void KMessageBox::queuedMessageBoxWId( WId parent_id, DialogType type, const QString &text, const QString &caption, Options options ) 01113 { 01114 KMessageBox_queue = true; 01115 (void) messageBoxWId(parent_id, type, text, caption, KStandardGuiItem::yes(), 01116 KStandardGuiItem::no(), KStandardGuiItem::cancel(), QString(), options); 01117 KMessageBox_queue = false; 01118 } 01119 01120 void KMessageBox::queuedMessageBox( QWidget *parent, DialogType type, const QString &text, const QString &caption ) 01121 { 01122 return queuedMessageBoxWId( parent ? parent->effectiveWinId() : 0, type, text, caption ); 01123 } 01124 01125 void KMessageBox::queuedMessageBoxWId( WId parent_id, DialogType type, const QString &text, const QString &caption ) 01126 { 01127 KMessageBox_queue = true; 01128 (void) messageBoxWId(parent_id, type, text, caption); 01129 KMessageBox_queue = false; 01130 }
KDE 4.6 API Reference