Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions

File Handling

(Extracts from chartform_files.cpp.)

Reading Chart Data

    void ChartForm::load( const TQString& filename )
    {
        TQFile file( filename );
        if ( !file.open( IO_ReadOnly ) ) {
            statusBar()->message( TQString( "Failed to load \'%1\'" ).
                                    arg( filename ), 2000 );
            return;
        }

        init(); // Make sure we have colours
        m_filename = filename;
        TQTextStream ts( &file );
        Element element;
        int errors = 0;
        int i = 0;
        while ( !ts.eof() ) {
            ts >> element;
            if ( element.isValid() )
                m_elements[i++] = element;
        file.close();

        setCaption( TQString( "Chart -- %1" ).arg( filename ) );
        updateRecentFiles( filename );

        drawElements();
        m_changed = FALSE;
    }

Loading a data set is very easy. We open the file and create a text stream. While there's data to read we stream an element into element and if it is valid we insert it into the m_elements vector. All the detail is handled by the Element class. Then we close the file and update the caption and the recent files list. Finally we draw the chart and mark it as unchanged.

Writing Chart Data

    void ChartForm::fileSave()
    {
        TQFile file( m_filename );
        if ( !file.open( IO_WriteOnly ) ) {
            statusBar()->message( TQString( "Failed to save \'%1\'" ).
                                    arg( m_filename ), 2000 );
            return;
        }
        TQTextStream ts( &file );
        for ( int i = 0; i < MAX_ELEMENTS; ++i )
            if ( m_elements[i].isValid() )
                ts << m_elements[i];

        file.close();

        setCaption( TQString( "Chart -- %1" ).arg( m_filename ) );
        statusBar()->message( TQString( "Saved \'%1\'" ).arg( m_filename ), 2000 );
        m_changed = FALSE;
    }

Saving data is equally easy. We open the file and create a text stream. We then stream every valid element to the text stream. All the detail is handled by the Element class.

« Canvas Control | Contents | Taking Data »


Copyright © 2007 TrolltechTrademarks
TQt 3.3.8