comparison examples/dialogs/classwizard/classwizard.d @ 145:7648ee2e023b

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