source: trunk/doc/html/canvas-chart-example.html@ 203

Last change on this file since 203 was 190, checked in by rudi, 14 years ago

reference documentation added

File size: 34.4 KB
Line 
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/examples/chart/chart.doc:1 -->
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>A Complete Canvas Application</title>
7<style type="text/css"><!--
8fn { margin-left: 1cm; text-indent: -1cm; }
9a:link { color: #004faf; text-decoration: none }
10a:visited { color: #672967; text-decoration: none }
11body { 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&nbsp;Classes</font></a>
23 | <a href="mainclasses.html">
24<font color="#004faf">Main&nbsp;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&nbsp;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>A Complete Canvas Application</h1>
33
34
35
36<p>
37<p> This is a complete example program with a main window, menus and
38toolbars. The main widget is a <a href="qcanvas.html">QCanvas</a>, and this example
39demonstrates basic canvas usage.
40
41<p> <hr>
42<p> Project file:
43<p> <pre>TEMPLATE = app
44
45CONFIG += warn_on
46
47REQUIRES = full-config
48
49HEADERS += element.h \
50 canvastext.h \
51 canvasview.h \
52 chartform.h \
53 optionsform.h \
54 setdataform.h
55SOURCES += element.cpp \
56 canvasview.cpp \
57 chartform.cpp \
58 chartform_canvas.cpp \
59 chartform_files.cpp \
60 optionsform.cpp \
61 setdataform.cpp \
62 main.cpp
63</pre>
64
65<p> <hr>
66<p> Header files:
67<p> <pre>#ifndef ELEMENT_H
68#define ELEMENT_H
69
70#include &lt;<a href="qcolor-h.html">qcolor.h</a>&gt;
71#include &lt;<a href="qnamespace-h.html">qnamespace.h</a>&gt;
72#include &lt;<a href="qstring-h.html">qstring.h</a>&gt;
73#include &lt;<a href="qvaluevector-h.html">qvaluevector.h</a>&gt;
74
75class Element;
76
77typedef QValueVector&lt;Element&gt; ElementVector;
78
79/*
80 Elements are valid if they have a value which is &gt; EPSILON.
81*/
82const double EPSILON = 0.0000001; // Must be &gt; INVALID.
83
84
85class Element
86{
87public:
88 enum { INVALID = -1 };
89 enum { NO_PROPORTION = -1 };
90 enum { MAX_PROPOINTS = 3 }; // One proportional point per chart type
91
92 Element( double value = INVALID, QColor valueColor = Qt::gray,
93 int valuePattern = Qt::SolidPattern,
94 const <a href="qstring.html">QString</a>&amp; label = <a href="qstring.html#QString-null">QString::null</a>,
95 <a href="qcolor.html">QColor</a> labelColor = Qt::black ) {
96 init( value, valueColor, valuePattern, label, labelColor );
97 for ( int i = 0; i &lt; MAX_PROPOINTS * 2; ++i )
98 m_propoints[i] = NO_PROPORTION;
99 }
100 ~Element() {}
101
102 bool isValid() const { return m_value &gt; EPSILON; }
103
104 double value() const { return m_value; }
105 <a href="qcolor.html">QColor</a> valueColor() const { return m_valueColor; }
106 int valuePattern() const { return m_valuePattern; }
107 <a href="qstring.html">QString</a> label() const { return m_label; }
108 <a href="qcolor.html">QColor</a> labelColor() const { return m_labelColor; }
109 double proX( int index ) const;
110 double proY( int index ) const;
111
112 void set( double value = INVALID, QColor valueColor = Qt::gray,
113 int valuePattern = Qt::SolidPattern,
114 const <a href="qstring.html">QString</a>&amp; label = QString::null,
115 <a href="qcolor.html">QColor</a> labelColor = Qt::black ) {
116 init( value, valueColor, valuePattern, label, labelColor );
117 }
118 void setValue( double value ) { m_value = value; }
119 void setValueColor( <a href="qcolor.html">QColor</a> valueColor ) { m_valueColor = valueColor; }
120 void setValuePattern( int valuePattern );
121 void setLabel( const <a href="qstring.html">QString</a>&amp; label ) { m_label = label; }
122 void setLabelColor( <a href="qcolor.html">QColor</a> labelColor ) { m_labelColor = labelColor; }
123 void setProX( int index, double value );
124 void setProY( int index, double value );
125
126#ifdef Q_FULL_TEMPLATE_INSTANTIATION
127 // xlC 3.x workaround
128 Q_DUMMY_COMPARISON_OPERATOR(Element)
129 bool operator!=( const Element&amp; e) const {
130 return ( !(e == *this) );
131 }
132#endif
133
134private:
135 void init( double value, QColor valueColor, int valuePattern,
136 const <a href="qstring.html">QString</a>&amp; label, QColor labelColor );
137
138 double m_value;
139 <a href="qcolor.html">QColor</a> m_valueColor;
140 int m_valuePattern;
141 <a href="qstring.html">QString</a> m_label;
142 <a href="qcolor.html">QColor</a> m_labelColor;
143 double m_propoints[2 * MAX_PROPOINTS];
144};
145
146
147QTextStream &amp;operator&lt;&lt;( <a href="qtextstream.html">QTextStream</a>&amp;, const Element&amp; );
148QTextStream &amp;operator&gt;&gt;( <a href="qtextstream.html">QTextStream</a>&amp;, Element&amp; );
149
150#endif
151</pre>
152
153<pre>#ifndef CHARTFORM_H
154#define CHARTFORM_H
155
156#include "element.h"
157
158#include &lt;<a href="qmainwindow-h.html">qmainwindow.h</a>&gt;
159#include &lt;<a href="qstringlist-h.html">qstringlist.h</a>&gt;
160
161
162class CanvasView;
163
164class QAction;
165class QCanvas;
166class QFont;
167class QPrinter;
168class QString;
169
170
171class ChartForm: public <a href="qmainwindow.html">QMainWindow</a>
172{
173 <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
174public:
175 enum { MAX_ELEMENTS = 100 };
176 enum { MAX_RECENTFILES = 9 }; // Must not exceed 9
177 enum ChartType { PIE, VERTICAL_BAR, HORIZONTAL_BAR };
178 enum AddValuesType { NO, YES, AS_PERCENTAGE };
179
180 ChartForm( const <a href="qstring.html">QString</a>&amp; filename );
181 ~ChartForm();
182
183 int chartType() { return m_chartType; }
184 void setChanged( bool changed = TRUE ) { m_changed = changed; }
185 void drawElements();
186
187 <a href="qpopupmenu.html">QPopupMenu</a> *optionsMenu; // Why public? See canvasview.cpp
188
189protected:
190 virtual void closeEvent( <a href="qcloseevent.html">QCloseEvent</a> * );
191
192private slots:
193 void fileNew();
194 void fileOpen();
195 void fileOpenRecent( int index );
196 void fileSave();
197 void fileSaveAs();
198 void fileSaveAsPixmap();
199 void filePrint();
200 void fileQuit();
201 void optionsSetData();
202 void updateChartType( <a href="qaction.html">QAction</a> *action );
203 void optionsSetFont();
204 void optionsSetOptions();
205 void helpHelp();
206 void helpAbout();
207 void helpAboutQt();
208 void saveOptions();
209
210private:
211 void init();
212 void load( const <a href="qstring.html">QString</a>&amp; filename );
213 bool okToClear();
214 void drawPieChart( const double scales[], double total, int count );
215 void drawVerticalBarChart( const double scales[], double total, int count );
216 void drawHorizontalBarChart( const double scales[], double total, int count );
217
218 <a href="qstring.html">QString</a> valueLabel( const <a href="qstring.html">QString</a>&amp; label, double value, double total );
219 void updateRecentFiles( const <a href="qstring.html">QString</a>&amp; filename );
220 void updateRecentFilesMenu();
221 void setChartType( ChartType chartType );
222
223 <a href="qpopupmenu.html">QPopupMenu</a> *fileMenu;
224 <a href="qaction.html">QAction</a> *optionsPieChartAction;
225 <a href="qaction.html">QAction</a> *optionsHorizontalBarChartAction;
226 <a href="qaction.html">QAction</a> *optionsVerticalBarChartAction;
227
228
229 <a href="qstring.html">QString</a> m_filename;
230 <a href="qstringlist.html">QStringList</a> m_recentFiles;
231 <a href="qcanvas.html">QCanvas</a> *m_canvas;
232 CanvasView *m_canvasView;
233 bool m_changed;
234 ElementVector m_elements;
235 <a href="qprinter.html">QPrinter</a> *m_printer;
236 ChartType m_chartType;
237 AddValuesType m_addValues;
238 int m_decimalPlaces;
239 <a href="qfont.html">QFont</a> m_font;
240};
241
242#endif
243</pre>
244
245<p> <hr>
246<p> Implementation:
247<p> <pre>#include "canvasview.h"
248#include "chartform.h"
249#include "optionsform.h"
250#include "setdataform.h"
251
252#include &lt;<a href="qaction-h.html">qaction.h</a>&gt;
253#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
254#include &lt;<a href="qcombobox-h.html">qcombobox.h</a>&gt;
255#include &lt;<a href="qfile-h.html">qfile.h</a>&gt;
256#include &lt;<a href="qfiledialog-h.html">qfiledialog.h</a>&gt;
257#include &lt;<a href="qfont-h.html">qfont.h</a>&gt;
258#include &lt;<a href="qfontdialog-h.html">qfontdialog.h</a>&gt;
259#include &lt;<a href="qmenubar-h.html">qmenubar.h</a>&gt;
260#include &lt;<a href="qmessagebox-h.html">qmessagebox.h</a>&gt;
261#include &lt;<a href="qpixmap-h.html">qpixmap.h</a>&gt;
262#include &lt;<a href="qpopupmenu-h.html">qpopupmenu.h</a>&gt;
263#include &lt;<a href="qprinter-h.html">qprinter.h</a>&gt;
264#include &lt;<a href="qradiobutton-h.html">qradiobutton.h</a>&gt;
265#include &lt;<a href="qsettings-h.html">qsettings.h</a>&gt;
266#include &lt;<a href="qspinbox-h.html">qspinbox.h</a>&gt;
267#include &lt;<a href="qstatusbar-h.html">qstatusbar.h</a>&gt;
268#include &lt;<a href="qtoolbar-h.html">qtoolbar.h</a>&gt;
269#include &lt;<a href="qtoolbutton-h.html">qtoolbutton.h</a>&gt;
270
271#include "images/file_new.xpm"
272#include "images/file_open.xpm"
273#include "images/file_save.xpm"
274#include "images/file_print.xpm"
275#include "images/options_setdata.xpm"
276#include "images/options_setfont.xpm"
277#include "images/options_setoptions.xpm"
278#include "images/options_horizontalbarchart.xpm"
279#include "images/options_piechart.xpm"
280#include "images/options_verticalbarchart.xpm"
281
282
283const <a href="qstring.html">QString</a> WINDOWS_REGISTRY = "/Trolltech/QtExamples";
284const <a href="qstring.html">QString</a> APP_KEY = "/Chart/";
285
286
287<a name="f596"></a>ChartForm::ChartForm( const <a href="qstring.html">QString</a>&amp; filename )
288 : <a href="qmainwindow.html">QMainWindow</a>( 0, 0, WDestructiveClose )
289{
290 <a href="qwidget.html#setIcon">setIcon</a>( QPixmap( options_piechart ) );
291
292 <a href="qaction.html">QAction</a> *fileNewAction;
293 <a href="qaction.html">QAction</a> *fileOpenAction;
294 <a href="qaction.html">QAction</a> *fileSaveAction;
295 <a href="qaction.html">QAction</a> *fileSaveAsAction;
296 <a href="qaction.html">QAction</a> *fileSaveAsPixmapAction;
297 <a href="qaction.html">QAction</a> *filePrintAction;
298 <a href="qaction.html">QAction</a> *fileQuitAction;
299 <a href="qaction.html">QAction</a> *optionsSetDataAction;
300 <a href="qaction.html">QAction</a> *optionsSetFontAction;
301 <a href="qaction.html">QAction</a> *optionsSetOptionsAction;
302
303 fileNewAction = new <a href="qaction.html">QAction</a>(
304 "New Chart", QPixmap( file_new ),
305 "&amp;New", CTRL+Key_N, this, "new" );
306<a name="x2869"></a> <a href="qobject.html#connect">connect</a>( fileNewAction, SIGNAL( <a href="qaction.html#activated">activated</a>() ), this, SLOT( fileNew() ) );
307
308 fileOpenAction = new <a href="qaction.html">QAction</a>(
309 "Open Chart", QPixmap( file_open ),
310 "&amp;Open...", CTRL+Key_O, this, "open" );
311 <a href="qobject.html#connect">connect</a>( fileOpenAction, SIGNAL( <a href="qaction.html#activated">activated</a>() ), this, SLOT( fileOpen() ) );
312
313 fileSaveAction = new <a href="qaction.html">QAction</a>(
314 "Save Chart", QPixmap( file_save ),
315 "&amp;Save", CTRL+Key_S, this, "save" );
316 <a href="qobject.html#connect">connect</a>( fileSaveAction, SIGNAL( <a href="qaction.html#activated">activated</a>() ), this, SLOT( fileSave() ) );
317
318 fileSaveAsAction = new <a href="qaction.html">QAction</a>(
319 "Save Chart As", QPixmap( file_save ),
320 "Save &amp;As...", 0, this, "save as" );
321 <a href="qobject.html#connect">connect</a>( fileSaveAsAction, SIGNAL( <a href="qaction.html#activated">activated</a>() ),
322 this, SLOT( fileSaveAs() ) );
323
324 fileSaveAsPixmapAction = new <a href="qaction.html">QAction</a>(
325 "Save Chart As Bitmap", QPixmap( file_save ),
326 "Save As &amp;Bitmap...", CTRL+Key_B, this, "save as bitmap" );
327 <a href="qobject.html#connect">connect</a>( fileSaveAsPixmapAction, SIGNAL( <a href="qaction.html#activated">activated</a>() ),
328 this, SLOT( fileSaveAsPixmap() ) );
329
330 filePrintAction = new <a href="qaction.html">QAction</a>(
331 "Print Chart", QPixmap( file_print ),
332 "&amp;Print Chart...", CTRL+Key_P, this, "print chart" );
333 <a href="qobject.html#connect">connect</a>( filePrintAction, SIGNAL( <a href="qaction.html#activated">activated</a>() ),
334 this, SLOT( filePrint() ) );
335
336 optionsSetDataAction = new <a href="qaction.html">QAction</a>(
337 "Set Data", QPixmap( options_setdata ),
338 "Set &amp;Data...", CTRL+Key_D, this, "set data" );
339 <a href="qobject.html#connect">connect</a>( optionsSetDataAction, SIGNAL( <a href="qaction.html#activated">activated</a>() ),
340 this, SLOT( optionsSetData() ) );
341
342
343 <a href="qactiongroup.html">QActionGroup</a> *chartGroup = new <a href="qactiongroup.html">QActionGroup</a>( this ); // Connected later
344<a name="x2874"></a> chartGroup-&gt;<a href="qactiongroup.html#setExclusive">setExclusive</a>( TRUE );
345
346 optionsPieChartAction = new <a href="qaction.html">QAction</a>(
347 "Pie Chart", QPixmap( options_piechart ),
348 "&amp;Pie Chart", CTRL+Key_I, chartGroup, "pie chart" );
349<a name="x2872"></a> optionsPieChartAction-&gt;<a href="qaction.html#setToggleAction">setToggleAction</a>( TRUE );
350
351 optionsHorizontalBarChartAction = new <a href="qaction.html">QAction</a>(
352 "Horizontal Bar Chart", QPixmap( options_horizontalbarchart ),
353 "&amp;Horizontal Bar Chart", CTRL+Key_H, chartGroup,
354 "horizontal bar chart" );
355 optionsHorizontalBarChartAction-&gt;<a href="qaction.html#setToggleAction">setToggleAction</a>( TRUE );
356
357 optionsVerticalBarChartAction = new <a href="qaction.html">QAction</a>(
358 "Vertical Bar Chart", QPixmap( options_verticalbarchart ),
359 "&amp;Vertical Bar Chart", CTRL+Key_V, chartGroup, "Vertical bar chart" );
360 optionsVerticalBarChartAction-&gt;<a href="qaction.html#setToggleAction">setToggleAction</a>( TRUE );
361
362
363 optionsSetFontAction = new <a href="qaction.html">QAction</a>(
364 "Set Font", QPixmap( options_setfont ),
365 "Set &amp;Font...", CTRL+Key_F, this, "set font" );
366 <a href="qobject.html#connect">connect</a>( optionsSetFontAction, SIGNAL( <a href="qaction.html#activated">activated</a>() ),
367 this, SLOT( optionsSetFont() ) );
368
369 optionsSetOptionsAction = new <a href="qaction.html">QAction</a>(
370 "Set Options", QPixmap( options_setoptions ),
371 "Set &amp;Options...", 0, this, "set options" );
372 <a href="qobject.html#connect">connect</a>( optionsSetOptionsAction, SIGNAL( <a href="qaction.html#activated">activated</a>() ),
373 this, SLOT( optionsSetOptions() ) );
374
375 fileQuitAction = new <a href="qaction.html">QAction</a>( "Quit", "&amp;Quit", CTRL+Key_Q, this, "quit" );
376 <a href="qobject.html#connect">connect</a>( fileQuitAction, SIGNAL( <a href="qaction.html#activated">activated</a>() ), this, SLOT( fileQuit() ) );
377
378
379 <a href="qtoolbar.html">QToolBar</a>* fileTools = new <a href="qtoolbar.html">QToolBar</a>( this, "file operations" );
380<a name="x2895"></a> fileTools-&gt;<a href="qtoolbar.html#setLabel">setLabel</a>( "File Operations" );
381<a name="x2870"></a> fileNewAction-&gt;<a href="qaction.html#addTo">addTo</a>( fileTools );
382 fileOpenAction-&gt;<a href="qaction.html#addTo">addTo</a>( fileTools );
383 fileSaveAction-&gt;<a href="qaction.html#addTo">addTo</a>( fileTools );
384<a name="x2894"></a> fileTools-&gt;<a href="qtoolbar.html#addSeparator">addSeparator</a>();
385 filePrintAction-&gt;<a href="qaction.html#addTo">addTo</a>( fileTools );
386
387 <a href="qtoolbar.html">QToolBar</a> *optionsTools = new <a href="qtoolbar.html">QToolBar</a>( this, "options operations" );
388 optionsTools-&gt;<a href="qtoolbar.html#setLabel">setLabel</a>( "Options Operations" );
389 optionsSetDataAction-&gt;<a href="qaction.html#addTo">addTo</a>( optionsTools );
390 optionsTools-&gt;<a href="qtoolbar.html#addSeparator">addSeparator</a>();
391 optionsPieChartAction-&gt;<a href="qaction.html#addTo">addTo</a>( optionsTools );
392 optionsHorizontalBarChartAction-&gt;<a href="qaction.html#addTo">addTo</a>( optionsTools );
393 optionsVerticalBarChartAction-&gt;<a href="qaction.html#addTo">addTo</a>( optionsTools );
394 optionsTools-&gt;<a href="qtoolbar.html#addSeparator">addSeparator</a>();
395 optionsSetFontAction-&gt;<a href="qaction.html#addTo">addTo</a>( optionsTools );
396 optionsTools-&gt;<a href="qtoolbar.html#addSeparator">addSeparator</a>();
397 optionsSetOptionsAction-&gt;<a href="qaction.html#addTo">addTo</a>( optionsTools );
398
399 fileMenu = new <a href="qpopupmenu.html">QPopupMenu</a>( this );
400 <a href="qmainwindow.html#menuBar">menuBar</a>()-&gt;insertItem( "&amp;File", fileMenu );
401 fileNewAction-&gt;<a href="qaction.html#addTo">addTo</a>( fileMenu );
402 fileOpenAction-&gt;<a href="qaction.html#addTo">addTo</a>( fileMenu );
403 fileSaveAction-&gt;<a href="qaction.html#addTo">addTo</a>( fileMenu );
404 fileSaveAsAction-&gt;<a href="qaction.html#addTo">addTo</a>( fileMenu );
405 fileMenu-&gt;<a href="qmenudata.html#insertSeparator">insertSeparator</a>();
406 fileSaveAsPixmapAction-&gt;<a href="qaction.html#addTo">addTo</a>( fileMenu );
407 fileMenu-&gt;<a href="qmenudata.html#insertSeparator">insertSeparator</a>();
408 filePrintAction-&gt;<a href="qaction.html#addTo">addTo</a>( fileMenu );
409 fileMenu-&gt;<a href="qmenudata.html#insertSeparator">insertSeparator</a>();
410 fileQuitAction-&gt;<a href="qaction.html#addTo">addTo</a>( fileMenu );
411
412 optionsMenu = new <a href="qpopupmenu.html">QPopupMenu</a>( this );
413 <a href="qmainwindow.html#menuBar">menuBar</a>()-&gt;insertItem( "&amp;Options", optionsMenu );
414 optionsSetDataAction-&gt;<a href="qaction.html#addTo">addTo</a>( optionsMenu );
415 optionsMenu-&gt;<a href="qmenudata.html#insertSeparator">insertSeparator</a>();
416 optionsPieChartAction-&gt;<a href="qaction.html#addTo">addTo</a>( optionsMenu );
417 optionsHorizontalBarChartAction-&gt;<a href="qaction.html#addTo">addTo</a>( optionsMenu );
418 optionsVerticalBarChartAction-&gt;<a href="qaction.html#addTo">addTo</a>( optionsMenu );
419 optionsMenu-&gt;<a href="qmenudata.html#insertSeparator">insertSeparator</a>();
420 optionsSetFontAction-&gt;<a href="qaction.html#addTo">addTo</a>( optionsMenu );
421 optionsMenu-&gt;<a href="qmenudata.html#insertSeparator">insertSeparator</a>();
422 optionsSetOptionsAction-&gt;<a href="qaction.html#addTo">addTo</a>( optionsMenu );
423
424 <a href="qmainwindow.html#menuBar">menuBar</a>()-&gt;insertSeparator();
425
426 <a href="qpopupmenu.html">QPopupMenu</a> *helpMenu = new <a href="qpopupmenu.html">QPopupMenu</a>( this );
427 <a href="qmainwindow.html#menuBar">menuBar</a>()-&gt;insertItem( "&amp;Help", helpMenu );
428 helpMenu-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "&amp;Help", this, SLOT(helpHelp()), Key_F1 );
429 helpMenu-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "&amp;About", this, SLOT(helpAbout()) );
430 helpMenu-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "About &amp;Qt", this, SLOT(helpAboutQt()) );
431
432
433 m_printer = 0;
434 m_elements.resize( MAX_ELEMENTS );
435
436 <a href="qsettings.html">QSettings</a> settings;
437<a name="x2890"></a> settings.<a href="qsettings.html#insertSearchPath">insertSearchPath</a>( QSettings::Windows, WINDOWS_REGISTRY );
438 int windowWidth = settings.<a href="qsettings.html#readNumEntry">readNumEntry</a>( APP_KEY + "WindowWidth", 460 );
439 int windowHeight = settings.<a href="qsettings.html#readNumEntry">readNumEntry</a>( APP_KEY + "WindowHeight", 530 );
440 int windowX = settings.<a href="qsettings.html#readNumEntry">readNumEntry</a>( APP_KEY + "WindowX", -1 );
441 int windowY = settings.<a href="qsettings.html#readNumEntry">readNumEntry</a>( APP_KEY + "WindowY", -1 );
442 setChartType( ChartType(
443 settings.<a href="qsettings.html#readNumEntry">readNumEntry</a>( APP_KEY + "ChartType", int(PIE) ) ) );
444 m_addValues = AddValuesType(
445 settings.<a href="qsettings.html#readNumEntry">readNumEntry</a>( APP_KEY + "AddValues", int(NO) ));
446 m_decimalPlaces = settings.<a href="qsettings.html#readNumEntry">readNumEntry</a>( APP_KEY + "Decimals", 2 );
447 m_font = QFont( "Helvetica", 18, QFont::Bold );
448 m_font.fromString(
449 settings.<a href="qsettings.html#readEntry">readEntry</a>( APP_KEY + "Font", m_font.toString() ) );
450 for ( int i = 0; i &lt; MAX_RECENTFILES; ++i ) {
451 <a href="qstring.html">QString</a> filename = settings.<a href="qsettings.html#readEntry">readEntry</a>( APP_KEY + "File" +
452<a name="x2893"></a> QString::<a href="qstring.html#number">number</a>( i + 1 ) );
453<a name="x2892"></a> if ( !filename.<a href="qstring.html#isEmpty">isEmpty</a>() )
454 m_recentFiles.push_back( filename );
455 }
456 if ( m_recentFiles.count() )
457 updateRecentFilesMenu();
458
459
460 // Connect *after* we've set the chart type on so we don't call
461 // drawElements() prematurely.
462<a name="x2873"></a> <a href="qobject.html#connect">connect</a>( chartGroup, SIGNAL( <a href="qactiongroup.html#selected">selected</a>(QAction*) ),
463 this, SLOT( updateChartType(QAction*) ) );
464
465 <a href="qwidget.html#resize">resize</a>( windowWidth, windowHeight );
466 if ( windowX != -1 || windowY != -1 )
467 <a href="qwidget.html#move">move</a>( windowX, windowY );
468
469 m_canvas = new <a href="qcanvas.html">QCanvas</a>( this );
470<a name="x2876"></a> m_canvas-&gt;<a href="qcanvas.html#resize">resize</a>( <a href="qwidget.html#width">width</a>(), height() );
471 m_canvasView = new CanvasView( m_canvas, &amp;m_elements, this );
472 <a href="qmainwindow.html#setCentralWidget">setCentralWidget</a>( m_canvasView );
473 m_canvasView-&gt;<a href="qwidget.html#show">show</a>();
474
475 if ( !filename.<a href="qstring.html#isEmpty">isEmpty</a>() )
476 load( filename );
477 else {
478 init();
479 m_elements[0].set( 20, red, 14, "Red" );
480 m_elements[1].set( 70, cyan, 2, "Cyan", darkGreen );
481 m_elements[2].set( 35, blue, 11, "Blue" );
482 m_elements[3].set( 55, yellow, 1, "Yellow", darkBlue );
483 m_elements[4].set( 80, magenta, 1, "Magenta" );
484 drawElements();
485 }
486
487 <a href="qmainwindow.html#statusBar">statusBar</a>()-&gt;message( "Ready", 2000 );
488}
489
490
491ChartForm::~ChartForm()
492{
493 delete m_printer;
494}
495
496
497void <a name="f597"></a>ChartForm::init()
498{
499 <a href="qwidget.html#setCaption">setCaption</a>( "Chart" );
500 m_filename = QString::null;
501 m_changed = FALSE;
502
503 m_elements[0] = Element( Element::INVALID, red );
504 m_elements[1] = Element( Element::INVALID, cyan );
505 m_elements[2] = Element( Element::INVALID, blue );
506 m_elements[3] = Element( Element::INVALID, yellow );
507 m_elements[4] = Element( Element::INVALID, green );
508 m_elements[5] = Element( Element::INVALID, magenta );
509 m_elements[6] = Element( Element::INVALID, darkYellow );
510 m_elements[7] = Element( Element::INVALID, darkRed );
511 m_elements[8] = Element( Element::INVALID, darkCyan );
512 m_elements[9] = Element( Element::INVALID, darkGreen );
513 m_elements[10] = Element( Element::INVALID, darkMagenta );
514 m_elements[11] = Element( Element::INVALID, darkBlue );
515 for ( int i = 12; i &lt; MAX_ELEMENTS; ++i ) {
516 double x = (double(i) / MAX_ELEMENTS) * 360;
517 int y = (int(x * 256) % 105) + 151;
518 int z = ((i * 17) % 105) + 151;
519 m_elements[i] = Element( Element::INVALID, QColor( int(x), y, z, QColor::Hsv ) );
520 }
521}
522
523<a name="x2896"></a>void ChartForm::<a href="qwidget.html#closeEvent">closeEvent</a>( <a href="qcloseevent.html">QCloseEvent</a> * )
524{
525 fileQuit();
526}
527
528
529void <a name="f598"></a>ChartForm::fileNew()
530{
531 if ( okToClear() ) {
532 init();
533 drawElements();
534 }
535}
536
537
538void <a name="f599"></a>ChartForm::fileOpen()
539{
540 if ( !okToClear() )
541 return;
542
543 <a href="qstring.html">QString</a> filename = QFileDialog::<a href="qfiledialog.html#getOpenFileName">getOpenFileName</a>(
544 QString::null, "Charts (*.cht)", this,
545 "file open", "Chart -- File Open" );
546 if ( !filename.<a href="qstring.html#isEmpty">isEmpty</a>() )
547 load( filename );
548 else
549 <a href="qmainwindow.html#statusBar">statusBar</a>()-&gt;message( "File Open abandoned", 2000 );
550}
551
552
553void <a name="f600"></a>ChartForm::fileSaveAs()
554{
555 <a href="qstring.html">QString</a> filename = QFileDialog::<a href="qfiledialog.html#getSaveFileName">getSaveFileName</a>(
556 QString::null, "Charts (*.cht)", this,
557 "file save as", "Chart -- File Save As" );
558 if ( !filename.<a href="qstring.html#isEmpty">isEmpty</a>() ) {
559 int answer = 0;
560<a name="x2878"></a> if ( QFile::<a href="qfile.html#exists">exists</a>( filename ) )
561<a name="x2889"></a> answer = QMessageBox::<a href="qmessagebox.html#warning">warning</a>(
562 this, "Chart -- Overwrite File",
563 QString( "Overwrite\n\'%1\'?" ).
564 arg( filename ),
565 "&amp;Yes", "&amp;No", QString::null, 1, 1 );
566 if ( answer == 0 ) {
567 m_filename = filename;
568 updateRecentFiles( filename );
569 fileSave();
570 return;
571 }
572 }
573 <a href="qmainwindow.html#statusBar">statusBar</a>()-&gt;message( "Saving abandoned", 2000 );
574}
575
576
577void <a name="f601"></a>ChartForm::fileOpenRecent( int index )
578{
579 if ( !okToClear() )
580 return;
581
582 load( m_recentFiles[index] );
583}
584
585
586void <a name="f602"></a>ChartForm::updateRecentFiles( const <a href="qstring.html">QString</a>&amp; filename )
587{
588 if ( m_recentFiles.find( filename ) != m_recentFiles.end() )
589 return;
590
591 m_recentFiles.push_back( filename );
592 if ( m_recentFiles.count() &gt; MAX_RECENTFILES )
593 m_recentFiles.pop_front();
594
595 updateRecentFilesMenu();
596}
597
598
599void <a name="f603"></a>ChartForm::updateRecentFilesMenu()
600{
601 for ( int i = 0; i &lt; MAX_RECENTFILES; ++i ) {
602<a name="x2882"></a> if ( fileMenu-&gt;<a href="qmenudata.html#findItem">findItem</a>( i ) )
603<a name="x2885"></a> fileMenu-&gt;<a href="qmenudata.html#removeItem">removeItem</a>( i );
604 if ( i &lt; int(m_recentFiles.count()) )
605 fileMenu-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( QString( "&amp;%1 %2" ).
606 arg( i + 1 ).arg( m_recentFiles[i] ),
607 this, SLOT( fileOpenRecent(int) ),
608 0, i );
609 }
610}
611
612
613void <a name="f604"></a>ChartForm::fileQuit()
614{
615 if ( okToClear() ) {
616 saveOptions();
617<a name="x2875"></a> qApp-&gt;<a href="qapplication.html#exit">exit</a>( 0 );
618 }
619}
620
621
622bool <a name="f605"></a>ChartForm::okToClear()
623{
624 if ( m_changed ) {
625 <a href="qstring.html">QString</a> msg;
626 if ( m_filename.isEmpty() )
627 msg = "Unnamed chart ";
628 else
629 msg = QString( "Chart '%1'\n" ).arg( m_filename );
630 msg += "has been changed.";
631
632 int x = QMessageBox::<a href="qmessagebox.html#information">information</a>( this, "Chart -- Unsaved Changes",
633 msg, "&amp;Save", "Cancel", "&amp;Abandon",
634 0, 1 );
635 switch( x ) {
636 case 0: // Save
637 fileSave();
638 break;
639 case 1: // Cancel
640 default:
641 return FALSE;
642 case 2: // Abandon
643 break;
644 }
645 }
646
647 return TRUE;
648}
649
650
651void <a name="f606"></a>ChartForm::saveOptions()
652{
653 <a href="qsettings.html">QSettings</a> settings;
654 settings.<a href="qsettings.html#insertSearchPath">insertSearchPath</a>( QSettings::Windows, WINDOWS_REGISTRY );
655<a name="x2891"></a> settings.<a href="qsettings.html#writeEntry">writeEntry</a>( APP_KEY + "WindowWidth", width() );
656 settings.<a href="qsettings.html#writeEntry">writeEntry</a>( APP_KEY + "WindowHeight", height() );
657 settings.<a href="qsettings.html#writeEntry">writeEntry</a>( APP_KEY + "WindowX", x() );
658 settings.<a href="qsettings.html#writeEntry">writeEntry</a>( APP_KEY + "WindowY", y() );
659 settings.<a href="qsettings.html#writeEntry">writeEntry</a>( APP_KEY + "ChartType", int(m_chartType) );
660 settings.<a href="qsettings.html#writeEntry">writeEntry</a>( APP_KEY + "AddValues", int(m_addValues) );
661 settings.<a href="qsettings.html#writeEntry">writeEntry</a>( APP_KEY + "Decimals", m_decimalPlaces );
662 settings.<a href="qsettings.html#writeEntry">writeEntry</a>( APP_KEY + "Font", m_font.toString() );
663 for ( int i = 0; i &lt; int(m_recentFiles.count()); ++i )
664 settings.<a href="qsettings.html#writeEntry">writeEntry</a>( APP_KEY + "File" + QString::number( i + 1 ),
665 m_recentFiles[i] );
666}
667
668
669void <a name="f607"></a>ChartForm::optionsSetData()
670{
671 SetDataForm *setDataForm = new SetDataForm( &amp;m_elements, m_decimalPlaces, this );
672<a name="x2877"></a> if ( setDataForm-&gt;<a href="qdialog.html#exec">exec</a>() ) {
673 m_changed = TRUE;
674 drawElements();
675 }
676 delete setDataForm;
677}
678
679
680void <a name="f608"></a>ChartForm::setChartType( ChartType chartType )
681{
682 m_chartType = chartType;
683 switch ( m_chartType ) {
684 case PIE:
685<a name="x2871"></a> optionsPieChartAction-&gt;<a href="qaction.html#setOn">setOn</a>( TRUE );
686 break;
687 case VERTICAL_BAR:
688 optionsVerticalBarChartAction-&gt;<a href="qaction.html#setOn">setOn</a>( TRUE );
689 break;
690 case HORIZONTAL_BAR:
691 optionsHorizontalBarChartAction-&gt;<a href="qaction.html#setOn">setOn</a>( TRUE );
692 break;
693 }
694}
695
696
697void <a name="f609"></a>ChartForm::updateChartType( <a href="qaction.html">QAction</a> *action )
698{
699 if ( action == optionsPieChartAction ) {
700 m_chartType = PIE;
701 }
702 else if ( action == optionsHorizontalBarChartAction ) {
703 m_chartType = HORIZONTAL_BAR;
704 }
705 else if ( action == optionsVerticalBarChartAction ) {
706 m_chartType = VERTICAL_BAR;
707 }
708
709 drawElements();
710}
711
712
713void <a name="f610"></a>ChartForm::optionsSetFont()
714{
715 bool ok;
716<a name="x2881"></a> <a href="qfont.html">QFont</a> font = QFontDialog::<a href="qfontdialog.html#getFont">getFont</a>( &amp;ok, m_font, this );
717 if ( ok ) {
718 m_font = font;
719 drawElements();
720 }
721}
722
723
724void <a name="f611"></a>ChartForm::optionsSetOptions()
725{
726 OptionsForm *optionsForm = new OptionsForm( this );
727 optionsForm-&gt;chartTypeComboBox-&gt;setCurrentItem( m_chartType );
728 optionsForm-&gt;<a href="qwidget.html#setFont">setFont</a>( m_font );
729 switch ( m_addValues ) {
730 case NO:
731 optionsForm-&gt;noRadioButton-&gt;setChecked( TRUE );
732 break;
733 case YES:
734 optionsForm-&gt;yesRadioButton-&gt;setChecked( TRUE );
735 break;
736 case AS_PERCENTAGE:
737 optionsForm-&gt;asPercentageRadioButton-&gt;setChecked( TRUE );
738 break;
739 }
740 optionsForm-&gt;decimalPlacesSpinBox-&gt;setValue( m_decimalPlaces );
741 if ( optionsForm-&gt;<a href="qdialog.html#exec">exec</a>() ) {
742 setChartType( ChartType(
743 optionsForm-&gt;chartTypeComboBox-&gt;currentItem()) );
744<a name="x2897"></a> m_font = optionsForm-&gt;<a href="qwidget.html#font">font</a>();
745 if ( optionsForm-&gt;noRadioButton-&gt;isChecked() )
746 m_addValues = NO;
747 else if ( optionsForm-&gt;yesRadioButton-&gt;isChecked() )
748 m_addValues = YES;
749 else if ( optionsForm-&gt;asPercentageRadioButton-&gt;isChecked() )
750 m_addValues = AS_PERCENTAGE;
751 m_decimalPlaces = optionsForm-&gt;decimalPlacesSpinBox-&gt;value();
752 drawElements();
753 }
754 delete optionsForm;
755}
756
757
758void <a name="f612"></a>ChartForm::helpHelp()
759{
760 <a href="qmainwindow.html#statusBar">statusBar</a>()-&gt;message( "Help is not implemented yet", 2000 );
761}
762
763
764void <a name="f613"></a>ChartForm::helpAbout()
765{
766<a name="x2886"></a> QMessageBox::<a href="qmessagebox.html#about">about</a>( this, "Chart -- About",
767 "&lt;center&gt;&lt;h1&gt;&lt;font color=blue&gt;Chart&lt;font&gt;&lt;/h1&gt;&lt;/center&gt;"
768 "&lt;p&gt;Chart your data with &lt;i&gt;chart&lt;/i&gt;.&lt;/p&gt;"
769 );
770}
771
772
773void <a name="f614"></a>ChartForm::helpAboutQt()
774{
775<a name="x2887"></a> QMessageBox::<a href="qmessagebox.html#aboutQt">aboutQt</a>( this, "Chart -- About Qt" );
776}
777
778</pre>
779
780<p> <hr>
781<p> Main:
782<p> <pre>#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
783#include "chartform.h"
784
785
786int main( int argc, char *argv[] )
787{
788 <a href="qapplication.html">QApplication</a> app( argc, argv );
789
790 <a href="qstring.html">QString</a> filename;
791<a name="x2900"></a> if ( app.<a href="qapplication.html#argc">argc</a>() &gt; 1 ) {
792<a name="x2901"></a> filename = app.<a href="qapplication.html#argv">argv</a>()[1];
793<a name="x2904"></a> if ( !filename.<a href="qstring.html#endsWith">endsWith</a>( ".cht" ) )
794 filename = QString::null;
795 }
796
797 ChartForm *cf = new ChartForm( filename );
798 app.<a href="qapplication.html#setMainWidget">setMainWidget</a>( cf );
799 cf-&gt;<a href="qwidget.html#show">show</a>();
800
801 return app.<a href="qapplication.html#exec">exec</a>();
802}
803</pre>
804
805<p> <p>See also <a href="step-by-step-examples.html">Step-by-step Examples</a>.
806
807<!-- eof -->
808<p><address><hr><div align=center>
809<table width=100% cellspacing=0 border=0><tr>
810<td>Copyright &copy; 2007
811<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
812<td align=right><div align=right>Qt 3.3.8</div>
813</table></div></address></body>
814</html>
Note: See TracBrowser for help on using the repository browser.