Skip to content

Commit

Permalink
beginRemoveRows takes parent item as argument
Browse files Browse the repository at this point in the history
  • Loading branch information
adeas31 committed Dec 9, 2020
1 parent c58169d commit f102bad
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 155 deletions.
Expand Up @@ -526,7 +526,7 @@ void BreakpointsTreeModel::removeBreakpoint(BreakpointTreeItem *pBreakpointTreeI
}
// remove the breakpoint from the tree.
int row = pBreakpointTreeItem->row();
beginRemoveRows(breakpointTreeItemIndex(pBreakpointTreeItem), row, row);
beginRemoveRows(breakpointTreeItemIndex(pBreakpointTreeItem->parent()), row, row);
pBreakpointTreeItem->removeChildren();
BreakpointTreeItem *pParentBreakpointTreeItem = pBreakpointTreeItem->parent();
pParentBreakpointTreeItem->removeChild(pBreakpointTreeItem);
Expand Down
17 changes: 8 additions & 9 deletions OMEdit/OMEditLIB/Debugger/Locals/LocalsWidget.cpp
Expand Up @@ -532,24 +532,23 @@ void LocalsTreeModel::removeLocalItem(LocalsTreeItem *pLocalsTreeItem)
{
QModelIndex index = localsTreeItemIndex(pLocalsTreeItem);
int row = index.row();
beginRemoveRows(index, row, row);
beginRemoveRows(localsTreeItemIndex(pLocalsTreeItem->parent()), row, row);
pLocalsTreeItem->removeChildren();
endRemoveRows();
LocalsTreeItem *pParentLocalsTreeItem = pLocalsTreeItem->parent();
pParentLocalsTreeItem->removeChild(pLocalsTreeItem);
pLocalsTreeItem->deleteLater();
endRemoveRows();
}

