Debian Codegraph @

src/lib/WPSCell.h

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* libwps
3 * Version: MPL 2.0 / LGPLv2.1+
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 * Major Contributor(s):
10 * Copyright (C) 2009, 2011 Alonso Laurent ([email protected])
11 * Copyright (C) 2006, 2007 Andrew Ziem
12 * Copyright (C) 2004-2006 Fridrich Strba ([email protected])
13 * Copyright (C) 2004 Marc Maurer ([email protected])
14 * Copyright (C) 2003-2005 William Lachance ([email protected])
15 *
16 * For minor contributions see the git repository.
17 *
18 * Alternatively, the contents of this file may be used under the terms
19 * of the GNU Lesser General Public License Version 2.1 or later
20 * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
21 * applicable instead of those above.
22 *
23 * For further information visit http://libwps.sourceforge.net
24 */
25
26 /* Define some classes used to store a Cell
27 */
28
29 #ifndef WPS_CELL_H
30 # define WPS_CELL_H
31
32 #include <iostream>
33 #include <vector>
34
35 #include "libwps_internal.h"
36
37 #include "WPSFont.h"
38
39 /** a structure used to defined the cell format */
40 class WPSCellFormat
41 {
42 public:
43 /** the default horizontal alignement.
44
45 \note actually mainly used for table/spreadsheet cell, FULL is not yet implemented */
46 enum HorizontalAlignment { HALIGN_LEFT, HALIGN_RIGHT, HALIGN_CENTER,
47 HALIGN_FULL, HALIGN_DEFAULT
48 };
49 /** the default vertical alignement. */
50 enum VerticalAlignment { VALIGN_TOP, VALIGN_CENTER, VALIGN_BOTTOM, VALIGN_DEFAULT };
51 /** the wrapping */
52 enum Wrapping { WRAP_WRAP, WRAP_NO_WRAP, WRAP_DEFAULT };
53 /** the different types of cell's field */
54 enum FormatType { F_TEXT, F_BOOLEAN, F_NUMBER, F_DATE, F_TIME, F_UNKNOWN };
55
56 /* subformat:
57 NUMBER DATE TIME TEXT
58 0 : default default[3/2/2000] default[10:03:00] default
59 1 : decimal
60 2 : exponential
61 3 : percent
62 4 : money
63 5 : thousand
64 6 : fixed
65 7 : fraction
66 */
67
68 //! constructor
69 WPSCellFormat()
70 : m_font()
71 , m_hAlign(HALIGN_DEFAULT)
72 , m_vAlign(VALIGN_DEFAULT)
73 , m_wrapping(WRAP_DEFAULT)
74 , m_rotation(0)
75 , m_bordersList()
76 , m_format(F_UNKNOWN)
77 , m_subFormat(0)
78 , m_DTFormat("")
79 , m_digits(-1000)
80 , m_protected(false)
81 , m_backgroundColor(WPSColor::white()) { }
82 WPSCellFormat(WPSCellFormat const &)=default;
83 WPSCellFormat &operator=(WPSCellFormat const &)=default;
84 //! destructor
85 virtual ~WPSCellFormat();
86 //! returns true if this is a basic format style
87 bool hasBasicFormat() const
88 {
89 return m_format==F_TEXT || m_format==F_UNKNOWN;
90 }
91 //! returns a value type
92 std::string getValueType() const;
93 //! add to the propList
94 void addTo(librevenge::RVNGPropertyList &propList) const;
95 //! get the number style
96 bool getNumberingProperties(librevenge::RVNGPropertyList &propList) const;
97
98 //! returns the font
99 WPSFont const &getFont() const
100 {
101 return m_font;
102 }
103 //! sets the font
104 void setFont(WPSFont const &font)
105 {
106 m_font=font;
107 }
108 //! returns the horizontal alignement
109 HorizontalAlignment hAlignment() const
110 {
111 return m_hAlign;
112 }
113 //! sets the horizontal alignement
114 void setHAlignment(HorizontalAlignment align)
115 {
116 m_hAlign = align;
117 }
118
119 //! returns the vertical alignement
120 VerticalAlignment vAlignment() const
121 {
122 return m_vAlign;
123 }
124 //! sets the vertical alignement
125 void setVAlignment(VerticalAlignment align)
126 {
127 m_vAlign = align;
128 }
129 //! returns the wrapping
130 Wrapping wrapping() const
131 {
132 return m_wrapping;
133 }
134 //! sets the wrapping
135 void setWrapping(Wrapping align)
136 {
137 m_wrapping = align;
138 }
139
140 //! returns the text rotation angle
141 int getTextRotation() const
142 {
143 return m_rotation;
144 }
145 //! sets the text rotation angle
146 void setTextRotation(int rotation)
147 {
148 m_rotation = rotation;
149 }
150
151 //! returns the format type
152 FormatType getFormat() const
153 {
154 return m_format;
155 }
156 //! returns the subformat type
157 int getSubFormat() const
158 {
159 return m_subFormat;
160 }
161 //! returns the date/time format ( if set)
162 std::string getDTFormat() const
163 {
164 return m_DTFormat;
165 }
166 //! sets the format type
167 void setFormat(FormatType form, int subForm=0)
168 {
169 m_format = form;
170 m_subFormat = subForm;
171 }
172 //! sets the format type
173 void setDTFormat(FormatType form, std::string const &dtFormat="")
174 {
175 m_format = form;
176 m_subFormat = 0;
177 m_DTFormat = dtFormat;
178 }
179
180 //! returns the number of digits ( for a number)
181 int digits() const
182 {
183 return m_digits;
184 }
185 //! set the number of digits ( for a number)
186 void setDigits(int newDigit)
187 {
188 m_digits = newDigit;
189 }
190
191 //! returns true if the cell is protected
192 bool isProtected() const
193 {
194 return m_protected;
195 }
196
197 //! returns true if the cell is protected
198 void setProtected(bool fl)
199 {
200 m_protected = fl;
201 }
202
203 //! return true if the cell has some border
204 bool hasBorders() const
205 {
206 return m_bordersList.size() != 0;
207 }
208
209 //! return the cell border: libwps::LeftBit | ...
210 std::vector<WPSBorder> const &borders() const
211 {
212 return m_bordersList;
213 }
214
215 //! reset the border
216 void resetBorders()
217 {
218 m_bordersList.resize(0);
219 }
220
221 //! sets the cell border: wh=WPSBorder::LeftBit|...
222 void setBorders(int wh, WPSBorder const &border);
223 //! sets the cell borders
224 void setBorders(std::vector<WPSBorder> const &newBorders)
225 {
226 m_bordersList=newBorders;
227 }
228
229 //! returns the background color
230 WPSColor backgroundColor() const
231 {
232 return m_backgroundColor;
233 }
234 //! set the background color
235 void setBackgroundColor(WPSColor const &color)
236 {
237 m_backgroundColor = color;
238 }
239
240 //! a comparison function
241 int compare(WPSCellFormat const &cell, bool onlyNumbering=false) const;
242
243 //! operator<<
244 friend std::ostream &operator<<(std::ostream &o, WPSCellFormat const &cell);
245
246 //! a comparaison structure used to store data
247 struct CompareFormat
248 {
249 //! constructor
250 CompareFormat() {}
251 //! comparaison function
252 bool operator()(WPSCellFormat const &c1, WPSCellFormat const &c2) const
253 {
254 return c1.compare(c2, true) < 0;
255 }
256 };
257
258 protected:
259 //! convert a DTFormat in a propertyList
260 static bool convertDTFormat(std::string const &dtFormat, librevenge::RVNGPropertyListVector &propListVector);
261 //! the cell font ( used in spreadsheet code )
262 WPSFont m_font;
263 //! the cell alignement : by default nothing
264 HorizontalAlignment m_hAlign;
265 //! the cell vertical alignement : by default nothing
266 VerticalAlignment m_vAlign;
267 //! the wrapping : by default nothing
268 Wrapping m_wrapping;
269 //! the text rotation
270 int m_rotation;
271 //! the cell border WPSBorder::Pos
272 std::vector<WPSBorder> m_bordersList;
273 //! the cell format : by default unknown
274 FormatType m_format;
275 //! the sub format
276 int m_subFormat;
277 //! a date/time format ( using a subset of strftime format )
278 std::string m_DTFormat;
279 //! the number of digits
280 int m_digits;
281 //! cell protected
282 bool m_protected;
283 //! the backgroung color
284 WPSColor m_backgroundColor;
285 };
286
287 class WPSTable;
288
289 /** a structure used to defined the cell position, and a format */
290 class WPSCell : public WPSCellFormat
291 {
292 friend class WPSTable;
293 public:
294 //! constructor
295 WPSCell()
296 : WPSCellFormat()
297 , m_box()
298 , m_verticalSet(true)
299 , m_position(0,0)
300 , m_numberCellSpanned(1,1) {}
301 WPSCell(WPSCell const &)=default;
302 WPSCell &operator=(WPSCell const &)=default;
303 //! destructor
304 ~WPSCell() override;
305
306 //! add to the propList
307 void addTo(librevenge::RVNGPropertyList &propList) const;
308
309 //! set the bounding box (units in point)
310 void setBox(WPSBox2f const &b)
311 {
312 m_box = b;
313 }
314 //! return the bounding box
315 WPSBox2f const &box() const
316 {
317 return m_box;
318 }
319 //! returns true if the vertical is fixed
320 bool isVerticalSet() const
321 {
322 return m_verticalSet;
323 }
324 //! fixes or not the vertical size
325 void setVerticalSet(bool verticalSet)
326 {
327 m_verticalSet = verticalSet;
328 }
329 //! position accessor
330 Vec2i &position()
331 {
332 return m_position;
333 }
334 //! position accessor
335 Vec2i const &position() const
336 {
337 return m_position;
338 }
339 //! set the cell positions : 0,0 -> A1, 0,1 -> A2
340 void setPosition(Vec2i posi)
341 {
342 m_position = posi;
343 }
344
345 //! returns the number of spanned cells
346 Vec2i const &numSpannedCells() const
347 {
348 return m_numberCellSpanned;
349 }
350 //! sets the number of spanned cells : Vec2i(1,1) means 1 cellule
351 void setNumSpannedCells(Vec2i numSpanned)
352 {
353 m_numberCellSpanned=numSpanned;
354 }
355
356 //! call when a cell must be send
357 virtual bool send(WPSListenerPtr &listener) = 0;
358
359 //! call when the content of a cell must be send
360 virtual bool sendContent(WPSListenerPtr &listener) = 0;
361
362 //! operator<<
363 friend std::ostream &operator<<(std::ostream &o, WPSCell const &cell);
364
365 protected:
366 //! a comparaison structure used retrieve the rows and the columns
367 struct Compare
368 {
369 explicit Compare(int dim)
370 : m_coord(dim) {}
371 //! small structure to define a cell point
372 struct Point
373 {
374 Point(int wh, WPSCell const *cell)
375 : m_which(wh)
376 , m_cell(cell) {}
377 float getPos(int coord) const
378 {
379 if (m_which)
380 return m_cell->box().max()[coord];
381 return m_cell->box().min()[coord];
382 }
383 float getSize(int coord) const
384 {
385 return m_cell->box().size()[coord];
386 }
387 int m_which;
388 WPSCell const *m_cell;
389 };
390
391 //! comparaison function
392 bool operator()(Point const &c1, Point const &c2) const
393 {
394 float diffF = c1.getPos(m_coord)-c2.getPos(m_coord);
395 if (diffF < 0) return true;
396 if (diffF > 0) return false;
397 int diff = c2.m_which - c1.m_which;
398 if (diff) return (diff < 0);
399 diffF = c1.m_cell->box().size()[m_coord]
400 - c2.m_cell->box().size()[m_coord];
401 if (diffF < 0) return true;
402 if (diffF > 0) return false;
403 if (c1.m_cell->m_verticalSet != c2.m_cell->m_verticalSet) return c1.m_cell->m_verticalSet;
404 #ifdef _WIN64
405 return ((__int64)c1.m_cell < (__int64)c2.m_cell);
406 #else
407 return long(c1.m_cell) < long(c2.m_cell);
408 #endif
409 }
410
411 //! the coord to compare
412 int m_coord;
413 };
414
415 /** the cell bounding box (unit in point)*/
416 WPSBox2f m_box;
417 /** true if y size is fixed */
418 bool m_verticalSet;
419 //! the cell row and column : 0,0 -> A1, 0,1 -> A2
420 Vec2i m_position;
421 //! the cell spanned : by default (1,1)
422 Vec2i m_numberCellSpanned;
423 };
424
425 #endif
426 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */