kchart

KDChartListTable.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 __KDCHARTLISTTABLE_H__
30#define __KDCHARTLISTTABLE_H__
31
32#include <tqvaluelist.h>
33#include <tqshared.h>
34#include <tqtable.h>
35
36#include <KDChartDataIntern.h>
37#include <KDChartTableBase.h>
38
39class KDCHART_EXPORT KDChartListTablePrivate : public TQShared
40{
41 public:
42 KDChartListTablePrivate() : TQShared() {
43 row_count = 0;
44 col_count = 0;
45 }
46
47 KDChartListTablePrivate( uint _rows, uint _cols ) : TQShared() {
48 for ( uint i = 0; i < _rows; i++ )
49 row_list.append( int() );
50 for ( uint j = 0; j < _cols; j++ )
51 col_list.append( int() );
52 for ( uint k = 0; k < _rows * _cols; k++ )
53 matrix.append( KDChartData() );
54 col_count = _cols;
55 row_count = _rows;
56 }
57
58 KDChartListTablePrivate( const KDChartListTablePrivate& _t ) :
59 TQShared(),
60 matrix( _t.matrix ),
61 row_list( _t.row_list ),
62 col_list( _t.col_list ),
63 col_count( _t.col_count ),
64 row_count( _t.row_count ) {}
65 ~KDChartListTablePrivate() {}
66
67 void expand( uint _rows, uint _cols ) {
68 if ( _rows > row_count ) {
69 for ( uint r = 0; r < _rows - row_count; ++r ) {
70 row_list.append( int() );
71 for ( uint i = 0; i < col_count; ++i )
72 matrix.append( KDChartData() );
73 }
74 row_count = _rows;
75 }
76 if ( _cols > col_count ) {
77 uint old = col_count;
78 col_count = _cols;
79 for ( uint c = 0; c < _cols - old; ++c ) {
80 col_list.append( int() );
81 for ( uint i = 0; i < row_count; ++i )
82 matrix.insert( matrix.at( i * col_count + old + c ), KDChartData() );
83 }
84 }
85 }
86
87 const KDChartData& cell( uint _row, uint _col ) const
88 {
89 Q_ASSERT( _row < row_count && _col < col_count );
90 return matrix[ static_cast < int > ( _row * col_count + _col ) ];
91 }
92 KDChartData& cell( uint _row, uint _col )
93 {
94 Q_ASSERT( _row < row_count && _col < col_count );
95 return matrix[ static_cast < int > ( _row * col_count + _col ) ];
96 }
97
98 void setCell( uint _row, uint _col, const KDChartData& _element )
99 {
100 Q_ASSERT( _row < row_count && _col < col_count );
101 matrix[ static_cast < int > ( _row * col_count + _col ) ].setAll( _element );
102 }
103
104 void clearCell( uint _row, uint _col ) {
105 Q_ASSERT( _row < row_count && _col < col_count );
106 matrix[ static_cast < int > ( _row * col_count + _col ) ].clearValue();
107 }
108
109 void clearAllCells() {
110 for ( uint r = 0; r < row_count; ++r )
111 for ( uint c = 0; c < col_count; ++c )
112 matrix[ static_cast < int > ( r * col_count + c ) ].clearValue();
113 }
114
115 int& row( uint _row ) {
116 Q_ASSERT( _row < row_count );
117 return row_list[ _row ];
118 }
119
120 const int& row( uint _row ) const {
121 Q_ASSERT( _row < row_count );
122 return row_list[ _row ];
123 }
124
125 void setRow( uint _row, const int& _v ) {
126 Q_ASSERT( _row < row_count );
127 row_list[ _row ] = _v;
128 }
129
130 int& col( uint _col ) {
131 Q_ASSERT( _col < col_count );
132 return col_list[ _col ];
133 }
134
135
136 const int& col( uint _col ) const {
137 Q_ASSERT( _col < col_count );
138 return col_list[ _col ];
139 }
140
141 void setCol( uint _col, const int& _v ) {
142 Q_ASSERT( _col < col_count );
143 col_list[ _col ] = _v;
144 }
145
146 void insertColumn( uint _c ) {
147 Q_ASSERT( _c <= col_count );
148 ++col_count;
149 TQValueList < KDChartData > ::Iterator it;
150 for ( uint i = 0; i < row_count; ++i ) {
151 it = matrix.at( i * col_count + _c );
152 matrix.insert( it, KDChartData() );
153 }
154
155 TQValueList < int > ::Iterator it2 = col_list.at( _c );
156 col_list.insert( it2, int() );
157 }
158
159 void insertRow( uint _r ) {
160 Q_ASSERT( _r <= row_count );
161 ++row_count;
162 TQValueList < KDChartData > ::Iterator it = matrix.at( _r * col_count );
163 for ( uint i = 0; i < col_count; ++i )
164 matrix.insert( it, KDChartData() );
165
166 TQValueList < int > ::Iterator it2 = row_list.at( _r );
167 row_list.insert( it2, int() );
168 }
169
170 void removeColumn( uint _c ) {
171 Q_ASSERT( _c < col_count );
172 --col_count;
173 TQValueList < KDChartData > ::Iterator it;
174 for ( uint i = 0; i < row_count; ++i ) {
175 it = matrix.at( i * col_count + _c );
176 matrix.remove( it );
177 }
178
179 TQValueList < int > ::Iterator it2 = col_list.at( _c );
180 col_list.remove( it2 );
181 }
182
183 void removeRow( uint _r ) {
184 Q_ASSERT( _r < row_count );
185 --row_count;
186 TQValueList < KDChartData > ::Iterator it = matrix.at( _r * col_count );
187 for ( uint i = 0; i < col_count; ++i )
188 it = matrix.remove( it );
189
190 TQValueList < int > ::Iterator it2 = row_list.at( _r );
191 row_list.remove( it2 );
192 }
193
194 TQValueList < KDChartData > matrix;
195 TQValueList < int > row_list;
196 TQValueList < int > col_list;
197
198 uint col_count;
199 uint row_count;
200};
201
202
203class KDChartListTableData : public KDChartTableDataBase
204{
205 private:
206 typedef KDChartListTablePrivate Priv;
207 uint _usedRows, _usedCols;
208
209 public:
213 typedef TQValueList < KDChartData > ::Iterator Iterator;
214 typedef TQValueList < KDChartData > ::ConstIterator ConstIterator;
215
216 typedef TQValueList < int > ::Iterator RowIterator;
217 typedef TQValueList < int > ::ConstIterator ConstRowIterator;
218
219 typedef TQValueList < int > ::Iterator ColIterator;
220 typedef TQValueList < int > ::ConstIterator ConstColIterator;
221
225 KDChartListTableData() :
227 {
228 sh = new Priv;
229 _usedCols = 0;
230 _usedRows = 0;
231 }
232 KDChartListTableData( uint _rows, uint _cols ) :
234 {
235 sh = new Priv( _rows, _cols );
236 _usedRows = _rows;
237 _usedCols = _cols;
238 }
239
240 KDChartListTableData( const KDChartListTableData& _t ) :
242 _useUsedRows = _t._useUsedRows;
243 _useUsedCols = _t._useUsedCols;
244 _usedRows = _t._usedRows;
245 _usedCols = _t._usedCols;
246 sh = _t.sh;
247 sh->ref();
248 setSorted( _t.sorted() );
249 }
250
251 virtual ~KDChartListTableData() {
252 if ( sh->deref() )
253 delete sh;
254 }
255
256 KDChartListTableData& operator=( const KDChartListTableData& t ) {
257 if ( &t == this )
258 return * this;
259 _useUsedRows = t._useUsedRows;
260 _useUsedCols = t._useUsedCols;
261 _usedRows = t._usedRows;
262 _usedCols = t._usedCols;
263 t.sh->ref();
264 if ( sh->deref() )
265 delete sh;
266 sh = t.sh;
267 setSorted( t.sorted() );
268 return *this;
269 }
270
271 Iterator begin() {
272 return sh->matrix.begin();
273 }
274
275 ConstIterator begin() const {
276 return sh->matrix.begin();
277 }
278
279 Iterator end() {
280 return sh->matrix.end();
281 }
282
283 ConstIterator end() const {
284 return sh->matrix.end();
285 }
286
287 ColIterator colBegin() {
288 return sh->col_list.begin();
289 }
290
291 ConstColIterator colBegin() const {
292 return sh->col_list.begin();
293 }
294
295 ColIterator colEnd() {
296 return sh->col_list.end();
297 }
298
299 ConstColIterator colEnd() const
300 {
301 return sh->col_list.end();
302 }
303
304 RowIterator rowBegin() {
305 return sh->row_list.begin();
306 }
307
308 ConstRowIterator rowBegin() const {
309 return sh->row_list.begin();
310 }
311
312 RowIterator rowEnd() {
313 return sh->row_list.end();
314 }
315
316 ConstRowIterator rowEnd() const {
317 return sh->row_list.end();
318 }
319
320 bool isEmpty() const {
321 return ( sh->col_count == 0 && sh->row_count == 0 );
322 }
323
324 uint cols() const {
325 return sh->col_count;
326 }
327
328 uint rows() const {
329 return sh->row_count;
330 }
331
332 virtual bool cellCoord( uint _row, uint _col,
333 TQVariant& _value,
334 int coordinate=1 ) const
335 {
336 if( _row >= sh->row_count || _col >= sh->col_count )
337 return false;
338 _value = sh->cell( _row, _col ).value( coordinate );
339 return true;
340 }
341
342 virtual bool cellProp( uint _row, uint _col,
343 int& _prop ) const
344 {
345 if( _row >= sh->row_count || _col >= sh->col_count )
346 return false;
347 _prop = sh->cell( _row, _col ).propertySet();
348 return true;
349 }
350
351 virtual void setCell( uint _row, uint _col,
352 const TQVariant& _value1,
353 const TQVariant& _value2=TQVariant() )
354 {
355 detach();
356 const KDChartData element( _value1, _value2 );
357 sh->setCell( _row, _col, element );
358 }
359
360 virtual void setProp( uint _row, uint _col,
361 int _propSet=0 )
362 {
363 sh->cell( _row, _col ).setPropertySet( _propSet );
364 }
365
366 void clearCell( uint _row, uint _col ) {
367 detach();
368 sh->clearCell( _row, _col );
369 }
370
371 void clearAllCells() {
372 detach();
373 sh->clearAllCells();
374 }
375
376 int& row( uint _row ) {
377 detach();
378 return sh->row( _row );
379 }
380
381 const int& row( uint _row ) const {
382 return sh->row( _row );
383 }
384
385 void setRow( uint _row, const int& _v ) {
386 detach();
387 sh->setRow( _row, _v );
388 }
389
390 int& col( uint _col ) {
391 detach();
392 return sh->col( _col );
393 }
394
395 const int& col( uint _col ) const {
396 return sh->col( _col );
397 }
398
399 void setCol( uint _col, const int& _v ) {
400 detach();
401 sh->setCol( _col, _v );
402 }
403
404 void insertColumn( uint _c ) {
405 detach();
406 sh->insertColumn( _c );
407 ++_usedCols;
408 }
409
410 void insertRow( uint _r ) {
411 detach();
412 sh->insertRow( _r );
413 ++_usedRows;
414 }
415
416 void removeColumn( uint _c ) {
417 detach();
418 sh->removeColumn( _c );
419 if( _usedCols )
420 --_usedCols;
421 }
422
423 void removeRow( uint _r ) {
424 detach();
425 sh->removeRow( _r );
426 if( _usedRows )
427 --_usedRows;
428 }
429
430 void expand( uint _rows, uint _cols ) {
431 detach();
432 sh->expand( _rows, _cols );
433 // adjust the usedRows / usedCols, if they had been set before
434 if( _useUsedCols )
435 setUsedCols( TQMIN( _usedCols, _cols ) );
436 if( _useUsedRows )
437 setUsedRows( TQMIN( _usedRows, _rows ) );
438 }
439
440 void setUsedRows( uint _rows ) {
441 Q_ASSERT( _rows <= rows() );
442 if( _usedRows < _rows )
443 setSorted( false );
444 _usedRows = _rows;
445 _useUsedRows = true;
446 }
447
448 uint usedRows() const {
449 return _useUsedRows ? _usedRows : rows();
450 }
451
452 void setUsedCols( uint _cols ) {
453 Q_ASSERT( _cols <= cols() );
454 if( _usedCols < _cols )
455 setSorted( false );
456 _usedCols = _cols;
457 _useUsedCols = true;
458 }
459
460 uint usedCols() const {
461 return _useUsedCols ? _usedCols : cols();
462 }
463
464 private:
468 void detach() {
469 if ( sh->count > 1 ) {
470 sh->deref();
471 sh = new Priv( *sh );
472 }
473 setSorted( false );
474 }
475
479 Priv* sh;
480};
481
482#endif
483// __KDCHARTLISTTABLE_H__
484
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.
virtual void setSorted(bool sorted)
Specified whether the table is sorted.
virtual uint rows() const =0
Returns the number of rows in the table.