Warning, file /office/calligra/libs/text/KoTextTableTemplate.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2012 Gopalakrishna Bhat A <gopalakbhat@gmail.com>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Library General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2 of the License, or (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 GNU
0012  * Library General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Library General Public License
0015  * along with this library; see the file COPYING.LIB.  If not, write to
0016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018  */
0019 
0020 #include "KoTextTableTemplate.h"
0021 
0022 #include "KoTextSharedLoadingData.h"
0023 
0024 #include <KoShapeLoadingContext.h>
0025 #include <KoTextSharedSavingData.h>
0026 #include <KoOdfWorkaround.h>
0027 #include <KoXmlNS.h>
0028 #include <KoTableCellStyle.h>
0029 #include <KoXmlWriter.h>
0030 #include <KoXmlReader.h>
0031 
0032 
0033 #include "Styles_p.h"
0034 
0035 #include "TextDebug.h"
0036 
0037 static const struct {
0038     KoTextTableTemplate::Property m_property;
0039     const char *m_element;
0040 } templateStyles[] = {
0041     { KoTextTableTemplate::BackGround,    "background"   },
0042     { KoTextTableTemplate::Body,          "body"         },
0043     { KoTextTableTemplate::EvenColumns,   "even-columns" },
0044     { KoTextTableTemplate::EvenRows,      "even-rows"    },
0045     { KoTextTableTemplate::FirstColumn,   "first-column" },
0046     { KoTextTableTemplate::FirstRow,      "first-row"    },
0047     { KoTextTableTemplate::LastColumn,    "last-column"  },
0048     { KoTextTableTemplate::LastRow,       "last-row"     },
0049     { KoTextTableTemplate::OddColumns,    "odd-columns"  },
0050     { KoTextTableTemplate::OddRows,       "odd-rows"     }
0051 };
0052 
0053 static const unsigned int numTemplateStyles = sizeof(templateStyles) / sizeof(*templateStyles);
0054 
0055 class Q_DECL_HIDDEN KoTextTableTemplate::Private
0056 {
0057 public:
0058     Private() { }
0059 
0060     ~Private() { }
0061 
0062     void setProperty(int key, const QVariant &value) {
0063         stylesPrivate.add(key, value);
0064     }
0065     int propertyInt(int key) const {
0066         QVariant variant = stylesPrivate.value(key);
0067         if (variant.isNull())
0068             return 0;
0069         return variant.toInt();
0070     }
0071 
0072     StylePrivate stylesPrivate;
0073     QString name;
0074 };
0075 
0076 
0077 KoTextTableTemplate::KoTextTableTemplate(QObject *parent)
0078     : QObject(parent),
0079       d(new Private())
0080 {
0081 
0082 }
0083 
0084 KoTextTableTemplate::~KoTextTableTemplate()
0085 {
0086     delete d;
0087 }
0088 
0089 QString KoTextTableTemplate::name() const
0090 {
0091     return d->name;
0092 }
0093 
0094 void KoTextTableTemplate::setName(const QString &name)
0095 {
0096     if (name == d->name)
0097         return;
0098     d->name = name;
0099 }
0100 
0101 void KoTextTableTemplate::setStyleId(int id)
0102 {
0103     d->stylesPrivate.add(KoTextTableTemplate::StyleId, id);
0104 }
0105 
0106 int KoTextTableTemplate::styleId() const
0107 {
0108     return d->propertyInt(StyleId);
0109 }
0110 
0111 int KoTextTableTemplate::background() const
0112 {
0113     return d->propertyInt(BackGround);
0114 }
0115 
0116 void KoTextTableTemplate::setBackground(int styleId)
0117 {
0118     d->stylesPrivate.add(BackGround, styleId);
0119 }
0120 
0121 int KoTextTableTemplate::body() const
0122 {
0123     return d->propertyInt(Body);
0124 }
0125 
0126 void KoTextTableTemplate::setBody(int styleId)
0127 {
0128     d->stylesPrivate.add(Body, styleId);
0129 }
0130 
0131 int KoTextTableTemplate::evenColumns() const
0132 {
0133     return d->propertyInt(EvenColumns);
0134 }
0135 
0136 void KoTextTableTemplate::setEvenColumns(int styleId)
0137 {
0138     d->stylesPrivate.add(EvenColumns, styleId);
0139 }
0140 
0141 int KoTextTableTemplate::evenRows() const
0142 {
0143     return d->propertyInt(EvenRows);
0144 }
0145 
0146 void KoTextTableTemplate::setEvenRows(int styleId)
0147 {
0148     d->stylesPrivate.add(EvenRows, styleId);
0149 }
0150 
0151 int KoTextTableTemplate::firstColumn() const
0152 {
0153     return d->propertyInt(FirstColumn);
0154 }
0155 
0156 void KoTextTableTemplate::setFirstColumn(int styleId)
0157 {
0158     d->stylesPrivate.add(FirstColumn, styleId);
0159 }
0160 
0161 int KoTextTableTemplate::firstRow() const
0162 {
0163     return d->propertyInt(FirstRow);
0164 }
0165 
0166 void KoTextTableTemplate::setFirstRow(int styleId)
0167 {
0168     d->stylesPrivate.add(FirstRow, styleId);
0169 }
0170 
0171 int KoTextTableTemplate::lastColumn() const
0172 {
0173     return d->propertyInt(LastColumn);
0174 }
0175 
0176 void KoTextTableTemplate::setLastColumn(int styleId)
0177 {
0178     d->stylesPrivate.add(LastColumn, styleId);
0179 }
0180 
0181 int KoTextTableTemplate::lastRow() const
0182 {
0183     return d->propertyInt(LastRow);
0184 }
0185 
0186 void KoTextTableTemplate::setLastRow(int styleId)
0187 {
0188     d->stylesPrivate.add(LastRow, styleId);
0189 }
0190 
0191 int KoTextTableTemplate::oddColumns() const
0192 {
0193     return d->propertyInt(OddColumns);
0194 }
0195 
0196 void KoTextTableTemplate::setOddColumns(int styleId)
0197 {
0198     d->stylesPrivate.add(OddColumns, styleId);
0199 }
0200 
0201 int KoTextTableTemplate::oddRows() const
0202 {
0203     return d->propertyInt(OddRows);
0204 }
0205 
0206 void KoTextTableTemplate::setOddRows(int styleId)
0207 {
0208     d->stylesPrivate.add(OddRows, styleId);
0209 }
0210 
0211 void KoTextTableTemplate::loadOdf(const KoXmlElement *element, KoShapeLoadingContext &context)
0212 {
0213     QString templateName = element->attributeNS(KoXmlNS::table, "name", QString());
0214 #ifndef NWORKAROUND_ODF_BUGS
0215     if (templateName.isEmpty()) {
0216         templateName = KoOdfWorkaround::fixTableTemplateName(*element);
0217     }
0218 #endif
0219     d->name = templateName;
0220 
0221     KoSharedLoadingData *sharedData = context.sharedData(KOTEXT_SHARED_LOADING_ID);
0222     KoTextSharedLoadingData *textSharedData = 0;
0223     if (sharedData) {
0224         textSharedData = dynamic_cast<KoTextSharedLoadingData *>(sharedData);
0225     }
0226 
0227     if (textSharedData) {
0228         KoXmlElement styleElem;
0229         forEachElement(styleElem, (*element)) {
0230             if (styleElem.namespaceURI() == KoXmlNS::table) {
0231                 for (uint index = 0; index < numTemplateStyles; ++index) {
0232                     if (templateStyles[index].m_element == styleElem.localName()) {
0233                         QString styleName = styleElem.attributeNS(KoXmlNS::table, "style-name", QString());
0234 #ifndef NWORKAROUND_ODF_BUGS
0235                         if (styleName.isEmpty()) {
0236                             styleName = KoOdfWorkaround::fixTableTemplateCellStyleName(styleElem);
0237                         }
0238 #endif
0239                         KoTableCellStyle *cs = 0;
0240                         if (!styleName.isEmpty()) {
0241                             cs = textSharedData->tableCellStyle(styleName, true);
0242                             if (!cs) {
0243                                 warnText << "Missing KoTableCellStyle!";
0244                             }
0245                             else {
0246                                 //                debugText << "==> cs.name:" << cs->name();
0247                                 //                debugText << "==> cs.styleId:" << cs->styleId();
0248                                 d->stylesPrivate.add(templateStyles[index].m_property, cs->styleId());
0249                             }
0250                         }
0251                     }
0252                 }
0253             }
0254         }
0255     }
0256 }
0257 
0258 void KoTextTableTemplate::saveOdf(KoXmlWriter *writer, KoTextSharedSavingData *savingData) const
0259 {
0260     writer->startElement("table:table-template");
0261 
0262     QString styleName(QString(QUrl::toPercentEncoding(name(), "", " ")).replace('%', '_'));
0263     if (styleName.isEmpty())
0264         styleName = "TT";
0265 
0266     QString generatedName = styleName;
0267     int num = 1;
0268     while (savingData->styleNames().contains(generatedName)) {
0269         generatedName = styleName + QString::number(num++);
0270     }
0271 
0272     savingData->setStyleName(styleId(), generatedName);
0273     d->name = generatedName;
0274 
0275     writer->addAttribute("table:name", name());
0276 
0277     for (uint index = 0; index < numTemplateStyles; ++index) {
0278         if (d->stylesPrivate.contains(templateStyles[index].m_property)) {
0279             writer->startElement(QString("table:%1").arg(templateStyles[index].m_element).toLatin1());
0280             QString savedStyleName = savingData->styleName(d->stylesPrivate.value(templateStyles[index].m_property).toInt());
0281             if (! savedStyleName.isEmpty()) {
0282                 writer->addAttribute("table:style-name", savedStyleName);
0283             }
0284 
0285             writer->endElement();
0286         }
0287     }
0288 
0289     writer->endElement(); //table:table-template
0290 }