1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
|
/* This file is part of the KDE project
Copyright 1998-2016 The Calligra Team <calligra-devel@kde.org>
Copyright 2016 Tomas Mecir <mecirt@gmail.com>
Copyright 2010 Marijn Kruisselbrink <mkruisselbrink@kde.org>
Copyright 2007 Stefan Nikolaus <stefan.nikolaus@kdemail.net>
Copyright 2007 Thorsten Zachmann <zachmann@kde.org>
Copyright 2005-2006 Inge Wallin <inge@lysator.liu.se>
Copyright 2004 Ariya Hidayat <ariya@kde.org>
Copyright 2002-2003 Norbert Andres <nandres@web.de>
Copyright 2000-2002 Laurent Montel <montel@kde.org>
Copyright 2002 John Dailey <dailey@vt.edu>
Copyright 2002 Phillip Mueller <philipp.mueller@gmx.de>
Copyright 2000 Werner Trobin <trobin@kde.org>
Copyright 1999-2000 Simon Hausmann <hausmann@kde.org>
Copyright 1999 David Faure <faure@kde.org>
Copyright 1998-2000 Torben Weis <weis@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "SheetsOdf.h"
#include "SheetsOdfPrivate.h"
#include "CalculationSettings.h"
#include "DocBase.h"
#include "LoadingInfo.h"
#include "Map.h"
#include "NamedAreaManager.h"
#include "RowColumnFormat.h"
#include "Sheet.h"
#include "StyleManager.h"
#include "Validity.h"
#include "database/DatabaseManager.h"
#include <KoCharacterStyle.h>
#include <KoDocumentResourceManager.h>
#include <KoGenStyles.h>
#include <KoStyleManager.h>
#include <KoStyleStack.h>
#include <KoText.h>
#include <KoTextSharedLoadingData.h>
#include <KoUnit.h>
#include <KoXmlNS.h>
#include <KoXmlWriter.h>
#include <kcodecs.h>
// This file contains functionality to load/save a Map
namespace Calligra {
namespace Sheets {
namespace Odf {
void fixupStyle(KoCharacterStyle* style);
}
void Odf::fixupStyle(KoCharacterStyle* style)
{
style->removeHardCodedDefaults();
QTextCharFormat format;
style->applyStyle(format);
switch (style->underlineStyle()) {
case KoCharacterStyle::NoLineStyle:
format.setUnderlineStyle(QTextCharFormat::NoUnderline); break;
case KoCharacterStyle::SolidLine:
format.setUnderlineStyle(QTextCharFormat::SingleUnderline); break;
case KoCharacterStyle::DottedLine:
format.setUnderlineStyle(QTextCharFormat::DotLine); break;
case KoCharacterStyle::DashLine:
format.setUnderlineStyle(QTextCharFormat::DashUnderline); break;
case KoCharacterStyle::DotDashLine:
format.setUnderlineStyle(QTextCharFormat::DashDotLine); break;
case KoCharacterStyle::DotDotDashLine:
format.setUnderlineStyle(QTextCharFormat::DashDotDotLine); break;
case KoCharacterStyle::LongDashLine:
format.setUnderlineStyle(QTextCharFormat::DashUnderline); break;
case KoCharacterStyle::WaveLine:
format.setUnderlineStyle(QTextCharFormat::WaveUnderline); break;
}
style->copyProperties(format);
}
bool Odf::loadMap(Map *map, const KoXmlElement& body, KoOdfLoadingContext& odfContext)
{
map->setLoading(true);
map->loadingInfo()->setFileFormat(LoadingInfo::OpenDocument);
//load in first
loadStyleTemplate(map->styleManager(), odfContext.stylesReader(), map);
OdfLoadingContext tableContext(odfContext);
tableContext.validities = Validity::preloadValidities(body); // table:content-validations
// load text styles for rich-text content and TOS
KoShapeLoadingContext shapeContext(tableContext.odfContext, map->resourceManager());
tableContext.shapeContext = &shapeContext;
KoTextSharedLoadingData * sharedData = new KoTextSharedLoadingData();
sharedData->loadOdfStyles(shapeContext, map->textStyleManager());
fixupStyle((KoCharacterStyle*)map->textStyleManager()->defaultParagraphStyle());
foreach (KoCharacterStyle* style, sharedData->characterStyles(true)) {
fixupStyle(style);
}
foreach (KoCharacterStyle* style, sharedData->characterStyles(false)) {
fixupStyle(style);
}
shapeContext.addSharedData(KOTEXT_SHARED_LOADING_ID, sharedData);
QVariant variant;
variant.setValue(map->textStyleManager());
map->resourceManager()->setResource(KoText::StyleManager, variant);
// load default column style
const KoXmlElement* defaultColumnStyle = odfContext.stylesReader().defaultStyle("table-column");
if (defaultColumnStyle) {
// debugSheets <<"style:default-style style:family=\"table-column\"";
KoStyleStack styleStack;
styleStack.push(*defaultColumnStyle);
styleStack.setTypeProperties("table-column");
if (styleStack.hasProperty(KoXmlNS::style, "column-width")) {
const double width = KoUnit::parseValue(styleStack.property(KoXmlNS::style, "column-width"), -1.0);
if (width != -1.0) {
// debugSheets <<"\tstyle:column-width:" << width;
map->setDefaultColumnWidth(width);
}
}
}
// load default row style
const KoXmlElement* defaultRowStyle = odfContext.stylesReader().defaultStyle("table-row");
if (defaultRowStyle) {
// debugSheets <<"style:default-style style:family=\"table-row\"";
KoStyleStack styleStack;
styleStack.push(*defaultRowStyle);
styleStack.setTypeProperties("table-row");
if (styleStack.hasProperty(KoXmlNS::style, "row-height")) {
const double height = KoUnit::parseValue(styleStack.property(KoXmlNS::style, "row-height"), -1.0);
if (height != -1.0) {
// debugSheets <<"\tstyle:row-height:" << height;
map->setDefaultRowHeight(height);
}
}
}
loadCalculationSettings(map->calculationSettings(), body); // table::calculation-settings
if (body.hasAttributeNS(KoXmlNS::table, "structure-protected")) {
loadProtection(map, body);
}
KoXmlNode sheetNode = KoXml::namedItemNS(body, KoXmlNS::table, "table");
if (sheetNode.isNull()) {
// We need at least one sheet !
map->doc()->setErrorMessage(i18n("This document has no sheets (tables)."));
map->setLoading(false);
return false;
}
int overallRowCount = 0;
while (!sheetNode.isNull()) {
KoXmlElement sheetElement = sheetNode.toElement();
if (!sheetElement.isNull()) {
//debugSheets<<" Odf::loadMap tableElement is not null";
//debugSheets<<"tableElement.nodeName() :"<<sheetElement.nodeName();
// make it slightly faster
KoXml::load(sheetElement);
if (sheetElement.nodeName() == "table:table") {
if (!sheetElement.attributeNS(KoXmlNS::table, "name", QString()).isEmpty()) {
const QString sheetName = sheetElement.attributeNS(KoXmlNS::table, "name", QString());
Sheet* sheet = map->addNewSheet(sheetName);
sheet->setSheetName(sheetName, true);
overallRowCount += KoXml::childNodesCount(sheetElement);
}
}
}
// reduce memory usage
KoXml::unload(sheetElement);
sheetNode = sheetNode.nextSibling();
}
map->setOverallRowsCounter(overallRowCount); // used for loading progress info
//pre-load auto styles
QHash<QString, Conditions> conditionalStyles;
Styles autoStyles = loadAutoStyles(map->styleManager(), odfContext.stylesReader(),
conditionalStyles, map->parser());
// load the sheet
sheetNode = body.firstChild();
while (!sheetNode.isNull()) {
KoXmlElement sheetElement = sheetNode.toElement();
if (!sheetElement.isNull()) {
// make it slightly faster
KoXml::load(sheetElement);
//debugSheets<<"tableElement.nodeName() bis :"<<sheetElement.nodeName();
if (sheetElement.nodeName() == "table:table") {
if (!sheetElement.attributeNS(KoXmlNS::table, "name", QString()).isEmpty()) {
QString name = sheetElement.attributeNS(KoXmlNS::table, "name", QString());
Sheet* sheet = map->findSheet(name);
if (sheet)
loadSheet(sheet, sheetElement, tableContext, autoStyles, conditionalStyles);
}
}
}
// reduce memory usage
KoXml::unload(sheetElement);
sheetNode = sheetNode.nextSibling();
}
// make sure always at least one sheet exists
if (map->count() == 0) {
map->addNewSheet();
}
//delete any styles which were not used
map->styleManager()->clearOasisStyles();
// Load databases. This needs the sheets to be loaded.
///TODO new style odf
map->databaseManager()->loadOdf(body); // table:database-ranges
loadNamedAreas(map->namedAreaManager(), body); // table:named-expressions
map->setLoading(false);
return true;
}
void Odf::loadMapSettings(Map *map, const KoOasisSettings &settings)
{
KoOasisSettings::Items viewSettings = settings.itemSet("view-settings");
KoOasisSettings::IndexedMap viewMap = viewSettings.indexedMap("Views");
KoOasisSettings::Items firstView = viewMap.entry(0);
KoOasisSettings::NamedMap sheetsMap = firstView.namedMap("Tables");
debugSheets << " loadMapSettings( KoOasisSettings &settings ) exist :" << !sheetsMap.isNull();
if (!sheetsMap.isNull()) {
foreach(Sheet* sheet, map->sheetList()) {
loadSheetSettings(sheet, sheetsMap);
}
}
QString activeSheet = firstView.parseConfigItemString("ActiveTable");
debugSheets << " loadMapSettings( KoOasisSettings &settings ) activeSheet :" << activeSheet;
if (!activeSheet.isEmpty()) {
// Used by View's constructor
map->loadingInfo()->setInitialActiveSheet(map->findSheet(activeSheet));
}
}
bool Odf::saveMap(Map *map, KoXmlWriter & xmlWriter, KoShapeSavingContext & savingContext)
{
// Saving the custom cell styles including the default cell style.
saveStyles(map->styleManager(), savingContext.mainStyles());
// Saving the default column style
KoGenStyle defaultColumnStyle(KoGenStyle::TableColumnStyle, "table-column");
defaultColumnStyle.addPropertyPt("style:column-width", map->defaultColumnFormat()->width());
defaultColumnStyle.setDefaultStyle(true);
savingContext.mainStyles().insert(defaultColumnStyle, "Default", KoGenStyles::DontAddNumberToName);
// Saving the default row style
KoGenStyle defaultRowStyle(KoGenStyle::TableRowStyle, "table-row");
defaultRowStyle.addPropertyPt("style:row-height", map->defaultRowFormat()->height());
defaultRowStyle.setDefaultStyle(true);
savingContext.mainStyles().insert(defaultRowStyle, "Default", KoGenStyles::DontAddNumberToName);
saveCalculationSettings(map->calculationSettings(), xmlWriter); // table::calculation-settings
QByteArray password;
map->password(password);
if (!password.isNull()) {
xmlWriter.addAttribute("table:structure-protected", "true");
QByteArray str = KCodecs::base64Encode(password);
// FIXME Stefan: see OpenDocument spec, ch. 17.3 Encryption
xmlWriter.addAttribute("table:protection-key", QString(str.data()));
}
OdfSavingContext tableContext(savingContext);
foreach(Sheet* sheet, map->sheetList()) {
saveSheet(sheet, tableContext);
}
tableContext.valStyle.writeStyle(xmlWriter);
saveNamedAreas(map->namedAreaManager(), savingContext.xmlWriter());
///TODO new style odf
map->databaseManager()->saveOdf(savingContext.xmlWriter());
return true;
}
// Table shape is here too, as the code is rather similar to Map load/save
bool Odf::loadTableShape(Sheet *sheet, const KoXmlElement &element, KoShapeLoadingContext &context)
{
// pre-load auto styles
KoOdfLoadingContext& odfContext = context.odfLoadingContext();
OdfLoadingContext tableContext(odfContext);
QHash<QString, Conditions> conditionalStyles;
Map *const map = sheet->map();
StyleManager *const styleManager = map->styleManager();
ValueParser *const parser = map->parser();
Styles autoStyles = loadAutoStyles(styleManager, odfContext.stylesReader(), conditionalStyles, parser);
if (!element.attributeNS(KoXmlNS::table, "name", QString()).isEmpty()) {
sheet->setSheetName(element.attributeNS(KoXmlNS::table, "name", QString()), true);
}
bool result = loadSheet(sheet, element, tableContext, autoStyles, conditionalStyles);
// delete any styles which were not used
sheet->map()->styleManager()->clearOasisStyles();
return result;
}
void Odf::saveTableShape(Sheet *sheet, KoShapeSavingContext &context)
{
const Map* map = sheet->map();
// Saving the custom cell styles including the default cell style.
saveStyles(map->styleManager(), context.mainStyles());
// Saving the default column style
KoGenStyle defaultColumnStyle(KoGenStyle::TableColumnStyle, "table-column");
defaultColumnStyle.addPropertyPt("style:column-width", map->defaultColumnFormat()->width());
defaultColumnStyle.setDefaultStyle(true);
context.mainStyles().insert(defaultColumnStyle, "Default", KoGenStyles::DontAddNumberToName);
// Saving the default row style
KoGenStyle defaultRowStyle(KoGenStyle::TableRowStyle, "table-row");
defaultRowStyle.addPropertyPt("style:row-height", map->defaultRowFormat()->height());
defaultRowStyle.setDefaultStyle(true);
context.mainStyles().insert(defaultRowStyle, "Default", KoGenStyles::DontAddNumberToName);
OdfSavingContext tableContext(context);
saveSheet(sheet, tableContext);
tableContext.valStyle.writeStyle(context.xmlWriter());
}
void Odf::loadNamedAreas(NamedAreaManager *manager, const KoXmlElement& body)
{
KoXmlNode namedAreas = KoXml::namedItemNS(body, KoXmlNS::table, "named-expressions");
if (namedAreas.isNull()) return;
debugSheetsODF << "Loading named areas...";
KoXmlElement element;
forEachElement(element, namedAreas) {
if (element.namespaceURI() != KoXmlNS::table)
continue;
if (element.localName() == "named-range") {
if (!element.hasAttributeNS(KoXmlNS::table, "name"))
continue;
if (!element.hasAttributeNS(KoXmlNS::table, "cell-range-address"))
continue;
// TODO: what is: table:base-cell-address
const QString base = element.attributeNS(KoXmlNS::table, "base-cell-address", QString());
// Handle the case where the table:base-cell-address does contain the referenced sheetname
// while it's missing in the table:cell-range-address. See bug #194386 for an example.
Sheet* fallbackSheet = 0;
if (!base.isEmpty()) {
Region region(loadRegion(base), manager->map());
fallbackSheet = region.lastSheet();
}
const QString name = element.attributeNS(KoXmlNS::table, "name", QString());
const QString range = element.attributeNS(KoXmlNS::table, "cell-range-address", QString());
debugSheetsODF << "Named area found, name:" << name << ", area:" << range;
Region region(Odf::loadRegion(range), manager->map(), fallbackSheet);
if (!region.isValid() || !region.lastSheet()) {
debugSheetsODF << "invalid area";
continue;
}
manager->insert(region, name);
} else if (element.localName() == "named-expression") {
debugSheetsODF << "Named expression found.";
// TODO
}
}
}
void Odf::saveNamedAreas(const NamedAreaManager *manager, KoXmlWriter& xmlWriter)
{
QList<QString> areas = manager->areaNames();
if (areas.isEmpty()) return;
Region region;
xmlWriter.startElement("table:named-expressions");
for (int i = 0; i < areas.count(); ++i) {
QString name = areas[i];
region = manager->namedArea(name);
xmlWriter.startElement("table:named-range");
xmlWriter.addAttribute("table:name", name);
xmlWriter.addAttribute("table:base-cell-address", Odf::saveRegion(Region(1, 1, manager->sheet(name)).name()));
xmlWriter.addAttribute("table:cell-range-address", Odf::saveRegion(®ion));
xmlWriter.endElement();
}
xmlWriter.endElement();
}
} // Sheets
} // Calligra
|