1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
11 | **
|
---|
12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
13 | ** modification, are permitted provided that the following conditions are
|
---|
14 | ** met:
|
---|
15 | ** * Redistributions of source code must retain the above copyright
|
---|
16 | ** notice, this list of conditions and the following disclaimer.
|
---|
17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
18 | ** notice, this list of conditions and the following disclaimer in
|
---|
19 | ** the documentation and/or other materials provided with the
|
---|
20 | ** distribution.
|
---|
21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
22 | ** the names of its contributors may be used to endorse or promote
|
---|
23 | ** products derived from this software without specific prior written
|
---|
24 | ** permission.
|
---|
25 | **
|
---|
26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
37 | ** $QT_END_LICENSE$
|
---|
38 | **
|
---|
39 | ****************************************************************************/
|
---|
40 |
|
---|
41 | #include <QtGui>
|
---|
42 |
|
---|
43 | #include "classwizard.h"
|
---|
44 |
|
---|
45 | //! [0] //! [1]
|
---|
46 | ClassWizard::ClassWizard(QWidget *parent)
|
---|
47 | : QWizard(parent)
|
---|
48 | {
|
---|
49 | addPage(new IntroPage);
|
---|
50 | addPage(new ClassInfoPage);
|
---|
51 | addPage(new CodeStylePage);
|
---|
52 | addPage(new OutputFilesPage);
|
---|
53 | addPage(new ConclusionPage);
|
---|
54 | //! [0]
|
---|
55 |
|
---|
56 | setPixmap(QWizard::BannerPixmap, QPixmap(":/images/banner.png"));
|
---|
57 | setPixmap(QWizard::BackgroundPixmap, QPixmap(":/images/background.png"));
|
---|
58 |
|
---|
59 | setWindowTitle(tr("Class Wizard"));
|
---|
60 | //! [2]
|
---|
61 | }
|
---|
62 | //! [1] //! [2]
|
---|
63 |
|
---|
64 | //! [3]
|
---|
65 | void ClassWizard::accept()
|
---|
66 | //! [3] //! [4]
|
---|
67 | {
|
---|
68 | QByteArray className = field("className").toByteArray();
|
---|
69 | QByteArray baseClass = field("baseClass").toByteArray();
|
---|
70 | QByteArray macroName = field("macroName").toByteArray();
|
---|
71 | QByteArray baseInclude = field("baseInclude").toByteArray();
|
---|
72 |
|
---|
73 | QString outputDir = field("outputDir").toString();
|
---|
74 | QString header = field("header").toString();
|
---|
75 | QString implementation = field("implementation").toString();
|
---|
76 | //! [4]
|
---|
77 |
|
---|
78 | QByteArray block;
|
---|
79 |
|
---|
80 | if (field("comment").toBool()) {
|
---|
81 | block += "/*\n";
|
---|
82 | block += " " + header.toAscii() + "\n";
|
---|
83 | block += "*/\n";
|
---|
84 | block += "\n";
|
---|
85 | }
|
---|
86 | if (field("protect").toBool()) {
|
---|
87 | block += "#ifndef " + macroName + "\n";
|
---|
88 | block += "#define " + macroName + "\n";
|
---|
89 | block += "\n";
|
---|
90 | }
|
---|
91 | if (field("includeBase").toBool()) {
|
---|
92 | block += "#include " + baseInclude + "\n";
|
---|
93 | block += "\n";
|
---|
94 | }
|
---|
95 |
|
---|
96 | block += "class " + className;
|
---|
97 | if (!baseClass.isEmpty())
|
---|
98 | block += " : public " + baseClass;
|
---|
99 | block += "\n";
|
---|
100 | block += "{\n";
|
---|
101 |
|
---|
102 | /* qmake ignore Q_OBJECT */
|
---|
103 |
|
---|
104 | if (field("qobjectMacro").toBool()) {
|
---|
105 | block += " Q_OBJECT\n";
|
---|
106 | block += "\n";
|
---|
107 | }
|
---|
108 | block += "public:\n";
|
---|
109 |
|
---|
110 | if (field("qobjectCtor").toBool()) {
|
---|
111 | block += " " + className + "(QObject *parent = 0);\n";
|
---|
112 | } else if (field("qwidgetCtor").toBool()) {
|
---|
113 | block += " " + className + "(QWidget *parent = 0);\n";
|
---|
114 | } else if (field("defaultCtor").toBool()) {
|
---|
115 | block += " " + className + "();\n";
|
---|
116 | if (field("copyCtor").toBool()) {
|
---|
117 | block += " " + className + "(const " + className + " &other);\n";
|
---|
118 | block += "\n";
|
---|
119 | block += " " + className + " &operator=" + "(const " + className
|
---|
120 | + " &other);\n";
|
---|
121 | }
|
---|
122 | }
|
---|
123 | block += "};\n";
|
---|
124 |
|
---|
125 | if (field("protect").toBool()) {
|
---|
126 | block += "\n";
|
---|
127 | block += "#endif\n";
|
---|
128 | }
|
---|
129 |
|
---|
130 | QFile headerFile(outputDir + "/" + header);
|
---|
131 | if (!headerFile.open(QFile::WriteOnly | QFile::Text)) {
|
---|
132 | QMessageBox::warning(0, QObject::tr("Simple Wizard"),
|
---|
133 | QObject::tr("Cannot write file %1:\n%2")
|
---|
134 | .arg(headerFile.fileName())
|
---|
135 | .arg(headerFile.errorString()));
|
---|
136 | return;
|
---|
137 | }
|
---|
138 | headerFile.write(block);
|
---|
139 |
|
---|
140 | block.clear();
|
---|
141 |
|
---|
142 | if (field("comment").toBool()) {
|
---|
143 | block += "/*\n";
|
---|
144 | block += " " + implementation.toAscii() + "\n";
|
---|
145 | block += "*/\n";
|
---|
146 | block += "\n";
|
---|
147 | }
|
---|
148 | block += "#include \"" + header.toAscii() + "\"\n";
|
---|
149 | block += "\n";
|
---|
150 |
|
---|
151 | if (field("qobjectCtor").toBool()) {
|
---|
152 | block += className + "::" + className + "(QObject *parent)\n";
|
---|
153 | block += " : " + baseClass + "(parent)\n";
|
---|
154 | block += "{\n";
|
---|
155 | block += "}\n";
|
---|
156 | } else if (field("qwidgetCtor").toBool()) {
|
---|
157 | block += className + "::" + className + "(QWidget *parent)\n";
|
---|
158 | block += " : " + baseClass + "(parent)\n";
|
---|
159 | block += "{\n";
|
---|
160 | block += "}\n";
|
---|
161 | } else if (field("defaultCtor").toBool()) {
|
---|
162 | block += className + "::" + className + "()\n";
|
---|
163 | block += "{\n";
|
---|
164 | block += " // missing code\n";
|
---|
165 | block += "}\n";
|
---|
166 |
|
---|
167 | if (field("copyCtor").toBool()) {
|
---|
168 | block += "\n";
|
---|
169 | block += className + "::" + className + "(const " + className
|
---|
170 | + " &other)\n";
|
---|
171 | block += "{\n";
|
---|
172 | block += " *this = other;\n";
|
---|
173 | block += "}\n";
|
---|
174 | block += "\n";
|
---|
175 | block += className + " &" + className + "::operator=(const "
|
---|
176 | + className + " &other)\n";
|
---|
177 | block += "{\n";
|
---|
178 | if (!baseClass.isEmpty())
|
---|
179 | block += " " + baseClass + "::operator=(other);\n";
|
---|
180 | block += " // missing code\n";
|
---|
181 | block += " return *this;\n";
|
---|
182 | block += "}\n";
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | QFile implementationFile(outputDir + "/" + implementation);
|
---|
187 | if (!implementationFile.open(QFile::WriteOnly | QFile::Text)) {
|
---|
188 | QMessageBox::warning(0, QObject::tr("Simple Wizard"),
|
---|
189 | QObject::tr("Cannot write file %1:\n%2")
|
---|
190 | .arg(implementationFile.fileName())
|
---|
191 | .arg(implementationFile.errorString()));
|
---|
192 | return;
|
---|
193 | }
|
---|
194 | implementationFile.write(block);
|
---|
195 |
|
---|
196 | //! [5]
|
---|
197 | QDialog::accept();
|
---|
198 | //! [5] //! [6]
|
---|
199 | }
|
---|
200 | //! [6]
|
---|
201 |
|
---|
202 | //! [7]
|
---|
203 | IntroPage::IntroPage(QWidget *parent)
|
---|
204 | : QWizardPage(parent)
|
---|
205 | {
|
---|
206 | setTitle(tr("Introduction"));
|
---|
207 | setPixmap(QWizard::WatermarkPixmap, QPixmap(":/images/watermark1.png"));
|
---|
208 |
|
---|
209 | label = new QLabel(tr("This wizard will generate a skeleton C++ class "
|
---|
210 | "definition, including a few functions. You simply "
|
---|
211 | "need to specify the class name and set a few "
|
---|
212 | "options to produce a header file and an "
|
---|
213 | "implementation file for your new C++ class."));
|
---|
214 | label->setWordWrap(true);
|
---|
215 |
|
---|
216 | QVBoxLayout *layout = new QVBoxLayout;
|
---|
217 | layout->addWidget(label);
|
---|
218 | setLayout(layout);
|
---|
219 | }
|
---|
220 | //! [7]
|
---|
221 |
|
---|
222 | //! [8] //! [9]
|
---|
223 | ClassInfoPage::ClassInfoPage(QWidget *parent)
|
---|
224 | : QWizardPage(parent)
|
---|
225 | {
|
---|
226 | //! [8]
|
---|
227 | setTitle(tr("Class Information"));
|
---|
228 | setSubTitle(tr("Specify basic information about the class for which you "
|
---|
229 | "want to generate skeleton source code files."));
|
---|
230 | setPixmap(QWizard::LogoPixmap, QPixmap(":/images/logo1.png"));
|
---|
231 |
|
---|
232 | //! [10]
|
---|
233 | classNameLabel = new QLabel(tr("&Class name:"));
|
---|
234 | classNameLineEdit = new QLineEdit;
|
---|
235 | classNameLabel->setBuddy(classNameLineEdit);
|
---|
236 |
|
---|
237 | baseClassLabel = new QLabel(tr("B&ase class:"));
|
---|
238 | baseClassLineEdit = new QLineEdit;
|
---|
239 | baseClassLabel->setBuddy(baseClassLineEdit);
|
---|
240 |
|
---|
241 | qobjectMacroCheckBox = new QCheckBox(tr("Generate Q_OBJECT ¯o"));
|
---|
242 |
|
---|
243 | //! [10]
|
---|
244 | groupBox = new QGroupBox(tr("C&onstructor"));
|
---|
245 | //! [9]
|
---|
246 |
|
---|
247 | qobjectCtorRadioButton = new QRadioButton(tr("&QObject-style constructor"));
|
---|
248 | qwidgetCtorRadioButton = new QRadioButton(tr("Q&Widget-style constructor"));
|
---|
249 | defaultCtorRadioButton = new QRadioButton(tr("&Default constructor"));
|
---|
250 | copyCtorCheckBox = new QCheckBox(tr("&Generate copy constructor and "
|
---|
251 | "operator="));
|
---|
252 |
|
---|
253 | defaultCtorRadioButton->setChecked(true);
|
---|
254 |
|
---|
255 | connect(defaultCtorRadioButton, SIGNAL(toggled(bool)),
|
---|
256 | copyCtorCheckBox, SLOT(setEnabled(bool)));
|
---|
257 |
|
---|
258 | //! [11] //! [12]
|
---|
259 | registerField("className*", classNameLineEdit);
|
---|
260 | registerField("baseClass", baseClassLineEdit);
|
---|
261 | registerField("qobjectMacro", qobjectMacroCheckBox);
|
---|
262 | //! [11]
|
---|
263 | registerField("qobjectCtor", qobjectCtorRadioButton);
|
---|
264 | registerField("qwidgetCtor", qwidgetCtorRadioButton);
|
---|
265 | registerField("defaultCtor", defaultCtorRadioButton);
|
---|
266 | registerField("copyCtor", copyCtorCheckBox);
|
---|
267 |
|
---|
268 | QVBoxLayout *groupBoxLayout = new QVBoxLayout;
|
---|
269 | //! [12]
|
---|
270 | groupBoxLayout->addWidget(qobjectCtorRadioButton);
|
---|
271 | groupBoxLayout->addWidget(qwidgetCtorRadioButton);
|
---|
272 | groupBoxLayout->addWidget(defaultCtorRadioButton);
|
---|
273 | groupBoxLayout->addWidget(copyCtorCheckBox);
|
---|
274 | groupBox->setLayout(groupBoxLayout);
|
---|
275 |
|
---|
276 | QGridLayout *layout = new QGridLayout;
|
---|
277 | layout->addWidget(classNameLabel, 0, 0);
|
---|
278 | layout->addWidget(classNameLineEdit, 0, 1);
|
---|
279 | layout->addWidget(baseClassLabel, 1, 0);
|
---|
280 | layout->addWidget(baseClassLineEdit, 1, 1);
|
---|
281 | layout->addWidget(qobjectMacroCheckBox, 2, 0, 1, 2);
|
---|
282 | layout->addWidget(groupBox, 3, 0, 1, 2);
|
---|
283 | setLayout(layout);
|
---|
284 | //! [13]
|
---|
285 | }
|
---|
286 | //! [13]
|
---|
287 |
|
---|
288 | //! [14]
|
---|
289 | CodeStylePage::CodeStylePage(QWidget *parent)
|
---|
290 | : QWizardPage(parent)
|
---|
291 | {
|
---|
292 | setTitle(tr("Code Style Options"));
|
---|
293 | setSubTitle(tr("Choose the formatting of the generated code."));
|
---|
294 | setPixmap(QWizard::LogoPixmap, QPixmap(":/images/logo2.png"));
|
---|
295 |
|
---|
296 | commentCheckBox = new QCheckBox(tr("&Start generated files with a "
|
---|
297 | //! [14]
|
---|
298 | "comment"));
|
---|
299 | commentCheckBox->setChecked(true);
|
---|
300 |
|
---|
301 | protectCheckBox = new QCheckBox(tr("&Protect header file against multiple "
|
---|
302 | "inclusions"));
|
---|
303 | protectCheckBox->setChecked(true);
|
---|
304 |
|
---|
305 | macroNameLabel = new QLabel(tr("&Macro name:"));
|
---|
306 | macroNameLineEdit = new QLineEdit;
|
---|
307 | macroNameLabel->setBuddy(macroNameLineEdit);
|
---|
308 |
|
---|
309 | includeBaseCheckBox = new QCheckBox(tr("&Include base class definition"));
|
---|
310 | baseIncludeLabel = new QLabel(tr("Base class include:"));
|
---|
311 | baseIncludeLineEdit = new QLineEdit;
|
---|
312 | baseIncludeLabel->setBuddy(baseIncludeLineEdit);
|
---|
313 |
|
---|
314 | connect(protectCheckBox, SIGNAL(toggled(bool)),
|
---|
315 | macroNameLabel, SLOT(setEnabled(bool)));
|
---|
316 | connect(protectCheckBox, SIGNAL(toggled(bool)),
|
---|
317 | macroNameLineEdit, SLOT(setEnabled(bool)));
|
---|
318 | connect(includeBaseCheckBox, SIGNAL(toggled(bool)),
|
---|
319 | baseIncludeLabel, SLOT(setEnabled(bool)));
|
---|
320 | connect(includeBaseCheckBox, SIGNAL(toggled(bool)),
|
---|
321 | baseIncludeLineEdit, SLOT(setEnabled(bool)));
|
---|
322 |
|
---|
323 | registerField("comment", commentCheckBox);
|
---|
324 | registerField("protect", protectCheckBox);
|
---|
325 | registerField("macroName", macroNameLineEdit);
|
---|
326 | registerField("includeBase", includeBaseCheckBox);
|
---|
327 | registerField("baseInclude", baseIncludeLineEdit);
|
---|
328 |
|
---|
329 | QGridLayout *layout = new QGridLayout;
|
---|
330 | layout->setColumnMinimumWidth(0, 20);
|
---|
331 | layout->addWidget(commentCheckBox, 0, 0, 1, 3);
|
---|
332 | layout->addWidget(protectCheckBox, 1, 0, 1, 3);
|
---|
333 | layout->addWidget(macroNameLabel, 2, 1);
|
---|
334 | layout->addWidget(macroNameLineEdit, 2, 2);
|
---|
335 | layout->addWidget(includeBaseCheckBox, 3, 0, 1, 3);
|
---|
336 | layout->addWidget(baseIncludeLabel, 4, 1);
|
---|
337 | layout->addWidget(baseIncludeLineEdit, 4, 2);
|
---|
338 | //! [15]
|
---|
339 | setLayout(layout);
|
---|
340 | }
|
---|
341 | //! [15]
|
---|
342 |
|
---|
343 | //! [16]
|
---|
344 | void CodeStylePage::initializePage()
|
---|
345 | {
|
---|
346 | QString className = field("className").toString();
|
---|
347 | macroNameLineEdit->setText(className.toUpper() + "_H");
|
---|
348 |
|
---|
349 | QString baseClass = field("baseClass").toString();
|
---|
350 |
|
---|
351 | includeBaseCheckBox->setChecked(!baseClass.isEmpty());
|
---|
352 | includeBaseCheckBox->setEnabled(!baseClass.isEmpty());
|
---|
353 | baseIncludeLabel->setEnabled(!baseClass.isEmpty());
|
---|
354 | baseIncludeLineEdit->setEnabled(!baseClass.isEmpty());
|
---|
355 |
|
---|
356 | if (baseClass.isEmpty()) {
|
---|
357 | baseIncludeLineEdit->clear();
|
---|
358 | } else if (QRegExp("Q[A-Z].*").exactMatch(baseClass)) {
|
---|
359 | baseIncludeLineEdit->setText("<" + baseClass + ">");
|
---|
360 | } else {
|
---|
361 | baseIncludeLineEdit->setText("\"" + baseClass.toLower() + ".h\"");
|
---|
362 | }
|
---|
363 | }
|
---|
364 | //! [16]
|
---|
365 |
|
---|
366 | OutputFilesPage::OutputFilesPage(QWidget *parent)
|
---|
367 | : QWizardPage(parent)
|
---|
368 | {
|
---|
369 | setTitle(tr("Output Files"));
|
---|
370 | setSubTitle(tr("Specify where you want the wizard to put the generated "
|
---|
371 | "skeleton code."));
|
---|
372 | setPixmap(QWizard::LogoPixmap, QPixmap(":/images/logo3.png"));
|
---|
373 |
|
---|
374 | outputDirLabel = new QLabel(tr("&Output directory:"));
|
---|
375 | outputDirLineEdit = new QLineEdit;
|
---|
376 | outputDirLabel->setBuddy(outputDirLineEdit);
|
---|
377 |
|
---|
378 | headerLabel = new QLabel(tr("&Header file name:"));
|
---|
379 | headerLineEdit = new QLineEdit;
|
---|
380 | headerLabel->setBuddy(headerLineEdit);
|
---|
381 |
|
---|
382 | implementationLabel = new QLabel(tr("&Implementation file name:"));
|
---|
383 | implementationLineEdit = new QLineEdit;
|
---|
384 | implementationLabel->setBuddy(implementationLineEdit);
|
---|
385 |
|
---|
386 | registerField("outputDir*", outputDirLineEdit);
|
---|
387 | registerField("header*", headerLineEdit);
|
---|
388 | registerField("implementation*", implementationLineEdit);
|
---|
389 |
|
---|
390 | QGridLayout *layout = new QGridLayout;
|
---|
391 | layout->addWidget(outputDirLabel, 0, 0);
|
---|
392 | layout->addWidget(outputDirLineEdit, 0, 1);
|
---|
393 | layout->addWidget(headerLabel, 1, 0);
|
---|
394 | layout->addWidget(headerLineEdit, 1, 1);
|
---|
395 | layout->addWidget(implementationLabel, 2, 0);
|
---|
396 | layout->addWidget(implementationLineEdit, 2, 1);
|
---|
397 | setLayout(layout);
|
---|
398 | }
|
---|
399 |
|
---|
400 | //! [17]
|
---|
401 | void OutputFilesPage::initializePage()
|
---|
402 | {
|
---|
403 | QString className = field("className").toString();
|
---|
404 | headerLineEdit->setText(className.toLower() + ".h");
|
---|
405 | implementationLineEdit->setText(className.toLower() + ".cpp");
|
---|
406 | outputDirLineEdit->setText(QDir::convertSeparators(QDir::tempPath()));
|
---|
407 | }
|
---|
408 | //! [17]
|
---|
409 |
|
---|
410 | ConclusionPage::ConclusionPage(QWidget *parent)
|
---|
411 | : QWizardPage(parent)
|
---|
412 | {
|
---|
413 | setTitle(tr("Conclusion"));
|
---|
414 | setPixmap(QWizard::WatermarkPixmap, QPixmap(":/images/watermark2.png"));
|
---|
415 |
|
---|
416 | label = new QLabel;
|
---|
417 | label->setWordWrap(true);
|
---|
418 |
|
---|
419 | QVBoxLayout *layout = new QVBoxLayout;
|
---|
420 | layout->addWidget(label);
|
---|
421 | setLayout(layout);
|
---|
422 | }
|
---|
423 |
|
---|
424 | void ConclusionPage::initializePage()
|
---|
425 | {
|
---|
426 | QString finishText = wizard()->buttonText(QWizard::FinishButton);
|
---|
427 | finishText.remove('&');
|
---|
428 | label->setText(tr("Click %1 to generate the class skeleton.")
|
---|
429 | .arg(finishText));
|
---|
430 | }
|
---|