Fix Gui app wizard to write form classes consistent with Designer.

Make Gui app wizard use Qt Designer wizard code to write the classes.
Remove the settings from the form class wizard page extension area and
add a settings page instead. Polish the designer plugin's structs to
parametrize class generation with QSharedData and settings code.
Export them and use them in the Gui app wizard.

Task-number: 256927
This commit is contained in:
Friedemann Kleint
2009-07-01 16:27:40 +02:00
parent 310e28ba95
commit f7da76ab27
18 changed files with 828 additions and 329 deletions

View File

@@ -34,32 +34,298 @@
#include <cpptools/cppmodelmanagerinterface.h>
#include <QtCore/QTextStream>
#include <QtCore/QSettings>
#include <QtCore/QFileInfo>
#include <QtCore/QDebug>
#include <QtCore/QSharedData>
static const char *uiMemberC = "m_ui";
static const char *uiNamespaceC = "Ui";
namespace Designer {
namespace Internal {
static const char *formClassWizardPageGroupC = "FormClassWizardPage";
static const char *translationKeyC = "RetranslationSupport";
static const char *embeddingModeKeyC = "Embedding";
FormClassWizardParameters::FormClassWizardParameters() :
embedding(PointerAggregatedUiClass),
languageChange(true)
// TODO: These 2 are general coding convention settings and
// should go to CppTools...
static const char *includeQtModuleKeyC = "IncludeQtModule";
static const char *indentNamespaceKeyC = "IndentNamespace";
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(true),
includeQtModule(false),
indentNamespace(false)
{
}
bool FormClassWizardParameters::generateCpp(QString *header, QString *source, int indentation) const
void FormClassWizardGenerationParametersPrivate::fromSettings(const QSettings *settings)
{
const QString indent = QString(indentation, QLatin1Char(' '));
QString key = QLatin1String(formClassWizardPageGroupC);
key += QLatin1Char('/');
const int groupLength = key.size();
key += QLatin1String(translationKeyC);
retranslationSupport = settings->value(key, true).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 (!FormTemplateWizardPage::getUIXmlData(uiTemplate, &formBaseClass, &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)
@@ -87,88 +353,89 @@ bool FormClassWizardParameters::generateCpp(QString *header, QString *source, in
<< "\n#define " << guard << '\n' << '\n';
// Include 'ui_'
if (embedding != PointerAggregatedUiClass) {
if (embedding != FormClassWizardGenerationParameters::PointerAggregatedUiClass) {
Core::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 = QLatin1String("QtGui/");
baseInclude += formBaseClass;
QString baseInclude = formBaseClass;
if (generationParameters.includeQtModule())
baseInclude.insert(0, QLatin1String("QtGui/"));
Core::Utils::writeIncludeFileDirective(baseInclude, true, headerStr);
}
}
const QString namespaceIndent = Core::Utils::writeOpeningNameSpaces(namespaceList, indent, headerStr);
const QString namespaceIndent = Core::Utils::writeOpeningNameSpaces(namespaceList,
generationParameters.indentNamespace() ? indent : QString(),
headerStr);
// Forward-declare the UI class
if (embedding == PointerAggregatedUiClass) {
if (embedding == FormClassWizardGenerationParameters::PointerAggregatedUiClass) {
headerStr << '\n'
<< namespaceIndent << "namespace " << uiNamespaceC << " {\n"
<< namespaceIndent << indent << "class " << FormTemplateWizardPage::stripNamespaces(uiClassName) << ";\n"
<< namespaceIndent << indent << "class " << Internal::FormTemplateWizardPage::stripNamespaces(uiClassName) << ";\n"
<< namespaceIndent << "}\n";
}
// Class declaration
headerStr << '\n' << namespaceIndent << "class " << unqualifiedClassName
<< " : public " << formBaseClass;
if (embedding == InheritedUiClass) {
if (embedding == FormClassWizardGenerationParameters::InheritedUiClass) {
headerStr << ", private " << uiClassName;
}
headerStr << " {\n" << namespaceIndent << indent << "Q_OBJECT\n"
<< namespaceIndent << "public:\n"
<< namespaceIndent << indent << unqualifiedClassName << "(QWidget *parent = 0);\n";
if (embedding == PointerAggregatedUiClass)
if (embedding == FormClassWizardGenerationParameters::PointerAggregatedUiClass)
headerStr << namespaceIndent << indent << "~" << unqualifiedClassName << "();\n";
// retranslation
if (languageChange)
if (generationParameters.retranslationSupport())
headerStr << '\n' << namespaceIndent << "protected:\n"
<< namespaceIndent << indent << "void changeEvent(QEvent *e);\n";
// Member variable
if (embedding != InheritedUiClass) {
if (embedding != FormClassWizardGenerationParameters::InheritedUiClass) {
headerStr << '\n' << namespaceIndent << "private:\n"
<< namespaceIndent << indent << uiClassName << ' ';
if (embedding == PointerAggregatedUiClass)
if (embedding == FormClassWizardGenerationParameters::PointerAggregatedUiClass)
headerStr << '*';
headerStr << uiMemberC << ";\n";
}
headerStr << namespaceIndent << "};\n\n";
Core::Utils::writeClosingNameSpaces(namespaceList, indent, headerStr);
Core::Utils::writeClosingNameSpaces(namespaceList, generationParameters.indentNamespace() ? indent : QString(), headerStr);
headerStr << "#endif // "<< guard << '\n';
// 2) Source file
QTextStream sourceStr(source);
sourceStr << license;
Core::Utils::writeIncludeFileDirective(headerFile, false, sourceStr);
if (embedding == PointerAggregatedUiClass)
if (embedding == FormClassWizardGenerationParameters::PointerAggregatedUiClass)
Core::Utils::writeIncludeFileDirective(uiInclude, false, sourceStr);
// NameSpaces(
Core::Utils::writeOpeningNameSpaces(namespaceList, indent, sourceStr);
Core::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 == PointerAggregatedUiClass)
if (embedding == FormClassWizardGenerationParameters::PointerAggregatedUiClass)
sourceStr << ",\n" << namespaceIndent << indent << uiMemberC << "(new " << uiClassName << ")\n";
sourceStr << namespaceIndent << "{\n" << namespaceIndent << indent;
if (embedding != InheritedUiClass)
sourceStr << uiMemberC << (embedding == PointerAggregatedUiClass ? "->" : ".");
sourceStr << "setupUi(this);\n" << namespaceIndent << "}\n";
writeUiMemberAccess(generationParameters, sourceStr);
sourceStr << "setupUi(this);\n" << namespaceIndent << "}\n";
// Deleting destructor for ptr
if (embedding == PointerAggregatedUiClass) {
if (embedding == FormClassWizardGenerationParameters::PointerAggregatedUiClass) {
sourceStr << '\n' << namespaceIndent << unqualifiedClassName << "::~" << unqualifiedClassName
<< "()\n" << namespaceIndent << "{\n"
<< namespaceIndent << indent << "delete " << uiMemberC << ";\n"
<< namespaceIndent << "}\n";
}
// retranslation
if (languageChange) {
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;
if (embedding != InheritedUiClass)
sourceStr << uiMemberC << (embedding == PointerAggregatedUiClass ? "->" : ".");
writeUiMemberAccess(generationParameters, sourceStr);
sourceStr << "retranslateUi(this);\n"
<< namespaceIndent << indent << indent << "break;\n"
<< namespaceIndent << indent << "default:\n"
@@ -176,9 +443,15 @@ bool FormClassWizardParameters::generateCpp(QString *header, QString *source, in
<< namespaceIndent << indent << "}\n"
<< namespaceIndent << "}\n";
}
Core::Utils::writeClosingNameSpaces(namespaceList, indent, sourceStr);
Core::Utils::writeClosingNameSpaces(namespaceList, generationParameters.indentNamespace() ? indent : QString(), sourceStr);
return true;
}
} // namespace Internal
bool FormClassWizardParameters::generateCpp(const FormClassWizardGenerationParameters &fgp,
QString *header, QString *source, int indentation) const
{
return m_d->generateCpp(fgp, header, source, indentation);
}
} // namespace Designer