Skip to content

Commit

Permalink
Fixed the saving of single file packages.
Browse files Browse the repository at this point in the history
  • Loading branch information
adeas31 committed Oct 13, 2015
1 parent 8d35fe6 commit a777e79
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 77 deletions.
48 changes: 22 additions & 26 deletions OMEdit/OMEditGUI/Modeling/LibraryTreeWidget.cpp
Expand Up @@ -365,29 +365,21 @@ void LibraryTreeItem::setClassInformation(OMCInterface::getClassInformation_res
{
if (mLibraryType == LibraryTreeItem::Modelica) {
mClassInformation = classInformation;
if (getFileName().isEmpty()) {
if (!isFilePathValid()) {
setFileName(classInformation.fileName);
}
setReadOnly(classInformation.fileReadOnly);
}
}

/*!
* \brief LibraryTreeItem::setFileName
* \param fileName
* Sets the LibraryTreeItem file name.
* \brief LibraryTreeItem::isFilePathValid
* Returns true if file path is valid file location and not modelica class name.
* \return
*/
void LibraryTreeItem::setFileName(QString fileName)
{
if (mLibraryType == LibraryTreeItem::Modelica) {
/* Since now we set the fileName via loadString() & parseString() so might get filename as className/<interactive>.
* We only set the fileName field if returned value is really a file path.
*/
mFileName = fileName.endsWith(".mo") ? fileName : "";
mFileName = mFileName.replace('\\', '/');
} else {
mFileName = fileName;
}
bool LibraryTreeItem::isFilePathValid() {
// Since now we set the fileName via loadString() & parseString() so might get filename as className/<interactive>.
return QFile::exists(mFileName);
}

/*!
Expand Down Expand Up @@ -1063,7 +1055,7 @@ LibraryTreeItem* LibraryTreeModel::createLibraryTreeItem(QString name, LibraryTr
if (pLibraryTreeItem && pLibraryTreeItem->isNonExisting()) {
wasNonExisting = true;
pLibraryTreeItem->setSystemLibrary(pParentLibraryTreeItem == mpRootLibraryTreeItem ? isSystemLibrary : pParentLibraryTreeItem->isSystemLibrary());
createNonExistingLibraryTreeItem(pLibraryTreeItem, pParentLibraryTreeItem);
createNonExistingLibraryTreeItem(pLibraryTreeItem, pParentLibraryTreeItem, isSaved);
// read the LibraryTreeItem text
readLibraryTreeItemClassText(pLibraryTreeItem);
createLibraryTreeItems(pLibraryTreeItem);
Expand Down Expand Up @@ -1176,6 +1168,7 @@ void LibraryTreeModel::createNonExistingLibraryTreeItem(LibraryTreeItem *pLibrar
{
pLibraryTreeItem->setParent(pParentLibraryTreeItem);
OMCProxy *pOMCProxy = mpLibraryWidget->getMainWindow()->getOMCProxy();
pLibraryTreeItem->setFileName("");
pLibraryTreeItem->setClassInformation(pOMCProxy->getClassInformation(pLibraryTreeItem->getNameStructure()));
pLibraryTreeItem->setIsSaved(isSaved);
pLibraryTreeItem->setIsProtected(pOMCProxy->isProtectedClass(pParentLibraryTreeItem->getNameStructure(), pLibraryTreeItem->getName()));
Expand Down Expand Up @@ -1225,7 +1218,7 @@ void LibraryTreeModel::updateLibraryTreeItem(LibraryTreeItem *pLibraryTreeItem)
*/
void LibraryTreeModel::readLibraryTreeItemClassText(LibraryTreeItem *pLibraryTreeItem)
{
if (pLibraryTreeItem->getFileName().isEmpty()) {
if (!pLibraryTreeItem->isFilePathValid()) {
// If class is top level then
if (pLibraryTreeItem->isTopLevel()) {
if (pLibraryTreeItem->getLibraryType() == LibraryTreeItem::Modelica) {
Expand Down Expand Up @@ -1327,8 +1320,9 @@ void LibraryTreeModel::updateLibraryTreeItemClassText(LibraryTreeItem *pLibraryT
}
// if we first updated the parent class then the child classes needs to be updated as well.
if (pParentLibraryTreeItem != pLibraryTreeItem) {
pOMCProxy->loadString(pParentLibraryTreeItem->getClassText(), pParentLibraryTreeItem->getNameStructure(), Helper::utf8, false);
pOMCProxy->loadString(pParentLibraryTreeItem->getClassText(), pParentLibraryTreeItem->getFileName(), Helper::utf8, false);
updateChildLibraryTreeItemClassText(pParentLibraryTreeItem, contents, pParentLibraryTreeItem->getFileName());
pParentLibraryTreeItem->setClassInformation(pOMCProxy->getClassInformation(pParentLibraryTreeItem->getNameStructure()));
}
}

Expand Down Expand Up @@ -2551,10 +2545,8 @@ bool LibraryWidget::saveModelicaLibraryTreeItem(LibraryTreeItem *pLibraryTreeIte
bool result = false;
LibraryTreeItem *pParentLibraryTreeItem = mpLibraryTreeModel->getContainingParentLibraryTreeItem(pLibraryTreeItem);
result = saveLibraryTreeItemHelper(pParentLibraryTreeItem);
if (result && !pLibraryTreeItem->isTopLevel()) {
setChildLibraryTreeItemsSaved(pParentLibraryTreeItem);
}
if (result) {
setChildLibraryTreeItemsSaved(pParentLibraryTreeItem);
getMainWindow()->addRecentFile(pLibraryTreeItem->getFileName(), Helper::utf8);
/* We need to load the file again so that the line number information for model_info.xml is correct.
* Update to AST (makes source info WRONG), saving it (source info STILL WRONG), reload it (and omc knows the new lines)
Expand Down Expand Up @@ -2755,7 +2747,7 @@ bool LibraryWidget::saveLibraryTreeItemHelper(LibraryTreeItem *pLibraryTreeItem)
{
mpMainWindow->getStatusBar()->showMessage(QString(tr("Saving")).append(" ").append(pLibraryTreeItem->getNameStructure()));
QString fileName;
if (pLibraryTreeItem->getFileName().isEmpty()) {
if (!pLibraryTreeItem->isFilePathValid()) {
QString name = pLibraryTreeItem->getName();
fileName = StringHandler::getSaveFileName(this, QString(Helper::applicationName).append(" - ").append(tr("Save File")), NULL,
Helper::omFileTypes, NULL, "mo", &name);
Expand All @@ -2772,19 +2764,23 @@ bool LibraryWidget::saveLibraryTreeItemHelper(LibraryTreeItem *pLibraryTreeItem)
return false;
}
}
mpMainWindow->getOMCProxy()->setSourceFile(pLibraryTreeItem->getNameStructure(), fileName);
//mpMainWindow->getOMCProxy()->setSourceFile(pLibraryTreeItem->getNameStructure(), fileName);
// save the class
QFile file(fileName);
if (file.open(QIODevice::WriteOnly)) {
QTextStream textStream(&file);
textStream.setCodec(Helper::utf8.toStdString().data());
textStream.setGenerateByteOrderMark(false);
textStream << pLibraryTreeItem->getModelWidget()->getEditor()->getPlainTextEdit()->toPlainText();
if (pLibraryTreeItem->getModelWidget()->getEditor()) {
textStream << pLibraryTreeItem->getModelWidget()->getEditor()->getPlainTextEdit()->toPlainText();
} else {
textStream << pLibraryTreeItem->getClassText();
}
file.close();
/* mark the file as saved and update the labels. */
pLibraryTreeItem->setIsSaved(true);
pLibraryTreeItem->setFileName(fileName);
if (pLibraryTreeItem->getModelWidget()) {
if (pLibraryTreeItem->getModelWidget() && pLibraryTreeItem->getModelWidget()->isLoadedWidgetComponents()) {
pLibraryTreeItem->getModelWidget()->setWindowTitle(pLibraryTreeItem->getNameStructure());
pLibraryTreeItem->getModelWidget()->setModelFilePathLabel(fileName);
}
Expand All @@ -2808,7 +2804,7 @@ void LibraryWidget::setChildLibraryTreeItemsSaved(LibraryTreeItem *pLibraryTreeI
LibraryTreeItem *pChildLibraryTreeItem = pLibraryTreeItem->child(i);
pChildLibraryTreeItem->setIsSaved(true);
pChildLibraryTreeItem->setFileName(pLibraryTreeItem->getFileName());
if (pChildLibraryTreeItem->getModelWidget()) {
if (pChildLibraryTreeItem->getModelWidget() && pChildLibraryTreeItem->getModelWidget()->isLoadedWidgetComponents()) {
pChildLibraryTreeItem->getModelWidget()->setWindowTitle(pChildLibraryTreeItem->getNameStructure());
pChildLibraryTreeItem->getModelWidget()->setModelFilePathLabel(pLibraryTreeItem->getFileName());
}
Expand Down
5 changes: 3 additions & 2 deletions OMEdit/OMEditGUI/Modeling/LibraryTreeWidget.h
Expand Up @@ -99,8 +99,9 @@ class LibraryTreeItem : public QObject
const QString& getNameStructure() {return mNameStructure;}
void setClassInformation(OMCInterface::getClassInformation_res classInformation);
OMCInterface::getClassInformation_res getClassInformation() {return mClassInformation;}
void setFileName(QString fileName);
void setFileName(QString fileName) {mFileName = fileName;}
const QString& getFileName() {return mFileName;}
bool isFilePathValid();
void setReadOnly(bool readOnly) {mReadOnly = readOnly;}
bool isReadOnly() {return mReadOnly;}
void setIsSaved(bool isSaved) {mIsSaved = isSaved;}
Expand All @@ -109,9 +110,9 @@ class LibraryTreeItem : public QObject
bool isProtected() {return mIsProtected;}
void setIsDocumentationClass(bool documentationClass) {mDocumentationClass = documentationClass;}
bool isDocumentationClass() {return mDocumentationClass;}
void setSaveContentsType(LibraryTreeItem::SaveContentsType saveContentsType) {mSaveContentsType = saveContentsType;}
StringHandler::ModelicaClasses getRestriction() {return StringHandler::getModelicaClassType(mClassInformation.restriction);}
bool isPartial() {return mClassInformation.partialPrefix;}
void setSaveContentsType(LibraryTreeItem::SaveContentsType saveContentsType) {mSaveContentsType = saveContentsType;}
SaveContentsType getSaveContentsType() {return mSaveContentsType;}
void setToolTip(QString toolTip) {mToolTip = toolTip;}
void setIcon(QIcon icon) {mIcon = icon;}
Expand Down
14 changes: 14 additions & 0 deletions OMEdit/OMEditGUI/Modeling/ModelWidgetContainer.cpp
Expand Up @@ -2292,6 +2292,20 @@ void ModelWidget::loadModelWidget()
drawModelInheritedConnections();
getModelConnections();
mpUndoStack->clear();
if (mloadWidgetComponents) {
// set Project Status Bar lables
mpReadOnlyLabel->setText(mpLibraryTreeItem->isReadOnly() ? Helper::readOnly : tr("Writable"));
setModelFilePathLabel(mpLibraryTreeItem->getFileName());
// documentation view tool button
mpFileLockToolButton->setIcon(QIcon(mpLibraryTreeItem->isReadOnly() ? ":/Resources/icons/lock.svg" : ":/Resources/icons/unlock.svg"));
mpFileLockToolButton->setText(mpLibraryTreeItem->isReadOnly() ? tr("Make writable") : tr("File is writable"));
mpFileLockToolButton->setToolTip(mpFileLockToolButton->text());
mpFileLockToolButton->setEnabled(mpLibraryTreeItem->isReadOnly() && !mpLibraryTreeItem->isSystemLibrary());
mpModelicaTypeLabel->setText(StringHandler::getModelicaClassType(mpLibraryTreeItem->getRestriction()));
// modelica text editor
ModelicaTextEditor *pModelicaTextEditor = dynamic_cast<ModelicaTextEditor*>(mpEditor);
pModelicaTextEditor->setPlainText(mpLibraryTreeItem->getClassText());
}
}
}

Expand Down
5 changes: 3 additions & 2 deletions OMEdit/OMEditGUI/Modeling/ModelWidgetContainer.h
Expand Up @@ -355,10 +355,11 @@ class ModelWidget : public QWidget
GraphicsView* getIconGraphicsView() {return mpIconGraphicsView;}
QUndoStack* getUndoStack() {return mpUndoStack;}
BaseEditor* getEditor() {return mpEditor;}
void setReloadNeeded(bool reloadNeeded) {mReloadNeeded = reloadNeeded;}
bool isReloadNeeded() {return mReloadNeeded;}
void setModelFilePathLabel(QString path) {mpModelFilePathLabel->setText(path);}
Label* getCursorPositionLabel() {return mpCursorPositionLabel;}
bool isLoadedWidgetComponents() {return mloadWidgetComponents;}
void setReloadNeeded(bool reloadNeeded) {mReloadNeeded = reloadNeeded;}
bool isReloadNeeded() {return mReloadNeeded;}
void loadModelWidget();
void addInheritedClass(LibraryTreeItem *pLibraryTreeItem);
void removeInheritedClass(InheritedClass *pInheritedClass) {mInheritedClassesList.removeOne(pInheritedClass);}
Expand Down
46 changes: 21 additions & 25 deletions OMEdit/OMEditGUI/Modeling/ModelicaClassDialog.cpp
Expand Up @@ -287,33 +287,32 @@ void ModelicaClassDialog::createModelicaClass()
return;
}
/* if extends class doesn't exist. */
LibraryTreeModel *pLibraryTreeModel = mpMainWindow->getLibraryWidget()->getLibraryTreeModel();
LibraryTreeItem *pExtendsLibraryTreeItem = 0;
if (!mpExtendsClassTextBox->text().isEmpty()) {
if (!mpMainWindow->getOMCProxy()->existClass(mpExtendsClassTextBox->text())) {
QMessageBox::critical(this, QString(Helper::applicationName).append(" - ").append(Helper::error), GUIMessages::getMessage(
GUIMessages::EXTENDS_CLASS_NOT_FOUND).arg(mpExtendsClassTextBox->text()), Helper::ok);
pExtendsLibraryTreeItem = pLibraryTreeModel->findLibraryTreeItem(mpExtendsClassTextBox->text());
if (!pExtendsLibraryTreeItem) {
QMessageBox::critical(this, QString(Helper::applicationName).append(" - ").append(Helper::error),
GUIMessages::getMessage(GUIMessages::EXTENDS_CLASS_NOT_FOUND).arg(mpExtendsClassTextBox->text()), Helper::ok);
return;
}
}
/* if insert in class doesn't exist. */
LibraryTreeItem *pParentLibraryTreeItem = pLibraryTreeModel->getRootLibraryTreeItem();
if (!mpParentClassTextBox->text().isEmpty()) {
if (!mpMainWindow->getOMCProxy()->existClass(mpParentClassTextBox->text())) {
QMessageBox::critical(this, QString(Helper::applicationName).append(" - ").append(Helper::error), GUIMessages::getMessage(
GUIMessages::INSERT_IN_CLASS_NOT_FOUND).arg(mpParentClassTextBox->text()), Helper::ok);
pParentLibraryTreeItem = pLibraryTreeModel->findLibraryTreeItem(mpParentClassTextBox->text());
if (!pParentLibraryTreeItem) {
QMessageBox::critical(this, QString(Helper::applicationName).append(" - ").append(Helper::error),
GUIMessages::getMessage(GUIMessages::INSERT_IN_CLASS_NOT_FOUND).arg(mpParentClassTextBox->text()), Helper::ok);
return;
}
}
/* if insert in class is system library. */
LibraryTreeModel *pLibraryTreeModel = mpMainWindow->getLibraryWidget()->getLibraryTreeModel();
LibraryTreeItem *pParentLibraryTreeItem = 0;
if (!mpParentClassTextBox->text().isEmpty()) {
pParentLibraryTreeItem = pLibraryTreeModel->findLibraryTreeItem(mpParentClassTextBox->text());
}
if (pParentLibraryTreeItem) {
if (pParentLibraryTreeItem->isSystemLibrary()) {
QMessageBox::critical(this, QString(Helper::applicationName).append(" - ").append(Helper::error), GUIMessages::getMessage(
GUIMessages::INSERT_IN_SYSTEM_LIBRARY_NOT_ALLOWED).arg(mpParentClassTextBox->text()), Helper::ok);
return;
}
if (pParentLibraryTreeItem && pParentLibraryTreeItem->isSystemLibrary()) {
QMessageBox::critical(this, QString(Helper::applicationName).append(" - ").append(Helper::error),
GUIMessages::getMessage(GUIMessages::INSERT_IN_SYSTEM_LIBRARY_NOT_ALLOWED)
.arg(mpParentClassTextBox->text()), Helper::ok);
return;
}
QString model, parentPackage;
if (mpParentClassTextBox->text().isEmpty()) {
Expand All @@ -335,29 +334,26 @@ void ModelicaClassDialog::createModelicaClass()
modelicaClass.append(mpPartialCheckBox->isChecked() ? "partial " : "");
modelicaClass.append(mpSpecializationComboBox->currentText().toLower());
if (mpParentClassTextBox->text().isEmpty()) {
if (!mpMainWindow->getOMCProxy()->createClass(modelicaClass, mpNameTextBox->text().trimmed(), mpExtendsClassTextBox->text().trimmed())) {
if (!mpMainWindow->getOMCProxy()->createClass(modelicaClass, mpNameTextBox->text().trimmed(), pExtendsLibraryTreeItem)) {
QMessageBox::critical(this, QString(Helper::applicationName).append(" - ").append(Helper::error), GUIMessages::getMessage(
GUIMessages::ERROR_OCCURRED).arg(mpMainWindow->getOMCProxy()->getErrorString()).append("\n\n").
append(GUIMessages::getMessage(GUIMessages::NO_OPENMODELICA_KEYWORDS)), Helper::ok);
return;
}
} else {
if (!mpMainWindow->getOMCProxy()->createSubClass(modelicaClass, mpNameTextBox->text().trimmed(), mpParentClassTextBox->text().trimmed(), mpExtendsClassTextBox->text().trimmed())) {
if (!mpMainWindow->getOMCProxy()->createSubClass(modelicaClass, mpNameTextBox->text().trimmed(), pParentLibraryTreeItem, pExtendsLibraryTreeItem)) {
QMessageBox::critical(this, QString(Helper::applicationName).append(" - ").append(Helper::error), GUIMessages::getMessage(
GUIMessages::ERROR_OCCURRED).arg(mpMainWindow->getOMCProxy()->getErrorString()).append("\n\n").
append(GUIMessages::getMessage(GUIMessages::NO_OPENMODELICA_KEYWORDS)), Helper::ok);
return;
}
}
//open the new tab in central widget and add the model to library tree.
// open the new tab in central widget and add the model to library tree.
LibraryTreeItem *pLibraryTreeItem;
bool wasNonExisting = false;
if (pParentLibraryTreeItem) {
pLibraryTreeItem = pLibraryTreeModel->createLibraryTreeItem(mpNameTextBox->text().trimmed(), pParentLibraryTreeItem, wasNonExisting, false, false, true);
} else {
pLibraryTreeItem = pLibraryTreeModel->createLibraryTreeItem(mpNameTextBox->text().trimmed(), pLibraryTreeModel->getRootLibraryTreeItem(), wasNonExisting, false, false, true);
}
pLibraryTreeItem = pLibraryTreeModel->createLibraryTreeItem(mpNameTextBox->text().trimmed(), pParentLibraryTreeItem, wasNonExisting, false, false, true);
pLibraryTreeItem->setSaveContentsType(mpSaveContentsInOneFileCheckBox->isChecked() ? LibraryTreeItem::SaveInOneFile : LibraryTreeItem::SaveFolderStructure);
pLibraryTreeModel->updateLibraryTreeItemClassText(pLibraryTreeItem);
if (wasNonExisting) {
// load the LibraryTreeItem pixmap
pLibraryTreeModel->loadLibraryTreeItemPixmap(pLibraryTreeItem);
Expand Down
47 changes: 27 additions & 20 deletions OMEdit/OMEditGUI/OMC/OMCProxy.cpp
Expand Up @@ -1364,38 +1364,45 @@ QList<QString> OMCProxy::parseString(QString value, QString fileName)
}

/*!
Creates a new class in OMC.
\param type - the class type.
\param className - the class name.
\return true on success.
*/
bool OMCProxy::createClass(QString type, QString className, QString extendsClass)
* \brief OMCProxy::createClass
* Creates a new class in OMC.
* \param type - the class type.
* \param className - the class name.
* \param pExtendsLibraryTreeItem - the extends class.
* \return
*/
bool OMCProxy::createClass(QString type, QString className, LibraryTreeItem *pExtendsLibraryTreeItem)
{
QString expression;
if (extendsClass.isEmpty()) {
expression = type + " " + className + " end " + className + ";";
if (!pExtendsLibraryTreeItem) {
expression = QString("%1 %2 end %3;").arg(type).arg(className).arg(className);
} else {
expression = type + " " + className + " extends " + extendsClass + "; end " + className + ";";
expression = QString("%1 %2 extends %3; end %4;").arg(type).arg(className).arg(pExtendsLibraryTreeItem->getNameStructure())
.arg(className);
}
return loadString(expression, className, Helper::utf8, false);
}

/*!
Creates a new sub class in OMC.
\param type - the class type.
\param className - the class name.
\param parentClassName - the parent class name.
\return true on success.
*/
bool OMCProxy::createSubClass(QString type, QString className, QString parentClassName, QString extendsClass)
* \brief OMCProxy::createSubClass
* Creates a new sub class in OMC.
* \param type - the class type.
* \param className - the class name.
* \param pParentLibraryTreeItem - the parent class.
* \param pExtendsLibraryTreeItem - the extends class.
* \return
*/
bool OMCProxy::createSubClass(QString type, QString className, LibraryTreeItem *pParentLibraryTreeItem,
LibraryTreeItem *pExtendsLibraryTreeItem)
{
QString expression;
if (extendsClass.isEmpty()) {
expression = "within " + parentClassName + "; " + type + " " + className + " end " + className + ";";
if (!pExtendsLibraryTreeItem) {
expression = QString("within %1; %2 %3 end %4;").arg(pParentLibraryTreeItem->getNameStructure()).arg(type).arg(className).arg(className);
} else {
expression = "within " + parentClassName + "; " + type + " " + className + " extends " + extendsClass + "; end " + className + ";";
expression = QString("within %1; %2 %3 extends %4; end %5;").arg(pParentLibraryTreeItem->getNameStructure()).arg(type).arg(className)
.arg(pExtendsLibraryTreeItem->getNameStructure()).arg(className);
}
return loadString(expression, StringHandler::getFirstWordBeforeDot(parentClassName), Helper::utf8, false);
return loadString(expression, pParentLibraryTreeItem->getClassInformation().fileName, Helper::utf8, false);
}

/*!
Expand Down

0 comments on commit a777e79

Please sign in to comment.