LibreOffice Module sw (master)  1
tblafmt.hxx
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 #ifndef INCLUDED_SW_INC_TBLAFMT_HXX
20 #define INCLUDED_SW_INC_TBLAFMT_HXX
21 /*
22  * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
23  *
24  * The structure of table auto formatting should not be changed. It is used
25  * by different code of Writer and Calc. If a change is necessary, the
26  * source code of both applications must be changed!
27  *
28  * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
29  */
30 
31 #include <memory>
32 
34 #include <editeng/keepitem.hxx>
35 #include <editeng/frmdiritem.hxx>
36 #include <editeng/shaditem.hxx>
37 #include <svx/autoformathelper.hxx>
38 #include "fmtpdsc.hxx"
39 #include "fmtornt.hxx"
40 #include "swdllapi.h"
41 
42 struct SwAfVersions;
43 
44 class SvNumberFormatter;
45 class SwTable;
46 
48 {
49 private:
50  // Writer specific
51  std::unique_ptr<SvxFrameDirectionItem> m_aTextOrientation;
52  std::unique_ptr<SwFormatVertOrient> m_aVerticalAlignment;
53 
54  // number format
58 
59  css::uno::WeakReference<css::uno::XInterface> m_wXObject;
60 
61 public:
63  SwBoxAutoFormat( const SwBoxAutoFormat& rNew );
65 
68 
70  bool operator==(const SwBoxAutoFormat& rRight) const;
71 
72  // The get-methods.
75 
76  void GetValueFormat( OUString& rFormat, LanguageType& rLng, LanguageType& rSys ) const
77  { rFormat = m_sNumFormatString; rLng = m_eNumFormatLanguage; rSys = m_eSysLanguage; }
78 
79  const OUString& GetNumFormatString() const { return m_sNumFormatString; }
80  const LanguageType& GetSysLanguage() const { return m_eSysLanguage; }
82 
83  // The set-methods.
84  void SetTextOrientation( const SvxFrameDirectionItem& rNew ) { m_aTextOrientation.reset(rNew.Clone()); }
85  void SetVerticalAlignment( const SwFormatVertOrient& rNew ) { m_aVerticalAlignment.reset(rNew.Clone()); }
86 
87  void SetValueFormat( const OUString& rFormat, LanguageType eLng, LanguageType eSys )
88  { m_sNumFormatString = rFormat; m_eNumFormatLanguage = eLng; m_eSysLanguage = eSys; }
89 
90  void SetNumFormatString(const OUString& rNew) { m_sNumFormatString = rNew; }
91  void SetSysLanguage(const LanguageType& rNew) { m_eSysLanguage = rNew; }
92  void SetNumFormatLanguage(const LanguageType& rNew) { m_eNumFormatLanguage = rNew; }
93 
94  css::uno::WeakReference<css::uno::XInterface> const& GetXObject() const
95  { return m_wXObject; }
96  void SetXObject(css::uno::Reference<css::uno::XInterface> const& xObject)
97  { m_wXObject = xObject; }
98 
99  bool Load( SvStream& rStream, const SwAfVersions& rVersions, sal_uInt16 nVer );
100  bool Save( SvStream& rStream, sal_uInt16 fileVersion ) const;
101 };
102 
103 enum class SwTableAutoFormatUpdateFlags { Char = 1, Box = 2 };
104 namespace o3tl {
105  template<> struct typed_flags<SwTableAutoFormatUpdateFlags> : is_typed_flags<SwTableAutoFormatUpdateFlags, 0x03> {};
106 };
107 
108 /*
109 @remarks
110 A table has a number of lines. These lines seem to correspond with rows, except in the case of
111 rows spanning more than one line. Each line contains a number of boxes/cells.
112 
113 AutoFormat properties are retrieved and stored in a grid of 16 table boxes. A sampling approach
114 is used to read the data. 4 lines are picked, and 4 boxes are picked from each.
115 
116 The line picking and box picking algorithms are similar. We start at the first line/box, and pick
117 lines/boxes one by one for a maximum of 3. The 4th line/box is the last line/box in the current
118 table/line. If we hit the end of lines/boxes, the last line/box encountered is picked several times.
119 
120 For example, in a 2x3 table, the 4 lines will be [0, 1, 1, 1]. In each line, the boxes will be
121 [0, 1, 2, 2]. In a 6x5 table, the 4 lines will be [0, 1, 2, 4] and the boxes per line will be
122 [0, 1, 2, 5].
123 
124 As you can see, property extraction/application is lossless for tables that are 4x4 or smaller
125 (and in fact has a bit of redundancy). For larger tables, we lose any individual cell formatting
126 for the range [(3,rows - 1) -> (3, cols - 1)]. That formatting is replaced by formatting from
127 the saved cells:
128 
129  0 1 2 3 4 5
130  +-----------------------------------------------------------------------+
131  0 | Saved | Saved | Saved | | | Saved |
132  +-----------------------------------------------------------------------+
133  1 | Saved | Saved | Saved | | | Saved |
134  +-----------------------------------------------------------------------+
135  2 | Saved | Saved | Saved | | | Saved |
136  +-----------------------------------------------------------------------+
137  3 | | | | | | |
138  +-----------------------------------------------------------------------+
139  4 | | | | | | |
140  +-----------------------------------------------------------------------+
141  5 | Saved | Saved | Saved | | | Saved |
142  +-----------+-----------+-----------+-----------+-----------+-----------+
143 
144 The properties saved are divided into three categories:
145  1. Character properties: Font, font size, weight, etc.
146  2. Box properties: Box, cell background
147  3. Table properties: Properties that are set in the Table->Table Properties dialog.
148 
149 Character and box properties are stored per cell (and are lossy for tables larger than 4x4). Table
150 properties are stored per-table, and are lossless.
151 */
153 {
154  friend class SwDocTest;
155  friend void FinitCore(); // To destroy default pointer.
157 
158  css::uno::WeakReference<css::uno::XInterface> m_wXObject;
159 
160  OUString m_aName;
161  sal_uInt16 m_nStrResId;
162 
163  // Common flags of Calc and Writer.
164  bool m_bInclFont : 1;
165  bool m_bInclJustify : 1;
166  bool m_bInclFrame : 1;
167  bool m_bInclBackground : 1;
168  bool m_bInclValueFormat : 1;
169 
170  // Calc specific flags.
171  bool m_bInclWidthHeight : 1;
172 
173  SwBoxAutoFormat* m_aBoxAutoFormat[ 16 ] = {};
174 
175  // Writer-specific options
176  std::shared_ptr<SvxFormatKeepItem> m_aKeepWithNextPara;
177  sal_uInt16 m_aRepeatHeading;
181  std::shared_ptr<SvxShadowItem> m_aShadow;
182 
183  bool m_bHidden;
185 public:
186  SwTableAutoFormat( const OUString& rName );
187  SwTableAutoFormat( const SwTableAutoFormat& rNew );
189 
190  SwTableAutoFormat& operator=( const SwTableAutoFormat& rNew );
191 
192  const SvxFormatKeepItem& GetKeepWithNextPara() const { return *m_aKeepWithNextPara; }
193  const SvxShadowItem& GetShadow() const { return *m_aShadow; }
194 
195  void SetKeepWithNextPara(const SvxFormatKeepItem& rNew) { m_aKeepWithNextPara.reset(rNew.Clone()); }
196  void SetShadow(const SvxShadowItem& rNew) { m_aShadow.reset(rNew.Clone()); }
197 
198  void SetBoxFormat( const SwBoxAutoFormat& rNew, sal_uInt8 nPos );
199  const SwBoxAutoFormat& GetBoxFormat( sal_uInt8 nPos ) const;
200  SwBoxAutoFormat& GetBoxFormat( sal_uInt8 nPos );
201  static const SwBoxAutoFormat& GetDefaultBoxFormat();
202 
203  void SetName( const OUString& rNew ) { m_aName = rNew; m_nStrResId = USHRT_MAX; }
204  const OUString& GetName() const { return m_aName; }
205 
206  void UpdateFromSet( sal_uInt8 nPos, const SfxItemSet& rSet,
208  void UpdateToSet( const sal_uInt8 nPos, const bool bSingleRowTable, const bool bSingleColTable,
210  SvNumberFormatter* ) const ;
211 
212  void RestoreTableProperties(SwTable &table) const;
213  void StoreTableProperties(const SwTable &table);
214 
215  bool IsFont() const { return m_bInclFont; }
216  bool IsJustify() const { return m_bInclJustify; }
217  bool IsFrame() const { return m_bInclFrame; }
218  bool IsBackground() const { return m_bInclBackground; }
219  bool IsValueFormat() const { return m_bInclValueFormat; }
220 
222  bool IsHidden() const { return m_bHidden; }
224  bool IsUserDefined() const { return m_bUserDefined; }
225 
226  void SetFont( const bool bNew ) { m_bInclFont = bNew; }
227  void SetJustify( const bool bNew ) { m_bInclJustify = bNew; }
228  void SetFrame( const bool bNew ) { m_bInclFrame = bNew; }
229  void SetBackground( const bool bNew ) { m_bInclBackground = bNew; }
230  void SetValueFormat( const bool bNew ) { m_bInclValueFormat = bNew; }
231  void SetWidthHeight( const bool bNew ) { m_bInclWidthHeight = bNew; }
232 
234  void SetHidden(bool bHidden) { m_bHidden = bHidden; }
236  void SetUserDefined(bool bUserDefined) { m_bUserDefined = bUserDefined; }
237 
239  bool FirstRowEndColumnIsRow();
240  bool FirstRowStartColumnIsRow();
241  bool LastRowEndColumnIsRow();
242  bool LastRowStartColumnIsRow();
243 
244  bool Load( SvStream& rStream, const SwAfVersions& );
245  bool Save( SvStream& rStream, sal_uInt16 fileVersion ) const;
246 
247  css::uno::WeakReference<css::uno::XInterface> const& GetXObject() const
248  { return m_wXObject; }
249  void SetXObject(css::uno::Reference<css::uno::XInterface> const& xObject)
250  { m_wXObject = xObject; }
251 
253  OUString GetTableTemplateCellSubName(const SwBoxAutoFormat& rBoxFormat) const;
255  static const std::vector<sal_Int32>& GetTableTemplateMap();
256 
261  static sal_uInt8 CountPos(sal_uInt32 nCol, sal_uInt32 nCols, sal_uInt32 nRow, sal_uInt32 nRows);
262 };
263 
265 {
266  struct Impl;
267  std::unique_ptr<Impl> m_pImpl;
268 
269  SAL_DLLPRIVATE bool Load( SvStream& rStream );
270  SAL_DLLPRIVATE bool Save( SvStream& rStream ) const;
271 
272 public:
273  explicit SwTableAutoFormatTable();
275 
276  size_t size() const;
277  SwTableAutoFormat const& operator[](size_t i) const;
278  SwTableAutoFormat & operator[](size_t i);
279 
281  void AddAutoFormat(const SwTableAutoFormat& rFormat);
282 
283  void InsertAutoFormat(size_t i, std::unique_ptr<SwTableAutoFormat> pFormat);
284  void EraseAutoFormat(size_t i);
285  void EraseAutoFormat(const OUString& rName);
286  std::unique_ptr<SwTableAutoFormat> ReleaseAutoFormat(size_t i);
288  std::unique_ptr<SwTableAutoFormat> ReleaseAutoFormat(const OUString& rName);
289 
291  SwTableAutoFormat* FindAutoFormat(std::u16string_view rName) const;
292 
293  void Load();
294  bool Save() const;
295 };
296 
298 {
299  const std::pair<OUString, std::unique_ptr<SwBoxAutoFormat>>& m_rCellStyleDesc;
300 public:
301  SwCellStyleDescriptor(const std::pair<OUString, std::unique_ptr<SwBoxAutoFormat>>& rCellStyleDesc) : m_rCellStyleDesc(rCellStyleDesc) { }
302 
303  const OUString& GetName() const { return m_rCellStyleDesc.first; }
304 };
305 
307 {
308  std::vector<std::pair<OUString, std::unique_ptr<SwBoxAutoFormat>>> m_aCellStyles;
309 public:
312 
313  size_t size() const;
314  SwCellStyleDescriptor operator[](size_t i) const;
315  void clear();
316 
318  void AddBoxFormat(const SwBoxAutoFormat& rBoxFormat, const OUString& sName);
319  void RemoveBoxFormat(const OUString& sName);
320  void ChangeBoxFormatName(std::u16string_view sFromName, const OUString& sToName);
322  OUString GetBoxFormatName(const SwBoxAutoFormat& rBoxFormat) const;
324  SwBoxAutoFormat* GetBoxFormat(std::u16string_view sName) const;
325 };
326 
327 #endif
328 
329 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void SetNumFormatString(const OUString &rNew)
Definition: tblafmt.hxx:90
void SetName(const OUString &rNew)
Definition: tblafmt.hxx:203
void SetValueFormat(const OUString &rFormat, LanguageType eLng, LanguageType eSys)
Definition: tblafmt.hxx:87
const OUString & GetName() const
Definition: tblafmt.hxx:303
bool IsValueFormat() const
Definition: tblafmt.hxx:219
void SetValueFormat(const bool bNew)
Definition: tblafmt.hxx:230
void SetVerticalAlignment(const SwFormatVertOrient &rNew)
Definition: tblafmt.hxx:85
std::shared_ptr< SvxFormatKeepItem > m_aKeepWithNextPara
Definition: tblafmt.hxx:176
SwCellStyleDescriptor(const std::pair< OUString, std::unique_ptr< SwBoxAutoFormat >> &rCellStyleDesc)
Definition: tblafmt.hxx:301
void RemoveBoxFormat(const OUString &sName)
Definition: tblafmt.cxx:1189
void SetFrame(const bool bNew)
Definition: tblafmt.hxx:228
std::vector< std::pair< OUString, std::unique_ptr< SwBoxAutoFormat > > > m_aCellStyles
Definition: tblafmt.hxx:308
virtual SvxShadowItem * Clone(SfxItemPool *pPool=nullptr) const override
css::uno::WeakReference< css::uno::XInterface > m_wXObject
Definition: tblafmt.hxx:158
void SetTextOrientation(const SvxFrameDirectionItem &rNew)
Definition: tblafmt.hxx:84
css::uno::WeakReference< css::uno::XInterface > const & GetXObject() const
Definition: tblafmt.hxx:247
std::unique_ptr< SwFormatVertOrient > m_aVerticalAlignment
Definition: tblafmt.hxx:52
sal_uInt16 m_aRepeatHeading
Definition: tblafmt.hxx:177
void ChangeBoxFormatName(std::u16string_view sFromName, const OUString &sToName)
Definition: tblafmt.cxx:1224
const OUString & GetNumFormatString() const
Definition: tblafmt.hxx:79
bool IsFont() const
Definition: tblafmt.hxx:215
bool operator==(const SwBoxAutoFormat &rRight) const
Comparing based of boxes backgrounds.
Definition: tblafmt.cxx:306
std::unique_ptr< SvxFrameDirectionItem > m_aTextOrientation
Definition: tblafmt.hxx:51
bool m_bCollapsingBorders
Definition: tblafmt.hxx:180
void SetUserDefined(bool bUserDefined)
Set if style is user defined.
Definition: tblafmt.hxx:236
void SetXObject(css::uno::Reference< css::uno::XInterface > const &xObject)
Definition: tblafmt.hxx:96
bool IsJustify() const
Definition: tblafmt.hxx:216
void SetFont(const bool bNew)
Definition: tblafmt.hxx:226
const SvxFormatKeepItem & GetKeepWithNextPara() const
Definition: tblafmt.hxx:192
virtual SvxFrameDirectionItem * Clone(SfxItemPool *pPool=nullptr) const override
void SetNumFormatLanguage(const LanguageType &rNew)
Definition: tblafmt.hxx:92
bool IsBackground() const
Definition: tblafmt.hxx:218
OUString m_aName
Definition: tblafmt.hxx:160
const LanguageType & GetNumFormatLanguage() const
Definition: tblafmt.hxx:81
void SetJustify(const bool bNew)
Definition: tblafmt.hxx:227
SwTableAutoFormatUpdateFlags
Definition: tblafmt.hxx:103
std::shared_ptr< SvxShadowItem > m_aShadow
Definition: tblafmt.hxx:181
SwBoxAutoFormat * GetBoxFormat(std::u16string_view sName) const
If found returns a ptr to a BoxFormat. If not found returns nullptr.
Definition: tblafmt.cxx:1213
const SvxFrameDirectionItem & GetTextOrientation() const
Definition: tblafmt.hxx:73
virtual SwFormatVertOrient * Clone(SfxItemPool *pPool=nullptr) const override
Definition: atrfrm.cxx:1382
size_t size() const
Definition: tblafmt.cxx:1169
bool IsUserDefined() const
Check if style is defined by user.
Definition: tblafmt.hxx:224
void FinitCore()
Definition: init.cxx:665
void SetShadow(const SvxShadowItem &rNew)
Definition: tblafmt.hxx:196
LanguageType m_eNumFormatLanguage
Definition: tblafmt.hxx:57
bool Save(SvStream &rStream, sal_uInt16 fileVersion) const
Definition: tblafmt.cxx:344
bool Load(SvStream &rStream, const SwAfVersions &rVersions, sal_uInt16 nVer)
Definition: tblafmt.cxx:311
void SetXObject(css::uno::Reference< css::uno::XInterface > const &xObject)
Definition: tblafmt.hxx:249
SwBoxAutoFormat & operator=(const SwBoxAutoFormat &rRef)
assignment-op (still used)
Definition: tblafmt.cxx:281
size
void SetWidthHeight(const bool bNew)
Definition: tblafmt.hxx:231
void SetBackground(const bool bNew)
Definition: tblafmt.hxx:229
css::uno::WeakReference< css::uno::XInterface > const & GetXObject() const
Definition: tblafmt.hxx:94
#define SW_DLLPUBLIC
Definition: swdllapi.h:28
std::unique_ptr< Impl > m_pImpl
Definition: tblafmt.hxx:266
SwTable is one table in the document model, containing rows (which contain cells).
Definition: swtable.hxx:111
void GetValueFormat(OUString &rFormat, LanguageType &rLng, LanguageType &rSys) const
Definition: tblafmt.hxx:76
OUString m_sNumFormatString
Definition: tblafmt.hxx:55
void SetHidden(bool bHidden)
Set if style is hidden.
Definition: tblafmt.hxx:234
const OUString & GetName() const
Definition: tblafmt.hxx:204
const SwFormatVertOrient & GetVerticalAlignment() const
Definition: tblafmt.hxx:74
bool IsHidden() const
Check if style is hidden.
Definition: tblafmt.hxx:222
unsigned char sal_uInt8
css::uno::WeakReference< css::uno::XInterface > m_wXObject
Definition: tblafmt.hxx:59
const LanguageType & GetSysLanguage() const
Definition: tblafmt.hxx:80
SwCellStyleDescriptor operator[](size_t i) const
Definition: tblafmt.cxx:1179
OUString GetBoxFormatName(const SwBoxAutoFormat &rBoxFormat) const
If found returns its name. If not found returns an empty OUString.
Definition: tblafmt.cxx:1201
void AddBoxFormat(const SwBoxAutoFormat &rBoxFormat, const OUString &sName)
Add a copy of rBoxFormat.
Definition: tblafmt.cxx:1184
const SvxShadowItem & GetShadow() const
Definition: tblafmt.hxx:193
const std::pair< OUString, std::unique_ptr< SwBoxAutoFormat > > & m_rCellStyleDesc
Definition: tblafmt.hxx:299
LanguageType m_eSysLanguage
Definition: tblafmt.hxx:56
static SwBoxAutoFormat * s_pDefaultBoxAutoFormat
Definition: tblafmt.hxx:156
void SetKeepWithNextPara(const SvxFormatKeepItem &rNew)
Definition: tblafmt.hxx:195
void SetSysLanguage(const LanguageType &rNew)
Definition: tblafmt.hxx:91
virtual SvxFormatKeepItem * Clone(SfxItemPool *pPool=nullptr) const override
bool IsFrame() const
Definition: tblafmt.hxx:217
sal_uInt16 m_nStrResId
Definition: tblafmt.hxx:161