[2] | 1 | /****************************************************************************
|
---|
| 2 | ** $Id: qmessagebox.cpp 21 2005-11-17 19:51:23Z dmik $
|
---|
| 3 | **
|
---|
| 4 | ** Implementation of QMessageBox class
|
---|
| 5 | **
|
---|
| 6 | ** Created : 950503
|
---|
| 7 | **
|
---|
| 8 | ** Copyright (C) 1992-2003 Trolltech AS. All rights reserved.
|
---|
| 9 | **
|
---|
| 10 | ** This file is part of the dialogs module of the Qt GUI Toolkit.
|
---|
| 11 | **
|
---|
| 12 | ** This file may be distributed under the terms of the Q Public License
|
---|
| 13 | ** as defined by Trolltech AS of Norway and appearing in the file
|
---|
| 14 | ** LICENSE.QPL included in the packaging of this file.
|
---|
| 15 | **
|
---|
| 16 | ** This file may be distributed and/or modified under the terms of the
|
---|
| 17 | ** GNU General Public License version 2 as published by the Free Software
|
---|
| 18 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
| 19 | ** packaging of this file.
|
---|
| 20 | **
|
---|
| 21 | ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
|
---|
| 22 | ** licenses may use this file in accordance with the Qt Commercial License
|
---|
| 23 | ** Agreement provided with the Software.
|
---|
| 24 | **
|
---|
| 25 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
---|
| 26 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
---|
| 27 | **
|
---|
| 28 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
|
---|
| 29 | ** information about Qt Commercial License Agreements.
|
---|
| 30 | ** See http://www.trolltech.com/qpl/ for QPL licensing information.
|
---|
| 31 | ** See http://www.trolltech.com/gpl/ for GPL licensing information.
|
---|
| 32 | **
|
---|
| 33 | ** Contact info@trolltech.com if any conditions of this licensing are
|
---|
| 34 | ** not clear to you.
|
---|
| 35 | **
|
---|
| 36 | **********************************************************************/
|
---|
| 37 |
|
---|
| 38 | #include "qmessagebox.h"
|
---|
| 39 |
|
---|
| 40 | #ifndef QT_NO_MESSAGEBOX
|
---|
| 41 |
|
---|
| 42 | #include "qaccel.h"
|
---|
| 43 | #include "qlabel.h"
|
---|
| 44 | #include "qpushbutton.h"
|
---|
| 45 | #include "qimage.h"
|
---|
| 46 | #include "qapplication.h"
|
---|
| 47 | #include "qstyle.h"
|
---|
| 48 | #include "qobjectlist.h"
|
---|
| 49 | #if defined(QT_ACCESSIBILITY_SUPPORT)
|
---|
| 50 | #include "qaccessible.h"
|
---|
| 51 | #endif
|
---|
| 52 | #if defined QT_NON_COMMERCIAL
|
---|
| 53 | #include "qnc_win.h"
|
---|
| 54 | #endif
|
---|
[8] | 55 | #if defined(Q_WS_PM)
|
---|
| 56 | //@@TODO (dmik): this is necessary for WinAlarm() which will be
|
---|
| 57 | // probably removed from here later.
|
---|
| 58 | #include "qt_os2.h"
|
---|
| 59 | #endif
|
---|
[2] | 60 |
|
---|
| 61 | // Internal class - don't touch
|
---|
| 62 |
|
---|
| 63 | class QMessageBoxLabel : public QLabel
|
---|
| 64 | {
|
---|
| 65 | Q_OBJECT
|
---|
| 66 | public:
|
---|
| 67 | QMessageBoxLabel( QWidget* parent ) : QLabel( parent, "messageBoxText")
|
---|
| 68 | {
|
---|
| 69 | setAlignment( AlignAuto|ExpandTabs );
|
---|
| 70 | }
|
---|
| 71 | };
|
---|
| 72 | #include "qmessagebox.moc"
|
---|
| 73 |
|
---|
| 74 |
|
---|
| 75 |
|
---|
| 76 | // the Qt logo, for aboutQt
|
---|
| 77 | /* XPM */
|
---|
| 78 | static const char * const qtlogo_xpm[] = {
|
---|
| 79 | /* width height ncolors chars_per_pixel */
|
---|
| 80 | "50 50 17 1",
|
---|
| 81 | /* colors */
|
---|
| 82 | " c #000000",
|
---|
| 83 | ". c #495808",
|
---|
| 84 | "X c #2A3304",
|
---|
| 85 | "o c #242B04",
|
---|
| 86 | "O c #030401",
|
---|
| 87 | "+ c #9EC011",
|
---|
| 88 | "@ c #93B310",
|
---|
| 89 | "# c #748E0C",
|
---|
| 90 | "$ c #A2C511",
|
---|
| 91 | "% c #8BA90E",
|
---|
| 92 | "& c #99BA10",
|
---|
| 93 | "* c #060701",
|
---|
| 94 | "= c #181D02",
|
---|
| 95 | "- c #212804",
|
---|
| 96 | "; c #61770A",
|
---|
| 97 | ": c #0B0D01",
|
---|
| 98 | "/ c None",
|
---|
| 99 | /* pixels */
|
---|
| 100 | "/$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$/",
|
---|
| 101 | "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$",
|
---|
| 102 | "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$",
|
---|
| 103 | "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$",
|
---|
| 104 | "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$",
|
---|
| 105 | "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$",
|
---|
| 106 | "$$$$$$$$$$$$$$$$$$$$$$$+++$$$$$$$$$$$$$$$$$$$$$$$$",
|
---|
| 107 | "$$$$$$$$$$$$$$$$$$$@;.o=::=o.;@$$$$$$$$$$$$$$$$$$$",
|
---|
| 108 | "$$$$$$$$$$$$$$$$+#X* **X#+$$$$$$$$$$$$$$$$",
|
---|
| 109 | "$$$$$$$$$$$$$$$#oO* O **o#+$$$$$$$$$$$$$$",
|
---|
| 110 | "$$$$$$$$$$$$$&.* OO O*.&$$$$$$$$$$$$$",
|
---|
| 111 | "$$$$$$$$$$$$@XOO * OO X&$$$$$$$$$$$$",
|
---|
| 112 | "$$$$$$$$$$$@XO OO O **:::OOO OOO X@$$$$$$$$$$$",
|
---|
| 113 | "$$$$$$$$$$&XO O-;#@++@%.oOO X&$$$$$$$$$$",
|
---|
| 114 | "$$$$$$$$$$.O : *-#+$$$$$$$$+#- : O O*.$$$$$$$$$$",
|
---|
| 115 | "$$$$$$$$$#*OO O*.&$$$$$$$$$$$$+.OOOO **#$$$$$$$$$",
|
---|
| 116 | "$$$$$$$$+-OO O *;$$$$$$$$$$$&$$$$;* o+$$$$$$$$",
|
---|
| 117 | "$$$$$$$$#O* O .+$$$$$$$$$$@X;$$$+.O *#$$$$$$$$",
|
---|
| 118 | "$$$$$$$$X* -&$$$$$$$$$$@- :;$$$&- OX$$$$$$$$",
|
---|
| 119 | "$$$$$$$@*O *O#$$$$$$$$$$@oOO**;$$$# O*%$$$$$$$",
|
---|
| 120 | "$$$$$$$; -+$$$$$$$$$@o O OO ;+$$-O *;$$$$$$$",
|
---|
| 121 | "$$$$$$$. ;$$$$$$$$$@-OO OO X&$$;O .$$$$$$$",
|
---|
| 122 | "$$$$$$$o *#$$$$$$$$@o O O O-@$$$#O *o$$$$$$$",
|
---|
| 123 | "$$$$$$+= *@$$$$$$$@o* OO -@$$$$&: =$$$$$$$",
|
---|
| 124 | "$$$$$$+: :+$$$$$$@- *-@$$$$$$: :+$$$$$$",
|
---|
| 125 | "$$$$$$+: :+$$$$$@o* O *-@$$$$$$: :+$$$$$$",
|
---|
| 126 | "$$$$$$$= :@$$$$@o*OOO -@$$$$@: =+$$$$$$",
|
---|
| 127 | "$$$$$$$- O%$$$@o* O O O O-@$$$#* OX$$$$$$$",
|
---|
| 128 | "$$$$$$$. O *O;$$&o O*O* *O -@$$; O.$$$$$$$",
|
---|
| 129 | "$$$$$$$;* Oo+$$;O*O:OO-- Oo@+= *;$$$$$$$",
|
---|
| 130 | "$$$$$$$@* O O#$$$;*OOOo@@-O Oo;O* **@$$$$$$$",
|
---|
| 131 | "$$$$$$$$X* OOO-+$$$;O o@$$@- O O OX$$$$$$$$",
|
---|
| 132 | "$$$$$$$$#* * O.$$$$;X@$$$$@-O O O#$$$$$$$$",
|
---|
| 133 | "$$$$$$$$+oO O OO.+$$+&$$$$$$@-O o+$$$$$$$$",
|
---|
| 134 | "$$$$$$$$$#* **.&$$$$$$$$$$@o OO:#$$$$$$$$$",
|
---|
| 135 | "$$$$$$$$$+. O* O-#+$$$$$$$$+;O OOO:@$$$$$$$$$",
|
---|
| 136 | "$$$$$$$$$$&X *O -;#@++@#;=O O -@$$$$$$$$",
|
---|
| 137 | "$$$$$$$$$$$&X O O*O::::O OO Oo@$$$$$$$",
|
---|
| 138 | "$$$$$$$$$$$$@XOO OO O*X+$$$$$$",
|
---|
| 139 | "$$$$$$$$$$$$$&.* ** O :: *:#$$$$$$$",
|
---|
| 140 | "$$$$$$$$$$$$$$$#o*OO O Oo#@-OOO=#$$$$$$$$",
|
---|
| 141 | "$$$$$$$$$$$$$$$$+#X:* * O**X#+$$@-*:#$$$$$$$$$",
|
---|
| 142 | "$$$$$$$$$$$$$$$$$$$%;.o=::=o.#@$$$$$$@X#$$$$$$$$$$",
|
---|
| 143 | "$$$$$$$$$$$$$$$$$$$$$$$$+++$$$$$$$$$$$+$$$$$$$$$$$",
|
---|
| 144 | "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$",
|
---|
| 145 | "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$",
|
---|
| 146 | "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$",
|
---|
| 147 | "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$",
|
---|
| 148 | "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$",
|
---|
| 149 | "/$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$/",
|
---|
| 150 | };
|
---|
| 151 |
|
---|
| 152 |
|
---|
| 153 | /*!
|
---|
| 154 | \class QMessageBox
|
---|
| 155 | \brief The QMessageBox class provides a modal dialog with a short message, an icon, and some buttons.
|
---|
| 156 | \ingroup dialogs
|
---|
| 157 | \mainclass
|
---|
| 158 |
|
---|
| 159 | Message boxes are used to provide informative messages and to ask
|
---|
| 160 | simple questions.
|
---|
| 161 |
|
---|
| 162 | QMessageBox provides a range of different messages, arranged
|
---|
| 163 | roughly along two axes: severity and complexity.
|
---|
| 164 |
|
---|
| 165 | Severity is
|
---|
| 166 | \table
|
---|
| 167 | \row
|
---|
| 168 | \i \img qmessagebox-quest.png
|
---|
| 169 | \i Question
|
---|
| 170 | \i For message boxes that ask a question as part of normal
|
---|
| 171 | operation. Some style guides recommend using Information for this
|
---|
| 172 | purpose.
|
---|
| 173 | \row
|
---|
| 174 | \i \img qmessagebox-info.png
|
---|
| 175 | \i Information
|
---|
| 176 | \i For message boxes that are part of normal operation.
|
---|
| 177 | \row
|
---|
| 178 | \i \img qmessagebox-warn.png
|
---|
| 179 | \i Warning
|
---|
| 180 | \i For message boxes that tell the user about unusual errors.
|
---|
| 181 | \row
|
---|
| 182 | \i \img qmessagebox-crit.png
|
---|
| 183 | \i Critical
|
---|
| 184 | \i For message boxes that tell the user about critical errors.
|
---|
| 185 | \endtable
|
---|
| 186 |
|
---|
| 187 | The message box has a different icon for each of the severity levels.
|
---|
| 188 |
|
---|
| 189 | Complexity is one button (OK) for simple messages, or two or even
|
---|
| 190 | three buttons for questions.
|
---|
| 191 |
|
---|
| 192 | There are static functions for the most common cases.
|
---|
| 193 |
|
---|
| 194 | Examples:
|
---|
| 195 |
|
---|
| 196 | If a program is unable to find a supporting file, but can do perfectly
|
---|
| 197 | well without it:
|
---|
| 198 |
|
---|
| 199 | \code
|
---|
| 200 | QMessageBox::information( this, "Application name",
|
---|
| 201 | "Unable to find the user preferences file.\n"
|
---|
| 202 | "The factory default will be used instead." );
|
---|
| 203 | \endcode
|
---|
| 204 |
|
---|
| 205 | question() is useful for simple yes/no questions:
|
---|
| 206 |
|
---|
| 207 | \code
|
---|
| 208 | if ( QFile::exists( filename ) &&
|
---|
| 209 | QMessageBox::question(
|
---|
| 210 | this,
|
---|
| 211 | tr("Overwrite File? -- Application Name"),
|
---|
| 212 | tr("A file called %1 already exists."
|
---|
| 213 | "Do you want to overwrite it?")
|
---|
| 214 | .arg( filename ),
|
---|
| 215 | tr("&Yes"), tr("&No"),
|
---|
| 216 | QString::null, 0, 1 ) )
|
---|
| 217 | return false;
|
---|
| 218 | \endcode
|
---|
| 219 |
|
---|
| 220 | warning() can be used to tell the user about unusual errors, or
|
---|
| 221 | errors which can't be easily fixed:
|
---|
| 222 |
|
---|
| 223 | \code
|
---|
| 224 | switch( QMessageBox::warning( this, "Application name",
|
---|
| 225 | "Could not connect to the <mumble> server.\n"
|
---|
| 226 | "This program can't function correctly "
|
---|
| 227 | "without the server.\n\n",
|
---|
| 228 | "Retry",
|
---|
| 229 | "Quit", 0, 0, 1 ) ) {
|
---|
| 230 | case 0: // The user clicked the Retry again button or pressed Enter
|
---|
| 231 | // try again
|
---|
| 232 | break;
|
---|
| 233 | case 1: // The user clicked the Quit or pressed Escape
|
---|
| 234 | // exit
|
---|
| 235 | break;
|
---|
| 236 | }
|
---|
| 237 | \endcode
|
---|
| 238 |
|
---|
| 239 | The text part of all message box messages can be either rich text
|
---|
| 240 | or plain text. If you specify a rich text formatted string, it
|
---|
| 241 | will be rendered using the default stylesheet. See
|
---|
| 242 | QStyleSheet::defaultSheet() for details. With certain strings that
|
---|
| 243 | contain XML meta characters, the auto-rich text detection may
|
---|
| 244 | fail, interpreting plain text incorrectly as rich text. In these
|
---|
| 245 | rare cases, use QStyleSheet::convertFromPlainText() to convert
|
---|
| 246 | your plain text string to a visually equivalent rich text string
|
---|
| 247 | or set the text format explicitly with setTextFormat().
|
---|
| 248 |
|
---|
| 249 | Note that the Microsoft Windows User Interface Guidelines
|
---|
| 250 | recommend using the application name as the window's caption.
|
---|
| 251 |
|
---|
| 252 | Below are more examples of how to use the static member functions.
|
---|
| 253 | After these examples you will find an overview of the non-static
|
---|
| 254 | member functions.
|
---|
| 255 |
|
---|
| 256 | Exiting a program is part of its normal operation. If there is
|
---|
| 257 | unsaved data the user probably should be asked if they want to
|
---|
| 258 | save the data. For example:
|
---|
| 259 |
|
---|
| 260 | \code
|
---|
| 261 | switch( QMessageBox::information( this, "Application name here",
|
---|
| 262 | "The document contains unsaved changes\n"
|
---|
| 263 | "Do you want to save the changes before exiting?",
|
---|
| 264 | "&Save", "&Discard", "Cancel",
|
---|
| 265 | 0, // Enter == button 0
|
---|
| 266 | 2 ) ) { // Escape == button 2
|
---|
| 267 | case 0: // Save clicked or Alt+S pressed or Enter pressed.
|
---|
| 268 | // save
|
---|
| 269 | break;
|
---|
| 270 | case 1: // Discard clicked or Alt+D pressed
|
---|
| 271 | // don't save but exit
|
---|
| 272 | break;
|
---|
| 273 | case 2: // Cancel clicked or Escape pressed
|
---|
| 274 | // don't exit
|
---|
| 275 | break;
|
---|
| 276 | }
|
---|
| 277 | \endcode
|
---|
| 278 |
|
---|
| 279 | The Escape button cancels the entire exit operation, and pressing
|
---|
| 280 | Enter causes the changes to be saved before the exit occurs.
|
---|
| 281 |
|
---|
| 282 | Disk full errors are unusual and they certainly can be hard to
|
---|
| 283 | correct. This example uses predefined buttons instead of
|
---|
| 284 | hard-coded button texts:
|
---|
| 285 |
|
---|
| 286 | \code
|
---|
| 287 | switch( QMessageBox::warning( this, "Application name here",
|
---|
| 288 | "Could not save the user preferences,\n"
|
---|
| 289 | "because the disk is full. You can delete\n"
|
---|
| 290 | "some files and press Retry, or you can\n"
|
---|
| 291 | "abort the Save Preferences operation.",
|
---|
| 292 | QMessageBox::Retry | QMessageBox::Default,
|
---|
| 293 | QMessageBox::Abort | QMessageBox::Escape )) {
|
---|
| 294 | case QMessageBox::Retry: // Retry clicked or Enter pressed
|
---|
| 295 | // try again
|
---|
| 296 | break;
|
---|
| 297 | case QMessageBox::Abort: // Abort clicked or Escape pressed
|
---|
| 298 | // abort
|
---|
| 299 | break;
|
---|
| 300 | }
|
---|
| 301 | \endcode
|
---|
| 302 |
|
---|
| 303 | The critical() function should be reserved for critical errors. In
|
---|
| 304 | this example errorDetails is a QString or const char*, and QString
|
---|
| 305 | is used to concatenate several strings:
|
---|
| 306 |
|
---|
| 307 | \code
|
---|
| 308 | QMessageBox::critical( 0, "Application name here",
|
---|
| 309 | QString("An internal error occurred. Please ") +
|
---|
| 310 | "call technical support at 1234-56789 and report\n"+
|
---|
| 311 | "these numbers:\n\n" + errorDetails +
|
---|
| 312 | "\n\nApplication will now exit." );
|
---|
| 313 | \endcode
|
---|
| 314 |
|
---|
| 315 | In this example an OK button is displayed.
|
---|
| 316 |
|
---|
| 317 | QMessageBox provides a very simple About box which displays an
|
---|
| 318 | appropriate icon and the string you provide:
|
---|
| 319 |
|
---|
| 320 | \code
|
---|
| 321 | QMessageBox::about( this, "About <Application>",
|
---|
| 322 | "<Application> is a <one-paragraph blurb>\n\n"
|
---|
| 323 | "Copyright 1991-2003 Such-and-such. "
|
---|
| 324 | "<License words here.>\n\n"
|
---|
| 325 | "For technical support, call 1234-56789 or see\n"
|
---|
| 326 | "http://www.such-and-such.com/Application/\n" );
|
---|
| 327 | \endcode
|
---|
| 328 |
|
---|
| 329 | See about() for more information.
|
---|
| 330 |
|
---|
| 331 | If you want your users to know that the application is built using
|
---|
| 332 | Qt (so they know that you use high quality tools) you might like
|
---|
| 333 | to add an "About Qt" menu option under the Help menu to invoke
|
---|
| 334 | aboutQt().
|
---|
| 335 |
|
---|
| 336 | If none of the standard message boxes is suitable, you can create a
|
---|
| 337 | QMessageBox from scratch and use custom button texts:
|
---|
| 338 |
|
---|
| 339 | \code
|
---|
| 340 | QMessageBox mb( "Application name here",
|
---|
| 341 | "Saving the file will overwrite the original file on the disk.\n"
|
---|
| 342 | "Do you really want to save?",
|
---|
| 343 | QMessageBox::Information,
|
---|
| 344 | QMessageBox::Yes | QMessageBox::Default,
|
---|
| 345 | QMessageBox::No,
|
---|
| 346 | QMessageBox::Cancel | QMessageBox::Escape );
|
---|
| 347 | mb.setButtonText( QMessageBox::Yes, "Save" );
|
---|
| 348 | mb.setButtonText( QMessageBox::No, "Discard" );
|
---|
| 349 | switch( mb.exec() ) {
|
---|
| 350 | case QMessageBox::Yes:
|
---|
| 351 | // save and exit
|
---|
| 352 | break;
|
---|
| 353 | case QMessageBox::No:
|
---|
| 354 | // exit without saving
|
---|
| 355 | break;
|
---|
| 356 | case QMessageBox::Cancel:
|
---|
| 357 | // don't save and don't exit
|
---|
| 358 | break;
|
---|
| 359 | }
|
---|
| 360 | \endcode
|
---|
| 361 |
|
---|
| 362 | QMessageBox defines two enum types: Icon and an unnamed button type.
|
---|
| 363 | Icon defines the \c Question, \c Information, \c Warning, and \c
|
---|
| 364 | Critical icons for each GUI style. It is used by the constructor
|
---|
| 365 | and by the static member functions question(), information(),
|
---|
| 366 | warning() and critical(). A function called standardIcon() gives
|
---|
| 367 | you access to the various icons.
|
---|
| 368 |
|
---|
| 369 | The button types are:
|
---|
| 370 | \list
|
---|
| 371 | \i Ok - the default for single-button message boxes
|
---|
| 372 | \i Cancel - note that this is \e not automatically Escape
|
---|
| 373 | \i Yes
|
---|
| 374 | \i No
|
---|
| 375 | \i Abort
|
---|
| 376 | \i Retry
|
---|
| 377 | \i Ignore
|
---|
| 378 | \i YesAll
|
---|
| 379 | \i NoAll
|
---|
| 380 | \endlist
|
---|
| 381 |
|
---|
| 382 | Button types can be combined with two modifiers by using OR, '|':
|
---|
| 383 | \list
|
---|
| 384 | \i Default - makes pressing Enter equivalent to
|
---|
| 385 | clicking this button. Normally used with Ok, Yes or similar.
|
---|
| 386 | \i Escape - makes pressing Escape equivalent to clicking this button.
|
---|
| 387 | Normally used with Abort, Cancel or similar.
|
---|
| 388 | \endlist
|
---|
| 389 |
|
---|
| 390 | The text(), icon() and iconPixmap() functions provide access to the
|
---|
| 391 | current text and pixmap of the message box. The setText(), setIcon()
|
---|
| 392 | and setIconPixmap() let you change it. The difference between
|
---|
| 393 | setIcon() and setIconPixmap() is that the former accepts a
|
---|
| 394 | QMessageBox::Icon and can be used to set standard icons, whereas the
|
---|
| 395 | latter accepts a QPixmap and can be used to set custom icons.
|
---|
| 396 |
|
---|
| 397 | setButtonText() and buttonText() provide access to the buttons.
|
---|
| 398 |
|
---|
| 399 | QMessageBox has no signals or slots.
|
---|
| 400 |
|
---|
| 401 | <img src=qmsgbox-m.png> <img src=qmsgbox-w.png>
|
---|
| 402 |
|
---|
| 403 | \sa QDialog,
|
---|
| 404 | \link http://www.iarchitect.com/errormsg.htm
|
---|
| 405 | Isys on error messages \endlink,
|
---|
| 406 | \link guibooks.html#fowler GUI Design Handbook: Message Box \endlink
|
---|
| 407 | */
|
---|
| 408 |
|
---|
| 409 |
|
---|
| 410 | /*!
|
---|
| 411 | \enum QMessageBox::Icon
|
---|
| 412 |
|
---|
| 413 | This enum has the following values:
|
---|
| 414 |
|
---|
| 415 | \value NoIcon the message box does not have any icon.
|
---|
| 416 |
|
---|
| 417 | \value Question an icon indicating that
|
---|
| 418 | the message is asking a question.
|
---|
| 419 |
|
---|
| 420 | \value Information an icon indicating that
|
---|
| 421 | the message is nothing out of the ordinary.
|
---|
| 422 |
|
---|
| 423 | \value Warning an icon indicating that the
|
---|
| 424 | message is a warning, but can be dealt with.
|
---|
| 425 |
|
---|
| 426 | \value Critical an icon indicating that
|
---|
| 427 | the message represents a critical problem.
|
---|
| 428 |
|
---|
| 429 | */
|
---|
| 430 |
|
---|
| 431 |
|
---|
| 432 | struct QMessageBoxData {
|
---|
| 433 | QMessageBoxData(QMessageBox* parent) :
|
---|
| 434 | iconLabel( parent, "icon" )
|
---|
| 435 | {
|
---|
| 436 | }
|
---|
| 437 |
|
---|
| 438 | int numButtons; // number of buttons
|
---|
| 439 | QMessageBox::Icon icon; // message box icon
|
---|
| 440 | QLabel iconLabel; // label holding any icon
|
---|
| 441 | int button[3]; // button types
|
---|
| 442 | int defButton; // default button (index)
|
---|
| 443 | int escButton; // escape button (index)
|
---|
| 444 | QSize buttonSize; // button size
|
---|
| 445 | QPushButton *pb[3]; // buttons
|
---|
| 446 | };
|
---|
| 447 |
|
---|
| 448 | static const int LastButton = QMessageBox::NoAll;
|
---|
| 449 |
|
---|
| 450 | /*
|
---|
| 451 | NOTE: The table of button texts correspond to the button enum.
|
---|
| 452 | */
|
---|
| 453 |
|
---|
| 454 | #ifndef Q_OS_TEMP
|
---|
| 455 | static const char * const mb_texts[] = {
|
---|
| 456 | #else
|
---|
| 457 | const char * mb_texts[] = {
|
---|
| 458 | #endif
|
---|
| 459 | 0,
|
---|
| 460 | QT_TRANSLATE_NOOP("QMessageBox","OK"),
|
---|
| 461 | QT_TRANSLATE_NOOP("QMessageBox","Cancel"),
|
---|
| 462 | QT_TRANSLATE_NOOP("QMessageBox","&Yes"),
|
---|
| 463 | QT_TRANSLATE_NOOP("QMessageBox","&No"),
|
---|
| 464 | QT_TRANSLATE_NOOP("QMessageBox","&Abort"),
|
---|
| 465 | QT_TRANSLATE_NOOP("QMessageBox","&Retry"),
|
---|
| 466 | QT_TRANSLATE_NOOP("QMessageBox","&Ignore"),
|
---|
| 467 | QT_TRANSLATE_NOOP("QMessageBox","Yes to &All"),
|
---|
| 468 | QT_TRANSLATE_NOOP("QMessageBox","N&o to All"),
|
---|
| 469 | 0
|
---|
| 470 | };
|
---|
| 471 |
|
---|
| 472 | /*!
|
---|
| 473 | Constructs a message box with no text and a button with the label
|
---|
| 474 | "OK".
|
---|
| 475 |
|
---|
| 476 | If \a parent is 0, the message box becomes an application-global
|
---|
| 477 | modal dialog box. If \a parent is a widget, the message box
|
---|
| 478 | becomes modal relative to \a parent.
|
---|
| 479 |
|
---|
| 480 | The \a parent and \a name arguments are passed to the QDialog
|
---|
| 481 | constructor.
|
---|
| 482 | */
|
---|
| 483 |
|
---|
| 484 | QMessageBox::QMessageBox( QWidget *parent, const char *name )
|
---|
| 485 | : QDialog( parent, name, TRUE, WStyle_Customize | WStyle_DialogBorder | WStyle_Title | WStyle_SysMenu )
|
---|
| 486 | {
|
---|
| 487 | init( Ok, 0, 0 );
|
---|
| 488 | }
|
---|
| 489 |
|
---|
| 490 |
|
---|
| 491 | /*!
|
---|
| 492 | Constructs a message box with a \a caption, a \a text, an \a icon,
|
---|
| 493 | and up to three buttons.
|
---|
| 494 |
|
---|
| 495 | The \a icon must be one of the following:
|
---|
| 496 | \list
|
---|
| 497 | \i QMessageBox::NoIcon
|
---|
| 498 | \i QMessageBox::Question
|
---|
| 499 | \i QMessageBox::Information
|
---|
| 500 | \i QMessageBox::Warning
|
---|
| 501 | \i QMessageBox::Critical
|
---|
| 502 | \endlist
|
---|
| 503 |
|
---|
| 504 | Each button, \a button0, \a button1 and \a button2, can have one
|
---|
| 505 | of the following values:
|
---|
| 506 | \list
|
---|
| 507 | \i QMessageBox::NoButton
|
---|
| 508 | \i QMessageBox::Ok
|
---|
| 509 | \i QMessageBox::Cancel
|
---|
| 510 | \i QMessageBox::Yes
|
---|
| 511 | \i QMessageBox::No
|
---|
| 512 | \i QMessageBox::Abort
|
---|
| 513 | \i QMessageBox::Retry
|
---|
| 514 | \i QMessageBox::Ignore
|
---|
| 515 | \i QMessageBox::YesAll
|
---|
| 516 | \i QMessageBox::NoAll
|
---|
| 517 | \endlist
|
---|
| 518 |
|
---|
| 519 | Use QMessageBox::NoButton for the later parameters to have fewer
|
---|
| 520 | than three buttons in your message box. If you don't specify any
|
---|
| 521 | buttons at all, QMessageBox will provide an Ok button.
|
---|
| 522 |
|
---|
| 523 | One of the buttons can be OR-ed with the \c QMessageBox::Default
|
---|
| 524 | flag to make it the default button (clicked when Enter is
|
---|
| 525 | pressed).
|
---|
| 526 |
|
---|
| 527 | One of the buttons can be OR-ed with the \c QMessageBox::Escape
|
---|
| 528 | flag to make it the cancel or close button (clicked when Escape is
|
---|
| 529 | pressed).
|
---|
| 530 |
|
---|
| 531 | Example:
|
---|
| 532 | \code
|
---|
| 533 | QMessageBox mb( "Application Name",
|
---|
| 534 | "Hardware failure.\n\nDisk error detected\nDo you want to stop?",
|
---|
| 535 | QMessageBox::Question,
|
---|
| 536 | QMessageBox::Yes | QMessageBox::Default,
|
---|
| 537 | QMessageBox::No | QMessageBox::Escape
|
---|
| 538 | QMessageBox::NoButton );
|
---|
| 539 | if ( mb.exec() == QMessageBox::No )
|
---|
| 540 | // try again
|
---|
| 541 | \endcode
|
---|
| 542 |
|
---|
| 543 | If \a parent is 0, the message box becomes an application-global
|
---|
| 544 | modal dialog box. If \a parent is a widget, the message box
|
---|
| 545 | becomes modal relative to \a parent.
|
---|
| 546 |
|
---|
| 547 | If \a modal is TRUE the message box is modal; otherwise it
|
---|
| 548 | is modeless.
|
---|
| 549 |
|
---|
| 550 | The \a parent, \a name, \a modal, and \a f arguments are passed to
|
---|
| 551 | the QDialog constructor.
|
---|
| 552 |
|
---|
| 553 | \sa setCaption(), setText(), setIcon()
|
---|
| 554 | */
|
---|
| 555 |
|
---|
| 556 | QMessageBox::QMessageBox( const QString& caption,
|
---|
| 557 | const QString &text, Icon icon,
|
---|
| 558 | int button0, int button1, int button2,
|
---|
| 559 | QWidget *parent, const char *name,
|
---|
| 560 | bool modal, WFlags f )
|
---|
| 561 | : QDialog( parent, name, modal, f | WStyle_Customize | WStyle_DialogBorder | WStyle_Title | WStyle_SysMenu )
|
---|
| 562 | {
|
---|
| 563 | init( button0, button1, button2 );
|
---|
| 564 | #ifndef QT_NO_WIDGET_TOPEXTRA
|
---|
| 565 | setCaption( caption );
|
---|
| 566 | #endif
|
---|
| 567 | setText( text );
|
---|
| 568 | setIcon( icon );
|
---|
| 569 | }
|
---|
| 570 |
|
---|
| 571 |
|
---|
| 572 | /*!
|
---|
| 573 | Destroys the message box.
|
---|
| 574 | */
|
---|
| 575 |
|
---|
| 576 | QMessageBox::~QMessageBox()
|
---|
| 577 | {
|
---|
| 578 | delete mbd;
|
---|
| 579 | }
|
---|
| 580 |
|
---|
| 581 | static QString * translatedTextAboutQt = 0;
|
---|
| 582 |
|
---|
| 583 | void QMessageBox::init( int button0, int button1, int button2 )
|
---|
| 584 | {
|
---|
| 585 | if ( !translatedTextAboutQt ) {
|
---|
| 586 | translatedTextAboutQt = new QString;
|
---|
| 587 |
|
---|
| 588 | #if defined(QT_NON_COMMERCIAL)
|
---|
| 589 | QT_NC_MSGBOX
|
---|
| 590 | #else
|
---|
| 591 | *translatedTextAboutQt = tr(
|
---|
| 592 | "<h3>About Qt</h3>"
|
---|
| 593 | "<p>This program uses Qt version %1.</p>"
|
---|
| 594 | "<p>Qt is a C++ toolkit for multiplatform GUI & "
|
---|
| 595 | "application development.</p>"
|
---|
| 596 | "<p>Qt provides single-source "
|
---|
| 597 | "portability across MS Windows, Mac OS X, "
|
---|
[8] | 598 | "IBM OS/2 Warp and eComStation, "
|
---|
[2] | 599 | "Linux, and all major commercial Unix variants."
|
---|
| 600 | "<br>Qt is also available for embedded devices.</p>"
|
---|
| 601 | "<p>Qt is a Trolltech product. "
|
---|
| 602 | "See <tt>http://www.trolltech.com/qt/</tt> "
|
---|
| 603 | "for more information.</p>"
|
---|
| 604 | ).arg( QT_VERSION_STR );
|
---|
| 605 | #endif
|
---|
| 606 |
|
---|
| 607 | }
|
---|
| 608 | label = new QMessageBoxLabel( this );
|
---|
| 609 | Q_CHECK_PTR( label );
|
---|
| 610 |
|
---|
| 611 | if ( (button2 && !button1) || (button1 && !button0) ) {
|
---|
| 612 | #if defined(QT_CHECK_RANGE)
|
---|
| 613 | qWarning( "QMessageBox: Inconsistent button parameters" );
|
---|
| 614 | #endif
|
---|
| 615 | button0 = button1 = button2 = 0;
|
---|
| 616 | }
|
---|
| 617 | mbd = new QMessageBoxData(this);
|
---|
| 618 | Q_CHECK_PTR( mbd );
|
---|
| 619 | mbd->icon = NoIcon;
|
---|
| 620 | mbd->iconLabel.setPixmap( QPixmap() );
|
---|
| 621 | mbd->numButtons = 0;
|
---|
| 622 | mbd->button[0] = button0;
|
---|
| 623 | mbd->button[1] = button1;
|
---|
| 624 | mbd->button[2] = button2;
|
---|
| 625 | mbd->defButton = -1;
|
---|
| 626 | mbd->escButton = -1;
|
---|
| 627 | int i;
|
---|
| 628 | for ( i=0; i<3; i++ ) {
|
---|
| 629 | int b = mbd->button[i];
|
---|
| 630 | if ( (b & Default) ) {
|
---|
| 631 | if ( mbd->defButton >= 0 ) {
|
---|
| 632 | #if defined(QT_CHECK_RANGE)
|
---|
| 633 | qWarning( "QMessageBox: There can be at most one "
|
---|
| 634 | "default button" );
|
---|
| 635 | #endif
|
---|
| 636 | } else {
|
---|
| 637 | mbd->defButton = i;
|
---|
| 638 | }
|
---|
| 639 | }
|
---|
| 640 | if ( (b & Escape) ) {
|
---|
| 641 | if ( mbd->escButton >= 0 ) {
|
---|
| 642 | #if defined(QT_CHECK_RANGE)
|
---|
| 643 | qWarning( "QMessageBox: There can be at most one "
|
---|
| 644 | "escape button" );
|
---|
| 645 | #endif
|
---|
| 646 | } else {
|
---|
| 647 | mbd->escButton = i;
|
---|
| 648 | }
|
---|
| 649 | }
|
---|
| 650 | b &= ButtonMask;
|
---|
| 651 | if ( b == 0 ) {
|
---|
| 652 | if ( i == 0 ) // no buttons, add an Ok button
|
---|
| 653 | b = Ok;
|
---|
| 654 | } else if ( b < 0 || b > LastButton ) {
|
---|
| 655 | #if defined(QT_CHECK_RANGE)
|
---|
| 656 | qWarning( "QMessageBox: Invalid button specifier" );
|
---|
| 657 | #endif
|
---|
| 658 | b = Ok;
|
---|
| 659 | } else {
|
---|
| 660 | if ( i > 0 && mbd->button[i-1] == 0 ) {
|
---|
| 661 | #if defined(QT_CHECK_RANGE)
|
---|
| 662 | qWarning( "QMessageBox: Inconsistent button parameters; "
|
---|
| 663 | "button %d defined but not button %d",
|
---|
| 664 | i+1, i );
|
---|
| 665 | #endif
|
---|
| 666 | b = 0;
|
---|
| 667 | }
|
---|
| 668 | }
|
---|
| 669 | mbd->button[i] = b;
|
---|
| 670 | if ( b )
|
---|
| 671 | mbd->numButtons++;
|
---|
| 672 | }
|
---|
| 673 | for ( i=0; i<3; i++ ) {
|
---|
| 674 | if ( i >= mbd->numButtons ) {
|
---|
| 675 | mbd->pb[i] = 0;
|
---|
| 676 | } else {
|
---|
| 677 | QCString buttonName;
|
---|
| 678 | buttonName.sprintf( "button%d", i+1 );
|
---|
| 679 | mbd->pb[i] = new QPushButton(
|
---|
| 680 | tr(mb_texts[mbd->button[i]]),
|
---|
| 681 | this, buttonName );
|
---|
| 682 | if ( mbd->defButton == i ) {
|
---|
| 683 | mbd->pb[i]->setDefault( TRUE );
|
---|
| 684 | mbd->pb[i]->setFocus();
|
---|
| 685 | }
|
---|
| 686 | mbd->pb[i]->setAutoDefault( TRUE );
|
---|
| 687 | mbd->pb[i]->setFocusPolicy( QWidget::StrongFocus );
|
---|
| 688 | connect( mbd->pb[i], SIGNAL(clicked()), SLOT(buttonClicked()) );
|
---|
| 689 | }
|
---|
| 690 | }
|
---|
| 691 | resizeButtons();
|
---|
| 692 | reserved1 = reserved2 = 0;
|
---|
| 693 | }
|
---|
| 694 |
|
---|
| 695 |
|
---|
| 696 | int QMessageBox::indexOf( int button ) const
|
---|
| 697 | {
|
---|
| 698 | int index = -1;
|
---|
| 699 | for ( int i=0; i<mbd->numButtons; i++ ) {
|
---|
| 700 | if ( mbd->button[i] == button ) {
|
---|
| 701 | index = i;
|
---|
| 702 | break;
|
---|
| 703 | }
|
---|
| 704 | }
|
---|
| 705 | return index;
|
---|
| 706 | }
|
---|
| 707 |
|
---|
| 708 |
|
---|
| 709 | void QMessageBox::resizeButtons()
|
---|
| 710 | {
|
---|
| 711 | int i;
|
---|
| 712 | QSize maxSize;
|
---|
| 713 | for ( i=0; i<mbd->numButtons; i++ ) {
|
---|
| 714 | QSize s = mbd->pb[i]->sizeHint();
|
---|
| 715 | maxSize.setWidth( QMAX(maxSize.width(), s.width()) );
|
---|
| 716 | maxSize.setHeight( QMAX(maxSize.height(),s.height()) );
|
---|
| 717 | }
|
---|
| 718 | mbd->buttonSize = maxSize;
|
---|
| 719 | for ( i=0; i<mbd->numButtons; i++ )
|
---|
| 720 | mbd->pb[i]->resize( maxSize );
|
---|
| 721 | }
|
---|
| 722 |
|
---|
| 723 |
|
---|
| 724 | /*!
|
---|
| 725 | \property QMessageBox::text
|
---|
| 726 | \brief the message box text to be displayed.
|
---|
| 727 |
|
---|
| 728 | The text will be interpreted either as a plain text or as rich
|
---|
| 729 | text, depending on the text format setting (\l
|
---|
| 730 | QMessageBox::textFormat). The default setting is \c AutoText, i.e.
|
---|
| 731 | the message box will try to auto-detect the format of the text.
|
---|
| 732 |
|
---|
| 733 | The default value of this property is QString::null.
|
---|
| 734 |
|
---|
| 735 | \sa textFormat
|
---|
| 736 | */
|
---|
| 737 | QString QMessageBox::text() const
|
---|
| 738 | {
|
---|
| 739 | return label->text();
|
---|
| 740 | }
|
---|
| 741 |
|
---|
| 742 |
|
---|
| 743 | void QMessageBox::setText( const QString &text )
|
---|
| 744 | {
|
---|
| 745 | label->setText( text );
|
---|
| 746 | }
|
---|
| 747 |
|
---|
| 748 |
|
---|
| 749 | /*!
|
---|
| 750 | \property QMessageBox::icon
|
---|
| 751 | \brief the message box's icon
|
---|
| 752 |
|
---|
| 753 | The icon of the message box can be one of the following predefined
|
---|
| 754 | icons:
|
---|
| 755 | \list
|
---|
| 756 | \i QMessageBox::NoIcon
|
---|
| 757 | \i QMessageBox::Question
|
---|
| 758 | \i QMessageBox::Information
|
---|
| 759 | \i QMessageBox::Warning
|
---|
| 760 | \i QMessageBox::Critical
|
---|
| 761 | \endlist
|
---|
| 762 |
|
---|
| 763 | The actual pixmap used for displaying the icon depends on the
|
---|
| 764 | current \link QWidget::style() GUI style\endlink. You can also set
|
---|
| 765 | a custom pixmap icon using the \l QMessageBox::iconPixmap
|
---|
| 766 | property. The default icon is QMessageBox::NoIcon.
|
---|
| 767 |
|
---|
| 768 | \sa iconPixmap
|
---|
| 769 | */
|
---|
| 770 |
|
---|
| 771 | QMessageBox::Icon QMessageBox::icon() const
|
---|
| 772 | {
|
---|
| 773 | return mbd->icon;
|
---|
| 774 | }
|
---|
| 775 |
|
---|
| 776 | void QMessageBox::setIcon( Icon icon )
|
---|
| 777 | {
|
---|
| 778 | setIconPixmap( standardIcon(icon) );
|
---|
| 779 | mbd->icon = icon;
|
---|
| 780 | }
|
---|
| 781 |
|
---|
| 782 | /*!
|
---|
| 783 | \obsolete
|
---|
| 784 |
|
---|
| 785 | Returns the pixmap used for a standard icon. This
|
---|
| 786 | allows the pixmaps to be used in more complex message boxes.
|
---|
| 787 | \a icon specifies the required icon, e.g. QMessageBox::Information,
|
---|
| 788 | QMessageBox::Warning or QMessageBox::Critical.
|
---|
| 789 |
|
---|
| 790 | \a style is unused.
|
---|
| 791 | */
|
---|
| 792 |
|
---|
| 793 | QPixmap QMessageBox::standardIcon( Icon icon, GUIStyle style)
|
---|
| 794 | {
|
---|
| 795 | Q_UNUSED(style);
|
---|
| 796 | return QMessageBox::standardIcon(icon);
|
---|
| 797 | }
|
---|
| 798 |
|
---|
| 799 |
|
---|
| 800 | /*!
|
---|
| 801 | Returns the pixmap used for a standard icon. This allows the
|
---|
| 802 | pixmaps to be used in more complex message boxes. \a icon
|
---|
| 803 | specifies the required icon, e.g. QMessageBox::Question,
|
---|
| 804 | QMessageBox::Information, QMessageBox::Warning or
|
---|
| 805 | QMessageBox::Critical.
|
---|
| 806 | */
|
---|
| 807 |
|
---|
| 808 | QPixmap QMessageBox::standardIcon( Icon icon )
|
---|
| 809 | {
|
---|
| 810 | QPixmap pm;
|
---|
| 811 | switch ( icon ) {
|
---|
| 812 | case Information:
|
---|
| 813 | pm = QApplication::style().stylePixmap( QStyle::SP_MessageBoxInformation );
|
---|
| 814 | break;
|
---|
| 815 | case Warning:
|
---|
| 816 | pm = QApplication::style().stylePixmap( QStyle::SP_MessageBoxWarning );
|
---|
| 817 | break;
|
---|
| 818 | case Critical:
|
---|
| 819 | pm = QApplication::style().stylePixmap( QStyle::SP_MessageBoxCritical );
|
---|
| 820 | break;
|
---|
| 821 | case Question:
|
---|
| 822 | pm = QApplication::style().stylePixmap( QStyle::SP_MessageBoxQuestion );
|
---|
| 823 | default:
|
---|
| 824 | break;
|
---|
| 825 | }
|
---|
| 826 | return pm;
|
---|
| 827 | }
|
---|
| 828 |
|
---|
| 829 |
|
---|
| 830 | /*!
|
---|
| 831 | \property QMessageBox::iconPixmap
|
---|
| 832 | \brief the current icon
|
---|
| 833 |
|
---|
| 834 | The icon currently used by the message box. Note that it's often
|
---|
| 835 | hard to draw one pixmap that looks appropriate in both Motif and
|
---|
| 836 | Windows GUI styles; you may want to draw two pixmaps.
|
---|
| 837 |
|
---|
| 838 | \sa icon
|
---|
| 839 | */
|
---|
| 840 |
|
---|
| 841 | const QPixmap *QMessageBox::iconPixmap() const
|
---|
| 842 | {
|
---|
| 843 | return mbd->iconLabel.pixmap();
|
---|
| 844 | }
|
---|
| 845 |
|
---|
| 846 |
|
---|
| 847 | void QMessageBox::setIconPixmap( const QPixmap &pixmap )
|
---|
| 848 | {
|
---|
| 849 | mbd->iconLabel.setPixmap(pixmap);
|
---|
| 850 | mbd->icon = NoIcon;
|
---|
| 851 | }
|
---|
| 852 |
|
---|
| 853 |
|
---|
| 854 | /*!
|
---|
| 855 | Returns the text of the message box button \a button, or
|
---|
| 856 | QString::null if the message box does not contain the button.
|
---|
| 857 |
|
---|
| 858 | \sa setButtonText()
|
---|
| 859 | */
|
---|
| 860 |
|
---|
| 861 | QString QMessageBox::buttonText( int button ) const
|
---|
| 862 | {
|
---|
| 863 | int index = indexOf(button);
|
---|
| 864 | return index >= 0 && mbd->pb[index]
|
---|
| 865 | ? mbd->pb[index]->text()
|
---|
| 866 | : QString::null;
|
---|
| 867 | }
|
---|
| 868 |
|
---|
| 869 |
|
---|
| 870 | /*!
|
---|
| 871 | Sets the text of the message box button \a button to \a text.
|
---|
| 872 | Setting the text of a button that is not in the message box is
|
---|
| 873 | silently ignored.
|
---|
| 874 |
|
---|
| 875 | \sa buttonText()
|
---|
| 876 | */
|
---|
| 877 |
|
---|
| 878 | void QMessageBox::setButtonText( int button, const QString &text )
|
---|
| 879 | {
|
---|
| 880 | int index = indexOf(button);
|
---|
| 881 | if ( index >= 0 && mbd->pb[index] ) {
|
---|
| 882 | mbd->pb[index]->setText( text );
|
---|
| 883 | resizeButtons();
|
---|
| 884 | }
|
---|
| 885 | }
|
---|
| 886 |
|
---|
| 887 |
|
---|
| 888 | /*!
|
---|
| 889 | \internal
|
---|
| 890 | Internal slot to handle button clicks.
|
---|
| 891 | */
|
---|
| 892 |
|
---|
| 893 | void QMessageBox::buttonClicked()
|
---|
| 894 | {
|
---|
| 895 | int reply = 0;
|
---|
| 896 | const QObject *s = sender();
|
---|
| 897 | for ( int i=0; i<mbd->numButtons; i++ ) {
|
---|
| 898 | if ( mbd->pb[i] == s )
|
---|
| 899 | reply = mbd->button[i];
|
---|
| 900 | }
|
---|
| 901 | done( reply );
|
---|
| 902 | }
|
---|
| 903 |
|
---|
| 904 |
|
---|
| 905 | /*!
|
---|
| 906 | Adjusts the size of the message box to fit the contents just before
|
---|
| 907 | QDialog::exec() or QDialog::show() is called.
|
---|
| 908 |
|
---|
| 909 | This function will not be called if the message box has been explicitly
|
---|
| 910 | resized before showing it.
|
---|
| 911 | */
|
---|
| 912 | void QMessageBox::adjustSize()
|
---|
| 913 | {
|
---|
| 914 | if ( !testWState(WState_Polished) )
|
---|
| 915 | polish();
|
---|
| 916 | resizeButtons();
|
---|
| 917 | label->adjustSize();
|
---|
| 918 | QSize labelSize( label->size() );
|
---|
| 919 | int n = mbd->numButtons;
|
---|
| 920 | int bw = mbd->buttonSize.width();
|
---|
| 921 | int bh = mbd->buttonSize.height();
|
---|
| 922 | int border = bh / 2 - style().pixelMetric(QStyle::PM_ButtonDefaultIndicator);
|
---|
| 923 | if ( border <= 0 )
|
---|
| 924 | border = 10;
|
---|
| 925 | int btn_spacing = 7;
|
---|
| 926 | if ( style().styleHint(QStyle::SH_GUIStyle) == MotifStyle )
|
---|
| 927 | btn_spacing = border;
|
---|
| 928 | #ifndef Q_OS_TEMP
|
---|
| 929 | int buttons = mbd->numButtons * bw + (n-1) * btn_spacing;
|
---|
| 930 | int h = bh;
|
---|
| 931 | #else
|
---|
| 932 | int visibleButtons = 0;
|
---|
| 933 | for ( int i = 0; i < mbd->numButtons; ++i )
|
---|
| 934 | visibleButtons += mbd->pb[i]->isVisible() ? 1 : 0;
|
---|
| 935 | int buttons = visibleButtons == 0 ? 0 : visibleButtons * bw + (visibleButtons-1) * btn_spacing;
|
---|
| 936 | int h = visibleButtons == 0 ? 0 : bh;
|
---|
| 937 | n = visibleButtons;
|
---|
| 938 | #endif
|
---|
| 939 | if ( labelSize.height() )
|
---|
| 940 | h += labelSize.height() + 3*border;
|
---|
| 941 | else
|
---|
| 942 | h += 2*border;
|
---|
| 943 | int lmargin = 0;
|
---|
| 944 | if ( mbd->iconLabel.pixmap() && mbd->iconLabel.pixmap()->width() ) {
|
---|
| 945 | mbd->iconLabel.adjustSize();
|
---|
| 946 | lmargin += mbd->iconLabel.width() + border;
|
---|
| 947 | if ( h < mbd->iconLabel.height() + 3*border + bh && n )
|
---|
| 948 | h = mbd->iconLabel.height() + 3*border + bh;
|
---|
| 949 | }
|
---|
| 950 | int w = QMAX( buttons, labelSize.width() + lmargin ) + 2*border;
|
---|
| 951 | QRect screen = QApplication::desktop()->screenGeometry( pos() );
|
---|
| 952 | if ( w > screen.width() )
|
---|
| 953 | w = screen.width();
|
---|
| 954 | resize( w, h );
|
---|
| 955 | setMinimumSize( size() );
|
---|
| 956 | }
|
---|
| 957 |
|
---|
| 958 |
|
---|
| 959 | /*!\reimp
|
---|
| 960 | */
|
---|
| 961 | void QMessageBox::resizeEvent( QResizeEvent * )
|
---|
| 962 | {
|
---|
| 963 | int i;
|
---|
| 964 | int n = mbd->numButtons;
|
---|
| 965 | int bw = mbd->buttonSize.width();
|
---|
| 966 | int bh = mbd->buttonSize.height();
|
---|
| 967 | #ifdef Q_OS_TEMP
|
---|
| 968 | int visibleButtons = 0;
|
---|
| 969 | for ( i = 0; i < n; ++i )
|
---|
| 970 | visibleButtons += mbd->pb[i]->isVisible() ? 1 : 0;
|
---|
| 971 | n = visibleButtons;
|
---|
| 972 | bw = visibleButtons == 0 ? 0 : bw;
|
---|
| 973 | bh = visibleButtons == 0 ? 0 : bh;
|
---|
| 974 | #endif
|
---|
| 975 | int border = bh / 2 - style().pixelMetric(QStyle::PM_ButtonDefaultIndicator);
|
---|
| 976 | if ( border <= 0 )
|
---|
| 977 | border = 10;
|
---|
| 978 | int btn_spacing = 7;
|
---|
| 979 | if ( style().styleHint(QStyle::SH_GUIStyle) == MotifStyle )
|
---|
| 980 | btn_spacing = border;
|
---|
| 981 | int lmargin = 0;
|
---|
| 982 | mbd->iconLabel.adjustSize();
|
---|
| 983 | mbd->iconLabel.move( border, border );
|
---|
| 984 | if ( mbd->iconLabel.pixmap() && mbd->iconLabel.pixmap()->width() )
|
---|
| 985 | lmargin += mbd->iconLabel.width() + border;
|
---|
| 986 | label->setGeometry( lmargin+border,
|
---|
| 987 | border,
|
---|
| 988 | width() - lmargin -2*border,
|
---|
| 989 | height() - 3*border - bh );
|
---|
| 990 | int extra_space = (width() - bw*n - 2*border - (n-1)*btn_spacing);
|
---|
| 991 | if ( style().styleHint(QStyle::SH_GUIStyle) == MotifStyle )
|
---|
| 992 | for ( i=0; i<n; i++ )
|
---|
| 993 | mbd->pb[i]->move( border + i*bw + i*btn_spacing + extra_space*(i+1)/(n+1),
|
---|
| 994 | height() - border - bh );
|
---|
| 995 | else
|
---|
| 996 | for ( i=0; i<n; i++ )
|
---|
| 997 | mbd->pb[i]->move( border + i*bw + extra_space/2 + i*btn_spacing,
|
---|
| 998 | height() - border - bh );
|
---|
| 999 | }
|
---|
| 1000 |
|
---|
| 1001 |
|
---|
| 1002 | /*!\reimp
|
---|
| 1003 | */
|
---|
| 1004 | void QMessageBox::keyPressEvent( QKeyEvent *e )
|
---|
| 1005 | {
|
---|
| 1006 | if ( e->key() == Key_Escape ) {
|
---|
| 1007 | if ( mbd->escButton >= 0 ) {
|
---|
| 1008 | QPushButton *pb = mbd->pb[mbd->escButton];
|
---|
| 1009 | pb->animateClick();
|
---|
| 1010 | e->accept();
|
---|
| 1011 | return;
|
---|
| 1012 | }
|
---|
| 1013 | }
|
---|
| 1014 | #ifndef QT_NO_ACCEL
|
---|
| 1015 | if ( !( e->state() & AltButton ) ) {
|
---|
| 1016 | QObjectList *list = queryList( "QPushButton" );
|
---|
| 1017 | QObjectListIt it( *list );
|
---|
| 1018 | QPushButton *pb;
|
---|
| 1019 | while ( (pb = (QPushButton*)it.current()) ) {
|
---|
| 1020 | int key = e->key() & ~(MODIFIER_MASK|UNICODE_ACCEL);
|
---|
| 1021 | int acc = pb->accel() & ~(MODIFIER_MASK|UNICODE_ACCEL);
|
---|
| 1022 | if ( key && acc && acc == key ) {
|
---|
| 1023 | delete list;
|
---|
| 1024 | emit pb->animateClick();
|
---|
| 1025 | return;
|
---|
| 1026 | }
|
---|
| 1027 | ++it;
|
---|
| 1028 | }
|
---|
| 1029 | delete list;
|
---|
| 1030 | }
|
---|
| 1031 | #endif
|
---|
| 1032 | QDialog::keyPressEvent( e );
|
---|
| 1033 | }
|
---|
| 1034 |
|
---|
| 1035 | /*!\reimp
|
---|
| 1036 | */
|
---|
| 1037 | void QMessageBox::showEvent( QShowEvent *e )
|
---|
| 1038 | {
|
---|
| 1039 | #if defined(QT_ACCESSIBILITY_SUPPORT)
|
---|
| 1040 | QAccessible::updateAccessibility( this, 0, QAccessible::Alert );
|
---|
[8] | 1041 | #else
|
---|
| 1042 | #if defined(Q_WS_PM)
|
---|
| 1043 | //@@TODO (dmik): currently, the accessibility support is not implemented
|
---|
| 1044 | // on OS/2, but we still need to play a sound when QMessageBox is opened.
|
---|
[21] | 1045 | ULONG alarmStyle = 0;
|
---|
[8] | 1046 | bool playAlarm = TRUE;
|
---|
| 1047 | switch ( icon() ) {
|
---|
| 1048 | case Warning:
|
---|
| 1049 | alarmStyle = WA_WARNING;
|
---|
| 1050 | break;
|
---|
| 1051 | case Critical:
|
---|
| 1052 | alarmStyle = WA_ERROR;
|
---|
| 1053 | break;
|
---|
| 1054 | case Information:
|
---|
| 1055 | alarmStyle = WA_NOTE;
|
---|
| 1056 | break;
|
---|
| 1057 | default:
|
---|
| 1058 | playAlarm = FALSE;
|
---|
| 1059 | break;
|
---|
| 1060 | }
|
---|
| 1061 | if ( playAlarm )
|
---|
| 1062 | WinAlarm( HWND_DESKTOP, alarmStyle );
|
---|
[2] | 1063 | #endif
|
---|
[8] | 1064 | #endif
|
---|
[2] | 1065 | QDialog::showEvent( e );
|
---|
| 1066 | }
|
---|
| 1067 |
|
---|
| 1068 | /*!\reimp
|
---|
| 1069 | */
|
---|
| 1070 | void QMessageBox::closeEvent( QCloseEvent *e )
|
---|
| 1071 | {
|
---|
| 1072 | QDialog::closeEvent( e );
|
---|
| 1073 | if ( mbd->escButton != -1 )
|
---|
| 1074 | setResult( mbd->button[mbd->escButton] );
|
---|
| 1075 | }
|
---|
| 1076 |
|
---|
| 1077 | /*****************************************************************************
|
---|
| 1078 | Static QMessageBox functions
|
---|
| 1079 | *****************************************************************************/
|
---|
| 1080 |
|
---|
| 1081 | /*!\fn int QMessageBox::message( const QString &,const QString&,const QString&,QWidget*,const char * )
|
---|
| 1082 | \obsolete
|
---|
| 1083 | Opens a modal message box directly using the specified parameters.
|
---|
| 1084 |
|
---|
| 1085 | Please use information(), warning(), question(), or critical() instead.
|
---|
| 1086 | */
|
---|
| 1087 |
|
---|
| 1088 | /*! \fn bool QMessageBox::query( const QString &,const QString&,const QString&,const QString&,QWidget *, const char * )
|
---|
| 1089 | \obsolete
|
---|
| 1090 | Queries the user using a modal message box with two buttons.
|
---|
| 1091 | Note that \a caption is not always shown, it depends on the window manager.
|
---|
| 1092 |
|
---|
| 1093 | Please use information(), question(), warning(), or critical() instead.
|
---|
| 1094 | */
|
---|
| 1095 |
|
---|
| 1096 | /*!
|
---|
| 1097 | Opens an information message box with the caption \a caption and
|
---|
| 1098 | the text \a text. The dialog may have up to three buttons. Each of
|
---|
| 1099 | the buttons, \a button0, \a button1 and \a button2 may be set to
|
---|
| 1100 | one of the following values:
|
---|
| 1101 |
|
---|
| 1102 | \list
|
---|
| 1103 | \i QMessageBox::NoButton
|
---|
| 1104 | \i QMessageBox::Ok
|
---|
| 1105 | \i QMessageBox::Cancel
|
---|
| 1106 | \i QMessageBox::Yes
|
---|
| 1107 | \i QMessageBox::No
|
---|
| 1108 | \i QMessageBox::Abort
|
---|
| 1109 | \i QMessageBox::Retry
|
---|
| 1110 | \i QMessageBox::Ignore
|
---|
| 1111 | \i QMessageBox::YesAll
|
---|
| 1112 | \i QMessageBox::NoAll
|
---|
| 1113 | \endlist
|
---|
| 1114 |
|
---|
| 1115 | If you don't want all three buttons, set the last button, or last
|
---|
| 1116 | two buttons to QMessageBox::NoButton.
|
---|
| 1117 |
|
---|
| 1118 | One button can be OR-ed with \c QMessageBox::Default, and one
|
---|
| 1119 | button can be OR-ed with \c QMessageBox::Escape.
|
---|
| 1120 |
|
---|
| 1121 | Returns the identity (QMessageBox::Ok, or QMessageBox::No, etc.)
|
---|
| 1122 | of the button that was clicked.
|
---|
| 1123 |
|
---|
| 1124 | If \a parent is 0, the message box becomes an application-global
|
---|
| 1125 | modal dialog box. If \a parent is a widget, the message box
|
---|
| 1126 | becomes modal relative to \a parent.
|
---|
| 1127 |
|
---|
| 1128 | \sa question(), warning(), critical()
|
---|
| 1129 | */
|
---|
| 1130 |
|
---|
| 1131 | int QMessageBox::information( QWidget *parent,
|
---|
| 1132 | const QString& caption, const QString& text,
|
---|
| 1133 | int button0, int button1, int button2 )
|
---|
| 1134 | {
|
---|
| 1135 | QMessageBox *mb = new QMessageBox( caption, text, Information,
|
---|
| 1136 | button0, button1, button2,
|
---|
| 1137 | parent, "qt_msgbox_information", TRUE,
|
---|
| 1138 | WDestructiveClose);
|
---|
| 1139 | Q_CHECK_PTR( mb );
|
---|
| 1140 | return mb->exec();
|
---|
| 1141 | }
|
---|
| 1142 |
|
---|
| 1143 | /*!
|
---|
| 1144 | Opens a question message box with the caption \a caption and the
|
---|
| 1145 | text \a text. The dialog may have up to three buttons. Each of the
|
---|
| 1146 | buttons, \a button0, \a button1 and \a button2 may be set to one
|
---|
| 1147 | of the following values:
|
---|
| 1148 |
|
---|
| 1149 | \list
|
---|
| 1150 | \i QMessageBox::NoButton
|
---|
| 1151 | \i QMessageBox::Ok
|
---|
| 1152 | \i QMessageBox::Cancel
|
---|
| 1153 | \i QMessageBox::Yes
|
---|
| 1154 | \i QMessageBox::No
|
---|
| 1155 | \i QMessageBox::Abort
|
---|
| 1156 | \i QMessageBox::Retry
|
---|
| 1157 | \i QMessageBox::Ignore
|
---|
| 1158 | \i QMessageBox::YesAll
|
---|
| 1159 | \i QMessageBox::NoAll
|
---|
| 1160 | \endlist
|
---|
| 1161 |
|
---|
| 1162 | If you don't want all three buttons, set the last button, or last
|
---|
| 1163 | two buttons to QMessageBox::NoButton.
|
---|
| 1164 |
|
---|
| 1165 | One button can be OR-ed with \c QMessageBox::Default, and one
|
---|
| 1166 | button can be OR-ed with \c QMessageBox::Escape.
|
---|
| 1167 |
|
---|
| 1168 | Returns the identity (QMessageBox::Yes, or QMessageBox::No, etc.)
|
---|
| 1169 | of the button that was clicked.
|
---|
| 1170 |
|
---|
| 1171 | If \a parent is 0, the message box becomes an application-global
|
---|
| 1172 | modal dialog box. If \a parent is a widget, the message box
|
---|
| 1173 | becomes modal relative to \a parent.
|
---|
| 1174 |
|
---|
| 1175 | \sa information(), warning(), critical()
|
---|
| 1176 | */
|
---|
| 1177 |
|
---|
| 1178 | int QMessageBox::question( QWidget *parent,
|
---|
| 1179 | const QString& caption, const QString& text,
|
---|
| 1180 | int button0, int button1, int button2 )
|
---|
| 1181 | {
|
---|
| 1182 | QMessageBox *mb = new QMessageBox( caption, text, Question,
|
---|
| 1183 | button0, button1, button2,
|
---|
| 1184 | parent, "qt_msgbox_information", TRUE,
|
---|
| 1185 | WDestructiveClose);
|
---|
| 1186 | Q_CHECK_PTR( mb );
|
---|
| 1187 | return mb->exec();
|
---|
| 1188 | }
|
---|
| 1189 |
|
---|
| 1190 |
|
---|
| 1191 | /*!
|
---|
| 1192 | Opens a warning message box with the caption \a caption and the
|
---|
| 1193 | text \a text. The dialog may have up to three buttons. Each of the
|
---|
| 1194 | button parameters, \a button0, \a button1 and \a button2 may be
|
---|
| 1195 | set to one of the following values:
|
---|
| 1196 |
|
---|
| 1197 | \list
|
---|
| 1198 | \i QMessageBox::NoButton
|
---|
| 1199 | \i QMessageBox::Ok
|
---|
| 1200 | \i QMessageBox::Cancel
|
---|
| 1201 | \i QMessageBox::Yes
|
---|
| 1202 | \i QMessageBox::No
|
---|
| 1203 | \i QMessageBox::Abort
|
---|
| 1204 | \i QMessageBox::Retry
|
---|
| 1205 | \i QMessageBox::Ignore
|
---|
| 1206 | \i QMessageBox::YesAll
|
---|
| 1207 | \i QMessageBox::NoAll
|
---|
| 1208 | \endlist
|
---|
| 1209 |
|
---|
| 1210 | If you don't want all three buttons, set the last button, or last
|
---|
| 1211 | two buttons to QMessageBox::NoButton.
|
---|
| 1212 |
|
---|
| 1213 | One button can be OR-ed with \c QMessageBox::Default, and one
|
---|
| 1214 | button can be OR-ed with \c QMessageBox::Escape.
|
---|
| 1215 |
|
---|
| 1216 | Returns the identity (QMessageBox::Ok, or QMessageBox::No, etc.)
|
---|
| 1217 | of the button that was clicked.
|
---|
| 1218 |
|
---|
| 1219 | If \a parent is 0, the message box becomes an application-global
|
---|
| 1220 | modal dialog box. If \a parent is a widget, the message box
|
---|
| 1221 | becomes modal relative to \a parent.
|
---|
| 1222 |
|
---|
| 1223 | \sa information(), question(), critical()
|
---|
| 1224 | */
|
---|
| 1225 |
|
---|
| 1226 | int QMessageBox::warning( QWidget *parent,
|
---|
| 1227 | const QString& caption, const QString& text,
|
---|
| 1228 | int button0, int button1, int button2 )
|
---|
| 1229 | {
|
---|
| 1230 | QMessageBox *mb = new QMessageBox( caption, text, Warning,
|
---|
| 1231 | button0, button1, button2,
|
---|
| 1232 | parent, "qt_msgbox_warning", TRUE,
|
---|
| 1233 | WDestructiveClose);
|
---|
| 1234 | Q_CHECK_PTR( mb );
|
---|
| 1235 | return mb->exec();
|
---|
| 1236 | }
|
---|
| 1237 |
|
---|
| 1238 |
|
---|
| 1239 | /*!
|
---|
| 1240 | Opens a critical message box with the caption \a caption and the
|
---|
| 1241 | text \a text. The dialog may have up to three buttons. Each of the
|
---|
| 1242 | button parameters, \a button0, \a button1 and \a button2 may be
|
---|
| 1243 | set to one of the following values:
|
---|
| 1244 |
|
---|
| 1245 | \list
|
---|
| 1246 | \i QMessageBox::NoButton
|
---|
| 1247 | \i QMessageBox::Ok
|
---|
| 1248 | \i QMessageBox::Cancel
|
---|
| 1249 | \i QMessageBox::Yes
|
---|
| 1250 | \i QMessageBox::No
|
---|
| 1251 | \i QMessageBox::Abort
|
---|
| 1252 | \i QMessageBox::Retry
|
---|
| 1253 | \i QMessageBox::Ignore
|
---|
| 1254 | \i QMessageBox::YesAll
|
---|
| 1255 | \i QMessageBox::NoAll
|
---|
| 1256 | \endlist
|
---|
| 1257 |
|
---|
| 1258 | If you don't want all three buttons, set the last button, or last
|
---|
| 1259 | two buttons to QMessageBox::NoButton.
|
---|
| 1260 |
|
---|
| 1261 | One button can be OR-ed with \c QMessageBox::Default, and one
|
---|
| 1262 | button can be OR-ed with \c QMessageBox::Escape.
|
---|
| 1263 |
|
---|
| 1264 | Returns the identity (QMessageBox::Ok, or QMessageBox::No, etc.)
|
---|
| 1265 | of the button that was clicked.
|
---|
| 1266 |
|
---|
| 1267 | If \a parent is 0, the message box becomes an application-global
|
---|
| 1268 | modal dialog box. If \a parent is a widget, the message box
|
---|
| 1269 | becomes modal relative to \a parent.
|
---|
| 1270 |
|
---|
| 1271 | \sa information(), question(), warning()
|
---|
| 1272 | */
|
---|
| 1273 |
|
---|
| 1274 | int QMessageBox::critical( QWidget *parent,
|
---|
| 1275 | const QString& caption, const QString& text,
|
---|
| 1276 | int button0, int button1, int button2 )
|
---|
| 1277 | {
|
---|
| 1278 | QMessageBox *mb = new QMessageBox( caption, text, Critical,
|
---|
| 1279 | button0, button1, button2,
|
---|
| 1280 | parent, "qt_msgbox_critical", TRUE,
|
---|
| 1281 | WDestructiveClose);
|
---|
| 1282 | Q_CHECK_PTR( mb );
|
---|
| 1283 | return mb->exec();
|
---|
| 1284 | }
|
---|
| 1285 |
|
---|
| 1286 |
|
---|
| 1287 | /*!
|
---|
| 1288 | Displays a simple about box with caption \a caption and text \a
|
---|
| 1289 | text. The about box's parent is \a parent.
|
---|
| 1290 |
|
---|
| 1291 | about() looks for a suitable icon in four locations:
|
---|
| 1292 | \list 1
|
---|
| 1293 | \i It prefers \link QWidget::icon() parent->icon() \endlink if that exists.
|
---|
| 1294 | \i If not, it tries the top-level widget containing \a parent.
|
---|
| 1295 | \i If that fails, it tries the \link
|
---|
| 1296 | QApplication::mainWidget() main widget. \endlink
|
---|
| 1297 | \i As a last resort it uses the Information icon.
|
---|
| 1298 | \endlist
|
---|
| 1299 |
|
---|
| 1300 | The about box has a single button labelled "OK".
|
---|
| 1301 |
|
---|
| 1302 | \sa QWidget::icon() QApplication::mainWidget()
|
---|
| 1303 | */
|
---|
| 1304 |
|
---|
| 1305 | void QMessageBox::about( QWidget *parent, const QString &caption,
|
---|
| 1306 | const QString& text )
|
---|
| 1307 | {
|
---|
| 1308 | QMessageBox *mb = new QMessageBox( caption, text,
|
---|
| 1309 | Information,
|
---|
| 1310 | Ok + Default, 0, 0,
|
---|
| 1311 | parent, "qt_msgbox_simple_about_box", TRUE,
|
---|
| 1312 | WDestructiveClose);
|
---|
| 1313 | Q_CHECK_PTR( mb );
|
---|
| 1314 | #ifndef QT_NO_WIDGET_TOPEXTRA
|
---|
| 1315 | const QPixmap *pm = parent ? parent->icon() : 0;
|
---|
| 1316 | if ( pm && !pm->isNull() )
|
---|
| 1317 | mb->setIconPixmap( *pm );
|
---|
| 1318 | else {
|
---|
| 1319 | pm = parent ? parent->topLevelWidget()->icon() : 0;
|
---|
| 1320 | if ( pm && !pm->isNull() )
|
---|
| 1321 | mb->setIconPixmap( *pm );
|
---|
| 1322 | else {
|
---|
| 1323 | pm = qApp && qApp->mainWidget() ? qApp->mainWidget()->icon() : 0;
|
---|
| 1324 | if ( pm && !pm->isNull() )
|
---|
| 1325 | mb->setIconPixmap( *pm );
|
---|
| 1326 | }
|
---|
| 1327 | }
|
---|
| 1328 | #endif
|
---|
| 1329 | mb->exec();
|
---|
| 1330 | }
|
---|
| 1331 |
|
---|
| 1332 |
|
---|
| 1333 | /*! \reimp
|
---|
| 1334 | */
|
---|
| 1335 |
|
---|
| 1336 | void QMessageBox::styleChanged( QStyle& )
|
---|
| 1337 | {
|
---|
| 1338 | if ( mbd->icon != NoIcon ) {
|
---|
| 1339 | // Reload icon for new style
|
---|
| 1340 | setIcon( mbd->icon );
|
---|
| 1341 | }
|
---|
| 1342 | }
|
---|
| 1343 |
|
---|
| 1344 |
|
---|
| 1345 | static int textBox( QWidget *parent, QMessageBox::Icon severity,
|
---|
| 1346 | const QString& caption, const QString& text,
|
---|
| 1347 | const QString& button0Text,
|
---|
| 1348 | const QString& button1Text,
|
---|
| 1349 | const QString& button2Text,
|
---|
| 1350 | int defaultButtonNumber,
|
---|
| 1351 | int escapeButtonNumber )
|
---|
| 1352 | {
|
---|
| 1353 | int b[3];
|
---|
| 1354 | b[0] = 1;
|
---|
| 1355 | b[1] = button1Text.isEmpty() ? 0 : 2;
|
---|
| 1356 | b[2] = button2Text.isEmpty() ? 0 : 3;
|
---|
| 1357 |
|
---|
| 1358 | int i;
|
---|
| 1359 | for( i=0; i<3; i++ ) {
|
---|
| 1360 | if ( b[i] && defaultButtonNumber == i )
|
---|
| 1361 | b[i] += QMessageBox::Default;
|
---|
| 1362 | if ( b[i] && escapeButtonNumber == i )
|
---|
| 1363 | b[i] += QMessageBox::Escape;
|
---|
| 1364 | }
|
---|
| 1365 |
|
---|
| 1366 | QMessageBox *mb = new QMessageBox( caption, text, severity,
|
---|
| 1367 | b[0], b[1], b[2],
|
---|
| 1368 | parent, "qt_msgbox_information", TRUE,
|
---|
| 1369 | Qt::WDestructiveClose);
|
---|
| 1370 | Q_CHECK_PTR( mb );
|
---|
| 1371 | if ( button0Text.isEmpty() )
|
---|
| 1372 | mb->setButtonText( 1, QMessageBox::tr(mb_texts[QMessageBox::Ok]) );
|
---|
| 1373 | else
|
---|
| 1374 | mb->setButtonText( 1, button0Text );
|
---|
| 1375 | if ( b[1] )
|
---|
| 1376 | mb->setButtonText( 2, button1Text );
|
---|
| 1377 | if ( b[2] )
|
---|
| 1378 | mb->setButtonText( 3, button2Text );
|
---|
| 1379 |
|
---|
| 1380 | #ifndef QT_NO_CURSOR
|
---|
| 1381 | mb->setCursor( Qt::arrowCursor );
|
---|
| 1382 | #endif
|
---|
| 1383 | return mb->exec() - 1;
|
---|
| 1384 | }
|
---|
| 1385 |
|
---|
| 1386 |
|
---|
| 1387 | /*!
|
---|
| 1388 | \overload
|
---|
| 1389 |
|
---|
| 1390 | Displays an information message box with caption \a caption, text
|
---|
| 1391 | \a text and one, two or three buttons. Returns the index of the
|
---|
| 1392 | button that was clicked (0, 1 or 2).
|
---|
| 1393 |
|
---|
| 1394 | \a button0Text is the text of the first button, and is optional.
|
---|
| 1395 | If \a button0Text is not supplied, "OK" (translated) will be used.
|
---|
| 1396 | \a button1Text is the text of the second button, and is optional.
|
---|
| 1397 | \a button2Text is the text of the third button, and is optional.
|
---|
| 1398 | \a defaultButtonNumber (0, 1 or 2) is the index of the default
|
---|
| 1399 | button; pressing Return or Enter is the same as clicking the
|
---|
| 1400 | default button. It defaults to 0 (the first button). \a
|
---|
| 1401 | escapeButtonNumber is the index of the Escape button; pressing
|
---|
| 1402 | Escape is the same as clicking this button. It defaults to -1;
|
---|
| 1403 | supply 0, 1 or 2 to make pressing Escape equivalent to clicking
|
---|
| 1404 | the relevant button.
|
---|
| 1405 |
|
---|
| 1406 | If \a parent is 0, the message box becomes an application-global
|
---|
| 1407 | modal dialog box. If \a parent is a widget, the message box
|
---|
| 1408 | becomes modal relative to \a parent.
|
---|
| 1409 |
|
---|
| 1410 | Note: If you do not specify an Escape button then if the Escape
|
---|
| 1411 | button is pressed then -1 will be returned. It is suggested that
|
---|
| 1412 | you specify an Escape button to prevent this from happening.
|
---|
| 1413 |
|
---|
| 1414 | \sa question(), warning(), critical()
|
---|
| 1415 | */
|
---|
| 1416 |
|
---|
| 1417 | int QMessageBox::information( QWidget *parent, const QString &caption,
|
---|
| 1418 | const QString& text,
|
---|
| 1419 | const QString& button0Text,
|
---|
| 1420 | const QString& button1Text,
|
---|
| 1421 | const QString& button2Text,
|
---|
| 1422 | int defaultButtonNumber,
|
---|
| 1423 | int escapeButtonNumber )
|
---|
| 1424 | {
|
---|
| 1425 | return textBox( parent, Information, caption, text,
|
---|
| 1426 | button0Text, button1Text, button2Text,
|
---|
| 1427 | defaultButtonNumber, escapeButtonNumber );
|
---|
| 1428 | }
|
---|
| 1429 |
|
---|
| 1430 | /*!
|
---|
| 1431 | \overload
|
---|
| 1432 |
|
---|
| 1433 | Displays a question message box with caption \a caption, text \a
|
---|
| 1434 | text and one, two or three buttons. Returns the index of the
|
---|
| 1435 | button that was clicked (0, 1 or 2).
|
---|
| 1436 |
|
---|
| 1437 | \a button0Text is the text of the first button, and is optional.
|
---|
| 1438 | If \a button0Text is not supplied, "OK" (translated) will be used.
|
---|
| 1439 | \a button1Text is the text of the second button, and is optional.
|
---|
| 1440 | \a button2Text is the text of the third button, and is optional.
|
---|
| 1441 | \a defaultButtonNumber (0, 1 or 2) is the index of the default
|
---|
| 1442 | button; pressing Return or Enter is the same as clicking the
|
---|
| 1443 | default button. It defaults to 0 (the first button). \a
|
---|
| 1444 | escapeButtonNumber is the index of the Escape button; pressing
|
---|
| 1445 | Escape is the same as clicking this button. It defaults to -1;
|
---|
| 1446 | supply 0, 1 or 2 to make pressing Escape equivalent to clicking
|
---|
| 1447 | the relevant button.
|
---|
| 1448 |
|
---|
| 1449 | If \a parent is 0, the message box becomes an application-global
|
---|
| 1450 | modal dialog box. If \a parent is a widget, the message box
|
---|
| 1451 | becomes modal relative to \a parent.
|
---|
| 1452 |
|
---|
| 1453 | Note: If you do not specify an Escape button then if the Escape
|
---|
| 1454 | button is pressed then -1 will be returned. It is suggested that
|
---|
| 1455 | you specify an Escape button to prevent this from happening.
|
---|
| 1456 |
|
---|
| 1457 | \sa information(), warning(), critical()
|
---|
| 1458 | */
|
---|
| 1459 | int QMessageBox::question( QWidget *parent, const QString &caption,
|
---|
| 1460 | const QString& text,
|
---|
| 1461 | const QString& button0Text,
|
---|
| 1462 | const QString& button1Text,
|
---|
| 1463 | const QString& button2Text,
|
---|
| 1464 | int defaultButtonNumber,
|
---|
| 1465 | int escapeButtonNumber )
|
---|
| 1466 | {
|
---|
| 1467 | return textBox( parent, Question, caption, text,
|
---|
| 1468 | button0Text, button1Text, button2Text,
|
---|
| 1469 | defaultButtonNumber, escapeButtonNumber );
|
---|
| 1470 | }
|
---|
| 1471 |
|
---|
| 1472 |
|
---|
| 1473 | /*!
|
---|
| 1474 | \overload
|
---|
| 1475 |
|
---|
| 1476 | Displays a warning message box with a caption, a text, and 1, 2 or
|
---|
| 1477 | 3 buttons. Returns the number of the button that was clicked (0,
|
---|
| 1478 | 1, or 2).
|
---|
| 1479 |
|
---|
| 1480 | \a button0Text is the text of the first button, and is optional.
|
---|
| 1481 | If \a button0Text is not supplied, "OK" (translated) will be used.
|
---|
| 1482 | \a button1Text is the text of the second button, and is optional,
|
---|
| 1483 | and \a button2Text is the text of the third button, and is
|
---|
| 1484 | optional. \a defaultButtonNumber (0, 1 or 2) is the index of the
|
---|
| 1485 | default button; pressing Return or Enter is the same as clicking
|
---|
| 1486 | the default button. It defaults to 0 (the first button). \a
|
---|
| 1487 | escapeButtonNumber is the index of the Escape button; pressing
|
---|
| 1488 | Escape is the same as clicking this button. It defaults to -1;
|
---|
| 1489 | supply 0, 1, or 2 to make pressing Escape equivalent to clicking
|
---|
| 1490 | the relevant button.
|
---|
| 1491 |
|
---|
| 1492 | If \a parent is 0, the message box becomes an application-global
|
---|
| 1493 | modal dialog box. If \a parent is a widget, the message box
|
---|
| 1494 | becomes modal relative to \a parent.
|
---|
| 1495 |
|
---|
| 1496 | Note: If you do not specify an Escape button then if the Escape
|
---|
| 1497 | button is pressed then -1 will be returned. It is suggested that
|
---|
| 1498 | you specify an Escape button to prevent this from happening.
|
---|
| 1499 |
|
---|
| 1500 | \sa information(), question(), critical()
|
---|
| 1501 | */
|
---|
| 1502 |
|
---|
| 1503 | int QMessageBox::warning( QWidget *parent, const QString &caption,
|
---|
| 1504 | const QString& text,
|
---|
| 1505 | const QString& button0Text,
|
---|
| 1506 | const QString& button1Text,
|
---|
| 1507 | const QString& button2Text,
|
---|
| 1508 | int defaultButtonNumber,
|
---|
| 1509 | int escapeButtonNumber )
|
---|
| 1510 | {
|
---|
| 1511 | return textBox( parent, Warning, caption, text,
|
---|
| 1512 | button0Text, button1Text, button2Text,
|
---|
| 1513 | defaultButtonNumber, escapeButtonNumber );
|
---|
| 1514 | }
|
---|
| 1515 |
|
---|
| 1516 |
|
---|
| 1517 | /*!
|
---|
| 1518 | \overload
|
---|
| 1519 |
|
---|
| 1520 | Displays a critical error message box with a caption, a text, and
|
---|
| 1521 | 1, 2 or 3 buttons. Returns the number of the button that was
|
---|
| 1522 | clicked (0, 1 or 2).
|
---|
| 1523 |
|
---|
| 1524 | \a button0Text is the text of the first button, and is optional.
|
---|
| 1525 | If \a button0Text is not supplied, "OK" (translated) will be used.
|
---|
| 1526 | \a button1Text is the text of the second button, and is optional,
|
---|
| 1527 | and \a button2Text is the text of the third button, and is
|
---|
| 1528 | optional. \a defaultButtonNumber (0, 1 or 2) is the index of the
|
---|
| 1529 | default button; pressing Return or Enter is the same as clicking
|
---|
| 1530 | the default button. It defaults to 0 (the first button). \a
|
---|
| 1531 | escapeButtonNumber is the index of the Escape button; pressing
|
---|
| 1532 | Escape is the same as clicking this button. It defaults to -1;
|
---|
| 1533 | supply 0, 1, or 2 to make pressing Escape equivalent to clicking
|
---|
| 1534 | the relevant button.
|
---|
| 1535 |
|
---|
| 1536 | If \a parent is 0, the message box becomes an application-global
|
---|
| 1537 | modal dialog box. If \a parent is a widget, the message box
|
---|
| 1538 | becomes modal relative to \a parent.
|
---|
| 1539 |
|
---|
| 1540 | \sa information(), question(), warning()
|
---|
| 1541 | */
|
---|
| 1542 |
|
---|
| 1543 | int QMessageBox::critical( QWidget *parent, const QString &caption,
|
---|
| 1544 | const QString& text,
|
---|
| 1545 | const QString& button0Text,
|
---|
| 1546 | const QString& button1Text,
|
---|
| 1547 | const QString& button2Text,
|
---|
| 1548 | int defaultButtonNumber,
|
---|
| 1549 | int escapeButtonNumber )
|
---|
| 1550 | {
|
---|
| 1551 | return textBox( parent, Critical, caption, text,
|
---|
| 1552 | button0Text, button1Text, button2Text,
|
---|
| 1553 | defaultButtonNumber, escapeButtonNumber );
|
---|
| 1554 | }
|
---|
| 1555 |
|
---|
| 1556 |
|
---|
| 1557 | /*!
|
---|
| 1558 | Displays a simple message box about Qt, with caption \a caption
|
---|
| 1559 | and centered over \a parent (if \a parent is not 0). The message
|
---|
| 1560 | includes the version number of Qt being used by the application.
|
---|
| 1561 |
|
---|
| 1562 | This is useful for inclusion in the Help menu of an application.
|
---|
| 1563 | See the examples/menu/menu.cpp example.
|
---|
| 1564 |
|
---|
| 1565 | QApplication provides this functionality as a slot.
|
---|
| 1566 |
|
---|
| 1567 | \sa QApplication::aboutQt()
|
---|
| 1568 | */
|
---|
| 1569 |
|
---|
| 1570 | void QMessageBox::aboutQt( QWidget *parent, const QString &caption )
|
---|
| 1571 | {
|
---|
| 1572 | QMessageBox *mb = new QMessageBox( parent, "qt_msgbox_about_qt" );
|
---|
| 1573 | Q_CHECK_PTR( mb );
|
---|
| 1574 | mb->setWFlags( WDestructiveClose );
|
---|
| 1575 |
|
---|
| 1576 | #ifndef QT_NO_WIDGET_TOPEXTRA
|
---|
| 1577 | QString c = caption;
|
---|
| 1578 | if ( c.isNull() )
|
---|
| 1579 | c = tr( "About Qt" );
|
---|
| 1580 | mb->setCaption( c );
|
---|
| 1581 | #endif
|
---|
| 1582 | mb->setText( *translatedTextAboutQt );
|
---|
| 1583 | #ifndef QT_NO_IMAGEIO
|
---|
| 1584 | QPixmap pm;
|
---|
| 1585 | QImage logo( (const char **)qtlogo_xpm);
|
---|
| 1586 | if ( qGray(mb->palette().active().text().rgb()) >
|
---|
| 1587 | qGray(mb->palette().active().base().rgb()) )
|
---|
| 1588 | {
|
---|
| 1589 | // light on dark, adjust some colors (where's 10?)
|
---|
| 1590 | logo.setColor( 0, 0xffffffff );
|
---|
| 1591 | logo.setColor( 1, 0xff666666 );
|
---|
| 1592 | logo.setColor( 2, 0xffcccc66 );
|
---|
| 1593 | logo.setColor( 4, 0xffcccccc );
|
---|
| 1594 | logo.setColor( 6, 0xffffff66 );
|
---|
| 1595 | logo.setColor( 7, 0xff999999 );
|
---|
| 1596 | logo.setColor( 8, 0xff3333ff );
|
---|
| 1597 | logo.setColor( 9, 0xffffff33 );
|
---|
| 1598 | logo.setColor( 11, 0xffcccc99 );
|
---|
| 1599 | }
|
---|
| 1600 | if ( pm.convertFromImage( logo ) )
|
---|
| 1601 | mb->setIconPixmap( pm );
|
---|
| 1602 | #endif
|
---|
| 1603 | mb->setButtonText( 0, tr("OK") );
|
---|
| 1604 | if ( mb->mbd && mb->mbd->pb[0] ) {
|
---|
| 1605 | mb->mbd->pb[0]->setAutoDefault( TRUE );
|
---|
| 1606 | mb->mbd->pb[0]->setFocusPolicy( QWidget::StrongFocus );
|
---|
| 1607 | mb->mbd->pb[0]->setDefault( TRUE );
|
---|
| 1608 | mb->mbd->pb[0]->setFocus();
|
---|
| 1609 | }
|
---|
| 1610 | mb->exec();
|
---|
| 1611 | }
|
---|
| 1612 |
|
---|
| 1613 | /*!
|
---|
| 1614 | \reimp
|
---|
| 1615 | */
|
---|
| 1616 |
|
---|
| 1617 | void QMessageBox::setIcon( const QPixmap &pix )
|
---|
| 1618 | {
|
---|
| 1619 | //reimplemented to avoid compiler warning.
|
---|
| 1620 | #ifndef QT_NO_WIDGET_TOPEXTRA
|
---|
| 1621 | QDialog::setIcon( pix );
|
---|
| 1622 | #endif
|
---|
| 1623 | }
|
---|
| 1624 |
|
---|
| 1625 |
|
---|
| 1626 | /*!
|
---|
| 1627 | \property QMessageBox::textFormat
|
---|
| 1628 | \brief the format of the text displayed by the message box
|
---|
| 1629 |
|
---|
| 1630 | The current text format used by the message box. See the \l
|
---|
| 1631 | Qt::TextFormat enum for an explanation of the possible options.
|
---|
| 1632 |
|
---|
| 1633 | The default format is \c AutoText.
|
---|
| 1634 |
|
---|
| 1635 | \sa setText()
|
---|
| 1636 | */
|
---|
| 1637 |
|
---|
| 1638 | Qt::TextFormat QMessageBox::textFormat() const
|
---|
| 1639 | {
|
---|
| 1640 | return label->textFormat();
|
---|
| 1641 | }
|
---|
| 1642 |
|
---|
| 1643 |
|
---|
| 1644 | void QMessageBox::setTextFormat( Qt::TextFormat format )
|
---|
| 1645 | {
|
---|
| 1646 | label->setTextFormat( format );
|
---|
| 1647 | }
|
---|
| 1648 |
|
---|
| 1649 |
|
---|
| 1650 | #endif
|
---|