Skip to content

Commit

Permalink
Allow renaming the MetaModel name.
Browse files Browse the repository at this point in the history
  • Loading branch information
adeas31 committed Sep 14, 2016
1 parent fe7fc5f commit 7f9de48
Show file tree
Hide file tree
Showing 13 changed files with 194 additions and 48 deletions.
31 changes: 31 additions & 0 deletions OMEdit/OMEditGUI/Editors/MetaModelEditor.cpp
Expand Up @@ -120,6 +120,21 @@ QDomElement MetaModelEditor::getSubModelElement(QString name)
return QDomElement();
}

/*!
* \brief MetaModelEditor::getMetaModelName
* Gets the MetaModel name.
* \return
*/
QString MetaModelEditor::getMetaModelName()
{
QDomNodeList nodes = mXmlDocument.elementsByTagName("Model");
for (int i = 0; i < nodes.size(); i++) {
QDomElement node = nodes.at(i).toElement();
return node.attribute("Name");
}
return "";
}

/*!
* \brief MetaModelEditor::getSubModelsElement
* Returns the SubModels element tag.
Expand Down Expand Up @@ -192,6 +207,22 @@ QDomNodeList MetaModelEditor::getConnections()
return mXmlDocument.elementsByTagName("Connection");
}

/*!
* \brief MetaModelEditor::setMetaModelName
* Sets the MetaModel name.
* \param name
*/
void MetaModelEditor::setMetaModelName(QString name)
{
QDomNodeList nodes = mXmlDocument.elementsByTagName("Model");
for (int i = 0; i < nodes.size(); i++) {
QDomElement node = nodes.at(i).toElement();
node.setAttribute("Name", name);
setPlainText(mXmlDocument.toString());
break;
}
}

/*!
* \brief MetaModelEditor::addSubModel
* Adds a SubModel tag with Annotation tag as child of it.
Expand Down
2 changes: 2 additions & 0 deletions OMEdit/OMEditGUI/Editors/MetaModelEditor.h
Expand Up @@ -67,12 +67,14 @@ class MetaModelEditor : public BaseEditor
bool validateText();
QString getXmlDocumentContent() {return mXmlDocument.toString();}
void setXmlDocumentContent(QString content) {mXmlDocument.setContent(content);}
QString getMetaModelName();
QDomElement getSubModelsElement();
QDomNodeList getSubModels();
QDomElement getInterfacePoint(QString subModelName, QString interfaceName);
QDomElement getConnectionsElement();
QDomNodeList getConnections();
QDomElement getSubModelElement(QString name);
void setMetaModelName(QString name);
bool addSubModel(QString name, QString exactStep, QString modelFile, QString startCommand, QString visible, QString origin, QString extent,
QString rotation);
void createAnnotationElement(QDomElement subModel, QString visible, QString origin, QString extent, QString rotation);
Expand Down
32 changes: 32 additions & 0 deletions OMEdit/OMEditGUI/Modeling/Commands.cpp
Expand Up @@ -1241,3 +1241,35 @@ void AlignInterfacesCommand::undo()
mpConnectionLineAnnotation->setAligned(mpMetaModelEditor->interfacesAligned(mFromInterface, mToInterface));
}
}

RenameMetaModelCommand::RenameMetaModelCommand(MetaModelEditor *pMetaModelEditor, QString oldMetaModelName, QString newMetaModelName,
QUndoCommand *pParent)
: QUndoCommand(pParent)
{
mpMetaModelEditor = pMetaModelEditor;
mOldMetaModelName = oldMetaModelName;
mNewMetaModelName = newMetaModelName;
setText(QString("Rename metamodel %1").arg(mpMetaModelEditor->getModelWidget()->getLibraryTreeItem()->getName()));
}

/*!
* \brief RenameMetaModelCommand::redo
* Redo the rename metamodel command
*/
void RenameMetaModelCommand::redo()
{
mpMetaModelEditor->setMetaModelName(mNewMetaModelName);
mpMetaModelEditor->getModelWidget()->getLibraryTreeItem()->setName(mNewMetaModelName);
mpMetaModelEditor->getModelWidget()->setWindowTitle(mNewMetaModelName);
}

/*!
* \brief RenameMetaModelCommand::undo
* Undo the rename metamodel command
*/
void RenameMetaModelCommand::undo()
{
mpMetaModelEditor->setMetaModelName(mOldMetaModelName);
mpMetaModelEditor->getModelWidget()->getLibraryTreeItem()->setName(mOldMetaModelName);
mpMetaModelEditor->getModelWidget()->setWindowTitle(mOldMetaModelName);
}
12 changes: 12 additions & 0 deletions OMEdit/OMEditGUI/Modeling/Commands.h
Expand Up @@ -290,4 +290,16 @@ class AlignInterfacesCommand : public QUndoCommand
LineAnnotation *mpConnectionLineAnnotation;
};

class RenameMetaModelCommand : public QUndoCommand
{
public:
RenameMetaModelCommand(MetaModelEditor *pMetaModelEditor, QString oldMetaModelName, QString newMetaModelName, QUndoCommand *pParent = 0);
void redo();
void undo();
private:
MetaModelEditor *mpMetaModelEditor;
QString mOldMetaModelName;
QString mNewMetaModelName;
};

