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 
26 typedef std::unordered_map< OUString,
27  std::shared_ptr<SfxItemSet> > SwStyleNameCache;
28 
29 namespace {
30 
31 class SwStyleCache
32 {
33  SwStyleNameCache mMap;
34 public:
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 };
41 
42 }
43 
44 void SwStyleCache::addCompletePool( StylePool& rPool )
45 {
46  std::unique_ptr<IStylePoolIteratorAccess> pIter = rPool.createIterator();
47  std::shared_ptr<SfxItemSet> pStyle = pIter->getNext();
48  while( pStyle )
49  {
50  OUString aName( StylePool::nameOf(pStyle) );
51  mMap[ aName ] = pStyle;
52  pStyle = pIter->getNext();
53  }
54 }
55 
56 namespace {
57 
58 class SwStyleManager : public IStyleAccess
59 {
60  StylePool m_aAutoCharPool;
61  StylePool m_aAutoParaPool;
62  std::unique_ptr<SwStyleCache> mpCharCache;
63  std::unique_ptr<SwStyleCache> mpParaCache;
64 
65 public:
66  // accept empty item set for ignorable paragraph items.
67  explicit SwStyleManager(SfxItemSet const* pIgnorableParagraphItems)
68  : m_aAutoCharPool()
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 
85 std::unique_ptr<IStyleAccess> createStyleManager( SfxItemSet const * pIgnorableParagraphItems )
86 {
87  return std::make_unique<SwStyleManager>( pIgnorableParagraphItems );
88 }
89 
90 void SwStyleManager::clearCaches()
91 {
92  mpCharCache.reset();
93  mpParaCache.reset();
94 }
95 
96 std::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 
105 std::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  if (!mpCharCache)
114  mpCharCache.reset(new SwStyleCache());
115  mpCharCache->addStyleName( pStyle );
116  }
117  else
118  {
119  if (!mpParaCache)
120  mpParaCache.reset(new SwStyleCache());
121  mpParaCache->addStyleName( pStyle );
122  }
123  return pStyle;
124 }
125 
126 std::shared_ptr<SfxItemSet> SwStyleManager::getByName( const OUString& rName,
128 {
129  StylePool& rAutoPool
130  = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? m_aAutoCharPool : m_aAutoParaPool;
131  std::unique_ptr<SwStyleCache> &rpCache = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? mpCharCache : mpParaCache;
132  if( !rpCache )
133  rpCache.reset(new SwStyleCache());
134  std::shared_ptr<SfxItemSet> pStyle = rpCache->getByName( rName );
135  if( !pStyle )
136  {
137  // Ok, ok, it's allowed to ask for uncached styles (from UNO) but it should not be done
138  // during loading a document
139  OSL_FAIL( "Don't ask for uncached styles" );
140  rpCache->addCompletePool( rAutoPool );
141  pStyle = rpCache->getByName( rName );
142  }
143  return pStyle;
144 }
145 
146 void SwStyleManager::getAllStyles( std::vector<std::shared_ptr<SfxItemSet>> &rStyles,
148 {
149  StylePool& rAutoPool
150  = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? m_aAutoCharPool : m_aAutoParaPool;
151  // setup <StylePool> iterator, which skips unused styles and ignorable items
152  std::unique_ptr<IStylePoolIteratorAccess> pIter = rAutoPool.createIterator( true, true );
153  std::shared_ptr<SfxItemSet> pStyle = pIter->getNext();
154  while( pStyle )
155  {
156  rStyles.push_back( pStyle );
157 
158  pStyle = pIter->getNext();
159  }
160 }
161 
162 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual std::shared_ptr< SfxItemSet > getAutomaticStyle(const SfxItemSet &rSet, SwAutoStyleFamily eFamily, const OUString *pParentName=nullptr)=0
std::shared_ptr< SfxItemSet > insertItemSet(const SfxItemSet &rSet, const OUString *pParentName=nullptr)
std::unordered_map< OUString, std::shared_ptr< SfxItemSet > > SwStyleNameCache
static OUString nameOf(const std::shared_ptr< SfxItemSet > &pSet)
virtual void getAllStyles(std::vector< std::shared_ptr< SfxItemSet >> &rStyles, SwAutoStyleFamily eFamily)=0
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!)
OUString aName
std::unique_ptr< IStylePoolIteratorAccess > createIterator(const bool bSkipUnusedItemSets=false, const bool bSkipIgnorableItems=false)
std::unique_ptr< IStyleAccess > createStyleManager(SfxItemSet const *pIgnorableParagraphItems)
virtual std::shared_ptr< SfxItemSet > cacheAutomaticStyle(const SfxItemSet &rSet, SwAutoStyleFamily eFamily)=0
insert the style to the pool and the cache (used during import)