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>
28 typedef std::unordered_map
< OUString
,
29 std::shared_ptr
<SfxItemSet
> > SwStyleNameCache
;
35 SwStyleNameCache mMap
;
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 void clear() { mMap
.clear(); }
47 void SwStyleCache::addCompletePool( StylePool
& rPool
)
49 rPool
.populateCacheMap(mMap
);
54 class SwStyleManager
: public IStyleAccess
56 StylePool m_aAutoCharPool
;
57 StylePool m_aAutoParaPool
;
58 SwStyleCache maCharCache
;
59 SwStyleCache maParaCache
;
62 // accept empty item set for ignorable paragraph items.
63 explicit SwStyleManager(SfxItemSet
const* pIgnorableParagraphItems
)
64 : m_aAutoParaPool(pIgnorableParagraphItems
)
66 virtual std::shared_ptr
<SfxItemSet
> getAutomaticStyle( const SfxItemSet
& rSet
,
67 IStyleAccess::SwAutoStyleFamily eFamily
,
68 const OUString
* pParentName
= nullptr ) override
;
69 virtual std::shared_ptr
<SwAttrSet
> getAutomaticStyle( const SwAttrSet
& rSet
,
70 IStyleAccess::SwAutoStyleFamily eFamily
,
71 const ProgName
* 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
;
83 std::unique_ptr
<IStyleAccess
> createStyleManager( SfxItemSet
const * pIgnorableParagraphItems
)
85 return std::make_unique
<SwStyleManager
>( pIgnorableParagraphItems
);
88 void SwStyleManager::clearCaches()
94 std::shared_ptr
<SfxItemSet
> SwStyleManager::getAutomaticStyle( const SfxItemSet
& rSet
,
95 IStyleAccess::SwAutoStyleFamily eFamily
,
96 const OUString
* pParentName
)
98 assert(eFamily
!= IStyleAccess::AUTO_STYLE_PARA
|| dynamic_cast<const SwAttrSet
*>(&rSet
));
100 = eFamily
== IStyleAccess::AUTO_STYLE_CHAR
? m_aAutoCharPool
: m_aAutoParaPool
;
101 return rAutoPool
.insertItemSet( rSet
, pParentName
);
104 std::shared_ptr
<SwAttrSet
> SwStyleManager::getAutomaticStyle( const SwAttrSet
& rSet
,
105 IStyleAccess::SwAutoStyleFamily eFamily
,
106 const ProgName
* pParentName
)
109 = eFamily
== IStyleAccess::AUTO_STYLE_CHAR
? m_aAutoCharPool
: m_aAutoParaPool
;
110 std::shared_ptr
<SfxItemSet
> pItemSet
= rAutoPool
.insertItemSet( rSet
, pParentName
? &pParentName
->toString() : nullptr );
111 std::shared_ptr
<SwAttrSet
> pAttrSet
= std::dynamic_pointer_cast
<SwAttrSet
>(pItemSet
);
112 assert(bool(pItemSet
) == bool(pAttrSet
) && "types do not match");
116 std::shared_ptr
<SfxItemSet
> SwStyleManager::cacheAutomaticStyle( const SfxItemSet
& rSet
,
117 IStyleAccess::SwAutoStyleFamily eFamily
)
119 assert(eFamily
!= IStyleAccess::AUTO_STYLE_PARA
|| dynamic_cast<const SwAttrSet
*>(&rSet
));
121 = eFamily
== IStyleAccess::AUTO_STYLE_CHAR
? m_aAutoCharPool
: m_aAutoParaPool
;
122 std::shared_ptr
<SfxItemSet
> pStyle
= rAutoPool
.insertItemSet( rSet
);
123 if (eFamily
== IStyleAccess::AUTO_STYLE_CHAR
)
125 maCharCache
.addStyleName( pStyle
);
129 maParaCache
.addStyleName( pStyle
);
134 std::shared_ptr
<SfxItemSet
> SwStyleManager::getByName( const OUString
& rName
,
135 IStyleAccess::SwAutoStyleFamily eFamily
)
138 = eFamily
== IStyleAccess::AUTO_STYLE_CHAR
? m_aAutoCharPool
: m_aAutoParaPool
;
139 SwStyleCache
&rCache
= eFamily
== IStyleAccess::AUTO_STYLE_CHAR
? maCharCache
: maParaCache
;
140 std::shared_ptr
<SfxItemSet
> pStyle
= rCache
.getByName( rName
);
143 // Ok, ok, it's allowed to ask for uncached styles (from UNO) but it should not be done
144 // during loading a document
145 OSL_FAIL( "Don't ask for uncached styles" );
146 rCache
.addCompletePool( rAutoPool
);
147 pStyle
= rCache
.getByName( rName
);
149 assert(!pStyle
|| eFamily
!= IStyleAccess::AUTO_STYLE_PARA
|| dynamic_cast<SwAttrSet
*>(pStyle
.get()));
153 void SwStyleManager::getAllStyles( std::vector
<std::shared_ptr
<SfxItemSet
>> &rStyles
,
154 IStyleAccess::SwAutoStyleFamily eFamily
)
157 = eFamily
== IStyleAccess::AUTO_STYLE_CHAR
? m_aAutoCharPool
: m_aAutoParaPool
;
158 rAutoPool
.getAllStyles(rStyles
);
161 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */