1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
---|
2 | <!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/doc/tutorial2.doc:3 -->
|
---|
3 | <html>
|
---|
4 | <head>
|
---|
5 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
---|
6 | <title>chart/element.cpp Example File</title>
|
---|
7 | <style type="text/css"><!--
|
---|
8 | fn { margin-left: 1cm; text-indent: -1cm; }
|
---|
9 | a:link { color: #004faf; text-decoration: none }
|
---|
10 | a:visited { color: #672967; text-decoration: none }
|
---|
11 | body { background: #ffffff; color: black; }
|
---|
12 | --></style>
|
---|
13 | </head>
|
---|
14 | <body>
|
---|
15 |
|
---|
16 | <table border="0" cellpadding="0" cellspacing="0" width="100%">
|
---|
17 | <tr bgcolor="#E5E5E5">
|
---|
18 | <td valign=center>
|
---|
19 | <a href="index.html">
|
---|
20 | <font color="#004faf">Home</font></a>
|
---|
21 | | <a href="classes.html">
|
---|
22 | <font color="#004faf">All Classes</font></a>
|
---|
23 | | <a href="mainclasses.html">
|
---|
24 | <font color="#004faf">Main Classes</font></a>
|
---|
25 | | <a href="annotated.html">
|
---|
26 | <font color="#004faf">Annotated</font></a>
|
---|
27 | | <a href="groups.html">
|
---|
28 | <font color="#004faf">Grouped Classes</font></a>
|
---|
29 | | <a href="functions.html">
|
---|
30 | <font color="#004faf">Functions</font></a>
|
---|
31 | </td>
|
---|
32 | <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>
|
---|
33 |
|
---|
34 |
|
---|
35 | <pre>#include "element.h"
|
---|
36 |
|
---|
37 | #include <<a href="qstringlist-h.html">qstringlist.h</a>>
|
---|
38 | #include <<a href="qtextstream-h.html">qtextstream.h</a>>
|
---|
39 |
|
---|
40 | const char FIELD_SEP = ':';
|
---|
41 | const char PROPOINT_SEP = ';';
|
---|
42 | const char XY_SEP = ',';
|
---|
43 |
|
---|
44 |
|
---|
45 | void Element::init( double value, QColor valueColor, int valuePattern,
|
---|
46 | const <a href="qstring.html">QString</a>& label, QColor labelColor )
|
---|
47 | {
|
---|
48 | m_value = value;
|
---|
49 | m_valueColor = valueColor;
|
---|
50 | if ( valuePattern < Qt::SolidPattern || valuePattern > Qt::DiagCrossPattern )
|
---|
51 | valuePattern = Qt::SolidPattern;
|
---|
52 | m_valuePattern = valuePattern;
|
---|
53 | m_label = label;
|
---|
54 | m_labelColor = labelColor;
|
---|
55 | }
|
---|
56 |
|
---|
57 |
|
---|
58 | void Element::setValuePattern( int valuePattern )
|
---|
59 | {
|
---|
60 | if ( valuePattern < Qt::SolidPattern || valuePattern > Qt::DiagCrossPattern )
|
---|
61 | valuePattern = Qt::SolidPattern;
|
---|
62 | m_valuePattern = valuePattern;
|
---|
63 | }
|
---|
64 |
|
---|
65 |
|
---|
66 | double Element::proX( int index ) const
|
---|
67 | {
|
---|
68 | <a href="qapplication.html#Q_ASSERT">Q_ASSERT</a>(index >= 0 && index < MAX_PROPOINTS);
|
---|
69 | return m_propoints[2 * index];
|
---|
70 | }
|
---|
71 |
|
---|
72 |
|
---|
73 | double Element::proY( int index ) const
|
---|
74 | {
|
---|
75 | <a href="qapplication.html#Q_ASSERT">Q_ASSERT</a>(index >= 0 && index < MAX_PROPOINTS);
|
---|
76 | return m_propoints[(2 * index) + 1];
|
---|
77 | }
|
---|
78 |
|
---|
79 |
|
---|
80 | void Element::setProX( int index, double value )
|
---|
81 | {
|
---|
82 | <a href="qapplication.html#Q_ASSERT">Q_ASSERT</a>(index >= 0 && index < MAX_PROPOINTS);
|
---|
83 | m_propoints[2 * index] = value;
|
---|
84 | }
|
---|
85 |
|
---|
86 |
|
---|
87 | void Element::setProY( int index, double value )
|
---|
88 | {
|
---|
89 | <a href="qapplication.html#Q_ASSERT">Q_ASSERT</a>(index >= 0 && index < MAX_PROPOINTS);
|
---|
90 | m_propoints[(2 * index) + 1] = value;
|
---|
91 | }
|
---|
92 |
|
---|
93 |
|
---|
94 | QTextStream &operator<<( <a href="qtextstream.html">QTextStream</a> &s, const Element &element )
|
---|
95 | {
|
---|
96 | s << element.value() << FIELD_SEP
|
---|
97 | << element.valueColor().name() << FIELD_SEP
|
---|
98 | << element.valuePattern() << FIELD_SEP
|
---|
99 | << element.labelColor().name() << FIELD_SEP;
|
---|
100 |
|
---|
101 | for ( int i = 0; i < Element::MAX_PROPOINTS; ++i ) {
|
---|
102 | s << element.proX( i ) << XY_SEP << element.proY( i );
|
---|
103 | s << ( i == Element::MAX_PROPOINTS - 1 ? FIELD_SEP : PROPOINT_SEP );
|
---|
104 | }
|
---|
105 |
|
---|
106 | s << element.label() << '\n';
|
---|
107 |
|
---|
108 | return s;
|
---|
109 | }
|
---|
110 |
|
---|
111 |
|
---|
112 | QTextStream &operator>>( <a href="qtextstream.html">QTextStream</a> &s, Element &element )
|
---|
113 | {
|
---|
114 | <a href="qstring.html">QString</a> data = s.<a href="qtextstream.html#readLine">readLine</a>();
|
---|
115 | element.setValue( Element::INVALID );
|
---|
116 |
|
---|
117 | int errors = 0;
|
---|
118 | bool ok;
|
---|
119 |
|
---|
120 | <a href="qstringlist.html">QStringList</a> fields = QStringList::<a href="qstringlist.html#split">split</a>( FIELD_SEP, data );
|
---|
121 | if ( fields.<a href="qvaluelist.html#count">count</a>() >= 4 ) {
|
---|
122 | double value = fields[0].toDouble( &ok );
|
---|
123 | if ( !ok )
|
---|
124 | errors++;
|
---|
125 | <a href="qcolor.html">QColor</a> valueColor = QColor( fields[1] );
|
---|
126 | if ( !valueColor.<a href="qcolor.html#isValid">isValid</a>() )
|
---|
127 | errors++;
|
---|
128 | int valuePattern = fields[2].toInt( &ok );
|
---|
129 | if ( !ok )
|
---|
130 | errors++;
|
---|
131 | <a href="qcolor.html">QColor</a> labelColor = QColor( fields[3] );
|
---|
132 | if ( !labelColor.<a href="qcolor.html#isValid">isValid</a>() )
|
---|
133 | errors++;
|
---|
134 | <a href="qstringlist.html">QStringList</a> propoints = QStringList::<a href="qstringlist.html#split">split</a>( PROPOINT_SEP, fields[4] );
|
---|
135 | <a href="qstring.html">QString</a> label = data.<a href="qstring.html#section">section</a>( FIELD_SEP, 5 );
|
---|
136 |
|
---|
137 | if ( !errors ) {
|
---|
138 | element.set( value, valueColor, valuePattern, label, labelColor );
|
---|
139 | int i = 0;
|
---|
140 | for ( QStringList::iterator point = propoints.<a href="qvaluelist.html#begin">begin</a>();
|
---|
141 | i < Element::MAX_PROPOINTS && point != propoints.<a href="qvaluelist.html#end">end</a>();
|
---|
142 | ++i, ++point ) {
|
---|
143 | errors = 0;
|
---|
144 | <a href="qstringlist.html">QStringList</a> xy = QStringList::<a href="qstringlist.html#split">split</a>( XY_SEP, *point );
|
---|
145 | double x = xy[0].toDouble( &ok );
|
---|
146 | if ( !ok || x <= 0.0 || x >= 1.0 )
|
---|
147 | errors++;
|
---|
148 | double y = xy[1].toDouble( &ok );
|
---|
149 | if ( !ok || y <= 0.0 || y >= 1.0 )
|
---|
150 | errors++;
|
---|
151 | if ( errors )
|
---|
152 | x = y = Element::NO_PROPORTION;
|
---|
153 | element.setProX( i, x );
|
---|
154 | element.setProY( i, y );
|
---|
155 | }
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 | return s;
|
---|
160 | }
|
---|
161 |
|
---|
162 | </pre><!-- eof -->
|
---|
163 | <p><address><hr><div align=center>
|
---|
164 | <table width=100% cellspacing=0 border=0><tr>
|
---|
165 | <td>Copyright © 2007
|
---|
166 | <a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
|
---|
167 | <td align=right><div align=right>Qt 3.3.8</div>
|
---|
168 | </table></div></address></body>
|
---|
169 | </html>
|
---|