2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2011-01-10 16:29:06 +01:00
|
|
|
**
|
2013-01-28 17:12:19 +01:00
|
|
|
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
|
2012-10-02 09:12:39 +02:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
2011-01-10 16:29:06 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2011-01-10 16:29:06 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
|
|
|
|
** use the contact form at http://qt.digia.com/contact-us.
|
2011-01-10 16:29:06 +01:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
2012-10-02 09:12:39 +02:00
|
|
|
** 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, Digia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
2011-01-10 16:29:06 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2011-01-10 16:29:06 +01:00
|
|
|
|
|
|
|
|
#include "qtdesignerformclasscodegenerator.h"
|
|
|
|
|
#include "formclasswizardparameters.h"
|
|
|
|
|
#include "formtemplatewizardpage.h"
|
|
|
|
|
|
|
|
|
|
#include <utils/codegeneration.h>
|
|
|
|
|
#include <coreplugin/icore.h>
|
|
|
|
|
#include <cpptools/abstracteditorsupport.h>
|
|
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QTextStream>
|
|
|
|
|
#include <QSettings>
|
|
|
|
|
#include <QFileInfo>
|
|
|
|
|
#include <QDebug>
|
2011-01-10 16:29:06 +01:00
|
|
|
|
|
|
|
|
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";
|
|
|
|
|
|
2012-02-16 09:36:33 +01:00
|
|
|
// TODO: These 3 are general coding convention settings and
|
2011-01-10 16:29:06 +01:00
|
|
|
// should go to CppTools...
|
|
|
|
|
static const char includeQtModuleKeyC[] = "IncludeQtModule";
|
2012-02-16 09:36:33 +01:00
|
|
|
static const char addQtVersionCheckKeyC[] = "AddQtVersionCheck";
|
2011-01-10 16:29:06 +01:00
|
|
|
static const char indentNamespaceKeyC[] = "IndentNamespace";
|
|
|
|
|
|
|
|
|
|
static const bool retranslationSupportDefault = false;
|
|
|
|
|
|
|
|
|
|
namespace Designer {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
|
|
|
|
FormClassWizardGenerationParameters::FormClassWizardGenerationParameters() :
|
|
|
|
|
embedding(PointerAggregatedUiClass),
|
|
|
|
|
retranslationSupport(retranslationSupportDefault),
|
|
|
|
|
includeQtModule(false),
|
2012-02-16 09:36:33 +01:00
|
|
|
addQtVersionCheck(false),
|
2011-01-10 16:29:06 +01:00
|
|
|
indentNamespace(false)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FormClassWizardGenerationParameters::fromSettings(const QSettings *settings)
|
|
|
|
|
{
|
|
|
|
|
QString group = QLatin1String(formClassWizardPageGroupC) + QLatin1Char('/');
|
|
|
|
|
|
|
|
|
|
retranslationSupport = settings->value(group + QLatin1String(translationKeyC), retranslationSupportDefault).toBool();
|
|
|
|
|
embedding = static_cast<UiClassEmbedding>(settings->value(group + QLatin1String(embeddingModeKeyC), int(PointerAggregatedUiClass)).toInt());
|
|
|
|
|
includeQtModule = settings->value(group + QLatin1String(includeQtModuleKeyC), false).toBool();
|
2012-02-16 09:36:33 +01:00
|
|
|
addQtVersionCheck = settings->value(group + QLatin1String(addQtVersionCheckKeyC), false).toBool();
|
2011-01-10 16:29:06 +01:00
|
|
|
indentNamespace = settings->value(group + QLatin1String(indentNamespaceKeyC), false).toBool();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FormClassWizardGenerationParameters::toSettings(QSettings *settings) const
|
|
|
|
|
{
|
|
|
|
|
settings->beginGroup(QLatin1String(formClassWizardPageGroupC));
|
|
|
|
|
settings->setValue(QLatin1String(translationKeyC), retranslationSupport);
|
|
|
|
|
settings->setValue(QLatin1String(embeddingModeKeyC), embedding);
|
|
|
|
|
settings->setValue(QLatin1String(includeQtModuleKeyC), includeQtModule);
|
2012-02-16 09:36:33 +01:00
|
|
|
settings->setValue(QLatin1String(addQtVersionCheckKeyC), addQtVersionCheck);
|
2011-01-10 16:29:06 +01:00
|
|
|
settings->setValue(QLatin1String(indentNamespaceKeyC), indentNamespace);
|
|
|
|
|
settings->endGroup();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FormClassWizardGenerationParameters::equals(const FormClassWizardGenerationParameters &rhs) const
|
|
|
|
|
{
|
2012-02-16 09:36:33 +01:00
|
|
|
return embedding == rhs.embedding
|
|
|
|
|
&& retranslationSupport == rhs.retranslationSupport
|
|
|
|
|
&& includeQtModule == rhs.includeQtModule
|
|
|
|
|
&& addQtVersionCheck == rhs.addQtVersionCheck
|
|
|
|
|
&& indentNamespace == rhs.indentNamespace;
|
2011-01-10 16:29:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Generation code
|
|
|
|
|
|
|
|
|
|
// 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 PointerAggregatedUiClass:
|
|
|
|
|
str << uiMemberC << "->";
|
|
|
|
|
break;
|
|
|
|
|
case AggregatedUiClass:
|
|
|
|
|
str << uiMemberC << '.';
|
|
|
|
|
break;
|
|
|
|
|
case InheritedUiClass:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
|
|
|
|
|
bool QtDesignerFormClassCodeGenerator::generateCpp(const FormClassWizardParameters ¶meters,
|
|
|
|
|
QString *header, QString *source, int indentation)
|
|
|
|
|
{
|
|
|
|
|
Internal::FormClassWizardGenerationParameters generationParameters;
|
2012-01-24 15:36:40 +01:00
|
|
|
generationParameters.fromSettings(Core::ICore::settings());
|
2011-01-10 16:29:06 +01:00
|
|
|
|
|
|
|
|
const QString indent = QString(indentation, QLatin1Char(' '));
|
|
|
|
|
QString formBaseClass;
|
|
|
|
|
QString uiClassName;
|
|
|
|
|
|
|
|
|
|
if (!Internal::FormTemplateWizardPage::getUIXmlData(parameters.uiTemplate, &formBaseClass, &uiClassName)) {
|
|
|
|
|
qWarning("Unable to determine the form base class from %s.", qPrintable(parameters.uiTemplate));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Build the ui class (Ui::Foo) name relative to the namespace (which is the same):
|
|
|
|
|
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 = parameters.className.split(colonColon);
|
|
|
|
|
if (namespaceList.empty()) // Paranoia!
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
const QString unqualifiedClassName = namespaceList.takeLast();
|
|
|
|
|
|
|
|
|
|
const QString headerLicense =
|
|
|
|
|
CppTools::AbstractEditorSupport::licenseTemplate(parameters.headerFile, parameters.className);
|
|
|
|
|
const QString sourceLicense =
|
|
|
|
|
CppTools::AbstractEditorSupport::licenseTemplate(parameters.sourceFile, parameters.className);
|
|
|
|
|
// Include guards
|
2011-01-18 12:18:08 +01:00
|
|
|
const QString guard = Utils::headerGuard(parameters.headerFile, namespaceList);
|
2011-01-10 16:29:06 +01:00
|
|
|
|
|
|
|
|
QString uiInclude = QLatin1String("ui_");
|
|
|
|
|
uiInclude += QFileInfo(parameters.uiFile).completeBaseName();
|
|
|
|
|
uiInclude += QLatin1String(".h");
|
|
|
|
|
|
|
|
|
|
// 1) Header file
|
|
|
|
|
QTextStream headerStr(header);
|
|
|
|
|
headerStr << headerLicense << "#ifndef " << guard
|
|
|
|
|
<< "\n#define " << guard << '\n' << '\n';
|
|
|
|
|
|
|
|
|
|
// Include 'ui_'
|
|
|
|
|
if (generationParameters.embedding != Internal::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'))) {
|
2012-02-16 09:36:33 +01:00
|
|
|
if (generationParameters.includeQtModule) {
|
|
|
|
|
if (generationParameters.addQtVersionCheck) {
|
|
|
|
|
Utils::writeBeginQtVersionCheck(headerStr);
|
|
|
|
|
Utils::writeIncludeFileDirective(QLatin1String("QtWidgets/") + formBaseClass, true, headerStr);
|
|
|
|
|
headerStr << "#else\n";
|
|
|
|
|
Utils::writeIncludeFileDirective(QLatin1String("QtGui/") + formBaseClass, true, headerStr);
|
|
|
|
|
headerStr << "#endif\n";
|
|
|
|
|
} else {
|
|
|
|
|
Utils::writeIncludeFileDirective(QLatin1String("QtGui/") + formBaseClass, true, headerStr);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Utils::writeIncludeFileDirective(formBaseClass, true, headerStr);
|
|
|
|
|
}
|
2011-01-10 16:29:06 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const QString namespaceIndent = Utils::writeOpeningNameSpaces(namespaceList,
|
|
|
|
|
generationParameters.indentNamespace ? indent : QString(),
|
|
|
|
|
headerStr);
|
|
|
|
|
|
|
|
|
|
// Forward-declare the UI class
|
|
|
|
|
if (generationParameters.embedding == Internal::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;
|
Remove braces for single lines of conditions
#!/usr/bin/env ruby
Dir.glob('**/*.cpp') { |file|
# skip ast (excluding paste, astpath, and canv'ast'imer)
next if file =~ /ast[^eip]|keywords\.|qualifiers|preprocessor|names.cpp/i
s = File.read(file)
next if s.include?('qlalr')
orig = s.dup
s.gsub!(/\n *if [^\n]*{\n[^\n]*\n\s+}(\s+else if [^\n]* {\n[^\n]*\n\s+})*(\s+else {\n[^\n]*\n\s+})?\n/m) { |m|
res = $&
if res =~ /^\s*(\/\/|[A-Z_]{3,})/ # C++ comment or macro (Q_UNUSED, SDEBUG), do not touch braces
res
else
res.gsub!('} else', 'else')
res.gsub!(/\n +} *\n/m, "\n")
res.gsub(/ *{$/, '')
end
}
s.gsub!(/ *$/, '')
File.open(file, 'wb').write(s) if s != orig
}
Change-Id: I3b30ee60df0986f66c02132c65fc38a3fbb6bbdc
Reviewed-by: hjk <qthjk@ovi.com>
2013-01-08 03:32:53 +02:00
|
|
|
if (generationParameters.embedding == Internal::InheritedUiClass)
|
2011-01-10 16:29:06 +01:00
|
|
|
headerStr << ", private " << uiClassName;
|
|
|
|
|
headerStr << "\n{\n" << namespaceIndent << indent << "Q_OBJECT\n\n"
|
|
|
|
|
<< namespaceIndent << "public:\n"
|
|
|
|
|
<< namespaceIndent << indent << "explicit " << unqualifiedClassName << "(QWidget *parent = 0);\n";
|
|
|
|
|
if (generationParameters.embedding == Internal::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 (generationParameters.embedding != Internal::InheritedUiClass) {
|
|
|
|
|
headerStr << '\n' << namespaceIndent << "private:\n"
|
|
|
|
|
<< namespaceIndent << indent << uiClassName << ' ';
|
|
|
|
|
if (generationParameters.embedding == Internal::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(parameters.headerFile, false, sourceStr);
|
|
|
|
|
if (generationParameters.embedding == Internal::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 (generationParameters.embedding == Internal::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 (generationParameters.embedding == Internal::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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QtDesignerFormClassCodeGenerator::QtDesignerFormClassCodeGenerator(QObject *parent) :
|
|
|
|
|
QObject(parent)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QVariant QtDesignerFormClassCodeGenerator::generateFormClassCode(const FormClassWizardParameters ¶meters)
|
|
|
|
|
{
|
|
|
|
|
QString header;
|
|
|
|
|
QString source;
|
|
|
|
|
QtDesignerFormClassCodeGenerator::generateCpp(parameters, &header, &source);
|
|
|
|
|
QVariantList result;
|
|
|
|
|
result << header << source;
|
|
|
|
|
return QVariant(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Designer
|