Skip to content

Commit

Permalink
Added API structure for managing the meta-model xml data.
Browse files Browse the repository at this point in the history
Create directories for the sub-models and save their respective model files in them while saving the meta-model xml.
  • Loading branch information
adeas31 committed Jun 22, 2015
1 parent 8d10576 commit 7e4f8d7
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 171 deletions.
33 changes: 5 additions & 28 deletions OMEdit/OMEditGUI/Component/Component.cpp
Expand Up @@ -901,32 +901,10 @@ void Component::updatePlacementAnnotation()
{
// Add component annotation.
LibraryTreeNode *pLibraryTreeNode = mpGraphicsView->getModelWidget()->getLibraryTreeNode();
if(pLibraryTreeNode->getLibraryType()== LibraryTreeNode::TLM) {
QDomDocument doc;
doc.setContent(mpGraphicsView->getModelWidget()->getEditor()->getPlainTextEdit()->toPlainText());
// Get the "Root" element
QDomElement docElem = doc.documentElement();
QDomElement subModels = docElem.firstChildElement();
while (!subModels.isNull()) {
if(subModels.tagName() == "SubModels")
break;
subModels = subModels.nextSiblingElement();
}
QDomElement subModel = subModels.firstChildElement();
while (!subModel.isNull()) {
if(subModel.tagName() == "SubModel" && subModel.attribute("Name") == mName) {
QDomElement annotation = subModel.firstChildElement("Annotation");
annotation.setAttribute("Visible", getTransformation()->getVisible()? "true" : "false");
annotation.setAttribute("Origin", getTransformationOrigin());
annotation.setAttribute("Extent", getTransformationExtent());
annotation.setAttribute("Rotation", QString::number(getTransformation()->getRotateAngle()));
break;
}
subModel = subModel.nextSiblingElement();
}
QString metaModelText = doc.toString();
MainWindow *pMainWindow = mpGraphicsView->getModelWidget()->getModelWidgetContainer()->getMainWindow();
pMainWindow->getModelWidgetContainer()->getCurrentModelWidget()->getEditor()->getPlainTextEdit()->setPlainText(metaModelText);
if (pLibraryTreeNode->getLibraryType()== LibraryTreeNode::TLM) {
TLMEditor *pTLMEditor = dynamic_cast<TLMEditor*>(mpGraphicsView->getModelWidget()->getEditor());
pTLMEditor->updateSubModelPlacementAnnotation(mName, getTransformation()->getVisible()? "true" : "false", getTransformationOrigin(),
getTransformationExtent(), QString::number(getTransformation()->getRotateAngle()));
} else {
mpOMCProxy->updateComponent(mName, mClassName, mpGraphicsView->getModelWidget()->getLibraryTreeNode()->getNameStructure(),
getPlacementAnnotation());
Expand Down Expand Up @@ -1052,8 +1030,7 @@ void Component::deleteMe()
// make the model modified
mpGraphicsView->getModelWidget()->setModelModified();
/* When something is deleted from the icon layer then update the LibraryTreeNode in the Library Browser */
if (mpGraphicsView->getViewType() == StringHandler::Icon)
{
if (mpGraphicsView->getViewType() == StringHandler::Icon) {
MainWindow *pMainWindow = mpGraphicsView->getModelWidget()->getModelWidgetContainer()->getMainWindow();
pMainWindow->getLibraryTreeWidget()->loadLibraryComponent(mpGraphicsView->getModelWidget()->getLibraryTreeNode());
}
Expand Down
111 changes: 108 additions & 3 deletions OMEdit/OMEditGUI/Editors/TLMEditor.cpp
Expand Up @@ -68,9 +68,13 @@ void TLMEditor::contentsHasChanged(int position, int charsRemoved, int charsAdde

bool TLMEditor::validateMetaModelText()
{
if (mTextChanged) {
emit focusOut();
}
if (mTextChanged) {
if (!emit focusOut()) {
return false;
} else {
mTextChanged = false;
}
}
return true;
}

Expand All @@ -85,10 +89,111 @@ void TLMEditor::setPlainText(const QString &text)
if (text != mpPlainTextEdit->toPlainText()) {
mForceSetPlainText = true;
mpPlainTextEdit->setPlainText(text);
mXmlDocument.setContent(text);
mForceSetPlainText = false;
}
}

QDomElement TLMEditor::getSubModelsElement()
{
QDomNodeList subModels = mXmlDocument.elementsByTagName("SubModels");
if (subModels.size() > 0) {
return subModels.at(0).toElement();
}
return QDomElement();
}

QDomNodeList TLMEditor::getSubModels()
{
return mXmlDocument.elementsByTagName("SubModel");
}

bool TLMEditor::addSubModel(QString name, QString exactStep, QString modelFile, QString startCommand, QString visible, QString origin,
QString extent, QString rotation)
{
QDomElement subModels = getSubModelsElement();
if (!subModels.isNull()) {
QDomElement subModel = mXmlDocument.createElement("SubModel");
subModel.setAttribute("Name", name);
subModel.setAttribute("ExactStep", exactStep);
subModel.setAttribute("ModelFile", modelFile);
subModel.setAttribute("StartCommand", startCommand);
// create Annotation Element
QDomElement annotation = mXmlDocument.createElement("Annotation");
annotation.setAttribute("Visible", visible);
annotation.setAttribute("Origin", origin);
annotation.setAttribute("Extent", extent);
annotation.setAttribute("Rotation", rotation);
subModel.appendChild(annotation);
subModels.appendChild(subModel);
mpPlainTextEdit->setPlainText(mXmlDocument.toString());
return true;
}
return false;
}

bool TLMEditor::updateSubModelPlacementAnnotation(QString name, QString visible, QString origin, QString extent, QString rotation)
{
QDomNodeList subModelList = mXmlDocument.elementsByTagName("SubModel");
for (int i = 0 ; i < subModelList.size() ; i++) {
QDomElement subModel = subModelList.at(i).toElement();
if (subModel.attribute("Name").compare(name) == 0) {
QDomNodeList subModelChildren = subModel.childNodes();
for (int j = 0 ; j < subModelChildren.size() ; j++) {
QDomElement annotationElement = subModelChildren.at(j).toElement();
if (annotationElement.tagName().compare("Annotation") == 0) {
annotationElement.setAttribute("Visible", visible);
annotationElement.setAttribute("Origin", origin);
annotationElement.setAttribute("Extent", extent);
annotationElement.setAttribute("Rotation", rotation);
mpPlainTextEdit->setPlainText(mXmlDocument.toString());
return true;
}
}
break;
}
}
return false;
}

void TLMEditor::addInterfacesData(QDomElement interfaces)
{
QDomNodeList subModelList = mXmlDocument.elementsByTagName("SubModel");
for (int i = 0 ; i < subModelList.size() ; i++) {
QDomElement subModel = subModelList.at(i).toElement();
QDomElement interfaceDataElement = interfaces.firstChildElement();
while (!interfaceDataElement.isNull()) {
if (subModel.attribute("Name").compare(interfaceDataElement.attribute("model")) == 0) {
QDomElement interfacePoint = mXmlDocument.createElement("InterfacePoint");
interfacePoint.setAttribute("Name",interfaceDataElement.attribute("name"));
interfacePoint.setAttribute("Position",interfaceDataElement.attribute("Position"));
interfacePoint.setAttribute("Angle321",interfaceDataElement.attribute("Angle321"));
subModel.appendChild(interfacePoint);
mpPlainTextEdit->setPlainText(mXmlDocument.toString());
}
interfaceDataElement = interfaceDataElement.nextSiblingElement();
}
}
}

bool TLMEditor::deleteSubModel(QString name)
{
QDomNodeList subModelList = mXmlDocument.elementsByTagName("SubModel");
for (int i = 0 ; i < subModelList.size() ; i++) {
QDomElement subModel = subModelList.at(i).toElement();
if (subModel.attribute("Name").compare(name) == 0) {
QDomElement subModels = getSubModelsElement();
if (!subModels.isNull()) {
subModels.removeChild(subModel);
mpPlainTextEdit->setPlainText(mXmlDocument.toString());
return true;
}
break;
}
}
return false;
}

//! @class TLMHighlighter
//! @brief A syntax highlighter for TLMEditor.

Expand Down
8 changes: 8 additions & 0 deletions OMEdit/OMEditGUI/Editors/TLMEditor.h
Expand Up @@ -49,9 +49,17 @@ class TLMEditor : public BaseEditor
TLMEditor(ModelWidget *pModelWidget);
bool validateMetaModelText();
void setPlainText(const QString &text);
QDomElement getSubModelsElement();
QDomNodeList getSubModels();
bool addSubModel(QString name, QString exactStep, QString modelFile, QString startCommand, QString visible, QString origin, QString extent,
QString rotation);
bool updateSubModelPlacementAnnotation(QString name, QString visible, QString origin, QString extent, QString rotation);
void addInterfacesData(QDomElement interfaces);
bool deleteSubModel(QString name);
private:
bool mForceSetPlainText;
bool mTextChanged;
QDomDocument mXmlDocument;
signals:
bool focusOut();
private slots:
Expand Down
46 changes: 10 additions & 36 deletions OMEdit/OMEditGUI/MainWindow.cpp
Expand Up @@ -2199,47 +2199,14 @@ void MainWindow::readInterfaceData(LibraryTreeNode *pLibraryTreeNode)
QDomDocument interfaceData;
interfaceData.setContent(&file);
file.close();
// Get the "Root" element
// Get the interfaces element
QDomElement interfaces = interfaceData.documentElement();
QDomElement interfaceDataElement = interfaces.firstChildElement();
while (!interfaceDataElement.isNull()) {
if (interfaceDataElement.tagName() == "Interface")
break;
interfaceDataElement = interfaceDataElement.nextSiblingElement();
}

// if we don't have ModelWidget then show it.
if (!pLibraryTreeNode->getModelWidget()) {
mpLibraryTreeWidget->showModelWidget(pLibraryTreeNode);
}
QDomDocument doc;
doc.setContent(pLibraryTreeNode->getModelWidget()->getEditor()->getPlainTextEdit()->toPlainText());
// Get the "Root" element
QDomElement docElem = doc.documentElement();
QDomElement subModels = docElem.firstChildElement();
while (!subModels.isNull()) {
if(subModels.tagName() == "SubModels")
break;
subModels = subModels.nextSiblingElement();
}
QDomElement subModel = subModels.firstChildElement();
while (!subModel.isNull()) {
QDomElement interfaceDataElement = interfaces.firstChildElement();
while(!interfaceDataElement.isNull()){
if(subModel.tagName() == "SubModel" && subModel.attribute("Name") == interfaceDataElement.attribute("model") ) {
QDomElement interfacePoint = doc.createElement("InterfacePoint");
interfacePoint.setAttribute("Name",interfaceDataElement.attribute("name") );
interfacePoint.setAttribute("Position",interfaceDataElement.attribute("Position") );
interfacePoint.setAttribute("Angle321",interfaceDataElement.attribute("Angle321") );
subModel.appendChild(interfacePoint);
break;
}
interfaceDataElement = interfaceDataElement.nextSiblingElement();
}
subModel = subModel.nextSiblingElement();
}
QString metaModelText = doc.toString();
pLibraryTreeNode->getModelWidget()->getEditor()->getPlainTextEdit()->setPlainText(metaModelText);
TLMEditor *pTLMEditor = dynamic_cast<TLMEditor*>(pLibraryTreeNode->getModelWidget()->getEditor());
pTLMEditor->addInterfacesData(interfaces);
}
}

Expand Down Expand Up @@ -2964,6 +2931,13 @@ void MainWindow::tileSubWindows(QMdiArea *pMdiArea, bool horizontally)
*/
void MainWindow::fetchInterfaceDataHelper(LibraryTreeNode *pLibraryTreeNode)
{
/* if Modelica text is changed manually by user then validate it before saving. */
if (pLibraryTreeNode->getModelWidget()) {
TLMEditor *pTLMEditor = dynamic_cast<TLMEditor*>(pLibraryTreeNode->getModelWidget()->getEditor());
if (pTLMEditor && !pTLMEditor->validateMetaModelText()) {
return;
}
}
FetchInterfaceDataDialog *pFetchInterfaceDataDialog = new FetchInterfaceDataDialog(pLibraryTreeNode, this);
connect(pFetchInterfaceDataDialog, SIGNAL(readInterfaceData(LibraryTreeNode*)), SLOT(readInterfaceData(LibraryTreeNode*)));
pFetchInterfaceDataDialog->exec();
Expand Down
31 changes: 18 additions & 13 deletions OMEdit/OMEditGUI/Modeling/LibraryTreeWidget.cpp
Expand Up @@ -1264,8 +1264,8 @@ bool LibraryTreeWidget::saveTLMLibraryTreeNode(LibraryTreeNode *pLibraryTreeNode
if (file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
QTextStream textStream(&file);
textStream.setCodec(Helper::utf8.toStdString().data());
textStream.setGenerateByteOrderMark(false);QString metaModelXml = pLibraryTreeNode->getModelWidget()->getEditor()->getPlainTextEdit()->toPlainText();
textStream << metaModelXml;
textStream.setGenerateByteOrderMark(false);
textStream << pLibraryTreeNode->getModelWidget()->getEditor()->getPlainTextEdit()->toPlainText();
file.close();
/* mark the file as saved and update the labels. */
pLibraryTreeNode->setIsSaved(true);
Expand All @@ -1275,18 +1275,24 @@ bool LibraryTreeWidget::saveTLMLibraryTreeNode(LibraryTreeNode *pLibraryTreeNode
pLibraryTreeNode->getModelWidget()->setModelFilePathLabel(fileName);
}
// Create folders for the submodels and copy there source file in them.
QDomDocument xml;
xml.setContent(metaModelXml);
QDomNodeList subModels = xml.elementsByTagName("SubModel");
TLMEditor *pTLMEditor = dynamic_cast<TLMEditor*>(pLibraryTreeNode->getModelWidget()->getEditor());
GraphicsView *pGraphicsView = pLibraryTreeNode->getModelWidget()->getDiagramGraphicsView();
QDomNodeList subModels = pTLMEditor->getSubModels();
for (int i = 0; i < subModels.size(); i++) {
QDomElement subModel = subModels.at(i).toElement();
QString directoryName = subModel.attribute("Name");
/*! @todo We need to have a complete filename here to copy the file. */
QString modelFile = subModel.attribute("ModelFile");
QFileInfo fileInfo(fileName);
QString directoryPath = fileInfo.absoluteDir().absolutePath() + "/" + directoryName;
if (!QDir().exists(directoryPath)) {
QDir().mkpath(directoryPath);
Component *pComponent = pGraphicsView->getComponentObject(directoryName);
if (pComponent) {
// create directory for submodel
QFileInfo fileInfo(fileName);
QString directoryPath = fileInfo.absoluteDir().absolutePath() + "/" + directoryName;
if (!QDir().exists(directoryPath)) {
QDir().mkpath(directoryPath);
}
// copy the submodel file to the created directory
QString modelFile = pComponent->getFileName();
QFileInfo modelFileInfo(modelFile);
QFile::copy(modelFileInfo.absoluteFilePath(), directoryPath + "/" + modelFileInfo.fileName());
}
}
} else {
Expand Down Expand Up @@ -2043,8 +2049,7 @@ void LibraryTreeWidget::showModelWidget(LibraryTreeNode *pLibraryTreeNode, bool
pLibraryTreeNode->getModelWidget()->setWindowTitle(pLibraryTreeNode->getNameStructure() + (pLibraryTreeNode->isSaved() ? "" : "*"));
mpMainWindow->getModelWidgetContainer()->addModelWidget(pLibraryTreeNode->getModelWidget());
} else {
ModelWidget *pModelWidget = 0;
pModelWidget = new ModelWidget(pLibraryTreeNode, mpMainWindow->getModelWidgetContainer(), newClass, extendsClass, text);
ModelWidget *pModelWidget = new ModelWidget(pLibraryTreeNode, mpMainWindow->getModelWidgetContainer(), newClass, extendsClass, text);
pLibraryTreeNode->setModelWidget(pModelWidget);
pLibraryTreeNode->getModelWidget()->setWindowTitle(pLibraryTreeNode->getNameStructure() + (pLibraryTreeNode->isSaved() ? "" : "*"));
mpMainWindow->getModelWidgetContainer()->addModelWidget(pModelWidget);
Expand Down

0 comments on commit 7e4f8d7

Please sign in to comment.