Sophie

Sophie

distrib > Mandriva > 9.1 > ppc > by-pkgid > 397cf13019f8c526877f271962f26f4c > files > 623

libqt3-devel-3.1.1-13mdk.ppc.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /home/reggie/tmp/qt-3.1-reggie-23625/qt-x11-free-3.1.1/examples/chart/chart.doc:1 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>A Complete Canvas Application</title>
<style type="text/css"><!--
h3.fn,span.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>A Complete Canvas Application</h1>

 

<p> 
<p> This is a complete example program with a main window, menus and
toolbars. The main widget is a <a href="qcanvas.html">QCanvas</a>, and this example
demonstrates basic canvas usage. 

<p> <hr>
<p> Project file:
<p> <pre>TEMPLATE = app
CONFIG  += warn_on

HEADERS +=  element.h \
            canvastext.h \
            canvasview.h \
            chartform.h \
            optionsform.h \
            setdataform.h
SOURCES +=  element.cpp \
            canvasview.cpp \
            chartform.cpp \
            chartform_canvas.cpp \
            chartform_files.cpp \
            optionsform.cpp \
            setdataform.cpp \
            main.cpp
REQUIRES=full-config
</pre>

<p> <hr>
<p> Header files:
<p> <pre>#ifndef ELEMENT_H
#define ELEMENT_H

#include &lt;<a href="qcolor-h.html">qcolor.h</a>&gt;
#include &lt;<a href="qnamespace-h.html">qnamespace.h</a>&gt;
#include &lt;<a href="qstring-h.html">qstring.h</a>&gt;
#include &lt;<a href="qvaluevector-h.html">qvaluevector.h</a>&gt;

class Element;

typedef QValueVector&lt;Element&gt; ElementVector;

/*
    Elements are valid if they have a value which is &gt; EPSILON.
*/
const double EPSILON = 0.0000001; // Must be &gt; INVALID.


class Element
{
public:
    enum { INVALID = -1 };
    enum { NO_PROPORTION = -1 };
    enum { MAX_PROPOINTS = 3 }; // One proportional point per chart type

    Element( double value = INVALID, QColor valueColor = Qt::gray,
             int valuePattern = Qt::SolidPattern,
             const <a href="qstring.html">QString</a>&amp; label = <a href="qstring.html#QString-null">QString::null</a>,
             <a href="qcolor.html">QColor</a> labelColor = Qt::black ) {
        init( value, valueColor, valuePattern, label, labelColor );
        for ( int i = 0; i &lt; MAX_PROPOINTS * 2; ++i )
            m_propoints[i] = NO_PROPORTION;
    }
    ~Element() {}

    bool isValid() const { return m_value &gt; EPSILON; }

    double value() const { return m_value; }
    <a href="qcolor.html">QColor</a> valueColor() const { return m_valueColor; }
    int valuePattern() const { return m_valuePattern; }
    <a href="qstring.html">QString</a> label() const { return m_label; }
    <a href="qcolor.html">QColor</a> labelColor() const { return m_labelColor; }
    double proX( int index ) const;
    double proY( int index ) const;

    void set( double value = INVALID, QColor valueColor = Qt::gray,
              int valuePattern = Qt::SolidPattern,
              const <a href="qstring.html">QString</a>&amp; label = QString::null,
              <a href="qcolor.html">QColor</a> labelColor = Qt::black ) {
        init( value, valueColor, valuePattern, label, labelColor );
    }
    void setValue( double value ) { m_value = value; }
    void setValueColor( <a href="qcolor.html">QColor</a> valueColor ) { m_valueColor = valueColor; }
    void setValuePattern( int valuePattern );
    void setLabel( const <a href="qstring.html">QString</a>&amp; label ) { m_label = label; }
    void setLabelColor( <a href="qcolor.html">QColor</a> labelColor ) { m_labelColor = labelColor; }
    void setProX( int index, double value );
    void setProY( int index, double value );

#ifdef Q_FULL_TEMPLATE_INSTANTIATION
    // xlC 3.x workaround
    Q_DUMMY_COMPARISON_OPERATOR(Element)
    bool operator!=( const Element&amp; e) const {
        return ( !(e == *this) );
    }
#endif

private:
    void init( double value, QColor valueColor, int valuePattern,
               const <a href="qstring.html">QString</a>&amp; label, QColor labelColor );

    double m_value;
    <a href="qcolor.html">QColor</a> m_valueColor;
    int m_valuePattern;
    <a href="qstring.html">QString</a> m_label;
    <a href="qcolor.html">QColor</a> m_labelColor;
    double m_propoints[2 * MAX_PROPOINTS];
};


QTextStream &amp;operator&lt;&lt;( <a href="qtextstream.html">QTextStream</a>&amp;, const Element&amp; );
QTextStream &amp;operator&gt;&gt;( <a href="qtextstream.html">QTextStream</a>&amp;, Element&amp; );

