filters

sheet.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 "cell.h"
21#include "sheet.h"
22#include "workbook.h"
23#include "ustring.h"
24
25#include <iostream>
26#include <map>
27
28namespace Swinder
29{
30
31class Sheet::Private
32{
33public:
34 Workbook* workbook;
35 UString name;
36
37 // hash to store cell, FIXME replace with quad-tree
38 std::map<unsigned,Cell*> cells;
39 unsigned maxRow;
40 unsigned maxColumn;
41 std::map<unsigned,Column*> columns;
42 std::map<unsigned,Row*> rows;
43
44 bool visible;
45 bool protect;
46
47 UString leftHeader;
48 UString centerHeader;
49 UString rightHeader;
50 UString leftFooter;
51 UString centerFooter;
52 UString rightFooter;
53
54 double leftMargin;
55 double rightMargin;
56 double topMargin;
57 double bottomMargin;
58};
59
60}
61
62using namespace Swinder;
63
64Sheet::Sheet( Workbook* wb )
65{
66 d = new Sheet::Private();
67 d->workbook = wb;
68 d->name = "Sheet"; // FIXME better name ?
69 d->maxRow = 0;
70 d->maxColumn = 0;
71 d->visible = true;
72 d->protect = false;
73 d->leftMargin = 54; // 0.75 inch
74 d->rightMargin = 54; // 0.75 inch
75 d->topMargin = 72; // 1 inch
76 d->bottomMargin = 72; // 1 inch
77}
78
79Sheet::~Sheet()
80{
81 clear();
82 delete d;
83}
84
85Workbook* Sheet::workbook()
86{
87 return d->workbook;
88}
89
90void Sheet::clear()
91{
92 // delete all cells
93 std::map<unsigned,Cell*>::iterator cell_it;
94 for( cell_it = d->cells.begin(); cell_it != d->cells.end(); ++cell_it )
95 delete cell_it->second;
96
97 // delete all columns
98 std::map<unsigned,Column*>::iterator col_it;
99 for( col_it = d->columns.begin(); col_it != d->columns.end(); ++col_it )
100 delete col_it->second;
101
102 // delete all rows
103 std::map<unsigned,Row*>::iterator row_it;
104 for( row_it = d->rows.begin(); row_it != d->rows.end(); ++row_it )
105 delete row_it->second;
106}
107
108UString Sheet::name() const
109{
110 return d->name;
111}
112
113void Sheet::setName( const UString& name )
114{
115 d->name = name;
116}
117
118Cell* Sheet::cell( unsigned columnIndex, unsigned rowIndex, bool autoCreate )
119{
120 unsigned hashed = (rowIndex+1)*1024 + columnIndex + 1;
121 Cell* c = d->cells[ hashed ];
122
123 // create cell if necessary
124 if( !c && autoCreate )
125 {
126 c = new Cell( this, columnIndex, rowIndex );
127 d->cells[ hashed ] = c;
128
129 // force creating the column and row
130 this->column( columnIndex, true );
131 this->row( rowIndex, true );
132
133 if( rowIndex > d->maxRow ) d->maxRow = rowIndex;
134 if( columnIndex > d->maxColumn ) d->maxColumn = columnIndex;
135 }
136
137 return c;
138}
139
140Column* Sheet::column( unsigned index, bool autoCreate )
141{
142 Column* c = d->columns[ index ];
143
144 // create column if necessary
145 if( !c && autoCreate )
146 {
147 c = new Column( this, index );
148 d->columns[ index ] = c;
149 if( index > d->maxColumn ) d->maxColumn = index;
150 }
151
152 return c;
153}
154
155Row* Sheet::row( unsigned index, bool autoCreate )
156{
157 Row* r = d->rows[ index ];
158
159 // create row if necessary
160 if( !r && autoCreate )
161 {
162 r = new Row( this, index );
163 d->rows[ index ] = r;
164 if( index > d->maxRow ) d->maxRow = index;
165 }
166
167 return r;
168}
169
170unsigned Sheet::maxRow() const
171{
172 return d->maxRow;
173}
174
175unsigned Sheet::maxColumn() const
176{
177 return d->maxColumn;
178}
179
180bool Sheet::visible() const
181{
182 return d->visible;
183}
184
185void Sheet::setVisible( bool v )
186{
187 d->visible = v;
188}
189
190bool Sheet::protect() const
191{
192 return d->protect;
193}
194
195void Sheet::setProtect( bool p )
196{
197 d->protect = p;
198}
199
200UString Sheet::leftHeader() const
201{
202 return d->leftHeader;
203}
204
205void Sheet::setLeftHeader( const UString& h )
206{
207 d->leftHeader = h;
208}
209
210UString Sheet::centerHeader() const
211{
212 return d->centerHeader;
213}
214
215void Sheet::setCenterHeader( const UString& h )
216{
217 d->centerHeader = h;
218}
219
220UString Sheet::rightHeader() const
221{
222 return d->rightHeader;
223}
224
225void Sheet::setRightHeader( const UString& h )
226{
227 d->rightHeader = h;
228}
229
230UString Sheet::leftFooter() const
231{
232 return d->leftFooter;
233}
234
235void Sheet::setLeftFooter( const UString& h )
236{
237 d->leftFooter = h;
238}
239
240UString Sheet::centerFooter() const
241{
242 return d->centerFooter;
243}
244
245void Sheet::setCenterFooter( const UString& h )
246{
247 d->centerFooter = h;
248}
249
250UString Sheet::rightFooter() const
251{
252 return d->rightFooter;
253}
254
255void Sheet::setRightFooter( const UString& h )
256{
257 d->rightFooter = h;
258}
259
260double Sheet::leftMargin() const
261{
262 return d->leftMargin;
263}
264
265void Sheet::setLeftMargin( double m )
266{
267 d->leftMargin = m;
268}
269
270double Sheet::rightMargin() const
271{
272 return d->rightMargin;
273}
274
275void Sheet::setRightMargin( double m )
276{
277 d->rightMargin = m;
278}
279
280double Sheet::topMargin() const
281{
282 return d->topMargin;
283}
284
285void Sheet::setTopMargin( double m )
286{
287 d->topMargin = m;
288}
289
290double Sheet::bottomMargin() const
291{
292 return d->bottomMargin;
293}
294
295void Sheet::setBottomMargin( double m )
296{
297 d->bottomMargin = m;
298}
299
300class Column::Private
301{
302public:
303 Sheet* sheet;
304 unsigned index;
305 double width;
306 Format format;
307 bool visible;
308 int formatIndex;
309};
310
311Column::Column( Sheet* sheet, unsigned index )
312{
313 d = new Column::Private;
314 d->sheet = sheet;
315 d->index = index;
316 d->width = 10;
317 d->visible = true;
318 d->formatIndex = -1;
319}
320
321Column::~Column()
322{
323 delete d;
324}
325
326Sheet* Column::sheet() const
327{
328 return d->sheet;
329}
330
331unsigned Column::index() const
332{
333 return d->index;
334}
335
336double Column::width() const
337{
338 return d->width;
339}
340
341void Column::setWidth( double w )
342{
343 d->width = w;
344}
345
346int Column::formatIndex() const
347{
348 return d->formatIndex;
349}
350
351void Column::setFormatIndex( int index )
352{
353 d->formatIndex = index;
354}
355
356
357const Format& Column::format() const
358{
359 return d->format;
360}
361
362void Column::setFormat( const Format& f )
363{
364 d->format = f;
365}
366
367bool Column::visible() const
368{
369 return d->visible;
370}
371
372void Column::setVisible( bool b )
373{
374 d->visible = b;
375}
376
377class Row::Private
378{
379public:
380 Sheet* sheet;
381 unsigned index;
382 double height;
383 Format format;
384 bool visible;
385 int formatIndex;
386};
387
388Row::Row( Sheet* sheet, unsigned index )
389{
390 d = new Row::Private;
391 d->sheet = sheet;
392 d->index = index;
393 d->height = 10;
394 d->visible = true;
395}
396
397Row::~Row()
398{
399 delete d;
400}
401
402Sheet* Row::sheet() const
403{
404 return d->sheet;
405}
406
407unsigned Row::index() const
408{
409 return d->index;
410}
411
412double Row::height() const
413{
414 return d->height;
415}
416
417void Row::setHeight( double w )
418{
419 d->height = w;
420}
421
422int Row::formatIndex() const
423{
424 return d->formatIndex;
425}
426
427void Row::setFormatIndex( int index )
428{
429 d->formatIndex = index;
430}
431
432const Format& Row::format() const
433{
434 return d->format;
435}
436
437void Row::setFormat( const Format& f )
438{
439 d->format = f;
440}
441
442bool Row::visible() const
443{
444 return d->visible;
445}
446
447void Row::setVisible( bool b )
448{
449 d->visible = b;
450}
Column()
Constructors.
Definition column.cpp:29
Row()
Constructors.
Definition row.cpp:29