summaryrefslogtreecommitdiff
path: root/src/plugins/designer/cpp/formclasswizardparameters.cpp
blob: 4bc058cde9587411b2c171005c3e027a1b6c3b07 (plain)
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
464
465
466
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** No Commercial Usage
**
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/

#include "formclasswizardparameters.h"
#include "formtemplatewizardpage.h"

#include <utils/codegeneration.h>
#include <cpptools/cppmodelmanagerinterface.h>

#include <QtCore/QTextStream>
#include <QtCore/QSettings>
#include <QtCore/QFileInfo>
#include <QtCore/QDebug>
#include <QtCore/QSharedData>

static const char *uiMemberC = "ui";
static const char *uiNamespaceC = "Ui";

static const char *formClassWizardPageGroupC = "FormClassWizardPage";
static const char *translationKeyC = "RetranslationSupport";
static const char *embeddingModeKeyC = "Embedding";

// TODO: These 2 are general coding convention settings and
// should go to CppTools...
static const char *includeQtModuleKeyC = "IncludeQtModule";
static const char *indentNamespaceKeyC = "IndentNamespace";

static const bool retranslationSupportDefault = false;

namespace Designer {

class FormClassWizardGenerationParametersPrivate : public QSharedData
{
public:
    FormClassWizardGenerationParametersPrivate();
    void fromSettings(const QSettings *);
    void toSettings(QSettings *) const;
    bool equals(const FormClassWizardGenerationParametersPrivate &rhs) const;

    FormClassWizardGenerationParameters::UiClassEmbedding embedding;
    bool retranslationSupport; // Add handling for language change events
    bool includeQtModule;      // Include "<QtGui/[Class]>" or just "<[Class]>"
    bool indentNamespace;
};

FormClassWizardGenerationParametersPrivate::FormClassWizardGenerationParametersPrivate() :
    embedding(FormClassWizardGenerationParameters::PointerAggregatedUiClass),
    retranslationSupport(retranslationSupportDefault),
    includeQtModule(false),
    indentNamespace(false)
{
}

void FormClassWizardGenerationParametersPrivate::fromSettings(const QSettings *settings)
{
    QString key = QLatin1String(formClassWizardPageGroupC);
    key += QLatin1Char('/');
    const int groupLength = key.size();

    key += QLatin1String(translationKeyC);
    retranslationSupport = settings->value(key, retranslationSupportDefault).toBool();

    key.truncate(groupLength);
    key += QLatin1String(embeddingModeKeyC);
    embedding =  static_cast<FormClassWizardGenerationParameters::UiClassEmbedding>(settings->value(key, int(FormClassWizardGenerationParameters::PointerAggregatedUiClass)).toInt());

    key.truncate(groupLength);
    key += QLatin1String(includeQtModuleKeyC);
    includeQtModule = settings->value(key, false).toBool();

    key.truncate(groupLength);
    key += QLatin1String(indentNamespaceKeyC);
    indentNamespace = settings->value(key, false).toBool();
}

void FormClassWizardGenerationParametersPrivate::toSettings(QSettings *settings) const
{
    settings->beginGroup(QLatin1String(formClassWizardPageGroupC));
    settings->setValue(QLatin1String(translationKeyC), retranslationSupport);
    settings->setValue(QLatin1String(embeddingModeKeyC), embedding);
    settings->setValue(QLatin1String(includeQtModuleKeyC), includeQtModule);
    settings->setValue(QLatin1String(indentNamespaceKeyC), indentNamespace);
    settings->endGroup();
}

bool FormClassWizardGenerationParametersPrivate::equals(const FormClassWizardGenerationParametersPrivate &rhs) const
{
    return embedding == rhs.embedding && retranslationSupport == rhs.retranslationSupport
         && includeQtModule == rhs.includeQtModule && indentNamespace == rhs.indentNamespace;
}

FormClassWizardGenerationParameters::FormClassWizardGenerationParameters() :
    m_d(new FormClassWizardGenerationParametersPrivate)
{
}

FormClassWizardGenerationParameters::~FormClassWizardGenerationParameters()
{
}

FormClassWizardGenerationParameters::FormClassWizardGenerationParameters(const FormClassWizardGenerationParameters &rhs) :
    m_d(rhs.m_d)
{
}

FormClassWizardGenerationParameters &FormClassWizardGenerationParameters::operator=(const FormClassWizardGenerationParameters &rhs)
{
    if (this != &rhs)
        m_d.operator=(rhs.m_d);
    return *this;
}

bool FormClassWizardGenerationParameters::equals(const FormClassWizardGenerationParameters &rhs) const
{
    return m_d->equals(*rhs.m_d.constData());
}

FormClassWizardGenerationParameters::UiClassEmbedding FormClassWizardGenerationParameters::embedding() const
{
    return m_d->embedding;
}

void FormClassWizardGenerationParameters::setEmbedding(UiClassEmbedding e)
{
    m_d->embedding = e;
}

bool FormClassWizardGenerationParameters::retranslationSupport() const
{
    return m_d->retranslationSupport;
}

void FormClassWizardGenerationParameters::setRetranslationSupport(bool v)
{
     m_d->retranslationSupport = v;
}

bool FormClassWizardGenerationParameters::includeQtModule() const
{
    return m_d->includeQtModule;
}

void FormClassWizardGenerationParameters::setIncludeQtModule(bool v)
{
     m_d->includeQtModule = v;
}

bool FormClassWizardGenerationParameters::indentNamespace() const
{
    return m_d->indentNamespace;
}

void FormClassWizardGenerationParameters::setIndentNamespace(bool v)
{
     m_d->indentNamespace = v;
}

void FormClassWizardGenerationParameters::fromSettings(const QSettings *settings)
{
    m_d->fromSettings(settings);
}

void FormClassWizardGenerationParameters::toSettings(QSettings *settings) const
{
    m_d->toSettings(settings);
}

// -----------

class FormClassWizardParametersPrivate : public QSharedData {
public:
    bool generateCpp(const FormClassWizardGenerationParameters &fgp,
                     QString *header, QString *source, int indentation) const;

