Skip to content

Commit

Permalink
Do not call update of model directly instead use a timer (#7381)
Browse files Browse the repository at this point in the history
The timer ensures that the update is called only once for several operations done together.
Fixes #5620
  • Loading branch information
adeas31 committed Apr 15, 2021
1 parent 379882e commit 0e0a981
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 57 deletions.
34 changes: 1 addition & 33 deletions OMEdit/OMEditLIB/Modeling/LibraryTreeWidget.cpp
Expand Up @@ -1577,36 +1577,6 @@ void LibraryTreeModel::updateLibraryTreeItemClassText(LibraryTreeItem *pLibraryT
}
}

/*!
* \brief LibraryTreeModel::updateLibraryTreeItemClassTextManually
* Updates the Parent Modelica class text after user has made changes manually in the text view.
* \param pLibraryTreeItem
* \param contents
*/
void LibraryTreeModel::updateLibraryTreeItemClassTextManually(LibraryTreeItem *pLibraryTreeItem, QString contents)
{
// set the library node not saved.
pLibraryTreeItem->setIsSaved(false);
updateLibraryTreeItem(pLibraryTreeItem);
// update the containing parent LibraryTreeItem class text.
LibraryTreeItem *pParentLibraryTreeItem = getContainingFileParentLibraryTreeItem(pLibraryTreeItem);
// we also mark the containing parent class unsaved because it is very important for saving of single file packages.
pParentLibraryTreeItem->setIsSaved(false);
updateLibraryTreeItem(pParentLibraryTreeItem);
OMCProxy *pOMCProxy = MainWindow::instance()->getOMCProxy();
pParentLibraryTreeItem->setClassText(contents);
if (pParentLibraryTreeItem->getModelWidget()) {
pParentLibraryTreeItem->getModelWidget()->setWindowTitle(QString(pParentLibraryTreeItem->getName()).append("*"));
}
// if we first updated the parent class then the child classes needs to be updated as well.
if (pParentLibraryTreeItem != pLibraryTreeItem) {
pOMCProxy->loadString(pParentLibraryTreeItem->getClassText(this), pParentLibraryTreeItem->getFileName(), Helper::utf8,
pParentLibraryTreeItem->getSaveContentsType() == LibraryTreeItem::SaveFolderStructure, false);
updateChildLibraryTreeItemClassText(pParentLibraryTreeItem, contents, pParentLibraryTreeItem->getFileName());
pParentLibraryTreeItem->setClassInformation(pOMCProxy->getClassInformation(pParentLibraryTreeItem->getNameStructure()));
}
}

/*!
* \brief LibraryTreeModel::updateChildLibraryTreeItemClassText
* Updates the class text of child LibraryTreeItems
Expand All @@ -1621,9 +1591,7 @@ void LibraryTreeModel::updateChildLibraryTreeItemClassText(LibraryTreeItem *pLib
if (pChildLibraryTreeItem && pChildLibraryTreeItem->getFileName().compare(fileName) == 0) {
pChildLibraryTreeItem->setClassInformation(MainWindow::instance()->getOMCProxy()->getClassInformation(pChildLibraryTreeItem->getNameStructure()));
readLibraryTreeItemClassTextFromText(pChildLibraryTreeItem, contents);
if (pChildLibraryTreeItem->childrenSize() > 0) {
updateChildLibraryTreeItemClassText(pChildLibraryTreeItem, contents, fileName);
}
updateChildLibraryTreeItemClassText(pChildLibraryTreeItem, contents, fileName);
}
}
}
Expand Down
1 change: 0 additions & 1 deletion OMEdit/OMEditLIB/Modeling/LibraryTreeWidget.h
Expand Up @@ -297,7 +297,6 @@ class LibraryTreeModel : public QAbstractItemModel
void removeNonExistingLibraryTreeItem(LibraryTreeItem *pLibraryTreeItem) {mNonExistingLibraryTreeItemsList.removeOne(pLibraryTreeItem);}
void updateLibraryTreeItem(LibraryTreeItem *pLibraryTreeItem);
void updateLibraryTreeItemClassText(LibraryTreeItem *pLibraryTreeItem);
void updateLibraryTreeItemClassTextManually(LibraryTreeItem *pLibraryTreeItem, QString contents);
void updateChildLibraryTreeItemClassText(LibraryTreeItem *pLibraryTreeItem, QString contents, QString fileName);
void readLibraryTreeItemClassText(LibraryTreeItem *pLibraryTreeItem);
LibraryTreeItem* getContainingFileParentLibraryTreeItem(LibraryTreeItem *pLibraryTreeItem);
Expand Down
33 changes: 20 additions & 13 deletions OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.cpp
Expand Up @@ -4294,6 +4294,15 @@ ModelWidget::ModelWidget(LibraryTreeItem* pLibraryTreeItem, ModelWidgetContainer
// drawModelInheritedClassComponents(this, StringHandler::Icon);
// getModelComponents();
// drawModelIconComponents();
/* Ticket:5620
* Hack to make the operations like moving objects with keys faster.
* We don't update the model directly instead we start a timer.
* Update the model on the timer timeout function. The timer is singleshot and ensures atleast one time run of updateModel().
* Bundles the several operations together by calling timer start function before the timer is timed out.
*/
mUpdateModelTimer.setSingleShot(true);
mUpdateModelTimer.setInterval(2000);
connect(&mUpdateModelTimer, SIGNAL(timeout()), SLOT(updateModel()));
} else if (mpLibraryTreeItem->getLibraryType() == LibraryTreeItem::OMS) {
// icon graphics framework
if (mpLibraryTreeItem->isSystemElement() || mpLibraryTreeItem->isComponentElement()) {
Expand Down Expand Up @@ -5480,7 +5489,7 @@ void ModelWidget::updateModelText()
}
} else {
setWindowTitle(QString("%1*").arg(mpLibraryTreeItem->getName()));
pLibraryTreeModel->updateLibraryTreeItemClassText(mpLibraryTreeItem);
mUpdateModelTimer.start();
}
#if !defined(WITHOUT_OSG)
// update the ThreeDViewer Browser
Expand All @@ -5490,18 +5499,6 @@ void ModelWidget::updateModelText()
#endif
}

