File indexing completed on 2024-12-08 12:56:12

0001 /*
0002  *  Copyright (c) 2010 Carlos Licea <carlos@kdab.com>
0003  *
0004  *  This library is free software; you can redistribute it and/or modify
0005  *  it under the terms of the GNU Lesser General Public License as published
0006  *  by the Free Software Foundation; either version 2.1 of the License, or
0007  *  (at your option) any later version.
0008  *
0009  *  This library is distributed in the hope that it will be useful,
0010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  *  GNU Lesser General Public License for more details.
0013  *
0014  *  You should have received a copy of the GNU Lesser General Public License
0015  *  along with this program; if not, write to the Free Software
0016  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
0017  */
0018 
0019 #include "KoTblStyle.h"
0020 
0021 KOSTYLE_DECLARE_SHARED_POINTER_IMPL(KoTblStyle)
0022 
0023 namespace {
0024     class BreakStyleMap : public QMap<KoTblStyle::BreakType, QString>
0025     {
0026     public:
0027         BreakStyleMap()
0028         {
0029             insert(KoTblStyle::NoBreak, QString());
0030             insert(KoTblStyle::AutoBreak, "auto");
0031             insert(KoTblStyle::ColumnBreak, "column");
0032             insert(KoTblStyle::PageBreak, "page");
0033         }
0034     } breakStyleMap;
0035 
0036     class HorizontalAlignMap : public QMap<KoTblStyle::HorizontalAlign, QString>
0037     {
0038     public:
0039         HorizontalAlignMap()
0040         {
0041             insert(KoTblStyle::CenterAlign, "center");
0042             insert(KoTblStyle::LeftAlign, "left");
0043             insert(KoTblStyle::MarginsAlign, "margins");
0044             insert(KoTblStyle::RightAlign, "right");
0045         }
0046     } horizontalAlignMap;
0047 
0048     class BorderModelMap : public QMap<KoTblStyle::BorderModel, QString>
0049     {
0050     public:
0051         BorderModelMap()
0052         {
0053             insert(KoTblStyle::CollapsingModel, "collapsing");
0054             insert(KoTblStyle::SeparatingModel, "separating");
0055         }
0056     } borderModelMap;
0057 
0058     class KeepWithNextMap : public QMap<KoTblStyle::KeepWithNext, QString>
0059     {
0060     public:
0061         KeepWithNextMap()
0062         {
0063             insert(KoTblStyle::AutoKeepWithNext, "auto");
0064             insert(KoTblStyle::AlwaysKeepWithNext, "always");
0065         }
0066     } keepWithNextMap;
0067 
0068     class WritingModeMap : public QMap<KoTblStyle::WritingMode, QString>
0069     {
0070     public:
0071         WritingModeMap()
0072         {
0073             insert(KoTblStyle::LrTbWritingMode, "lr-tb");
0074             insert(KoTblStyle::RlTbWritingMode, "rl-tb");
0075             insert(KoTblStyle::TbRlWritingMode, "tb-rl");
0076             insert(KoTblStyle::TbLrWritingMode, "tb-lr");
0077             insert(KoTblStyle::LrWritingMode, "lr");
0078             insert(KoTblStyle::RlWritingMode, "rl");
0079             insert(KoTblStyle::TbWritingMode, "tb");
0080             insert(KoTblStyle::PageWritingMode, "page");
0081         }
0082     } writingModeMap;
0083 
0084     QString prefix = "table";
0085     const char familyName[] = "table";
0086 }
0087 
0088 KoTblStyle::KoTblStyle()
0089   : KoStyle()
0090   , m_backgroundColor()
0091   , m_breakAfter(NoBreak)
0092   , m_breakBefore(NoBreak)
0093   , m_allowBreakBetweenRows(false)
0094   , m_leftMargin(0.0)
0095   , m_topMargin(0.0)
0096   , m_rightMargin(0.0)
0097   , m_bottomMargin(0.0)
0098   , m_width(0.0)
0099   , m_widthUnit(PointsUnit)
0100   , m_horizontalAlign(LeftAlign)
0101   , m_borderModel(CollapsingModel)
0102   , m_keepWithNext(AutoKeepWithNext)
0103   , m_writingMode(PageWritingMode)
0104   , m_display(true)
0105 {
0106 }
0107 
0108 KoTblStyle::~KoTblStyle()
0109 {
0110 }
0111 
0112 void KoTblStyle::setAllowBreakBetweenRows(bool allow)
0113 {
0114     m_allowBreakBetweenRows = allow;
0115 }
0116 
0117 bool KoTblStyle::allowBreakBetweenRows() const
0118 {
0119     return m_allowBreakBetweenRows;
0120 }
0121 
0122 void KoTblStyle::setMasterPageName(const QString& name)
0123 {
0124     m_masterPageName = name;
0125 }
0126 
0127 void KoTblStyle::setMasterPageName(const char* name)
0128 {
0129     m_masterPageName = QString::fromUtf8(name);
0130 }
0131 
0132 QString KoTblStyle::masterPageName() const
0133 {
0134     return m_masterPageName;
0135 }
0136 
0137 void KoTblStyle::setBackgroundColor(const QColor& color)
0138 {
0139     m_backgroundColor = color;
0140 }
0141 
0142 QColor KoTblStyle::backgroundColor() const
0143 {
0144     return m_backgroundColor;
0145 }
0146 
0147 void KoTblStyle::setWidth(qreal width, KoTblStyle::WidthUnit unit)
0148 {
0149     m_width = width;
0150     m_widthUnit = unit;
0151 }
0152 
0153 qreal KoTblStyle::width() const
0154 {
0155     return m_width;
0156 }
0157 
0158 KoTblStyle::WidthUnit KoTblStyle::widthUnit() const
0159 {
0160     return m_widthUnit;
0161 }
0162 
0163 void KoTblStyle::setLeftMargin(qreal left)
0164 {
0165     m_leftMargin = left;
0166 }
0167 
0168 qreal KoTblStyle::leftMargin() const
0169 {
0170     return m_leftMargin;
0171 }
0172 
0173 void KoTblStyle::setTopMargin(qreal top)
0174 {
0175     m_topMargin = top;
0176 }
0177 
0178 qreal KoTblStyle::topMargin() const
0179 {
0180     return m_topMargin;
0181 }
0182 
0183 void KoTblStyle::setRightMargin(qreal right)
0184 {
0185     m_rightMargin = right;
0186 }
0187 
0188 qreal KoTblStyle::rightMargin() const
0189 {
0190     return m_rightMargin;
0191 }
0192 
0193 void KoTblStyle::setBottomMargin(qreal bottom)
0194 {
0195     m_bottomMargin = bottom;
0196 }
0197 
0198 qreal KoTblStyle::bottomMargin() const
0199 {
0200     return m_bottomMargin;
0201 }
0202 
0203 void KoTblStyle::setHorizontalAlign(KoTblStyle::HorizontalAlign align)
0204 {
0205     m_horizontalAlign = align;
0206 }
0207 
0208 KoTblStyle::HorizontalAlign KoTblStyle::horizontalAlign() const
0209 {
0210     return m_horizontalAlign;
0211 }
0212 
0213 void KoTblStyle::setDisplay(bool display)
0214 {
0215     m_display = display;
0216 }
0217 
0218 bool KoTblStyle::display() const
0219 {
0220     return m_display;
0221 }
0222 
0223 void KoTblStyle::setBreakBefore(KoTblStyle::BreakType breakBefore)
0224 {
0225     m_breakBefore = breakBefore;
0226 }
0227 
0228 KoTblStyle::BreakType KoTblStyle::breakBefore() const
0229 {
0230     return m_breakBefore;
0231 }
0232 
0233 void KoTblStyle::setBreakAfter(KoTblStyle::BreakType breakAfter)
0234 {
0235     m_breakAfter = breakAfter;
0236 }
0237 
0238 KoTblStyle::BreakType KoTblStyle::breakAfter() const
0239 {
0240     return m_breakAfter;
0241 }
0242 
0243 void KoTblStyle::setBorderModel(KoTblStyle::BorderModel bordelModel)
0244 {
0245     m_borderModel = bordelModel;
0246 }
0247 
0248 KoTblStyle::BorderModel KoTblStyle::borderModel() const
0249 {
0250     return m_borderModel;
0251 }
0252 
0253 void KoTblStyle::setKeepWithNext(KoTblStyle::KeepWithNext keepWithNext)
0254 {
0255     m_keepWithNext = keepWithNext;
0256 }
0257 
0258 KoTblStyle::KeepWithNext KoTblStyle::keepWithNext() const
0259 {
0260     return m_keepWithNext;
0261 }
0262 
0263 void KoTblStyle::setWritingMode(KoTblStyle::WritingMode writingMode)
0264 {
0265     m_writingMode = writingMode;
0266 }
0267 
0268 KoTblStyle::WritingMode KoTblStyle::writingMode() const
0269 {
0270     return m_writingMode;
0271 }
0272 
0273 KoGenStyle::Type KoTblStyle::automaticstyleType() const
0274 {
0275     return KoGenStyle::TableAutoStyle;
0276 }
0277 
0278 KoGenStyle::Type KoTblStyle::styleType() const
0279 {
0280     return KoGenStyle::TableStyle;
0281 }
0282 
0283 const char* KoTblStyle::styleFamilyName() const
0284 {
0285     return familyName;
0286 }
0287 
0288 QString KoTblStyle::defaultPrefix() const
0289 {
0290     return prefix;
0291 }
0292 
0293 void KoTblStyle::prepareStyle(KoGenStyle& style) const
0294 {
0295     if(m_backgroundColor.isValid()) {
0296         style.addProperty("fo:background-color", m_backgroundColor.name());
0297     }
0298     if (m_breakAfter != KoTblStyle::NoBreak) {
0299         style.addProperty("fo:break-after", breakStyleMap.value(m_breakAfter));
0300     }
0301     if (m_breakBefore != KoTblStyle::NoBreak) {
0302         style.addProperty("fo:break-before", breakStyleMap.value(m_breakBefore));
0303     }
0304     style.addProperty("fo:keep-with-next", keepWithNextMap.value(m_keepWithNext));
0305 
0306     style.addPropertyPt("fo:margin-top", m_topMargin);
0307     style.addPropertyPt("fo:margin-right", m_rightMargin);
0308     style.addPropertyPt("fo:margin-bottom", m_bottomMargin);
0309     style.addPropertyPt("fo:margin-left", m_leftMargin);
0310 
0311     // style:width may not be 0, use style:rel-width if width is 0
0312     if (m_widthUnit == PercentageUnit || m_width <= 0) {
0313         style.addProperty("style:rel-width", QString::number(m_width) + QLatin1Char('%'));
0314     } else {
0315         style.addPropertyPt("style:width", m_width);
0316     }
0317 
0318     style.addProperty("style:may-break-between-rows", m_allowBreakBetweenRows ? "true" : "false");
0319     style.addProperty("style:writing-mode", writingModeMap.value(m_writingMode));
0320     style.addProperty("table:align", horizontalAlignMap.value(m_horizontalAlign));
0321     style.addProperty("table:border-model", borderModelMap.value(m_borderModel));
0322 
0323     if(!m_display) {
0324         style.addProperty("table:display", "false");
0325     }
0326 
0327     if(!m_masterPageName.isEmpty()) {
0328         style.addAttribute("style:master-page-name", m_masterPageName);
0329     }
0330 }