workbook.cpp
1/* Swinder - Portable library for spreadsheet
2 Copyright (C) 2003 Ariya Hidayat <ariya@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA
18*/
19
20#include "workbook.h"
21#include "sheet.h"
22#include "excel.h"
23#include "format.h"
24
25#include <iostream>
26#include <vector>
27#include <map>
28
29using namespace Swinder;
30
31class Workbook::Private
32{
33public:
34 std::vector<Sheet*> sheets;
35 bool autoCalc;
36 bool passwordProtected;
37 std::map<int,Format> formats;
38 int maxFormatIndex;
39};
40
41Workbook::Workbook()
42{
43 d = new Workbook::Private();
44 d->autoCalc = true;
45 d->passwordProtected = false;
46 d->maxFormatIndex = 0;
47}
48
49Workbook::~Workbook()
50{
51 clear();
52 delete d;
53}
54
55void Workbook::clear()
56{
57 // FIXME use iterator
58 for( unsigned i=0; i<sheetCount(); i++ )
59 {
60 Sheet* s = sheet( i );
61 delete s;
62 }
63 d->sheets.clear();
64}
65
66bool Workbook::load( const char* filename )
67{
68 ExcelReader* reader = new ExcelReader;
69 bool result = reader->load( this, filename );
70 delete reader;
71 return result;
72}
73
74void Workbook::appendSheet( Sheet* sheet )
75{
76 d->sheets.push_back( sheet );
77}
78
79unsigned Workbook::sheetCount() const
80{
81 return d->sheets.size();
82}
83
84Sheet* Workbook::sheet( unsigned index )
85{
86 if( index >= sheetCount() ) return (Sheet*)0;
87 return d->sheets[index];
88}
89
90int Workbook::indexOf( Sheet *s )
91{
92 if( s )
93 for( unsigned i = 0; i < sheetCount(); i++ )
94 if( d->sheets[i] == s )
95 return i;
96
97 return -1;
98}
99
100bool Workbook::autoCalc() const
101{
102 return d->autoCalc;
103}
104
105void Workbook::setAutoCalc( bool a )
106{
107 d->autoCalc = a;
108}
109
110void Workbook::setFormat( int index, const Format& format )
111{
112 d->formats[index] = format;
113 if (index > d->maxFormatIndex)
114 d->maxFormatIndex = index;
115}
116
117const Format& Workbook::format( int index ) const
118{
119 return d->formats[index];
120}
121
122const int Workbook::maxFormatIndex() const
123{
124 return d->maxFormatIndex;
125}
126
127bool Workbook::isPasswordProtected() const
128{
129 return d->passwordProtected;
130}
131
132void Workbook::setPasswordProtected( bool p )
133{
134 d->passwordProtected = p;
135}