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