Commit 50df8aa9 authored by Tobias Hunger's avatar Tobias Hunger
Browse files

JsonWizard: Replace CppClassWizard with a JsonWizard



Change-Id: I4e1aeea4ccd1087b5e804e9ba4c2aa8fde4f4fcb
Reviewed-by: default avatarNikolai Kosjar <nikolai.kosjar@theqtcompany.com>
parent f429d3a0
%{Cpp:LicenseTemplate}
#include "%{HdrFileName}"
%{JS: Cpp.openNamespaces('%{Class}')}
@if '%{IncludeQSharedData}'
class %{CN}Data : public QSharedData
{
public:
};
@endif
@if '%{Base}' === 'QObject'
%{CN}::%{CN}(QObject *parent) : QObject(parent)%{JS: ('%{SharedDataInit}') ? ', %{SharedDataInit}' : ''}
@elsif '%{Base}' === 'QWidget' || '%{Base}' === 'QMainWindow'
%{CN}::%{CN}(QWidget *parent) : %{Base}(parent)%{JS: ('%{SharedDataInit}') ? ', %{SharedDataInit}' : ''}
@else
%{CN}::%{CN}()%{JS: ('%{SharedDataInit}') ? ' : %{SharedDataInit}' : ''}
@endif
{
}
@if '%{IncludeQSharedData}'
%{CN}::%{CN}(const %{CN} &rhs) : data(rhs.data)
{
}
%{CN} &%{CN}::operator=(const %{CN} &rhs)
{
if (this != &rhs)
data.operator=(rhs.data);
return *this;
}
@endif
%{CN}::~%{CN}()
{
}
%{JS: Cpp.closeNamespaces('%{Class}')}
%{Cpp:LicenseTemplate}
#ifndef %{GUARD}
#define %{GUARD}
%{JS: QtSupport.qtIncludes([ ( '%{IncludeQObject}' ) ? 'QtCore/%{IncludeQObject}' : '',
( '%{IncludeQWidget}' ) ? 'QtGui/%{IncludeQWidget}' : '',
( '%{IncludeQMainWindow}' ) ? 'QtGui/%{IncludeQMainWindow}' : '',
( '%{IncludeQDeclarativeItem}' ) ? 'QtDeclarative/%{IncludeQDeclarativeItem}' : '',
( '%{IncludeQSharedData}' ) ? 'QtCore/QSharedDataPointer' : '' ],
[ ( '%{IncludeQObject}' ) ? 'QtCore/%{IncludeQObject}' : '',
( '%{IncludeQWidget}' ) ? 'QtWidgets/%{IncludeQWidget}' : '',
( '%{IncludeQMainWindow}' ) ? 'QtWidgets/%{IncludeQMainWindow}' : '',
( '%{IncludeQDeclarativeItem}' ) ? 'QtQuick1/%{IncludeQDeclarativeItem}' : '',
( '%{IncludeQQuickItem}' ) ? 'QtDeclarative/%{IncludeQQuickItem}' : '',
( '%{IncludeQSharedData}' ) ? 'QtCore/QSharedDataPointer' : '' ])}
%{JS: Cpp.openNamespaces('%{Class}')}
@if '%{IncludeQSharedData}'
class %{CN}Data;
@endif
@if '%{Base}'
class %{CN} : public %{Base}
@else
class %{CN}
@endif
{
@if '%{isQObject}'
Q_OBJECT
public:
explicit %{CN}(QObject *parent = 0);
@elsif '%{Base}' === 'QWidget' || '%{Base}' === 'QMainWindow'
public
explicit %{CN}(QWidget *parent = 0);
@else
public:
%{CN}();
@endif
@if '%{IncludeQSharedData}'
%{CN}(const %{CN} &);
%{CN} &operator=(const %{CN} &);
@endif
~%{CN}();
@if '%{isQObject}'
signals:
public slots:
@endif
@if '%{IncludeQSharedData}'
private:
QSharedDataPointer<%{CN}Data> data;
@endif
};
%{JS: Cpp.closeNamespaces('%{Class}')}
#endif // %{GUARD}
{
"version": 1,
"kind": "class",
"id": "A.Class",
"category": "O.C++",
"trDescription": "Creates a C++ header and a source file for a new class that you can add to a C++ project.",
"trDisplayName": "C++ Class",
"trDisplayCategory": "C++",
"icon": "../../global/genericfilewizard.png",
"featuresRequired": [ "Plugin.CppEditor" ],
"options":
[
{ "key": "TargetPath", "value": "%{Path}" },
{ "key": "HdrPath", "value": "%{Path}/%{HdrFileName}" },
{ "key": "SrcPath", "value": "%{Path}/%{SrcFileName}" },
{ "key": "CN", "value": "%{JS: Cpp.className('%{Class}')}" },
{ "key": "Base", "value": "%{JS: ( '%{BaseCB}' === '' ) ? '%{BaseEdit}' : '%{BaseCB}'}" },
{ "key": "isQObject", "value": "%{JS: ('%{Base}' === 'QObject' || '%{Base}' === 'QWidget' || '%{Base}' === 'QMainWindow' || '%{Base}' === 'QDeclarativeItem' || '%{Base}' === 'QQuickItem' ) ? 'yes' : ''}" },
{ "key": "GUARD", "value": "%{JS: Cpp.classToHeaderGuard('%{Class}', '%{JS: Util.preferredSuffix('text/x-c++hdr')}')}" },
{ "key": "SharedDataInit", "value": "%{JS: ('%{IncludeQSharedData}') ? 'data(new %{CN}Data)' : '' }" }
],
"pages":
[
{
"trDisplayName": "Define Class",
"trShortTitle": "Details",
"typeId": "Fields",
"data" :
[
{
"name": "Class",
"trDisplayName": "Class name:",
"mandatory": true,
"type": "LineEdit"
},
{
"name": "BaseCB",
"trDisplayName": "Base class:",
"type": "ComboBox",
"data":
{
"items": [ { "trKey": "<Custom>", "value": "" },
"QObject", "QWidget", "QMainWindow", "QDeclarativeItem", "QQuickItem" ]
}
},
{
"name": "BaseEdit",
"type": "LineEdit",
"enabled": "%{JS: ( '%{BaseCB}' === '' ) ? 'yes' : ''}",
"mandatory": false,
"data":
{
"trText": "%{BaseCB}",
"trDisabledText": "%{BaseCB}"
}
},
{
"name": "Sp1",
"type": "Spacer",
"data": { "factor": 2 }
},
{
"name": "IncludeQObject",
"trDisplayName": "Include QObject",
"type": "CheckBox",
"data":
{
"checkedValue": "QObject",
"uncheckedValue": "",
"checked": "%{JS: ('%{BaseCB}' === 'QObject' ) ? 'yes' : ''}"
}
},
{
"name": "IncludeQWidget",
"trDisplayName": "Include QWidget",
"type": "CheckBox",
"data":
{
"checkedValue": "QWidget",
"uncheckedValue": "",
"checked": "%{JS: ('%{BaseCB}' === 'QWidget') ? 'yes' : ''}"
}
},
{
"name": "IncludeQMainWindow",
"trDisplayName": "Include QMainWindow",
"type": "CheckBox",
"data":
{
"checkedValue": "QMainWindow",
"uncheckedValue": "",
"checked": "%{JS: ('%{BaseCB}' === 'QMainWindow') ? 'yes' : ''}"
}
},
{
"name": "IncludeQDeclarativeItem",
"trDisplayName": "Include QDeclarativeItem - Qt Quick 1",
"type": "CheckBox",
"data":
{
"checkedValue": "QDeclarativeItem",
"uncheckedValue": "",
"checked": "%{JS: ('%{BaseCB}' === 'QDeclarativeItem') ? 'yes' : ''}"
}
},
{
"name": "IncludeQQuickItem",
"trDisplayName": "Include QQuickItem - Qt Quick 2",
"type": "CheckBox",
"data":
{
"checkedValue": "QQuickItem",
"uncheckedValue": "",
"checked": "%{JS: ('%{BaseCB}' === 'QQuickItem') ? 'yes' : ''}"
}
},
{
"name": "IncludeQSharedData",
"trDisplayName": "Include QSharedData",
"type": "CheckBox",
"data":
{
"checkedValue": "QSharedData",
"uncheckedValue": "",
"checked": false
}
},
{
"name": "Sp2",
"type": "Spacer",
"data": { "size": 12 }
},
{
"name": "HdrFileName",
"type": "LineEdit",
"trDisplayName": "Header file:",
"mandatory": true,
"data": { "trText": "%{JS: Cpp.classToFileName('%{Class}', '%{JS: Util.preferredSuffix('text/x-c++hdr')}')}" }
},
{
"name": "SrcFileName",
"type": "LineEdit",
"trDisplayName": "Source file:",
"mandatory": true,
"data": { "trText": "%{JS: Cpp.classToFileName('%{Class}', '%{JS: Util.preferredSuffix('text/x-c++src')}')}" }
},
{
"name": "Path",
"type": "PathChooser",
"trDisplayName": "Path:",
"mandatory": true,
"data":
{
"kind": "existingDirectory",
"basePath": "%{InitialPath}",
"path": "%{InitialPath}"
}
}
]
},
{
"trDisplayName": "Project Management",
"trShortTitle": "Summary",
"typeId": "Summary"
}
],
"generators":
[
{
"typeId": "File",
"data":
[
{
"source": "file.h",
"target": "%{HdrPath}",
"openInEditor": true
},
{
"source": "file.cpp",
"target": "%{SrcPath}",
"openInEditor": true
}
]
}
]
}
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** 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://www.qt.io/licensing. For further information
** use the contact form at http://www.qt.io/contact-us.
**
** 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 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** 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
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "cppclasswizard.h"
#include "cppeditorconstants.h"
#include <coreplugin/icore.h>
#include <coreplugin/mimedatabase.h>
#include <cpptools/abstracteditorsupport.h>
#include <cpptools/cpptoolsconstants.h>
#include <utils/codegeneration.h>
#include <utils/newclasswidget.h>
#include <utils/qtcassert.h>
#include <utils/wizard.h>
#include <QDebug>
#include <QTextStream>
#include <QVBoxLayout>
#include <QSpacerItem>
#include <QWizard>
using namespace CppEditor;
using namespace CppEditor::Internal;
// ========= ClassNamePage =========
ClassNamePage::ClassNamePage(QWidget *parent) :
QWizardPage(parent),
m_isValid(false)
{
setTitle(tr("Enter Class Name"));
setSubTitle(tr("The header and source file names will be derived from the class name"));
m_newClassWidget = new Utils::NewClassWidget;
// Order, set extensions first before suggested name is derived
m_newClassWidget->setBaseClassInputVisible(true);
m_newClassWidget->setBaseClassChoices(QStringList() << QString()
<< QLatin1String("QObject")
<< QLatin1String("QWidget")
<< QLatin1String("QMainWindow")
<< QLatin1String("QDeclarativeItem")
<< QLatin1String("QQuickItem"));
m_newClassWidget->setBaseClassEditable(true);
m_newClassWidget->setFormInputVisible(false);
m_newClassWidget->setNamespacesEnabled(true);
m_newClassWidget->setAllowDirectories(true);
m_newClassWidget->setBaseClassInputVisible(true);
connect(m_newClassWidget, SIGNAL(validChanged()), this, SLOT(slotValidChanged()));
QVBoxLayout *pageLayout = new QVBoxLayout(this);
pageLayout->addWidget(m_newClassWidget);
QSpacerItem *vSpacer = new QSpacerItem(0, 0, QSizePolicy::Ignored, QSizePolicy::Expanding);
pageLayout->addItem(vSpacer);
initParameters();
setProperty(Utils::SHORT_TITLE_PROPERTY, tr("Details"));
}
// Retrieve settings of CppTools plugin.
static bool lowerCaseFiles()
{
QString lowerCaseSettingsKey = QLatin1String(CppTools::Constants::CPPTOOLS_SETTINGSGROUP);
lowerCaseSettingsKey += QLatin1Char('/');
lowerCaseSettingsKey += QLatin1String(CppTools::Constants::LOWERCASE_CPPFILES_KEY);
const bool lowerCaseDefault = CppTools::Constants::lowerCaseFilesDefault;
return Core::ICore::settings()->value(lowerCaseSettingsKey, QVariant(lowerCaseDefault)).toBool();
}
// Set up new class widget from settings
void ClassNamePage::initParameters()
{
m_newClassWidget->setHeaderExtension(Core::MimeDatabase::preferredSuffixByType(QLatin1String(Constants::CPP_HEADER_MIMETYPE)));
m_newClassWidget->setSourceExtension(Core::MimeDatabase::preferredSuffixByType(QLatin1String(Constants::CPP_SOURCE_MIMETYPE)));
m_newClassWidget->setLowerCaseFiles(lowerCaseFiles());
}
void ClassNamePage::slotValidChanged()
{
const bool validNow = m_newClassWidget->isValid();
if (m_isValid != validNow) {
m_isValid = validNow;
emit completeChanged();
}
}
CppClassWizardDialog::CppClassWizardDialog(QWidget *parent) :
Core::BaseFileWizard(parent),
m_classNamePage(new ClassNamePage(this))
{
setWindowTitle(tr("C++ Class Wizard"));
addPage(m_classNamePage);
}
void CppClassWizardDialog::setPath(const QString &path)
{
m_classNamePage->newClassWidget()->setPath(path);
}
CppClassWizardParameters CppClassWizardDialog::parameters() const
{
CppClassWizardParameters rc;
const Utils::NewClassWidget *ncw = m_classNamePage->newClassWidget();
rc.className = ncw->className();
rc.headerFile = ncw->headerFileName();
rc.sourceFile = ncw->sourceFileName();
rc.baseClass = ncw->baseClassName();
rc.path = ncw->path();
rc.classType = ncw->classType();
return rc;
}
// ========= CppClassWizard =========
CppClassWizard::CppClassWizard()
{
}
QString CppClassWizard::sourceSuffix() const
{
return preferredSuffix(QLatin1String(Constants::CPP_SOURCE_MIMETYPE));
}
QString CppClassWizard::headerSuffix() const
{
return preferredSuffix(QLatin1String(Constants::CPP_HEADER_MIMETYPE));
}
Core::BaseFileWizard *CppClassWizard::create(QWidget *parent, const Core::WizardDialogParameters &parameters) const
{
CppClassWizardDialog *wizard = new CppClassWizardDialog(parent);
foreach (QWizardPage *p, parameters.extensionPages())
wizard->addPage(p);
wizard->setPath(parameters.defaultPath());
return wizard;
}
Core::GeneratedFiles CppClassWizard::generateFiles(const QWizard *w, QString *errorMessage) const
{
const CppClassWizardDialog *wizard = qobject_cast<const CppClassWizardDialog *>(w);
const CppClassWizardParameters params = wizard->parameters();
const QString sourceFileName = Core::BaseFileWizardFactory::buildFileName(params.path, params.sourceFile, sourceSuffix());
const QString headerFileName = Core::BaseFileWizardFactory::buildFileName(params.path, params.headerFile, headerSuffix());
Core::GeneratedFile sourceFile(sourceFileName);
Core::GeneratedFile headerFile(headerFileName);
QString header, source;
if (!generateHeaderAndSource(params, &header, &source)) {
*errorMessage = tr("Error while generating file contents.");
return Core::GeneratedFiles();
}
headerFile.setContents(header);
headerFile.setAttributes(Core::GeneratedFile::OpenEditorAttribute);
sourceFile.setContents(source);
sourceFile.setAttributes(Core::GeneratedFile::OpenEditorAttribute);
return Core::GeneratedFiles() << headerFile << sourceFile;
}
bool CppClassWizard::generateHeaderAndSource(const CppClassWizardParameters &params,
QString *header, QString *source)
{
// TODO:
// Quite a bit of this code has been copied from FormClassWizardParameters::generateCpp
// and is duplicated in the library wizard.
// Maybe more of it could be merged into Utils.
const QString indent = QString(4, QLatin1Char(' '));
// Do we have namespaces?
QStringList namespaceList = params.className.split(QLatin1String("::"));
if (namespaceList.empty()) // Paranoia!
return false;
const QString headerLicense =
CppTools::AbstractEditorSupport::licenseTemplate(params.headerFile,
params.className);
const QString sourceLicense =
CppTools::AbstractEditorSupport::licenseTemplate(params.sourceFile,
params.className);
const QString unqualifiedClassName = namespaceList.takeLast();
const QString guard = Utils::headerGuard(params.headerFile, namespaceList);
// == Header file ==
QTextStream headerStr(header);
headerStr << headerLicense << "#ifndef " << guard
<< "\n#define " << guard << '\n';
QRegExp qtClassExpr(QLatin1String("^Q[A-Z3].+"));
QTC_CHECK(qtClassExpr.isValid());
// Determine parent QObject type for Qt types. Provide base
// class in case the user did not specify one.
QString parentQObjectClass;
bool defineQObjectMacro = false;
switch (params.classType) {
case Utils::NewClassWidget::ClassInheritsQObject:
parentQObjectClass = QLatin1String("QObject");
defineQObjectMacro = true;
break;
case Utils::NewClassWidget::ClassInheritsQWidget:
parentQObjectClass = QLatin1String("QWidget");
defineQObjectMacro = true;
break;
case Utils::NewClassWidget::ClassInheritsQDeclarativeItem:
parentQObjectClass = QLatin1String("QDeclarativeItem");
defineQObjectMacro = true;
break;
case Utils::NewClassWidget::ClassInheritsQQuickItem:
parentQObjectClass = QLatin1String("QQuickItem");
defineQObjectMacro = true;
break;
case Utils::NewClassWidget::NoClassType:
case Utils::NewClassWidget::SharedDataClass:
break;
}
const QString baseClass = params.baseClass.isEmpty()
&& params.classType != Utils::NewClassWidget::NoClassType ?
parentQObjectClass : params.baseClass;
const bool superIsQtClass = qtClassExpr.exactMatch(baseClass);
if (superIsQtClass) {
headerStr << '\n';
Utils::writeIncludeFileDirective(baseClass, true, headerStr);
}
if (params.classType == Utils::NewClassWidget::SharedDataClass) {
headerStr << '\n';
Utils::writeIncludeFileDirective(QLatin1String("QSharedDataPointer"), true, headerStr);
}
const QString namespaceIndent = Utils::writeOpeningNameSpaces(namespaceList, QString(), headerStr);
const QString sharedDataClass = unqualifiedClassName + QLatin1String("Data");
if (params.classType == Utils::NewClassWidget::SharedDataClass)
headerStr << '\n' << "class " << sharedDataClass << ";\n";
// Class declaration
headerStr << '\n' << namespaceIndent << "class " << unqualifiedClassName;
if (!baseClass.isEmpty())
headerStr << " : public " << baseClass << "\n";
else
headerStr << "\n";
headerStr << namespaceIndent << "{\n";
if (defineQObjectMacro)
headerStr << namespaceIndent << indent << "Q_OBJECT\n";
headerStr << namespaceIndent << "public:\n"
<< namespaceIndent << indent;
// Constructor
if (parentQObjectClass.isEmpty()) {
headerStr << unqualifiedClassName << "();\n";
} else {
headerStr << "explicit " << unqualifiedClassName << '(' << parentQObjectClass
<< " *parent = 0);\n";
}
// Copy/Assignment for shared data classes.
if (params.classType == Utils::NewClassWidget::SharedDataClass) {