examples/dialogs/classwizard/classwizard.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
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 examples 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 #include "classwizard.h"
       
    45 
       
    46 //! [0] //! [1]
       
    47 ClassWizard::ClassWizard(QWidget *parent)
       
    48     : QWizard(parent)
       
    49 {
       
    50     addPage(new IntroPage);
       
    51     addPage(new ClassInfoPage);
       
    52     addPage(new CodeStylePage);
       
    53     addPage(new OutputFilesPage);
       
    54     addPage(new ConclusionPage);
       
    55 //! [0]
       
    56 
       
    57     setPixmap(QWizard::BannerPixmap, QPixmap(":/images/banner.png"));
       
    58     setPixmap(QWizard::BackgroundPixmap, QPixmap(":/images/background.png"));
       
    59 
       
    60     setWindowTitle(tr("Class Wizard"));
       
    61 //! [2]
       
    62 }
       
    63 //! [1] //! [2]
       
    64 
       
    65 //! [3]
       
    66 void ClassWizard::accept()
       
    67 //! [3] //! [4]
       
    68 {
       
    69     QByteArray className = field("className").toByteArray();
       
    70     QByteArray baseClass = field("baseClass").toByteArray();
       
    71     QByteArray macroName = field("macroName").toByteArray();
       
    72     QByteArray baseInclude = field("baseInclude").toByteArray();
       
    73 
       
    74     QString outputDir = field("outputDir").toString();
       
    75     QString header = field("header").toString();
       
    76     QString implementation = field("implementation").toString();
       
    77 //! [4]
       
    78 
       
    79     QByteArray block;
       
    80 
       
    81     if (field("comment").toBool()) {
       
    82         block += "/*\n";
       
    83         block += "    " + header.toAscii() + "\n";
       
    84         block += "*/\n";
       
    85         block += "\n";
       
    86     }
       
    87     if (field("protect").toBool()) {
       
    88         block += "#ifndef " + macroName + "\n";
       
    89         block += "#define " + macroName + "\n";
       
    90         block += "\n";
       
    91     }
       
    92     if (field("includeBase").toBool()) {
       
    93         block += "#include " + baseInclude + "\n";
       
    94         block += "\n";
       
    95     }
       
    96 
       
    97     block += "class " + className;
       
    98     if (!baseClass.isEmpty())
       
    99         block += " : public " + baseClass;
       
   100     block += "\n";
       
   101     block += "{\n";
       
   102 
       
   103     /* qmake ignore Q_OBJECT */
       
   104 
       
   105     if (field("qobjectMacro").toBool()) {
       
   106         block += "    Q_OBJECT\n";
       
   107         block += "\n";
       
   108     }
       
   109     block += "public:\n";
       
   110 
       
   111     if (field("qobjectCtor").toBool()) {
       
   112         block += "    " + className + "(QObject *parent = 0);\n";
       
   113     } else if (field("qwidgetCtor").toBool()) {
       
   114         block += "    " + className + "(QWidget *parent = 0);\n";
       
   115     } else if (field("defaultCtor").toBool()) {
       
   116         block += "    " + className + "();\n";
       
   117         if (field("copyCtor").toBool()) {
       
   118             block += "    " + className + "(const " + className + " &other);\n";
       
   119             block += "\n";
       
   120             block += "    " + className + " &operator=" + "(const " + className
       
   121                      + " &other);\n";
       
   122         }
       
   123     }
       
   124     block += "};\n";
       
   125 
       
   126     if (field("protect").toBool()) {
       
   127         block += "\n";
       
   128         block += "#endif\n";
       
   129     }
       
   130 
       
   131     QFile headerFile(outputDir + "/" + header);
       
   132     if (!headerFile.open(QFile::WriteOnly | QFile::Text)) {
       
   133         QMessageBox::warning(0, QObject::tr("Simple Wizard"),
       
   134                              QObject::tr("Cannot write file %1:\n%2")
       
   135                              .arg(headerFile.fileName())
       
   136                              .arg(headerFile.errorString()));
       
   137         return;
       
   138     }
       
   139     headerFile.write(block);
       
   140 
       
   141     block.clear();
       
   142 
       
   143     if (field("comment").toBool()) {
       
   144         block += "/*\n";
       
   145         block += "    " + implementation.toAscii() + "\n";
       
   146         block += "*/\n";
       
   147         block += "\n";
       
   148     }
       
   149     block += "#include \"" + header.toAscii() + "\"\n";
       
   150     block += "\n";
       
   151 
       
   152     if (field("qobjectCtor").toBool()) {
       
   153         block += className + "::" + className + "(QObject *parent)\n";
       
   154         block += "    : " + baseClass + "(parent)\n";
       
   155         block += "{\n";
       
   156         block += "}\n";
       
   157     } else if (field("qwidgetCtor").toBool()) {
       
   158         block += className + "::" + className + "(QWidget *parent)\n";
       
   159         block += "    : " + baseClass + "(parent)\n";
       
   160         block += "{\n";
       
   161         block += "}\n";
       
   162     } else if (field("defaultCtor").toBool()) {
       
   163         block += className + "::" + className + "()\n";
       
   164         block += "{\n";
       
   165         block += "    // missing code\n";
       
   166         block += "}\n";
       
   167 
       
   168         if (field("copyCtor").toBool()) {
       
   169             block += "\n";
       
   170             block += className + "::" + className + "(const " + className
       
   171                      + " &other)\n";
       
   172             block += "{\n";
       
   173             block += "    *this = other;\n";
       
   174             block += "}\n";
       
   175             block += "\n";
       
   176             block += className + " &" + className + "::operator=(const "
       
   177                      + className + " &other)\n";
       
   178             block += "{\n";
       
   179             if (!baseClass.isEmpty())
       
   180                 block += "    " + baseClass + "::operator=(other);\n";
       
   181             block += "    // missing code\n";
       
   182             block += "    return *this;\n";
       
   183             block += "}\n";
       
   184         }
       
   185     }
       
   186 
       
   187     QFile implementationFile(outputDir + "/" + implementation);
       
   188     if (!implementationFile.open(QFile::WriteOnly | QFile::Text)) {
       
   189         QMessageBox::warning(0, QObject::tr("Simple Wizard"),
       
   190                              QObject::tr("Cannot write file %1:\n%2")
       
   191                              .arg(implementationFile.fileName())
       
   192                              .arg(implementationFile.errorString()));
       
   193         return;
       
   194     }
       
   195     implementationFile.write(block);
       
   196 
       
   197 //! [5]
       
   198     QDialog::accept();
       
   199 //! [5] //! [6]
       
   200 }
       
   201 //! [6]
       
   202 
       
   203 //! [7]
       
   204 IntroPage::IntroPage(QWidget *parent)
       
   205     : QWizardPage(parent)
       
   206 {
       
   207     setTitle(tr("Introduction"));
       
   208     setPixmap(QWizard::WatermarkPixmap, QPixmap(":/images/watermark1.png"));
       
   209 
       
   210     label = new QLabel(tr("This wizard will generate a skeleton C++ class "
       
   211                           "definition, including a few functions. You simply "
       
   212                           "need to specify the class name and set a few "
       
   213                           "options to produce a header file and an "
       
   214                           "implementation file for your new C++ class."));
       
   215     label->setWordWrap(true);
       
   216 
       
   217     QVBoxLayout *layout = new QVBoxLayout;
       
   218     layout->addWidget(label);
       
   219     setLayout(layout);
       
   220 }
       
   221 //! [7]
       
   222 
       
   223 //! [8] //! [9]
       
   224 ClassInfoPage::ClassInfoPage(QWidget *parent)
       
   225     : QWizardPage(parent)
       
   226 {
       
   227 //! [8]
       
   228     setTitle(tr("Class Information"));
       
   229     setSubTitle(tr("Specify basic information about the class for which you "
       
   230                    "want to generate skeleton source code files."));
       
   231     setPixmap(QWizard::LogoPixmap, QPixmap(":/images/logo1.png"));
       
   232 
       
   233 //! [10]
       
   234     classNameLabel = new QLabel(tr("&Class name:"));
       
   235     classNameLineEdit = new QLineEdit;
       
   236     classNameLabel->setBuddy(classNameLineEdit);
       
   237 
       
   238     baseClassLabel = new QLabel(tr("B&ase class:"));
       
   239     baseClassLineEdit = new QLineEdit;
       
   240     baseClassLabel->setBuddy(baseClassLineEdit);
       
   241 
       
   242     qobjectMacroCheckBox = new QCheckBox(tr("Generate Q_OBJECT &macro"));
       
   243 
       
   244 //! [10]
       
   245     groupBox = new QGroupBox(tr("C&onstructor"));
       
   246 //! [9]
       
   247 
       
   248     qobjectCtorRadioButton = new QRadioButton(tr("&QObject-style constructor"));
       
   249     qwidgetCtorRadioButton = new QRadioButton(tr("Q&Widget-style constructor"));
       
   250     defaultCtorRadioButton = new QRadioButton(tr("&Default constructor"));
       
   251     copyCtorCheckBox = new QCheckBox(tr("&Generate copy constructor and "
       
   252                                         "operator="));
       
   253 
       
   254     defaultCtorRadioButton->setChecked(true);
       
   255 
       
   256     connect(defaultCtorRadioButton, SIGNAL(toggled(bool)),
       
   257             copyCtorCheckBox, SLOT(setEnabled(bool)));
       
   258 
       
   259 //! [11] //! [12]
       
   260     registerField("className*", classNameLineEdit);
       
   261     registerField("baseClass", baseClassLineEdit);
       
   262     registerField("qobjectMacro", qobjectMacroCheckBox);
       
   263 //! [11]
       
   264     registerField("qobjectCtor", qobjectCtorRadioButton);
       
   265     registerField("qwidgetCtor", qwidgetCtorRadioButton);
       
   266     registerField("defaultCtor", defaultCtorRadioButton);
       
   267     registerField("copyCtor", copyCtorCheckBox);
       
   268 
       
   269     QVBoxLayout *groupBoxLayout = new QVBoxLayout;
       
   270 //! [12]
       
   271     groupBoxLayout->addWidget(qobjectCtorRadioButton);
       
   272     groupBoxLayout->addWidget(qwidgetCtorRadioButton);
       
   273     groupBoxLayout->addWidget(defaultCtorRadioButton);
       
   274     groupBoxLayout->addWidget(copyCtorCheckBox);
       
   275     groupBox->setLayout(groupBoxLayout);
       
   276 
       
   277     QGridLayout *layout = new QGridLayout;
       
   278     layout->addWidget(classNameLabel, 0, 0);
       
   279     layout->addWidget(classNameLineEdit, 0, 1);
       
   280     layout->addWidget(baseClassLabel, 1, 0);
       
   281     layout->addWidget(baseClassLineEdit, 1, 1);
       
   282     layout->addWidget(qobjectMacroCheckBox, 2, 0, 1, 2);
       
   283     layout->addWidget(groupBox, 3, 0, 1, 2);
       
   284     setLayout(layout);
       
   285 //! [13]
       
   286 }
       
   287 //! [13]
       
   288 
       
   289 //! [14]
       
   290 CodeStylePage::CodeStylePage(QWidget *parent)
       
   291     : QWizardPage(parent)
       
   292 {
       
   293     setTitle(tr("Code Style Options"));
       
   294     setSubTitle(tr("Choose the formatting of the generated code."));
       
   295     setPixmap(QWizard::LogoPixmap, QPixmap(":/images/logo2.png"));
       
   296 
       
   297     commentCheckBox = new QCheckBox(tr("&Start generated files with a "
       
   298 //! [14]
       
   299                                        "comment"));
       
   300     commentCheckBox->setChecked(true);
       
   301 
       
   302     protectCheckBox = new QCheckBox(tr("&Protect header file against multiple "
       
   303                                        "inclusions"));
       
   304     protectCheckBox->setChecked(true);
       
   305 
       
   306     macroNameLabel = new QLabel(tr("&Macro name:"));
       
   307     macroNameLineEdit = new QLineEdit;
       
   308     macroNameLabel->setBuddy(macroNameLineEdit);
       
   309 
       
   310     includeBaseCheckBox = new QCheckBox(tr("&Include base class definition"));
       
   311     baseIncludeLabel = new QLabel(tr("Base class include:"));
       
   312     baseIncludeLineEdit = new QLineEdit;
       
   313     baseIncludeLabel->setBuddy(baseIncludeLineEdit);
       
   314 
       
   315     connect(protectCheckBox, SIGNAL(toggled(bool)),
       
   316             macroNameLabel, SLOT(setEnabled(bool)));
       
   317     connect(protectCheckBox, SIGNAL(toggled(bool)),
       
   318             macroNameLineEdit, SLOT(setEnabled(bool)));
       
   319     connect(includeBaseCheckBox, SIGNAL(toggled(bool)),
       
   320             baseIncludeLabel, SLOT(setEnabled(bool)));
       
   321     connect(includeBaseCheckBox, SIGNAL(toggled(bool)),
       
   322             baseIncludeLineEdit, SLOT(setEnabled(bool)));
       
   323 
       
   324     registerField("comment", commentCheckBox);
       
   325     registerField("protect", protectCheckBox);
       
   326     registerField("macroName", macroNameLineEdit);
       
   327     registerField("includeBase", includeBaseCheckBox);
       
   328     registerField("baseInclude", baseIncludeLineEdit);
       
   329 
       
   330     QGridLayout *layout = new QGridLayout;
       
   331     layout->setColumnMinimumWidth(0, 20);
       
   332     layout->addWidget(commentCheckBox, 0, 0, 1, 3);
       
   333     layout->addWidget(protectCheckBox, 1, 0, 1, 3);
       
   334     layout->addWidget(macroNameLabel, 2, 1);
       
   335     layout->addWidget(macroNameLineEdit, 2, 2);
       
   336     layout->addWidget(includeBaseCheckBox, 3, 0, 1, 3);
       
   337     layout->addWidget(baseIncludeLabel, 4, 1);
       
   338     layout->addWidget(baseIncludeLineEdit, 4, 2);
       
   339 //! [15]
       
   340     setLayout(layout);
       
   341 }
       
   342 //! [15]
       
   343 
       
   344 //! [16]
       
   345 void CodeStylePage::initializePage()
       
   346 {
       
   347     QString className = field("className").toString();
       
   348     macroNameLineEdit->setText(className.toUpper() + "_H");
       
   349 
       
   350     QString baseClass = field("baseClass").toString();
       
   351 
       
   352     includeBaseCheckBox->setChecked(!baseClass.isEmpty());
       
   353     includeBaseCheckBox->setEnabled(!baseClass.isEmpty());
       
   354     baseIncludeLabel->setEnabled(!baseClass.isEmpty());
       
   355     baseIncludeLineEdit->setEnabled(!baseClass.isEmpty());
       
   356 
       
   357     if (baseClass.isEmpty()) {
       
   358         baseIncludeLineEdit->clear();
       
   359     } else if (QRegExp("Q[A-Z].*").exactMatch(baseClass)) {
       
   360         baseIncludeLineEdit->setText("<" + baseClass + ">");
       
   361     } else {
       
   362         baseIncludeLineEdit->setText("\"" + baseClass.toLower() + ".h\"");
       
   363     }
       
   364 }
       
   365 //! [16]
       
   366 
       
   367 OutputFilesPage::OutputFilesPage(QWidget *parent)
       
   368     : QWizardPage(parent)
       
   369 {
       
   370     setTitle(tr("Output Files"));
       
   371     setSubTitle(tr("Specify where you want the wizard to put the generated "
       
   372                    "skeleton code."));
       
   373     setPixmap(QWizard::LogoPixmap, QPixmap(":/images/logo3.png"));
       
   374 
       
   375     outputDirLabel = new QLabel(tr("&Output directory:"));
       
   376     outputDirLineEdit = new QLineEdit;
       
   377     outputDirLabel->setBuddy(outputDirLineEdit);
       
   378 
       
   379     headerLabel = new QLabel(tr("&Header file name:"));
       
   380     headerLineEdit = new QLineEdit;
       
   381     headerLabel->setBuddy(headerLineEdit);
       
   382 
       
   383     implementationLabel = new QLabel(tr("&Implementation file name:"));
       
   384     implementationLineEdit = new QLineEdit;
       
   385     implementationLabel->setBuddy(implementationLineEdit);
       
   386 
       
   387     registerField("outputDir*", outputDirLineEdit);
       
   388     registerField("header*", headerLineEdit);
       
   389     registerField("implementation*", implementationLineEdit);
       
   390 
       
   391     QGridLayout *layout = new QGridLayout;
       
   392     layout->addWidget(outputDirLabel, 0, 0);
       
   393     layout->addWidget(outputDirLineEdit, 0, 1);
       
   394     layout->addWidget(headerLabel, 1, 0);
       
   395     layout->addWidget(headerLineEdit, 1, 1);
       
   396     layout->addWidget(implementationLabel, 2, 0);
       
   397     layout->addWidget(implementationLineEdit, 2, 1);
       
   398     setLayout(layout);
       
   399 }
       
   400 
       
   401 //! [17]
       
   402 void OutputFilesPage::initializePage()
       
   403 {
       
   404     QString className = field("className").toString();
       
   405     headerLineEdit->setText(className.toLower() + ".h");
       
   406     implementationLineEdit->setText(className.toLower() + ".cpp");
       
   407     outputDirLineEdit->setText(QDir::convertSeparators(QDir::tempPath()));
       
   408 }
       
   409 //! [17]
       
   410 
       
   411 ConclusionPage::ConclusionPage(QWidget *parent)
       
   412     : QWizardPage(parent)
       
   413 {
       
   414     setTitle(tr("Conclusion"));
       
   415     setPixmap(QWizard::WatermarkPixmap, QPixmap(":/images/watermark2.png"));
       
   416 
       
   417     label = new QLabel;
       
   418     label->setWordWrap(true);
       
   419 
       
   420     QVBoxLayout *layout = new QVBoxLayout;
       
   421     layout->addWidget(label);
       
   422     setLayout(layout);
       
   423 }
       
   424 
       
   425 void ConclusionPage::initializePage()
       
   426 {
       
   427     QString finishText = wizard()->buttonText(QWizard::FinishButton);
       
   428     finishText.remove('&');
       
   429     label->setText(tr("Click %1 to generate the class skeleton.")
       
   430                    .arg(finishText));
       
   431 }