sw/source/uibase/inc/conttree.hxx  |    4 
 sw/source/uibase/utlui/content.cxx |  183 +++++++++++++++++++------------------
 2 files changed, 97 insertions(+), 90 deletions(-)

New commits:
commit 7a555725dad87b977154961beeb5c0da2a856709
Author:     Jim Raykowski <rayk...@gmail.com>
AuthorDate: Thu Nov 23 16:54:51 2023 -0900
Commit:     Jim Raykowski <rayk...@gmail.com>
CommitDate: Wed Nov 29 07:59:38 2023 +0100

    SwNavigator: Rework the SwContentTree Expand function
    
    in effort to improve readability and operation.
    
    Change-Id: I7bfe485e24ee4cda3a3d99c522177950ad9ad399
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/159887
    Tested-by: Jenkins
    Reviewed-by: Jim Raykowski <rayk...@gmail.com>

diff --git a/sw/source/uibase/inc/conttree.hxx 
b/sw/source/uibase/inc/conttree.hxx
index a8094c40303a..9e1653fd08e3 100644
--- a/sw/source/uibase/inc/conttree.hxx
+++ b/sw/source/uibase/inc/conttree.hxx
@@ -109,10 +109,10 @@ class SwContentTree final : public SfxListener
     std::map< void*, bool > mOutLineNodeMap;
     std::map<const void*, bool> m_aRegionNodeExpandMap; // stores expand state 
of nodes with children
 
-    sal_Int32           m_nActiveBlock; // used to restore content 
types/categories expand state
+    sal_Int32           m_nActiveBlock;  // used to restore content types 
expand state
     sal_Int32           m_nHiddenBlock;
     size_t              m_nEntryCount;
-    ContentTypeId       m_nRootType;
+    ContentTypeId       m_nRootType;    // content type that is currently 
displayed in the tree
     ContentTypeId       m_nLastSelType;
     sal_uInt8           m_nOutlineLevel;
 
diff --git a/sw/source/uibase/utlui/content.cxx 
b/sw/source/uibase/utlui/content.cxx
index 352a6daa9bba..4ca633697cdd 100644
--- a/sw/source/uibase/utlui/content.cxx
+++ b/sw/source/uibase/utlui/content.cxx
@@ -2364,122 +2364,129 @@ SdrObject* 
SwContentTree::GetDrawingObjectsByContent(const SwContent *pCnt)
     return pRetObj;
 }
 
