source: trunk/examples/chart/chartform_files.cpp

Last change on this file was 165, checked in by dmik, 19 years ago

Examples: Added #ifndef QT_NO_PRINTER to allow building when no printer support is available.

  • Property svn:keywords set to Id
File size: 2.9 KB
Line 
1#include "canvasview.h"
2#include "chartform.h"
3
4#include <qfile.h>
5#include <qfiledialog.h>
6#include <qpainter.h>
7#include <qprinter.h>
8#include <qstatusbar.h>
9
10
11void ChartForm::load( const QString& filename )
12{
13 QFile file( filename );
14 if ( !file.open( IO_ReadOnly ) ) {
15 statusBar()->message( QString( "Failed to load \'%1\'" ).
16 arg( filename ), 2000 );
17 return;
18 }
19
20 init(); // Make sure we have colours
21 m_filename = filename;
22 QTextStream ts( &file );
23 Element element;
24 int errors = 0;
25 int i = 0;
26 while ( !ts.eof() ) {
27 ts >> element;
28 if ( element.isValid() )
29 m_elements[i++] = element;
30 else
31 errors++;
32 if ( i == MAX_ELEMENTS ) {
33 statusBar()->message(
34 QString( "Read maximum number of elements (%1)"
35 " discarding others" ).arg( i ), 2000 );
36 break;
37 }
38 }
39
40 file.close();
41
42 QString bad = "";
43 if ( errors ) {
44 bad = QString( "; skipped " ) + QString::number( errors ) + " bad record";
45 if ( errors > 1 )
46 bad += "s";
47 }
48 statusBar()->message( QString( "Read %1 values from \'%2\'%3" ).
49 arg( i ).arg( filename ).arg( bad ), 3000 );
50
51 setCaption( QString( "Chart -- %1" ).arg( filename ) );
52 updateRecentFiles( filename );
53
54 drawElements();
55 m_changed = FALSE;
56}
57
58
59void ChartForm::fileSave()
60{
61 if ( m_filename.isEmpty() ) {
62 fileSaveAs();
63 return;
64 }
65
66 QFile file( m_filename );
67 if ( !file.open( IO_WriteOnly ) ) {
68 statusBar()->message( QString( "Failed to save \'%1\'" ).
69 arg( m_filename ), 2000 );
70 return;
71 }
72 QTextStream ts( &file );
73 for ( int i = 0; i < MAX_ELEMENTS; ++i )
74 if ( m_elements[i].isValid() )
75 ts << m_elements[i];
76
77 file.close();
78
79 setCaption( QString( "Chart -- %1" ).arg( m_filename ) );
80 statusBar()->message( QString( "Saved \'%1\'" ).arg( m_filename ), 2000 );
81 m_changed = FALSE;
82}
83
84
85void ChartForm::fileSaveAsPixmap()
86{
87 QString filename = QFileDialog::getSaveFileName(
88 QString::null, "Images (*.png *.xpm *.jpg)",
89 this, "file save as bitmap",
90 "Chart -- File Save As Bitmap" );
91 if ( QPixmap::grabWidget( m_canvasView ).
92 save( filename,
93 filename.mid( filename.findRev( '.' ) + 1 ).upper() ) )
94 statusBar()->message( QString( "Wrote \'%1\'" ).arg( filename ), 2000 );
95 else
96 statusBar()->message( QString( "Failed to write \'%1\'" ).
97 arg( filename ), 2000 );
98}
99
100void ChartForm::filePrint()
101{
102#ifndef QT_NO_PRINTER
103 if ( !m_printer )
104 m_printer = new QPrinter;
105 if ( m_printer->setup() ) {
106 QPainter painter( m_printer );
107 m_canvas->drawArea( QRect( 0, 0, m_canvas->width(), m_canvas->height() ),
108 &painter, FALSE );
109 if ( !m_printer->outputFileName().isEmpty() )
110 statusBar()->message( QString( "Printed \'%1\'" ).
111 arg( m_printer->outputFileName() ), 2000 );
112 }
113#endif
114}
115
Note: See TracBrowser for help on using the repository browser.