#endif // COMMANDS_H
32 changes: 23 additions & 9 deletions OMEdit/OMEditGUI/Modeling/LibraryTreeWidget.cpp
Expand Up @@ -2616,8 +2616,8 @@ void LibraryTreeView::createActions()
connect(mpNewFolderAction, SIGNAL(triggered()), SLOT(createNewFolder()));
// rename Action
mpRenameAction = new QAction(Helper::rename, this);
mpRenameAction->setStatusTip(tr("Renames a file/folder"));
connect(mpRenameAction, SIGNAL(triggered()), SLOT(renameFileOrFolder()));
mpRenameAction->setStatusTip(Helper::renameTip);
connect(mpRenameAction, SIGNAL(triggered()), SLOT(renameLibraryTreeItem()));
// Delete Action
mpDeleteAction = new QAction(QIcon(":/Resources/icons/delete.svg"), Helper::deleteStr, this);
mpDeleteAction->setStatusTip(tr("Deletes the file"));
Expand Down Expand Up @@ -3078,16 +3078,16 @@ void LibraryTreeView::createNewFolder()
}

/*!
* \brief LibraryTreeView::renameFileOrFolder
* Renames the file/folder.
* \brief LibraryTreeView::renameLibraryTreeItem
* Renames the LibraryTreeItem.
*/
void LibraryTreeView::renameFileOrFolder()
void LibraryTreeView::renameLibraryTreeItem()
{
LibraryTreeItem *pLibraryTreeItem = getSelectedLibraryTreeItem();
if (!pLibraryTreeItem) {
return;
}
RenameItemDialog *pRenameItemDialog = new RenameItemDialog(pLibraryTreeItem->getFileName(), false, mpLibraryWidget->getMainWindow());
RenameItemDialog *pRenameItemDialog = new RenameItemDialog(pLibraryTreeItem, mpLibraryWidget->getMainWindow());
pRenameItemDialog->exec();
}

