From 71db603e11f68bd108e351101e2ebbd6a4531fd8 Mon Sep 17 00:00:00 2001 From: Magne Sjaastad Date: Wed, 6 Sep 2017 14:46:32 +0200 Subject: [PATCH] #1830 AppFwk : Invalidate only relevant QModelIndex when selecting items --- .../cafTestApplication/MainWindow.cpp | 80 ++++++++++++------- .../cafPdmUiTreeSelectionEditor.cpp | 23 ++++-- .../cafPdmUiTreeSelectionQModel.cpp | 13 +++ 3 files changed, 79 insertions(+), 37 deletions(-) diff --git a/Fwk/AppFwk/cafTests/cafTestApplication/MainWindow.cpp b/Fwk/AppFwk/cafTests/cafTestApplication/MainWindow.cpp index f14f863c7e..5f324385f7 100644 --- a/Fwk/AppFwk/cafTests/cafTestApplication/MainWindow.cpp +++ b/Fwk/AppFwk/cafTests/cafTestApplication/MainWindow.cpp @@ -114,38 +114,60 @@ public: { QList options; - QString text; - - text = "First"; - options.push_back(caf::PdmOptionItemInfo(text, text)); - - text = "Second"; - options.push_back(caf::PdmOptionItemInfo(text, text)); - - + if (fieldNeedingOptions == &m_multiSelectList) { - text = "Second_a"; - caf::PdmOptionItemInfo itemInfo = caf::PdmOptionItemInfo(text, text); - itemInfo.setLevel(1); - options.push_back(itemInfo); + QString text; + + text = "First"; + options.push_back(caf::PdmOptionItemInfo(text, text)); + + text = "Second"; + options.push_back(caf::PdmOptionItemInfo(text, text)); + + + { + text = "Second_a"; + caf::PdmOptionItemInfo itemInfo = caf::PdmOptionItemInfo(text, text); + itemInfo.setLevel(1); + options.push_back(itemInfo); + } + + { + text = "Second_b"; + caf::PdmOptionItemInfo itemInfo = caf::PdmOptionItemInfo(text, text); + itemInfo.setLevel(1); + options.push_back(itemInfo); + } + + static int s_additionalSubItems = 0; + s_additionalSubItems++; + for (auto i = 0; i < s_additionalSubItems; i++) + { + text = "Second_b_" + QString::number(i); + caf::PdmOptionItemInfo itemInfo = caf::PdmOptionItemInfo(text, text); + itemInfo.setLevel(1); + options.push_back(itemInfo); + } + + + int additionalSubItems = 0; + for (auto i = 0; i < additionalSubItems; i++) + { + text = "Second_b_" + QString::number(i); + caf::PdmOptionItemInfo itemInfo = caf::PdmOptionItemInfo(text, text); + itemInfo.setLevel(1); + options.push_back(itemInfo); + } + + + + text = "Third"; + options.push_back(caf::PdmOptionItemInfo(text, text)); + + text = "Fourth"; + options.push_back(caf::PdmOptionItemInfo(text, text)); } - if (1) - { - text = "Second_b"; - caf::PdmOptionItemInfo itemInfo = caf::PdmOptionItemInfo(text, text); - itemInfo.setLevel(1); - options.push_back(itemInfo); - } - - - text = "Third"; - options.push_back(caf::PdmOptionItemInfo(text, text)); - - text = "Fourth"; - options.push_back(caf::PdmOptionItemInfo(text, text)); - - return options; } diff --git a/Fwk/AppFwk/cafUserInterface/cafPdmUiTreeSelectionEditor.cpp b/Fwk/AppFwk/cafUserInterface/cafPdmUiTreeSelectionEditor.cpp index 7928ea1912..94e5295b43 100644 --- a/Fwk/AppFwk/cafUserInterface/cafPdmUiTreeSelectionEditor.cpp +++ b/Fwk/AppFwk/cafUserInterface/cafPdmUiTreeSelectionEditor.cpp @@ -40,6 +40,8 @@ #include "cafPdmObject.h" #include "cafPdmUiTreeSelectionQModel.h" +#include "cafQTreeViewStateSerializer.h" + #include #include @@ -75,15 +77,20 @@ void PdmUiTreeSelectionEditor::configureAndUpdateUi(const QString& uiConfigName) bool optionsOnly = true; QList options = field()->valueOptions(&optionsOnly); - caf::PdmUiTreeSelectionQModel* model = new caf::PdmUiTreeSelectionQModel(m_treeView); - m_treeView->setModel(model); + if (!m_treeView->model()) + { + caf::PdmUiTreeSelectionQModel* model = new caf::PdmUiTreeSelectionQModel(m_treeView); + m_treeView->setModel(model); + connect(model, SIGNAL(signalSelectionStateForIndexHasChanged(int, bool)), this, SLOT(slotSetSelectionStateForIndex(int, bool))); + } - connect(model, SIGNAL(signalSelectionStateForIndexHasChanged(int, bool)), this, SLOT(slotSetSelectionStateForIndex(int, bool))); - - model->setOptions(this, options); - - // TODO: Try to merge expanded state with newly generated tree - //m_treeView->expandAll(); + caf::PdmUiTreeSelectionQModel* treeSelectionQModel = dynamic_cast(m_treeView->model()); + if (treeSelectionQModel) + { + // TODO: If the count is different between incoming and current list of items, + // use cafQTreeViewStateSerializer to restore collapsed state + treeSelectionQModel->setOptions(this, options); + } } //-------------------------------------------------------------------------------------------------- diff --git a/Fwk/AppFwk/cafUserInterface/cafPdmUiTreeSelectionQModel.cpp b/Fwk/AppFwk/cafUserInterface/cafPdmUiTreeSelectionQModel.cpp index 0d0229f24e..1012c50a53 100644 --- a/Fwk/AppFwk/cafUserInterface/cafPdmUiTreeSelectionQModel.cpp +++ b/Fwk/AppFwk/cafUserInterface/cafPdmUiTreeSelectionQModel.cpp @@ -58,10 +58,21 @@ caf::PdmUiTreeSelectionQModel::PdmUiTreeSelectionQModel(QObject *parent /*= 0*/) //-------------------------------------------------------------------------------------------------- void caf::PdmUiTreeSelectionQModel::setOptions(caf::PdmUiFieldEditorHandle* field, const QList& options) { + bool itemCountIsChanged = false; + if (m_options.size() != options.size()) + { + itemCountIsChanged = true; + } + m_uiFieldHandle = field; m_options = options; computeOptionItemTreeData(); + + if (itemCountIsChanged) + { + reset(); + } } //-------------------------------------------------------------------------------------------------- @@ -175,6 +186,8 @@ bool caf::PdmUiTreeSelectionQModel::setData(const QModelIndex &index, const QVar emit signalSelectionStateForIndexHasChanged(toOptionItemIndex(index), isSelected); + emit dataChanged(index, index); + return true; }