doc/src/snippets/dialogs/dialogs.cpp
changeset 0 1918ee327afb
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the documentation of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include <QtGui>
       
    43 
       
    44 typedef QDialog WordCountDialog;
       
    45 typedef QDialog FindDialog;
       
    46 
       
    47 #define this 0
       
    48 #define setWordCount(x) isVisible()
       
    49 
       
    50 QString tr(const char *text)
       
    51 {
       
    52     return QApplication::translate(text, text);
       
    53 }
       
    54 
       
    55 class EditorWindow : public QMainWindow
       
    56 {
       
    57 public:
       
    58     void find();
       
    59     void countWords();
       
    60 
       
    61 private:
       
    62     FindDialog *findDialog;
       
    63 };
       
    64 
       
    65 //! [0]
       
    66 void EditorWindow::find()
       
    67 {
       
    68     if (!findDialog) {
       
    69         findDialog = new FindDialog(this);
       
    70         connect(findDialog, SIGNAL(findNext()), this, SLOT(findNext()));
       
    71     }
       
    72 
       
    73     findDialog->show();
       
    74     findDialog->raise();
       
    75     findDialog->activateWindow();
       
    76 }
       
    77 //! [0]
       
    78 
       
    79 //! [1]
       
    80 void EditorWindow::countWords()
       
    81 {
       
    82     WordCountDialog dialog(this);
       
    83     dialog.setWordCount(document().wordCount());
       
    84     dialog.exec();
       
    85 }
       
    86 //! [1]
       
    87 
       
    88 inline bool boo()
       
    89 {
       
    90     QMessageBox::information(this, "Application name",
       
    91                              "Unable to find the user preferences file.\n"
       
    92                              "The factory default will be used instead.");
       
    93 
       
    94     QString filename;
       
    95     if (QFile::exists(filename) &&
       
    96         QMessageBox::question(
       
    97             this,
       
    98             tr("Overwrite File? -- Application Name"),
       
    99             tr("A file called %1 already exists."
       
   100                "Do you want to overwrite it?")
       
   101             .arg(filename),
       
   102             tr("&Yes"), tr("&No"),
       
   103             QString(), 0, 1))
       
   104         return false;
       
   105 
       
   106     switch(QMessageBox::warning(this, "Application name",
       
   107                                 "Could not connect to the <mumble> server.\n"
       
   108                                 "This program can't function correctly "
       
   109                                 "without the server.\n\n",
       
   110                                 "Retry",
       
   111                                 "Quit", 0, 0, 1)) {
       
   112     case 0: // The user clicked the Retry again button or pressed Enter
       
   113         // try again
       
   114         break;
       
   115     case 1: // The user clicked the Quit or pressed Escape
       
   116         // exit
       
   117         break;
       
   118     }
       
   119 
       
   120     switch(QMessageBox::information(this, "Application name here",
       
   121                                     "The document contains unsaved changes\n"
       
   122                                     "Do you want to save the changes before exiting?",
       
   123                                     "&Save", "&Discard", "Cancel",
       
   124                                     0,      // Enter == button 0
       
   125                                     2)) { // Escape == button 2
       
   126     case 0: // Save clicked or Alt+S pressed or Enter pressed.
       
   127         // save
       
   128         break;
       
   129     case 1: // Discard clicked or Alt+D pressed
       
   130         // don't save but exit
       
   131         break;
       
   132     case 2: // Cancel clicked or Escape pressed
       
   133         // don't exit
       
   134         break;
       
   135     }
       
   136 
       
   137     switch(QMessageBox::warning(this, "Application name here",
       
   138                                 "Could not save the user preferences,\n"
       
   139                                 "because the disk is full. You can delete\n"
       
   140                                 "some files and press Retry, or you can\n"
       
   141                                 "abort the Save Preferences operation.",
       
   142                                 QMessageBox::Retry | QMessageBox::Default,
       
   143                                 QMessageBox::Abort | QMessageBox::Escape)) {
       
   144     case QMessageBox::Retry: // Retry clicked or Enter pressed
       
   145         // try again
       
   146         break;
       
   147     case QMessageBox::Abort: // Abort clicked or Escape pressed
       
   148         // abort
       
   149         break;
       
   150     }
       
   151 
       
   152     QString errorDetails;
       
   153     QMessageBox::critical(0, "Application name here",
       
   154                           QString("An internal error occurred. Please ") +
       
   155                           "call technical support at 1234-56789 and report\n"+
       
   156                           "these numbers:\n\n" + errorDetails +
       
   157                           "\n\nApplication will now exit.");
       
   158 
       
   159     QMessageBox::about(this, "About <Application>",
       
   160                        "<Application> is a <one-paragraph blurb>\n\n"
       
   161                        "Copyright 1991-2003 Such-and-such. "
       
   162                        "<License words here.>\n\n"
       
   163                        "For technical support, call 1234-56789 or see\n"
       
   164                        "http://www.such-and-such.com/Application/\n");
       
   165 
       
   166     {
       
   167         // saving the file
       
   168         QMessageBox mb("Application name here",
       
   169                        "Saving the file will overwrite the original file on the disk.\n"
       
   170                        "Do you really want to save?",
       
   171                        QMessageBox::Information,
       
   172                        QMessageBox::Yes | QMessageBox::Default,
       
   173                        QMessageBox::No,
       
   174                        QMessageBox::Cancel | QMessageBox::Escape);
       
   175         mb.setButtonText(QMessageBox::Yes, "Save");
       
   176         mb.setButtonText(QMessageBox::No, "Discard");
       
   177         switch(mb.exec()) {
       
   178         case QMessageBox::Yes:
       
   179             // save and exit
       
   180             break;
       
   181         case QMessageBox::No:
       
   182             // exit without saving
       
   183             break;
       
   184         case QMessageBox::Cancel:
       
   185             // don't save and don't exit
       
   186             break;
       
   187         }
       
   188     }
       
   189 
       
   190     {
       
   191         // hardware failure
       
   192 //! [2]
       
   193         QMessageBox mb("Application Name",
       
   194                        "Hardware failure.\n\nDisk error detected\nDo you want to stop?",
       
   195                        QMessageBox::Question,
       
   196                        QMessageBox::Yes | QMessageBox::Default,
       
   197                        QMessageBox::No | QMessageBox::Escape,
       
   198                        QMessageBox::NoButton);
       
   199         if (mb.exec() == QMessageBox::No) {
       
   200             // try again
       
   201 //! [2]
       
   202         }
       
   203     }
       
   204 }
       
   205 
       
   206 inline void moo()
       
   207 {
       
   208     int numFiles;
       
   209 //! [3]
       
   210     QProgressDialog progress("Copying files...", "Abort Copy", 0, numFiles, this);
       
   211     progress.setWindowModality(Qt::WindowModal);
       
   212 
       
   213     for (int i = 0; i < numFiles; i++) {
       
   214         progress.setValue(i);
       
   215 
       
   216         if (progress.wasCanceled())
       
   217             break;
       
   218         //... copy one file
       
   219     }
       
   220     progress.setValue(numFiles);
       
   221 //! [3]
       
   222 }
       
   223 
       
   224 class Operation : public QObject
       
   225 {
       
   226 public:
       
   227     Operation(QObject *parent);
       
   228     void perform();
       
   229     void cancel();
       
   230 
       
   231 private:
       
   232     int steps;
       
   233     QProgressDialog *pd;
       
   234     QTimer *t;
       
   235 };
       
   236 
       
   237 //! [4]
       
   238 // Operation constructor
       
   239 Operation::Operation(QObject *parent)
       
   240     : QObject(parent), steps(0)
       
   241 {
       
   242     pd = new QProgressDialog("Operation in progress.", "Cancel", 0, 100);
       
   243     connect(pd, SIGNAL(canceled()), this, SLOT(cancel()));
       
   244     t = new QTimer(this);
       
   245     connect(t, SIGNAL(timeout()), this, SLOT(perform()));
       
   246     t->start(0);
       
   247 }
       
   248 //! [4] //! [5]
       
   249 
       
   250 void Operation::perform()
       
   251 {
       
   252     pd->setValue(steps);
       
   253     //... perform one percent of the operation
       
   254     steps++;
       
   255     if (steps > pd->maximum())
       
   256         t->stop();
       
   257 }
       
   258 //! [5] //! [6]
       
   259 
       
   260 void Operation::cancel()
       
   261 {
       
   262     t->stop();
       
   263     //... cleanup
       
   264 }
       
   265 //! [6]
       
   266 
       
   267 int main()
       
   268 {
       
   269 }