kspread

krs_doc.cpp

00001 /*
00002  *  Copyright (c) 2005 Cyrille Berger <cberger@cberger.net>
00003  *  Copyright (c) 2006 Isaac Clerencia <isaac@warp.es>
00004  *
00005  *  This program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU Library General Public License as published by
00007  *  the Free Software Foundation; either version 2 of the License, or
00008  *  (at your option) any later version.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU Library General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU Library General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018  */
00019 
00020 #include "krs_doc.h"
00021 #include "krs_sheet.h"
00022 
00023 #include <kspread_map.h>
00024 #include <kspread_sheet.h>
00025 
00026 namespace Kross { namespace KSpreadCore {
00027 
00028 Doc::Doc(KSpread::Doc* doc)
00029     : Kross::Api::Class<Doc>("KSpreadDocument"), m_doc(doc)
00030 {
00031     this->addFunction0< Sheet >("currentSheet", this, &Doc::currentSheet);
00032     this->addFunction1< Sheet, Kross::Api::Variant >("sheetByName", this, &Doc::sheetByName);
00033     this->addFunction0< Kross::Api::Variant >("sheetNames", this, &Doc::sheetNames);
00034 
00035     this->addFunction1< Kross::Api::Variant, Kross::Api::Variant >("addSheet", this, &Doc::addSheet);
00036     this->addFunction1< Kross::Api::Variant, Kross::Api::Variant >("removeSheet", this, &Doc::removeSheet);
00037 
00038     this->addFunction1< Kross::Api::Variant, Kross::Api::Variant >("loadNativeXML", this, &Doc::loadNativeXML);
00039     this->addFunction0< Kross::Api::Variant >("saveNativeXML", this, &Doc::saveNativeXML);
00040 
00041     this->addFunction1< Kross::Api::Variant, Kross::Api::Variant >("openUrl", this, &Doc::openUrl);
00042     this->addFunction1< Kross::Api::Variant, Kross::Api::Variant >("saveUrl", this, &Doc::saveUrl);
00043     this->addFunction1< Kross::Api::Variant, Kross::Api::Variant >("import", this, &Doc::import);
00044     this->addFunction1< Kross::Api::Variant, Kross::Api::Variant >("exp0rt", this, &Doc::exp0rt);
00045 
00046 }
00047 
00048 Doc::~Doc() {
00049 
00050 }
00051 
00052 const QString Doc::getClassName() const {
00053     return "Kross::KSpreadCore::Doc";
00054 }
00055 
00056 Sheet* Doc::currentSheet()
00057 {
00058     return new Sheet(m_doc->displaySheet(), m_doc);
00059 }
00060 
00061 Sheet* Doc::sheetByName(const QString& name)
00062 {
00063     QPtrListIterator<KSpread::Sheet> it (m_doc->map()->sheetList());
00064     for( ; it.current(); ++it )
00065         if(it.current()->sheetName() == name)
00066             return new Sheet(it.current(), m_doc);
00067     return 0;
00068 }
00069 
00070 QStringList Doc::sheetNames()
00071 {
00072     QStringList names;
00073     QPtrListIterator<KSpread::Sheet> it (m_doc->map()->sheetList());
00074     for( ; it.current(); ++it )
00075         names.append( it.current()->sheetName() );
00076     return names;
00077 }
00078 
00079 bool Doc::addSheet(const QString& sheetname)
00080 {
00081     KSpread::Sheet* sheet = m_doc->map()->createSheet();
00082     if(sheet) {
00083         if(! sheet->setSheetName(sheetname)) {
00084             delete sheet;
00085             return false;
00086         }
00087         m_doc->map()->addSheet(sheet);
00088         return true;
00089     }
00090     return false;
00091 }
00092 
00093 bool Doc::removeSheet(const QString& sheetname)
00094 {
00095     KSpread::Sheet* sheet = m_doc->map()->findSheet(sheetname);
00096     if(sheet) {
00097         m_doc->map()->takeSheet(sheet);
00098         return true;
00099     }
00100     return false;
00101 }
00102 
00103 bool Doc::loadNativeXML(const QString& xml) {
00104     QDomDocument doc;
00105     if(! doc.setContent(xml, true))
00106         return false;
00107     return m_doc->loadXML(0, doc);
00108 }
00109 
00110 QString Doc::saveNativeXML() {
00111     return m_doc->saveXML().toString(2);
00112 }
00113 
00114 bool Doc::openUrl(const QString& url)
00115 {
00116     return m_doc->openURL(url);
00117 }
00118 
00119 bool Doc::saveUrl(const QString& url)
00120 {
00121     return m_doc->saveAs(url);
00122 }
00123 
00124 bool Doc::import(const QString& url)
00125 {
00126     return m_doc->import(url);
00127 }
00128 
00129 bool Doc::exp0rt(const QString& url)
00130 {
00131     return m_doc->exp0rt(url);
00132 }
00133 
00134 }}
00135 
KDE Home | KDE Accessibility Home | Description of Access Keys