From 293cc57064d6c8dcfde5db0af24b0870f50898f6 Mon Sep 17 00:00:00 2001 From: Tobias Hunger Date: Mon, 30 Nov 2015 16:02:29 +0100 Subject: [PATCH] Modeleditor: Use a JSON-based wizard Use a JSON wizard in favor of implementing the same thing in code. Change-Id: I5952bf417255fb75378e42e3e80b13c0a0d3d317 Reviewed-by: Jochen Becher Reviewed-by: Tobias Hunger --- .../wizards/files/modeling/file.qmodel | 55 ++++++++ .../wizards/files/modeling/wizard.json | 67 +++++++++ src/plugins/modeleditor/modeleditor.pro | 2 - src/plugins/modeleditor/modeleditor.qbs | 2 - .../modeleditor/modeleditor_file_wizard.cpp | 128 ------------------ .../modeleditor/modeleditor_file_wizard.h | 60 -------- .../modeleditor/modeleditor_plugin.cpp | 5 - 7 files changed, 122 insertions(+), 197 deletions(-) create mode 100644 share/qtcreator/templates/wizards/files/modeling/file.qmodel create mode 100644 share/qtcreator/templates/wizards/files/modeling/wizard.json delete mode 100644 src/plugins/modeleditor/modeleditor_file_wizard.cpp delete mode 100644 src/plugins/modeleditor/modeleditor_file_wizard.h diff --git a/share/qtcreator/templates/wizards/files/modeling/file.qmodel b/share/qtcreator/templates/wizards/files/modeling/file.qmodel new file mode 100644 index 00000000000..5a4645df0a4 --- /dev/null +++ b/share/qtcreator/templates/wizards/files/modeling/file.qmodel @@ -0,0 +1,55 @@ + + + + %{UUID1} + + + + + + + + %{UUID2} + + + %{Name} + + + + + + + %{UUID3} + + + + + + + + + + %{UUID3} + + + %{Name} + + + + + + + + + + + + + + + + + + + + diff --git a/share/qtcreator/templates/wizards/files/modeling/wizard.json b/share/qtcreator/templates/wizards/files/modeling/wizard.json new file mode 100644 index 00000000000..6a2f5d07ebf --- /dev/null +++ b/share/qtcreator/templates/wizards/files/modeling/wizard.json @@ -0,0 +1,67 @@ +{ + "version": 1, + "kind": "file", + "id": "SA.Model", + "category": "O.Model", + "trDescription": "Creates a new empty model with an empty diagram.", + "trDisplayName": "Model", + "trDisplayCategory": "Modeling", + "platformIndependent": true, + "icon": "../../global/genericfilewizard.png", + "enabled": "%{JS: [ %{Plugins} ].indexOf('ModelEditor') >= 0}", + + "options": + [ + { "key": "TargetPath", "value": "%{JS: Util.fileName('%{Location}/%{FileName}', '%{JS: Util.preferredSuffix('text/vnd.qtcreator.model')}')}" }, + { "key": "FileName", "value": "%{JS: Modeling.elementNameToFileName('%{Name}')}" } + ], + + "pages" : + [ + { + "trDisplayName": "Model Name and Location", + "trShortTitle": "Location", + "typeId": "Fields", + "data": + [ + { + "name": "Name", + "trDisplayName": "Model name:", + "mandatory": true, + "type": "LineEdit" + }, + { + "name": "Location", + "trDisplayName": "Location:", + "type": "PathChooser", + "isComplete": "%{JS: '%{Location}' === '' || !Util.exists('%{TargetPath}')}", + "trIncompleteMessage": "\"%{JS: Util.toNativeSeparators('%{TargetPath}')}\" exists in the filesystem.", + "data": + { + "kind": "existingDirectory", + "basePath": "%{InitialPath}", + "path": "%{InitialPath}" + } + } + ] + } + ], + "generators" : + [ + { + "typeId": "File", + "data": + { + "source": "file.qmodel", + "target": "%{TargetPath}", + "openInEditor": true, + "options": + [ + { "key": "UUID1", "value": "%{UUID}", "evaluate": true }, + { "key": "UUID2", "value": "%{UUID}", "evaluate": true }, + { "key": "UUID3", "value": "%{UUID}", "evaluate": true } + ] + } + } + ] +} diff --git a/src/plugins/modeleditor/modeleditor.pro b/src/plugins/modeleditor/modeleditor.pro index 74f9a08ce8f..35060b048e6 100644 --- a/src/plugins/modeleditor/modeleditor.pro +++ b/src/plugins/modeleditor/modeleditor.pro @@ -39,7 +39,6 @@ SOURCES += \ modeldocument.cpp \ modeleditor.cpp \ modeleditorfactory.cpp \ - modeleditor_file_wizard.cpp \ modeleditor_plugin.cpp \ modelindexer.cpp \ modelsmanager.cpp \ @@ -62,7 +61,6 @@ HEADERS += \ modeldocument.h \ modeleditor_constants.h \ modeleditorfactory.h \ - modeleditor_file_wizard.h \ modeleditor_global.h \ modeleditor.h \ modeleditor_plugin.h \ diff --git a/src/plugins/modeleditor/modeleditor.qbs b/src/plugins/modeleditor/modeleditor.qbs index 4da4a5e4ec4..48a74d6d790 100644 --- a/src/plugins/modeleditor/modeleditor.qbs +++ b/src/plugins/modeleditor/modeleditor.qbs @@ -43,8 +43,6 @@ QtcPlugin { "modeleditor.cpp", "modeleditorfactory.cpp", "modeleditorfactory.h", - "modeleditor_file_wizard.cpp", - "modeleditor_file_wizard.h", "modeleditor_global.h", "modeleditor.h", "modeleditor_plugin.cpp", diff --git a/src/plugins/modeleditor/modeleditor_file_wizard.cpp b/src/plugins/modeleditor/modeleditor_file_wizard.cpp deleted file mode 100644 index 1ca54d40d5e..00000000000 --- a/src/plugins/modeleditor/modeleditor_file_wizard.cpp +++ /dev/null @@ -1,128 +0,0 @@ -/*************************************************************************** -** -** Copyright (C) 2015 Jochen Becher -** Contact: http://www.qt.io/licensing -** -** 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 The Qt Company. For licensing terms and -** conditions see http://www.qt.io/terms-conditions. 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, The Qt Company gives you certain additional -** rights. These rights are described in The Qt Company LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -****************************************************************************/ - -#include "modeleditor_file_wizard.h" - -#include "modeleditor_constants.h" - -#include "qmt/controller/namecontroller.h" -#include "qmt/model/mcanvasdiagram.h" -#include "qmt/model/mpackage.h" -#include "qmt/serializer/projectserializer.h" -#include "qmt/project/project.h" - -#include -#include -#include - -#include - -namespace ModelEditor { -namespace Internal { - -FileWizardFactory::FileWizardFactory() - : Core::BaseFileWizardFactory() -{ - setWizardKind(Core::IWizardFactory::FileWizard); - setCategory(QLatin1String(Constants::WIZARD_CATEGORY)); - QString trCategory = QCoreApplication::translate(Constants::WIZARD_CATEGORY, - Constants::WIZARD_TR_CATEGORY); - setDisplayCategory(trCategory); - setDisplayName(tr("Model")); - setId(Constants::WIZARD_MODEL_ID); - setDescription(tr("Creates an empty model")); - setFlags(Core::IWizardFactory::PlatformIndependent); -} - -Core::BaseFileWizard *FileWizardFactory::create( - QWidget *parent, const Core::WizardDialogParameters ¶meters) const -{ - auto wizard = new Core::BaseFileWizard(this, QVariantMap(), parent); - wizard->setWindowTitle(tr("New %1").arg(displayName())); - - auto page = new Utils::FileWizardPage; - if (parameters.flags().testFlag(Core::WizardDialogParameters::ForceCapitalLetterForFileName)) - page->setForceFirstCapitalLetterForFileName(true); - page->setTitle(tr("Model Name and Location")); - page->setFileNameLabel(tr("Model name:")); - page->setPathLabel(tr("Location:")); - page->setPath(parameters.defaultPath()); - wizard->addPage(page); - - foreach (QWizardPage *p, wizard->extensionPages()) - wizard->addPage(p); - return wizard; -} - -Core::GeneratedFiles FileWizardFactory::generateFiles(const QWizard *w, - QString *errorMessage) const -{ - const Utils::Wizard *wizard = qobject_cast(w); - Utils::FileWizardPage *page = wizard->find(); - QTC_ASSERT(page, return Core::GeneratedFiles()); - - return generateFilesFromPath(page->path(), page->fileName(), errorMessage); -} - -Core::GeneratedFiles FileWizardFactory::generateFilesFromPath(const QString &path, - const QString &name, - QString *errorMessage) const -{ - Q_UNUSED(errorMessage); - - Core::GeneratedFiles files; - - const QString suffix = preferredSuffix(QLatin1String(Constants::MIME_TYPE_MODEL)); - const QString fileName = Core::BaseFileWizardFactory::buildFileName(path, name, suffix); - Core::GeneratedFile file(fileName); - - auto rootPackage = new qmt::MPackage(); - rootPackage->setName(qmt::NameController::convertFileNameToElementName(fileName)); - - auto rootDiagram = new qmt::MCanvasDiagram(); - rootDiagram->setName(qmt::NameController::convertFileNameToElementName(fileName)); - rootPackage->addChild(rootDiagram); - - qmt::Project project; - project.setRootPackage(rootPackage); - project.setFileName(fileName); - - qmt::ProjectSerializer serializer; - QByteArray contents = serializer.save(&project); - - file.setContents(QString::fromUtf8(contents)); - file.setAttributes(Core::GeneratedFile::OpenEditorAttribute); - - files << file; - return files; -} - -} // namespace Internal -} // namespace ModelEditor diff --git a/src/plugins/modeleditor/modeleditor_file_wizard.h b/src/plugins/modeleditor/modeleditor_file_wizard.h deleted file mode 100644 index f713d935c43..00000000000 --- a/src/plugins/modeleditor/modeleditor_file_wizard.h +++ /dev/null @@ -1,60 +0,0 @@ -/*************************************************************************** -** -** Copyright (C) 2015 Jochen Becher -** Contact: http://www.qt.io/licensing -** -** 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 The Qt Company. For licensing terms and -** conditions see http://www.qt.io/terms-conditions. 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, The Qt Company gives you certain additional -** rights. These rights are described in The Qt Company LGPL Exception -** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -** -****************************************************************************/ - -#ifndef MODELEDITOR_FILE_WIZARD_H -#define MODELEDITOR_FILE_WIZARD_H - -#include - -namespace ModelEditor { -namespace Internal { - -class FileWizardFactory : - public Core::BaseFileWizardFactory -{ - Q_OBJECT - -public: - explicit FileWizardFactory(); - -protected: - Core::BaseFileWizard *create(QWidget *parent, - const Core::WizardDialogParameters ¶meters) const override; - Core::GeneratedFiles generateFiles(const QWizard *w, QString *errorMessage) const override; - -private: - Core::GeneratedFiles generateFilesFromPath(const QString &path, const QString &name, - QString *errorMessage) const; -}; - -} // namespace Internal -} // namespace ModelEditor - -#endif // MODELEDITOR_FILE_WIZARD_H diff --git a/src/plugins/modeleditor/modeleditor_plugin.cpp b/src/plugins/modeleditor/modeleditor_plugin.cpp index d676b29f4b4..1ce05c0043e 100644 --- a/src/plugins/modeleditor/modeleditor_plugin.cpp +++ b/src/plugins/modeleditor/modeleditor_plugin.cpp @@ -34,7 +34,6 @@ #include "modelsmanager.h" #include "settingscontroller.h" #include "modeleditor_constants.h" -#include "modeleditor_file_wizard.h" #include "uicontroller.h" #include "jsextension.h" @@ -103,10 +102,6 @@ bool ModelEditorPlugin::initialize(const QStringList &arguments, QString *errorS d->uiController = new UiController(this); addAutoReleasedObject(d->uiController); - Core::IWizardFactory::registerFactoryCreator([]() { - return QList() << new FileWizardFactory; - }); - d->modelFactory = new ModelEditorFactory(d->uiController, this); addAutoReleasedObject(d->modelFactory);