File indexing completed on 2024-05-12 16:35:14
0001 /* This file is part of the KDE project 0002 Copyright 2007 Stefan Nikolaus <stefan.nikolaus@kdemail.net> 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 "DatabaseManager.h" 0021 0022 #include <QHash> 0023 0024 #include <KoXmlNS.h> 0025 #include <KoXmlWriter.h> 0026 0027 #include "CellStorage.h" 0028 #include "Database.h" 0029 #include "Map.h" 0030 #include "Region.h" 0031 #include "Sheet.h" 0032 0033 using namespace Calligra::Sheets; 0034 0035 class Q_DECL_HIDDEN DatabaseManager::Private 0036 { 0037 public: 0038 const Map* map; 0039 static int s_id; 0040 }; 0041 0042 int DatabaseManager::Private::s_id = 1; 0043 0044 0045 DatabaseManager::DatabaseManager(const Map* map) 0046 : d(new Private) 0047 { 0048 d->map = map; 0049 } 0050 0051 DatabaseManager::~DatabaseManager() 0052 { 0053 delete d; 0054 } 0055 0056 QString DatabaseManager::createUniqueName() const 0057 { 0058 return "database-" + QString::number(Private::s_id++); 0059 } 0060 0061 bool DatabaseManager::loadOdf(const KoXmlElement& body) 0062 { 0063 const KoXmlNode databaseRanges = KoXml::namedItemNS(body, KoXmlNS::table, "database-ranges"); 0064 KoXmlElement element; 0065 forEachElement(element, databaseRanges) { 0066 if (element.namespaceURI() != KoXmlNS::table) 0067 continue; 0068 if (element.localName() == "database-range") { 0069 Database database; 0070 if (!database.loadOdf(element, d->map)) 0071 return false; 0072 const Region region = database.range(); 0073 if (!region.isValid()) 0074 continue; 0075 const Sheet* sheet = region.lastSheet(); 0076 if (!sheet) 0077 continue; 0078 sheet->cellStorage()->setDatabase(region, database); 0079 } 0080 } 0081 return true; 0082 } 0083 0084 void DatabaseManager::saveOdf(KoXmlWriter& xmlWriter) const 0085 { 0086 QList< QPair<QRectF, Database> > databases; 0087 const Region region(QRect(QPoint(1, 1), QPoint(KS_colMax, KS_rowMax))); 0088 const QList<Sheet*>& sheets = d->map->sheetList(); 0089 for (int i = 0; i < sheets.count(); ++i) 0090 databases << sheets[i]->cellStorage()->databases(region); 0091 if (databases.isEmpty()) 0092 return; 0093 0094 xmlWriter.startElement("table:database-ranges"); 0095 for (int i = 0; i < databases.count(); ++i) { 0096 Database database = databases[i].second; 0097 database.setRange(Region(databases[i].first.toRect(), database.range().firstSheet())); 0098 if (!database.range().isValid()) 0099 continue; 0100 database.saveOdf(xmlWriter); 0101 } 0102 xmlWriter.endElement(); 0103 }