#endif
</pre>

<pre>#ifndef CHARTFORM_H
#define CHARTFORM_H

#include "element.h"

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


class CanvasView;

class QAction;
class QCanvas;
class QFont;
class QPrinter;
class QString;


class ChartForm: public <a href="qmainwindow.html">QMainWindow</a>
{
    <a href="metaobjects.html#Q_OBJECT">Q_OBJECT</a>
public:
    enum { MAX_ELEMENTS = 100 };
    enum { MAX_RECENTFILES = 9 }; // Must not exceed 9
    enum ChartType { PIE, VERTICAL_BAR, HORIZONTAL_BAR };
    enum AddValuesType { NO, YES, AS_PERCENTAGE };

    ChartForm( const <a href="qstring.html">QString</a>&amp; filename );
    ~ChartForm();

    int chartType() { return m_chartType; }
    void setChanged( bool changed = TRUE ) { m_changed = changed; }
    void drawElements();

    <a href="qpopupmenu.html">QPopupMenu</a> *optionsMenu; // Why public? See canvasview.cpp

protected:
    virtual void closeEvent( <a href="qcloseevent.html">QCloseEvent</a> * );

private slots:
    void fileNew();
    void fileOpen();
    void fileOpenRecent( int index );
    void fileSave();
    void fileSaveAs();
    void fileSaveAsPixmap();
    void filePrint();
    void fileQuit();
    void optionsSetData();
    void updateChartType( <a href="qaction.html">QAction</a> *action );
    void optionsSetFont();
    void optionsSetOptions();
    void helpHelp();
    void helpAbout();
    void helpAboutQt();
    void saveOptions();

private:
    void init();
    void load( const <a href="qstring.html">QString</a>&amp; filename );
    bool okToClear();
    void drawPieChart( const double scales[], double total, int count );
    void drawVerticalBarChart( const double scales[], double total, int count );
    void drawHorizontalBarChart( const double scales[], double total, int count );

    <a href="qstring.html">QString</a> valueLabel( const <a href="qstring.html">QString</a>&amp; label, double value, double total );
    void updateRecentFiles( const <a href="qstring.html">QString</a>&amp; filename );
    void updateRecentFilesMenu();
    void setChartType( ChartType chartType );

    <a href="qpopupmenu.html">QPopupMenu</a> *fileMenu;
    <a href="qaction.html">QAction</a> *optionsPieChartAction;
    <a href="qaction.html">QAction</a> *optionsHorizontalBarChartAction;
    <a href="qaction.html">QAction</a> *optionsVerticalBarChartAction;


    <a href="qstring.html">QString</a> m_filename;
    <a href="qstringlist.html">QStringList</a> m_recentFiles;
    <a href="qcanvas.html">QCanvas</a> *m_canvas;
    CanvasView *m_canvasView;
    bool m_changed;
    ElementVector m_elements;
    <a href="qprinter.html">QPrinter</a> *m_printer;
    ChartType m_chartType;
    AddValuesType m_addValues;
    int m_decimalPlaces;
    <a href="qfont.html">QFont</a> m_font;
};

#endif
</pre>

<p> <hr>
<p> Implementation:
<p> <pre>#include "canvasview.h"
#include "chartform.h"
#include "optionsform.h"
#include "setdataform.h"

#include &lt;<a href="qaction-h.html">qaction.h</a>&gt;
#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
#include &lt;<a href="qcombobox-h.html">qcombobox.h</a>&gt;
#include &lt;<a href="qfile-h.html">qfile.h</a>&gt;
#include &lt;<a href="qfiledialog-h.html">qfiledialog.h</a>&gt;
#include &lt;<a href="qfont-h.html">qfont.h</a>&gt;
#include &lt;<a href="qfontdialog-h.html">qfontdialog.h</a>&gt;
#include &lt;<a href="qmenubar-h.html">qmenubar.h</a>&gt;
#include &lt;<a href="qmessagebox-h.html">qmessagebox.h</a>&gt;
#include &lt;<a href="qpixmap-h.html">qpixmap.h</a>&gt;
#include &lt;<a href="qpopupmenu-h.html">qpopupmenu.h</a>&gt;
#include &lt;<a href="qprinter-h.html">qprinter.h</a>&gt;
#include &lt;<a href="qradiobutton-h.html">qradiobutton.h</a>&gt;
#include &lt;<a href="qsettings-h.html">qsettings.h</a>&gt;
#include &lt;<a href="qspinbox-h.html">qspinbox.h</a>&gt;
#include &lt;<a href="qstatusbar-h.html">qstatusbar.h</a>&gt;
#include &lt;<a href="qtoolbar-h.html">qtoolbar.h</a>&gt;
#include &lt;<a href="qtoolbutton-h.html">qtoolbutton.h</a>&gt;

