kchart

KDChartVectorTable.h
1/*
2 KDChart - a multi-platform charting engine
3*/
4
5/****************************************************************************
6 ** Copyright (C) 2001-2003 Klarälvdalens Datakonsult AB. All rights reserved.
7 **
8 ** This file is part of the KDChart library.
9 **
10 ** This file may be distributed and/or modified under the terms of the
11 ** GNU General Public License version 2 as published by the Free Software
12 ** Foundation and appearing in the file LICENSE.GPL included in the
13 ** packaging of this file.
14 **
15 ** Licensees holding valid commercial KDChart licenses may use this file in
16 ** accordance with the KDChart Commercial License Agreement provided with
17 ** the Software.
18 **
19 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
20 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 **
22 ** See http://www.klaralvdalens-datakonsult.se/?page=products for
23 ** information about KDChart Commercial License Agreements.
24 **
25 ** Contact info@klaralvdalens-datakonsult.se if any conditions of this
26 ** licensing are not clear to you.
27 **
28 **********************************************************************/
29#ifndef __KDCHARTVECTORTABLE_H__
30#define __KDCHARTVECTORTABLE_H__
31
32#include <tqvaluevector.h>
33#include <tqshared.h>
34#include <tqtable.h>
35
36#include <KDChartDataIntern.h>
37#include <KDChartTableBase.h>
38
39class KDCHART_EXPORT KDChartVectorTablePrivate : public TQShared
40{
41public:
42 KDChartVectorTablePrivate() : TQShared() {
43 row_count = 0;
44 col_count = 0;
45 }
46
47 KDChartVectorTablePrivate( uint _rows, uint _cols ) : TQShared() {
48 matrix.resize( _rows * _cols, KDChartData() );
49 col_count = _cols;
50 row_count = _rows;
51 }
52
53 KDChartVectorTablePrivate( const KDChartVectorTablePrivate& _t ) :
54 TQShared(),
55 matrix( _t.matrix ),
56 col_count( _t.col_count ),
57 row_count( _t.row_count ) {}
58
59 ~KDChartVectorTablePrivate() {}
60
61 void expand( uint _rows, uint _cols ) {
62 // Save the old table
63 TQValueVector<KDChartData> save( matrix );
64
65 // Delete old data, then resize
66 matrix.resize( 0 );
67 matrix.resize( _rows * _cols, KDChartData() );
68
69 // Copy over the old data
70 for( uint row = 0; row < TQMIN( row_count, _rows ); row++ )
71 for( uint col = 0; col < TQMIN( col_count, _cols ); col++ )
72 matrix[ row * _cols + col ].setAll( save[ row * col_count + col ] );
73
74 // set the new counts
75 col_count = _cols;
76 row_count = _rows;
77 }
78
79 KDChartData& cell( uint _row, uint _col ) {
80 Q_ASSERT( _row < row_count );
81 Q_ASSERT( _col < col_count );
82 return matrix[ static_cast < int > ( _row * col_count + _col ) ];
83 }
84 const KDChartData& cell( uint _row, uint _col ) const {
85 Q_ASSERT( _row < row_count );
86 Q_ASSERT( _col < col_count );
87 return matrix[ static_cast < int > ( _row * col_count + _col ) ];
88 }
89 void setCell( uint _row, uint _col, const KDChartData& _element ) {
90 Q_ASSERT( _row < row_count );
91 Q_ASSERT( _col < col_count );
92 matrix[ static_cast < int > ( _row * col_count + _col ) ].setAll( _element );
93 }
94
95 void clearCell( uint _row, uint _col ) {
96 Q_ASSERT( _row < row_count );
97 Q_ASSERT( _col < col_count );
98 matrix[ static_cast < int > ( _row * col_count + _col ) ].clearValue();
99 }
100
101 void clearAllCells() {
102 for ( uint r = 0; r < row_count; ++r )
103 for ( uint c = 0; c < col_count; ++c )
104 matrix[ r * col_count + c ].clearValue();
105 }
106
107 TQValueVector<KDChartData> matrix;
108
109 uint col_count;
110 uint row_count;
111};
112
113
114class KDCHART_EXPORT KDChartVectorTableData : public KDChartTableDataBase
115{
116 TQ_OBJECT
117
118
119private:
120 typedef KDChartVectorTablePrivate Priv;
121 uint _usedRows, _usedCols;
122
123public:
127 typedef TQValueVector<KDChartData>::iterator Iterator;
128 typedef TQValueVector<KDChartData>::const_iterator ConstIterator;
129
130 typedef TQValueVector<int>::iterator RowIterator;
131 typedef TQValueVector<int>::const_iterator ConstRowIterator;
132
133 typedef TQValueVector<int>::iterator ColIterator;
134 typedef TQValueVector<int>::const_iterator ConstColIterator;
135
139 KDChartVectorTableData() :
141 {
142 sh = new Priv;
143 _usedCols = 0;
144 _usedRows = 0;
145 }
146 KDChartVectorTableData( uint _rows, uint _cols ) :
148 {
149 sh = new Priv( _rows, _cols );
150 _usedRows = _rows;
151 _usedCols = _cols;
152 }
153
154 KDChartVectorTableData( const KDChartVectorTableData& _t ) :
156 _useUsedRows = _t._useUsedRows;
157 _useUsedCols = _t._useUsedCols;
158 _usedRows = _t._usedRows;
159 _usedCols = _t._usedCols;
160 sh = _t.sh;
161 sh->ref();
162 setSorted( _t.sorted() );
163 }
164
165 virtual ~KDChartVectorTableData() {
166 if ( sh->deref() )
167 delete sh;
168 }
169
170 KDChartVectorTableData& operator=( const KDChartVectorTableData& t ) {
171 if ( &t == this )
172 return * this;
173 _useUsedRows = t._useUsedRows;
174 _useUsedCols = t._useUsedCols;
175 _usedRows = t._usedRows;
176 _usedCols = t._usedCols;
177 t.sh->ref();
178 if ( sh->deref() )
179 delete sh;
180 sh = t.sh;
181 setSorted( t.sorted() );
182 return *this;
183 }
184
185public slots:
186 Iterator begin() {
187 return sh->matrix.begin();
188 }
189
190 ConstIterator begin() const {
191 return sh->matrix.begin();
192 }
193
194 Iterator end() {
195 return sh->matrix.end();
196 }
197
198 ConstIterator end() const {
199 return sh->matrix.end();
200 }
201
202 bool isEmpty() const {
203 return ( sh->col_count == 0 && sh->row_count == 0 );
204 }
205
206 uint cols() const {
207 return sh->col_count;
208 }
209
210 uint rows() const {
211 return sh->row_count;
212 }
213 // WARNING: The KDChartData class is an internal class now,
214 // and nobody supposed to use it any longer.
215 // Instead, please use cellCoord(), cellProp(), setCell(), setProp(), ...
216 // (khz, 2006-05-23)
217/*
218 KDChartData& cell( uint _row, uint _col ) {
219 detach();
220 return sh->cell( _row, _col );
221 }
222*/
223 virtual bool cellCoord( uint _row, uint _col,
224 TQVariant& _value,
225 int coordinate=1 ) const
226 {
227 if( _row >= sh->row_count || _col >= sh->col_count )
228 return false;
229 _value = sh->cell( _row, _col ).value( coordinate );
230 return true;
231 }
232
233 virtual bool cellProp( uint _row, uint _col,
234 int& _prop ) const
235 {
236 if( _row >= sh->row_count || _col >= sh->col_count )
237 return false;
238 _prop = sh->cell( _row, _col ).propertySet();
239 return true;
240 }
241
242 virtual void setCell( uint _row, uint _col,
243 const TQVariant& _value1,
244 const TQVariant& _value2=TQVariant() ) {
245 detach();
246 const KDChartData element( _value1, _value2 );
247 sh->setCell( _row, _col, element );
248 }
249
250 virtual void setProp( uint _row, uint _col,
251 int _propSet=0 )
252 {
253 sh->cell( _row, _col ).setPropertySet( _propSet );
254 }
255
256 void clearCell( uint _row, uint _col ) {
257 detach();
258 sh->clearCell( _row, _col );
259 }
260
261 void clearAllCells() {
262 detach();
263 sh->clearAllCells();
264 }
265
266 void expand( uint _rows, uint _cols ) {
267 detach();
268 sh->expand( _rows, _cols );
269 // adjust the usedRows / usedCols, if they had been set before
270 if( _useUsedCols )
271 setUsedCols( TQMIN( _usedCols, _cols ) );
272 if( _useUsedRows )
273 setUsedRows( TQMIN( _usedRows, _rows ) );
274 }
275
276 void setUsedRows( uint _rows ) {
277 Q_ASSERT( _rows <= rows() );
278 if( _usedRows < _rows )
279 setSorted( false );
280 _usedRows = _rows;
281 _useUsedRows = true;
282 }
283
284 uint usedRows() const {
285 return _useUsedRows ? _usedRows : rows();
286 }
287
288 void setUsedCols( uint _cols ) {
289 Q_ASSERT( _cols <= cols() );
290 if( _usedCols < _cols )
291 setSorted( false );
292 _usedCols = _cols;
293 _useUsedCols = true;
294 }
295
296 uint usedCols() const {
297 return _useUsedCols ? _usedCols : cols();
298 }
299
300private:
304 void detach() {
305 if ( sh->count > 1 ) {
306 sh->deref();
307 sh = new Priv( *sh );
308 }
309 setSorted( false );
310 }
311
315 Priv* sh;
316};
317
318#endif
319// __KDCHARTLISTTABLE_H__
320
Provides a table class holding all data values that are to be used in a chart.
Encapsulates one data cell in a chart.
KD Chart's build-in table data for an easy way of storing data values.