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_aAutoParaPool(pIgnorableParagraphItems)
69  {}
70  virtual std::shared_ptr<SfxItemSet> getAutomaticStyle( const SfxItemSet& rSet,
72  const OUString* pParentName = nullptr ) override;
73  virtual std::shared_ptr<SfxItemSet> getByName( const OUString& rName,
74  IStyleAccess::SwAutoStyleFamily eFamily ) override;
75  virtual void getAllStyles( std::vector<std::shared_ptr<SfxItemSet>> &rStyles,
76  IStyleAccess::SwAutoStyleFamily eFamily ) override;
77  virtual std::shared_ptr<SfxItemSet> cacheAutomaticStyle( const SfxItemSet& rSet,
78  SwAutoStyleFamily eFamily ) override;
79  virtual void clearCaches() override;
80 };
81 
82 }
83 
84 std::unique_ptr<IStyleAccess> createStyleManager( SfxItemSet const * pIgnorableParagraphItems )
85 {
86  return std::make_unique<SwStyleManager>( pIgnorableParagraphItems );
87 }
88 
89 void SwStyleManager::clearCaches()
90 {
91  mpCharCache.reset();
92  mpParaCache.reset();
93 }
94 
95 std::shared_ptr<SfxItemSet> SwStyleManager::getAutomaticStyle( const SfxItemSet& rSet,
97  const OUString* pParentName )
98 {
99  StylePool& rAutoPool
100  = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? m_aAutoCharPool : m_aAutoParaPool;
101  return rAutoPool.insertItemSet( rSet, pParentName );
102 }
103 
104 std::shared_ptr<SfxItemSet> SwStyleManager::cacheAutomaticStyle( const SfxItemSet& rSet,
106 {
107  StylePool& rAutoPool
108  = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? m_aAutoCharPool : m_aAutoParaPool;
109  std::shared_ptr<SfxItemSet> pStyle = rAutoPool.insertItemSet( rSet );
110  if (eFamily == IStyleAccess::AUTO_STYLE_CHAR)
111  {
112  if (!mpCharCache)
113  mpCharCache.reset(new SwStyleCache());
114  mpCharCache->addStyleName( pStyle );
115  }
116  else
117  {
118  if (!mpParaCache)
119  mpParaCache.reset(new SwStyleCache());
120  mpParaCache->addStyleName( pStyle );
121  }
122  return pStyle;
123 }
124 
125 std::shared_ptr<SfxItemSet> SwStyleManager::getByName( const OUString& rName,
127 {
128  StylePool& rAutoPool
129  = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? m_aAutoCharPool : m_aAutoParaPool;
130  std::unique_ptr<SwStyleCache> &rpCache = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? mpCharCache : mpParaCache;
131  if( !rpCache )
132  rpCache.reset(new SwStyleCache());
133  std::shared_ptr<SfxItemSet> pStyle = rpCache->getByName( rName );
134  if( !pStyle )
135  {
136  // Ok, ok, it's allowed to ask for uncached styles (from UNO) but it should not be done
137  // during loading a document
138  OSL_FAIL( "Don't ask for uncached styles" );
139  rpCache->addCompletePool( rAutoPool );
140  pStyle = rpCache->getByName( rName );
141  }
142  return pStyle;
143 }
144 
145 void SwStyleManager::getAllStyles( std::vector<std::shared_ptr<SfxItemSet>> &rStyles,
147 {
148  StylePool& rAutoPool
149  = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? m_aAutoCharPool : m_aAutoParaPool;
150  // setup <StylePool> iterator, which skips unused styles and ignorable items
151  std::unique_ptr<IStylePoolIteratorAccess> pIter = rAutoPool.createIterator( true, true );
152  std::shared_ptr<SfxItemSet> pStyle = pIter->getNext();
153  while( pStyle )
154  {
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 > 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)