#include "images/file_new.xpm"
#include "images/file_open.xpm"
#include "images/file_save.xpm"
#include "images/file_print.xpm"
#include "images/options_setdata.xpm"
#include "images/options_setfont.xpm"
#include "images/options_setoptions.xpm"
#include "images/options_horizontalbarchart.xpm"
#include "images/options_piechart.xpm"
#include "images/options_verticalbarchart.xpm"


const <a href="qstring.html">QString</a> WINDOWS_REGISTRY = "/Trolltech/QtExamples";
const <a href="qstring.html">QString</a> APP_KEY = "/Chart/";


<a name="f574"></a>ChartForm::ChartForm( const <a href="qstring.html">QString</a>&amp; filename )
    : <a href="qmainwindow.html">QMainWindow</a>( 0, 0, WDestructiveClose )
{
    <a href="qwidget.html#setIcon">setIcon</a>( QPixmap( options_piechart ) );

    <a href="qaction.html">QAction</a> *fileNewAction;
    <a href="qaction.html">QAction</a> *fileOpenAction;
    <a href="qaction.html">QAction</a> *fileSaveAction;
    <a href="qaction.html">QAction</a> *fileSaveAsAction;
    <a href="qaction.html">QAction</a> *fileSaveAsPixmapAction;
    <a href="qaction.html">QAction</a> *filePrintAction;
    <a href="qaction.html">QAction</a> *fileQuitAction;
    <a href="qaction.html">QAction</a> *optionsSetDataAction;
    <a href="qaction.html">QAction</a> *optionsSetFontAction;
    <a href="qaction.html">QAction</a> *optionsSetOptionsAction;

    fileNewAction = new <a href="qaction.html">QAction</a>(
            "New Chart", QPixmap( file_new ),
            "&amp;New", CTRL+Key_N, this, "new" );
<a name="x2484"></a>    <a href="qobject.html#connect">connect</a>( fileNewAction, SIGNAL( <a href="qaction.html#activated">activated</a>() ), this, SLOT( fileNew() ) );

    fileOpenAction = new <a href="qaction.html">QAction</a>(
            "Open Chart", QPixmap( file_open ),
            "&amp;Open...", CTRL+Key_O, this, "open" );
    <a href="qobject.html#connect">connect</a>( fileOpenAction, SIGNAL( <a href="qaction.html#activated">activated</a>() ), this, SLOT( fileOpen() ) );

    fileSaveAction = new <a href="qaction.html">QAction</a>(
            "Save Chart", QPixmap( file_save ),
            "&amp;Save", CTRL+Key_S, this, "save" );
    <a href="qobject.html#connect">connect</a>( fileSaveAction, SIGNAL( <a href="qaction.html#activated">activated</a>() ), this, SLOT( fileSave() ) );

    fileSaveAsAction = new <a href="qaction.html">QAction</a>(
            "Save Chart As", QPixmap( file_save ),
            "Save &amp;As...", 0, this, "save as" );
    <a href="qobject.html#connect">connect</a>( fileSaveAsAction, SIGNAL( <a href="qaction.html#activated">activated</a>() ),
             this, SLOT( fileSaveAs() ) );

    fileSaveAsPixmapAction = new <a href="qaction.html">QAction</a>(
            "Save Chart As Bitmap", QPixmap( file_save ),
            "Save As &amp;Bitmap...", CTRL+Key_B, this, "save as bitmap" );
    <a href="qobject.html#connect">connect</a>( fileSaveAsPixmapAction, SIGNAL( <a href="qaction.html#activated">activated</a>() ),
             this, SLOT( fileSaveAsPixmap() ) );

    filePrintAction = new <a href="qaction.html">QAction</a>(
            "Print Chart", QPixmap( file_print ),
            "&amp;Print Chart...", CTRL+Key_P, this, "print chart" );
    <a href="qobject.html#connect">connect</a>( filePrintAction, SIGNAL( <a href="qaction.html#activated">activated</a>() ),
             this, SLOT( filePrint() ) );

    optionsSetDataAction = new <a href="qaction.html">QAction</a>(
            "Set Data", QPixmap( options_setdata ),
            "Set &amp;Data...", CTRL+Key_D, this, "set data" );
    <a href="qobject.html#connect">connect</a>( optionsSetDataAction, SIGNAL( <a href="qaction.html#activated">activated</a>() ),
             this, SLOT( optionsSetData() ) );


    <a href="qactiongroup.html">QActionGroup</a> *chartGroup = new <a href="qactiongroup.html">QActionGroup</a>( this ); // Connected later
<a name="x2489"></a>    chartGroup-&gt;<a href="qactiongroup.html#setExclusive">setExclusive</a>( TRUE );

    optionsPieChartAction = new <a href="qaction.html">QAction</a>(
            "Pie Chart", QPixmap( options_piechart ),
            "&amp;Pie Chart", CTRL+Key_I, chartGroup, "pie chart" );
<a name="x2487"></a>    optionsPieChartAction-&gt;<a href="qaction.html#setToggleAction">setToggleAction</a>( TRUE );

    optionsHorizontalBarChartAction = new <a href="qaction.html">QAction</a>(
            "Horizontal Bar Chart", QPixmap( options_horizontalbarchart ),
            "&amp;Horizontal Bar Chart", CTRL+Key_H, chartGroup,
            "horizontal bar chart" );
    optionsHorizontalBarChartAction-&gt;<a href="qaction.html#setToggleAction">setToggleAction</a>( TRUE );

    optionsVerticalBarChartAction = new <a href="qaction.html">QAction</a>(
            "Vertical Bar Chart", QPixmap( options_verticalbarchart ),
            "&amp;Vertical Bar Chart", CTRL+Key_V, chartGroup, "Vertical bar chart" );
    optionsVerticalBarChartAction-&gt;<a href="qaction.html#setToggleAction">setToggleAction</a>( TRUE );


    optionsSetFontAction = new <a href="qaction.html">QAction</a>(
            "Set Font", QPixmap( options_setfont ),
            "Set &amp;Font...", CTRL+Key_F, this, "set font" );
    <a href="qobject.html#connect">connect</a>( optionsSetFontAction, SIGNAL( <a href="qaction.html#activated">activated</a>() ),
             this, SLOT( optionsSetFont() ) );

    optionsSetOptionsAction = new <a href="qaction.html">QAction</a>(
            "Set Options", QPixmap( options_setoptions ),
            "Set &amp;Options...", 0, this, "set options" );
    <a href="qobject.html#connect">connect</a>( optionsSetOptionsAction, SIGNAL( <a href="qaction.html#activated">activated</a>() ),
             this, SLOT( optionsSetOptions() ) );

    fileQuitAction = new <a href="qaction.html">QAction</a>( "Quit", "&amp;Quit", CTRL+Key_Q, this, "quit" );
    <a href="qobject.html#connect">connect</a>( fileQuitAction, SIGNAL( <a href="qaction.html#activated">activated</a>() ), this, SLOT( fileQuit() ) );


    <a href="qtoolbar.html">QToolBar</a>* fileTools = new <a href="qtoolbar.html">QToolBar</a>( this, "file operations" );
<a name="x2510"></a>    fileTools-&gt;<a href="qtoolbar.html#setLabel">setLabel</a>( "File Operations" );
<a name="x2485"></a>    fileNewAction-&gt;<a href="qaction.html#addTo">addTo</a>( fileTools );
    fileOpenAction-&gt;<a href="qaction.html#addTo">addTo</a>( fileTools );
    fileSaveAction-&gt;<a href="qaction.html#addTo">addTo</a>( fileTools );
<a name="x2509"></a>    fileTools-&gt;<a href="qtoolbar.html#addSeparator">addSeparator</a>();
    filePrintAction-&gt;<a href="qaction.html#addTo">addTo</a>( fileTools );

    <a href="qtoolbar.html">QToolBar</a> *optionsTools = new <a href="qtoolbar.html">QToolBar</a>( this, "options operations" );
    optionsTools-&gt;<a href="qtoolbar.html#setLabel">setLabel</a>( "Options Operations" );
    optionsSetDataAction-&gt;<a href="qaction.html#addTo">addTo</a>( optionsTools );
    optionsTools-&gt;<a href="qtoolbar.html#addSeparator">addSeparator</a>();
    optionsPieChartAction-&gt;<a href="qaction.html#addTo">addTo</a>( optionsTools );
    optionsHorizontalBarChartAction-&gt;<a href="qaction.html#addTo">addTo</a>( optionsTools );
    optionsVerticalBarChartAction-&gt;<a href="qaction.html#addTo">addTo</a>( optionsTools );
    optionsTools-&gt;<a href="qtoolbar.html#addSeparator">addSeparator</a>();
    optionsSetFontAction-&gt;<a href="qaction.html#addTo">addTo</a>( optionsTools );
    optionsTools-&gt;<a href="qtoolbar.html#addSeparator">addSeparator</a>();
    optionsSetOptionsAction-&gt;<a href="qaction.html#addTo">addTo</a>( optionsTools );

    fileMenu = new <a href="qpopupmenu.html">QPopupMenu</a>( this );
    <a href="qmainwindow.html#menuBar">menuBar</a>()-&gt;insertItem( "&amp;File", fileMenu );
    fileNewAction-&gt;<a href="qaction.html#addTo">addTo</a>( fileMenu );
    fileOpenAction-&gt;<a href="qaction.html#addTo">addTo</a>( fileMenu );
    fileSaveAction-&gt;<a href="qaction.html#addTo">addTo</a>( fileMenu );
    fileSaveAsAction-&gt;<a href="qaction.html#addTo">addTo</a>( fileMenu );
    fileMenu-&gt;<a href="qmenudata.html#insertSeparator">insertSeparator</a>();
    fileSaveAsPixmapAction-&gt;<a href="qaction.html#addTo">addTo</a>( fileMenu );
    fileMenu-&gt;<a href="qmenudata.html#insertSeparator">insertSeparator</a>();
    filePrintAction-&gt;<a href="qaction.html#addTo">addTo</a>( fileMenu );
    fileMenu-&gt;<a href="qmenudata.html#insertSeparator">insertSeparator</a>();
    fileQuitAction-&gt;<a href="qaction.html#addTo">addTo</a>( fileMenu );

    optionsMenu = new <a href="qpopupmenu.html">QPopupMenu</a>( this );
    <a href="qmainwindow.html#menuBar">menuBar</a>()-&gt;insertItem( "&amp;Options", optionsMenu );
    optionsSetDataAction-&gt;<a href="qaction.html#addTo">addTo</a>( optionsMenu );
    optionsMenu-&gt;<a href="qmenudata.html#insertSeparator">insertSeparator</a>();
    optionsPieChartAction-&gt;<a href="qaction.html#addTo">addTo</a>( optionsMenu );
    optionsHorizontalBarChartAction-&gt;<a href="qaction.html#addTo">addTo</a>( optionsMenu );
    optionsVerticalBarChartAction-&gt;<a href="qaction.html#addTo">addTo</a>( optionsMenu );
    optionsMenu-&gt;<a href="qmenudata.html#insertSeparator">insertSeparator</a>();
    optionsSetFontAction-&gt;<a href="qaction.html#addTo">addTo</a>( optionsMenu );
    optionsMenu-&gt;<a href="qmenudata.html#insertSeparator">insertSeparator</a>();
    optionsSetOptionsAction-&gt;<a href="qaction.html#addTo">addTo</a>( optionsMenu );

    <a href="qmainwindow.html#menuBar">menuBar</a>()-&gt;insertSeparator();

    <a href="qpopupmenu.html">QPopupMenu</a> *helpMenu = new <a href="qpopupmenu.html">QPopupMenu</a>( this );
    <a href="qmainwindow.html#menuBar">menuBar</a>()-&gt;insertItem( "&amp;Help", helpMenu );
    helpMenu-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "&amp;Help", this, SLOT(helpHelp()), Key_F1 );
    helpMenu-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "&amp;About", this, SLOT(helpAbout()) );
    helpMenu-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( "About &amp;Qt", this, SLOT(helpAboutQt()) );


    m_printer = 0;
    m_elements.resize( MAX_ELEMENTS );

    <a href="qsettings.html">QSettings</a> settings;
<a name="x2505"></a>    settings.<a href="qsettings.html#insertSearchPath">insertSearchPath</a>( QSettings::Windows, WINDOWS_REGISTRY );
    int windowWidth = settings.<a href="qsettings.html#readNumEntry">readNumEntry</a>( APP_KEY + "WindowWidth", 460 );
    int windowHeight = settings.<a href="qsettings.html#readNumEntry">readNumEntry</a>( APP_KEY + "WindowHeight", 530 );
    int windowX = settings.<a href="qsettings.html#readNumEntry">readNumEntry</a>( APP_KEY + "WindowX", -1 );
    int windowY = settings.<a href="qsettings.html#readNumEntry">readNumEntry</a>( APP_KEY + "WindowY", -1 );
    setChartType( ChartType(
            settings.<a href="qsettings.html#readNumEntry">readNumEntry</a>( APP_KEY + "ChartType", int(PIE) ) ) );
    m_addValues = AddValuesType(
                    settings.<a href="qsettings.html#readNumEntry">readNumEntry</a>( APP_KEY + "AddValues", int(NO) ));
    m_decimalPlaces = settings.<a href="qsettings.html#readNumEntry">readNumEntry</a>( APP_KEY + "Decimals", 2 );
    m_font = QFont( "Helvetica", 18, QFont::Bold );
    m_font.fromString(
            settings.<a href="qsettings.html#readEntry">readEntry</a>( APP_KEY + "Font", m_font.toString() ) );
    for ( int i = 0; i &lt; MAX_RECENTFILES; ++i ) {
        <a href="qstring.html">QString</a> filename = settings.<a href="qsettings.html#readEntry">readEntry</a>( APP_KEY + "File" +
<a name="x2508"></a>                                               QString::<a href="qstring.html#number">number</a>( i + 1 ) );
<a name="x2507"></a>        if ( !filename.<a href="qstring.html#isEmpty">isEmpty</a>() )
            m_recentFiles.push_back( filename );
    }
    if ( m_recentFiles.count() )
        updateRecentFilesMenu();


    // Connect *after* we've set the chart type on so we don't call
    // drawElements() prematurely.
<a name="x2488"></a>    <a href="qobject.html#connect">connect</a>( chartGroup, SIGNAL( <a href="qactiongroup.html#selected">selected</a>(QAction*) ),
             this, SLOT( updateChartType(QAction*) ) );

    <a href="qwidget.html#resize">resize</a>( windowWidth, windowHeight );
    if ( windowX != -1 || windowY != -1 )
        <a href="qwidget.html#move">move</a>( windowX, windowY );

    m_canvas = new <a href="qcanvas.html">QCanvas</a>( this );
<a name="x2491"></a>    m_canvas-&gt;<a href="qcanvas.html#resize">resize</a>( <a href="qwidget.html#width">width</a>(), height() );
    m_canvasView = new CanvasView( m_canvas, &amp;m_elements, this );
    <a href="qmainwindow.html#setCentralWidget">setCentralWidget</a>( m_canvasView );
    m_canvasView-&gt;<a href="qwidget.html#show">show</a>();

    if ( !filename.<a href="qstring.html#isEmpty">isEmpty</a>() )
        load( filename );
    else {
        init();
        m_elements[0].set( 20, red,    14, "Red" );
        m_elements[1].set( 70, cyan,    2, "Cyan",   darkGreen );
        m_elements[2].set( 35, blue,   11, "Blue" );
        m_elements[3].set( 55, yellow,  1, "Yellow", darkBlue );
        m_elements[4].set( 80, magenta, 1, "Magenta" );
        drawElements();
    }

    <a href="qmainwindow.html#statusBar">statusBar</a>()-&gt;message( "Ready", 2000 );
}


