145
|
1 /****************************************************************************
|
|
2 **
|
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 ** Contact: Qt Software Information (qt-info@nokia.com)
|
|
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 qt-sales@nokia.com.
|
|
38 ** $QT_END_LICENSE$
|
|
39 **
|
|
40 ****************************************************************************/
|
|
41 module classwizard;
|
|
42
|
|
43
|
|
44 import qt.gui.QWizardPage;
|
|
45 import qt.gui.QCheckBox;
|
|
46 import qt.gui.QGroupBox;
|
|
47 import qt.gui.QLabel;
|
|
48 import qt.gui.QLineEdit;
|
|
49 import qt.gui.QRadioButton;
|
|
50 import qt.gui.QMessageBox;
|
|
51 import qt.gui.QVBoxLayout;
|
|
52 import qt.gui.QGridLayout;
|
|
53 import qt.core.QFile;
|
|
54 import qt.core.QDir;
|
|
55 import qt.core.QRegExp;
|
|
56
|
211
|
57 version(Tango)
|
|
58 {
|
|
59 import tango.text.convert.Format: Format = format;
|
|
60 import tango.core.Array;
|
|
61 import tango.text.Ascii;
|
|
62 alias toUpper toupper;
|
|
63 alias toLower tolower;
|
|
64
|
|
65 const string cannot_write_file_str = tr("Cannot write file {}:\n{}");
|
|
66 }
|
|
67 else {
|
|
68 import std.string;
|
|
69 import std.algorithm;
|
|
70 const string cannot_write_file_str = tr("Cannot write file %s:\n%s");
|
|
71 }
|
145
|
72
|
|
73
|
|
74 class ClassWizard : public QWizard
|
|
75 {
|
|
76 public:
|
|
77
|
|
78 this(QWidget parent = null)
|
|
79 {
|
|
80 super(parent);
|
|
81 addPage(new IntroPage);
|
|
82 addPage(new ClassInfoPage);
|
|
83 addPage(new CodeStylePage);
|
|
84 addPage(new OutputFilesPage);
|
|
85 addPage(new ConclusionPage);
|
|
86
|
|
87 setPixmap(QWizard.BannerPixmap, new QPixmap(":/images/banner.png"));
|
|
88 setPixmap(QWizard.BackgroundPixmap, new QPixmap(":/images/background.png"));
|
|
89
|
|
90 setWindowTitle(tr("Class Wizard"));
|
|
91 }
|
|
92
|
|
93 void accept()
|
|
94 {
|
211
|
95 version(Tango)
|
|
96 const string cannot_write_file_str = tr("Cannot write file {}:\n{}");
|
|
97 else
|
|
98 const string cannot_write_file_str = tr("Cannot write file %s:\n%s");
|
|
99
|
|
100
|
145
|
101 string className = field("className").toString();
|
|
102 string baseClass = field("baseClass").toString();
|
|
103 string macroName = field("macroName").toString();
|
|
104 string baseInclude = field("baseInclude").toString();
|
|
105
|
|
106 string outputDir = field("outputDir").toString();
|
|
107 string header = field("header").toString();
|
|
108 string implementation = field("implementation").toString();
|
|
109
|
|
110 string block;
|
|
111
|
|
112 if (field("comment").toBool()) {
|
|
113 block ~= "/*\n";
|
|
114 block ~= " " ~ header ~ "\n";
|
|
115 block ~= "*/\n";
|
|
116 block ~= "\n";
|
|
117 }
|
|
118 if (field("protect").toBool()) {
|
|
119 block ~= "#ifndef " ~ macroName ~ "\n";
|
|
120 block ~= "#define " ~ macroName ~ "\n";
|
|
121 block ~= "\n";
|
|
122 }
|
|
123 if (field("includeBase").toBool()) {
|
|
124 block ~= "#include " ~ baseInclude ~ "\n";
|
|
125 block ~= "\n";
|
|
126 }
|
|
127
|
|
128 block ~= "class " ~ className;
|
|
129 if (baseClass.length)
|
|
130 block ~= " : public " ~ baseClass;
|
|
131 block ~= "\n";
|
|
132 block ~= "{\n";
|
|
133
|
|
134 /* qmake ignore */
|
|
135
|
|
136 if (field("qobjectMacro").toBool()) {
|
|
137 block ~= " \n";
|
|
138 block ~= "\n";
|
|
139 }
|
|
140 block ~= "public:\n";
|
|
141
|
|
142 if (field("qobjectCtor").toBool()) {
|
|
143 block ~= " " ~ className ~ "(QObject *parent = 0);\n";
|
|
144 } else if (field("qwidgetCtor").toBool()) {
|
|
145 block ~= " " ~ className ~ "(QWidget *parent = 0);\n";
|
|
146 } else if (field("defaultCtor").toBool()) {
|
|
147 block ~= " " ~ className ~ "();\n";
|
|
148 if (field("copyCtor").toBool()) {
|
|
149 block ~= " " ~ className ~ "(const " ~ className ~ " &other);\n";
|
|
150 block ~= "\n";
|
|
151 block ~= " " ~ className ~ " &operator=" ~ "(const " ~ className ~ " &other);\n";
|
|
152 }
|
|
153 }
|
|
154 block ~= "};\n";
|
|
155
|
|
156 if (field("protect").toBool()) {
|
|
157 block ~= "\n";
|
|
158 block ~= "#endif\n";
|
|
159 }
|
|
160
|
|
161 auto headerFile = new QFile(outputDir ~ "/" ~ header);
|
|
162 if (!headerFile.open(QFile.WriteOnly | QFile.Text)) {
|
|
163 QMessageBox.warning(null, tr("Simple Wizard"),
|
211
|
164 format(cannot_write_file_str,
|
145
|
165 headerFile.fileName(),
|
|
166 headerFile.errorString()));
|
|
167 return;
|
|
168 }
|
180
|
169 headerFile.write(block);
|
145
|
170
|
|
171 block.length = 0;
|
|
172
|
|
173 if (field("comment").toBool()) {
|
|
174 block ~= "/*\n";
|
|
175 block ~= " " ~ implementation ~ "\n";
|
|
176 block ~= "*/\n";
|
|
177 block ~= "\n";
|
|
178 }
|
|
179 block ~= "#include \"" ~ header ~ "\"\n";
|
|
180 block ~= "\n";
|
|
181
|
|
182 if (field("qobjectCtor").toBool()) {
|
|
183 block ~= className ~ "." ~ className ~ "(QObject *parent)\n";
|
|
184 block ~= " : " ~ baseClass ~ "(parent)\n";
|
|
185 block ~= "{\n";
|
|
186 block ~= "}\n";
|
|
187 } else if (field("qwidgetCtor").toBool()) {
|
|
188 block ~= className ~ "." ~ className ~ "(QWidget *parent)\n";
|
|
189 block ~= " : " ~ baseClass ~ "(parent)\n";
|
|
190 block ~= "{\n";
|
|
191 block ~= "}\n";
|
|
192 } else if (field("defaultCtor").toBool()) {
|
|
193 block ~= className ~ "." ~ className ~ "()\n";
|
|
194 block ~= "{\n";
|
|
195 block ~= " // missing code\n";
|
|
196 block ~= "}\n";
|
|
197
|
|
198 if (field("copyCtor").toBool()) {
|
|
199 block ~= "\n";
|
|
200 block ~= className ~ "." ~ className ~ "(const " ~ className ~ " &other)\n";
|
|
201 block ~= "{\n";
|
|
202 block ~= " *this = other;\n";
|
|
203 block ~= "}\n";
|
|
204 block ~= "\n";
|
|
205 block ~= className ~ " &" ~ className ~ ".operator=(const " ~ className ~ " &other)\n";
|
|
206 block ~= "{\n";
|
|
207 if (baseClass.length)
|
|
208 block ~= " " ~ baseClass ~ ".operator=(other);\n";
|
|
209 block ~= " // missing code\n";
|
|
210 block ~= " return *this;\n";
|
|
211 block ~= "}\n";
|
|
212 }
|
|
213 }
|
|
214
|
|
215 auto implementationFile = new QFile(outputDir ~ "/" ~ implementation);
|
|
216 if (!implementationFile.open(QFile.WriteOnly | QFile.Text)) {
|
|
217 QMessageBox.warning(null, tr("Simple Wizard"),
|
211
|
218 format(cannot_write_file_str,
|
145
|
219 implementationFile.fileName(),
|
|
220 implementationFile.errorString()));
|
|
221 return;
|
|
222 }
|
180
|
223 implementationFile.write(block);
|
145
|
224
|
|
225 QDialog.accept();
|
|
226 }
|
|
227 }
|
|
228
|
|
229
|
|
230 class IntroPage : public QWizardPage
|
|
231 {
|
|
232 public:
|
|
233
|
|
234 this(QWidget parent = null)
|
|
235 {
|
|
236 super(parent);
|
|
237 setTitle(tr("Introduction"));
|
|
238 setPixmap(QWizard.WatermarkPixmap, new QPixmap(":/images/watermark1.png"));
|
|
239
|
|
240 label = new QLabel(tr("This wizard will generate a skeleton C++ class "
|
|
241 "definition, including a few functions. You simply "
|
|
242 "need to specify the class name and set a few "
|
|
243 "options to produce a header file and an "
|
|
244 "implementation file for your new C++ class."));
|
|
245 label.setWordWrap(true);
|
|
246
|
|
247 QVBoxLayout layout = new QVBoxLayout;
|
|
248 layout.addWidget(label);
|
|
249 setLayout(layout);
|
|
250 }
|
|
251
|
|
252 private:
|
|
253
|
|
254 QLabel label;
|
|
255 }
|
|
256
|
|
257 class ClassInfoPage : public QWizardPage
|
|
258 {
|
|
259 public:
|
|
260
|
|
261 this(QWidget parent = null)
|
|
262 {
|
|
263 super(parent);
|
|
264 setTitle(tr("Class Information"));
|
|
265 setSubTitle(tr("Specify basic information about the class for which you want to generate skeleton source code files."));
|
|
266 setPixmap(QWizard.LogoPixmap, new QPixmap(":/images/logo1.png"));
|
|
267
|
|
268 classNameLabel = new QLabel(tr("&Class name:"));
|
|
269 classNameLineEdit = new QLineEdit;
|
|
270 classNameLabel.setBuddy(classNameLineEdit);
|
|
271
|
|
272 baseClassLabel = new QLabel(tr("B&ase class:"));
|
|
273 baseClassLineEdit = new QLineEdit;
|
|
274 baseClassLabel.setBuddy(baseClassLineEdit);
|
|
275
|
|
276 qobjectMacroCheckBox = new QCheckBox(tr("Generate ¯o"));
|
|
277
|
|
278 groupBox = new QGroupBox(tr("C&onstructor"));
|
|
279
|
|
280 qobjectCtorRadioButton = new QRadioButton(tr("&QObject-style constructor"));
|
|
281 qwidgetCtorRadioButton = new QRadioButton(tr("Q&Widget-style constructor"));
|
|
282 defaultCtorRadioButton = new QRadioButton(tr("&Default constructor"));
|
|
283 copyCtorCheckBox = new QCheckBox(tr("&Generate copy constructor and operator="));
|
|
284
|
|
285 defaultCtorRadioButton.setChecked(true);
|
|
286 defaultCtorRadioButton.toggled.connect(©CtorCheckBox.setEnabled);
|
|
287
|
|
288 registerField("className*", classNameLineEdit);
|
|
289 registerField("baseClass", baseClassLineEdit);
|
|
290 registerField("qobjectMacro", qobjectMacroCheckBox);
|
|
291 registerField("qobjectCtor", qobjectCtorRadioButton);
|
|
292 registerField("qwidgetCtor", qwidgetCtorRadioButton);
|
|
293 registerField("defaultCtor", defaultCtorRadioButton);
|
|
294 registerField("copyCtor", copyCtorCheckBox);
|
|
295
|
|
296 QVBoxLayout groupBoxLayout = new QVBoxLayout;
|
|
297
|
|
298 groupBoxLayout.addWidget(qobjectCtorRadioButton);
|
|
299 groupBoxLayout.addWidget(qwidgetCtorRadioButton);
|
|
300 groupBoxLayout.addWidget(defaultCtorRadioButton);
|
|
301 groupBoxLayout.addWidget(copyCtorCheckBox);
|
|
302 groupBox.setLayout(groupBoxLayout);
|
|
303
|
|
304 QGridLayout layout = new QGridLayout;
|
|
305 layout.addWidget(classNameLabel, 0, 0);
|
|
306 layout.addWidget(classNameLineEdit, 0, 1);
|
|
307 layout.addWidget(baseClassLabel, 1, 0);
|
|
308 layout.addWidget(baseClassLineEdit, 1, 1);
|
|
309 layout.addWidget(qobjectMacroCheckBox, 2, 0, 1, 2);
|
|
310 layout.addWidget(groupBox, 3, 0, 1, 2);
|
|
311 setLayout(layout);
|
|
312 }
|
|
313
|
|
314 private:
|
|
315
|
|
316 QLabel classNameLabel;
|
|
317 QLabel baseClassLabel;
|
|
318 QLineEdit classNameLineEdit;
|
|
319 QLineEdit baseClassLineEdit;
|
|
320 QCheckBox qobjectMacroCheckBox;
|
|
321 QGroupBox groupBox;
|
|
322 QRadioButton qobjectCtorRadioButton;
|
|
323 QRadioButton qwidgetCtorRadioButton;
|
|
324 QRadioButton defaultCtorRadioButton;
|
|
325 QCheckBox copyCtorCheckBox;
|
|
326 }
|
|
327
|
|
328 class CodeStylePage : public QWizardPage
|
|
329 {
|
|
330 public:
|
|
331
|
|
332 this(QWidget parent = null)
|
|
333 {
|
|
334 super(parent);
|
|
335 setTitle(tr("Code Style Options"));
|
|
336 setSubTitle(tr("Choose the formatting of the generated code."));
|
|
337 setPixmap(QWizard.LogoPixmap, new QPixmap(":/images/logo2.png"));
|
|
338
|
|
339 commentCheckBox = new QCheckBox(tr("&Start generated files with a comment"));
|
|
340 commentCheckBox.setChecked(true);
|
|
341
|
|
342 protectCheckBox = new QCheckBox(tr("&Protect header file against multiple inclusions"));
|
|
343 protectCheckBox.setChecked(true);
|
|
344
|
|
345 macroNameLabel = new QLabel(tr("&Macro name:"));
|
|
346 macroNameLineEdit = new QLineEdit;
|
|
347 macroNameLabel.setBuddy(macroNameLineEdit);
|
|
348
|
|
349 includeBaseCheckBox = new QCheckBox(tr("&Include base class definition"));
|
|
350 baseIncludeLabel = new QLabel(tr("Base class include:"));
|
|
351 baseIncludeLineEdit = new QLineEdit;
|
|
352 baseIncludeLabel.setBuddy(baseIncludeLineEdit);
|
|
353
|
|
354 protectCheckBox.toggled.connect(¯oNameLabel.setEnabled);
|
|
355 protectCheckBox.toggled.connect(¯oNameLabel.setEnabled);
|
|
356 includeBaseCheckBox.toggled.connect(¯oNameLabel.setEnabled);
|
|
357 includeBaseCheckBox.toggled.connect(¯oNameLabel.setEnabled);
|
|
358
|
|
359 registerField("comment", commentCheckBox);
|
|
360 registerField("protect", protectCheckBox);
|
|
361 registerField("macroName", macroNameLineEdit);
|
|
362 registerField("includeBase", includeBaseCheckBox);
|
|
363 registerField("baseInclude", baseIncludeLineEdit);
|
|
364
|
|
365 QGridLayout layout = new QGridLayout;
|
|
366 layout.setColumnMinimumWidth(0, 20);
|
|
367 layout.addWidget(commentCheckBox, 0, 0, 1, 3);
|
|
368 layout.addWidget(protectCheckBox, 1, 0, 1, 3);
|
|
369 layout.addWidget(macroNameLabel, 2, 1);
|
|
370 layout.addWidget(macroNameLineEdit, 2, 2);
|
|
371 layout.addWidget(includeBaseCheckBox, 3, 0, 1, 3);
|
|
372 layout.addWidget(baseIncludeLabel, 4, 1);
|
|
373 layout.addWidget(baseIncludeLineEdit, 4, 2);
|
|
374
|
|
375 setLayout(layout);
|
|
376 }
|
|
377
|
|
378 protected:
|
|
379
|
|
380 void initializePage()
|
|
381 {
|
|
382 string className = field("className").toString();
|
211
|
383 macroNameLineEdit.setText(toupper(className) ~ "_H");
|
145
|
384
|
|
385 string baseClass = field("baseClass").toString();
|
|
386
|
|
387 includeBaseCheckBox.setChecked(baseClass.length != 0);
|
|
388 includeBaseCheckBox.setEnabled(baseClass.length != 0);
|
|
389 baseIncludeLabel.setEnabled(baseClass.length != 0);
|
|
390 baseIncludeLineEdit.setEnabled(baseClass.length != 0);
|
|
391
|
|
392 if (baseClass.length == 0) {
|
|
393 baseIncludeLineEdit.clear();
|
|
394 } else if ((new QRegExp("Q[A-Z].*")).exactMatch(baseClass)) {
|
|
395 baseIncludeLineEdit.setText("<" ~ baseClass ~ ">");
|
|
396 } else {
|
211
|
397 baseIncludeLineEdit.setText("\"" ~ tolower(baseClass) ~ ".h\"");
|
145
|
398 }
|
|
399 }
|
|
400
|
|
401 private:
|
|
402
|
|
403 QCheckBox commentCheckBox;
|
|
404 QCheckBox protectCheckBox;
|
|
405 QCheckBox includeBaseCheckBox;
|
|
406 QLabel macroNameLabel;
|
|
407 QLabel baseIncludeLabel;
|
|
408 QLineEdit macroNameLineEdit;
|
|
409 QLineEdit baseIncludeLineEdit;
|
|
410 }
|
|
411
|
|
412 class OutputFilesPage : public QWizardPage
|
|
413 {
|
|
414 public:
|
|
415
|
|
416 this(QWidget parent = null)
|
|
417 {
|
|
418 super(parent);
|
|
419 setTitle(tr("Output Files"));
|
|
420 setSubTitle(tr("Specify where you want the wizard to put the generated skeleton code."));
|
|
421 setPixmap(QWizard.LogoPixmap, new QPixmap(":/images/logo3.png"));
|
|
422
|
|
423 outputDirLabel = new QLabel(tr("&Output directory:"));
|
|
424 outputDirLineEdit = new QLineEdit;
|
|
425 outputDirLabel.setBuddy(outputDirLineEdit);
|
|
426
|
|
427 headerLabel = new QLabel(tr("&Header file name:"));
|
|
428 headerLineEdit = new QLineEdit;
|
|
429 headerLabel.setBuddy(headerLineEdit);
|
|
430
|
|
431 implementationLabel = new QLabel(tr("&Implementation file name:"));
|
|
432 implementationLineEdit = new QLineEdit;
|
|
433 implementationLabel.setBuddy(implementationLineEdit);
|
|
434
|
|
435 registerField("outputDir*", outputDirLineEdit);
|
|
436 registerField("header*", headerLineEdit);
|
|
437 registerField("implementation*", implementationLineEdit);
|
|
438
|
|
439 QGridLayout layout = new QGridLayout;
|
|
440 layout.addWidget(outputDirLabel, 0, 0);
|
|
441 layout.addWidget(outputDirLineEdit, 0, 1);
|
|
442 layout.addWidget(headerLabel, 1, 0);
|
|
443 layout.addWidget(headerLineEdit, 1, 1);
|
|
444 layout.addWidget(implementationLabel, 2, 0);
|
|
445 layout.addWidget(implementationLineEdit, 2, 1);
|
|
446 setLayout(layout);
|
|
447 }
|
|
448
|
|
449 protected:
|
|
450
|
|
451 void initializePage()
|
|
452 {
|
|
453 string className = field("className").toString();
|
211
|
454 headerLineEdit.setText(tolower(className) ~ ".h");
|
|
455 implementationLineEdit.setText(tolower(className) ~ ".cpp");
|
145
|
456 outputDirLineEdit.setText(QDir.convertSeparators(QDir.tempPath()));
|
|
457 }
|
|
458
|
|
459 private:
|
|
460
|
|
461 QLabel outputDirLabel;
|
|
462 QLabel headerLabel;
|
|
463 QLabel implementationLabel;
|
|
464 QLineEdit outputDirLineEdit;
|
|
465 QLineEdit headerLineEdit;
|
|
466 QLineEdit implementationLineEdit;
|
|
467 }
|
|
468
|
|
469 class ConclusionPage : public QWizardPage
|
|
470 {
|
|
471 public:
|
|
472
|
|
473 this(QWidget parent = null)
|
|
474 {
|
|
475 super(parent);
|
|
476 setTitle(tr("Conclusion"));
|
|
477 setPixmap(QWizard.WatermarkPixmap, new QPixmap(":/images/watermark2.png"));
|
|
478
|
|
479 label = new QLabel;
|
|
480 label.setWordWrap(true);
|
|
481
|
|
482 QVBoxLayout layout = new QVBoxLayout;
|
|
483 layout.addWidget(label);
|
|
484 setLayout(layout);
|
|
485 }
|
|
486
|
|
487 protected:
|
|
488
|
|
489 void initializePage()
|
|
490 {
|
211
|
491 string finishText = wizard().buttonText(QWizard.FinishButton);
|
|
492 version(Tango)
|
|
493 {
|
|
494 auto pos = remove(finishText, '&');
|
|
495 const string str = tr("Click {} to generate the class skeleton.");
|
|
496 }
|
|
497 else
|
|
498 {
|
|
499 const string str = tr("Click %s to generate the class skeleton.");
|
|
500 // TODO: port to D2.
|
|
501 }
|
|
502 label.setText(format(str, finishText));
|
145
|
503 }
|
|
504
|
|
505 private:
|
|
506
|
|
507 QLabel label;
|
|
508 }
|