2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2008-12-02 12:01:29 +01:00
|
|
|
**
|
2015-01-14 18:07:15 +01:00
|
|
|
** Copyright (C) 2015 The Qt Company Ltd.
|
|
|
|
|
** Contact: http://www.qt.io/licensing
|
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
|
2015-01-14 18:07:15 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms and
|
|
|
|
|
** conditions see http://www.qt.io/terms-conditions. For further information
|
2014-10-01 13:21:18 +02:00
|
|
|
** use the contact form at http://www.qt.io/contact-us.
|
2008-12-02 14:17:16 +01:00
|
|
|
**
|
2009-02-25 09:15:00 +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
|
2014-10-01 13:21:18 +02:00
|
|
|
** 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.
|
2012-10-02 09:12:39 +02:00
|
|
|
**
|
2015-01-14 18:07:15 +01:00
|
|
|
** In addition, as a special exception, The Qt Company gives you certain additional
|
|
|
|
|
** rights. These rights are described in The Qt Company 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 16:19:05 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "guiappwizard.h"
|
2010-02-17 14:26:42 +01:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
#include "guiappwizarddialog.h"
|
|
|
|
|
|
2011-11-18 12:54:41 +01:00
|
|
|
#include <projectexplorer/projectexplorerconstants.h>
|
2010-12-03 13:49:35 +01:00
|
|
|
#include <cpptools/abstracteditorsupport.h>
|
2009-07-01 16:27:40 +02:00
|
|
|
#include <designer/cpp/formclasswizardparameters.h>
|
2011-01-10 16:29:06 +01:00
|
|
|
#include <extensionsystem/pluginmanager.h>
|
|
|
|
|
#include <extensionsystem/invoker.h>
|
2012-01-18 13:50:14 +01:00
|
|
|
#include <qtsupport/qtsupportconstants.h>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2011-03-30 15:15:15 +02:00
|
|
|
#include <utils/fileutils.h>
|
|
|
|
|
|
2013-09-20 15:12:44 +02:00
|
|
|
#include <QCoreApplication>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QDir>
|
|
|
|
|
#include <QTextStream>
|
|
|
|
|
#include <QSharedPointer>
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2011-05-10 15:19:38 +02:00
|
|
|
static const char mainSourceFileC[] = "main";
|
|
|
|
|
static const char mainSourceShowC[] = " w.show();\n";
|
2012-08-22 13:27:25 +02:00
|
|
|
static const char mainSourceMobilityShowC[] = " w.show();\n";
|
2010-04-08 14:56:56 +02:00
|
|
|
|
2011-05-10 15:19:38 +02:00
|
|
|
static const char mainWindowUiContentsC[] =
|
2008-12-02 12:01:29 +01:00
|
|
|
"\n <widget class=\"QMenuBar\" name=\"menuBar\" />"
|
|
|
|
|
"\n <widget class=\"QToolBar\" name=\"mainToolBar\" />"
|
|
|
|
|
"\n <widget class=\"QWidget\" name=\"centralWidget\" />"
|
|
|
|
|
"\n <widget class=\"QStatusBar\" name=\"statusBar\" />";
|
2011-05-10 15:19:38 +02:00
|
|
|
static const char mainWindowMobileUiContentsC[] =
|
2010-03-24 15:07:17 +01:00
|
|
|
"\n <widget class=\"QWidget\" name=\"centralWidget\" />";
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
static const char *baseClassesC[] = { "QMainWindow", "QWidget", "QDialog" };
|
|
|
|
|
|
|
|
|
|
static inline QStringList baseClasses()
|
|
|
|
|
{
|
|
|
|
|
QStringList rc;
|
|
|
|
|
const int baseClassCount = sizeof(baseClassesC)/sizeof(const char *);
|
|
|
|
|
for (int i = 0; i < baseClassCount; i++)
|
|
|
|
|
rc.push_back(QLatin1String(baseClassesC[i]));
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-16 11:02:37 +02:00
|
|
|
namespace QmakeProjectManager {
|
2008-12-02 12:01:29 +01:00
|
|
|
namespace Internal {
|
|
|
|
|
|
2013-11-04 13:56:41 +01:00
|
|
|
GuiAppWizard::GuiAppWizard()
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2015-05-18 17:45:04 +02:00
|
|
|
setId("C.Qt4Gui");
|
2013-09-20 15:12:44 +02:00
|
|
|
setCategory(QLatin1String(ProjectExplorer::Constants::QT_APPLICATION_WIZARD_CATEGORY));
|
|
|
|
|
setDisplayCategory(QCoreApplication::translate("ProjectExplorer",
|
|
|
|
|
ProjectExplorer::Constants::QT_APPLICATION_WIZARD_CATEGORY_DISPLAY));
|
2013-10-22 12:11:47 +02:00
|
|
|
setDisplayName(tr("Qt Widgets Application"));
|
2013-09-20 15:12:44 +02:00
|
|
|
setDescription(tr("Creates a Qt application for the desktop. "
|
|
|
|
|
"Includes a Qt Designer-based main window.\n\n"
|
|
|
|
|
"Preselects a desktop Qt for building the application if available."));
|
|
|
|
|
setIcon(QIcon(QLatin1String(":/wizards/images/gui.png")));
|
|
|
|
|
setRequiredFeatures(Core::Feature(QtSupport::Constants::FEATURE_QWIDGETS));
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
|
2014-05-27 13:18:05 +02:00
|
|
|
Core::BaseFileWizard *GuiAppWizard::create(QWidget *parent, const Core::WizardDialogParameters ¶meters) const
|
2008-12-02 12:01:29 +01:00
|
|
|
{
|
2015-05-29 17:25:40 +02:00
|
|
|
GuiAppWizardDialog *dialog = new GuiAppWizardDialog(this, displayName(), icon(), parent, parameters);
|
2014-05-27 12:23:38 +02:00
|
|
|
dialog->setProjectName(GuiAppWizardDialog::uniqueProjectName(parameters.defaultPath()));
|
2008-12-02 12:01:29 +01:00
|
|
|
// Order! suffixes first to generate files correctly
|
2009-06-17 12:40:54 +02:00
|
|
|
dialog->setLowerCaseFiles(QtWizard::lowerCaseFiles());
|
2008-12-02 12:01:29 +01:00
|
|
|
dialog->setSuffixes(headerSuffix(), sourceSuffix(), formSuffix());
|
2010-01-29 21:33:57 +01:00
|
|
|
dialog->setBaseClasses(baseClasses());
|
2008-12-02 12:01:29 +01:00
|
|
|
return dialog;
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-01 16:27:40 +02:00
|
|
|
// Use the class generation utils provided by the designer plugin
|
|
|
|
|
static inline bool generateFormClass(const GuiAppParameters ¶ms,
|
|
|
|
|
const Core::GeneratedFile &uiFile,
|
|
|
|
|
Core::GeneratedFile *formSource,
|
|
|
|
|
Core::GeneratedFile *formHeader,
|
|
|
|
|
QString *errorMessage)
|
|
|
|
|
{
|
|
|
|
|
// Retrieve parameters from settings
|
|
|
|
|
Designer::FormClassWizardParameters fp;
|
2011-01-10 16:29:06 +01:00
|
|
|
fp.uiTemplate = uiFile.contents();
|
|
|
|
|
fp.uiFile = uiFile.path();
|
|
|
|
|
fp.className = params.className;
|
|
|
|
|
fp.sourceFile = params.sourceFileName;
|
|
|
|
|
fp.headerFile = params.headerFileName;
|
2009-07-01 16:27:40 +02:00
|
|
|
QString headerContents;
|
|
|
|
|
QString sourceContents;
|
2011-01-10 16:29:06 +01:00
|
|
|
// Invoke code generation service of Qt Designer plugin.
|
2012-06-18 11:34:15 +02:00
|
|
|
if (QObject *codeGenerator = ExtensionSystem::PluginManager::getObjectByClassName(QLatin1String("Designer::QtDesignerFormClassCodeGenerator"))) {
|
2011-01-10 16:29:06 +01:00
|
|
|
const QVariant code = ExtensionSystem::invoke<QVariant>(codeGenerator, "generateFormClassCode", fp);
|
|
|
|
|
if (code.type() == QVariant::List) {
|
|
|
|
|
const QVariantList vl = code.toList();
|
|
|
|
|
if (vl.size() == 2) {
|
|
|
|
|
headerContents = vl.front().toString();
|
|
|
|
|
sourceContents = vl.back().toString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (headerContents.isEmpty() || sourceContents.isEmpty()) {
|
2012-09-21 13:54:38 +02:00
|
|
|
*errorMessage = QString::fromLatin1("Failed to obtain Designer plugin code generation service.");
|
2009-07-01 16:27:40 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
2011-01-10 16:29:06 +01:00
|
|
|
|
2009-07-01 16:27:40 +02:00
|
|
|
formHeader->setContents(headerContents);
|
|
|
|
|
formSource->setContents(sourceContents);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
Core::GeneratedFiles GuiAppWizard::generateFiles(const QWizard *w,
|
|
|
|
|
QString *errorMessage) const
|
|
|
|
|
{
|
|
|
|
|
const GuiAppWizardDialog *dialog = qobject_cast<const GuiAppWizardDialog *>(w);
|
|
|
|
|
const QtProjectParameters projectParams = dialog->projectParameters();
|
|
|
|
|
const QString projectPath = projectParams.projectPath();
|
|
|
|
|
const GuiAppParameters params = dialog->parameters();
|
|
|
|
|
|
|
|
|
|
// Generate file names. Note that the path for the project files is the
|
|
|
|
|
// newly generated project directory.
|
|
|
|
|
const QString templatePath = templateDir();
|
|
|
|
|
// Create files: main source
|
|
|
|
|
QString contents;
|
|
|
|
|
const QString mainSourceFileName = buildFileName(projectPath, QLatin1String(mainSourceFileC), sourceSuffix());
|
|
|
|
|
Core::GeneratedFile mainSource(mainSourceFileName);
|
|
|
|
|
if (!parametrizeTemplate(templatePath, QLatin1String("main.cpp"), params, &contents, errorMessage))
|
|
|
|
|
return Core::GeneratedFiles();
|
2010-08-09 12:23:21 +02:00
|
|
|
mainSource.setContents(CppTools::AbstractEditorSupport::licenseTemplate(mainSourceFileName)
|
|
|
|
|
+ contents);
|
2009-07-01 16:27:40 +02:00
|
|
|
// Create files: form source with or without form
|
2008-12-02 12:01:29 +01:00
|
|
|
const QString formSourceFileName = buildFileName(projectPath, params.sourceFileName, sourceSuffix());
|
|
|
|
|
const QString formHeaderName = buildFileName(projectPath, params.headerFileName, headerSuffix());
|
2009-07-01 16:27:40 +02:00
|
|
|
Core::GeneratedFile formSource(formSourceFileName);
|
2008-12-02 12:01:29 +01:00
|
|
|
Core::GeneratedFile formHeader(formHeaderName);
|
2010-09-30 15:17:51 +02:00
|
|
|
formSource.setAttributes(Core::GeneratedFile::OpenEditorAttribute);
|
2009-07-01 16:27:40 +02:00
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
QSharedPointer<Core::GeneratedFile> form;
|
|
|
|
|
if (params.designerForm) {
|
2009-07-01 16:27:40 +02:00
|
|
|
// Create files: form
|
2008-12-02 12:01:29 +01:00
|
|
|
const QString formName = buildFileName(projectPath, params.formFileName, formSuffix());
|
|
|
|
|
form = QSharedPointer<Core::GeneratedFile>(new Core::GeneratedFile(formName));
|
|
|
|
|
if (!parametrizeTemplate(templatePath, QLatin1String("widget.ui"), params, &contents, errorMessage))
|
|
|
|
|
return Core::GeneratedFiles();
|
2010-01-29 21:33:57 +01:00
|
|
|
form->setContents(contents);
|
2009-07-01 16:27:40 +02:00
|
|
|
if (!generateFormClass(params, *form, &formSource, &formHeader, errorMessage))
|
|
|
|
|
return Core::GeneratedFiles();
|
|
|
|
|
} else {
|
|
|
|
|
const QString formSourceTemplate = QLatin1String("mywidget.cpp");
|
|
|
|
|
if (!parametrizeTemplate(templatePath, formSourceTemplate, params, &contents, errorMessage))
|
|
|
|
|
return Core::GeneratedFiles();
|
2010-08-09 12:23:21 +02:00
|
|
|
formSource.setContents(CppTools::AbstractEditorSupport::licenseTemplate(formSourceFileName)
|
|
|
|
|
+ contents);
|
2009-07-01 16:27:40 +02:00
|
|
|
// Create files: form header
|
|
|
|
|
const QString formHeaderTemplate = QLatin1String("mywidget.h");
|
|
|
|
|
if (!parametrizeTemplate(templatePath, formHeaderTemplate, params, &contents, errorMessage))
|
|
|
|
|
return Core::GeneratedFiles();
|
2010-08-09 12:23:21 +02:00
|
|
|
formHeader.setContents(CppTools::AbstractEditorSupport::licenseTemplate(formHeaderName)
|
|
|
|
|
+ contents);
|
2008-12-02 12:01:29 +01:00
|
|
|
}
|
|
|
|
|
// Create files: profile
|
2010-01-07 18:17:24 +01:00
|
|
|
const QString profileName = buildFileName(projectPath, projectParams.fileName, profileSuffix());
|
2008-12-02 12:01:29 +01:00
|
|
|
Core::GeneratedFile profile(profileName);
|
2010-04-16 15:55:32 +02:00
|
|
|
profile.setAttributes(Core::GeneratedFile::OpenProjectAttribute);
|
2008-12-02 12:01:29 +01:00
|
|
|
contents.clear();
|
|
|
|
|
{
|
|
|
|
|
QTextStream proStr(&contents);
|
|
|
|
|
QtProjectParameters::writeProFileHeader(proStr);
|
|
|
|
|
projectParams.writeProFile(proStr);
|
2015-01-10 23:40:32 +02:00
|
|
|
proStr << "\n\nSOURCES += " << Utils::FileName::fromString(mainSourceFileName).fileName()
|
|
|
|
|
<< "\\\n " << Utils::FileName::fromString(formSource.path()).fileName()
|
|
|
|
|
<< "\n\nHEADERS += " << Utils::FileName::fromString(formHeader.path()).fileName();
|
2008-12-02 12:01:29 +01:00
|
|
|
if (params.designerForm)
|
2015-01-10 23:40:32 +02:00
|
|
|
proStr << "\n\nFORMS += " << Utils::FileName::fromString(form->path()).fileName();
|
2010-03-26 14:44:25 +01:00
|
|
|
if (params.isMobileApplication) {
|
|
|
|
|
proStr << "\n\nCONFIG += mobility"
|
|
|
|
|
<< "\nMOBILITY = "
|
2012-08-22 13:27:25 +02:00
|
|
|
<< "\n";
|
2010-03-26 14:44:25 +01:00
|
|
|
}
|
2008-12-02 12:01:29 +01:00
|
|
|
proStr << '\n';
|
|
|
|
|
}
|
|
|
|
|
profile.setContents(contents);
|
|
|
|
|
// List
|
|
|
|
|
Core::GeneratedFiles rc;
|
|
|
|
|
rc << mainSource << formSource << formHeader;
|
|
|
|
|
if (params.designerForm)
|
|
|
|
|
rc << *form;
|
|
|
|
|
rc << profile;
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool GuiAppWizard::parametrizeTemplate(const QString &templatePath, const QString &templateName,
|
|
|
|
|
const GuiAppParameters ¶ms,
|
|
|
|
|
QString *target, QString *errorMessage)
|
|
|
|
|
{
|
2015-01-31 22:11:59 +02:00
|
|
|
const QString fileName = templatePath + QLatin1Char('/') + templateName;
|
2011-03-30 15:15:15 +02:00
|
|
|
Utils::FileReader reader;
|
|
|
|
|
if (!reader.fetch(fileName, QIODevice::Text, errorMessage))
|
2008-12-02 12:01:29 +01:00
|
|
|
return false;
|
2011-03-30 15:15:15 +02:00
|
|
|
QString contents = QString::fromUtf8(reader.data());
|
2008-12-02 12:01:29 +01:00
|
|
|
|
2011-11-18 20:08:44 +01:00
|
|
|
contents.replace(QLatin1String("%QAPP_INCLUDE%"), QLatin1String("QApplication"));
|
2008-12-02 12:01:29 +01:00
|
|
|
contents.replace(QLatin1String("%INCLUDE%"), params.headerFileName);
|
|
|
|
|
contents.replace(QLatin1String("%CLASS%"), params.className);
|
|
|
|
|
contents.replace(QLatin1String("%BASECLASS%"), params.baseClassName);
|
2010-02-18 15:23:24 +01:00
|
|
|
contents.replace(QLatin1String("%WIDGET_HEIGHT%"), QString::number(params.widgetHeight));
|
|
|
|
|
contents.replace(QLatin1String("%WIDGET_WIDTH%"), QString::number(params.widgetWidth));
|
2010-04-08 14:56:56 +02:00
|
|
|
if (params.isMobileApplication)
|
|
|
|
|
contents.replace(QLatin1String("%SHOWMETHOD%"), QString::fromLatin1(mainSourceMobilityShowC));
|
|
|
|
|
else
|
|
|
|
|
contents.replace(QLatin1String("%SHOWMETHOD%"), QString::fromLatin1(mainSourceShowC));
|
|
|
|
|
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
const QChar dot = QLatin1Char('.');
|
|
|
|
|
|
|
|
|
|
QString preDef = params.headerFileName.toUpper();
|
|
|
|
|
preDef.replace(dot, QLatin1Char('_'));
|
2012-01-13 14:20:45 +01:00
|
|
|
contents.replace(QLatin1String("%PRE_DEF%"), preDef);
|
2008-12-02 12:01:29 +01:00
|
|
|
|
|
|
|
|
const QString uiFileName = params.formFileName;
|
|
|
|
|
QString uiHdr = QLatin1String("ui_");
|
|
|
|
|
uiHdr += uiFileName.left(uiFileName.indexOf(dot));
|
|
|
|
|
uiHdr += QLatin1String(".h");
|
|
|
|
|
|
|
|
|
|
contents.replace(QLatin1String("%UI_HDR%"), uiHdr);
|
|
|
|
|
if (params.baseClassName == QLatin1String("QMainWindow")) {
|
2010-03-24 15:07:17 +01:00
|
|
|
if (params.isMobileApplication)
|
|
|
|
|
contents.replace(QLatin1String("%CENTRAL_WIDGET%"), QLatin1String(mainWindowMobileUiContentsC));
|
|
|
|
|
else
|
|
|
|
|
contents.replace(QLatin1String("%CENTRAL_WIDGET%"), QLatin1String(mainWindowUiContentsC));
|
2008-12-02 12:01:29 +01:00
|
|
|
} else {
|
|
|
|
|
contents.remove(QLatin1String("%CENTRAL_WIDGET%"));
|
|
|
|
|
}
|
|
|
|
|
*target = contents;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2008-12-02 16:19:05 +01:00
|
|
|
|
|
|
|
|
} // namespace Internal
|
2013-10-16 11:02:37 +02:00
|
|
|
} // namespace QmakeProjectManager
|