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);
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(static_cast<SvxFrameDirectionItem*>(rNew.Clone())); }
85  void SetVerticalAlignment( const SwFormatVertOrient& rNew ) { m_aVerticalAlignment.reset(static_cast<SwFormatVertOrient*>(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<SvxFormatBreakItem> m_aBreak;
178  std::shared_ptr<SvxFormatKeepItem> m_aKeepWithNextPara;
179  sal_uInt16 m_aRepeatHeading;
183  std::shared_ptr<SvxShadowItem> m_aShadow;
184 
185  bool m_bHidden;
187 public:
188  SwTableAutoFormat( const OUString& rName );
189  SwTableAutoFormat( const SwTableAutoFormat& rNew );
191 
192  SwTableAutoFormat& operator=( const SwTableAutoFormat& rNew );
193 
194  const SvxFormatBreakItem& GetBreak() const { return *m_aBreak; }
195  const SvxFormatKeepItem& GetKeepWithNextPara() const { return *m_aKeepWithNextPara; }
196  const SvxShadowItem& GetShadow() const { return *m_aShadow; }
197 
198  void SetBreak(const SvxFormatBreakItem& rNew) { m_aBreak.reset(static_cast<SvxFormatBreakItem*>(rNew.Clone())); }
199  void SetKeepWithNextPara(const SvxFormatKeepItem& rNew) { m_aKeepWithNextPara.reset(static_cast<SvxFormatKeepItem*>(rNew.Clone())); }
200  void SetShadow(const SvxShadowItem& rNew) { m_aShadow.reset(static_cast<SvxShadowItem*>(rNew.Clone())); }
201 
202  void SetBoxFormat( const SwBoxAutoFormat& rNew, sal_uInt8 nPos );
203  const SwBoxAutoFormat& GetBoxFormat( sal_uInt8 nPos ) const;
204  SwBoxAutoFormat& GetBoxFormat( sal_uInt8 nPos );
205  static const SwBoxAutoFormat& GetDefaultBoxFormat();
206 
207  void SetName( const OUString& rNew ) { m_aName = rNew; m_nStrResId = USHRT_MAX; }
208  const OUString& GetName() const { return m_aName; }
209 
210  void UpdateFromSet( sal_uInt8 nPos, const SfxItemSet& rSet,
212  void UpdateToSet( sal_uInt8 nPos, SfxItemSet& rSet, SwTableAutoFormatUpdateFlags eFlags,
213  SvNumberFormatter* ) const ;
214 
215  void RestoreTableProperties(SwTable &table) const;
216  void StoreTableProperties(const SwTable &table);
217 
218  bool IsFont() const { return m_bInclFont; }
219  bool IsJustify() const { return m_bInclJustify; }
220  bool IsFrame() const { return m_bInclFrame; }
221  bool IsBackground() const { return m_bInclBackground; }
222  bool IsValueFormat() const { return m_bInclValueFormat; }
223 
225  bool IsHidden() const { return m_bHidden; }
227  bool IsUserDefined() const { return m_bUserDefined; }
228 
229  void SetFont( const bool bNew ) { m_bInclFont = bNew; }
230  void SetJustify( const bool bNew ) { m_bInclJustify = bNew; }
231  void SetFrame( const bool bNew ) { m_bInclFrame = bNew; }
232  void SetBackground( const bool bNew ) { m_bInclBackground = bNew; }
233  void SetValueFormat( const bool bNew ) { m_bInclValueFormat = bNew; }
234  void SetWidthHeight( const bool bNew ) { m_bInclWidthHeight = bNew; }
235 
237  void SetHidden(bool bHidden) { m_bHidden = bHidden; }
239  void SetUserDefined(bool bUserDefined) { m_bUserDefined = bUserDefined; }
240 
242  bool FirstRowEndColumnIsRow();
243  bool FirstRowStartColumnIsRow();
244  bool LastRowEndColumnIsRow();
245  bool LastRowStartColumnIsRow();
246 
247  bool Load( SvStream& rStream, const SwAfVersions& );
248  bool Save( SvStream& rStream, sal_uInt16 fileVersion ) const;
249 
250  css::uno::WeakReference<css::uno::XInterface> const& GetXObject() const
251  { return m_wXObject; }
252  void SetXObject(css::uno::Reference<css::uno::XInterface> const& xObject)
253  { m_wXObject = xObject; }
254 
256  OUString GetTableTemplateCellSubName(const SwBoxAutoFormat& rBoxFormat) const;
258  static const std::vector<sal_Int32>& GetTableTemplateMap();
259 
264  static sal_uInt8 CountPos(sal_uInt32 nCol, sal_uInt32 nCols, sal_uInt32 nRow, sal_uInt32 nRows);
265 };
266 
268 {
269  struct Impl;
270  std::unique_ptr<Impl> m_pImpl;
271 
272  SAL_DLLPRIVATE bool Load( SvStream& rStream );
273  SAL_DLLPRIVATE bool Save( SvStream& rStream ) const;
274 
275 public:
276  explicit SwTableAutoFormatTable();
278 
279  size_t size() const;
280  SwTableAutoFormat const& operator[](size_t i) const;
281  SwTableAutoFormat & operator[](size_t i);
282 
284  void AddAutoFormat(const SwTableAutoFormat& rFormat);
285 
286  void InsertAutoFormat(size_t i, std::unique_ptr<SwTableAutoFormat> pFormat);
287  void EraseAutoFormat(size_t i);
288  void EraseAutoFormat(const OUString& rName);
289  std::unique_ptr<SwTableAutoFormat> ReleaseAutoFormat(size_t i);
291  std::unique_ptr<SwTableAutoFormat> ReleaseAutoFormat(const OUString& rName);
292 
294  SwTableAutoFormat* FindAutoFormat(const OUString& rName) const;
295 
296  void Load();
297  bool Save() const;
298 };
299 
301 {
302  const std::pair<OUString, std::unique_ptr<SwBoxAutoFormat>>& m_rCellStyleDesc;
303 public:
304  SwCellStyleDescriptor(const std::pair<OUString, std::unique_ptr<SwBoxAutoFormat>>& rCellStyleDesc) : m_rCellStyleDesc(rCellStyleDesc) { }
305 
306  const OUString& GetName() const { return m_rCellStyleDesc.first; }
307 };
308 
310 {
311  std::vector<std::pair<OUString, std::unique_ptr<SwBoxAutoFormat>>> m_aCellStyles;
312 public:
315 
316  size_t size() const;
317  SwCellStyleDescriptor operator[](size_t i) const;
318  void clear();
319 
321  void AddBoxFormat(const SwBoxAutoFormat& rBoxFormat, const OUString& sName);
322  void RemoveBoxFormat(const OUString& sName);
323  void ChangeBoxFormatName(const OUString& sFromName, const OUString& sToName);
325  OUString GetBoxFormatName(const SwBoxAutoFormat& rBoxFormat) const;
327  SwBoxAutoFormat* GetBoxFormat(const OUString& sName) const;
328 };
329 
330 #endif
331 
332 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void SetNumFormatString(const OUString &rNew)
Definition: tblafmt.hxx:90
static SwBoxAutoFormat * pDfltBoxAutoFormat
Definition: tblafmt.hxx:156
void SetName(const OUString &rNew)
Definition: tblafmt.hxx:207
void SetValueFormat(const OUString &rFormat, LanguageType eLng, LanguageType eSys)
Definition: tblafmt.hxx:87
const OUString & GetName() const
Definition: tblafmt.hxx:306
bool IsValueFormat() const
Definition: tblafmt.hxx:222
void SetValueFormat(const bool bNew)
Definition: tblafmt.hxx:233
void SetVerticalAlignment(const SwFormatVertOrient &rNew)
Definition: tblafmt.hxx:85
Pagedescriptor Client of SwPageDesc that is "described" by the attribute.
Definition: fmtpdsc.hxx:35
std::shared_ptr< SvxFormatKeepItem > m_aKeepWithNextPara
Definition: tblafmt.hxx:178
const SvxFormatBreakItem & GetBreak() const
Definition: tblafmt.hxx:194
SwCellStyleDescriptor(const std::pair< OUString, std::unique_ptr< SwBoxAutoFormat >> &rCellStyleDesc)
Definition: tblafmt.hxx:304
void RemoveBoxFormat(const OUString &sName)
Definition: tblafmt.cxx:1178
void SetFrame(const bool bNew)
Definition: tblafmt.hxx:231
std::vector< std::pair< OUString, std::unique_ptr< SwBoxAutoFormat > > > m_aCellStyles
Definition: tblafmt.hxx:311
css::uno::WeakReference< css::uno::XInterface > m_wXObject
Definition: tblafmt.hxx:158
void SetTextOrientation(const SvxFrameDirectionItem &rNew)
Definition: tblafmt.hxx:84
virtual SfxPoolItem * Clone(SfxItemPool *pPool=nullptr) const override
css::uno::WeakReference< css::uno::XInterface > const & GetXObject() const
Definition: tblafmt.hxx:250
std::unique_ptr< SwFormatVertOrient > m_aVerticalAlignment
Definition: tblafmt.hxx:52
std::shared_ptr< SvxFormatBreakItem > m_aBreak
Definition: tblafmt.hxx:176
sal_uInt16 m_aRepeatHeading
Definition: tblafmt.hxx:179
virtual SfxPoolItem * Clone(SfxItemPool *pPool=nullptr) const override
Definition: atrfrm.cxx:1275
const OUString & GetNumFormatString() const
Definition: tblafmt.hxx:79
bool IsFont() const
Definition: tblafmt.hxx:218
std::unique_ptr< SvxFrameDirectionItem > m_aTextOrientation
Definition: tblafmt.hxx:51
bool m_bCollapsingBorders
Definition: tblafmt.hxx:182
void SetUserDefined(bool bUserDefined)
Set if style is user defined.
Definition: tblafmt.hxx:239
void SetXObject(css::uno::Reference< css::uno::XInterface > const &xObject)
Definition: tblafmt.hxx:96
bool IsJustify() const
Definition: tblafmt.hxx:219
void SetFont(const bool bNew)
Definition: tblafmt.hxx:229
const SvxFormatKeepItem & GetKeepWithNextPara() const
Definition: tblafmt.hxx:195
void SetNumFormatLanguage(const LanguageType &rNew)
Definition: tblafmt.hxx:92
bool IsBackground() const
Definition: tblafmt.hxx:221
virtual SfxPoolItem * Clone(SfxItemPool *pPool=nullptr) const override
OUString m_aName
Definition: tblafmt.hxx:160
const LanguageType & GetNumFormatLanguage() const
Definition: tblafmt.hxx:81
void SetJustify(const bool bNew)
Definition: tblafmt.hxx:230
SwTableAutoFormatUpdateFlags
Definition: tblafmt.hxx:103
void ChangeBoxFormatName(const OUString &sFromName, const OUString &sToName)
Definition: tblafmt.cxx:1213
bool operator==(const SwBoxAutoFormat &rRight)
Comparing based of boxes backgrounds.
Definition: tblafmt.cxx:308
std::shared_ptr< SvxShadowItem > m_aShadow
Definition: tblafmt.hxx:183
const SvxFrameDirectionItem & GetTextOrientation() const
Definition: tblafmt.hxx:73
size_t size() const
Definition: tblafmt.cxx:1158
bool IsUserDefined() const
Check if style is defined by user.
Definition: tblafmt.hxx:227
void FinitCore()
Definition: init.cxx:672
void SetShadow(const SvxShadowItem &rNew)
Definition: tblafmt.hxx:200
LanguageType m_eNumFormatLanguage
Definition: tblafmt.hxx:57
bool Save(SvStream &rStream, sal_uInt16 fileVersion) const
Definition: tblafmt.cxx:346
bool Load(SvStream &rStream, const SwAfVersions &rVersions, sal_uInt16 nVer)
Definition: tblafmt.cxx:313
void SetXObject(css::uno::Reference< css::uno::XInterface > const &xObject)
Definition: tblafmt.hxx:252
SwFormatPageDesc m_aPageDesc
Definition: tblafmt.hxx:177
SwBoxAutoFormat & operator=(const SwBoxAutoFormat &rRef)
assignment-op (still used)
Definition: tblafmt.cxx:283
size
void SetWidthHeight(const bool bNew)
Definition: tblafmt.hxx:234
void SetBackground(const bool bNew)
Definition: tblafmt.hxx:232
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:269
SwTable is one table in the document model, containing rows (which contain cells).
Definition: swtable.hxx:110
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:237
const OUString & GetName() const
Definition: tblafmt.hxx:208
const SwFormatVertOrient & GetVerticalAlignment() const
Definition: tblafmt.hxx:74
bool IsHidden() const
Check if style is hidden.
Definition: tblafmt.hxx:225
unsigned char sal_uInt8
const o3tl::enumarray< SvxAdjust, unsigned short > aSvxToUnoAdjust USHRT_MAX
Definition: unosett.cxx:261
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:1168
OUString GetBoxFormatName(const SwBoxAutoFormat &rBoxFormat) const
If found returns its name. If not found returns an empty OUString.
Definition: tblafmt.cxx:1190
void AddBoxFormat(const SwBoxAutoFormat &rBoxFormat, const OUString &sName)
Add a copy of rBoxFormat.
Definition: tblafmt.cxx:1173
const SvxShadowItem & GetShadow() const
Definition: tblafmt.hxx:196
void SetBreak(const SvxFormatBreakItem &rNew)
Definition: tblafmt.hxx:198
const std::pair< OUString, std::unique_ptr< SwBoxAutoFormat > > & m_rCellStyleDesc
Definition: tblafmt.hxx:302
LanguageType m_eSysLanguage
Definition: tblafmt.hxx:56
virtual SfxPoolItem * Clone(SfxItemPool *pPool=nullptr) const override
SwBoxAutoFormat * GetBoxFormat(const OUString &sName) const
If found returns a ptr to a BoxFormat. If not found returns nullptr.
Definition: tblafmt.cxx:1202
void SetKeepWithNextPara(const SvxFormatKeepItem &rNew)
Definition: tblafmt.hxx:199
void SetSysLanguage(const LanguageType &rNew)
Definition: tblafmt.hxx:91
virtual SfxPoolItem * Clone(SfxItemPool *pPool=nullptr) const override
bool IsFrame() const
Definition: tblafmt.hxx:220
sal_uInt16 m_nStrResId
Definition: tblafmt.hxx:161