summaryrefslogtreecommitdiffstats
path: root/doc/html/chart-element-cpp.html
blob: 2e118aaad7ba66047b2771ceb5c82b416a813f47 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/doc/tutorial2.doc:3 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>chart/element.cpp Example File</title>
<style type="text/css"><!--
fn { margin-left: 1cm; text-indent: -1cm; }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
body { background: #ffffff; color: black; }
--></style>
</head>
<body>

<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr bgcolor="#E5E5E5">
<td valign=center>
 <a href="index.html">
<font color="#004faf">Home</font></a>
 | <a href="classes.html">
<font color="#004faf">All&nbsp;Classes</font></a>
 | <a href="mainclasses.html">
<font color="#004faf">Main&nbsp;Classes</font></a>
 | <a href="annotated.html">
<font color="#004faf">Annotated</font></a>
 | <a href="groups.html">
<font color="#004faf">Grouped&nbsp;Classes</font></a>
 | <a href="functions.html">
<font color="#004faf">Functions</font></a>
</td>
<td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>chart/element.cpp Example File</h1>

 
<pre>#include "element.h"

#include &lt;<a href="qstringlist-h.html">qstringlist.h</a>&gt;
#include &lt;<a href="qtextstream-h.html">qtextstream.h</a>&gt;

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


void Element::init( double value, QColor valueColor, int valuePattern,
                    const <a href="qstring.html">QString</a>&amp; label, QColor labelColor )
{
    m_value = value;
    m_valueColor = valueColor;
    if ( valuePattern &lt; Qt::SolidPattern || valuePattern &gt; Qt::DiagCrossPattern )
        valuePattern = Qt::SolidPattern;
    m_valuePattern = valuePattern;
    m_label = label;
    m_labelColor = labelColor;
}


void Element::setValuePattern( int valuePattern )
{
    if ( valuePattern &lt; Qt::SolidPattern || valuePattern &gt; Qt::DiagCrossPattern )
        valuePattern = Qt::SolidPattern;
    m_valuePattern = valuePattern;
}


double Element::proX( int index ) const
{
    <a href="qapplication.html#Q_ASSERT">Q_ASSERT</a>(index &gt;= 0 &amp;&amp; index &lt; MAX_PROPOINTS);
    return m_propoints[2 * index];
}


double Element::proY( int index ) const
{
    <a href="qapplication.html#Q_ASSERT">Q_ASSERT</a>(index &gt;= 0 &amp;&amp; index &lt; MAX_PROPOINTS);
    return m_propoints[(2 * index) + 1];
}


void Element::setProX( int index, double value )
{
    <a href="qapplication.html#Q_ASSERT">Q_ASSERT</a>(index &gt;= 0 &amp;&amp; index &lt; MAX_PROPOINTS);
    m_propoints[2 * index] = value;
}


void Element::setProY( int index, double value )
{
    <a href="qapplication.html#Q_ASSERT">Q_ASSERT</a>(index &gt;= 0 &amp;&amp; index &lt; MAX_PROPOINTS);
    m_propoints[(2 * index) + 1] = value;
}


QTextStream &amp;operator&lt;&lt;( <a href="qtextstream.html">QTextStream</a> &amp;s, const Element &amp;element )
{
    s &lt;&lt; element.value() &lt;&lt; FIELD_SEP
      &lt;&lt; element.valueColor().name() &lt;&lt; FIELD_SEP
      &lt;&lt; element.valuePattern() &lt;&lt; FIELD_SEP
      &lt;&lt; element.labelColor().name() &lt;&lt; FIELD_SEP;

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

    s &lt;&lt; element.label() &lt;&lt; '\n';

    return s;
}


QTextStream &amp;operator&gt;&gt;( <a href="qtextstream.html">QTextStream</a> &amp;s, Element &amp;element )
{
    <a href="qstring.html">QString</a> data = s.<a href="qtextstream.html#readLine">readLine</a>();
    element.setValue( Element::INVALID );

    int errors = 0;
    bool ok;

    <a href="qstringlist.html">QStringList</a> fields = QStringList::<a href="qstringlist.html#split">split</a>( FIELD_SEP, data );
    if ( fields.<a href="qvaluelist.html#count">count</a>() &gt;= 4 ) {
        double value = fields[0].toDouble( &amp;ok );
        if ( !ok )
            errors++;
        <a href="qcolor.html">QColor</a> valueColor = QColor( fields[1] );
        if ( !valueColor.<a href="qcolor.html#isValid">isValid</a>() )
            errors++;
        int valuePattern = fields[2].toInt( &amp;ok );
        if ( !ok )
            errors++;
        <a href="qcolor.html">QColor</a> labelColor = QColor( fields[3] );
        if ( !labelColor.<a href="qcolor.html#isValid">isValid</a>() )
            errors++;
        <a href="qstringlist.html">QStringList</a> propoints = QStringList::<a href="qstringlist.html#split">split</a>( PROPOINT_SEP, fields[4] );
        <a href="qstring.html">QString</a> label = data.<a href="qstring.html#section">section</a>( FIELD_SEP, 5 );

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

    return s;
}

</pre><!-- eof -->
<p><address><hr><div align=center>
<table width=100% cellspacing=0 border=0><tr>
<td>Copyright &copy; 2007
<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
<td align=right><div align=right>Qt 3.3.8</div>
</table></div></address></body>
</html>