Skip to content

Commit

Permalink
Fixed opening of TLM/Text files.
Browse files Browse the repository at this point in the history
  • Loading branch information
adeas31 committed Aug 25, 2015
1 parent 485c5f5 commit 04315ef
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 93 deletions.
8 changes: 1 addition & 7 deletions OMEdit/OMEditGUI/MainWindow.cpp
Expand Up @@ -1373,13 +1373,7 @@ void MainWindow::loadExternalModels()
pMessageBox->setStandardButtons(QMessageBox::Ok);
pMessageBox->exec();
} else {
QFileInfo fileInfo(file);
LibraryTreeItem *pLibraryTreeItem = mpLibraryWidget->getLibraryTreeModel()->createLibraryTreeItem(LibraryTreeItem::Text, fileInfo.completeBaseName(), true);
if (pLibraryTreeItem) {
pLibraryTreeItem->setSaveContentsType(LibraryTreeItem::SaveInOneFile);
pLibraryTreeItem->setIsSaved(true);
pLibraryTreeItem->setFileName(fileInfo.absoluteFilePath());
}
mpLibraryWidget->openFile(file, Helper::utf8, false);
}
}
mpStatusBar->clearMessage();
Expand Down
108 changes: 43 additions & 65 deletions OMEdit/OMEditGUI/Modeling/LibraryTreeWidget.cpp
Expand Up @@ -1186,7 +1186,7 @@ void LibraryTreeModel::showHideProtectedClasses()

