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 <swatrset.hxx>
24#include <unordered_map>
25#include <osl/diagnose.h>
26
27typedef std::unordered_map< OUString,
28 std::shared_ptr<SfxItemSet> > SwStyleNameCache;
29
30namespace {
31
32class SwStyleCache
33{
35public:
36 SwStyleCache() {}
37 void addStyleName( const std::shared_ptr<SfxItemSet>& pStyle )
38 { mMap[ StylePool::nameOf(pStyle) ] = pStyle; }
39 void addCompletePool( StylePool& rPool );
40 std::shared_ptr<SfxItemSet> getByName( const OUString& rName ) { return mMap[rName]; }
41 void clear() { mMap.clear(); }
42};
43
44}
45
46void SwStyleCache::addCompletePool( StylePool& rPool )
47{
48 std::unique_ptr<IStylePoolIteratorAccess> pIter = rPool.createIterator();
49 std::shared_ptr<SfxItemSet> pStyle = pIter->getNext();
50 while( pStyle )
51 {
52 OUString aName( StylePool::nameOf(pStyle) );
53 mMap[ aName ] = pStyle;
54 pStyle = pIter->getNext();
55 }
56}
57
58namespace {
59
60class SwStyleManager : public IStyleAccess
61{
62 StylePool m_aAutoCharPool;
63 StylePool m_aAutoParaPool;
64 SwStyleCache maCharCache;
65 SwStyleCache maParaCache;
66
67public:
68 // accept empty item set for ignorable paragraph items.
69 explicit SwStyleManager(SfxItemSet const* pIgnorableParagraphItems)
70 : m_aAutoParaPool(pIgnorableParagraphItems)
71 {}
72 virtual std::shared_ptr<SfxItemSet> getAutomaticStyle( const SfxItemSet& rSet,
74 const OUString* pParentName = nullptr ) override;
75 virtual std::shared_ptr<SfxItemSet> getByName( const OUString& rName,
76 IStyleAccess::SwAutoStyleFamily eFamily ) override;
77 virtual void getAllStyles( std::vector<std::shared_ptr<SfxItemSet>> &rStyles,
78 IStyleAccess::SwAutoStyleFamily eFamily ) override;
79 virtual std::shared_ptr<SfxItemSet> cacheAutomaticStyle( const SfxItemSet& rSet,
80 SwAutoStyleFamily eFamily ) override;
81 virtual void clearCaches() override;
82};
83
84}
85
86std::unique_ptr<IStyleAccess> createStyleManager( SfxItemSet const * pIgnorableParagraphItems )
87{
88 return std::make_unique<SwStyleManager>( pIgnorableParagraphItems );
89}
90
91void SwStyleManager::clearCaches()
92{
93 maCharCache.clear();
94 maParaCache.clear();
95}
96
97std::shared_ptr<SfxItemSet> SwStyleManager::getAutomaticStyle( const SfxItemSet& rSet,
99 const OUString* pParentName )
100{
101 assert(eFamily != IStyleAccess::AUTO_STYLE_PARA || dynamic_cast<const SwAttrSet*>(&rSet));
102 StylePool& rAutoPool
103 = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? m_aAutoCharPool : m_aAutoParaPool;
104 return rAutoPool.insertItemSet( rSet, pParentName );
105}
106
107std::shared_ptr<SfxItemSet> SwStyleManager::cacheAutomaticStyle( const SfxItemSet& rSet,
109{
110 assert(eFamily != IStyleAccess::AUTO_STYLE_PARA || dynamic_cast<const SwAttrSet*>(&rSet));
111 StylePool& rAutoPool
112 = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? m_aAutoCharPool : m_aAutoParaPool;
113 std::shared_ptr<SfxItemSet> pStyle = rAutoPool.insertItemSet( rSet );
114 if (eFamily == IStyleAccess::AUTO_STYLE_CHAR)
115 {
116 maCharCache.addStyleName( pStyle );
117 }
118 else
119 {
120 maParaCache.addStyleName( pStyle );
121 }
122 return pStyle;
123}
124
125std::shared_ptr<SfxItemSet> SwStyleManager::getByName( const OUString& rName,
127{
128 StylePool& rAutoPool
129 = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? m_aAutoCharPool : m_aAutoParaPool;
130 SwStyleCache &rCache = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? maCharCache : maParaCache;
131 std::shared_ptr<SfxItemSet> pStyle = rCache.getByName( rName );
132 if( !pStyle )
133 {
134 // Ok, ok, it's allowed to ask for uncached styles (from UNO) but it should not be done
135 // during loading a document
136 OSL_FAIL( "Don't ask for uncached styles" );
137 rCache.addCompletePool( rAutoPool );
138 pStyle = rCache.getByName( rName );
139 }
140 assert(!pStyle || eFamily != IStyleAccess::AUTO_STYLE_PARA || dynamic_cast<SwAttrSet*>(pStyle.get()));
141 return pStyle;
142}
143
144void SwStyleManager::getAllStyles( std::vector<std::shared_ptr<SfxItemSet>> &rStyles,
146{
147 StylePool& rAutoPool
148 = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? m_aAutoCharPool : m_aAutoParaPool;
149 // setup <StylePool> iterator, which skips unused styles and ignorable items
150 std::unique_ptr<IStylePoolIteratorAccess> pIter = rAutoPool.createIterator( true, true );
151 std::shared_ptr<SfxItemSet> pStyle = pIter->getNext();
152 while( pStyle )
153 {
154 assert(eFamily != IStyleAccess::AUTO_STYLE_PARA || dynamic_cast<SwAttrSet*>(pStyle.get()));
155 rStyles.push_back( pStyle );
156
157 pStyle = pIter->getNext();
158 }
159}
160
161/* 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)