    QString uiTemplate;
    QString className;

    QString path;
    QString sourceFile;
    QString headerFile;
    QString uiFile;
};

FormClassWizardParameters::FormClassWizardParameters() :
    m_d(new FormClassWizardParametersPrivate)
{
}

FormClassWizardParameters::~FormClassWizardParameters()
{
}

FormClassWizardParameters::FormClassWizardParameters(const FormClassWizardParameters &rhs) :
        m_d(rhs.m_d)
{
}

FormClassWizardParameters &FormClassWizardParameters::operator=(const FormClassWizardParameters &rhs)
{
    if (this != &rhs)
        m_d.operator =(rhs.m_d);
    return *this;
}

QString FormClassWizardParameters::uiTemplate() const
{
    return m_d->uiTemplate;
}

void FormClassWizardParameters::setUiTemplate(const QString &s)
{
     m_d->uiTemplate = s;
}

QString FormClassWizardParameters::className() const
{
    return m_d->className;
}

void FormClassWizardParameters::setClassName(const QString &s)
{
     m_d->className = s;
}

QString FormClassWizardParameters::path() const
{
    return m_d->path;
}

void FormClassWizardParameters::setPath(const QString &s)
{
     m_d->path = s;
}


QString FormClassWizardParameters::sourceFile() const
{
    return m_d->sourceFile;
}

void FormClassWizardParameters::setSourceFile(const QString &s)
{
     m_d->sourceFile = s;
}

QString FormClassWizardParameters::headerFile() const
{
    return m_d->headerFile;
}

void FormClassWizardParameters::setHeaderFile(const QString &s)
{
     m_d->headerFile = s;
}


QString FormClassWizardParameters::uiFile() const
{
    return m_d->uiFile;
}

void FormClassWizardParameters::setUiFile(const QString &s)
{
     m_d->uiFile = s;
}

bool FormClassWizardParameters::getUIXmlData(const QString &uiXml, QString *formBaseClass, QString *uiClassName)
{
    return Designer::Internal::FormTemplateWizardPage::getUIXmlData(uiXml, formBaseClass, uiClassName);
}

QString FormClassWizardParameters::changeUiClassName(const QString &uiXml, const QString &newUiClassName)
{
    return Designer::Internal::FormTemplateWizardPage::changeUiClassName(uiXml, newUiClassName);
}

// Write out how to access the Ui class in the source code.
static inline void writeUiMemberAccess(const FormClassWizardGenerationParameters &fp, QTextStream &str)
{
    switch(fp.embedding()) {
    case FormClassWizardGenerationParameters::PointerAggregatedUiClass:
        str << uiMemberC << "->";
        break;
    case FormClassWizardGenerationParameters::AggregatedUiClass:
        str << uiMemberC << '.';
        break;
    case FormClassWizardGenerationParameters::InheritedUiClass:
        break;
    }
}

bool FormClassWizardParametersPrivate::generateCpp(const FormClassWizardGenerationParameters &generationParameters,
                                                   QString *header, QString *source, int indentation) const
{
    const QString indent = QString(indentation, QLatin1Char(' '));
    QString formBaseClass;
    QString uiClassName;
    if (!FormClassWizardParameters::getUIXmlData(uiTemplate, &formBaseClass, &uiClassName)) {
        qWarning("Unable to determine the form base class from %s.", uiTemplate.toUtf8().constData());
        return false;
    }

    // Build the ui class (Ui::Foo) name relative to the namespace (which is the same):
    const FormClassWizardGenerationParameters::UiClassEmbedding embedding = generationParameters.embedding();
    const QString colonColon = QLatin1String("::");
    const int lastSeparator = uiClassName.lastIndexOf(colonColon);
    if (lastSeparator != -1)
        uiClassName.remove(0, lastSeparator + colonColon.size());
    uiClassName.insert(0, QLatin1String(uiNamespaceC) + colonColon);

    // Do we have namespaces?
    QStringList namespaceList = className.split(colonColon);
    if (namespaceList.empty()) // Paranoia!
        return false;

    const QString unqualifiedClassName = namespaceList.takeLast();

    const QString headerLicense =
            CppTools::AbstractEditorSupport::licenseTemplate(headerFile, className);
    const QString sourceLicense =
            CppTools::AbstractEditorSupport::licenseTemplate(sourceFile, className);
    // Include guards
    const QString guard = Utils::headerGuard(headerFile);

    QString uiInclude = QLatin1String("ui_");
    uiInclude += QFileInfo(uiFile).completeBaseName();
    uiInclude += QLatin1String(".h");

    // 1) Header file
    QTextStream headerStr(header);
    headerStr << headerLicense << "#ifndef " << guard
              << "\n#define " <<  guard << '\n' << '\n';

    // Include 'ui_'
    if (embedding != FormClassWizardGenerationParameters::PointerAggregatedUiClass) {
        Utils::writeIncludeFileDirective(uiInclude, false, headerStr);
    } else {
        // Todo: Can we obtain the header from the code model for custom widgets?
        // Alternatively, from Designer.
        if (formBaseClass.startsWith(QLatin1Char('Q'))) {
            QString baseInclude = formBaseClass;
            if (generationParameters.includeQtModule())
                baseInclude.insert(0, QLatin1String("QtGui/"));
            Utils::writeIncludeFileDirective(baseInclude, true, headerStr);
        }
    }

    const QString namespaceIndent = Utils::writeOpeningNameSpaces(namespaceList,
                                                                        generationParameters.indentNamespace() ? indent : QString(),
                                                                        headerStr);

    // Forward-declare the UI class
    if (embedding == FormClassWizardGenerationParameters::PointerAggregatedUiClass) {
          headerStr << '\n'
                  << namespaceIndent << "namespace " <<  uiNamespaceC << " {\n"
                  << namespaceIndent << indent << "class " << Internal::FormTemplateWizardPage::stripNamespaces(uiClassName) << ";\n"
                  << namespaceIndent << "}\n";
    }

    // Class declaration
    headerStr << '\n' << namespaceIndent << "class " << unqualifiedClassName
              << " : public " << formBaseClass;
    if (embedding == FormClassWizardGenerationParameters::InheritedUiClass) {
        headerStr << ", private " << uiClassName;
    }
    headerStr << "\n{\n" << namespaceIndent << indent << "Q_OBJECT\n\n"
              << namespaceIndent << "public:\n"
              << namespaceIndent << indent << "explicit " << unqualifiedClassName << "(QWidget *parent = 0);\n";
    if (embedding == FormClassWizardGenerationParameters::PointerAggregatedUiClass)
        headerStr << namespaceIndent << indent << "~" << unqualifiedClassName << "();\n";
    // retranslation
    if (generationParameters.retranslationSupport())
        headerStr << '\n' << namespaceIndent << "protected:\n"
                  << namespaceIndent << indent << "void changeEvent(QEvent *e);\n";
    // Member variable
    if (embedding != FormClassWizardGenerationParameters::InheritedUiClass) {
        headerStr << '\n' << namespaceIndent << "private:\n"
                  << namespaceIndent << indent << uiClassName << ' ';
        if (embedding == FormClassWizardGenerationParameters::PointerAggregatedUiClass)
            headerStr << '*';
        headerStr << uiMemberC << ";\n";
    }
    headerStr << namespaceIndent << "};\n\n";
    Utils::writeClosingNameSpaces(namespaceList, generationParameters.indentNamespace() ? indent : QString(), headerStr);
    headerStr << "#endif // "<<  guard << '\n';

    // 2) Source file
    QTextStream sourceStr(source);
    sourceStr << sourceLicense;
    Utils::writeIncludeFileDirective(headerFile, false, sourceStr);
    if (embedding == FormClassWizardGenerationParameters::PointerAggregatedUiClass)
        Utils::writeIncludeFileDirective(uiInclude, false, sourceStr);
    // NameSpaces(
    Utils::writeOpeningNameSpaces(namespaceList, generationParameters.indentNamespace() ? indent : QString(), sourceStr);
    // Constructor with setupUi
    sourceStr << '\n' << namespaceIndent << unqualifiedClassName << "::" << unqualifiedClassName << "(QWidget *parent) :\n"
               << namespaceIndent << indent << formBaseClass << "(parent)";
    if (embedding == FormClassWizardGenerationParameters::PointerAggregatedUiClass)
        sourceStr << ",\n"  << namespaceIndent << indent <<  uiMemberC << "(new " << uiClassName << ")";
    sourceStr <<  '\n' << namespaceIndent << "{\n" <<  namespaceIndent << indent;
    writeUiMemberAccess(generationParameters, sourceStr);
    sourceStr <<  "setupUi(this);\n" << namespaceIndent << "}\n";
    // Deleting destructor for ptr
    if (embedding == FormClassWizardGenerationParameters::PointerAggregatedUiClass) {
        sourceStr << '\n' <<  namespaceIndent << unqualifiedClassName << "::~" << unqualifiedClassName
                  << "()\n" << namespaceIndent << "{\n"
                  << namespaceIndent << indent << "delete " << uiMemberC << ";\n"
                  << namespaceIndent << "}\n";
    }
    // retranslation
    if (generationParameters.retranslationSupport()) {
        sourceStr  << '\n' << namespaceIndent << "void " << unqualifiedClassName << "::" << "changeEvent(QEvent *e)\n"
        << namespaceIndent << "{\n"
        << namespaceIndent << indent << formBaseClass << "::changeEvent(e);\n"
        << namespaceIndent << indent << "switch (e->type()) {\n" << namespaceIndent << indent << "case QEvent::LanguageChange:\n"
        << namespaceIndent << indent << indent;
        writeUiMemberAccess(generationParameters, sourceStr);
        sourceStr << "retranslateUi(this);\n"
                  << namespaceIndent << indent <<  indent << "break;\n"
                  << namespaceIndent << indent << "default:\n"
                  << namespaceIndent << indent << indent << "break;\n"
                  << namespaceIndent << indent << "}\n"
                  << namespaceIndent << "}\n";
    }
    Utils::writeClosingNameSpaces(namespaceList, generationParameters.indentNamespace() ? indent : QString(), sourceStr);
    return true;
}

bool FormClassWizardParameters::generateCpp(const FormClassWizardGenerationParameters &fgp,
                                            QString *header, QString *source, int indentation) const
{
    return m_d->generateCpp(fgp, header, source, indentation);
}


} // namespace Designer