ChartForm::~ChartForm()
{
    delete m_printer;
}


void <a name="f575"></a>ChartForm::init()
{
    <a href="qwidget.html#setCaption">setCaption</a>( "Chart" );
    m_filename = QString::null;
    m_changed = FALSE;

    m_elements[0]  = Element( Element::INVALID, red );
    m_elements[1]  = Element( Element::INVALID, cyan );
    m_elements[2]  = Element( Element::INVALID, blue );
    m_elements[3]  = Element( Element::INVALID, yellow );
    m_elements[4]  = Element( Element::INVALID, green );
    m_elements[5]  = Element( Element::INVALID, magenta );
    m_elements[6]  = Element( Element::INVALID, darkYellow );
    m_elements[7]  = Element( Element::INVALID, darkRed );
    m_elements[8]  = Element( Element::INVALID, darkCyan );
    m_elements[9]  = Element( Element::INVALID, darkGreen );
    m_elements[10] = Element( Element::INVALID, darkMagenta );
    m_elements[11] = Element( Element::INVALID, darkBlue );
    for ( int i = 12; i &lt; MAX_ELEMENTS; ++i ) {
        double x = (double(i) / MAX_ELEMENTS) * 360;
        int y = (int(x * 256) % 105) + 151;
        int z = ((i * 17) % 105) + 151;
        m_elements[i] = Element( Element::INVALID, QColor( int(x), y, z, QColor::Hsv ) );
    }
}

<a name="x2511"></a>void ChartForm::<a href="qwidget.html#closeEvent">closeEvent</a>( <a href="qcloseevent.html">QCloseEvent</a> * )
{
    fileQuit();
}


void <a name="f576"></a>ChartForm::fileNew()
{
    if ( okToClear() ) {
        init();
        drawElements();
    }
}


void <a name="f577"></a>ChartForm::fileOpen()
{
    if ( !okToClear() )
        return;

<a name="x2494"></a>    <a href="qstring.html">QString</a> filename = QFileDialog::<a href="qfiledialog.html#getOpenFileName">getOpenFileName</a>(
                            QString::null, "Charts (*.cht)", this,
                            "file open", "Chart -- File Open" );
    if ( !filename.<a href="qstring.html#isEmpty">isEmpty</a>() )
        load( filename );
    else
        <a href="qmainwindow.html#statusBar">statusBar</a>()-&gt;message( "File Open abandoned", 2000 );
}


void <a name="f578"></a>ChartForm::fileSaveAs()
{
    <a href="qstring.html">QString</a> filename = QFileDialog::<a href="qfiledialog.html#getSaveFileName">getSaveFileName</a>(
                            QString::null, "Charts (*.cht)", this,
                            "file save as", "Chart -- File Save As" );
    if ( !filename.<a href="qstring.html#isEmpty">isEmpty</a>() ) {
        int answer = 0;
<a name="x2493"></a>        if ( QFile::<a href="qfile.html#exists">exists</a>( filename ) )
<a name="x2504"></a>            answer = QMessageBox::<a href="qmessagebox.html#warning">warning</a>(
                            this, "Chart -- Overwrite File",
                            QString( "Overwrite\n\'%1\'?" ).
                                arg( filename ),
                            "&amp;Yes", "&amp;No", QString::null, 1, 1 );
        if ( answer == 0 ) {
            m_filename = filename;
            updateRecentFiles( filename );
            fileSave();
            return;
        }
    }
    <a href="qmainwindow.html#statusBar">statusBar</a>()-&gt;message( "Saving abandoned", 2000 );
}


