File: classwizard.rb

package info (click to toggle)
qtruby 4%3A4.8.4-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 4,564 kB
  • sloc: ruby: 24,603; cpp: 23,004; makefile: 63; sh: 8
file content (374 lines) | stat: -rw-r--r-- 13,870 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
=begin
**
** Copyright (C) 2004-2005 Trolltech AS. All rights reserved.
**
** This file is part of the example classes of the Qt Toolkit.
**
** This file may be used under the terms of the GNU General Public
** License version 2.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of
** this file.  Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
** http://www.trolltech.com/products/qt/opensource.html
**
** If you are unsure which license is appropriate for your use, please
** review the following information:
** http://www.trolltech.com/products/qt/licensing.html or contact the
** sales department at sales@trolltech.com.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**

** Translated to QtRuby by Richard Dale
=end
    
require 'simplewizard.rb'
    
class ClassWizard < SimpleWizard

    attr_accessor :firstPage, :secondPage, :thirdPage

    def initialize(parent = nil)
        super(parent)
        setNumPages(3)
    end
    
    def createPage(index)
        case index
        when 0:
            @firstPage = FirstPage.new(self)
            return @firstPage
        when 1:
            @secondPage = SecondPage.new(self)
            return @secondPage
        when 2:
            @thirdPage = ThirdPage.new(self)
            return @thirdPage
        end
        return nil
    end
    
    def accept()
        className = @firstPage.classNameLineEdit.text
        baseClass = @firstPage.baseClassLineEdit.text
        qobjectMacro = @firstPage.qobjectMacroCheckBox.checked?
        qobjectCtor = @firstPage.qobjectCtorRadioButton.checked?
        qwidgetCtor = @firstPage.qwidgetCtorRadioButton.checked?
        defaultCtor = @firstPage.defaultCtorRadioButton.checked?
        copyCtor = @firstPage.copyCtorCheckBox.checked?
    
        comment = @secondPage.commentCheckBox.checked?
        protect = @secondPage.protectCheckBox.checked?
        macroName = @secondPage.macroNameLineEdit.text
        includeBase = @secondPage.includeBaseCheckBox.checked?
        baseInclude = @secondPage.baseIncludeLineEdit.text
    
        outputDir = @thirdPage.outputDirLineEdit.text
        header = @thirdPage.headerLineEdit.text
        implementation = @thirdPage.implementationLineEdit.text
    
        block = Qt::ByteArray.new
    
        if comment
            block += "/*\n"
            block += "    " + header + "\n"
            block += "*/\n"
            block += "\n"
        end
        if protect
            block += "#ifndef " + macroName + "\n"
            block += "#define " + macroName + "\n"
            block += "\n"
        end
        if includeBase
            block += "#include " + baseInclude + "\n"
            block += "\n"
        end
    
        block += "class " + className
        if !baseClass.empty?
            block += " : public " + baseClass
        end
        block += "\n"
        block += "{\n"
    
        # qmake ignore Q_OBJECT
    
        if qobjectMacro
            block += "    Q_OBJECT\n"
            block += "\n"
        end
        block += "public:\n"
    
        if qobjectCtor
            block += "    " + className + "(QObject *parent);\n"
        elsif qwidgetCtor
            block += "    " + className + "(QWidget *parent);\n"
        elsif defaultCtor
            block += "    " + className + "();\n"
            if copyCtor
                block += "    " + className + "(const " + className + " &other);\n"
                block += "\n"
                block += "    " + className + " &operator=" + "(const " + className +
                         " &other);\n"
            end
        end
        block += "};\n"
    
        if protect
            block += "\n"
            block += "#endif\n"
        end
    
        headerFile = Qt::File.new(outputDir + "/" + header)
        if !headerFile.open(Qt::File::WriteOnly | Qt::File::Text)
            Qt::MessageBox.warning(self, tr("Simple Wizard"),
                                 tr(    "Cannot write file %s:\n%s" %
                                    [   headerFile.fileName(), headerFile.errorString()] ) )
            return
        end

        headerFile.write(block)
    
        block = Qt::ByteArray.new
    
        if comment
            block += "/*\n"
            block += "    " + implementation + "\n"
            block += "*/\n"
            block += "\n"
        end
        block += "#include \"" + header + "\"\n"
        block += "\n"
    
        if qobjectCtor
            block += className + "::" + className + "(QObject *parent)\n"
            block += "    : " + baseClass + "(parent)\n"
            block += "{\n"
            block += "}\n"
        elsif qwidgetCtor
            block += className + "::" + className + "(QWidget *parent)\n"
            block += "    : " + baseClass + "(parent)\n"
            block += "{\n"
            block += "}\n"
        elsif defaultCtor
            block += className + "::" + className + "()\n"
            block += "{\n"
            block += "    // missing code\n"
            block += "}\n"
    
            if copyCtor
                block += "\n"
                block += className + "::" + className + "(const " + className +
                         " &other)\n"
                block += "{\n"
                block += "    *this = other;\n"
                block += "}\n"
                block += "\n"
                block += className + " &" + className + "::operator=(const " +
                         className + " &other)\n"
                block += "{\n"
                if !baseClass.empty?
                    block += "    " + baseClass + "::operator=(other);\n"
                end
                block += "    // missing code\n"
                block += "    return *this;\n"
                block += "}\n"
            end
        end
    
        implementationFile = Qt::File.new(outputDir + "/" + implementation)

        if !implementationFile.open(Qt::File::WriteOnly | Qt::File::Text)
            Qt::MessageBox.warning(self, tr("Simple Wizard"),
                                 tr("Cannot write file %s:\n%s" %
                                       [    implementationFile.fileName(), 
                                            implementationFile.errorString() ] ) )
            return
        end

        implementationFile.write(block)
        super
    end