/*!
* \brief LibraryTreeModel::unloadClass
* unloads/deletes the Modelica class.
* Unloads/deletes the Modelica class.
* \param pLibraryTreeItem
* \param askQuestion
* \return
Expand Down Expand Up @@ -1254,10 +1254,16 @@ void LibraryTreeModel::unloadClassChildren(LibraryTreeItem *pParentLibraryTreeIt
}
}

bool LibraryTreeModel::unloadTextFile(LibraryTreeItem *pLibraryTreeItem, bool askQuestion)
/*!
* \brief LibraryTreeModel::unloadTLMOrTextFile
* Unloads/deletes the TLM/Text class.
* \param pLibraryTreeItem
* \param askQuestion
* \return
*/
bool LibraryTreeModel::unloadTLMOrTextFile(LibraryTreeItem *pLibraryTreeItem, bool askQuestion)
{
if (askQuestion)
{
if (askQuestion) {
QMessageBox *pMessageBox = new QMessageBox(mpLibraryWidget->getMainWindow());
pMessageBox->setWindowTitle(QString(Helper::applicationName).append(" - ").append(Helper::question));
pMessageBox->setIcon(QMessageBox::Question);
Expand All @@ -1266,8 +1272,7 @@ bool LibraryTreeModel::unloadTextFile(LibraryTreeItem *pLibraryTreeItem, bool as
pMessageBox->setStandardButtons(QMessageBox::Yes | QMessageBox::No);
pMessageBox->setDefaultButton(QMessageBox::Yes);
int answer = pMessageBox->exec();
switch (answer)
{
switch (answer) {
case QMessageBox::Yes:
// Yes was clicked. Don't return.
break;
Expand All @@ -1279,36 +1284,22 @@ bool LibraryTreeModel::unloadTextFile(LibraryTreeItem *pLibraryTreeItem, bool as
return false;
}
}
// mpLibraryWidget->unloadLibraryTreeItemAndModelWidget(pLibraryTreeItem);
return true;
}

bool LibraryTreeModel::unloadTLMFile(LibraryTreeItem *pLibraryTreeItem, bool askQuestion)
{
if (askQuestion)
{
QMessageBox *pMessageBox = new QMessageBox(mpLibraryWidget->getMainWindow());
pMessageBox->setWindowTitle(QString(Helper::applicationName).append(" - ").append(Helper::question));
pMessageBox->setIcon(QMessageBox::Question);
pMessageBox->setAttribute(Qt::WA_DeleteOnClose);
pMessageBox->setText(GUIMessages::getMessage(GUIMessages::DELETE_TEXT_FILE_MSG).arg(pLibraryTreeItem->getNameStructure()));
pMessageBox->setStandardButtons(QMessageBox::Yes | QMessageBox::No);
pMessageBox->setDefaultButton(QMessageBox::Yes);
int answer = pMessageBox->exec();
switch (answer)
{
case QMessageBox::Yes:
// Yes was clicked. Don't return.
break;
case QMessageBox::No:
// No was clicked. Return
return false;
default:
// should never be reached
return false;
/* remove the ModelWidget of LibraryTreeItem and remove the QMdiSubWindow from MdiArea and delete it. */
if (pLibraryTreeItem->getModelWidget()) {
QMdiSubWindow *pMdiSubWindow = mpLibraryWidget->getMainWindow()->getModelWidgetContainer()->getMdiSubWindow(pLibraryTreeItem->getModelWidget());
if (pMdiSubWindow) {
pMdiSubWindow->close();
pMdiSubWindow->deleteLater();
}
pLibraryTreeItem->getModelWidget()->deleteLater();
}
// mpLibraryWidget->unloadLibraryTreeItemAndModelWidget(pLibraryTreeItem);
// remove the LibraryTreeItem from Libraries Browser
int row = pLibraryTreeItem->row();
beginRemoveRows(libraryTreeItemIndex(pLibraryTreeItem), row, row);
pLibraryTreeItem->removeChildren();
mpRootLibraryTreeItem->removeChild(pLibraryTreeItem);
delete pLibraryTreeItem;
endRemoveRows();
return true;
}

Expand Down Expand Up @@ -1466,14 +1457,10 @@ void LibraryTreeView::createActions()
mpUnloadClassAction = new QAction(QIcon(":/Resources/icons/delete.svg"), Helper::unloadClass, this);
mpUnloadClassAction->setStatusTip(Helper::unloadClassTip);
connect(mpUnloadClassAction, SIGNAL(triggered()), SLOT(unloadClass()));
// unload text file Action
mpUnloadTextFileAction = new QAction(QIcon(":/Resources/icons/delete.svg"), Helper::unloadClass, this);
mpUnloadTextFileAction->setStatusTip(Helper::unloadClassTip);
connect(mpUnloadTextFileAction, SIGNAL(triggered()), SLOT(unloadTextFile()));
// unload xml file Action
// unload TLM/Text file Action
mpUnloadTLMFileAction = new QAction(QIcon(":/Resources/icons/delete.svg"), Helper::unloadClass, this);
mpUnloadTLMFileAction->setStatusTip(Helper::unloadXMLTip);
connect(mpUnloadTLMFileAction, SIGNAL(triggered()), SLOT(unloadTLMFile()));
mpUnloadTLMFileAction->setStatusTip(Helper::unloadTLMOrTextTip);
connect(mpUnloadTLMFileAction, SIGNAL(triggered()), SLOT(unloadTLMOrTextFile()));
/*
// refresh Action
mpRefreshAction = new QAction(QIcon(":/Resources/icons/refresh.svg"), Helper::refresh, this);
Expand Down Expand Up @@ -1612,7 +1599,7 @@ void LibraryTreeView::showContextMenu(QPoint point)
menu.addAction(mpExportFigaroAction);
break;
case LibraryTreeItem::Text:
menu.addAction(mpUnloadTextFileAction);
menu.addAction(mpUnloadTLMFileAction);
break;
case LibraryTreeItem::TLM:
menu.addAction(mpFetchInterfaceDataAction);
Expand Down Expand Up @@ -1774,26 +1761,14 @@ void LibraryTreeView::unloadClass()
}

/*!
* \brief LibraryTreeView::unloadTextFile
* Unloads/Deletes the Text LibraryTreeItem.
* \brief LibraryTreeView::unloadTLMOrTextFile
* Unloads/Deletes the TLM/Text LibraryTreeItem.
*/
void LibraryTreeView::unloadTextFile()
void LibraryTreeView::unloadTLMOrTextFile()
{
LibraryTreeItem *pLibraryTreeItem = getSelectedLibraryTreeItem();
if (pLibraryTreeItem) {
mpLibraryWidget->getLibraryTreeModel()->unloadTextFile(pLibraryTreeItem);
}
}

/*!
* \brief LibraryTreeView::unloadTLMFile
* Unloads/Deletes the TLM LibraryTreeItem.
*/
void LibraryTreeView::unloadTLMFile()
{
LibraryTreeItem *pLibraryTreeItem = getSelectedLibraryTreeItem();
if (pLibraryTreeItem) {
mpLibraryWidget->getLibraryTreeModel()->unloadTLMFile(pLibraryTreeItem);
mpLibraryWidget->getLibraryTreeModel()->unloadTLMOrTextFile(pLibraryTreeItem);
}
}

Expand Down Expand Up @@ -1959,11 +1934,8 @@ void LibraryWidget::openFile(QString fileName, QString encoding, bool showProgre
}
if (fileInfo.suffix().compare("mo") == 0) {
openModelicaFile(fileName, encoding, showProgress);
} else if (fileInfo.suffix().compare("xml") == 0) {
openTLMFile(fileInfo, showProgress);
} else {
QMessageBox::information(this, Helper::applicationName + " - " + Helper::error, GUIMessages::getMessage(GUIMessages::ERROR_OCCURRED)
.arg(tr("Unable to open the file, unknown file type.")), Helper::ok);
openTLMOrTextFile(fileInfo, showProgress);
}
}

Expand Down Expand Up @@ -2041,7 +2013,7 @@ void LibraryWidget::openModelicaFile(QString fileName, QString encoding, bool sh
if (showProgress) mpMainWindow->getStatusBar()->clearMessage();
}

void LibraryWidget::openTLMFile(QFileInfo fileInfo, bool showProgress)
void LibraryWidget::openTLMOrTextFile(QFileInfo fileInfo, bool showProgress)
{
if (showProgress) mpMainWindow->getStatusBar()->showMessage(QString(Helper::loading).append(": ").append(fileInfo.absoluteFilePath()));
// check if the file is already loaded.
Expand All @@ -2061,12 +2033,18 @@ void LibraryWidget::openTLMFile(QFileInfo fileInfo, bool showProgress)
return;
}
}
// create a LibraryTreeItem for new loaded TLM file.
LibraryTreeItem *pLibraryTreeItem = mpLibraryTreeModel->createLibraryTreeItem(LibraryTreeItem::TLM, fileInfo.completeBaseName(), true);
// create a LibraryTreeItem for new loaded file.
LibraryTreeItem *pLibraryTreeItem = 0;
if (fileInfo.suffix().compare("xml") == 0) {
pLibraryTreeItem = mpLibraryTreeModel->createLibraryTreeItem(LibraryTreeItem::TLM, fileInfo.completeBaseName(), true);
} else {
pLibraryTreeItem = mpLibraryTreeModel->createLibraryTreeItem(LibraryTreeItem::Text, fileInfo.completeBaseName(), true);
}
if (pLibraryTreeItem) {
pLibraryTreeItem->setSaveContentsType(LibraryTreeItem::SaveInOneFile);
pLibraryTreeItem->setIsSaved(true);
pLibraryTreeItem->setFileName(fileInfo.absoluteFilePath());
mpLibraryTreeModel->readLibraryTreeItemClassText(pLibraryTreeItem);
mpMainWindow->addRecentFile(fileInfo.absoluteFilePath(), Helper::utf8);
}
if (showProgress) mpMainWindow->getStatusBar()->clearMessage();
Expand Down
9 changes: 3 additions & 6 deletions OMEdit/OMEditGUI/Modeling/LibraryTreeWidget.h
Expand Up @@ -207,8 +207,7 @@ class LibraryTreeModel : public QAbstractItemModel
void showHideProtectedClasses();
bool unloadClass(LibraryTreeItem *pLibraryTreeItem, bool askQuestion = true);
void unloadClassChildren(LibraryTreeItem *pParentLibraryTreeItem);
bool unloadTextFile(LibraryTreeItem *pLibraryTreeItem, bool askQuestion = true);
bool unloadTLMFile(LibraryTreeItem *pLibraryTreeItem, bool askQuestion = true);
bool unloadTLMOrTextFile(LibraryTreeItem *pLibraryTreeItem, bool askQuestion = true);
QString getUniqueTopLevelItemName(QString name, int number = 1);
private:
LibraryWidget *mpLibraryWidget;
Expand Down Expand Up @@ -239,7 +238,6 @@ class LibraryTreeView : public QTreeView
QAction *mpSimulationSetupAction;
QAction *mpDuplicateClassAction;
QAction *mpUnloadClassAction;
QAction *mpUnloadTextFileAction;
QAction *mpUnloadTLMFileAction;
QAction *mpRefreshAction;
QAction *mpExportFMUAction;
Expand All @@ -265,8 +263,7 @@ public slots:
void simulationSetup();
void duplicateClass();
void unloadClass();
void unloadTextFile();
void unloadTLMFile();
void unloadTLMOrTextFile();
void exportModelFMU();
void exportModelXML();
void exportModelFigaro();
Expand All @@ -289,7 +286,7 @@ class LibraryWidget : public QWidget
LibraryTreeView* getLibraryTreeView() {return mpLibraryTreeView;}
void openFile(QString fileName, QString encoding = Helper::utf8, bool showProgress = true, bool checkFileExists = false);
void openModelicaFile(QString fileName, QString encoding = Helper::utf8, bool showProgress = true);
void openTLMFile(QFileInfo fileInfo, bool showProgress = true);
void openTLMOrTextFile(QFileInfo fileInfo, bool showProgress = true);
void parseAndLoadModelicaText(QString modelText);
bool saveLibraryTreeItem(LibraryTreeItem *pLibraryTreeItem);
void openLibraryTreeItem(QString nameStructure);
Expand Down
25 changes: 14 additions & 11 deletions OMEdit/OMEditGUI/Modeling/ModelWidgetContainer.cpp
Expand Up @@ -2875,12 +2875,21 @@ void ModelWidget::refresh()
QApplication::restoreOverrideCursor();
}

bool ModelWidget::validateModelicaText()
/*!
* \brief ModelWidget::validateText
* Validates the text of the editor.
* \return Returns true if validation is successful otherwise return false.
*/
bool ModelWidget::validateText()
{
ModelicaTextEditor *pModelicaTextEditor = dynamic_cast<ModelicaTextEditor*>(mpEditor);
if (pModelicaTextEditor) {
return pModelicaTextEditor->validateText();
}
TLMEditor *pTLMEditor = dynamic_cast<TLMEditor*>(mpEditor);
if (pTLMEditor) {
return pTLMEditor->validateMetaModelText();
}
return false;
}

Expand Down Expand Up @@ -2961,8 +2970,7 @@ void ModelWidget::showIconView(bool checked)
{
// validate the modelica text before switching to icon view
if (checked) {
ModelicaTextEditor *pModelicaTextEditor = dynamic_cast<ModelicaTextEditor*>(mpEditor);
if (pModelicaTextEditor && !pModelicaTextEditor->validateText()) {
if (!validateText()) {
mpTextViewToolButton->setChecked(true);
return;
}
Expand Down Expand Up @@ -2991,12 +2999,7 @@ void ModelWidget::showDiagramView(bool checked)
{
// validate the modelica text before switching to diagram view
if (checked) {
if (!validateModelicaText()) {
mpTextViewToolButton->setChecked(true);
return;
}
TLMEditor *pTLMEditor = dynamic_cast<TLMEditor*>(mpEditor);
if (pTLMEditor && !pTLMEditor->validateMetaModelText()) {
if (!validateText()) {
mpTextViewToolButton->setChecked(true);
return;
}
Expand Down Expand Up @@ -3388,7 +3391,7 @@ void ModelWidgetContainer::loadPreviousViewType(ModelWidget *pModelWidget)
void ModelWidgetContainer::saveModelicaModelWidget(ModelWidget *pModelWidget)
{
/* if Modelica text is changed manually by user then validate it before saving. */
if (!pModelWidget->validateModelicaText()) {
if (!pModelWidget->validateText()) {
return;
}
mpMainWindow->getLibraryWidget()->saveLibraryTreeItem(pModelWidget->getLibraryTreeItem());
Expand Down Expand Up @@ -3537,7 +3540,7 @@ void ModelWidgetContainer::saveTotalModelWidget()
return;
}
/* if Modelica text is changed manually by user then validate it before saving. */
if (!pModelWidget->validateModelicaText()) {
if (!pModelWidget->validateText()) {
return;
}
/* save total model */
Expand Down
2 changes: 1 addition & 1 deletion OMEdit/OMEditGUI/Modeling/ModelWidgetContainer.h
Expand Up @@ -332,7 +332,7 @@ class ModelWidget : public QWidget
void getTLMConnections();
Component* getConnectorComponent(Component *pConnectorComponent, QString connectorName);
void refresh();
bool validateModelicaText();
bool validateText();
bool modelicaEditorTextChanged();
void updateModelicaText();
private:
Expand Down
4 changes: 2 additions & 2 deletions OMEdit/OMEditGUI/Util/Helper.cpp
Expand Up @@ -183,7 +183,7 @@ QString Helper::unloadClass;
QString Helper::duplicate;
QString Helper::duplicateTip;
QString Helper::unloadClassTip;
QString Helper::unloadXMLTip;
QString Helper::unloadTLMOrTextTip;
QString Helper::refresh;
QString Helper::simulate;
QString Helper::simulateTip;
Expand Down Expand Up @@ -368,7 +368,7 @@ void Helper::initHelperVariables()
Helper::duplicateTip = tr("Duplicates the item");
Helper::unloadClass = tr("Unload");
Helper::unloadClassTip = tr("Unload the Modelica class");
Helper::unloadXMLTip = tr("Unload the XML file");
Helper::unloadTLMOrTextTip = tr("Unload the TLM/Text file");
Helper::refresh = tr("Refresh");
Helper::simulate = tr("Simulate");
Helper::simulateTip = tr("Simulates the Modelica class");
Expand Down
2 changes: 1 addition & 1 deletion OMEdit/OMEditGUI/Util/Helper.h
Expand Up @@ -185,7 +185,7 @@ class Helper : public QObject
static QString duplicateTip;
static QString unloadClass;
static QString unloadClassTip;
static QString unloadXMLTip;
static QString unloadTLMOrTextTip;
static QString refresh;
static QString simulate;
static QString simulateTip;
Expand Down

0 comments on commit 04315ef

Please sign in to comment.