void <a name="f579"></a>ChartForm::fileOpenRecent( int index )
{
    if ( !okToClear() )
        return;

    load( m_recentFiles[index] );
}


void <a name="f580"></a>ChartForm::updateRecentFiles( const <a href="qstring.html">QString</a>&amp; filename )
{
    if ( m_recentFiles.find( filename ) != m_recentFiles.end() )
        return;

    m_recentFiles.push_back( filename );
    if ( m_recentFiles.count() &gt; MAX_RECENTFILES )
        m_recentFiles.pop_front();

    updateRecentFilesMenu();
}


void <a name="f581"></a>ChartForm::updateRecentFilesMenu()
{
    for ( int i = 0; i &lt; MAX_RECENTFILES; ++i ) {
<a name="x2497"></a>        if ( fileMenu-&gt;<a href="qmenudata.html#findItem">findItem</a>( i ) )
<a name="x2500"></a>            fileMenu-&gt;<a href="qmenudata.html#removeItem">removeItem</a>( i );
        if ( i &lt; int(m_recentFiles.count()) )
            fileMenu-&gt;<a href="qmenudata.html#insertItem">insertItem</a>( QString( "&amp;%1 %2" ).
                                    arg( i + 1 ).arg( m_recentFiles[i] ),
                                  this, SLOT( fileOpenRecent(int) ),
                                  0, i );
    }
}


void <a name="f582"></a>ChartForm::fileQuit()
{
    if ( okToClear() ) {
        saveOptions();
<a name="x2490"></a>        qApp-&gt;<a href="qapplication.html#exit">exit</a>( 0 );
    }
}


bool <a name="f583"></a>ChartForm::okToClear()
{
    if ( m_changed ) {
        <a href="qstring.html">QString</a> msg;
        if ( m_filename.isEmpty() )
            msg = "Unnamed chart ";
        else
            msg = QString( "Chart '%1'\n" ).arg( m_filename );
        msg += "has been changed.";

        int x = QMessageBox::<a href="qmessagebox.html#information">information</a>( this, "Chart -- Unsaved Changes",
                                          msg, "&amp;Save", "Cancel", "&amp;Abandon",
                                          0, 1 );
        switch( x ) {
            case 0: // Save
                fileSave();
                break;
            case 1: // Cancel
            default:
                return FALSE;
                break;
            case 2: // Abandon
                break;
        }
    }

    return TRUE;
}


void <a name="f584"></a>ChartForm::saveOptions()
{
    <a href="qsettings.html">QSettings</a> settings;
    settings.<a href="qsettings.html#insertSearchPath">insertSearchPath</a>( QSettings::Windows, WINDOWS_REGISTRY );
<a name="x2506"></a>    settings.<a href="qsettings.html#writeEntry">writeEntry</a>( APP_KEY + "WindowWidth", width() );
    settings.<a href="qsettings.html#writeEntry">writeEntry</a>( APP_KEY + "WindowHeight", height() );
    settings.<a href="qsettings.html#writeEntry">writeEntry</a>( APP_KEY + "WindowX", x() );
    settings.<a href="qsettings.html#writeEntry">writeEntry</a>( APP_KEY + "WindowY", y() );
    settings.<a href="qsettings.html#writeEntry">writeEntry</a>( APP_KEY + "ChartType", int(m_chartType) );
    settings.<a href="qsettings.html#writeEntry">writeEntry</a>( APP_KEY + "AddValues", int(m_addValues) );
    settings.<a href="qsettings.html#writeEntry">writeEntry</a>( APP_KEY + "Decimals", m_decimalPlaces );
    settings.<a href="qsettings.html#writeEntry">writeEntry</a>( APP_KEY + "Font", m_font.toString() );
    for ( int i = 0; i &lt; int(m_recentFiles.count()); ++i )
        settings.<a href="qsettings.html#writeEntry">writeEntry</a>( APP_KEY + "File" + QString::number( i + 1 ),
                             m_recentFiles[i] );
}


void <a name="f585"></a>ChartForm::optionsSetData()
{
    SetDataForm *setDataForm = new SetDataForm( &amp;m_elements, m_decimalPlaces, this );
<a name="x2492"></a>    if ( setDataForm-&gt;<a href="qdialog.html#exec">exec</a>() ) {
        m_changed = TRUE;
        drawElements();
    }
    delete setDataForm;
}


void <a name="f586"></a>ChartForm::setChartType( ChartType chartType )
{
    m_chartType = chartType;
    switch ( m_chartType ) {
        case PIE:
<a name="x2486"></a>            optionsPieChartAction-&gt;<a href="qaction.html#setOn">setOn</a>( TRUE );
            break;
        case VERTICAL_BAR:
            optionsVerticalBarChartAction-&gt;<a href="qaction.html#setOn">setOn</a>( TRUE );
            break;
        case HORIZONTAL_BAR:
            optionsHorizontalBarChartAction-&gt;<a href="qaction.html#setOn">setOn</a>( TRUE );
            break;
    }
}


void <a name="f587"></a>ChartForm::updateChartType( <a href="qaction.html">QAction</a> *action )
{
    if ( action == optionsPieChartAction ) {
        m_chartType = PIE;
    }
    else if ( action == optionsHorizontalBarChartAction ) {
        m_chartType = HORIZONTAL_BAR;
    }
    else if ( action == optionsVerticalBarChartAction ) {
        m_chartType = VERTICAL_BAR;
    }

    drawElements();
}


void <a name="f588"></a>ChartForm::optionsSetFont()
{
    bool ok;
<a name="x2496"></a>    <a href="qfont.html">QFont</a> font = QFontDialog::<a href="qfontdialog.html#getFont">getFont</a>( &amp;ok, m_font, this );
    if ( ok ) {
        m_font = font;
        drawElements();
    }
}


void <a name="f589"></a>ChartForm::optionsSetOptions()
{
    OptionsForm *optionsForm = new OptionsForm( this );
    optionsForm-&gt;chartTypeComboBox-&gt;setCurrentItem( m_chartType );
    optionsForm-&gt;<a href="qwidget.html#setFont">setFont</a>( m_font );
    switch ( m_addValues ) {
        case NO:
            optionsForm-&gt;noRadioButton-&gt;setChecked( TRUE );
            break;
        case YES:
            optionsForm-&gt;yesRadioButton-&gt;setChecked( TRUE );
            break;
        case AS_PERCENTAGE:
            optionsForm-&gt;asPercentageRadioButton-&gt;setChecked( TRUE );
            break;
    }
    optionsForm-&gt;decimalPlacesSpinBox-&gt;setValue( m_decimalPlaces );
    if ( optionsForm-&gt;<a href="qdialog.html#exec">exec</a>() ) {
        setChartType( ChartType(
                optionsForm-&gt;chartTypeComboBox-&gt;currentItem()) );
<a name="x2512"></a>        m_font = optionsForm-&gt;<a href="qwidget.html#font">font</a>();
        if ( optionsForm-&gt;noRadioButton-&gt;isChecked() )
            m_addValues = NO;
        else if ( optionsForm-&gt;yesRadioButton-&gt;isChecked() )
            m_addValues = YES;
        else if ( optionsForm-&gt;asPercentageRadioButton-&gt;isChecked() )
            m_addValues = AS_PERCENTAGE;
        m_decimalPlaces = optionsForm-&gt;decimalPlacesSpinBox-&gt;value();
        drawElements();
    }
    delete optionsForm;
}


void <a name="f590"></a>ChartForm::helpHelp()
{
    <a href="qmainwindow.html#statusBar">statusBar</a>()-&gt;message( "Help is not implemented yet", 2000 );
}


void <a name="f591"></a>ChartForm::helpAbout()
{
<a name="x2501"></a>    QMessageBox::<a href="qmessagebox.html#about">about</a>( this, "Chart -- About",
                        "&lt;center&gt;&lt;h1&gt;&lt;font color=blue&gt;Chart&lt;font&gt;&lt;/h1&gt;&lt;/center&gt;"
                        "&lt;p&gt;Chart your data with &lt;i&gt;chart&lt;/i&gt;.&lt;/p&gt;"
                        );
}


void <a name="f592"></a>ChartForm::helpAboutQt()
{
<a name="x2502"></a>    QMessageBox::<a href="qmessagebox.html#aboutQt">aboutQt</a>( this, "Chart -- About Qt" );
}

</pre>

<p> <hr>
<p> Main:
<p> <pre>#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
#include "chartform.h"


int main( int argc, char *argv[] )
{
    <a href="qapplication.html">QApplication</a> app( argc, argv );

    <a href="qstring.html">QString</a> filename;
<a name="x2515"></a>    if ( app.<a href="qapplication.html#argc">argc</a>() &gt; 1 ) {
<a name="x2516"></a>        filename = app.<a href="qapplication.html#argv">argv</a>()[1];
<a name="x2519"></a>        if ( !filename.<a href="qstring.html#endsWith">endsWith</a>( ".cht" ) )
            filename = QString::null;
    }

    ChartForm *cf = new ChartForm( filename );
    app.<a href="qapplication.html#setMainWidget">setMainWidget</a>( cf );
    cf-&gt;<a href="qwidget.html#show">show</a>();

    return app.<a href="qapplication.html#exec">exec</a>();
}
</pre>

<p> <p>See also <a href="step-by-step-examples.html">Step-by-step Examples</a>.

<!-- eof -->
<p><address><hr><div align=center>
<table width=100% cellspacing=0 border=0><tr>
<td>Copyright &copy; 2002 
<a href="http://www.trolltech.com">Trolltech</a><td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a>
<td align=right><div align=right>Qt version 3.1.1</div>
</table></div></address></body>
</html>