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 
30 {
32 public:
34  void addStyleName( const std::shared_ptr<SfxItemSet>& pStyle )
35  { mMap[ StylePool::nameOf(pStyle) ] = pStyle; }
36  void addCompletePool( StylePool& rPool );
37  std::shared_ptr<SfxItemSet> getByName( const OUString& rName ) { return mMap[rName]; }
38 };
39 
41 {
42  std::unique_ptr<IStylePoolIteratorAccess> pIter = rPool.createIterator();
43  std::shared_ptr<SfxItemSet> pStyle = pIter->getNext();
44  while( pStyle.get() )
45  {
46  OUString aName( StylePool::nameOf(pStyle) );
47  mMap[ aName ] = pStyle;
48  pStyle = pIter->getNext();
49  }
50 }
51 
53 {
56  std::unique_ptr<SwStyleCache> mpCharCache;
57  std::unique_ptr<SwStyleCache> mpParaCache;
58 
59 public:
60  // accept empty item set for ignorable paragraph items.
61  explicit SwStyleManager( SfxItemSet const * pIgnorableParagraphItems )
62  : aAutoCharPool(),
63  aAutoParaPool( pIgnorableParagraphItems )
64  {}
65  virtual std::shared_ptr<SfxItemSet> getAutomaticStyle( const SfxItemSet& rSet,
67  const OUString* pParentName = nullptr ) override;
68  virtual std::shared_ptr<SfxItemSet> getByName( const OUString& rName,
69  IStyleAccess::SwAutoStyleFamily eFamily ) override;
70  virtual void getAllStyles( std::vector<std::shared_ptr<SfxItemSet>> &rStyles,
71  IStyleAccess::SwAutoStyleFamily eFamily ) override;
72  virtual std::shared_ptr<SfxItemSet> cacheAutomaticStyle( const SfxItemSet& rSet,
73  SwAutoStyleFamily eFamily ) override;
74  virtual void clearCaches() override;
75 };
76 
77 std::unique_ptr<IStyleAccess> createStyleManager( SfxItemSet const * pIgnorableParagraphItems )
78 {
79  return std::make_unique<SwStyleManager>( pIgnorableParagraphItems );
80 }
81 
83 {
84  mpCharCache.reset();
85  mpParaCache.reset();
86 }
87 
88 std::shared_ptr<SfxItemSet> SwStyleManager::getAutomaticStyle( const SfxItemSet& rSet,
90  const OUString* pParentName )
91 {
93  return rAutoPool.insertItemSet( rSet, pParentName );
94 }
95 
96 std::shared_ptr<SfxItemSet> SwStyleManager::cacheAutomaticStyle( const SfxItemSet& rSet,
98 {
100  std::shared_ptr<SfxItemSet> pStyle = rAutoPool.insertItemSet( rSet );
101  if (eFamily == IStyleAccess::AUTO_STYLE_CHAR)
102  {
103  if (!mpCharCache)
104  mpCharCache.reset(new SwStyleCache());
105  mpCharCache->addStyleName( pStyle );
106  }
107  else
108  {
109  if (!mpParaCache)
110  mpParaCache.reset(new SwStyleCache());
111  mpParaCache->addStyleName( pStyle );
112  }
113  return pStyle;
114 }
115 
116 std::shared_ptr<SfxItemSet> SwStyleManager::getByName( const OUString& rName,
118 {
120  std::unique_ptr<SwStyleCache> &rpCache = eFamily == IStyleAccess::AUTO_STYLE_CHAR ? mpCharCache : mpParaCache;
121  if( !rpCache )
122  rpCache.reset(new SwStyleCache());
123  std::shared_ptr<SfxItemSet> pStyle = rpCache->getByName( rName );
124  if( !pStyle.get() )
125  {
126  // Ok, ok, it's allowed to ask for uncached styles (from UNO) but it should not be done
127  // during loading a document
128  OSL_FAIL( "Don't ask for uncached styles" );
129  rpCache->addCompletePool( rAutoPool );
130  pStyle = rpCache->getByName( rName );
131  }
132  return pStyle;
133 }
134 
135 void SwStyleManager::getAllStyles( std::vector<std::shared_ptr<SfxItemSet>> &rStyles,
137 {
139  // setup <StylePool> iterator, which skips unused styles and ignorable items
140  std::unique_ptr<IStylePoolIteratorAccess> pIter = rAutoPool.createIterator( true, true );
141  std::shared_ptr<SfxItemSet> pStyle = pIter->getNext();
142  while( pStyle.get() )
143  {
144  rStyles.push_back( pStyle );
145 
146  pStyle = pIter->getNext();
147  }
148 }
149 
150 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::shared_ptr< SfxItemSet > insertItemSet(const SfxItemSet &rSet, const OUString *pParentName=nullptr)
virtual std::shared_ptr< SfxItemSet > cacheAutomaticStyle(const SfxItemSet &rSet, SwAutoStyleFamily eFamily) override
insert the style to the pool and the cache (used during import)
std::unordered_map< OUString, std::shared_ptr< SfxItemSet > > SwStyleNameCache
StylePool aAutoCharPool
SwStyleManager(SfxItemSet const *pIgnorableParagraphItems)
static OUString nameOf(const std::shared_ptr< SfxItemSet > &pSet)
virtual std::shared_ptr< SfxItemSet > getByName(const OUString &rName, IStyleAccess::SwAutoStyleFamily eFamily) override
It's slow to iterate through a stylepool looking for a special name, but if the style has been insert...
void addStyleName(const std::shared_ptr< SfxItemSet > &pStyle)
StylePool aAutoParaPool
virtual std::shared_ptr< SfxItemSet > getAutomaticStyle(const SfxItemSet &rSet, IStyleAccess::SwAutoStyleFamily eFamily, const OUString *pParentName=nullptr) override
virtual void clearCaches() override
To release the cached styles (shared_pointer!)
std::unique_ptr< SwStyleCache > mpParaCache
SwStyleNameCache mMap
virtual void getAllStyles(std::vector< std::shared_ptr< SfxItemSet >> &rStyles, IStyleAccess::SwAutoStyleFamily eFamily) override
OString const aName
std::unique_ptr< IStylePoolIteratorAccess > createIterator(const bool bSkipUnusedItemSets=false, const bool bSkipIgnorableItems=false)
std::unique_ptr< IStyleAccess > createStyleManager(SfxItemSet const *pIgnorableParagraphItems)
std::shared_ptr< SfxItemSet > getByName(const OUString &rName)
void addCompletePool(StylePool &rPool)
std::unique_ptr< SwStyleCache > mpCharCache