end

class FirstPage < Qt::Widget

    attr_accessor :classNameLineEdit, :baseClassLineEdit, :headerLineEdit,
                  :qobjectMacroCheckBox, :qobjectCtorRadioButton,
                  :qwidgetCtorRadioButton, :defaultCtorRadioButton,
                  :copyCtorCheckBox
    
    slots 'classNameChanged()'
    
    def initialize(wizard)
        super(wizard)
        @topLabel = Qt::Label.new(tr("<center><b>Class information</b></center>" +
                                 "<p>This wizard will generate a skeleton class " +
                                 "definition and member function definitions."))
        @topLabel.wordWrap = false
    
        @classNameLabel = Qt::Label.new(tr("Class &name:"))
        @classNameLineEdit = Qt::LineEdit.new
        @classNameLabel.buddy = @classNameLineEdit
        setFocusProxy(@classNameLineEdit)
    
        @baseClassLabel = Qt::Label.new(tr("&Base class:"))
        @baseClassLineEdit = Qt::LineEdit.new
        @baseClassLabel.buddy = @baseClassLineEdit
    
        @qobjectMacroCheckBox = Qt::CheckBox.new(tr("&Generate Q_OBJECT macro"))
    
        @groupBox = Qt::GroupBox.new(tr("&Constructor"))
    
        @qobjectCtorRadioButton = Qt::RadioButton.new(tr("&QObject-style constructor"))
        @qwidgetCtorRadioButton = Qt::RadioButton.new(tr("Q&Widget-style constructor"))
        @defaultCtorRadioButton = Qt::RadioButton.new(tr("&Default constructor"))
        @copyCtorCheckBox = Qt::CheckBox.new(tr("&Also generate copy constructor and " +
                                            "assignment operator"))
    
        @defaultCtorRadioButton.checked = true
    
        connect(@classNameLineEdit, SIGNAL('textChanged(const QString &)'),
                self, SLOT('classNameChanged()'))
        connect(@defaultCtorRadioButton, SIGNAL('toggled(bool)'),
                @copyCtorCheckBox, SLOT('setEnabled(bool)'))
    
        wizard.buttonEnabled = false
    
        @groupBox.layout = Qt::VBoxLayout.new do |g|
            g.addWidget(@qobjectCtorRadioButton)
            g.addWidget(@qwidgetCtorRadioButton)
            g.addWidget(@defaultCtorRadioButton)
            g.addWidget(@copyCtorCheckBox)
        end
    
        self.layout = Qt::GridLayout.new do |l|
            l.addWidget(@topLabel, 0, 0, 1, 2)
            l.setRowMinimumHeight(1, 10)
            l.addWidget(@classNameLabel, 2, 0)
            l.addWidget(@classNameLineEdit, 2, 1)
            l.addWidget(@baseClassLabel, 3, 0)
            l.addWidget(@baseClassLineEdit, 3, 1)
            l.addWidget(@qobjectMacroCheckBox, 4, 0, 1, 2)
            l.addWidget(@groupBox, 5, 0, 1, 2)
            l.setRowStretch(6, 1)
        end
    end
    
    def classNameChanged()
        wizard = parent()
        wizard.buttonEnabled = !@classNameLineEdit.text.empty?
    end
end