Expand Down Expand Up @@ -3469,9 +3469,10 @@ void LibraryWidget::openMetaModelOrTextFile(QFileInfo fileInfo, bool showProgres
}
// create a LibraryTreeItem for new loaded file.
LibraryTreeItem *pLibraryTreeItem = 0;
QString metaModelName;
if (fileInfo.suffix().compare("xml") == 0) {
if (parseMetaModelFile(fileInfo)) {
pLibraryTreeItem = mpLibraryTreeModel->createLibraryTreeItem(LibraryTreeItem::MetaModel, fileInfo.completeBaseName(),
if (parseMetaModelFile(fileInfo, &metaModelName)) {
pLibraryTreeItem = mpLibraryTreeModel->createLibraryTreeItem(LibraryTreeItem::MetaModel, metaModelName,
fileInfo.absoluteFilePath(), fileInfo.absoluteFilePath(), true,
mpLibraryTreeModel->getRootLibraryTreeItem());
}
Expand Down Expand Up @@ -3534,7 +3535,7 @@ void LibraryWidget::openDirectory(QFileInfo fileInfo, bool showProgress)
* \param fileInfo
* \return
*/
bool LibraryWidget::parseMetaModelFile(QFileInfo fileInfo)
bool LibraryWidget::parseMetaModelFile(QFileInfo fileInfo, QString *pMetaModelName)
{
QString contents = "";
QFile file(fileInfo.absoluteFilePath());
Expand All @@ -3559,6 +3560,19 @@ bool LibraryWidget::parseMetaModelFile(QFileInfo fileInfo)
delete pMessageHandler;
return false;
} else {
// if there are no errors with the document then read the Model Name attribute.
QDomDocument xmlDocument;
if (!xmlDocument.setContent(&file)) {
QMessageBox::critical(this, QString(Helper::applicationName).append(" - ").append(Helper::error),
tr("Error reading the xml file"), Helper::ok);
}
// read the file
QDomNodeList nodes = xmlDocument.elementsByTagName("Model");
for (int i = 0; i < nodes.size(); i++) {
QDomElement node = nodes.at(i).toElement();
*pMetaModelName = node.attribute("Name");
break;
}
delete pMessageHandler;
return true;
}
Expand Down
4 changes: 2 additions & 2 deletions OMEdit/OMEditGUI/Modeling/LibraryTreeWidget.h
Expand Up @@ -355,7 +355,7 @@ public slots:
void unloadMetaModelOrTextFile();
void createNewFile();
void createNewFolder();
void renameFileOrFolder();
void renameLibraryTreeItem();
void deleteTextFile();
void exportModelFMU();
void exportModelXML();
Expand Down Expand Up @@ -384,7 +384,7 @@ class LibraryWidget : public QWidget
void openModelicaFile(QString fileName, QString encoding = Helper::utf8, bool showProgress = true);
void openMetaModelOrTextFile(QFileInfo fileInfo, bool showProgress = true);
void openDirectory(QFileInfo fileInfo, bool showProgress = true);
bool parseMetaModelFile(QFileInfo fileInfo);
bool parseMetaModelFile(QFileInfo fileInfo, QString *pMetaModelName);
void parseAndLoadModelicaText(QString modelText);
bool saveFile(QString fileName, QString contents);
bool saveLibraryTreeItem(LibraryTreeItem *pLibraryTreeItem);
Expand Down
34 changes: 34 additions & 0 deletions OMEdit/OMEditGUI/Modeling/ModelWidgetContainer.cpp
Expand Up @@ -976,6 +976,10 @@ void GraphicsView::createActions()
// Graphics View Properties Action
mpPropertiesAction = new QAction(Helper::properties, this);
connect(mpPropertiesAction, SIGNAL(triggered()), SLOT(showGraphicsViewProperties()));
// rename Action
mpRenameAction = new QAction(Helper::rename, this);
mpRenameAction->setStatusTip(Helper::renameTip);
connect(mpRenameAction, SIGNAL(triggered()), SLOT(showRenameDialog()));
// Simulation Params Action
mpSimulationParamsAction = new QAction(QIcon(":/Resources/icons/simulation-parameters.svg"), Helper::simulationParams, this);
mpSimulationParamsAction->setStatusTip(Helper::simulationParamsTip);
Expand Down Expand Up @@ -1361,6 +1365,17 @@ void GraphicsView::showSimulationParamsDialog()
pMetaModelSimulationParamsDialog->exec();
}

/*!
* \brief GraphicsView::showRenameDialog
* Opens the RenameItemDialog.
*/
void GraphicsView::showRenameDialog()
{
RenameItemDialog *pRenameItemDialog;
pRenameItemDialog = new RenameItemDialog(mpModelWidget->getLibraryTreeItem(), mpModelWidget->getModelWidgetContainer()->getMainWindow());
pRenameItemDialog->exec();
}

/*!
* \brief GraphicsView::manhattanizeItems
* Manhattanize the selected items by emitting GraphicsView::mouseManhattanize() SIGNAL.
Expand Down Expand Up @@ -2030,6 +2045,8 @@ void GraphicsView::contextMenuEvent(QContextMenuEvent *event)
menu.addSeparator();
menu.addAction(mpPropertiesAction);
} else if (mpModelWidget->getLibraryTreeItem()->getLibraryType() == LibraryTreeItem::MetaModel) {
menu.addSeparator();
menu.addAction(mpRenameAction);
menu.addSeparator();
menu.addAction(mpSimulationParamsAction);
}
Expand Down Expand Up @@ -2888,6 +2905,12 @@ void ModelWidget::reDrawModelWidget()
mpDiagramGraphicsView->scene()->clear();
/* get model components, connection and shapes. */
if (getLibraryTreeItem()->getLibraryType() == LibraryTreeItem::MetaModel) {
// read new metamodel anem
QString metaModelName = getMetaModelName();
mpLibraryTreeItem->setName(metaModelName);
mpModelWidgetContainer->getMainWindow()->getLibraryWidget()->getLibraryTreeModel()->updateLibraryTreeItem(mpLibraryTreeItem);
setWindowTitle(metaModelName);
// get the submodels and connections
getMetaModelSubModels();
getMetaModelConnections();
// clear the undo stack
Expand Down Expand Up @@ -3666,6 +3689,17 @@ void ModelWidget::getModelConnections()
}
}

/*!
* \brief ModelWidget::getMetaModelName
* Gets the MetaModel name.
* \return
*/
QString ModelWidget::getMetaModelName()
{
MetaModelEditor *pMetaModelEditor = dynamic_cast<MetaModelEditor*>(mpEditor);
return pMetaModelEditor->getMetaModelName();
}

/*!
* \brief ModelWidget::getMetaModelSubModels
* Gets the submodels of the TLM and place them in the diagram GraphicsView.
Expand Down
3 changes: 3 additions & 0 deletions OMEdit/OMEditGUI/Modeling/ModelWidgetContainer.h
Expand Up @@ -106,6 +106,7 @@ class GraphicsView : public QGraphicsView
TextAnnotation *mpTextShapeAnnotation;
BitmapAnnotation *mpBitmapShapeAnnotation;
QAction *mpPropertiesAction;
QAction *mpRenameAction;
QAction *mpSimulationParamsAction;
QAction *mpManhattanizeAction;
QAction *mpDeleteAction;
Expand Down Expand Up @@ -256,6 +257,7 @@ public slots:
void clearSelection();
void addClassAnnotation(bool alwaysAdd = true);
void showGraphicsViewProperties();
void showRenameDialog();
void showSimulationParamsDialog();
void manhattanizeItems();
void deleteItems();
Expand Down Expand Up @@ -412,6 +414,7 @@ class ModelWidget : public QWidget
void drawModelInheritedClassConnections(ModelWidget *pModelWidget);
void removeInheritedClassConnections();
void getModelConnections();
QString getMetaModelName();
void getMetaModelSubModels();
void getMetaModelConnections();
private slots:
Expand Down

0 comments on commit 7f9de48

Please sign in to comment.