KDChartListTable.h

Go to the documentation of this file.
00001 /* -*- Mode: C++ -*-
00002    KDChart - a multi-platform charting engine
00003    */
00004 
00005 /****************************************************************************
00006  ** Copyright (C) 2001-2003 Klarälvdalens Datakonsult AB.  All rights reserved.
00007  **
00008  ** This file is part of the KDChart library.
00009  **
00010  ** This file may be distributed and/or modified under the terms of the
00011  ** GNU General Public License version 2 as published by the Free Software
00012  ** Foundation and appearing in the file LICENSE.GPL included in the
00013  ** packaging of this file.
00014  **
00015  ** Licensees holding valid commercial KDChart licenses may use this file in
00016  ** accordance with the KDChart Commercial License Agreement provided with
00017  ** the Software.
00018  **
00019  ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00020  ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00021  **
00022  ** See http://www.klaralvdalens-datakonsult.se/?page=products for
00023  **   information about KDChart Commercial License Agreements.
00024  **
00025  ** Contact info@klaralvdalens-datakonsult.se if any conditions of this
00026  ** licensing are not clear to you.
00027  **
00028  **********************************************************************/
00029 #ifndef __KDCHARTLISTTABLE_H__
00030 #define __KDCHARTLISTTABLE_H__
00031 
00032 #include <qvaluelist.h>
00033 #include <qshared.h>
00034 #include <qtable.h>
00035 
00036 #include <KDChartDataIntern.h>
00037 #include <KDChartTableBase.h>
00038 
00039 class KDCHART_EXPORT KDChartListTablePrivate : public QShared
00040 {
00041     public:
00042         KDChartListTablePrivate() : QShared() {
00043             row_count = 0;
00044             col_count = 0;
00045         }
00046 
00047         KDChartListTablePrivate( uint _rows, uint _cols ) : QShared() {
00048             for ( uint i = 0; i < _rows; i++ )
00049                 row_list.append( int() );
00050             for ( uint j = 0; j < _cols; j++ )
00051                 col_list.append( int() );
00052             for ( uint k = 0; k < _rows * _cols; k++ )
00053                 matrix.append( KDChartData() );
00054             col_count = _cols;
00055             row_count = _rows;
00056         }
00057 
00058         KDChartListTablePrivate( const KDChartListTablePrivate& _t ) :
00059             QShared(),
00060         matrix( _t.matrix ),
00061         row_list( _t.row_list ),
00062         col_list( _t.col_list ),
00063         col_count( _t.col_count ),
00064         row_count( _t.row_count ) {}
00065         ~KDChartListTablePrivate() {}
00066 
00067         void expand( uint _rows, uint _cols ) {
00068             if ( _rows > row_count ) {
00069                 for ( uint r = 0; r < _rows - row_count; ++r ) {
00070                     row_list.append( int() );
00071                     for ( uint i = 0; i < col_count; ++i )
00072                         matrix.append( KDChartData() );
00073                 }
00074                 row_count = _rows;
00075             }
00076             if ( _cols > col_count ) {
00077                 uint old = col_count;
00078                 col_count = _cols;
00079                 for ( uint c = 0; c < _cols - old; ++c ) {
00080                     col_list.append( int() );
00081                     for ( uint i = 0; i < row_count; ++i )
00082                         matrix.insert( matrix.at( i * col_count + old + c ), KDChartData() );
00083                 }
00084             }
00085         }
00086 
00087         const KDChartData& cell( uint _row, uint _col ) const
00088         {
00089             Q_ASSERT( _row < row_count && _col < col_count );
00090             return matrix[ static_cast < int > ( _row * col_count + _col ) ];
00091         }
00092         KDChartData& cell( uint _row, uint _col )
00093         {
00094             Q_ASSERT( _row < row_count && _col < col_count );
00095             return matrix[ static_cast < int > ( _row * col_count + _col ) ];
00096         }
00097         
00098         void setCell( uint _row, uint _col, const KDChartData& _element )
00099         {
00100             Q_ASSERT( _row < row_count && _col < col_count );
00101             matrix[ static_cast < int > ( _row * col_count + _col ) ].setAll( _element );
00102         }
00103 
00104         void clearCell( uint _row, uint _col ) {
00105             Q_ASSERT( _row < row_count && _col < col_count );
00106             matrix[ static_cast < int > ( _row * col_count + _col ) ].clearValue();
00107         }
00108 
00109         void clearAllCells() {
00110             for ( uint r = 0; r < row_count; ++r )
00111                 for ( uint c = 0; c < col_count; ++c )
00112                     matrix[ static_cast < int > ( r * col_count + c ) ].clearValue();
00113         }
00114 
00115         int& row( uint _row ) {
00116             Q_ASSERT( _row < row_count );
00117             return row_list[ _row ];
00118         }
00119 
00120         const int& row( uint _row ) const {
00121             Q_ASSERT( _row < row_count );
00122             return row_list[ _row ];
00123         }
00124 
00125         void setRow( uint _row, const int& _v ) {
00126             Q_ASSERT( _row < row_count );
00127             row_list[ _row ] = _v;
00128         }
00129 
00130         int& col( uint _col ) {
00131             Q_ASSERT( _col < col_count );
00132             return col_list[ _col ];
00133         }
00134 
00135 
00136         const int& col( uint _col ) const {
00137             Q_ASSERT( _col < col_count );
00138             return col_list[ _col ];
00139         }
00140 
00141         void setCol( uint _col, const int& _v ) {
00142             Q_ASSERT( _col < col_count );
00143             col_list[ _col ] = _v;
00144         }
00145 
00146         void insertColumn( uint _c ) {
00147             Q_ASSERT( _c <= col_count );
00148             ++col_count;
00149             QValueList < KDChartData > ::Iterator it;
00150             for ( uint i = 0; i < row_count; ++i ) {
00151                 it = matrix.at( i * col_count + _c );
00152                 matrix.insert( it, KDChartData() );
00153             }
00154 
00155             QValueList < int > ::Iterator it2 = col_list.at( _c );
00156             col_list.insert( it2, int() );
00157         }
00158 
00159         void insertRow( uint _r ) {
00160             Q_ASSERT( _r <= row_count );
00161             ++row_count;
00162             QValueList < KDChartData > ::Iterator it = matrix.at( _r * col_count );
00163             for ( uint i = 0; i < col_count; ++i )
00164                 matrix.insert( it, KDChartData() );
00165 
00166             QValueList < int > ::Iterator it2 = row_list.at( _r );
00167             row_list.insert( it2, int() );
00168         }
00169 
00170         void removeColumn( uint _c ) {
00171             Q_ASSERT( _c < col_count );
00172             --col_count;
00173             QValueList < KDChartData > ::Iterator it;
00174             for ( uint i = 0; i < row_count; ++i ) {
00175                 it = matrix.at( i * col_count + _c );
00176                 matrix.remove( it );
00177             }
00178 
00179             QValueList < int > ::Iterator it2 = col_list.at( _c );
00180             col_list.remove( it2 );
00181         }
00182 
00183         void removeRow( uint _r ) {
00184             Q_ASSERT( _r < row_count );
00185             --row_count;
00186             QValueList < KDChartData > ::Iterator it = matrix.at( _r * col_count );
00187             for ( uint i = 0; i < col_count; ++i )
00188                 it = matrix.remove( it );
00189 
00190             QValueList < int > ::Iterator it2 = row_list.at( _r );
00191             row_list.remove( it2 );
00192         }
00193 
00194         QValueList < KDChartData > matrix;
00195         QValueList < int > row_list;
00196         QValueList < int > col_list;
00197 
00198         uint col_count;
00199         uint row_count;
00200 };
00201 
00202 
00203 class KDChartListTableData : public KDChartTableDataBase
00204 {
00205     private:
00206         typedef KDChartListTablePrivate Priv;
00207         uint _usedRows, _usedCols;
00208 
00209     public:
00213         typedef QValueList < KDChartData > ::Iterator Iterator;
00214         typedef QValueList < KDChartData > ::ConstIterator ConstIterator;
00215 
00216         typedef QValueList < int > ::Iterator RowIterator;
00217         typedef QValueList < int > ::ConstIterator ConstRowIterator;
00218 
00219         typedef QValueList < int > ::Iterator ColIterator;
00220         typedef QValueList < int > ::ConstIterator ConstColIterator;
00221 
00225         KDChartListTableData() :
00226             KDChartTableDataBase()
00227             {
00228                 sh = new Priv;
00229                 _usedCols = 0;
00230                 _usedRows = 0;
00231             }
00232         KDChartListTableData( uint _rows, uint _cols ) :
00233             KDChartTableDataBase()
00234             {
00235                 sh = new Priv( _rows, _cols );
00236                 _usedRows = _rows;
00237                 _usedCols = _cols;
00238             }
00239 
00240         KDChartListTableData( const KDChartListTableData& _t ) :
00241             KDChartTableDataBase( _t ) {
00242                 _useUsedRows = _t._useUsedRows;
00243                 _useUsedCols = _t._useUsedCols;
00244                 _usedRows = _t._usedRows;
00245                 _usedCols = _t._usedCols;
00246                 sh = _t.sh;
00247                 sh->ref();
00248                 setSorted( _t.sorted() );
00249             }
00250 
00251         virtual ~KDChartListTableData() {
00252             if ( sh->deref() )
00253                 delete sh;
00254         }
00255 
00256         KDChartListTableData& operator=( const KDChartListTableData& t ) {
00257             if ( &t == this )
00258                 return * this;
00259             _useUsedRows = t._useUsedRows;
00260             _useUsedCols = t._useUsedCols;
00261             _usedRows = t._usedRows;
00262             _usedCols = t._usedCols;
00263             t.sh->ref();
00264             if ( sh->deref() )
00265                 delete sh;
00266             sh = t.sh;
00267             setSorted( t.sorted() );
00268             return *this;
00269         }
00270 
00271         Iterator begin() {
00272             return sh->matrix.begin();
00273         }
00274 
00275         ConstIterator begin() const {
00276             return sh->matrix.begin();
00277         }
00278 
00279         Iterator end() {
00280             return sh->matrix.end();
00281         }
00282 
00283         ConstIterator end() const {
00284             return sh->matrix.end();
00285         }
00286 
00287         ColIterator colBegin() {
00288             return sh->col_list.begin();
00289         }
00290 
00291         ConstColIterator colBegin() const {
00292             return sh->col_list.begin();
00293         }
00294 
00295         ColIterator colEnd() {
00296             return sh->col_list.end();
00297         }
00298 
00299         ConstColIterator colEnd() const
00300         {
00301             return sh->col_list.end();
00302         }
00303 
00304         RowIterator rowBegin() {
00305             return sh->row_list.begin();
00306         }
00307 
00308         ConstRowIterator rowBegin() const {
00309             return sh->row_list.begin();
00310         }
00311 
00312         RowIterator rowEnd() {
00313             return sh->row_list.end();
00314         }
00315 
00316         ConstRowIterator rowEnd() const {
00317             return sh->row_list.end();
00318         }
00319 
00320         bool isEmpty() const {
00321             return ( sh->col_count == 0 && sh->row_count == 0 );
00322         }
00323 
00324         uint cols() const {
00325             return sh->col_count;
00326         }
00327 
00328         uint rows() const {
00329             return sh->row_count;
00330         }
00331 
00332         virtual bool cellCoord( uint _row, uint _col,
00333                                 QVariant& _value,
00334                                 int coordinate=1 ) const
00335         {
00336             if( _row >= sh->row_count || _col >= sh->col_count )
00337                 return false;
00338             _value = sh->cell( _row, _col ).value( coordinate );
00339             return true;
00340         }
00341 
00342         virtual bool cellProp( uint _row, uint _col,
00343                                int& _prop ) const
00344         {
00345             if( _row >= sh->row_count || _col >= sh->col_count )
00346                 return false;
00347             _prop = sh->cell( _row, _col ).propertySet();
00348             return true;
00349         }
00350 
00351         virtual void setCell( uint _row, uint _col,
00352                               const QVariant& _value1,
00353                               const QVariant& _value2=QVariant() )
00354         {
00355             detach();
00356             const KDChartData element( _value1, _value2 );
00357             sh->setCell( _row, _col, element );
00358         }
00359 
00360         virtual void setProp( uint _row, uint _col,
00361                               int _propSet=0 )
00362         {
00363             sh->cell( _row, _col ).setPropertySet( _propSet );
00364         }
00365         
00366         void clearCell( uint _row, uint _col ) {
00367             detach();
00368             sh->clearCell( _row, _col );
00369         }
00370 
00371         void clearAllCells() {
00372             detach();
00373             sh->clearAllCells();
00374         }
00375 
00376         int& row( uint _row ) {
00377             detach();
00378             return sh->row( _row );
00379         }
00380 
00381         const int& row( uint _row ) const {
00382             return sh->row( _row );
00383         }
00384 
00385         void setRow( uint _row, const int& _v ) {
00386             detach();
00387             sh->setRow( _row, _v );
00388         }
00389 
00390         int& col( uint _col ) {
00391             detach();
00392             return sh->col( _col );
00393         }
00394 
00395         const int& col( uint _col ) const {
00396             return sh->col( _col );
00397         }
00398 
00399         void setCol( uint _col, const int& _v ) {
00400             detach();
00401             sh->setCol( _col, _v );
00402         }
00403 
00404         void insertColumn( uint _c ) {
00405             detach();
00406             sh->insertColumn( _c );
00407             ++_usedCols;
00408         }
00409 
00410         void insertRow( uint _r ) {
00411             detach();
00412             sh->insertRow( _r );
00413             ++_usedRows;
00414         }
00415 
00416         void removeColumn( uint _c ) {
00417             detach();
00418             sh->removeColumn( _c );
00419             if( _usedCols  )
00420                 --_usedCols;
00421         }
00422 
00423         void removeRow( uint _r ) {
00424             detach();
00425             sh->removeRow( _r );
00426             if( _usedRows  )
00427                 --_usedRows;
00428         }
00429 
00430         void expand( uint _rows, uint _cols ) {
00431             detach();
00432             sh->expand( _rows, _cols );
00433             // adjust the usedRows / usedCols, if they had been set before
00434             if( _useUsedCols )
00435                 setUsedCols( QMIN( _usedCols, _cols ) );
00436             if( _useUsedRows )
00437                 setUsedRows( QMIN( _usedRows, _rows ) );
00438         }
00439 
00440         void setUsedRows( uint _rows ) {
00441             Q_ASSERT( _rows <= rows() );
00442             if( _usedRows < _rows )
00443                 setSorted( false );
00444             _usedRows = _rows;
00445             _useUsedRows = true;
00446         }
00447 
00448         uint usedRows() const {
00449             return _useUsedRows ? _usedRows : rows();
00450         }
00451 
00452         void setUsedCols( uint _cols ) {
00453             Q_ASSERT( _cols <= cols() );
00454             if( _usedCols < _cols )
00455                 setSorted( false );
00456             _usedCols = _cols;
00457             _useUsedCols = true;
00458         }
00459 
00460         uint usedCols() const {
00461             return _useUsedCols ? _usedCols : cols();
00462         }
00463 
00464     private:
00468         void detach() {
00469             if ( sh->count > 1 ) {
00470                 sh->deref();
00471                 sh = new Priv( *sh );
00472             }
00473             setSorted( false );
00474         }
00475 
00479         Priv* sh;
00480 };
00481 
00482 #endif
00483 // __KDCHARTLISTTABLE_H__
00484 

Generated on Wed Jan 26 13:03:16 2011 for KMyMoney by  doxygen 1.5.6