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 };
00309 
00310 Column::Column( Sheet* sheet, unsigned index )
00311 {
00312   d = new Column::Private;
00313   d->sheet   = sheet;
00314   d->index   = index;
00315   d->width   = 10;
00316   d->visible = true;
00317 }
00318 
00319 Column::~Column()
00320 {
00321   delete d;
00322 }
00323 
00324 Sheet* Column::sheet() const
00325 {
00326   return d->sheet;
00327 }
00328 
00329 unsigned Column::index() const
00330 {
00331   return d->index;
00332 }
00333 
00334 double Column::width() const
00335 {
00336   return d->width;
00337 }
00338 
00339 void Column::setWidth( double w )
00340 {
00341   d->width = w;
00342 }
00343 
00344 const Format& Column::format() const
00345 {
00346   return d->format;
00347 }
00348 
00349 void Column::setFormat( const Format& f )
00350 {
00351   d->format = f;
00352 }
00353 
00354 bool Column::visible() const
00355 {
00356   return d->visible;
00357 }
00358 
00359 void Column::setVisible( bool b )
00360 {
00361   d->visible = b;
00362 }
00363 
00364 class Row::Private
00365 {
00366 public:
00367   Sheet* sheet;
00368   unsigned index;
00369   double height;
00370   Format format;
00371   bool visible;
00372 };
00373 
00374 Row::Row( Sheet* sheet, unsigned index )
00375 {
00376   d = new Row::Private;
00377   d->sheet   = sheet;
00378   d->index   = index;
00379   d->height  = 10;
00380   d->visible = true;
00381 }
00382 
00383 Row::~Row()
00384 {
00385   delete d;
00386 }
00387 
00388 Sheet* Row::sheet() const
00389 {
00390   return d->sheet;
00391 }
00392 
00393 unsigned Row::index() const
00394 {
00395   return d->index;
00396 }
00397 
00398 double Row::height() const
00399 {
00400   return d->height;
00401 }
00402 
00403 void Row::setHeight( double w )
00404 {
00405   d->height = w;
00406 }
00407 
00408 const Format& Row::format() const
00409 {
00410   return d->format;
00411 }
00412 
00413 void Row::setFormat( const Format& f )
00414 {
00415   d->format = f;
00416 }
00417 
00418 bool Row::visible() const
00419 {
00420   return d->visible;
00421 }
00422 
00423 void Row::setVisible( bool b )
00424 {
00425   d->visible = b;
00426 }
KDE Home | KDE Accessibility Home | Description of Access Keys