1 /*
2  *  Copyright (c) 2010 Carlos Licea <carlos@kdab.com>
3  *
4  *  This library is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU Lesser General Public License as published
6  *  by the Free Software Foundation; either version 2.1 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This library is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU Lesser General Public License for more details.
13  *
14  *  You should have received a copy of the GNU Lesser General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 #include "KoTblStyle.h"
20 
21 KOSTYLE_DECLARE_SHARED_POINTER_IMPL(KoTblStyle)
22 
23 namespace {
24     class BreakStyleMap : public QMap<KoTblStyle::BreakType, QString>
25     {
26     public:
BreakStyleMap()27         BreakStyleMap()
28         {
29             insert(KoTblStyle::NoBreak, QString());
30             insert(KoTblStyle::AutoBreak, "auto");
31             insert(KoTblStyle::ColumnBreak, "column");
32             insert(KoTblStyle::PageBreak, "page");
33         }
34     } breakStyleMap;
35 
36     class HorizontalAlignMap : public QMap<KoTblStyle::HorizontalAlign, QString>
37     {
38     public:
HorizontalAlignMap()39         HorizontalAlignMap()
40         {
41             insert(KoTblStyle::CenterAlign, "center");
42             insert(KoTblStyle::LeftAlign, "left");
43             insert(KoTblStyle::MarginsAlign, "margins");
44             insert(KoTblStyle::RightAlign, "right");
45         }
46     } horizontalAlignMap;
47 
48     class BorderModelMap : public QMap<KoTblStyle::BorderModel, QString>
49     {
50     public:
BorderModelMap()51         BorderModelMap()
52         {
53             insert(KoTblStyle::CollapsingModel, "collapsing");
54             insert(KoTblStyle::SeparatingModel, "separating");
55         }
56     } borderModelMap;
57 
58     class KeepWithNextMap : public QMap<KoTblStyle::KeepWithNext, QString>
59     {
60     public:
KeepWithNextMap()61         KeepWithNextMap()
62         {
63             insert(KoTblStyle::AutoKeepWithNext, "auto");
64             insert(KoTblStyle::AlwaysKeepWithNext, "always");
65         }
66     } keepWithNextMap;
67 
68     class WritingModeMap : public QMap<KoTblStyle::WritingMode, QString>
69     {
70     public:
WritingModeMap()71         WritingModeMap()
72         {
73             insert(KoTblStyle::LrTbWritingMode, "lr-tb");
74             insert(KoTblStyle::RlTbWritingMode, "rl-tb");
75             insert(KoTblStyle::TbRlWritingMode, "tb-rl");
76             insert(KoTblStyle::TbLrWritingMode, "tb-lr");
77             insert(KoTblStyle::LrWritingMode, "lr");
78             insert(KoTblStyle::RlWritingMode, "rl");
79             insert(KoTblStyle::TbWritingMode, "tb");
80             insert(KoTblStyle::PageWritingMode, "page");
81         }
82     } writingModeMap;
83 
84     QString prefix = "table";
85     const char familyName[] = "table";
86 }
87 
KoTblStyle()88 KoTblStyle::KoTblStyle()
89   : KoStyle()
90   , m_backgroundColor()
91   , m_breakAfter(NoBreak)
92   , m_breakBefore(NoBreak)
93   , m_allowBreakBetweenRows(false)
94   , m_leftMargin(0.0)
95   , m_topMargin(0.0)
96   , m_rightMargin(0.0)
97   , m_bottomMargin(0.0)
98   , m_width(0.0)
99   , m_widthUnit(PointsUnit)
100   , m_horizontalAlign(LeftAlign)
101   , m_borderModel(CollapsingModel)
102   , m_keepWithNext(AutoKeepWithNext)
103   , m_writingMode(PageWritingMode)
104   , m_display(true)
105 {
106 }
107 
~KoTblStyle()108 KoTblStyle::~KoTblStyle()
109 {
110 }
111 
setAllowBreakBetweenRows(bool allow)112 void KoTblStyle::setAllowBreakBetweenRows(bool allow)
113 {
114     m_allowBreakBetweenRows = allow;
115 }
116 
allowBreakBetweenRows() const117 bool KoTblStyle::allowBreakBetweenRows() const
118 {
119     return m_allowBreakBetweenRows;
120 }
121 
setMasterPageName(const QString & name)122 void KoTblStyle::setMasterPageName(const QString& name)
123 {
124     m_masterPageName = name;
125 }
126 
setMasterPageName(const char * name)127 void KoTblStyle::setMasterPageName(const char* name)
128 {
129     m_masterPageName = QString::fromUtf8(name);
130 }
131 
masterPageName() const132 QString KoTblStyle::masterPageName() const
133 {
134     return m_masterPageName;
135 }
136 
setBackgroundColor(const QColor & color)137 void KoTblStyle::setBackgroundColor(const QColor& color)
138 {
139     m_backgroundColor = color;
140 }
141 
backgroundColor() const142 QColor KoTblStyle::backgroundColor() const
143 {
144     return m_backgroundColor;
145 }
146 
setWidth(qreal width,KoTblStyle::WidthUnit unit)147 void KoTblStyle::setWidth(qreal width, KoTblStyle::WidthUnit unit)
148 {
149     m_width = width;
150     m_widthUnit = unit;
151 }
152 
width() const153 qreal KoTblStyle::width() const
154 {
155     return m_width;
156 }
157 
widthUnit() const158 KoTblStyle::WidthUnit KoTblStyle::widthUnit() const
159 {
160     return m_widthUnit;
161 }
162 
setLeftMargin(qreal left)163 void KoTblStyle::setLeftMargin(qreal left)
164 {
165     m_leftMargin = left;
166 }
167 
leftMargin() const168 qreal KoTblStyle::leftMargin() const
169 {
170     return m_leftMargin;
171 }
172 
setTopMargin(qreal top)173 void KoTblStyle::setTopMargin(qreal top)
174 {
175     m_topMargin = top;
176 }
177 
topMargin() const178 qreal KoTblStyle::topMargin() const
179 {
180     return m_topMargin;
181 }
182 
setRightMargin(qreal right)183 void KoTblStyle::setRightMargin(qreal right)
184 {
185     m_rightMargin = right;
186 }
187 
rightMargin() const188 qreal KoTblStyle::rightMargin() const
189 {
190     return m_rightMargin;
191 }
192 
setBottomMargin(qreal bottom)193 void KoTblStyle::setBottomMargin(qreal bottom)
194 {
195     m_bottomMargin = bottom;
196 }
197 
bottomMargin() const198 qreal KoTblStyle::bottomMargin() const
199 {
200     return m_bottomMargin;
201 }
202 
setHorizontalAlign(KoTblStyle::HorizontalAlign align)203 void KoTblStyle::setHorizontalAlign(KoTblStyle::HorizontalAlign align)
204 {
205     m_horizontalAlign = align;
206 }
207 
horizontalAlign() const208 KoTblStyle::HorizontalAlign KoTblStyle::horizontalAlign() const
209 {
210     return m_horizontalAlign;
211 }
212 
setDisplay(bool display)213 void KoTblStyle::setDisplay(bool display)
214 {
215     m_display = display;
216 }
217 
display() const218 bool KoTblStyle::display() const
219 {
220     return m_display;
221 }
222 
setBreakBefore(KoTblStyle::BreakType breakBefore)223 void KoTblStyle::setBreakBefore(KoTblStyle::BreakType breakBefore)
224 {
225     m_breakBefore = breakBefore;
226 }
227 
breakBefore() const228 KoTblStyle::BreakType KoTblStyle::breakBefore() const
229 {
230     return m_breakBefore;
231 }
232 
setBreakAfter(KoTblStyle::BreakType breakAfter)233 void KoTblStyle::setBreakAfter(KoTblStyle::BreakType breakAfter)
234 {
235     m_breakAfter = breakAfter;
236 }
237 
breakAfter() const238 KoTblStyle::BreakType KoTblStyle::breakAfter() const
239 {
240     return m_breakAfter;
241 }
242 
setBorderModel(KoTblStyle::BorderModel bordelModel)243 void KoTblStyle::setBorderModel(KoTblStyle::BorderModel bordelModel)
244 {
245     m_borderModel = bordelModel;
246 }
247 
borderModel() const248 KoTblStyle::BorderModel KoTblStyle::borderModel() const
249 {
250     return m_borderModel;
251 }
252 
setKeepWithNext(KoTblStyle::KeepWithNext keepWithNext)253 void KoTblStyle::setKeepWithNext(KoTblStyle::KeepWithNext keepWithNext)
254 {
255     m_keepWithNext = keepWithNext;
256 }
257 
keepWithNext() const258 KoTblStyle::KeepWithNext KoTblStyle::keepWithNext() const
259 {
260     return m_keepWithNext;
261 }
262 
setWritingMode(KoTblStyle::WritingMode writingMode)263 void KoTblStyle::setWritingMode(KoTblStyle::WritingMode writingMode)
264 {
265     m_writingMode = writingMode;
266 }
267 
writingMode() const268 KoTblStyle::WritingMode KoTblStyle::writingMode() const
269 {
270     return m_writingMode;
271 }
272 
automaticstyleType() const273 KoGenStyle::Type KoTblStyle::automaticstyleType() const
274 {
275     return KoGenStyle::TableAutoStyle;
276 }
277 
styleType() const278 KoGenStyle::Type KoTblStyle::styleType() const
279 {
280     return KoGenStyle::TableStyle;
281 }
282 
styleFamilyName() const283 const char* KoTblStyle::styleFamilyName() const
284 {
285     return familyName;
286 }
287 
defaultPrefix() const288 QString KoTblStyle::defaultPrefix() const
289 {
290     return prefix;
291 }
292 
prepareStyle(KoGenStyle & style) const293 void KoTblStyle::prepareStyle(KoGenStyle& style) const
294 {
295     if(m_backgroundColor.isValid()) {
296         style.addProperty("fo:background-color", m_backgroundColor.name());
297     }
298     if (m_breakAfter != KoTblStyle::NoBreak) {
299         style.addProperty("fo:break-after", breakStyleMap.value(m_breakAfter));
300     }
301     if (m_breakBefore != KoTblStyle::NoBreak) {
302         style.addProperty("fo:break-before", breakStyleMap.value(m_breakBefore));
303     }
304     style.addProperty("fo:keep-with-next", keepWithNextMap.value(m_keepWithNext));
305 
306     style.addPropertyPt("fo:margin-top", m_topMargin);
307     style.addPropertyPt("fo:margin-right", m_rightMargin);
308     style.addPropertyPt("fo:margin-bottom", m_bottomMargin);
309     style.addPropertyPt("fo:margin-left", m_leftMargin);
310 
311     // style:width may not be 0, use style:rel-width if width is 0
312     if (m_widthUnit == PercentageUnit || m_width <= 0) {
313         style.addProperty("style:rel-width", QString::number(m_width) + QLatin1Char('%'));
314     } else {
315         style.addPropertyPt("style:width", m_width);
316     }
317 
318     style.addProperty("style:may-break-between-rows", m_allowBreakBetweenRows ? "true" : "false");
319     style.addProperty("style:writing-mode", writingModeMap.value(m_writingMode));
320     style.addProperty("table:align", horizontalAlignMap.value(m_horizontalAlign));
321     style.addProperty("table:border-model", borderModelMap.value(m_borderModel));
322 
323     if(!m_display) {
324         style.addProperty("table:display", "false");
325     }
326 
327     if(!m_masterPageName.isEmpty()) {
328         style.addAttribute("style:master-page-name", m_masterPageName);
329     }
330 }
331