source: trunk/examples/dialogs/classwizard/classwizard.cpp@ 5

Last change on this file since 5 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 14.8 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <QtGui>
43
44#include "classwizard.h"
45
46//! [0] //! [1]
47ClassWizard::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]
66void 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]
204IntroPage::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]
224ClassInfoPage::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]
290CodeStylePage::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]
345void 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]