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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
|
/***************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Qt Software Information (qt-info@nokia.com)
**
**
** Non-Open Source Usage
**
** Licensees may use this file in accordance with the Qt Beta Version
** License Agreement, Agreement version 2.2 provided with the Software or,
** alternatively, in accordance with the terms contained in a written
** agreement between you and Nokia.
**
** GNU General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.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.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt GPL Exception
** version 1.2, included in the file GPL_EXCEPTION.txt in this package.
**
***************************************************************************/
***************************************************************************/
#include "newclasswidget.h"
#include "ui_newclasswidget.h"
#include <utils/filewizardpage.h>
#include <QtGui/QFileDialog>
#include <QtCore/QFileInfo>
#include <QtCore/QStringList>
#include <QtCore/QDir>
#include <QtCore/QDebug>
#include <QtCore/QRegExp>
enum { debugNewClassWidget = 0 };
namespace Core {
namespace Utils {
struct NewClassWidgetPrivate {
NewClassWidgetPrivate();
Ui::NewClassWidget m_ui;
QString m_headerExtension;
QString m_sourceExtension;
QString m_formExtension;
bool m_valid;
bool m_classEdited;
// Store the "visible" values to prevent the READ accessors from being
// fooled by a temporarily hidden widget
bool m_baseClassInputVisible;
bool m_formInputVisible;
bool m_pathInputVisible;
bool m_formInputCheckable;
};
NewClassWidgetPrivate:: NewClassWidgetPrivate() :
m_headerExtension(QLatin1String("h")),
m_sourceExtension(QLatin1String("cpp")),
m_formExtension(QLatin1String("ui")),
m_valid(false),
m_classEdited(false),
m_baseClassInputVisible(true),
m_formInputVisible(true),
m_pathInputVisible(true),
m_formInputCheckable(false)
{
}
// --------------------- NewClassWidget
NewClassWidget::NewClassWidget(QWidget *parent) :
QWidget(parent),
m_d(new NewClassWidgetPrivate)
{
m_d->m_ui.setupUi(this);
m_d->m_ui.baseClassComboBox->setEditable(false);
connect(m_d->m_ui.classLineEdit, SIGNAL(updateFileName(QString)),
this, SLOT(updateFileNames(QString)));
connect(m_d->m_ui.classLineEdit, SIGNAL(textEdited(QString)),
this, SLOT(classNameEdited()));
connect(m_d->m_ui.baseClassComboBox, SIGNAL(currentIndexChanged(int)),
this, SLOT(suggestClassNameFromBase()));
connect(m_d->m_ui.baseClassComboBox, SIGNAL(editTextChanged(QString)),
this, SLOT(slotValidChanged()));
connect(m_d->m_ui.classLineEdit, SIGNAL(validChanged()),
this, SLOT(slotValidChanged()));
connect(m_d->m_ui.headerFileLineEdit, SIGNAL(validChanged()),
this, SLOT(slotValidChanged()));
connect(m_d->m_ui.sourceFileLineEdit, SIGNAL(validChanged()),
this, SLOT(slotValidChanged()));
connect(m_d->m_ui.formFileLineEdit, SIGNAL(validChanged()),
this, SLOT(slotValidChanged()));
connect(m_d->m_ui.pathChooser, SIGNAL(validChanged()),
this, SLOT(slotValidChanged()));
connect(m_d->m_ui.classLineEdit, SIGNAL(validReturnPressed()),
this, SLOT(slotActivated()));
connect(m_d->m_ui.headerFileLineEdit, SIGNAL(validReturnPressed()),
this, SLOT(slotActivated()));
connect(m_d->m_ui.sourceFileLineEdit, SIGNAL(validReturnPressed()),
this, SLOT(slotActivated()));
connect(m_d->m_ui.formFileLineEdit, SIGNAL(validReturnPressed()),
this, SLOT(slotActivated()));
connect(m_d->m_ui.formFileLineEdit, SIGNAL(validReturnPressed()),
this, SLOT(slotActivated()));
connect(m_d->m_ui.pathChooser, SIGNAL(returnPressed()),
this, SLOT(slotActivated()));
connect(m_d->m_ui.generateFormCheckBox, SIGNAL(stateChanged(int)),
this, SLOT(slotFormInputChecked()));
m_d->m_ui.generateFormCheckBox->setChecked(true);
setFormInputCheckable(false, true);
}
NewClassWidget::~NewClassWidget()
{
delete m_d;
}
void NewClassWidget::classNameEdited()
{
if (debugNewClassWidget)
qDebug() << Q_FUNC_INFO << m_d->m_headerExtension << m_d->m_sourceExtension;
m_d->m_classEdited = true;
}
void NewClassWidget::suggestClassNameFromBase()
{
if (debugNewClassWidget)
qDebug() << Q_FUNC_INFO << m_d->m_headerExtension << m_d->m_sourceExtension;
if (m_d->m_classEdited)
return;
// Suggest a class unless edited ("QMainWindow"->"MainWindow")
QString base = baseClassName();
if (base.startsWith(QLatin1Char('Q'))) {
base.remove(0, 1);
setClassName(base);
}
}
QStringList NewClassWidget::baseClassChoices() const
{
QStringList rc;
const int count = m_d->m_ui.baseClassComboBox->count();
for (int i = 0; i < count; i++)
rc.push_back(m_d->m_ui.baseClassComboBox->itemText(i));
return rc;
}
void NewClassWidget::setBaseClassChoices(const QStringList &choices)
{
m_d->m_ui.baseClassComboBox->clear();
m_d->m_ui.baseClassComboBox->addItems(choices);
}
void NewClassWidget::setBaseClassInputVisible(bool visible)
{
m_d->m_baseClassInputVisible = visible;
m_d->m_ui.baseClassLabel->setVisible(visible);
m_d->m_ui.baseClassComboBox->setVisible(visible);
}
void NewClassWidget::setBaseClassEditable(bool editable)
{
m_d->m_ui.baseClassComboBox->setEditable(editable);
}
bool NewClassWidget::isBaseClassInputVisible() const
{
return m_d->m_baseClassInputVisible;
}
bool NewClassWidget::isBaseClassEditable() const
{
return m_d->m_ui.baseClassComboBox->isEditable();
}
void NewClassWidget::setFormInputVisible(bool visible)
{
m_d->m_formInputVisible = visible;
m_d->m_ui.formLabel->setVisible(visible);
m_d->m_ui.formFileLineEdit->setVisible(visible);
}
bool NewClassWidget::isFormInputVisible() const
{
return m_d->m_formInputVisible;
}
void NewClassWidget::setFormInputCheckable(bool checkable)
{
setFormInputCheckable(checkable, false);
}
void NewClassWidget::setFormInputCheckable(bool checkable, bool force)
{
if (!force && checkable == m_d->m_formInputCheckable)
return;
m_d->m_formInputCheckable = checkable;
m_d->m_ui.generateFormLabel->setVisible(checkable);
m_d->m_ui.generateFormCheckBox->setVisible(checkable);
}
void NewClassWidget::setFormInputChecked(bool v)
{
m_d->m_ui.generateFormCheckBox->setChecked(v);
}
bool NewClassWidget::formInputCheckable() const
{
return m_d->m_formInputCheckable;
}
bool NewClassWidget::formInputChecked() const
{
return m_d->m_ui.generateFormCheckBox->isChecked();
}
void NewClassWidget::slotFormInputChecked()
{
const bool checked = formInputChecked();
m_d->m_ui.formLabel->setEnabled(checked);
m_d->m_ui.formFileLineEdit->setEnabled(checked);
}
void NewClassWidget::setPathInputVisible(bool visible)
{
m_d->m_pathInputVisible = visible;
m_d->m_ui.pathLabel->setVisible(visible);
m_d->m_ui.pathChooser->setVisible(visible);
}
bool NewClassWidget::isPathInputVisible() const
{
return m_d->m_pathInputVisible;
}
void NewClassWidget::setClassName(const QString &suggestedName)
{
if (debugNewClassWidget)
qDebug() << Q_FUNC_INFO << suggestedName << m_d->m_headerExtension << m_d->m_sourceExtension;
m_d->m_ui.classLineEdit->setText(ClassNameValidatingLineEdit::createClassName(suggestedName));
}
QString NewClassWidget::className() const
{
return m_d->m_ui.classLineEdit->text();
}
QString NewClassWidget::baseClassName() const
{
return m_d->m_ui.baseClassComboBox->currentText();
}
void NewClassWidget::setBaseClassName(const QString &c)
{
const int index = m_d->m_ui.baseClassComboBox->findText(c);
if (index != -1) {
m_d->m_ui.baseClassComboBox->setCurrentIndex(index);
suggestClassNameFromBase();
}
}
QString NewClassWidget::sourceFileName() const
{
return m_d->m_ui.sourceFileLineEdit->text();
}
QString NewClassWidget::headerFileName() const
{
return m_d->m_ui.headerFileLineEdit->text();
}
QString NewClassWidget::formFileName() const
{
return m_d->m_ui.formFileLineEdit->text();
}
QString NewClassWidget::path() const
{
return m_d->m_ui.pathChooser->path();
}
void NewClassWidget::setPath(const QString &path)
{
m_d->m_ui.pathChooser->setPath(path);
}
bool NewClassWidget::namespacesEnabled() const
{
return m_d->m_ui.classLineEdit->namespacesEnabled();
}
void NewClassWidget::setNamespacesEnabled(bool b)
{
m_d->m_ui.classLineEdit->setNamespacesEnabled(b);
}
QString NewClassWidget::sourceExtension() const
{
return m_d->m_sourceExtension;
}
void NewClassWidget::setSourceExtension(const QString &e)
{
if (debugNewClassWidget)
qDebug() << Q_FUNC_INFO << e;
m_d->m_sourceExtension = fixSuffix(e);
}
QString NewClassWidget::headerExtension() const
{
return m_d->m_headerExtension;
}
void NewClassWidget::setHeaderExtension(const QString &e)
{
if (debugNewClassWidget)
qDebug() << Q_FUNC_INFO << e;
m_d->m_headerExtension = fixSuffix(e);
}
QString NewClassWidget::formExtension() const
{
return m_d->m_formExtension;
}
void NewClassWidget::setFormExtension(const QString &e)
{
if (debugNewClassWidget)
qDebug() << Q_FUNC_INFO << e;
m_d->m_formExtension = fixSuffix(e);
}
void NewClassWidget::slotValidChanged()
{
const bool newValid = isValid();
if (newValid != m_d->m_valid) {
m_d->m_valid = newValid;
emit validChanged();
}
}
bool NewClassWidget::isValid(QString *error) const
{
if (!m_d->m_ui.classLineEdit->isValid()) {
if (error)
*error = m_d->m_ui.classLineEdit->errorMessage();
return false;
}
if (isBaseClassInputVisible() && isBaseClassEditable()) {
// TODO: Should this be a ClassNameValidatingComboBox?
QRegExp classNameValidator(QLatin1String("[a-zA-Z_][a-zA-Z0-9_]*(::[a-zA-Z_][a-zA-Z0-9_]*)*"));
const QString baseClass = m_d->m_ui.baseClassComboBox->currentText().trimmed();
if (!baseClass.isEmpty() && !classNameValidator.exactMatch(baseClass)) {
if (error)
*error = tr("Invalid base class name");
return false;
}
}
if (!m_d->m_ui.headerFileLineEdit->isValid()) {
if (error)
*error = tr("Invalid header file name: %1").arg(m_d->m_ui.headerFileLineEdit->errorMessage());
return false;
}
if (!m_d->m_ui.sourceFileLineEdit->isValid()) {
if (error)
*error = tr("Invalid source file name: %1").arg(m_d->m_ui.sourceFileLineEdit->errorMessage());
return false;
}
if (isFormInputVisible()) {
if (!m_d->m_ui.formFileLineEdit->isValid()) {
if (error)
*error = tr("Invalid form file name: %1").arg(m_d->m_ui.formFileLineEdit->errorMessage());
return false;
}
}
if (isPathInputVisible()) {
if (!m_d->m_ui.pathChooser->isValid()) {
if (error)
*error = m_d->m_ui.pathChooser->errorMessage();
return false;
}
}
return true;
}
void NewClassWidget::updateFileNames(const QString &baseName)
{
if (debugNewClassWidget)
qDebug() << Q_FUNC_INFO << baseName << m_d->m_headerExtension << m_d->m_sourceExtension;
const QChar dot = QLatin1Char('.');
m_d->m_ui.sourceFileLineEdit->setText(baseName + dot + m_d->m_sourceExtension);
m_d->m_ui.headerFileLineEdit->setText(baseName + dot + m_d->m_headerExtension);
m_d->m_ui.formFileLineEdit->setText(baseName + dot + m_d->m_formExtension);
}
void NewClassWidget::slotActivated()
{
if (m_d->m_valid)
emit activated();
}
QString NewClassWidget::fixSuffix(const QString &suffix)
{
QString s = suffix;
if (s.startsWith(QLatin1Char('.')))
s.remove(0, 1);
return s;
}
// Utility to add a suffix to a file unless the user specified one
static QString ensureSuffix(QString f, const QString &extension)
{
const QChar dot = QLatin1Char('.');
if (f.contains(dot))
return f;
f += dot;
f += extension;
return f;
}
// If a non-empty name was passed, expand to directory and suffix
static QString expandFileName(const QDir &dir, const QString name, const QString &extension)
{
if (name.isEmpty())
return QString();
return dir.absoluteFilePath(ensureSuffix(name, extension));
}
QStringList NewClassWidget::files() const
{
QStringList rc;
const QDir dir = QDir(path());
rc.push_back(expandFileName(dir, headerFileName(), headerExtension()));
rc.push_back(expandFileName(dir, sourceFileName(), sourceExtension()));
if (isFormInputVisible())
rc.push_back(expandFileName(dir, formFileName(), formExtension()));
return rc;
}
} // namespace Utils
} // namespace Core
|