Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Tous les espaces de nom  ·  Toutes les classes  ·  Classes principales  ·  Annotées  ·  Classes groupées  ·  Modules  ·  Fonctions  · 

classwizard.cpp Example File
dialogs/classwizard/classwizard.cpp

 /****************************************************************************
 **
 ** Copyright (C) 2007-2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
 **
 ** This file is part of the documentation of the Qt Toolkit.
 **
 ** Licensees holding a valid Qt License Agreement may use this file in
 ** accordance with the rights, responsibilities and obligations
 ** contained therein.  Please consult your licensing agreement or
 ** contact qt-sales@nokia.com if any conditions of this licensing
 ** agreement are not clear to you.
 **
 ** Further information about Qt licensing is available at:
 ** http://trolltech.com/products/appdev/licensing.
 **
 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 **
 ****************************************************************************/

 #include <QtGui>

 #include "classwizard.h"

 ClassWizard::ClassWizard(QWidget *parent)
     : QWizard(parent)
 {
     addPage(new IntroPage);
     addPage(new ClassInfoPage);
     addPage(new CodeStylePage);
     addPage(new OutputFilesPage);
     addPage(new ConclusionPage);

     setPixmap(QWizard::BannerPixmap, QPixmap(":/images/banner.png"));
     setPixmap(QWizard::BackgroundPixmap, QPixmap(":/images/background.png"));

     setWindowTitle(tr("Class Wizard"));
 }

 void ClassWizard::accept()
 {
     QByteArray className = field("className").toByteArray();
     QByteArray baseClass = field("baseClass").toByteArray();
     QByteArray macroName = field("macroName").toByteArray();
     QByteArray baseInclude = field("baseInclude").toByteArray();

     QString outputDir = field("outputDir").toString();
     QString header = field("header").toString();
     QString implementation = field("implementation").toString();

     QByteArray block;

     if (field("comment").toBool()) {
         block += "/*\n";
         block += "    " + header.toAscii() + "\n";
         block += "*/\n";
         block += "\n";
     }
     if (field("protect").toBool()) {
         block += "#ifndef " + macroName + "\n";
         block += "#define " + macroName + "\n";
         block += "\n";
     }
     if (field("includeBase").toBool()) {
         block += "#include " + baseInclude + "\n";
         block += "\n";
     }

     block += "class " + className;
     if (!baseClass.isEmpty())
         block += " : public " + baseClass;
     block += "\n";
     block += "{\n";

     /* qmake ignore Q_OBJECT */

     if (field("qobjectMacro").toBool()) {
         block += "    Q_OBJECT\n";
         block += "\n";
     }
     block += "public:\n";

     if (field("qobjectCtor").toBool()) {
         block += "    " + className + "(QObject *parent = 0);\n";
     } else if (field("qwidgetCtor").toBool()) {
         block += "    " + className + "(QWidget *parent = 0);\n";
     } else if (field("defaultCtor").toBool()) {
         block += "    " + className + "();\n";
         if (field("copyCtor").toBool()) {
             block += "    " + className + "(const " + className + " &other);\n";
             block += "\n";
             block += "    " + className + " &operator=" + "(const " + className
                      + " &other);\n";
         }
     }
     block += "};\n";

     if (field("protect").toBool()) {
         block += "\n";
         block += "#endif\n";
     }

     QFile headerFile(outputDir + "/" + header);
     if (!headerFile.open(QFile::WriteOnly | QFile::Text)) {
         QMessageBox::warning(0, QObject::tr("Simple Wizard"),
                              QObject::tr("Cannot write file %1:\n%2")
                              .arg(headerFile.fileName())
                              .arg(headerFile.errorString()));
         return;
     }
     headerFile.write(block);

     block.clear();

     if (field("comment").toBool()) {
         block += "/*\n";
         block += "    " + implementation.toAscii() + "\n";
         block += "*/\n";
         block += "\n";
     }
     block += "#include \"" + header.toAscii() + "\"\n";
     block += "\n";

     if (field("qobjectCtor").toBool()) {
         block += className + "::" + className + "(QObject *parent)\n";
         block += "    : " + baseClass + "(parent)\n";
         block += "{\n";
         block += "}\n";
     } else if (field("qwidgetCtor").toBool()) {
         block += className + "::" + className + "(QWidget *parent)\n";
         block += "    : " + baseClass + "(parent)\n";
         block += "{\n";
         block += "}\n";
     } else if (field("defaultCtor").toBool()) {
         block += className + "::" + className + "()\n";
         block += "{\n";
         block += "    // missing code\n";
         block += "}\n";

         if (field("copyCtor").toBool()) {
             block += "\n";
             block += className + "::" + className + "(const " + className
                      + " &other)\n";
             block += "{\n";
             block += "    *this = other;\n";
             block += "}\n";
             block += "\n";
             block += className + " &" + className + "::operator=(const "
                      + className + " &other)\n";
             block += "{\n";
             if (!baseClass.isEmpty())
                 block += "    " + baseClass + "::operator=(other);\n";
             block += "    // missing code\n";
             block += "    return *this;\n";
             block += "}\n";
         }
     }

     QFile implementationFile(outputDir + "/" + implementation);
     if (!implementationFile.open(QFile::WriteOnly | QFile::Text)) {
         QMessageBox::warning(0, QObject::tr("Simple Wizard"),
                              QObject::tr("Cannot write file %1:\n%2")
                              .arg(implementationFile.fileName())
                              .arg(implementationFile.errorString()));
         return;
     }
     implementationFile.write(block);

     QDialog::accept();
 }

 IntroPage::IntroPage(QWidget *parent)
     : QWizardPage(parent)
 {
     setTitle(tr("Introduction"));
     setPixmap(QWizard::WatermarkPixmap, QPixmap(":/images/watermark1.png"));

     label = new QLabel(tr("This wizard will generate a skeleton C++ class "
                           "definition, including a few functions. You simply "
                           "need to specify the class name and set a few "
                           "options to produce a header file and an "
                           "implementation file for your new C++ class."));
     label->setWordWrap(true);

     QVBoxLayout *layout = new QVBoxLayout;
     layout->addWidget(label);
     setLayout(layout);
 }

 ClassInfoPage::ClassInfoPage(QWidget *parent)
     : QWizardPage(parent)
 {
     setTitle(tr("Class Information"));
     setSubTitle(tr("Specify basic information about the class for which you "
                    "want to generate skeleton source code files."));
     setPixmap(QWizard::LogoPixmap, QPixmap(":/images/logo1.png"));

     classNameLabel = new QLabel(tr("&Class name:"));
     classNameLineEdit = new QLineEdit;
     classNameLabel->setBuddy(classNameLineEdit);

     baseClassLabel = new QLabel(tr("B&ase class:"));
     baseClassLineEdit = new QLineEdit;
     baseClassLabel->setBuddy(baseClassLineEdit);

     qobjectMacroCheckBox = new QCheckBox(tr("Generate Q_OBJECT &macro"));

     groupBox = new QGroupBox(tr("C&onstructor"));

     qobjectCtorRadioButton = new QRadioButton(tr("&QObject-style constructor"));
     qwidgetCtorRadioButton = new QRadioButton(tr("Q&Widget-style constructor"));
     defaultCtorRadioButton = new QRadioButton(tr("&Default constructor"));
     copyCtorCheckBox = new QCheckBox(tr("&Generate copy constructor and "
                                         "operator="));

     defaultCtorRadioButton->setChecked(true);

     connect(defaultCtorRadioButton, SIGNAL(toggled(bool)),
             copyCtorCheckBox, SLOT(setEnabled(bool)));

     registerField("className*", classNameLineEdit);
     registerField("baseClass", baseClassLineEdit);
     registerField("qobjectMacro", qobjectMacroCheckBox);
     registerField("qobjectCtor", qobjectCtorRadioButton);
     registerField("qwidgetCtor", qwidgetCtorRadioButton);
     registerField("defaultCtor", defaultCtorRadioButton);
     registerField("copyCtor", copyCtorCheckBox);

     QVBoxLayout *groupBoxLayout = new QVBoxLayout;
     groupBoxLayout->addWidget(qobjectCtorRadioButton);
     groupBoxLayout->addWidget(qwidgetCtorRadioButton);
     groupBoxLayout->addWidget(defaultCtorRadioButton);
     groupBoxLayout->addWidget(copyCtorCheckBox);
     groupBox->setLayout(groupBoxLayout);

     QGridLayout *layout = new QGridLayout;
     layout->addWidget(classNameLabel, 0, 0);
     layout->addWidget(classNameLineEdit, 0, 1);
     layout->addWidget(baseClassLabel, 1, 0);
     layout->addWidget(baseClassLineEdit, 1, 1);
     layout->addWidget(qobjectMacroCheckBox, 2, 0, 1, 2);
     layout->addWidget(groupBox, 3, 0, 1, 2);
     setLayout(layout);
 }

 CodeStylePage::CodeStylePage(QWidget *parent)
     : QWizardPage(parent)
 {
     setTitle(tr("Code Style Options"));
     setSubTitle(tr("Choose the formatting of the generated code."));
     setPixmap(QWizard::LogoPixmap, QPixmap(":/images/logo2.png"));

     commentCheckBox = new QCheckBox(tr("&Start generated files with a "
                                        "comment"));
     commentCheckBox->setChecked(true);

     protectCheckBox = new QCheckBox(tr("&Protect header file against multiple "
                                        "inclusions"));
     protectCheckBox->setChecked(true);

     macroNameLabel = new QLabel(tr("&Macro name:"));
     macroNameLineEdit = new QLineEdit;
     macroNameLabel->setBuddy(macroNameLineEdit);

     includeBaseCheckBox = new QCheckBox(tr("&Include base class definition"));
     baseIncludeLabel = new QLabel(tr("Base class include:"));
     baseIncludeLineEdit = new QLineEdit;
     baseIncludeLabel->setBuddy(baseIncludeLineEdit);

     connect(protectCheckBox, SIGNAL(toggled(bool)),
             macroNameLabel, SLOT(setEnabled(bool)));
     connect(protectCheckBox, SIGNAL(toggled(bool)),
             macroNameLineEdit, SLOT(setEnabled(bool)));
     connect(includeBaseCheckBox, SIGNAL(toggled(bool)),
             baseIncludeLabel, SLOT(setEnabled(bool)));
     connect(includeBaseCheckBox, SIGNAL(toggled(bool)),
             baseIncludeLineEdit, SLOT(setEnabled(bool)));

     registerField("comment", commentCheckBox);
     registerField("protect", protectCheckBox);
     registerField("macroName", macroNameLineEdit);
     registerField("includeBase", includeBaseCheckBox);
     registerField("baseInclude", baseIncludeLineEdit);

     QGridLayout *layout = new QGridLayout;
     layout->setColumnMinimumWidth(0, 20);
     layout->addWidget(commentCheckBox, 0, 0, 1, 3);
     layout->addWidget(protectCheckBox, 1, 0, 1, 3);
     layout->addWidget(macroNameLabel, 2, 1);
     layout->addWidget(macroNameLineEdit, 2, 2);
     layout->addWidget(includeBaseCheckBox, 3, 0, 1, 3);
     layout->addWidget(baseIncludeLabel, 4, 1);
     layout->addWidget(baseIncludeLineEdit, 4, 2);
     setLayout(layout);
 }

 void CodeStylePage::initializePage()
 {
     QString className = field("className").toString();
     macroNameLineEdit->setText(className.toUpper() + "_H");

     QString baseClass = field("baseClass").toString();

     includeBaseCheckBox->setChecked(!baseClass.isEmpty());
     includeBaseCheckBox->setEnabled(!baseClass.isEmpty());
     baseIncludeLabel->setEnabled(!baseClass.isEmpty());
     baseIncludeLineEdit->setEnabled(!baseClass.isEmpty());

     if (baseClass.isEmpty()) {
         baseIncludeLineEdit->clear();
     } else if (QRegExp("Q[A-Z].*").exactMatch(baseClass)) {
         baseIncludeLineEdit->setText("<" + baseClass + ">");
     } else {
         baseIncludeLineEdit->setText("\"" + baseClass.toLower() + ".h\"");
     }
 }

 OutputFilesPage::OutputFilesPage(QWidget *parent)
     : QWizardPage(parent)
 {
     setTitle(tr("Output Files"));
     setSubTitle(tr("Specify where you want the wizard to put the generated "
                    "skeleton code."));
     setPixmap(QWizard::LogoPixmap, QPixmap(":/images/logo3.png"));

     outputDirLabel = new QLabel(tr("&Output directory:"));
     outputDirLineEdit = new QLineEdit;
     outputDirLabel->setBuddy(outputDirLineEdit);

     headerLabel = new QLabel(tr("&Header file name:"));
     headerLineEdit = new QLineEdit;
     headerLabel->setBuddy(headerLineEdit);

     implementationLabel = new QLabel(tr("&Implementation file name:"));
     implementationLineEdit = new QLineEdit;
     implementationLabel->setBuddy(implementationLineEdit);

     registerField("outputDir*", outputDirLineEdit);
     registerField("header*", headerLineEdit);
     registerField("implementation*", implementationLineEdit);

     QGridLayout *layout = new QGridLayout;
     layout->addWidget(outputDirLabel, 0, 0);
     layout->addWidget(outputDirLineEdit, 0, 1);
     layout->addWidget(headerLabel, 1, 0);
     layout->addWidget(headerLineEdit, 1, 1);
     layout->addWidget(implementationLabel, 2, 0);
     layout->addWidget(implementationLineEdit, 2, 1);
     setLayout(layout);
 }

 void OutputFilesPage::initializePage()
 {
     QString className = field("className").toString();
     headerLineEdit->setText(className.toLower() + ".h");
     implementationLineEdit->setText(className.toLower() + ".cpp");
     outputDirLineEdit->setText(QDir::convertSeparators(QDir::tempPath()));
 }

 ConclusionPage::ConclusionPage(QWidget *parent)
     : QWizardPage(parent)
 {
     setTitle(tr("Conclusion"));
     setPixmap(QWizard::WatermarkPixmap, QPixmap(":/images/watermark2.png"));

     label = new QLabel;
     label->setWordWrap(true);

     QVBoxLayout *layout = new QVBoxLayout;
     layout->addWidget(label);
     setLayout(layout);
 }

 void ConclusionPage::initializePage()
 {
     QString finishText = wizard()->buttonText(QWizard::FinishButton);
     finishText.remove('&');
     label->setText(tr("Click %1 to generate the class skeleton.")
                    .arg(finishText));
 }

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année

Le Qt Labs au hasard

Logo

QLocale : à propos du temps (et des dates, et des langues, et des...)

Les Qt Labs sont les laboratoires des développeurs de Qt, où ils peuvent partager des impressions sur le framework, son utilisation, ce que pourrait être son futur. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 4.4
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web