summaryrefslogtreecommitdiffstats
path: root/examples/chart/element.cpp
blob: 570fccce8075a70fe8fd5d49c5877be17de56b3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "element.h"

#include <qstringlist.h>
#include <qtextstream.h>

const char FIELD_SEP = ':';
const char PROPOINT_SEP = ';';
const char XY_SEP = ',';


void Element::init( double value, TQColor valueColor, int valuePattern,
		    const TQString& label, TQColor labelColor )
{
    m_value = value;
    m_valueColor = valueColor;
    if ( valuePattern < TQt::SolidPattern || valuePattern > TQt::DiagCrossPattern )
	valuePattern = TQt::SolidPattern;
    m_valuePattern = valuePattern;
    m_label = label;
    m_labelColor = labelColor;
}


void Element::setValuePattern( int valuePattern )
{
    if ( valuePattern < TQt::SolidPattern || valuePattern > TQt::DiagCrossPattern )
	valuePattern = TQt::SolidPattern;
    m_valuePattern = valuePattern;
}


double Element::proX( int index ) const
{
    Q_ASSERT(index >= 0 && index < MAX_PROPOINTS);
    return m_propoints[2 * index];
}


double Element::proY( int index ) const
{
    Q_ASSERT(index >= 0 && index < MAX_PROPOINTS);
    return m_propoints[(2 * index) + 1];
}


void Element::setProX( int index, double value )
{
    Q_ASSERT(index >= 0 && index < MAX_PROPOINTS);
    m_propoints[2 * index] = value;
}


void Element::setProY( int index, double value )
{
    Q_ASSERT(index >= 0 && index < MAX_PROPOINTS);
    m_propoints[(2 * index) + 1] = value;
}


TQTextStream &operator<<( TQTextStream &s, const Element &element )
{
    s << element.value() << FIELD_SEP
      << element.valueColor().name() << FIELD_SEP
      << element.valuePattern() << FIELD_SEP
      << element.labelColor().name() << FIELD_SEP;

    for ( int i = 0; i < Element::MAX_PROPOINTS; ++i ) {
	s << element.proX( i ) << XY_SEP << element.proY( i );
	s << ( i == Element::MAX_PROPOINTS - 1 ? FIELD_SEP : PROPOINT_SEP );
    }

    s << element.label() << '\n';

    return s;
}


TQTextStream &operator>>( TQTextStream &s, Element &element )
{
    TQString data = s.readLine();
    element.setValue( Element::INVALID );

    int errors = 0;
    bool ok;

    TQStringList fields = TQStringList::split( FIELD_SEP, data );
    if ( fields.count() >= 4 ) {
	double value = fields[0].toDouble( &ok );
	if ( !ok )
	    errors++;
	TQColor valueColor = TQColor( fields[1] );
	if ( !valueColor.isValid() )
	    errors++;
	int valuePattern = fields[2].toInt( &ok );
	if ( !ok )
	    errors++;
	TQColor labelColor = TQColor( fields[3] );
	if ( !labelColor.isValid() )
	    errors++;
	TQStringList propoints = TQStringList::split( PROPOINT_SEP, fields[4] );
	TQString label = data.section( FIELD_SEP, 5 );

	if ( !errors ) {
	    element.set( value, valueColor, valuePattern, label, labelColor );
	    int i = 0;
	    for ( TQStringList::iterator point = propoints.begin();
		i < Element::MAX_PROPOINTS && point != propoints.end();
		++i, ++point ) {
		errors = 0;
		TQStringList xy = TQStringList::split( XY_SEP, *point );
		double x = xy[0].toDouble( &ok );
		if ( !ok || x <= 0.0 || x >= 1.0 )
		    errors++;
		double y = xy[1].toDouble( &ok );
		if ( !ok || y <= 0.0 || y >= 1.0 )
		    errors++;
		if ( errors )
		    x = y = Element::NO_PROPORTION;
		element.setProX( i, x );
		element.setProY( i, y );
	    }
	}
    }

    return s;
}