sc: factor out some more code
[LibreOffice.git] / sw / source / core / doc / swstylemanager.cxx
blobcdca3dc341472b9da662c5db7a33fd952ccc5f7b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "swstylemanager.hxx"
21 #include <svl/stylepool.hxx>
22 #include <istyleaccess.hxx>
23 #include <swatrset.hxx>
24 #include <unordered_map>
25 #include <osl/diagnose.h>
26 #include <names.hxx>
28 typedef std::unordered_map< OUString,
29 std::shared_ptr<SfxItemSet> > SwStyleNameCache;
31 namespace {
33 class SwStyleCache
35 SwStyleNameCache mMap;
36 public:
37 SwStyleCache() {}
38 void addStyleName( const std::shared_ptr<SfxItemSet>& pStyle )
39 { mMap[ StylePool::nameOf(pStyle) ] = pStyle; }
40 void addCompletePool( StylePool& rPool );
41 std::shared_ptr<SfxItemSet> getByName( const OUString& rName ) { return mMap[rName]; }
42 void clear() { mMap.clear(); }
47 void SwStyleCache::addCompletePool( StylePool& rPool )
49 rPool.populateCacheMap(mMap);
52 namespace {
54 class SwStyleManager : public IStyleAccess
56 StylePool m_aAutoCharPool;
57 StylePool m_aAutoParaPool;
58 SwStyleCache maCharCache;
59 SwStyleCache maParaCache;
61 public:
62 // accept empty item set for ignorable paragraph items.
63 explicit SwStyleManager(SfxItemSet const* pIgnorableParagraphItems)
64 : m_aAutoParaPool(pIgnorableParagraphItems)
66 virtual std::shared_ptr<SfxItemSet> getAutomaticStyle( const SfxItemSet& rSet,
67 IStyleAccess::SwAutoStyleFamily eFamily,
68 const OUString* pParentName = nullptr ) override;
69 virtual std::shared_ptr<SwAttrSet> getAutomaticStyle( const SwAttrSet& rSet,
70 IStyleAccess::SwAutoStyleFamily eFamily,
71 const ProgName* pParentName = nullptr ) override;
72 virtual std::shared_ptr<SfxItemSet> getByName( const OUString& rName,
73 IStyleAccess::SwAutoStyleFamily eFamily ) override;
74 virtual void getAllStyles( std::vector<std::shared_ptr<SfxItemSet>> &rStyles,
75 IStyleAccess::SwAutoStyleFamily eFamily ) override;
76 virtual std::shared_ptr<SfxItemSet> cacheAutomaticStyle( const SfxItemSet& rSet,
77 SwAutoStyleFamily eFamily ) override;
78 virtual void clearCaches() override;
83 std::unique_ptr<IStyleAccess> createStyleManager( SfxItemSet const * pIgnorableParagraphItems )
85 return std::make_unique<SwStyleManager>( pIgnorableParagraphItems );
88 void SwStyleManager::clearCaches()
90 maCharCache.clear();
91 maParaCache.clear();
94 std::shared_ptr<SfxItemSet> SwStyleManager::getAutomaticStyle( const SfxItemSet& rSet,
95 IStyleAccess::SwAutoStyleFamily eFamily,
96 const OUString* pParentName )
98 assert(eFamily != IStyleAccess::AUTO_STYLE_PARA || dynamic_cast<const SwAttrSet*>(&rSet));
99 StylePool& rAutoPool
100 = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? m_aAutoCharPool : m_aAutoParaPool;
101 return rAutoPool.insertItemSet( rSet, pParentName );
104 std::shared_ptr<SwAttrSet> SwStyleManager::getAutomaticStyle( const SwAttrSet& rSet,
105 IStyleAccess::SwAutoStyleFamily eFamily,
106 const ProgName* pParentName )
108 StylePool& rAutoPool
109 = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? m_aAutoCharPool : m_aAutoParaPool;
110 std::shared_ptr<SfxItemSet> pItemSet = rAutoPool.insertItemSet( rSet, pParentName ? &pParentName->toString() : nullptr );
111 std::shared_ptr<SwAttrSet> pAttrSet = std::dynamic_pointer_cast<SwAttrSet>(pItemSet);
112 assert(bool(pItemSet) == bool(pAttrSet) && "types do not match");
113 return pAttrSet;
116 std::shared_ptr<SfxItemSet> SwStyleManager::cacheAutomaticStyle( const SfxItemSet& rSet,
117 IStyleAccess::SwAutoStyleFamily eFamily )
119 assert(eFamily != IStyleAccess::AUTO_STYLE_PARA || dynamic_cast<const SwAttrSet*>(&rSet));
120 StylePool& rAutoPool
121 = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? m_aAutoCharPool : m_aAutoParaPool;
122 std::shared_ptr<SfxItemSet> pStyle = rAutoPool.insertItemSet( rSet );
123 if (eFamily == IStyleAccess::AUTO_STYLE_CHAR)
125 maCharCache.addStyleName( pStyle );
127 else
129 maParaCache.addStyleName( pStyle );
131 return pStyle;
134 std::shared_ptr<SfxItemSet> SwStyleManager::getByName( const OUString& rName,
135 IStyleAccess::SwAutoStyleFamily eFamily )
137 StylePool& rAutoPool
138 = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? m_aAutoCharPool : m_aAutoParaPool;
139 SwStyleCache &rCache = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? maCharCache : maParaCache;
140 std::shared_ptr<SfxItemSet> pStyle = rCache.getByName( rName );
141 if( !pStyle )
143 // Ok, ok, it's allowed to ask for uncached styles (from UNO) but it should not be done
144 // during loading a document
145 OSL_FAIL( "Don't ask for uncached styles" );
146 rCache.addCompletePool( rAutoPool );
147 pStyle = rCache.getByName( rName );
149 assert(!pStyle || eFamily != IStyleAccess::AUTO_STYLE_PARA || dynamic_cast<SwAttrSet*>(pStyle.get()));
150 return pStyle;
153 void SwStyleManager::getAllStyles( std::vector<std::shared_ptr<SfxItemSet>> &rStyles,
154 IStyleAccess::SwAutoStyleFamily eFamily )
156 StylePool& rAutoPool
157 = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? m_aAutoCharPool : m_aAutoParaPool;
158 rAutoPool.getAllStyles(rStyles);
161 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */