kchart

KDChartVectorTable.h

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 __KDCHARTVECTORTABLE_H__
00030 #define __KDCHARTVECTORTABLE_H__
00031 
00032 #include <qvaluevector.h>
00033 #include <qshared.h>
00034 #include <qtable.h>
00035 
00036 #include <KDChartDataIntern.h>
00037 #include <KDChartTableBase.h>
00038 
00039 class KDCHART_EXPORT KDChartVectorTablePrivate : public QShared
00040 {
00041 public:
00042     KDChartVectorTablePrivate() : QShared() {
00043         row_count = 0;
00044         col_count = 0;
00045     }
00046 
00047     KDChartVectorTablePrivate( uint _rows, uint _cols ) : QShared() {
00048         matrix.resize( _rows * _cols, KDChartData() );
00049         col_count = _cols;
00050         row_count = _rows;
00051     }
00052 
00053     KDChartVectorTablePrivate( const KDChartVectorTablePrivate& _t ) :
00054         QShared(),
00055         matrix( _t.matrix ),
00056         col_count( _t.col_count ),
00057         row_count( _t.row_count ) {}
00058 
00059     ~KDChartVectorTablePrivate() {}
00060 
00061     void expand( uint _rows, uint _cols ) {
00062         // Save the old table
00063         QValueVector<KDChartData> save( matrix );
00064 
00065         // Delete old data, then resize
00066         matrix.resize( 0 );
00067         matrix.resize( _rows * _cols, KDChartData() );
00068 
00069         // Copy over the old data
00070         for( uint row = 0; row < QMIN( row_count, _rows ); row++ )
00071             for( uint col = 0; col < QMIN( col_count, _cols ); col++ )
00072                 matrix[ row * _cols + col ].setAll( save[ row * col_count + col ] );
00073 
00074         // set the new counts
00075         col_count = _cols;
00076         row_count = _rows;
00077     }
00078 
00079     KDChartData& cell( uint _row, uint _col ) {
00080         Q_ASSERT( _row < row_count  );
00081         Q_ASSERT( _col < col_count );
00082         return matrix[ static_cast < int > ( _row * col_count + _col ) ];
00083     }
00084     const KDChartData& cell( uint _row, uint _col ) const {
00085         Q_ASSERT( _row < row_count  );
00086         Q_ASSERT( _col < col_count );
00087         return matrix[ static_cast < int > ( _row * col_count + _col ) ];
00088     }
00089     void setCell( uint _row, uint _col, const KDChartData& _element ) {
00090         Q_ASSERT( _row < row_count  );
00091         Q_ASSERT( _col < col_count );
00092         matrix[ static_cast < int > ( _row * col_count + _col ) ].setAll( _element );
00093     }
00094 
00095     void clearCell( uint _row, uint _col ) {
00096         Q_ASSERT( _row < row_count  );
00097         Q_ASSERT( _col < col_count );
00098         matrix[ static_cast < int > ( _row * col_count + _col ) ].clearValue();
00099     }
00100 
00101     void clearAllCells() {
00102         for ( uint r = 0; r < row_count; ++r )
00103             for ( uint c = 0; c < col_count; ++c )
00104                 matrix[ r * col_count + c ].clearValue();
00105     }
00106 
00107     QValueVector<KDChartData> matrix;
00108 
00109     uint col_count;
00110     uint row_count;
00111 };
00112 
00113 
00114 class KDCHART_EXPORT KDChartVectorTableData : public KDChartTableDataBase
00115 {
00116     Q_OBJECT
00117 
00118 private:
00119     typedef KDChartVectorTablePrivate Priv;
00120     uint _usedRows, _usedCols;
00121 
00122 public:
00126     typedef QValueVector<KDChartData>::iterator Iterator;
00127     typedef QValueVector<KDChartData>::const_iterator ConstIterator;
00128 
00129     typedef QValueVector<int>::iterator RowIterator;
00130     typedef QValueVector<int>::const_iterator ConstRowIterator;
00131 
00132     typedef QValueVector<int>::iterator ColIterator;
00133     typedef QValueVector<int>::const_iterator ConstColIterator;
00134 
00138     KDChartVectorTableData() :
00139         KDChartTableDataBase()
00140     {
00141         sh = new Priv;
00142         _usedCols = 0;
00143         _usedRows = 0;
00144     }
00145     KDChartVectorTableData( uint _rows, uint _cols ) :
00146         KDChartTableDataBase()
00147     {
00148         sh = new Priv( _rows, _cols );
00149         _usedRows = _rows;
00150         _usedCols = _cols;
00151     }
00152 
00153     KDChartVectorTableData( const KDChartVectorTableData& _t ) :
00154         KDChartTableDataBase( _t ) {
00155         _useUsedRows = _t._useUsedRows;
00156         _useUsedCols = _t._useUsedCols;
00157         _usedRows = _t._usedRows;
00158         _usedCols = _t._usedCols;
00159         sh = _t.sh;
00160         sh->ref();
00161         setSorted( _t.sorted() );
00162     }
00163 
00164     virtual ~KDChartVectorTableData() {
00165         if ( sh->deref() )
00166             delete sh;
00167     }
00168 
00169     KDChartVectorTableData& operator=( const KDChartVectorTableData& t ) {
00170         if ( &t == this )
00171             return * this;
00172         _useUsedRows = t._useUsedRows;
00173         _useUsedCols = t._useUsedCols;
00174         _usedRows = t._usedRows;
00175         _usedCols = t._usedCols;
00176         t.sh->ref();
00177         if ( sh->deref() )
00178             delete sh;
00179         sh = t.sh;
00180         setSorted( t.sorted() );
00181         return *this;
00182     }
00183 
00184 public slots:
00185     Iterator begin() {
00186         return sh->matrix.begin();
00187     }
00188 
00189     ConstIterator begin() const {
00190         return sh->matrix.begin();
00191     }
00192 
00193     Iterator end() {
00194         return sh->matrix.end();
00195     }
00196 
00197     ConstIterator end() const {
00198         return sh->matrix.end();
00199     }
00200 
00201     bool isEmpty() const {
00202         return ( sh->col_count == 0 && sh->row_count == 0 );
00203     }
00204 
00205     uint cols() const {
00206         return sh->col_count;
00207     }
00208 
00209     uint rows() const {
00210         return sh->row_count;
00211     }
00212     // WARNING: The KDChartData class is an internal class now,
00213     //          and nobody supposed to use it any longer.
00214     // Instead, please use cellCoord(), cellProp(), setCell(), setProp(), ...
00215     // (khz, 2006-05-23)
00216 /*
00217     KDChartData& cell( uint _row, uint _col ) {
00218         detach();
00219         return sh->cell( _row, _col );
00220     }
00221 */
00222     virtual bool cellCoord( uint _row, uint _col,
00223                             QVariant& _value,
00224                             int coordinate=1 ) const
00225     {
00226         if( _row >= sh->row_count || _col >= sh->col_count )
00227             return false;
00228         _value = sh->cell( _row, _col ).value( coordinate );
00229         return true;
00230     }
00231 
00232     virtual bool cellProp( uint _row, uint _col,
00233                            int& _prop ) const
00234     {
00235         if( _row >= sh->row_count || _col >= sh->col_count )
00236             return false;
00237         _prop = sh->cell( _row, _col ).propertySet();
00238         return true;
00239     }
00240 
00241     virtual void setCell( uint _row, uint _col,
00242                           const QVariant& _value1,
00243                           const QVariant& _value2=QVariant() ) {
00244         detach();
00245         const KDChartData element( _value1, _value2 );
00246         sh->setCell( _row, _col, element );
00247     }
00248 
00249     virtual void setProp( uint _row, uint _col,
00250                           int _propSet=0 )
00251     {
00252         sh->cell( _row, _col ).setPropertySet( _propSet );
00253     }
00254     
00255     void clearCell( uint _row, uint _col ) {
00256         detach();
00257         sh->clearCell( _row, _col );
00258     }
00259 
00260     void clearAllCells() {
00261         detach();
00262         sh->clearAllCells();
00263     }
00264 
00265     void expand( uint _rows, uint _cols ) {
00266         detach();
00267         sh->expand( _rows, _cols );
00268         // adjust the usedRows / usedCols, if they had been set before
00269         if( _useUsedCols )
00270             setUsedCols( QMIN( _usedCols, _cols ) );
00271         if( _useUsedRows )
00272             setUsedRows( QMIN( _usedRows, _rows ) );
00273     }
00274 
00275     void setUsedRows( uint _rows ) {
00276         Q_ASSERT( _rows <= rows() );
00277         if( _usedRows < _rows )
00278             setSorted( false );
00279         _usedRows = _rows;
00280         _useUsedRows = true;
00281     }
00282 
00283     uint usedRows() const {
00284         return _useUsedRows ? _usedRows : rows();
00285     }
00286 
00287     void setUsedCols( uint _cols ) {
00288         Q_ASSERT( _cols <= cols() );
00289         if( _usedCols < _cols )
00290             setSorted( false );
00291         _usedCols = _cols;
00292         _useUsedCols = true;
00293     }
00294 
00295     uint usedCols() const {
00296         return _useUsedCols ? _usedCols : cols();
00297     }
00298 
00299 private:
00303     void detach() {
00304         if ( sh->count > 1 ) {
00305             sh->deref();
00306             sh = new Priv( *sh );
00307         }
00308         setSorted( false );
00309     }
00310 
00314     Priv* sh;
00315 };
00316 
00317 #endif
00318 // __KDCHARTLISTTABLE_H__
00319 
KDE Home | KDE Accessibility Home | Description of Access Keys