2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2011-01-10 16:29:06 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
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
|
2016-01-15 14:57:40 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2011-01-10 16:29:06 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2011-01-10 16:29:06 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2011-01-10 16:29:06 +01:00
|
|
|
|
|
|
|
|
#include "qtdesignerformclasscodegenerator.h"
|
|
|
|
|
#include "formtemplatewizardpage.h"
|
2013-08-29 16:36:42 +02:00
|
|
|
#include <designer/cpp/formclasswizardparameters.h>
|
2011-01-10 16:29:06 +01:00
|
|
|
|
|
|
|
|
#include <utils/codegeneration.h>
|
|
|
|
|
#include <coreplugin/icore.h>
|
|
|
|
|
#include <cpptools/abstracteditorsupport.h>
|
2014-07-21 15:38:32 +02:00
|
|
|
#include <qtsupport/codegenerator.h>
|
2014-07-18 15:47:20 +02:00
|
|
|
#include <qtsupport/codegensettings.h>
|
2011-01-10 16:29:06 +01:00
|
|
|
|
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";
|
|
|
|
|
|
|
|
|
|
namespace Designer {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
|
|
|
|
// Generation code
|
|
|
|
|
|
|
|
|
|
// Write out how to access the Ui class in the source code.
|
2014-07-18 15:47:20 +02:00
|
|
|
static inline void writeUiMemberAccess(const QtSupport::CodeGenSettings &fp, QTextStream &str)
|
2011-01-10 16:29:06 +01:00
|
|
|
{
|
|
|
|
|
switch (fp.embedding) {
|
2014-07-18 15:47:20 +02:00
|
|
|
case QtSupport::CodeGenSettings::PointerAggregatedUiClass:
|
2011-01-10 16:29:06 +01:00
|
|
|
str << uiMemberC << "->";
|
|
|
|
|
break;
|
2014-07-18 15:47:20 +02:00
|
|
|
case QtSupport::CodeGenSettings::AggregatedUiClass:
|
2011-01-10 16:29:06 +01:00
|
|
|
str << uiMemberC << '.';
|
|
|
|
|
break;
|
2014-07-18 15:47:20 +02:00
|
|
|
case QtSupport::CodeGenSettings::InheritedUiClass:
|
2011-01-10 16:29:06 +01:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
|
|
|
|
|
bool QtDesignerFormClassCodeGenerator::generateCpp(const FormClassWizardParameters ¶meters,
|
|
|
|
|
QString *header, QString *source, int indentation)
|
|
|
|
|
{
|
2014-07-18 15:47:20 +02:00
|
|
|
QtSupport::CodeGenSettings generationParameters;
|
2012-01-24 15:36:40 +01:00
|
|
|
generationParameters.fromSettings(Core::ICore::settings());
|
2011-01-10 16:29:06 +01:00
|
|
|
|
2017-10-16 20:47:05 +02:00
|
|
|
const QString indent = QString(indentation, ' ');
|
2011-01-10 16:29:06 +01:00
|
|
|
QString formBaseClass;
|
|
|
|
|
QString uiClassName;
|
|
|
|
|
|
2014-07-21 15:38:32 +02:00
|
|
|
if (!QtSupport::CodeGenerator::uiData(parameters.uiTemplate, &formBaseClass, &uiClassName)) {
|
2011-01-10 16:29:06 +01:00
|
|
|
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):
|
2017-10-16 20:47:05 +02:00
|
|
|
const QString colonColon = "::";
|
2011-01-10 16:29:06 +01:00
|
|
|
const int lastSeparator = uiClassName.lastIndexOf(colonColon);
|
|
|
|
|
if (lastSeparator != -1)
|
|
|
|
|
uiClassName.remove(0, lastSeparator + colonColon.size());
|
2017-10-16 20:47:05 +02:00
|
|
|
uiClassName.insert(0, uiNamespaceC + colonColon);
|
2011-01-10 16:29:06 +01:00
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2017-10-16 20:47:05 +02:00
|
|
|
const QString uiInclude = "ui_" + QFileInfo(parameters.uiFile).completeBaseName() + ".h";
|
2011-01-10 16:29:06 +01:00
|
|
|
|
|
|
|
|
// 1) Header file
|
|
|
|
|
QTextStream headerStr(header);
|
|
|
|
|
headerStr << headerLicense << "#ifndef " << guard
|
|
|
|
|
<< "\n#define " << guard << '\n' << '\n';
|
|
|
|
|
|
|
|
|
|
// Include 'ui_'
|
2014-07-18 15:47:20 +02:00
|
|
|
if (generationParameters.embedding != QtSupport::CodeGenSettings::PointerAggregatedUiClass) {
|
2011-01-10 16:29:06 +01:00
|
|
|
Utils::writeIncludeFileDirective(uiInclude, false, headerStr);
|
|
|
|
|
} else {
|
|
|
|
|
// Todo: Can we obtain the header from the code model for custom widgets?
|
|
|
|
|
// Alternatively, from Designer.
|
2017-10-16 20:47:05 +02:00
|
|
|
if (formBaseClass.startsWith('Q')) {
|
2012-02-16 09:36:33 +01:00
|
|
|
if (generationParameters.includeQtModule) {
|
|
|
|
|
if (generationParameters.addQtVersionCheck) {
|
|
|
|
|
Utils::writeBeginQtVersionCheck(headerStr);
|
2017-10-16 20:47:05 +02:00
|
|
|
Utils::writeIncludeFileDirective("QtWidgets/" + formBaseClass, true, headerStr);
|
2012-02-16 09:36:33 +01:00
|
|
|
headerStr << "#else\n";
|
2017-10-16 20:47:05 +02:00
|
|
|
Utils::writeIncludeFileDirective("QtGui/" + formBaseClass, true, headerStr);
|
2012-02-16 09:36:33 +01:00
|
|
|
headerStr << "#endif\n";
|
|
|
|
|
} else {
|
2017-10-16 20:47:05 +02:00
|
|
|
Utils::writeIncludeFileDirective("QtGui/" + formBaseClass, true, headerStr);
|
2012-02-16 09:36:33 +01:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Utils::writeIncludeFileDirective(formBaseClass, true, headerStr);
|
|
|
|
|
}
|
2011-01-10 16:29:06 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-24 10:59:11 +02:00
|
|
|
const QString namespaceIndent = Utils::writeOpeningNameSpaces(namespaceList, QString(),
|
2011-01-10 16:29:06 +01:00
|
|
|
headerStr);
|
|
|
|
|
|
|
|
|
|
// Forward-declare the UI class
|
2014-07-18 15:47:20 +02:00
|
|
|
if (generationParameters.embedding == QtSupport::CodeGenSettings::PointerAggregatedUiClass) {
|
2011-01-10 16:29:06 +01:00
|
|
|
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;
|
2014-07-18 15:47:20 +02:00
|
|
|
if (generationParameters.embedding == QtSupport::CodeGenSettings::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";
|
2014-07-18 15:47:20 +02:00
|
|
|
if (generationParameters.embedding == QtSupport::CodeGenSettings::PointerAggregatedUiClass)
|
2011-01-10 16:29:06 +01:00
|
|
|
headerStr << namespaceIndent << indent << "~" << unqualifiedClassName << "();\n";
|
|
|
|
|
// retranslation
|
|
|
|
|
if (generationParameters.retranslationSupport)
|
|
|
|
|
headerStr << '\n' << namespaceIndent << "protected:\n"
|
|
|
|
|
<< namespaceIndent << indent << "void changeEvent(QEvent *e);\n";
|
|
|
|
|
// Member variable
|
2014-07-18 15:47:20 +02:00
|
|
|
if (generationParameters.embedding != QtSupport::CodeGenSettings::InheritedUiClass) {
|
2011-01-10 16:29:06 +01:00
|
|
|
headerStr << '\n' << namespaceIndent << "private:\n"
|
|
|
|
|
<< namespaceIndent << indent << uiClassName << ' ';
|
2014-07-18 15:47:20 +02:00
|
|
|
if (generationParameters.embedding == QtSupport::CodeGenSettings::PointerAggregatedUiClass)
|
2011-01-10 16:29:06 +01:00
|
|
|
headerStr << '*';
|
|
|
|
|
headerStr << uiMemberC << ";\n";
|
|
|
|
|
}
|
|
|
|
|
headerStr << namespaceIndent << "};\n\n";
|
2014-07-24 10:59:11 +02:00
|
|
|
Utils::writeClosingNameSpaces(namespaceList, QString(), headerStr);
|
2011-01-10 16:29:06 +01:00
|
|
|
headerStr << "#endif // "<< guard << '\n';
|
|
|
|
|
|
|
|
|
|
// 2) Source file
|
|
|
|
|
QTextStream sourceStr(source);
|
|
|
|
|
sourceStr << sourceLicense;
|
|
|
|
|
Utils::writeIncludeFileDirective(parameters.headerFile, false, sourceStr);
|
2014-07-18 15:47:20 +02:00
|
|
|
if (generationParameters.embedding == QtSupport::CodeGenSettings::PointerAggregatedUiClass)
|
2011-01-10 16:29:06 +01:00
|
|
|
Utils::writeIncludeFileDirective(uiInclude, false, sourceStr);
|
|
|
|
|
// NameSpaces(
|
2014-07-24 10:59:11 +02:00
|
|
|
Utils::writeOpeningNameSpaces(namespaceList, QString(), sourceStr);
|
2011-01-10 16:29:06 +01:00
|
|
|
// Constructor with setupUi
|
|
|
|
|
sourceStr << '\n' << namespaceIndent << unqualifiedClassName << "::" << unqualifiedClassName << "(QWidget *parent) :\n"
|
|
|
|
|
<< namespaceIndent << indent << formBaseClass << "(parent)";
|
2014-07-18 15:47:20 +02:00
|
|
|
if (generationParameters.embedding == QtSupport::CodeGenSettings::PointerAggregatedUiClass)
|
2011-01-10 16:29:06 +01:00
|
|
|
sourceStr << ",\n" << namespaceIndent << indent << uiMemberC << "(new " << uiClassName << ")";
|
|
|
|
|
sourceStr << '\n' << namespaceIndent << "{\n" << namespaceIndent << indent;
|
2014-07-18 15:47:20 +02:00
|
|
|
Internal::writeUiMemberAccess(generationParameters, sourceStr);
|
2011-01-10 16:29:06 +01:00
|
|
|
sourceStr << "setupUi(this);\n" << namespaceIndent << "}\n";
|
|
|
|
|
// Deleting destructor for ptr
|
2014-07-18 15:47:20 +02:00
|
|
|
if (generationParameters.embedding == QtSupport::CodeGenSettings::PointerAggregatedUiClass) {
|
2011-01-10 16:29:06 +01:00
|
|
|
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;
|
2014-07-18 15:47:20 +02:00
|
|
|
Internal::writeUiMemberAccess(generationParameters, sourceStr);
|
2011-01-10 16:29:06 +01:00
|
|
|
sourceStr << "retranslateUi(this);\n"
|
|
|
|
|
<< namespaceIndent << indent << indent << "break;\n"
|
|
|
|
|
<< namespaceIndent << indent << "default:\n"
|
|
|
|
|
<< namespaceIndent << indent << indent << "break;\n"
|
|
|
|
|
<< namespaceIndent << indent << "}\n"
|
|
|
|
|
<< namespaceIndent << "}\n";
|
|
|
|
|
}
|
2014-07-24 10:59:11 +02:00
|
|
|
Utils::writeClosingNameSpaces(namespaceList, QString(), sourceStr);
|
2011-01-10 16:29:06 +01:00
|
|
|
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
|