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