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 
00166     KDChartVectorTableData& operator=( const KDChartVectorTableData& t ) {
00167         if ( &t == this )
00168             return * this;
00169         _useUsedRows = t._useUsedRows;
00170         _useUsedCols = t._useUsedCols;
00171         _usedRows = t._usedRows;
00172         _usedCols = t._usedCols;
00173         t.sh->ref();
00174         if ( sh->deref() )
00175             delete sh;
00176         sh = t.sh;
00177         setSorted( t.sorted() );
00178         return *this;
00179     }
00180 
00181 public slots:
00182     Iterator begin() {
00183         return sh->matrix.begin();
00184     }
00185 
00186     ConstIterator begin() const {
00187         return sh->matrix.begin();
00188     }
00189 
00190     Iterator end() {
00191         return sh->matrix.end();
00192     }
00193 
00194     ConstIterator end() const {
00195         return sh->matrix.end();
00196     }
00197 
00198     bool isEmpty() const {
00199         return ( sh->col_count == 0 && sh->row_count == 0 );
00200     }
00201 
00202     uint cols() const {
00203         return sh->col_count;
00204     }
00205 
00206     uint rows() const {
00207         return sh->row_count;
00208     }
00209 /*
00210     KDChartData& cell( uint _row, uint _col ) {
00211         detach();
00212         return sh->cell( _row, _col );
00213     }
00214 */
00215     virtual bool cellCoord( uint _row, uint _col,
00216                             QVariant& _value,
00217                             int coordinate=1 ) const
00218     {
00219         if( _row >= sh->row_count || _col >= sh->col_count )
00220             return false;
00221         _value = sh->cell( _row, _col ).value( coordinate );
00222         return true;
00223     }
00224 
00225     virtual bool cellProp( uint _row, uint _col,
00226                            int& _prop ) const
00227     {
00228         if( _row >= sh->row_count || _col >= sh->col_count )
00229             return false;
00230         _prop = sh->cell( _row, _col ).propertySet();
00231         return true;
00232     }
00233 
00234     virtual void setCell( uint _row, uint _col,
00235                           const QVariant& _value1,
00236                           const QVariant& _value2=QVariant() ) {
00237         detach();
00238         const KDChartData element( _value1, _value2 );
00239         sh->setCell( _row, _col, element );
00240     }
00241 
00242     virtual void setProp( uint _row, uint _col,
00243                           int _propSet=0 )
00244     {
00245         sh->cell( _row, _col ).setPropertySet( _propSet );
00246     }
00247     
00248     void clearCell( uint _row, uint _col ) {
00249         detach();
00250         sh->clearCell( _row, _col );
00251     }
00252 
00253     void clearAllCells() {
00254         detach();
00255         sh->clearAllCells();
00256     }
00257 
00258     void expand( uint _rows, uint _cols ) {
00259         detach();
00260         sh->expand( _rows, _cols );
00261         _usedRows = _rows;
00262         _usedCols = _cols;
00263     }
00264 
00265     void setUsedRows( uint _rows ) {
00266         Q_ASSERT( _rows <= rows() );
00267         if( _usedRows < _rows )
00268             setSorted( false );
00269         _usedRows = _rows;
00270         _useUsedRows = true;
00271     }
00272 
00273     uint usedRows() const {
00274         return _useUsedRows ? _usedRows : rows();
00275     }
00276 
00277     void setUsedCols( uint _cols ) {
00278         Q_ASSERT( _cols <= cols() );
00279         if( _usedCols < _cols )
00280             setSorted( false );
00281         _usedCols = _cols;
00282         _useUsedCols = true;
00283     }
00284 
00285     uint usedCols() const {
00286         return _useUsedCols ? _usedCols : cols();
00287     }
00288 
00289 private:
00293     void detach() {
00294         if ( sh->count > 1 ) {
00295             sh->deref();
00296             sh = new Priv( *sh );
00297         }
00298         setSorted( false );
00299     }
00300 
00304     Priv* sh;
00305 };
00306 
00307 #endif
00308 // __KDCHARTLISTTABLE_H__
00309 
KDE Home | KDE Accessibility Home | Description of Access Keys