source: vendor/trolltech/current/examples/chart/element.cpp

Last change on this file was 160, checked in by dmik, 19 years ago

Imported table and iconview modules and a bunch of dependent examples from the official release 3.3.1 from Trolltech.

  • Property svn:keywords set to Id
File size: 3.2 KB
Line 
1#include "element.h"
2
3#include <qstringlist.h>
4#include <qtextstream.h>
5
6const char FIELD_SEP = ':';
7const char PROPOINT_SEP = ';';
8const char XY_SEP = ',';
9
10
11void Element::init( double value, QColor valueColor, int valuePattern,
12 const QString& label, QColor labelColor )
13{
14 m_value = value;
15 m_valueColor = valueColor;
16 if ( valuePattern < Qt::SolidPattern || valuePattern > Qt::DiagCrossPattern )
17 valuePattern = Qt::SolidPattern;
18 m_valuePattern = valuePattern;
19 m_label = label;
20 m_labelColor = labelColor;
21}
22
23
24void Element::setValuePattern( int valuePattern )
25{
26 if ( valuePattern < Qt::SolidPattern || valuePattern > Qt::DiagCrossPattern )
27 valuePattern = Qt::SolidPattern;
28 m_valuePattern = valuePattern;
29}
30
31
32double Element::proX( int index ) const
33{
34 Q_ASSERT(index >= 0 && index < MAX_PROPOINTS);
35 return m_propoints[2 * index];
36}
37
38
39double Element::proY( int index ) const
40{
41 Q_ASSERT(index >= 0 && index < MAX_PROPOINTS);
42 return m_propoints[(2 * index) + 1];
43}
44
45
46void Element::setProX( int index, double value )
47{
48 Q_ASSERT(index >= 0 && index < MAX_PROPOINTS);
49 m_propoints[2 * index] = value;
50}
51
52
53void Element::setProY( int index, double value )
54{
55 Q_ASSERT(index >= 0 && index < MAX_PROPOINTS);
56 m_propoints[(2 * index) + 1] = value;
57}
58
59
60QTextStream &operator<<( QTextStream &s, const Element &element )
61{
62 s << element.value() << FIELD_SEP
63 << element.valueColor().name() << FIELD_SEP
64 << element.valuePattern() << FIELD_SEP
65 << element.labelColor().name() << FIELD_SEP;
66
67 for ( int i = 0; i < Element::MAX_PROPOINTS; ++i ) {
68 s << element.proX( i ) << XY_SEP << element.proY( i );
69 s << ( i == Element::MAX_PROPOINTS - 1 ? FIELD_SEP : PROPOINT_SEP );
70 }
71
72 s << element.label() << '\n';
73
74 return s;
75}
76
77
78QTextStream &operator>>( QTextStream &s, Element &element )
79{
80 QString data = s.readLine();
81 element.setValue( Element::INVALID );
82
83 int errors = 0;
84 bool ok;
85
86 QStringList fields = QStringList::split( FIELD_SEP, data );
87 if ( fields.count() >= 4 ) {
88 double value = fields[0].toDouble( &ok );
89 if ( !ok )
90 errors++;
91 QColor valueColor = QColor( fields[1] );
92 if ( !valueColor.isValid() )
93 errors++;
94 int valuePattern = fields[2].toInt( &ok );
95 if ( !ok )
96 errors++;
97 QColor labelColor = QColor( fields[3] );
98 if ( !labelColor.isValid() )
99 errors++;
100 QStringList propoints = QStringList::split( PROPOINT_SEP, fields[4] );
101 QString label = data.section( FIELD_SEP, 5 );
102
103 if ( !errors ) {
104 element.set( value, valueColor, valuePattern, label, labelColor );
105 int i = 0;
106 for ( QStringList::iterator point = propoints.begin();
107 i < Element::MAX_PROPOINTS && point != propoints.end();
108 ++i, ++point ) {
109 errors = 0;
110 QStringList xy = QStringList::split( XY_SEP, *point );
111 double x = xy[0].toDouble( &ok );
112 if ( !ok || x <= 0.0 || x >= 1.0 )
113 errors++;
114 double y = xy[1].toDouble( &ok );
115 if ( !ok || y <= 0.0 || y >= 1.0 )
116 errors++;
117 if ( errors )
118 x = y = Element::NO_PROPORTION;
119 element.setProX( i, x );
120 element.setProY( i, y );
121 }
122 }
123 }
124
125 return s;
126}
127
Note: See TracBrowser for help on using the repository browser.