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 aAutoCharPool;
61  StylePool 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  : aAutoCharPool(),
69  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 = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? aAutoCharPool : aAutoParaPool;
101  return rAutoPool.insertItemSet( rSet, pParentName );
102 }
103 
104 std::shared_ptr<SfxItemSet> SwStyleManager::cacheAutomaticStyle( const SfxItemSet& rSet,
106 {
107  StylePool& rAutoPool = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? aAutoCharPool : aAutoParaPool;
108  std::shared_ptr<SfxItemSet> pStyle = rAutoPool.insertItemSet( rSet );
109  if (eFamily == IStyleAccess::AUTO_STYLE_CHAR)
110  {
111  if (!mpCharCache)
112  mpCharCache.reset(new SwStyleCache());
113  mpCharCache->addStyleName( pStyle );
114  }
115  else
116  {
117  if (!mpParaCache)
118  mpParaCache.reset(new SwStyleCache());
119  mpParaCache->addStyleName( pStyle );
120  }
121  return pStyle;
122 }
123 
124 std::shared_ptr<SfxItemSet> SwStyleManager::getByName( const OUString& rName,
126 {
127  StylePool& rAutoPool = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? aAutoCharPool : aAutoParaPool;
128  std::unique_ptr<SwStyleCache> &rpCache = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? mpCharCache : mpParaCache;
129  if( !rpCache )
130  rpCache.reset(new SwStyleCache());
131  std::shared_ptr<SfxItemSet> pStyle = rpCache->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  rpCache->addCompletePool( rAutoPool );
138  pStyle = rpCache->getByName( rName );
139  }
140  return pStyle;
141 }
142 
143 void SwStyleManager::getAllStyles( std::vector<std::shared_ptr<SfxItemSet>> &rStyles,
145 {
146  StylePool& rAutoPool = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? aAutoCharPool : aAutoParaPool;
147  // setup <StylePool> iterator, which skips unused styles and ignorable items
148  std::unique_ptr<IStylePoolIteratorAccess> pIter = rAutoPool.createIterator( true, true );
149  std::shared_ptr<SfxItemSet> pStyle = pIter->getNext();
150  while( pStyle )
151  {
152  rStyles.push_back( pStyle );
153 
154  pStyle = pIter->getNext();
155  }
156 }
157 
158 /* 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)