diff options
author | Sandro S. Andrade <sandroandrade@kde.org> | 2013-10-29 18:58:12 -0300 |
---|---|---|
committer | Sandro S. Andrade <sandroandrade@kde.org> | 2013-10-29 22:54:30 +0100 |
commit | 85fabc37249a27b125dbdb8e9ffa5a3024f4efd0 (patch) | |
tree | a9feacc8e8b0983fa9705e172318a71c72e3173b /examples | |
parent | 81fb3fce46700d98d99ae2a7f76fa4228e91032f (diff) |
Refactor DuSE-MT's Save, SaveAs, and New actions
Change-Id: Ic215ce4edb0a7ffc5918fe5a3367515d8162d16a
Reviewed-by: Sandro S. Andrade <sandroandrade@kde.org>
Diffstat (limited to 'examples')
7 files changed, 145 insertions, 120 deletions
diff --git a/examples/uml/duse-mt/src/app/shell/mainwindow.cpp b/examples/uml/duse-mt/src/app/shell/mainwindow.cpp index b76b3a22..46d335b3 100644 --- a/examples/uml/duse-mt/src/app/shell/mainwindow.cpp +++ b/examples/uml/duse-mt/src/app/shell/mainwindow.cpp @@ -153,91 +153,24 @@ void MainWindow::readSettings() void MainWindow::on_actionFileNewModel_triggered() { -// _newModel->lneModel->clear(); -// _newModel->cboMetamodel->clear(); -// int i = 0; -// typedef QPair<QMetaModelPlugin *, QJsonObject> PluginData; -// foreach (const PluginData &pair, _loadedPlugins.values()) { -// _newModel->cboMetamodel->addItem(pair.first->metaObject()->className()); -// ++i; -// } -// int type; -// _newModel->lneModel->setFocus(); -// if (_newModelDialog->exec() == QDialog::Accepted) { -// foreach (const PluginData &pair, _loadedPlugins.values()) { -// if (pair.first->metaObject()->className() == _newModel->cboMetamodel->currentText()) -// pair.first->initMetaModel(&_engine); -// } -// if ((type = QMetaType::type(_newModel->lstTopLevelContainers->currentItem()->text().append("*").toLatin1())) != QMetaType::UnknownType) { -// const QMetaObject *metaObject = QMetaType::metaObjectForType(type); -// if (metaObject) { -// QWrappedObject *topLevelElement = dynamic_cast<QWrappedObject *>(metaObject->newInstance()); -// if (topLevelElement) { -// topLevelElement->setObjectName(_newModel->lneModel->text()); -// _modelingObjectModel->clear(); -// _modelingObjectModel->addWrappedObject(topLevelElement); -// setWindowTitle("DuSE-MT"); -// ui->txeJavaScript->setText("self"); -// QTimer::singleShot(0, this, SLOT(on_psbJSEvaluate_clicked())); -// } -// } -// } -// } -} - -void MainWindow::saveXmi(QList<QModelingObject *> modelObjects) -{ - QFile file(_currentFileName); - if (!file.open(QFile::WriteOnly | QFile::Text)) { - QMessageBox::critical(this, tr("Save As"), tr("Cannot write file !")); - return; - } - - QXmiWriter writer; - setCursor(Qt::WaitCursor); - if (!writer.writeFile(modelObjects, &file)) - QMessageBox::critical(this, tr("Save As"), tr("Error when writing XMI file !")); - else { - statusBar()->showMessage("XMI file successfully saved !", 3000); - setWindowTitle(QFileInfo(file).fileName() + " - DuSE-MT"); - } - setCursor(Qt::ArrowCursor); -} - -QList<QModelingElement *> MainWindow::loadXmi(QString fileName) -{ - QFile file(fileName); - if (!file.open(QFile::ReadOnly | QFile::Text)) { - QMessageBox::critical(this, tr("Open"), tr("Cannot read file !")); - return QList<QModelingElement *>(); + _newModel->lneModel->clear(); + _newModel->cboMetamodel->clear(); + int i = 0; + typedef QPair<QMetaModelPlugin *, QJsonObject> PluginData; + foreach (const PluginData &pair, _metamodelPlugins.values()) { + _newModel->cboMetamodel->addItem(pair.first->metaObject()->className()); + ++i; } - - QXmiReader reader; - if (fileName.contains("duse-mt")) - setWindowTitle(QFileInfo(file).fileName() + " - DuSE-MT"); - QList<QModelingElement *> modelingObjectList = reader.readFile(&file); - - setModelInspector(modelingObjectList); - - return modelingObjectList; -} - -void MainWindow::setModelInspector(QList<QModelingElement *> modelingObjectList) -{ - if (!modelingObjectList.isEmpty()) { -// _engine.globalObject().setProperty(modelingObjectList.at(0)->asQModelingObject()->objectName(), _engine.newQObject(modelingObjectList.at(0)->asQModelingObject())); - -// QScriptValue array = _engine.newArray(); -// foreach (QModelingElement *modelingObject, modelingObjectList) -// array.property(QString::fromLatin1("push")).call(array, QScriptValueList() << _engine.newQObject(modelingObject->asQModelingObject())); -// _engine.globalObject().setProperty("input", array); - -// ui->txeJavaScript->setText("self"); - QTimer::singleShot(0, this, SLOT(on_psbJSEvaluate_clicked())); + _newModel->lneModel->setFocus(); + if (_newModelDialog->exec() == QDialog::Accepted) { + QMetaModelPlugin *metamodelPlugin; + foreach (const PluginData &pair, _metamodelPlugins.values()) { + if (pair.first->metaObject()->className() == _newModel->cboMetamodel->currentText()) + metamodelPlugin = pair.first; + } + if (ICore::self()->projectController()->createModel(_newModel->lneModel->text(), metamodelPlugin, _newModel->lstTopLevelContainers->currentItem()->text())) + setWindowTitle(ICore::self()->projectController()->currentModelFileName() + " - DuSE-MT"); } -// _modelingObjectModel->clear(); -// foreach (QModelingElement *object, modelingObjectList) -// _modelingObjectModel->addModelingObject(object->asQModelingObject()); } void MainWindow::on_actionFileOpenModel_triggered() @@ -248,7 +181,7 @@ void MainWindow::on_actionFileOpenModel_triggered() bool ret = ICore::self()->projectController()->openModel(fileName); setCursor(Qt::ArrowCursor); if (!ret) { - QMessageBox::critical(this, tr("Open Model"), ICore::self()->projectController()->errorStrings().first()); + QMessageBox::critical(this, tr("Open model"), ICore::self()->projectController()->errorStrings().first()); return; } setWindowTitle(QFileInfo(QFile(fileName)).fileName() + " - DuSE-MT"); @@ -287,10 +220,10 @@ void MainWindow::on_actionFileNewDuseDesign_triggered() return; } - _currentFileName = _newDuseDesign->_inputModelFileName; +// _currentFileName = _newDuseDesign->_inputModelFileName; // foreach (QWrappedObject *object, _inputModel) // delete object; - _inputModel = loadXmi(_currentFileName); +// _inputModel = loadXmi(_currentFileName); _modelQuickView->setClearBeforeRendering(true); _modelQuickView->setSource(QUrl("qrc:/qml/modelview.qml")); @@ -376,18 +309,36 @@ void MainWindow::on_actionFileOpenDuseDesign_triggered() void MainWindow::on_actionFileSaveAs_triggered() { QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"), QDir::currentPath(), "XMI files (*.xmi)"); - if (!fileName.isEmpty()) { - _currentFileName = fileName; -// saveXmi(_modelingObjectModel->modelingObjects()); - } + if (!fileName.isEmpty()) + saveXmi(fileName); } void MainWindow::on_actionFileSave_triggered() { - if (_currentFileName.isEmpty()) + if (ICore::self()->projectController()->currentModelFileName().isEmpty()) on_actionFileSaveAs_triggered(); -// else -// saveXmi(_modelingObjectModel->modelingObjects()); + else + saveXmi(); +} + +void MainWindow::saveXmi(QString fileName) +{ + IProjectController *projectController = ICore::self()->projectController(); + setCursor(Qt::WaitCursor); + bool ret; + if (fileName.isEmpty()) + ret = projectController->saveModel(); + else + ret = projectController->saveModelAs(fileName); + setCursor(Qt::ArrowCursor); + if (!ret) { + QMessageBox::critical(this, tr("Save model%1").arg(fileName.isEmpty() ? "":" as"), projectController->errorStrings().first()); + return; + } + else { + statusBar()->showMessage("XMI file successfully saved !", 3000); + setWindowTitle(projectController->currentModelFileName() + " - DuSE-MT"); + } } void MainWindow::on_actionHelpAboutPlugins_triggered() @@ -402,18 +353,18 @@ void MainWindow::on_actionHelpAboutDuSEMT_triggered() void MainWindow::on_centralWidget_currentChanged(int) { - if (_currentFileName.isEmpty()) - return; +// if (_currentFileName.isEmpty()) +// return; if (ui->centralWidget->currentIndex() == 1) { // foreach (QWrappedObject *object, _inputModel) // delete object; - _inputModel = loadXmi(_currentFileName); +// _inputModel = loadXmi(_currentFileName); evaluateQualityMetrics(); } else if (ui->centralWidget->currentIndex() == 2) { // foreach (QWrappedObject *object, _designSpaceLocation) // delete object; - _designSpaceLocation = loadXmi("/data/devel/qtmodeling/examples/uml/r1.xmi"); +// _designSpaceLocation = loadXmi("/data/devel/qtmodeling/examples/uml/r1.xmi"); addToDesignSpaceView(_designSpaceLocation.first()); evaluateQualityMetrics(); } @@ -438,12 +389,12 @@ void MainWindow::update() if (progress->value() == 99) { timer->stop(); progress->hide(); - _designSpaceLocation = loadXmi(QString::fromLatin1("/data/devel/qtmodeling/examples/uml/r%1.xmi").arg(qrand() % 11)); - addToPareto(_designSpaceLocation.first(), 0); - _designSpaceLocation = loadXmi(QString::fromLatin1("/data/devel/qtmodeling/examples/uml/r%1.xmi").arg(qrand() % 11)); - addToPareto(_designSpaceLocation.first(), 1); - _designSpaceLocation = loadXmi(QString::fromLatin1("/data/devel/qtmodeling/examples/uml/r%1.xmi").arg(qrand() % 11)); - addToPareto(_designSpaceLocation.first(), 2); +// _designSpaceLocation = loadXmi(QString::fromLatin1("/data/devel/qtmodeling/examples/uml/r%1.xmi").arg(qrand() % 11)); +// addToPareto(_designSpaceLocation.first(), 0); +// _designSpaceLocation = loadXmi(QString::fromLatin1("/data/devel/qtmodeling/examples/uml/r%1.xmi").arg(qrand() % 11)); +// addToPareto(_designSpaceLocation.first(), 1); +// _designSpaceLocation = loadXmi(QString::fromLatin1("/data/devel/qtmodeling/examples/uml/r%1.xmi").arg(qrand() % 11)); +// addToPareto(_designSpaceLocation.first(), 2); evaluateQualityMetrics(); } } @@ -545,7 +496,7 @@ void MainWindow::designSpaceChanged() // foreach (QWrappedObject *object, _designSpaceLocation) // delete object; - _designSpaceLocation = loadXmi(QString::fromLatin1("/data/devel/qtmodeling/examples/uml/r%1.xmi").arg(qrand() % 11)); +// _designSpaceLocation = loadXmi(QString::fromLatin1("/data/devel/qtmodeling/examples/uml/r%1.xmi").arg(qrand() % 11)); addToDesignSpaceView(_designSpaceLocation.first()); evaluateQualityMetrics(); } diff --git a/examples/uml/duse-mt/src/app/shell/mainwindow.h b/examples/uml/duse-mt/src/app/shell/mainwindow.h index 1358ffe6..b8193346 100644 --- a/examples/uml/duse-mt/src/app/shell/mainwindow.h +++ b/examples/uml/duse-mt/src/app/shell/mainwindow.h @@ -100,7 +100,6 @@ private Q_SLOTS: void on_centralWidget_currentChanged(int); void on_btnOptimize_clicked(); void evaluateQualityMetrics(); - void setModelInspector(QList<QModelingElement *> modelingObjectList); void metaModelChanged(QString newMetaModel); void addToView(QModelingElement *modelingObject, QQuickItem *parent = 0); @@ -112,14 +111,11 @@ private Q_SLOTS: private: Ui::MainWindow *ui; - void saveXmi(QList<QModelingObject *> modelObjects); - QList<QModelingElement *> loadXmi(QString fileName = 0); void populateDesignSpaceView(QModelingElement *modelingObject); QList<QModelingElement *> _inputModel; QList<QModelingElement *> _designSpaceLocation; - QString _currentFileName; QHash< QString, QPair<QMetaModelPlugin *, QJsonObject> > _metamodelPlugins; QList< QPair<DuSE::IPlugin *, QJsonObject> > _dusemtPlugins; QDialog *_aboutPluginsDialog; @@ -145,6 +141,7 @@ private: QTreeWidgetItem *itemForCategory(const QString &category); friend class UiController; + void saveXmi(QString fileName = QString()); }; } diff --git a/examples/uml/duse-mt/src/app/shell/projectcontroller.cpp b/examples/uml/duse-mt/src/app/shell/projectcontroller.cpp index 73eb2b94..78e77b4e 100644 --- a/examples/uml/duse-mt/src/app/shell/projectcontroller.cpp +++ b/examples/uml/duse-mt/src/app/shell/projectcontroller.cpp @@ -44,6 +44,8 @@ #include <QtWidgets/QMessageBox> #include <QtModeling/QXmiReader> +#include <QtModeling/QXmiWriter> +#include <QtModeling/QMetaModelPlugin> #include <QtModeling/QModelingElement> namespace DuSE @@ -67,33 +69,81 @@ QStringList ProjectController::errorStrings() const return _errorStrings; } +QString ProjectController::currentModelFileName() const +{ + return _currentModelFileName; +} + bool ProjectController::openModel(const QString &fileName) { - qDeleteAll(_currentModel); - _currentModel.clear(); + qDeleteAll(_currentModelElements); + _currentModelElements.clear(); + _currentModelObjects.clear(); _errorStrings.clear(); - _currentModelFileName = fileName; - QFile file(fileName); if (!file.open(QFile::ReadOnly | QFile::Text)) { _errorStrings << QObject::tr("Cannot read file %1").arg(fileName); return false; } + _currentModelFileName = fileName; + QXmiReader reader; - _currentModel = reader.readFile(&file); + _currentModelElements = reader.readFile(&file); _errorStrings << reader.errorStrings(); -// ui->txeIssues->setModel(new QStringListModel(reader.errorStrings())); -// setModelInspector(modelingObjectList); - QList<QModelingObject *> modelObjects; - foreach (QModelingElement *element, _currentModel) - modelObjects << element->asQModelingObject(); + foreach (QModelingElement *element, _currentModelElements) + _currentModelObjects << element->asQModelingObject(); + + emit modelOpened(_currentModelObjects); - emit modelOpened(modelObjects); + return true; +} + +bool ProjectController::saveModel() +{ + Q_ASSERT(!_currentModelFileName.isEmpty()); + _errorStrings.clear(); + + QFile file(_currentModelFileName); + if (!file.open(QFile::WriteOnly | QFile::Text)) { + _errorStrings << QObject::tr("Cannot write file %1").arg(_currentModelFileName); + return false; + } + QXmiWriter writer; + if (!writer.writeFile(_currentModelObjects, &file)) { + _errorStrings << QObject::tr("Error when writing XMI file %1").arg(_currentModelFileName); + return false; + } return true; } +bool ProjectController::saveModelAs(const QString &fileName) +{ + _currentModelFileName = fileName; + return saveModel(); +} + +bool ProjectController::createModel(const QString &modelFileName, QMetaModelPlugin *metamodelPlugin, const QString &topLevelType) +{ + QModelingElement *topLevelElement = metamodelPlugin->createModelingElement(topLevelType); + if (topLevelElement) { + topLevelElement->asQModelingObject()->setObjectName(topLevelType); + qDeleteAll(_currentModelElements); + _currentModelElements.clear(); + _currentModelObjects.clear(); + + _currentModelElements << topLevelElement; + _currentModelObjects << topLevelElement->asQModelingObject(); + + emit modelOpened(_currentModelObjects); + + return saveModelAs(modelFileName); + } + else + return false; +} + } diff --git a/examples/uml/duse-mt/src/app/shell/projectcontroller.h b/examples/uml/duse-mt/src/app/shell/projectcontroller.h index 6c7f598d..7b9bd8ee 100644 --- a/examples/uml/duse-mt/src/app/shell/projectcontroller.h +++ b/examples/uml/duse-mt/src/app/shell/projectcontroller.h @@ -56,13 +56,18 @@ public: virtual bool initialize(); virtual QStringList errorStrings() const; + virtual QString currentModelFileName() const; public Q_SLOTS: virtual bool openModel(const QString &fileName); + virtual bool saveModel(); + virtual bool saveModelAs(const QString &fileName); + virtual bool createModel(const QString &modelFileName, QMetaModelPlugin *metamodelPlugin, const QString &topLevelType); private: QString _currentModelFileName; - QList<QModelingElement *> _currentModel; + QList<QModelingElement *> _currentModelElements; + QList<QModelingObject *> _currentModelObjects; QStringList _errorStrings; }; diff --git a/examples/uml/duse-mt/src/libs/interfaces/iprojectcontroller.h b/examples/uml/duse-mt/src/libs/interfaces/iprojectcontroller.h index 304ccade..8d64929d 100644 --- a/examples/uml/duse-mt/src/libs/interfaces/iprojectcontroller.h +++ b/examples/uml/duse-mt/src/libs/interfaces/iprojectcontroller.h @@ -48,6 +48,7 @@ class QWidget; class QModelingObject; +class QMetaModelPlugin; namespace DuSE { @@ -61,9 +62,13 @@ public: virtual bool initialize() = 0; virtual QStringList errorStrings() const = 0; + virtual QString currentModelFileName() const = 0; public Q_SLOTS: virtual bool openModel(const QString &fileName) = 0; + virtual bool saveModel() = 0; + virtual bool saveModelAs(const QString &fileName) = 0; + virtual bool createModel(const QString &modelFileName, QMetaModelPlugin *metamodelPlugin, const QString &topLevelType) = 0; Q_SIGNALS: void modelOpened(QList<QModelingObject *> model); diff --git a/examples/uml/duse-mt/src/plugins/javascriptconsole/javascriptconsoleplugin.cpp b/examples/uml/duse-mt/src/plugins/javascriptconsole/javascriptconsoleplugin.cpp index 29785923..3e421d91 100644 --- a/examples/uml/duse-mt/src/plugins/javascriptconsole/javascriptconsoleplugin.cpp +++ b/examples/uml/duse-mt/src/plugins/javascriptconsole/javascriptconsoleplugin.cpp @@ -53,6 +53,7 @@ #include <QtWidgets/QListView> +#include <QtCore/QTimer> #include <QtCore/QStringListModel> template <class T> @@ -117,6 +118,7 @@ bool JavaScriptConsolePlugin::initialize(DuSE::ICore *core) connect(_javaScriptConsole->psbJSEvaluate, &QPushButton::clicked, this, &JavaScriptConsolePlugin::evaluate); connect(_javaScriptConsole->psbJSEvaluate, SIGNAL(clicked()), core->uiController(), SIGNAL(updateCurrentModelingObject())); connect(core->uiController(), &DuSE::IUiController::currentModelingObjectChanged, this, &JavaScriptConsolePlugin::setSelfProperty); + connect(core->projectController(), SIGNAL(modelOpened(QList<QModelingObject*>)), this, SLOT(setRootAndInputProperties(QList<QModelingObject*>))); qScriptRegisterMetaType(&_engine, qSetToScriptValue<QObject>, scriptValueToQSet<QObject>); qScriptRegisterMetaType(&_engine, qListToScriptValue<QObject>, scriptValueToQList<QObject>); @@ -129,6 +131,20 @@ void JavaScriptConsolePlugin::setSelfProperty(QModelingObject *modelingObject) _engine.globalObject().setProperty("self", _engine.newQObject(modelingObject)); } +void JavaScriptConsolePlugin::setRootAndInputProperties(QList<QModelingObject *> modelingObjects) +{ + QModelingObject *modelingObject = modelingObjects.at(0); + _engine.globalObject().setProperty(modelingObject->objectName(), _engine.newQObject(modelingObject)); + + QScriptValue array = _engine.newArray(); + foreach (QModelingObject *modelingObject, modelingObjects) + array.property(QString::fromLatin1("push")).call(array, QScriptValueList() << _engine.newQObject(modelingObject)); + _engine.globalObject().setProperty("input", array); + + _javaScriptConsole->txeJavaScript->setText("self"); + QTimer::singleShot(0, this, SLOT(evaluate())); +} + bool JavaScriptConsolePlugin::eventFilter(QObject *obj, QEvent *event) { if (event->type() == QEvent::KeyPress && obj == _javaScriptConsole->txeJavaScript) { diff --git a/examples/uml/duse-mt/src/plugins/javascriptconsole/javascriptconsoleplugin.h b/examples/uml/duse-mt/src/plugins/javascriptconsole/javascriptconsoleplugin.h index d9fc9529..2cd77d83 100644 --- a/examples/uml/duse-mt/src/plugins/javascriptconsole/javascriptconsoleplugin.h +++ b/examples/uml/duse-mt/src/plugins/javascriptconsole/javascriptconsoleplugin.h @@ -73,6 +73,7 @@ protected: public Q_SLOTS: void setSelfProperty(QModelingObject *modelingObject); + void setRootAndInputProperties(QList<QModelingObject *> modelingObjects); private Q_SLOTS: void evaluate(); |