Files
qt-creator/src/plugins/cppeditor/cppclasswizard.cpp
T

361 lines
14 KiB
C++
Raw Normal View History

2012-10-02 09:12:39 +02:00
/****************************************************************************
2008-12-02 12:01:29 +01:00
**
2014-01-07 13:27:11 +01:00
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
2012-10-02 09:12:39 +02:00
** Contact: http://www.qt-project.org/legal
2008-12-02 12:01:29 +01:00
**
2012-10-02 09:12:39 +02:00
** This file is part of Qt Creator.
2008-12-02 12:01:29 +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.
**
** 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.
**
2012-10-02 09:12:39 +02:00
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
2010-12-17 16:01:08 +01:00
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
2012-10-02 09:12:39 +02:00
****************************************************************************/
2008-12-02 15:08:31 +01:00
2008-12-02 12:01:29 +01:00
#include "cppclasswizard.h"
2008-12-02 12:01:29 +01:00
#include "cppeditorconstants.h"
#include <coreplugin/icore.h>
#include <coreplugin/mimedatabase.h>
#include <cpptools/abstracteditorsupport.h>
#include <cpptools/cpptoolsconstants.h>
2008-12-02 12:01:29 +01:00
#include <utils/codegeneration.h>
#include <utils/newclasswidget.h>
2008-12-09 15:25:01 +01:00
#include <utils/qtcassert.h>
#include <utils/wizard.h>
2008-12-02 12:01:29 +01:00
#include <QDebug>
#include <QTextStream>
#include <QVBoxLayout>
#include <QSpacerItem>
#include <QWizard>
2008-12-02 12:01:29 +01:00
using namespace CppEditor;
using namespace CppEditor::Internal;
// ========= ClassNamePage =========
ClassNamePage::ClassNamePage(QWidget *parent) :
2008-12-02 12:01:29 +01:00
QWizardPage(parent),
m_isValid(false)
{
2010-03-31 14:48:08 +02:00
setTitle(tr("Enter Class Name"));
2008-12-02 12:01:29 +01:00
setSubTitle(tr("The header and source file names will be derived from the class name"));
2009-10-05 11:06:05 +02:00
m_newClassWidget = new Utils::NewClassWidget;
2008-12-02 12:01:29 +01:00
// Order, set extensions first before suggested name is derived
m_newClassWidget->setBaseClassInputVisible(true);
m_newClassWidget->setBaseClassChoices(QStringList() << QString()
<< QLatin1String("QObject")
<< QLatin1String("QWidget")
<< QLatin1String("QMainWindow")
2013-01-17 16:18:29 +01:00
<< QLatin1String("QDeclarativeItem")
<< QLatin1String("QQuickItem"));
2008-12-02 12:01:29 +01:00
m_newClassWidget->setBaseClassEditable(true);
m_newClassWidget->setFormInputVisible(false);
m_newClassWidget->setNamespacesEnabled(true);
m_newClassWidget->setAllowDirectories(true);
m_newClassWidget->setBaseClassInputVisible(true);
2008-12-02 12:01:29 +01:00
connect(m_newClassWidget, SIGNAL(validChanged()), this, SLOT(slotValidChanged()));
2008-12-02 12:01:29 +01:00
2010-01-29 21:33:57 +01:00
QVBoxLayout *pageLayout = new QVBoxLayout(this);
2008-12-02 12:01:29 +01:00
pageLayout->addWidget(m_newClassWidget);
QSpacerItem *vSpacer = new QSpacerItem(0, 0, QSizePolicy::Ignored, QSizePolicy::Expanding);
pageLayout->addItem(vSpacer);
2010-10-19 11:10:51 +02:00
initParameters();
setProperty(Utils::SHORT_TITLE_PROPERTY, tr("Details"));
}
// Retrieve settings of CppTools plugin.
2012-01-24 15:36:40 +01:00
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;
2012-01-24 15:36:40 +01:00
return Core::ICore::settings()->value(lowerCaseSettingsKey, QVariant(lowerCaseDefault)).toBool();
}
// Set up new class widget from settings
void ClassNamePage::initParameters()
{
2013-08-30 16:38:57 +02:00
m_newClassWidget->setHeaderExtension(Core::MimeDatabase::preferredSuffixByType(QLatin1String(Constants::CPP_HEADER_MIMETYPE)));
m_newClassWidget->setSourceExtension(Core::MimeDatabase::preferredSuffixByType(QLatin1String(Constants::CPP_SOURCE_MIMETYPE)));
2012-01-24 15:36:40 +01:00
m_newClassWidget->setLowerCaseFiles(lowerCaseFiles());
}
2008-12-02 12:01:29 +01:00
void ClassNamePage::slotValidChanged()
{
const bool validNow = m_newClassWidget->isValid();
if (m_isValid != validNow) {
m_isValid = validNow;
emit completeChanged();
}
}
CppClassWizardDialog::CppClassWizardDialog(QWidget *parent) :
2014-05-27 13:18:05 +02:00
Core::BaseFileWizard(parent),
m_classNamePage(new ClassNamePage(this))
2008-12-02 12:01:29 +01:00
{
setWindowTitle(tr("C++ Class Wizard"));
addPage(m_classNamePage);
2008-12-02 12:01:29 +01:00
}
void CppClassWizardDialog::setPath(const QString &path)
{
m_classNamePage->newClassWidget()->setPath(path);
}
CppClassWizardParameters CppClassWizardDialog::parameters() const
{
CppClassWizardParameters rc;
2009-10-05 11:06:05 +02:00
const Utils::NewClassWidget *ncw = m_classNamePage->newClassWidget();
2008-12-02 12:01:29 +01:00
rc.className = ncw->className();
rc.headerFile = ncw->headerFileName();
rc.sourceFile = ncw->sourceFileName();
rc.baseClass = ncw->baseClassName();
rc.path = ncw->path();
rc.classType = ncw->classType();
2008-12-02 12:01:29 +01:00
return rc;
}
// ========= CppClassWizard =========
2013-09-20 15:12:44 +02:00
CppClassWizard::CppClassWizard()
2008-12-02 12:01:29 +01:00
{
}
QString CppClassWizard::sourceSuffix() const
{
return preferredSuffix(QLatin1String(Constants::CPP_SOURCE_MIMETYPE));
}
QString CppClassWizard::headerSuffix() const
{
return preferredSuffix(QLatin1String(Constants::CPP_HEADER_MIMETYPE));
}
2014-05-27 13:18:05 +02:00
Core::BaseFileWizard *CppClassWizard::create(QWidget *parent, const Core::WizardDialogParameters &parameters) const
2008-12-02 12:01:29 +01:00
{
CppClassWizardDialog *wizard = new CppClassWizardDialog(parent);
foreach (QWizardPage *p, parameters.extensionPages())
wizard->addPage(p);
wizard->setPath(parameters.defaultPath());
2008-12-02 12:01:29 +01:00
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());
2008-12-02 12:01:29 +01:00
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);
2008-12-02 12:01:29 +01:00
sourceFile.setContents(source);
sourceFile.setAttributes(Core::GeneratedFile::OpenEditorAttribute);
2008-12-02 12:01:29 +01:00
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.
2009-10-05 11:06:05 +02:00
// Maybe more of it could be merged into Utils.
2008-12-02 12:01:29 +01:00
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);
2008-12-02 12:01:29 +01:00
const QString unqualifiedClassName = namespaceList.takeLast();
2011-01-18 12:18:08 +01:00
const QString guard = Utils::headerGuard(params.headerFile, namespaceList);
2008-12-02 12:01:29 +01:00
// == Header file ==
QTextStream headerStr(header);
headerStr << headerLicense << "#ifndef " << guard
<< "\n#define " << guard << '\n';
2008-12-02 12:01:29 +01:00
2012-04-30 12:01:15 +02:00
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;
2012-11-28 20:44:03 +02:00
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;
2013-01-17 16:18:29 +01:00
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);
2008-12-02 12:01:29 +01:00
if (superIsQtClass) {
headerStr << '\n';
Utils::writeIncludeFileDirective(baseClass, true, headerStr);
2008-12-02 12:01:29 +01:00
}
2009-12-01 16:17:45 +01:00
if (params.classType == Utils::NewClassWidget::SharedDataClass) {
headerStr << '\n';
Utils::writeIncludeFileDirective(QLatin1String("QSharedDataPointer"), true, headerStr);
}
2008-12-02 12:01:29 +01:00
2009-10-05 11:06:05 +02:00
const QString namespaceIndent = Utils::writeOpeningNameSpaces(namespaceList, QString(), headerStr);
2008-12-02 12:01:29 +01:00
2009-12-01 16:17:45 +01:00
const QString sharedDataClass = unqualifiedClassName + QLatin1String("Data");
if (params.classType == Utils::NewClassWidget::SharedDataClass)
headerStr << '\n' << "class " << sharedDataClass << ";\n";
2008-12-02 12:01:29 +01:00
// Class declaration
2009-12-01 16:17:45 +01:00
headerStr << '\n' << namespaceIndent << "class " << unqualifiedClassName;
if (!baseClass.isEmpty())
headerStr << " : public " << baseClass << "\n";
2008-12-02 12:01:29 +01:00
else
headerStr << "\n";
headerStr << namespaceIndent << "{\n";
if (defineQObjectMacro)
headerStr << namespaceIndent << indent << "Q_OBJECT\n";
2008-12-02 12:01:29 +01:00
headerStr << namespaceIndent << "public:\n"
<< namespaceIndent << indent;
// Constructor
if (parentQObjectClass.isEmpty()) {
headerStr << unqualifiedClassName << "();\n";
} else {
headerStr << "explicit " << unqualifiedClassName << '(' << parentQObjectClass
<< " *parent = 0);\n";
}
2009-12-01 16:17:45 +01:00
// Copy/Assignment for shared data classes.
if (params.classType == Utils::NewClassWidget::SharedDataClass) {
headerStr << namespaceIndent << indent
2009-12-01 16:25:27 +01:00
<< unqualifiedClassName << "(const " << unqualifiedClassName << " &);\n"
2009-12-01 16:17:45 +01:00
<< namespaceIndent << indent
2009-12-01 16:25:27 +01:00
<< unqualifiedClassName << " &operator=(const " << unqualifiedClassName << " &);\n"
2009-12-01 16:17:45 +01:00
<< namespaceIndent << indent
<< '~' << unqualifiedClassName << "();\n";
}
if (defineQObjectMacro)
headerStr << '\n' << namespaceIndent << "signals:\n\n" << namespaceIndent << "public slots:\n\n";
2009-12-01 16:17:45 +01:00
if (params.classType == Utils::NewClassWidget::SharedDataClass) {
headerStr << '\n' << namespaceIndent << "private:\n"
<< namespaceIndent << indent << "QSharedDataPointer<" << sharedDataClass << "> data;\n";
}
headerStr << namespaceIndent << "};\n";
2008-12-02 12:01:29 +01:00
2009-10-05 11:06:05 +02:00
Utils::writeClosingNameSpaces(namespaceList, QString(), headerStr);
headerStr << '\n';
2008-12-02 12:01:29 +01:00
headerStr << "#endif // "<< guard << '\n';
// == Source file ==
QTextStream sourceStr(source);
sourceStr << sourceLicense;
2009-10-05 11:06:05 +02:00
Utils::writeIncludeFileDirective(params.headerFile, false, sourceStr);
2009-12-01 16:17:45 +01:00
if (params.classType == Utils::NewClassWidget::SharedDataClass)
Utils::writeIncludeFileDirective(QLatin1String("QSharedData"), true, sourceStr);
2009-10-05 11:06:05 +02:00
Utils::writeOpeningNameSpaces(namespaceList, QString(), sourceStr);
2009-12-01 16:17:45 +01:00
// Private class:
if (params.classType == Utils::NewClassWidget::SharedDataClass) {
sourceStr << '\n' << namespaceIndent << "class " << sharedDataClass
<< " : public QSharedData {\n"
<< namespaceIndent << "public:\n"
<< namespaceIndent << "};\n";
}
2008-12-02 12:01:29 +01:00
// Constructor
2009-12-01 16:17:45 +01:00
sourceStr << '\n' << namespaceIndent;
if (parentQObjectClass.isEmpty()) {
2009-12-01 16:17:45 +01:00
sourceStr << unqualifiedClassName << "::" << unqualifiedClassName << "()";
if (params.classType == Utils::NewClassWidget::SharedDataClass)
sourceStr << " : data(new " << sharedDataClass << ')';
sourceStr << '\n';
} else {
sourceStr << unqualifiedClassName << "::" << unqualifiedClassName
<< '(' << parentQObjectClass << " *parent) :\n"
<< namespaceIndent << indent << baseClass << "(parent)\n";
}
2008-12-02 12:01:29 +01:00
sourceStr << namespaceIndent << "{\n" << namespaceIndent << "}\n";
2009-12-01 16:17:45 +01:00
if (params.classType == Utils::NewClassWidget::SharedDataClass) {
// Copy
sourceStr << '\n' << namespaceIndent << unqualifiedClassName << "::" << unqualifiedClassName << "(const "
<< unqualifiedClassName << " &rhs) : data(rhs.data)\n"
<< namespaceIndent << "{\n" << namespaceIndent << "}\n\n";
// Assignment
sourceStr << namespaceIndent << unqualifiedClassName << " &"
<< unqualifiedClassName << "::operator=(const " << unqualifiedClassName << " &rhs)\n"
<< namespaceIndent << "{\n"
<< namespaceIndent << indent << "if (this != &rhs)\n"
<< namespaceIndent << indent << indent << "data.operator=(rhs.data);\n"
<< namespaceIndent << indent << "return *this;\n"
<< namespaceIndent << "}\n\n";
// Destructor
sourceStr << namespaceIndent << unqualifiedClassName << "::~"
<< unqualifiedClassName << "()\n"
<< namespaceIndent << "{\n"
<< namespaceIndent << "}\n";
}
2009-10-05 11:06:05 +02:00
Utils::writeClosingNameSpaces(namespaceList, QString(), sourceStr);
2008-12-02 12:01:29 +01:00
return true;
}