LibreOffice Module sw (master) 1
swstylemanager.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include "swstylemanager.hxx"
21#include <svl/stylepool.hxx>
22#include <istyleaccess.hxx>
23#include <unordered_map>
24#include <osl/diagnose.h>
25
26typedef std::unordered_map< OUString,
27 std::shared_ptr<SfxItemSet> > SwStyleNameCache;
28
29namespace {
30
31class SwStyleCache
32{
34public:
35 SwStyleCache() {}
36 void addStyleName( const std::shared_ptr<SfxItemSet>& pStyle )
37 { mMap[ StylePool::nameOf(pStyle) ] = pStyle; }
38 void addCompletePool( StylePool& rPool );
39 std::shared_ptr<SfxItemSet> getByName( const OUString& rName ) { return mMap[rName]; }
40 void clear() { mMap.clear(); }
41};
42
43}
44
45void SwStyleCache::addCompletePool( StylePool& rPool )
46{
47 std::unique_ptr<IStylePoolIteratorAccess> pIter = rPool.createIterator();
48 std::shared_ptr<SfxItemSet> pStyle = pIter->getNext();
49 while( pStyle )
50 {
51 OUString aName( StylePool::nameOf(pStyle) );
52 mMap[ aName ] = pStyle;
53 pStyle = pIter->getNext();
54 }
55}
56
57namespace {
58
59class SwStyleManager : public IStyleAccess
60{
61 StylePool m_aAutoCharPool;
62 StylePool m_aAutoParaPool;
63 SwStyleCache maCharCache;
64 SwStyleCache maParaCache;
65
66public:
67 // accept empty item set for ignorable paragraph items.
68 explicit SwStyleManager(SfxItemSet const* pIgnorableParagraphItems)
69 : m_aAutoParaPool(pIgnorableParagraphItems)
70 {}
71 virtual std::shared_ptr<SfxItemSet> getAutomaticStyle( const SfxItemSet& rSet,
73 const OUString* pParentName = nullptr ) override;
74 virtual std::shared_ptr<SfxItemSet> getByName( const OUString& rName,
75 IStyleAccess::SwAutoStyleFamily eFamily ) override;
76 virtual void getAllStyles( std::vector<std::shared_ptr<SfxItemSet>> &rStyles,
77 IStyleAccess::SwAutoStyleFamily eFamily ) override;
78 virtual std::shared_ptr<SfxItemSet> cacheAutomaticStyle( const SfxItemSet& rSet,
79 SwAutoStyleFamily eFamily ) override;
80 virtual void clearCaches() override;
81};
82
83}
84
85std::unique_ptr<IStyleAccess> createStyleManager( SfxItemSet const * pIgnorableParagraphItems )
86{
87 return std::make_unique<SwStyleManager>( pIgnorableParagraphItems );
88}
89
90void SwStyleManager::clearCaches()
91{
92 maCharCache.clear();
93 maParaCache.clear();
94}
95
96std::shared_ptr<SfxItemSet> SwStyleManager::getAutomaticStyle( const SfxItemSet& rSet,
98 const OUString* pParentName )
99{
100 StylePool& rAutoPool
101 = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? m_aAutoCharPool : m_aAutoParaPool;
102 return rAutoPool.insertItemSet( rSet, pParentName );
103}
104
105std::shared_ptr<SfxItemSet> SwStyleManager::cacheAutomaticStyle( const SfxItemSet& rSet,
107{
108 StylePool& rAutoPool
109 = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? m_aAutoCharPool : m_aAutoParaPool;
110 std::shared_ptr<SfxItemSet> pStyle = rAutoPool.insertItemSet( rSet );
111 if (eFamily == IStyleAccess::AUTO_STYLE_CHAR)
112 {
113 maCharCache.addStyleName( pStyle );
114 }
115 else
116 {
117 maParaCache.addStyleName( pStyle );
118 }
119 return pStyle;
120}
121
122std::shared_ptr<SfxItemSet> SwStyleManager::getByName( const OUString& rName,
124{
125 StylePool& rAutoPool
126 = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? m_aAutoCharPool : m_aAutoParaPool;
127 SwStyleCache &rCache = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? maCharCache : maParaCache;
128 std::shared_ptr<SfxItemSet> pStyle = rCache.getByName( rName );
129 if( !pStyle )
130 {
131 // Ok, ok, it's allowed to ask for uncached styles (from UNO) but it should not be done
132 // during loading a document
133 OSL_FAIL( "Don't ask for uncached styles" );
134 rCache.addCompletePool( rAutoPool );
135 pStyle = rCache.getByName( rName );
136 }
137 return pStyle;
138}
139
140void SwStyleManager::getAllStyles( std::vector<std::shared_ptr<SfxItemSet>> &rStyles,
142{
143 StylePool& rAutoPool
144 = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? m_aAutoCharPool : m_aAutoParaPool;
145 // setup <StylePool> iterator, which skips unused styles and ignorable items
146 std::unique_ptr<IStylePoolIteratorAccess> pIter = rAutoPool.createIterator( true, true );
147 std::shared_ptr<SfxItemSet> pStyle = pIter->getNext();
148 while( pStyle )
149 {
150 rStyles.push_back( pStyle );
151
152 pStyle = pIter->getNext();
153 }
154}
155
156/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual std::shared_ptr< SfxItemSet > getByName(const OUString &rName, SwAutoStyleFamily eFamily)=0
It's slow to iterate through a stylepool looking for a special name, but if the style has been insert...
virtual void clearCaches()=0
To release the cached styles (shared_pointer!)
virtual std::shared_ptr< SfxItemSet > cacheAutomaticStyle(const SfxItemSet &rSet, SwAutoStyleFamily eFamily)=0
insert the style to the pool and the cache (used during import)
virtual void getAllStyles(std::vector< std::shared_ptr< SfxItemSet > > &rStyles, SwAutoStyleFamily eFamily)=0
virtual std::shared_ptr< SfxItemSet > getAutomaticStyle(const SfxItemSet &rSet, SwAutoStyleFamily eFamily, const OUString *pParentName=nullptr)=0
static OUString nameOf(const std::shared_ptr< SfxItemSet > &pSet)
std::unique_ptr< IStylePoolIteratorAccess > createIterator(const bool bSkipUnusedItemSets=false, const bool bSkipIgnorableItems=false)
std::shared_ptr< SfxItemSet > insertItemSet(const SfxItemSet &rSet, const OUString *pParentName=nullptr)
OUString aName
std::unordered_map< OUString, std::shared_ptr< SfxItemSet > > SwStyleNameCache
std::unique_ptr< IStyleAccess > createStyleManager(SfxItemSet const *pIgnorableParagraphItems)