class SecondPage < Qt::Widget

    attr_accessor :commentCheckBox, :protectCheckBox, :includeBaseCheckBox,
                  :macroNameLineEdit, :baseIncludeLineEdit,
                  :protectCheckBox, :commentCheckBox, :includeBaseCheckBox

    def initialize(wizard)
        super(wizard)
        @topLabel = Qt::Label.new(tr("<center><b>Code style options</b></center>"))
    
        @commentCheckBox = Qt::CheckBox.new(tr("&Start generated files with a comment"))
        @commentCheckBox.checked = true
        setFocusProxy(@commentCheckBox)
    
        @protectCheckBox = Qt::CheckBox.new(tr("&Protect header file against multiple " +
                                           "inclusions"))
        @protectCheckBox.checked = true
    
        @macroNameLabel = Qt::Label.new(tr("&Macro name:"))
        @macroNameLineEdit = Qt::LineEdit.new
        @macroNameLabel.buddy = @macroNameLineEdit
    
        @includeBaseCheckBox = Qt::CheckBox.new(tr("&Include base class definition"))
        @baseIncludeLabel = Qt::Label.new(tr("Base class include:"))
        @baseIncludeLineEdit = Qt::LineEdit.new
        @baseIncludeLabel.buddy = @baseIncludeLineEdit
    
        className = wizard.firstPage.classNameLineEdit.text()
        @macroNameLineEdit.text = className.upcase + "_H"
    
        baseClass = wizard.firstPage.baseClassLineEdit.text()
        if baseClass.empty?
            @includeBaseCheckBox.enabled = false
            @baseIncludeLabel.enabled = false
            @baseIncludeLineEdit.enabled = false
        else
            @includeBaseCheckBox.checked = true
            if Regexp.new("Q[A-Z].*").match(baseClass)
                @baseIncludeLineEdit.text = "<" + baseClass + ">"
            else
                @baseIncludeLineEdit.text = '"' + baseClass.downcase + '.h"'
            end
        end
    
        connect(@protectCheckBox, SIGNAL('toggled(bool)'),
                @macroNameLabel, SLOT('setEnabled(bool)'))
        connect(@protectCheckBox, SIGNAL('toggled(bool)'),
                @macroNameLineEdit, SLOT('setEnabled(bool)'))
        connect(@includeBaseCheckBox, SIGNAL('toggled(bool)'),
                @baseIncludeLabel, SLOT('setEnabled(bool)'))
        connect(@includeBaseCheckBox, SIGNAL('toggled(bool)'),
                @baseIncludeLineEdit, SLOT('setEnabled(bool)'))
    
        self.layout = Qt::GridLayout.new do |l|
            l.setColumnMinimumWidth(0, 20)
            l.addWidget(@topLabel, 0, 0, 1, 3)
            l.setRowMinimumHeight(1, 10)
            l.addWidget(@commentCheckBox, 2, 0, 1, 3)
            l.addWidget(@protectCheckBox, 3, 0, 1, 3)
            l.addWidget(@macroNameLabel, 4, 1)
            l.addWidget(@macroNameLineEdit, 4, 2)
            l.addWidget(@includeBaseCheckBox, 5, 0, 1, 3)
            l.addWidget(@baseIncludeLabel, 6, 1)
            l.addWidget(@baseIncludeLineEdit, 6, 2)
            l.setRowStretch(7, 1)
        end
    end
end


class ThirdPage < Qt::Widget

    attr_accessor :outputDirLineEdit, :headerLineEdit, :implementationLineEdit

    def initialize(wizard)
        super(wizard)
        @topLabel = Qt::Label.new(tr("<center><b>Output files</b></center>"))
    
        @outputDirLabel = Qt::Label.new(tr("&Output directory:"))
        @outputDirLineEdit = Qt::LineEdit.new
        @outputDirLabel.buddy = @outputDirLineEdit
        self.focusProxy = @outputDirLineEdit
    
        @headerLabel = Qt::Label.new(tr("&Header file name:"))
        @headerLineEdit = Qt::LineEdit.new
        @headerLabel.buddy = @headerLineEdit
    
        @implementationLabel = Qt::Label.new(tr("&Implementation file name:"))
        @implementationLineEdit = Qt::LineEdit.new
        @implementationLabel.buddy = @implementationLineEdit
    
        className = wizard.firstPage.classNameLineEdit.text()
        @headerLineEdit.text = className.downcase + ".h"
        @implementationLineEdit.text = className.downcase + ".cpp"
        @outputDirLineEdit.text = Qt::Dir.convertSeparators(Qt::Dir.homePath())
    
        self.layout = Qt::GridLayout.new do |l|
            l.addWidget(@topLabel, 0, 0, 1, 2)
            l.setRowMinimumHeight(1, 10)
            l.addWidget(@outputDirLabel, 2, 0)
            l.addWidget(@outputDirLineEdit, 2, 1)
            l.addWidget(@headerLabel, 3, 0)
            l.addWidget(@headerLineEdit, 3, 1)
            l.addWidget(@implementationLabel, 4, 0)
            l.addWidget(@implementationLineEdit, 4, 1)
            l.setRowStretch(5, 1)
        end
    end
end