void LocalsTreeModel::removeLocalItems()
{
int n = mpRootLocalsTreeItem->getChildren().size();
if (n == 0) {
return;
const int n = mpRootLocalsTreeItem->getChildren().size();
if (n > 0) {
QModelIndex index = localsTreeItemIndex(mpRootLocalsTreeItem);
beginRemoveRows(index, 0, n - 1);
mpRootLocalsTreeItem->removeChildren();
endRemoveRows();
}
QModelIndex index = localsTreeItemIndex(mpRootLocalsTreeItem);
beginRemoveRows(index, 0, n - 1);
mpRootLocalsTreeItem->removeChildren();
endRemoveRows();
}

/*!
Expand Down
153 changes: 14 additions & 139 deletions OMEdit/OMEditLIB/Modeling/LibraryTreeWidget.cpp
Expand Up @@ -1884,31 +1884,7 @@ bool LibraryTreeModel::unloadClass(LibraryTreeItem *pLibraryTreeItem, bool askQu
* If deleteClass is successful remove the class from Library Browser and delete the corresponding ModelWidget.
*/
if (MainWindow::instance()->getOMCProxy()->deleteClass(pLibraryTreeItem->getNameStructure())) {
/* QSortFilterProxy::filterAcceptRows changes the expand/collapse behavior of indexes or I am using it in some stupid way.
* If index is expanded and we delete it then the next sibling index automatically becomes expanded.
* The following code overcomes this issue. It stores the next index expand state and then apply it after deletion.
*/
int row = pLibraryTreeItem->row();
LibraryTreeItem *pNextLibraryTreeItem = 0;
bool expandState = false;
if (pLibraryTreeItem->parent()->childrenSize() > row + 1) {
pNextLibraryTreeItem = pLibraryTreeItem->parent()->child(row + 1);
QModelIndex modelIndex = libraryTreeItemIndex(pNextLibraryTreeItem);
QModelIndex proxyIndex = mpLibraryWidget->getLibraryTreeProxyModel()->mapFromSource(modelIndex);
expandState = mpLibraryWidget->getLibraryTreeView()->isExpanded(proxyIndex);
}
// remove the LibraryTreeItem from Libraries Browser
beginRemoveRows(libraryTreeItemIndex(pLibraryTreeItem), row, row);
// unload the LibraryTreeItem children if any and then unload the LibraryTreeItem.
unloadClassChildren(pLibraryTreeItem);
endRemoveRows();
if (pNextLibraryTreeItem) {
QModelIndex modelIndex = libraryTreeItemIndex(pNextLibraryTreeItem);
QModelIndex proxyIndex = mpLibraryWidget->getLibraryTreeProxyModel()->mapFromSource(modelIndex);
mpLibraryWidget->getLibraryTreeView()->setExpanded(proxyIndex, expandState);
}
/* Update the model switcher toolbar button. */
MainWindow::instance()->updateModelSwitcherMenu(0);
removeLibraryTreeItem(pLibraryTreeItem);
if (!pLibraryTreeItem->isTopLevel()) {
LibraryTreeItem *pContainingFileParentLibraryTreeItem = getContainingFileParentLibraryTreeItem(pLibraryTreeItem);
// if we unload in a package saved in one file strucutre then we should update its containing file item text.
Expand Down Expand Up @@ -1959,31 +1935,7 @@ bool LibraryTreeModel::unloadCompositeModelOrTextFile(LibraryTreeItem *pLibraryT
return false;
}
}
/* QSortFilterProxy::filterAcceptRows changes the expand/collapse behavior of indexes or I am using it in some stupid way.
* If index is expanded and we delete it then the next sibling index automatically becomes expanded.
* The following code overcomes this issue. It stores the next index expand state and then apply it after deletion.
*/
int row = pLibraryTreeItem->row();
LibraryTreeItem *pNextLibraryTreeItem = 0;
bool expandState = false;
if (pLibraryTreeItem->parent()->childrenSize() > row + 1) {
pNextLibraryTreeItem = pLibraryTreeItem->parent()->child(row + 1);
QModelIndex modelIndex = libraryTreeItemIndex(pNextLibraryTreeItem);
QModelIndex proxyIndex = mpLibraryWidget->getLibraryTreeProxyModel()->mapFromSource(modelIndex);
expandState = mpLibraryWidget->getLibraryTreeView()->isExpanded(proxyIndex);
}
// remove the LibraryTreeItem from Libraries Browser
beginRemoveRows(libraryTreeItemIndex(pLibraryTreeItem), row, row);
// unload the LibraryTreeItem children if any and then unload the LibraryTreeItem.
unloadFileChildren(pLibraryTreeItem);
endRemoveRows();
if (pNextLibraryTreeItem) {
QModelIndex modelIndex = libraryTreeItemIndex(pNextLibraryTreeItem);
QModelIndex proxyIndex = mpLibraryWidget->getLibraryTreeProxyModel()->mapFromSource(modelIndex);
mpLibraryWidget->getLibraryTreeView()->setExpanded(proxyIndex, expandState);
}
/* Update the model switcher toolbar button. */
MainWindow::instance()->updateModelSwitcherMenu(0);
removeLibraryTreeItem(pLibraryTreeItem);
return true;
}

Expand Down Expand Up @@ -2020,31 +1972,7 @@ bool LibraryTreeModel::unloadOMSModel(LibraryTreeItem *pLibraryTreeItem, bool do
}
// unload OMSimulator model
if (!doDelete || OMSProxy::instance()->omsDelete(pLibraryTreeItem->getNameStructure())) {
/* QSortFilterProxy::filterAcceptRows changes the expand/collapse behavior of indexes or I am using it in some stupid way.
* If index is expanded and we delete it then the next sibling index automatically becomes expanded.
* The following code overcomes this issue. It stores the next index expand state and then apply it after deletion.
*/
int row = pLibraryTreeItem->row();
LibraryTreeItem *pNextLibraryTreeItem = 0;
bool expandState = false;
if (pLibraryTreeItem->parent()->childrenSize() > row + 1) {
pNextLibraryTreeItem = pLibraryTreeItem->parent()->child(row + 1);
QModelIndex modelIndex = libraryTreeItemIndex(pNextLibraryTreeItem);
QModelIndex proxyIndex = mpLibraryWidget->getLibraryTreeProxyModel()->mapFromSource(modelIndex);
expandState = mpLibraryWidget->getLibraryTreeView()->isExpanded(proxyIndex);
}
// remove the LibraryTreeItem from Libraries Browser
beginRemoveRows(libraryTreeItemIndex(pLibraryTreeItem), row, row);
// unload the LibraryTreeItem children if any and then unload the LibraryTreeItem.
unloadFileChildren(pLibraryTreeItem);
endRemoveRows();
if (pNextLibraryTreeItem) {
QModelIndex modelIndex = libraryTreeItemIndex(pNextLibraryTreeItem);
QModelIndex proxyIndex = mpLibraryWidget->getLibraryTreeProxyModel()->mapFromSource(modelIndex);
mpLibraryWidget->getLibraryTreeView()->setExpanded(proxyIndex, expandState);
}
/* Update the model switcher toolbar button. */
MainWindow::instance()->updateModelSwitcherMenu(0);
removeLibraryTreeItem(pLibraryTreeItem);
return true;
} else {
return false;
Expand Down Expand Up @@ -2158,19 +2086,10 @@ bool LibraryTreeModel::unloadLibraryTreeItem(LibraryTreeItem *pLibraryTreeItem,
* If deleteClass is successful remove the class from Library Browser.
*/
if (!doDeleteClass || MainWindow::instance()->getOMCProxy()->deleteClass(pLibraryTreeItem->getNameStructure())) {
/* QSortFilterProxy::filterAcceptRows changes the expand/collapse behavior of indexes or I am using it in some stupid way.
* If index is expanded and we delete it then the next sibling index automatically becomes expanded.
* The following code overcomes this issue. It stores the next index expand state and then apply it after deletion.
*/
int row = pLibraryTreeItem->row();
LibraryTreeItem *pNextLibraryTreeItem = 0;
bool expandState = false;
if (pLibraryTreeItem->parent()->childrenSize() > row + 1) {
pNextLibraryTreeItem = pLibraryTreeItem->parent()->child(row + 1);
QModelIndex modelIndex = libraryTreeItemIndex(pNextLibraryTreeItem);
QModelIndex proxyIndex = mpLibraryWidget->getLibraryTreeProxyModel()->mapFromSource(modelIndex);
expandState = mpLibraryWidget->getLibraryTreeView()->isExpanded(proxyIndex);
}
// remove the LibraryTreeItem from Libraries Browser
row = pLibraryTreeItem->row();
beginRemoveRows(libraryTreeItemIndex(pLibraryTreeItem->parent()), row, row);
int i = 0;
while(i < pLibraryTreeItem->childrenSize()) {
unloadClassChildren(pLibraryTreeItem->child(i));
Expand All @@ -2185,16 +2104,8 @@ bool LibraryTreeModel::unloadLibraryTreeItem(LibraryTreeItem *pLibraryTreeItem,
// notify the inherits classes
pLibraryTreeItem->emitUnLoaded();
addNonExistingLibraryTreeItem(pLibraryTreeItem);
// remove the LibraryTreeItem from Libraries Browser
row = pLibraryTreeItem->row();
beginRemoveRows(libraryTreeItemIndex(pLibraryTreeItem), row, row);
pLibraryTreeItem->parent()->removeChild(pLibraryTreeItem);
endRemoveRows();
if (pNextLibraryTreeItem) {
QModelIndex modelIndex = libraryTreeItemIndex(pNextLibraryTreeItem);
QModelIndex proxyIndex = mpLibraryWidget->getLibraryTreeProxyModel()->mapFromSource(modelIndex);
mpLibraryWidget->getLibraryTreeView()->setExpanded(proxyIndex, expandState);
}
/* Update the model switcher toolbar button. */
MainWindow::instance()->updateModelSwitcherMenu(0);
return true;
Expand All @@ -2210,38 +2121,19 @@ bool LibraryTreeModel::unloadLibraryTreeItem(LibraryTreeItem *pLibraryTreeItem,
* \brief LibraryTreeModel::removeLibraryTreeItem
* Removes the LibraryTreeItem.
* \param pLibraryTreeItem
* \param type
* \return
*/
bool LibraryTreeModel::removeLibraryTreeItem(LibraryTreeItem *pLibraryTreeItem, LibraryTreeItem::LibraryType type)
bool LibraryTreeModel::removeLibraryTreeItem(LibraryTreeItem *pLibraryTreeItem)
{
/* QSortFilterProxy::filterAcceptRows changes the expand/collapse behavior of indexes or I am using it in some stupid way.
* If index is expanded and we delete it then the next sibling index automatically becomes expanded.
* The following code overcomes this issue. It stores the next index expand state and then apply it after deletion.
*/
// remove the LibraryTreeItem from Libraries Browser
int row = pLibraryTreeItem->row();
LibraryTreeItem *pNextLibraryTreeItem = 0;
bool expandState = false;
if (pLibraryTreeItem->parent()->childrenSize() > row + 1) {
pNextLibraryTreeItem = pLibraryTreeItem->parent()->child(row + 1);
QModelIndex modelIndex = libraryTreeItemIndex(pNextLibraryTreeItem);
QModelIndex proxyIndex = mpLibraryWidget->getLibraryTreeProxyModel()->mapFromSource(modelIndex);
expandState = mpLibraryWidget->getLibraryTreeView()->isExpanded(proxyIndex);
}
if (type == LibraryTreeItem::OMS) {
// remove the LibraryTreeItem from Libraries Browser
int row = pLibraryTreeItem->row();
beginRemoveRows(libraryTreeItemIndex(pLibraryTreeItem), row, row);
unloadFileChildren(pLibraryTreeItem);
endRemoveRows();
} else {
beginRemoveRows(libraryTreeItemIndex(pLibraryTreeItem->parent()), row, row);
if (pLibraryTreeItem->getLibraryType() == LibraryTreeItem::Modelica) {
unloadClassChildren(pLibraryTreeItem);
} else {
unloadFileChildren(pLibraryTreeItem);
}
if (pNextLibraryTreeItem) {
QModelIndex modelIndex = libraryTreeItemIndex(pNextLibraryTreeItem);
QModelIndex proxyIndex = mpLibraryWidget->getLibraryTreeProxyModel()->mapFromSource(modelIndex);
mpLibraryWidget->getLibraryTreeView()->setExpanded(proxyIndex, expandState);
}
endRemoveRows();
/* Update the model switcher toolbar button. */
MainWindow::instance()->updateModelSwitcherMenu(0);
return true;
Expand Down Expand Up @@ -2277,29 +2169,12 @@ bool LibraryTreeModel::deleteTextFile(LibraryTreeItem *pLibraryTreeItem, bool as
return false;
}
}
/* QSortFilterProxy::filterAcceptRows changes the expand/collapse behavior of indexes or I am using it in some stupid way.
* If index is expanded and we delete it then the next sibling index automatically becomes expanded.
* The following code overcomes this issue. It stores the next index expand state and then apply it after deletion.
*/
int row = pLibraryTreeItem->row();
LibraryTreeItem *pNextLibraryTreeItem = 0;
bool expandState = false;
if (pLibraryTreeItem->parent()->childrenSize() > row + 1) {
pNextLibraryTreeItem = pLibraryTreeItem->parent()->child(row + 1);
QModelIndex modelIndex = libraryTreeItemIndex(pNextLibraryTreeItem);
QModelIndex proxyIndex = mpLibraryWidget->getLibraryTreeProxyModel()->mapFromSource(modelIndex);
expandState = mpLibraryWidget->getLibraryTreeView()->isExpanded(proxyIndex);
}
// remove the LibraryTreeItem from Libraries Browser
beginRemoveRows(libraryTreeItemIndex(pLibraryTreeItem), row, row);
beginRemoveRows(libraryTreeItemIndex(pLibraryTreeItem->parent()), row, row);
// Deletes the LibraryTreeItem children if any and then deletes the LibraryTreeItem.
deleteFileChildren(pLibraryTreeItem);
endRemoveRows();
if (pNextLibraryTreeItem) {
QModelIndex modelIndex = libraryTreeItemIndex(pNextLibraryTreeItem);
QModelIndex proxyIndex = mpLibraryWidget->getLibraryTreeProxyModel()->mapFromSource(modelIndex);
mpLibraryWidget->getLibraryTreeView()->setExpanded(proxyIndex, expandState);
}
/* Update the model switcher toolbar button. */
MainWindow::instance()->updateModelSwitcherMenu(0);
return true;
Expand Down
2 changes: 1 addition & 1 deletion OMEdit/OMEditLIB/Modeling/LibraryTreeWidget.h
Expand Up @@ -322,7 +322,7 @@ class LibraryTreeModel : public QAbstractItemModel
void expandLibraryTreeItems(LibraryTreeItem *pLibraryTreeItem, QStringList expandedLibraryTreeItemsList);
void reLoadOMSimulatorModel(const QString &modelName, const QString &editedCref, const QString &snapShot);
bool unloadLibraryTreeItem(LibraryTreeItem *pLibraryTreeItem, bool doDeleteClass);
bool removeLibraryTreeItem(LibraryTreeItem *pLibraryTreeItem, LibraryTreeItem::LibraryType type);
bool removeLibraryTreeItem(LibraryTreeItem *pLibraryTreeItem);
bool deleteTextFile(LibraryTreeItem *pLibraryTreeItem, bool askQuestion = true);
void moveClassUpDown(LibraryTreeItem *pLibraryTreeItem, bool up);
void moveClassTopBottom(LibraryTreeItem *pLibraryTreeItem, bool top);
Expand Down
2 changes: 1 addition & 1 deletion OMEdit/OMEditLIB/Modeling/ModelWidgetContainer.cpp
Expand Up @@ -5324,7 +5324,7 @@ void ModelWidget::updateChildClasses(LibraryTreeItem *pLibraryTreeItem)
while(i != pLibraryTreeItem->childrenSize()) {
LibraryTreeItem *pChildLibraryTreeItem = pLibraryTreeItem->child(i);
if (!classNames.contains(pChildLibraryTreeItem->getName())) {
pLibraryTreeModel->removeLibraryTreeItem(pChildLibraryTreeItem, LibraryTreeItem::Modelica);
pLibraryTreeModel->removeLibraryTreeItem(pChildLibraryTreeItem);
i = 0; //Restart iteration if list has changed
} else {
i++;
Expand Down
2 changes: 1 addition & 1 deletion OMEdit/OMEditLIB/Plotting/VariablesWidget.cpp
Expand Up @@ -897,7 +897,7 @@ bool VariablesTreeModel::removeVariableTreeItem(QString variable)
mpVariablesTreeView->getVariablesWidget()->enableVisualizationControls(false);
mpVariablesTreeView->getVariablesWidget()->rewindVisualization();
}
beginRemoveRows(variablesTreeItemIndex(pVariablesTreeItem), 0, pVariablesTreeItem->getChildren().size());
beginRemoveRows(variablesTreeItemIndex(pVariablesTreeItem->parent()), 0, pVariablesTreeItem->getChildren().size());
pVariablesTreeItem->removeChildren();
VariablesTreeItem *pParentVariablesTreeItem = pVariablesTreeItem->parent();
pParentVariablesTreeItem->removeChild(pVariablesTreeItem);
Expand Down
Expand Up @@ -369,9 +369,13 @@ void TVariablesTreeModel::insertTVariablesItems(QHashIterator<QString, OMVariabl

void TVariablesTreeModel::clearTVariablesTreeItems()
{
beginRemoveRows(tVariablesTreeItemIndex(mpRootTVariablesTreeItem), 0, mpRootTVariablesTreeItem->getChildren().size());
mpRootTVariablesTreeItem->removeChildren();
endRemoveRows();
const int n = mpRootTVariablesTreeItem->getChildren().size();
if (n > 0) {
QModelIndex index = tVariablesTreeItemIndex(mpRootTVariablesTreeItem);
beginRemoveRows(index, 0, n - 1);
mpRootTVariablesTreeItem->removeChildren();
endRemoveRows();
}
}

TVariableTreeProxyModel::TVariableTreeProxyModel(QObject *parent)
Expand Down

0 comments on commit f102bad

Please sign in to comment.