<p> Although the <tt>Element</tt> class is a purely internal data class, it
<tt>#include</tt>s four TQt classes. TQt is often perceived as a purely GUI
toolkit, but it provides many non-GUI classes to support most aspects
of application programming. We use <ahref="qcolor-h.html">qcolor.h</a> so that we can hold the
paint color and text color in the <tt>Element</tt> class. The use of <ahref="qnamespace-h.html">qnamespace.h</a> is slightly obscure. Most TQt classes are derived from the
<ahref="qt.html">TQt</a> superclass which contains various
enumerations. The <tt>Element</tt> class does not derive from <ahref="qt.html">TQt</a>, so we need to include <ahref="qnamespace-h.html">qnamespace.h</a> to have access to
the TQt enum names. An alternative approach would have been to have
made <tt>Element</tt> a <ahref="qt.html">TQt</a> subclass. We include <ahref="qstring-h.html">qstring.h</a> to make use of TQt's Unicode strings. As a convenience we
will <tt>typedef</tt> a vector container for <tt>Element</tt>s, which is why we
pull in the <ahref="qvaluevector-h.html">qvaluevector.h</a> header.
<p> TQt provides a number of containers, some value based like
<ahref="qvaluevector.html">TQValueVector</a>, and others pointer based. (See <ahref="collection.html">Collection Classes</a>.) Here we've just typedefed one container
type; we will keep each data set of elements in one <tt>ElementVector</tt>.
<p><pre> const double EPSILON = 0.0000001; // Must be > INVALID.
</pre>
<p> Elements may only have positive values. Because we hold values as
doubles we cannot readily compare them with zero. Instead we specify a
value, <tt>EPSILON</tt>, which is close to zero, and consider any value
greater than <tt>EPSILON</tt> to be positive and valid.
<p><pre> class Element
{
public:
enum { INVALID = -1 };
enum { NO_PROPORTION = -1 };
enum { MAX_PROPOINTS = 3 }; // One proportional point per chart type
</pre>
<p> We define three public enums for <tt>Element</tt>s. <tt>INVALID</tt> is used by
the isValid() function. It is useful because we are going to use a
fixed size vector of <tt>Element</tt>s, and can mark unused <tt>Element</tt>s by
giving them <tt>INVALID</tt> values. The <tt>NO_PROPORTION</tt> enum is used to
signify that the user has not positioned the Element's label; any
positive proportion value is taken to be the text element's position
proportional to the canvas's size.
<p> If we stored each label's actual x and y position, then every time the
user resized the main window (and therefore the canvas), the text
would retain its original (now incorrect) position. So instead of
storing absolute (x, y) positions we store <em>proportional</em> positions,
i.e. x/width and y/height. We can then multiply these positions by
the current width and height respectively when we come to draw the
text and the text will be positioned correctly regardless of any
resizing. For example, if a label has an x position of 300 and the
canvas is 400 pixels wide, the proportional x value is 300/400 = 0.75.
<p> The <tt>MAX_PROPOINTS</tt> enum is problematic. We need to store the x and y
proportions for the text label for every chart type. And we have
chosen to store these proportions in a fixed-size array. Because of
this we must specify the maximum number of proportion pairs needed.
This value must be changed if we change the number of chart types,
which means that the <tt>Element</tt> class is strongly coupled to the
number of chart types provided by the <tt>ChartForm</tt> class. In a
larger application we might have used a vector to store these points
and dynamically resized it depending on how many chart types are
available.
<p><pre> Element( double value = INVALID, TQColor valueColor = TQt::gray,
<p> Our implementation of the operators requires the inclusion of <ahref="qtextstream-h.html">qtextstream.h</a> and <ahref="qstringlist-h.html">qstringlist.h</a>.
<tdvalign="top">For more information on TQt's data streaming facilities see <ahref="datastreamformat.html">TQDataStream Operators' Formats</a>, and see
the source code for any of the TQt classes mentioned that are similar
to what you want to store.
</table></center>
<p><palign="right">
<ahref="tutorial2-02.html">« The 'Big Picture'</a> |