/*!
* \brief ModelWidget::updateModelicaTextManually
* Updates the Parent Modelica class text after user has made changes manually in the text view.
* \param contents
*/
void ModelWidget::updateModelicaTextManually(QString contents)
{
setWindowTitle(QString(mpLibraryTreeItem->getName()).append("*"));
LibraryTreeModel *pLibraryTreeModel = MainWindow::instance()->getLibraryWidget()->getLibraryTreeModel();
pLibraryTreeModel->updateLibraryTreeItemClassTextManually(mpLibraryTreeItem, contents);
}

/*!
* \brief ModelWidget::updateUndoRedoActions
* Enables/disables the Undo/Redo actions based on the stack situation.
Expand Down Expand Up @@ -7534,6 +7531,16 @@ void ModelWidget::showTextView(bool checked)
updateUndoRedoActions();
}

/*!
* \brief ModelWidget::updateModel
* Slot activated when mUpdateModelTimer timeout SIGNAL is raised.
*/
void ModelWidget::updateModel()
{
LibraryTreeModel *pLibraryTreeModel = MainWindow::instance()->getLibraryWidget()->getLibraryTreeModel();
pLibraryTreeModel->updateLibraryTreeItemClassText(mpLibraryTreeItem);
}

void ModelWidget::makeFileWritAble()
{
const QString &fileName = mpLibraryTreeItem->getFileName();
Expand Down
3 changes: 2 additions & 1 deletion OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.h
Expand Up @@ -553,7 +553,6 @@ class ModelWidget : public QWidget
void clearSelection();
void updateClassAnnotationIfNeeded();
void updateModelText();
void updateModelicaTextManually(QString contents);
void updateUndoRedoActions();
bool writeCoSimulationResultFile(QString fileName);
bool writeVisualXMLFile(QString fileName, bool canWriteVisualXMLFile = false);
Expand Down Expand Up @@ -604,6 +603,7 @@ class ModelWidget : public QWidget
QMap<int, IconDiagramMap> mInheritedClassesDiagramMap;
QList<ElementInfo*> mElementsList;
QStringList mElementsAnnotationsList;
QTimer mUpdateModelTimer;

void createUndoStack();
void handleCanUndoRedoChanged();
Expand Down Expand Up @@ -637,6 +637,7 @@ private slots:
void showIconView(bool checked);
void showDiagramView(bool checked);
void showTextView(bool checked);
void updateModel();
public slots:
void makeFileWritAble();
void showDocumentationView();
Expand Down
6 changes: 2 additions & 4 deletions OMEdit/OMEditLIB/Modeling/ModelicaClassDialog.cpp
Expand Up @@ -1008,8 +1008,7 @@ void DuplicateClassDialog::syncDuplicatedModelWithOMC(LibraryTreeItem *pLibraryT
* \param pSourceLibraryTreeItem
* \param classText
*/
void DuplicateClassDialog::folderToOneFilePackage(LibraryTreeItem *pDestinationLibraryTreeItem, LibraryTreeItem *pSourceLibraryTreeItem,
QString *classText)
void DuplicateClassDialog::folderToOneFilePackage(LibraryTreeItem *pDestinationLibraryTreeItem, LibraryTreeItem *pSourceLibraryTreeItem, QString *classText)
{
LibraryTreeModel *pLibraryTreeModel = MainWindow::instance()->getLibraryWidget()->getLibraryTreeModel();
for (int i = 0 ; i < pSourceLibraryTreeItem->childrenSize() ; i++) {
Expand All @@ -1022,8 +1021,7 @@ void DuplicateClassDialog::folderToOneFilePackage(LibraryTreeItem *pDestinationL
QString lineToRemove = QString("within %1;").arg(pDestinationLibraryTreeItem->getNameStructure());
*classText += StringHandler::removeLine(diffClassText, lineToRemove) + "\n";
} else {
QString afterChildClassText = MainWindow::instance()->getOMCProxy()->listFile(pDestinationChildLibraryTreeItem->getNameStructure(),
false);
QString afterChildClassText = MainWindow::instance()->getOMCProxy()->listFile(pDestinationChildLibraryTreeItem->getNameStructure(), false);
QString beforeChildClassText = pSourceChildLibraryTreeItem->getClassText(pLibraryTreeModel);
QString parentClassText = MainWindow::instance()->getOMCProxy()->diffModelicaFileListings(beforeChildClassText, afterChildClassText);
QString lineToRemove = QString("within %1;").arg(pDestinationLibraryTreeItem->getNameStructure());
Expand Down
8 changes: 4 additions & 4 deletions OMEdit/OMEditLIB/OMC/OMCProxy.cpp
Expand Up @@ -394,8 +394,8 @@ void OMCProxy::logResponse(QString command, QString response, double elapsed)
if (isLoggingEnabled()) {
QString firstLine("");
for (int i = 0; i < command.length(); i++) {
if (command[i] != '\n') {
firstLine.append(command[i]);
if (command.at(i) != '\n') {
firstLine.append(command.at(i));
} else {
break;
}
Expand Down Expand Up @@ -1853,15 +1853,15 @@ QString OMCProxy::listFile(QString className, bool nestedClasses)
* \param after
* \return
*/
QString OMCProxy::diffModelicaFileListings(QString before, QString after)
QString OMCProxy::diffModelicaFileListings(const QString &before, const QString &after)
{
QString result = "";
// check if both strings are same
// only use the diffModelicaFileListings when preserve text indentation settings is true
if (before.compare(after) != 0 && OptionsDialog::instance()->getModelicaEditorPage()->getPreserveTextIndentationCheckBox()->isChecked()) {
QString escapedBefore = StringHandler::escapeString(before);
QString escapedAfter = StringHandler::escapeString(after);
sendCommand("diffModelicaFileListings(\"" + escapedBefore + "\", \"" + escapedAfter + "\", OpenModelica.Scripting.DiffFormat.plain)");
sendCommand(QString("diffModelicaFileListings(\"%1\", \"%2\", OpenModelica.Scripting.DiffFormat.plain)").arg(escapedBefore, escapedAfter));
result = StringHandler::unparse(getResult());
/* ticket:5413 Don't show the error of diffModelicaFileListings
* Instead show the following warning. The developers can read the actual error message from the log file.
Expand Down
2 changes: 1 addition & 1 deletion OMEdit/OMEditLIB/OMC/OMCProxy.h
Expand Up @@ -172,7 +172,7 @@ class OMCProxy : public QObject
bool saveTotalModel(QString fileName, QString className);
QString list(QString className);
QString listFile(QString className, bool nestedClasses = true);
QString diffModelicaFileListings(QString before, QString after);
QString diffModelicaFileListings(const QString &before, const QString &after);
QString instantiateModel(QString className);
bool addClassAnnotation(QString className, QString annotation);
QString getDefaultComponentName(QString className);
Expand Down

0 comments on commit 0e0a981

Please sign in to comment.