-void SwContentTree::Expand(const weld::TreeIter& rParent, 
std::vector<std::unique_ptr<weld::TreeIter>>* pNodesToExpand)
+void SwContentTree::Expand(const weld::TreeIter& rParent,
+                           std::vector<std::unique_ptr<weld::TreeIter>>* 
pNodesToExpand)
 {
-    if (!(m_xTreeView->iter_has_child(rParent) || 
m_xTreeView->get_children_on_demand(rParent)))
+    if (!m_xTreeView->iter_has_child(rParent) && 
!m_xTreeView->get_children_on_demand(rParent))
         return;
 
-    if (m_nRootType == ContentTypeId::UNKNOWN || m_nRootType == 
ContentTypeId::OUTLINE ||
-            m_nRootType == ContentTypeId::REGION)
+    // pNodesToExpand is used by the Display function to restore the trees 
expand structure for
+    // hierarchical content types, e.g., OUTLINE and REGION.
+    if (pNodesToExpand)
+        pNodesToExpand->emplace_back(m_xTreeView->make_iterator(&rParent));
+
+    // rParentId is a string representation of a pointer to SwContentType or 
SwContent
+    const OUString& rParentId = m_xTreeView->get_id(rParent);
+    // bParentIsContentType tells if the passed rParent tree entry is a 
content type or content
+    const bool bParentIsContentType = lcl_IsContentType(rParent, *m_xTreeView);
+    // eParentContentTypeId is the content type of the passed rParent tree 
entry
+    const ContentTypeId eParentContentTypeId =
+            bParentIsContentType ? 
weld::fromId<SwContentType*>(rParentId)->GetType() :
+                                   
weld::fromId<SwContent*>(rParentId)->GetParent()->GetType();
+
+    if (m_nRootType == ContentTypeId::UNKNOWN && bParentIsContentType)
     {
-        if (lcl_IsContentType(rParent, *m_xTreeView))
+        // m_nActiveBlock and m_nHiddenBlock are used to persist the content 
type expand state for
+        // the all content view mode
+        const sal_Int32 nOr = 1 << static_cast<int>(eParentContentTypeId); 
//linear -> Bitposition
+        if (State::HIDDEN != m_eState)
         {
-            ContentTypeId eContentTypeId =
-                    
weld::fromId<SwContentType*>(m_xTreeView->get_id(rParent))->GetType();
-            const sal_Int32 nOr = 1 << static_cast<int>(eContentTypeId); 
//linear -> Bitposition
-            if (State::HIDDEN != m_eState)
-            {
-                m_nActiveBlock |= nOr;
-                m_pConfig->SetActiveBlock(m_nActiveBlock);
-            }
-            else
-                m_nHiddenBlock |= nOr;
-            if (eContentTypeId == ContentTypeId::OUTLINE)
-            {
-                std::map< void*, bool > aCurrOutLineNodeMap;
+            m_nActiveBlock |= nOr;
+            m_pConfig->SetActiveBlock(m_nActiveBlock);
+        }
+        else
+            m_nHiddenBlock |= nOr;
+    }
+
+    if (m_nRootType == ContentTypeId::OUTLINE || (m_nRootType == 
ContentTypeId::UNKNOWN &&
+                                                  eParentContentTypeId == 
ContentTypeId::OUTLINE))
+    {
+        if (bParentIsContentType)
+        {
+            std::map< void*, bool > aCurrOutLineNodeMap;
 
-                SwWrtShell* pShell = GetWrtShell();
-                bool bParentHasChild = RequestingChildren(rParent);
-                if (pNodesToExpand)
-                    
pNodesToExpand->emplace_back(m_xTreeView->make_iterator(&rParent));
-                if (bParentHasChild)
+            SwWrtShell* pShell = GetWrtShell();
+            bool bParentHasChild = RequestingChildren(rParent);
+            if (bParentHasChild)
+            {
+                std::unique_ptr<weld::TreeIter> 
xChild(m_xTreeView->make_iterator(&rParent));
+                bool bChild = m_xTreeView->iter_next(*xChild);
+                while (bChild && lcl_IsContent(*xChild, *m_xTreeView))
                 {
-                    std::unique_ptr<weld::TreeIter> 
xChild(m_xTreeView->make_iterator(&rParent));
-                    bool bChild = m_xTreeView->iter_next(*xChild);
-                    while (bChild && lcl_IsContent(*xChild, *m_xTreeView))
+                    if (m_xTreeView->iter_has_child(*xChild))
                     {
-                        if (m_xTreeView->iter_has_child(*xChild))
+                        
assert(dynamic_cast<SwOutlineContent*>(weld::fromId<SwTypeNumber*>(m_xTreeView->get_id(*xChild))));
+                        auto const nPos = 
weld::fromId<SwOutlineContent*>(m_xTreeView->get_id(*xChild))->GetOutlinePos();
+                        void* key = 
static_cast<void*>(pShell->getIDocumentOutlineNodesAccess()->getOutlineNode( 
nPos ));
+                        aCurrOutLineNodeMap.emplace( key, false );
+                        std::map<void*, bool>::iterator iter = 
mOutLineNodeMap.find( key );
+                        if( iter != mOutLineNodeMap.end() && 
mOutLineNodeMap[key])
                         {
-                            
assert(dynamic_cast<SwOutlineContent*>(weld::fromId<SwTypeNumber*>(m_xTreeView->get_id(*xChild))));
-                            auto const nPos = 
weld::fromId<SwOutlineContent*>(m_xTreeView->get_id(*xChild))->GetOutlinePos();
-                            void* key = 
static_cast<void*>(pShell->getIDocumentOutlineNodesAccess()->getOutlineNode( 
nPos ));
-                            aCurrOutLineNodeMap.emplace( key, false );
-                            std::map<void*, bool>::iterator iter = 
mOutLineNodeMap.find( key );
-                            if( iter != mOutLineNodeMap.end() && 
mOutLineNodeMap[key])
-                            {
-                                aCurrOutLineNodeMap[key] = true;
-                                RequestingChildren(*xChild);
-                                if (pNodesToExpand)
-                                    
pNodesToExpand->emplace_back(m_xTreeView->make_iterator(xChild.get()));
-                                m_xTreeView->set_children_on_demand(*xChild, 
false);
-                            }
+                            aCurrOutLineNodeMap[key] = true;
+                            RequestingChildren(*xChild);
+                            if (pNodesToExpand)
+                                
pNodesToExpand->emplace_back(m_xTreeView->make_iterator(xChild.get()));
+                            m_xTreeView->set_children_on_demand(*xChild, 
false);
                         }
-                        bChild = m_xTreeView->iter_next(*xChild);
                     }
+                    bChild = m_xTreeView->iter_next(*xChild);
                 }
-                mOutLineNodeMap = aCurrOutLineNodeMap;
-                return;
             }
-            if (eContentTypeId == ContentTypeId::REGION)
+            mOutLineNodeMap = aCurrOutLineNodeMap;
+            return;
+        }
+        else // content entry
+        {
+            SwWrtShell* pShell = GetWrtShell();
+            
assert(dynamic_cast<SwOutlineContent*>(weld::fromId<SwTypeNumber*>(rParentId)));
+            auto const nPos = 
weld::fromId<SwOutlineContent*>(rParentId)->GetOutlinePos();
+            void* key = 
static_cast<void*>(pShell->getIDocumentOutlineNodesAccess()->getOutlineNode( 
nPos ));
+            mOutLineNodeMap[key] = true;
+        }
+    }
+    else if (m_nRootType == ContentTypeId::REGION || (m_nRootType == 
ContentTypeId::UNKNOWN &&
+                                                      eParentContentTypeId == 
ContentTypeId::REGION))
+    {
+        if (bParentIsContentType)
+        {
+            std::map<const void*, bool> aCurrentRegionNodeExpandMap;
+            if (RequestingChildren(rParent))
             {
-                std::map<const void*, bool> aCurrentRegionNodeExpandMap;
-
-                bool bParentHasChild = RequestingChildren(rParent);
-                if (pNodesToExpand)
-                    
pNodesToExpand->emplace_back(m_xTreeView->make_iterator(&rParent));
-                if (bParentHasChild)
+                std::unique_ptr<weld::TreeIter> 
xChild(m_xTreeView->make_iterator(&rParent));
+                while (m_xTreeView->iter_next(*xChild) && 
lcl_IsContent(*xChild, *m_xTreeView))
                 {
-                    std::unique_ptr<weld::TreeIter> 
xChild(m_xTreeView->make_iterator(&rParent));
-                    bool bChild = m_xTreeView->iter_next(*xChild);
-                    while (bChild && lcl_IsContent(*xChild, *m_xTreeView))
+                    if (m_xTreeView->iter_has_child(*xChild))
                     {
-                        if (m_xTreeView->iter_has_child(*xChild))
+                        
assert(dynamic_cast<SwRegionContent*>(weld::fromId<SwTypeNumber*>(m_xTreeView->get_id(*xChild))));
+                        const void* key =
+                                static_cast<const 
void*>(weld::fromId<SwRegionContent*>(
+                                                
m_xTreeView->get_id(*xChild))->GetSectionFormat());
+                        bool bExpandNode =
+                                m_aRegionNodeExpandMap.contains(key) && 
m_aRegionNodeExpandMap[key];
+                        aCurrentRegionNodeExpandMap.emplace(key, bExpandNode);
+                        if (bExpandNode)
                         {
-                            
assert(dynamic_cast<SwRegionContent*>(weld::fromId<SwTypeNumber*>(m_xTreeView->get_id(*xChild))));
-                            const void* key = static_cast<const void*>(
-                                        
weld::fromId<SwRegionContent*>(m_xTreeView->get_id(*xChild))
-                                        ->GetSectionFormat());
-                            aCurrentRegionNodeExpandMap.emplace(key, false);
-                            if (m_aRegionNodeExpandMap.contains(key) && 
m_aRegionNodeExpandMap[key])
-                            {
-                                aCurrentRegionNodeExpandMap[key] = true;
-                                RequestingChildren(*xChild);
-                                if (pNodesToExpand)
-                                    
pNodesToExpand->emplace_back(m_xTreeView->make_iterator(xChild.get()));
-                                m_xTreeView->set_children_on_demand(*xChild, 
false);
-                            }
+                            if (pNodesToExpand)
+                                
pNodesToExpand->emplace_back(m_xTreeView->make_iterator(xChild.get()));
+                            RequestingChildren(*xChild);
+                            m_xTreeView->set_children_on_demand(*xChild, 
false);
                         }
-                        bChild = m_xTreeView->iter_next(*xChild);
                     }
                 }
-                m_aRegionNodeExpandMap = aCurrentRegionNodeExpandMap;
-                return;
             }
+            m_aRegionNodeExpandMap = aCurrentRegionNodeExpandMap;
+            return;
         }
         else // content entry
         {
-            ContentTypeId eContentTypeId =
-                    
weld::fromId<SwContent*>(m_xTreeView->get_id(rParent))->GetParent()->GetType();
-            if (eContentTypeId == ContentTypeId::OUTLINE)
-            {
-                SwWrtShell* pShell = GetWrtShell();
-                
assert(dynamic_cast<SwOutlineContent*>(weld::fromId<SwTypeNumber*>(m_xTreeView->get_id(rParent))));
-                auto const nPos = 
weld::fromId<SwOutlineContent*>(m_xTreeView->get_id(rParent))->GetOutlinePos();
-                void* key = 
static_cast<void*>(pShell->getIDocumentOutlineNodesAccess()->getOutlineNode( 
nPos ));
-                mOutLineNodeMap[key] = true;
-            }
-            else if(eContentTypeId == ContentTypeId::REGION)
-            {
-                
assert(dynamic_cast<SwRegionContent*>(weld::fromId<SwTypeNumber*>(m_xTreeView->get_id(rParent))));
-                const void* key = static_cast<const 
void*>(weld::fromId<SwRegionContent*>(m_xTreeView->get_id(rParent))->GetSectionFormat());
-                m_aRegionNodeExpandMap[key] = true;
-            }
+            
assert(dynamic_cast<SwRegionContent*>(weld::fromId<SwTypeNumber*>(rParentId)));
+            const void* key = static_cast<const void*>(
+                        
weld::fromId<SwRegionContent*>(rParentId)->GetSectionFormat());
+            m_aRegionNodeExpandMap[key] = true;
         }
     }
 
     RequestingChildren(rParent);
-    if (pNodesToExpand)
-        pNodesToExpand->emplace_back(m_xTreeView->make_iterator(&rParent));
 }
 
 IMPL_LINK(SwContentTree, ExpandHdl, const weld::TreeIter&, rParent, bool)

Reply via email to