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