krs_doc.cpp
1/*
2 * Copyright (c) 2005 Cyrille Berger <cberger@cberger.net>
3 * Copyright (c) 2006 Isaac Clerencia <isaac@warp.es>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Library General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19
20#include "krs_doc.h"
21#include "krs_sheet.h"
22
23#include <kspread_map.h>
24#include <kspread_sheet.h>
25
26namespace Kross { namespace KSpreadCore {
27
28Doc::Doc(KSpread::Doc* doc)
29 : Kross::Api::Class<Doc>("KSpreadDocument"), m_doc(doc)
30{
31 this->addFunction0< Sheet >("currentSheet", this, &Doc::currentSheet);
32 this->addFunction1< Sheet, Kross::Api::Variant >("sheetByName", this, &Doc::sheetByName);
33 this->addFunction0< Kross::Api::Variant >("sheetNames", this, &Doc::sheetNames);
34
35 this->addFunction1< Kross::Api::Variant, Kross::Api::Variant >("addSheet", this, &Doc::addSheet);
36 this->addFunction1< Kross::Api::Variant, Kross::Api::Variant >("removeSheet", this, &Doc::removeSheet);
37
38 this->addFunction1< Kross::Api::Variant, Kross::Api::Variant >("loadNativeXML", this, &Doc::loadNativeXML);
39 this->addFunction0< Kross::Api::Variant >("saveNativeXML", this, &Doc::saveNativeXML);
40
41 this->addFunction1< Kross::Api::Variant, Kross::Api::Variant >("openUrl", this, &Doc::openUrl);
42 this->addFunction1< Kross::Api::Variant, Kross::Api::Variant >("saveUrl", this, &Doc::saveUrl);
43 this->addFunction1< Kross::Api::Variant, Kross::Api::Variant >("import", this, &Doc::import);
44 this->addFunction1< Kross::Api::Variant, Kross::Api::Variant >("exp0rt", this, &Doc::exp0rt);
45
46}
47
48Doc::~Doc() {
49
50}
51
52const TQString Doc::getClassName() const {
53 return "Kross::KSpreadCore::Doc";
54}
55
56Sheet* Doc::currentSheet()
57{
58 return new Sheet(m_doc->displaySheet(), m_doc);
59}
60
61Sheet* Doc::sheetByName(const TQString& name)
62{
63 TQPtrListIterator<KSpread::Sheet> it (m_doc->map()->sheetList());
64 for( ; it.current(); ++it )
65 if(it.current()->sheetName() == name)
66 return new Sheet(it.current(), m_doc);
67 return 0;
68}
69
70TQStringList Doc::sheetNames()
71{
72 TQStringList names;
73 TQPtrListIterator<KSpread::Sheet> it (m_doc->map()->sheetList());
74 for( ; it.current(); ++it )
75 names.append( it.current()->sheetName() );
76 return names;
77}
78
79bool Doc::addSheet(const TQString& sheetname)
80{
81 KSpread::Sheet* sheet = m_doc->map()->createSheet();
82 if(sheet) {
83 if(! sheet->setSheetName(sheetname)) {
84 delete sheet;
85 return false;
86 }
87 m_doc->map()->addSheet(sheet);
88 return true;
89 }
90 return false;
91}
92
93bool Doc::removeSheet(const TQString& sheetname)
94{
95 KSpread::Sheet* sheet = m_doc->map()->findSheet(sheetname);
96 if(sheet) {
97 m_doc->map()->takeSheet(sheet);
98 return true;
99 }
100 return false;
101}
102
103bool Doc::loadNativeXML(const TQString& xml) {
104 TQDomDocument doc;
105 if(! doc.setContent(xml, true))
106 return false;
107 return m_doc->loadXML(0, doc);
108}
109
110TQString Doc::saveNativeXML() {
111 return m_doc->saveXML().toString(2);
112}
113
114bool Doc::openUrl(const TQString& url)
115{
116 return m_doc->openURL(url);
117}
118
119bool Doc::saveUrl(const TQString& url)
120{
121 return m_doc->saveAs(url);
122}
123
124bool Doc::import(const TQString& url)
125{
126 return m_doc->import(url);
127}
128
129bool Doc::exp0rt(const TQString& url)
130{
131 return m_doc->exp0rt(url);
132}
133
134}}
135
This class holds the data that makes up a spreadsheet.
Definition kspread_doc.h:98