1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "swstylemanager.hxx"
21 #include <svl/stylepool.hxx>
22 #include <istyleaccess.hxx>
23 #include <swatrset.hxx>
24 #include <unordered_map>
25 #include <osl/diagnose.h>
27 typedef std::unordered_map
< OUString
,
28 std::shared_ptr
<SfxItemSet
> > SwStyleNameCache
;
34 SwStyleNameCache mMap
;
37 void addStyleName( const std::shared_ptr
<SfxItemSet
>& pStyle
)
38 { mMap
[ StylePool::nameOf(pStyle
) ] = pStyle
; }
39 void addCompletePool( StylePool
& rPool
);
40 std::shared_ptr
<SfxItemSet
> getByName( const OUString
& rName
) { return mMap
[rName
]; }
41 void clear() { mMap
.clear(); }
46 void SwStyleCache::addCompletePool( StylePool
& rPool
)
48 std::unique_ptr
<IStylePoolIteratorAccess
> pIter
= rPool
.createIterator();
49 std::shared_ptr
<SfxItemSet
> pStyle
= pIter
->getNext();
52 OUString
aName( StylePool::nameOf(pStyle
) );
53 mMap
[ aName
] = pStyle
;
54 pStyle
= pIter
->getNext();
60 class SwStyleManager
: public IStyleAccess
62 StylePool m_aAutoCharPool
;
63 StylePool m_aAutoParaPool
;
64 SwStyleCache maCharCache
;
65 SwStyleCache maParaCache
;
68 // accept empty item set for ignorable paragraph items.
69 explicit SwStyleManager(SfxItemSet
const* pIgnorableParagraphItems
)
70 : m_aAutoParaPool(pIgnorableParagraphItems
)
72 virtual std::shared_ptr
<SfxItemSet
> getAutomaticStyle( const SfxItemSet
& rSet
,
73 IStyleAccess::SwAutoStyleFamily eFamily
,
74 const OUString
* pParentName
= nullptr ) override
;
75 virtual std::shared_ptr
<SfxItemSet
> getByName( const OUString
& rName
,
76 IStyleAccess::SwAutoStyleFamily eFamily
) override
;
77 virtual void getAllStyles( std::vector
<std::shared_ptr
<SfxItemSet
>> &rStyles
,
78 IStyleAccess::SwAutoStyleFamily eFamily
) override
;
79 virtual std::shared_ptr
<SfxItemSet
> cacheAutomaticStyle( const SfxItemSet
& rSet
,
80 SwAutoStyleFamily eFamily
) override
;
81 virtual void clearCaches() override
;
86 std::unique_ptr
<IStyleAccess
> createStyleManager( SfxItemSet
const * pIgnorableParagraphItems
)
88 return std::make_unique
<SwStyleManager
>( pIgnorableParagraphItems
);
91 void SwStyleManager::clearCaches()
97 std::shared_ptr
<SfxItemSet
> SwStyleManager::getAutomaticStyle( const SfxItemSet
& rSet
,
98 IStyleAccess::SwAutoStyleFamily eFamily
,
99 const OUString
* pParentName
)
101 assert(eFamily
!= IStyleAccess::AUTO_STYLE_PARA
|| dynamic_cast<const SwAttrSet
*>(&rSet
));
103 = eFamily
== IStyleAccess::AUTO_STYLE_CHAR
? m_aAutoCharPool
: m_aAutoParaPool
;
104 return rAutoPool
.insertItemSet( rSet
, pParentName
);
107 std::shared_ptr
<SfxItemSet
> SwStyleManager::cacheAutomaticStyle( const SfxItemSet
& rSet
,
108 IStyleAccess::SwAutoStyleFamily eFamily
)
110 assert(eFamily
!= IStyleAccess::AUTO_STYLE_PARA
|| dynamic_cast<const SwAttrSet
*>(&rSet
));
112 = eFamily
== IStyleAccess::AUTO_STYLE_CHAR
? m_aAutoCharPool
: m_aAutoParaPool
;
113 std::shared_ptr
<SfxItemSet
> pStyle
= rAutoPool
.insertItemSet( rSet
);
114 if (eFamily
== IStyleAccess::AUTO_STYLE_CHAR
)
116 maCharCache
.addStyleName( pStyle
);
120 maParaCache
.addStyleName( pStyle
);
125 std::shared_ptr
<SfxItemSet
> SwStyleManager::getByName( const OUString
& rName
,
126 IStyleAccess::SwAutoStyleFamily eFamily
)
129 = eFamily
== IStyleAccess::AUTO_STYLE_CHAR
? m_aAutoCharPool
: m_aAutoParaPool
;
130 SwStyleCache
&rCache
= eFamily
== IStyleAccess::AUTO_STYLE_CHAR
? maCharCache
: maParaCache
;
131 std::shared_ptr
<SfxItemSet
> pStyle
= rCache
.getByName( rName
);
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 rCache
.addCompletePool( rAutoPool
);
138 pStyle
= rCache
.getByName( rName
);
140 assert(!pStyle
|| eFamily
!= IStyleAccess::AUTO_STYLE_PARA
|| dynamic_cast<SwAttrSet
*>(pStyle
.get()));
144 void SwStyleManager::getAllStyles( std::vector
<std::shared_ptr
<SfxItemSet
>> &rStyles
,
145 IStyleAccess::SwAutoStyleFamily eFamily
)
148 = eFamily
== IStyleAccess::AUTO_STYLE_CHAR
? m_aAutoCharPool
: m_aAutoParaPool
;
149 // setup <StylePool> iterator, which skips unused styles and ignorable items
150 std::unique_ptr
<IStylePoolIteratorAccess
> pIter
= rAutoPool
.createIterator( true, true );
151 std::shared_ptr
<SfxItemSet
> pStyle
= pIter
->getNext();
154 assert(eFamily
!= IStyleAccess::AUTO_STYLE_PARA
|| dynamic_cast<SwAttrSet
*>(pStyle
.get()));
155 rStyles
.push_back( pStyle
);
157 pStyle
= pIter
->getNext();
161 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */