filters

sheet.cpp

00001 /* Swinder - Portable library for spreadsheet
00002    Copyright (C) 2003 Ariya Hidayat <ariya@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008    
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA
00018 */
00019 
00020 #include "cell.h"
00021 #include "sheet.h"
00022 #include "workbook.h"
00023 #include "ustring.h"
00024 
00025 #include <iostream>
00026 #include <map>
00027 
00028 namespace Swinder
00029 {
00030 
00031 class Sheet::Private
00032 {
00033 public:
00034   Workbook* workbook;
00035   UString name;
00036     
00037   // hash to store cell, FIXME replace with quad-tree
00038   std::map<unsigned,Cell*> cells;
00039   unsigned maxRow;
00040   unsigned maxColumn;
00041   std::map<unsigned,Column*> columns;
00042   std::map<unsigned,Row*> rows;
00043   
00044   bool visible;
00045   bool protect;
00046 
00047   UString leftHeader;
00048   UString centerHeader;
00049   UString rightHeader;
00050   UString leftFooter;
00051   UString centerFooter;
00052   UString rightFooter;
00053 
00054   double leftMargin;
00055   double rightMargin;
00056   double topMargin;
00057   double bottomMargin;
00058 };
00059 
00060 }
00061 
00062 using namespace Swinder;
00063 
00064 Sheet::Sheet( Workbook* wb )
00065 {
00066   d = new Sheet::Private();
00067   d->workbook = wb;
00068   d->name = "Sheet"; // FIXME better name ?
00069   d->maxRow = 0;
00070   d->maxColumn = 0;
00071   d->visible      = true;
00072   d->protect      = false;
00073   d->leftMargin   = 54;  // 0.75 inch
00074   d->rightMargin  = 54;  // 0.75 inch
00075   d->topMargin    = 72;  // 1 inch
00076   d->bottomMargin = 72;  // 1 inch
00077 }
00078 
00079 Sheet::~Sheet()
00080 {
00081   clear();
00082   delete d;
00083 }
00084 
00085 Workbook* Sheet::workbook()
00086 {
00087   return d->workbook;
00088 }
00089 
00090 void Sheet::clear()
00091 {
00092   // delete all cells
00093   std::map<unsigned,Cell*>::iterator cell_it;
00094   for( cell_it = d->cells.begin(); cell_it != d->cells.end(); ++cell_it )
00095     delete cell_it->second;
00096   
00097   // delete all columns
00098   std::map<unsigned,Column*>::iterator col_it;
00099   for( col_it = d->columns.begin(); col_it != d->columns.end(); ++col_it )
00100     delete col_it->second;
00101   
00102   // delete all rows
00103   std::map<unsigned,Row*>::iterator row_it;
00104   for( row_it = d->rows.begin(); row_it != d->rows.end(); ++row_it )
00105     delete row_it->second;
00106 }
00107 
00108 UString Sheet::name() const
00109 {
00110   return d->name;
00111 }
00112 
00113 void Sheet::setName( const UString& name )
00114 {
00115   d->name = name;
00116 }
00117 
00118 Cell* Sheet::cell( unsigned columnIndex, unsigned rowIndex, bool autoCreate )
00119 {
00120   unsigned hashed = (rowIndex+1)*1024 + columnIndex + 1;
00121   Cell* c = d->cells[ hashed ];
00122   
00123   // create cell if necessary
00124   if( !c && autoCreate )
00125   {
00126     c = new Cell( this, columnIndex, rowIndex );
00127     d->cells[ hashed ] = c;
00128 
00129     // force creating the column and row
00130     this->column( columnIndex, true );
00131     this->row( rowIndex, true );
00132     
00133     if( rowIndex > d->maxRow ) d->maxRow = rowIndex;
00134     if( columnIndex > d->maxColumn ) d->maxColumn = columnIndex;
00135   }
00136   
00137   return c;
00138 }
00139 
00140 Column* Sheet::column( unsigned index, bool autoCreate )
00141 {
00142   Column* c = d->columns[ index ];
00143   
00144   // create column if necessary
00145   if( !c && autoCreate )
00146   {
00147     c = new Column( this, index );
00148     d->columns[ index ] = c;
00149     if( index > d->maxColumn ) d->maxColumn = index;
00150   }
00151   
00152   return c;
00153 }
00154 
00155 Row* Sheet::row( unsigned index, bool autoCreate )
00156 {
00157   Row* r = d->rows[ index ];
00158   
00159   // create row if necessary
00160   if( !r && autoCreate )
00161   {
00162     r = new Row( this, index );
00163     d->rows[ index ] = r;
00164     if( index > d->maxRow ) d->maxRow = index;
00165   }
00166   
00167   return r;
00168 }
00169 
00170 unsigned Sheet::maxRow() const
00171 {
00172   return d->maxRow;
00173 }
00174 
00175 unsigned Sheet::maxColumn() const
00176 {
00177   return d->maxColumn;
00178 }
00179 
00180 bool Sheet::visible() const
00181 {
00182   return d->visible;
00183 }
00184 
00185 void Sheet::setVisible( bool v )
00186 {
00187   d->visible = v;
00188 }
00189 
00190 bool Sheet::protect() const
00191 {
00192   return d->protect;
00193 }
00194 
00195 void Sheet::setProtect( bool p )
00196 {
00197   d->protect = p;
00198 }
00199 
00200 UString Sheet::leftHeader() const
00201 {
00202   return d->leftHeader;
00203 }
00204 
00205 void Sheet::setLeftHeader( const UString& h )
00206 {
00207   d->leftHeader = h;
00208 }
00209 
00210 UString Sheet::centerHeader() const
00211 {
00212   return d->centerHeader;
00213 }
00214 
00215 void Sheet::setCenterHeader( const UString& h )
00216 {
00217   d->centerHeader = h;
00218 }
00219 
00220 UString Sheet::rightHeader() const
00221 {
00222   return d->rightHeader;
00223 }
00224 
00225 void Sheet::setRightHeader( const UString& h )
00226 {
00227   d->rightHeader = h;
00228 }
00229 
00230 UString Sheet::leftFooter() const
00231 {
00232   return d->leftFooter;
00233 }
00234 
00235 void Sheet::setLeftFooter( const UString& h )
00236 {
00237   d->leftFooter = h;
00238 }
00239 
00240 UString Sheet::centerFooter() const
00241 {
00242   return d->centerFooter;
00243 }
00244 
00245 void Sheet::setCenterFooter( const UString& h )
00246 {
00247   d->centerFooter = h;
00248 }
00249 
00250 UString Sheet::rightFooter() const
00251 {
00252   return d->rightFooter;
00253 }
00254 
00255 void Sheet::setRightFooter( const UString& h )
00256 {
00257   d->rightFooter = h;
00258 }
00259 
00260 double Sheet::leftMargin() const
00261 {
00262   return d->leftMargin;
00263 }
00264 
00265 void Sheet::setLeftMargin( double m )
00266 {
00267   d->leftMargin = m;
00268 }
00269 
00270 double Sheet::rightMargin() const
00271 {
00272   return d->rightMargin;
00273 }
00274 
00275 void Sheet::setRightMargin( double m ) 
00276 {
00277   d->rightMargin = m;
00278 }
00279 
00280 double Sheet::topMargin() const
00281 {
00282   return d->topMargin;
00283 }
00284 
00285 void Sheet::setTopMargin( double m ) 
00286 {
00287   d->topMargin = m;
00288 }
00289 
00290 double Sheet::bottomMargin() const
00291 {
00292   return d->bottomMargin;
00293 }
00294 
00295 void Sheet::setBottomMargin( double m ) 
00296 {
00297   d->bottomMargin = m;
00298 }
00299 
00300 class Column::Private
00301 {
00302 public:
00303   Sheet* sheet;
00304   unsigned index;
00305   double width;
00306   Format format;
00307   bool visible;
00308   int formatIndex;
00309 };
00310 
00311 Column::Column( Sheet* sheet, unsigned index )
00312 {
00313   d = new Column::Private;
00314   d->sheet   = sheet;
00315   d->index   = index;
00316   d->width   = 10;
00317   d->visible = true;
00318   d->formatIndex = -1;
00319 }
00320 
00321 Column::~Column()
00322 {
00323   delete d;
00324 }
00325 
00326 Sheet* Column::sheet() const
00327 {
00328   return d->sheet;
00329 }
00330 
00331 unsigned Column::index() const
00332 {
00333   return d->index;
00334 }
00335 
00336 double Column::width() const
00337 {
00338   return d->width;
00339 }
00340 
00341 void Column::setWidth( double w )
00342 {
00343   d->width = w;
00344 }
00345 
00346 int Column::formatIndex() const
00347 {
00348   return d->formatIndex;
00349 }
00350 
00351 void Column::setFormatIndex( int index )
00352 {
00353   d->formatIndex = index;
00354 }
00355 
00356 
00357 const Format& Column::format() const
00358 {
00359   return d->format;
00360 }
00361 
00362 void Column::setFormat( const Format& f )
00363 {
00364   d->format = f;
00365 }
00366 
00367 bool Column::visible() const
00368 {
00369   return d->visible;
00370 }
00371 
00372 void Column::setVisible( bool b )
00373 {
00374   d->visible = b;
00375 }
00376 
00377 class Row::Private
00378 {
00379 public:
00380   Sheet* sheet;
00381   unsigned index;
00382   double height;
00383   Format format;
00384   bool visible;
00385   int formatIndex;
00386 };
00387 
00388 Row::Row( Sheet* sheet, unsigned index )
00389 {
00390   d = new Row::Private;
00391   d->sheet   = sheet;
00392   d->index   = index;
00393   d->height  = 10;
00394   d->visible = true;
00395 }
00396 
00397 Row::~Row()
00398 {
00399   delete d;
00400 }
00401 
00402 Sheet* Row::sheet() const
00403 {
00404   return d->sheet;
00405 }
00406 
00407 unsigned Row::index() const
00408 {
00409   return d->index;
00410 }
00411 
00412 double Row::height() const
00413 {
00414   return d->height;
00415 }
00416 
00417 void Row::setHeight( double w )
00418 {
00419   d->height = w;
00420 }
00421 
00422 int Row::formatIndex() const
00423 {
00424   return d->formatIndex;
00425 }
00426 
00427 void Row::setFormatIndex( int index )
00428 {
00429   d->formatIndex = index;
00430 }
00431 
00432 const Format& Row::format() const
00433 {
00434   return d->format;
00435 }
00436 
00437 void Row::setFormat( const Format& f )
00438 {
00439   d->format = f;
00440 }
00441 
00442 bool Row::visible() const
00443 {
00444   return d->visible;
00445 }
00446 
00447 void Row::setVisible( bool b )
00448 {
00449   d->visible = b;
00450 }
KDE Home | KDE Accessibility Home | Description of Access Keys