source: trunk/examples/chart/chartform.cpp@ 203

Last change on this file since 203 was 160, checked in by dmik, 19 years ago

Imported table and iconview modules and a bunch of dependent examples from the official release 3.3.1 from Trolltech.

  • Property svn:keywords set to Id
File size: 15.8 KB
Line 
1#include "canvasview.h"
2#include "chartform.h"
3#include "optionsform.h"
4#include "setdataform.h"
5
6#include <qaction.h>
7#include <qapplication.h>
8#include <qcombobox.h>
9#include <qfile.h>
10#include <qfiledialog.h>
11#include <qfont.h>
12#include <qfontdialog.h>
13#include <qmenubar.h>
14#include <qmessagebox.h>
15#include <qpixmap.h>
16#include <qpopupmenu.h>
17#include <qprinter.h>
18#include <qradiobutton.h>
19#include <qsettings.h>
20#include <qspinbox.h>
21#include <qstatusbar.h>
22#include <qtoolbar.h>
23#include <qtoolbutton.h>
24
25#include "images/file_new.xpm"
26#include "images/file_open.xpm"
27#include "images/file_save.xpm"
28#include "images/file_print.xpm"
29#include "images/options_setdata.xpm"
30#include "images/options_setfont.xpm"
31#include "images/options_setoptions.xpm"
32#include "images/options_horizontalbarchart.xpm"
33#include "images/options_piechart.xpm"
34#include "images/options_verticalbarchart.xpm"
35
36
37const QString WINDOWS_REGISTRY = "/Trolltech/QtExamples";
38const QString APP_KEY = "/Chart/";
39
40
41ChartForm::ChartForm( const QString& filename )
42 : QMainWindow( 0, 0, WDestructiveClose )
43{
44 setIcon( QPixmap( options_piechart ) );
45
46 QAction *fileNewAction;
47 QAction *fileOpenAction;
48 QAction *fileSaveAction;
49 QAction *fileSaveAsAction;
50 QAction *fileSaveAsPixmapAction;
51 QAction *filePrintAction;
52 QAction *fileQuitAction;
53 QAction *optionsSetDataAction;
54 QAction *optionsSetFontAction;
55 QAction *optionsSetOptionsAction;
56
57 fileNewAction = new QAction(
58 "New Chart", QPixmap( file_new ),
59 "&New", CTRL+Key_N, this, "new" );
60 connect( fileNewAction, SIGNAL( activated() ), this, SLOT( fileNew() ) );
61
62 fileOpenAction = new QAction(
63 "Open Chart", QPixmap( file_open ),
64 "&Open...", CTRL+Key_O, this, "open" );
65 connect( fileOpenAction, SIGNAL( activated() ), this, SLOT( fileOpen() ) );
66
67 fileSaveAction = new QAction(
68 "Save Chart", QPixmap( file_save ),
69 "&Save", CTRL+Key_S, this, "save" );
70 connect( fileSaveAction, SIGNAL( activated() ), this, SLOT( fileSave() ) );
71
72 fileSaveAsAction = new QAction(
73 "Save Chart As", QPixmap( file_save ),
74 "Save &As...", 0, this, "save as" );
75 connect( fileSaveAsAction, SIGNAL( activated() ),
76 this, SLOT( fileSaveAs() ) );
77
78 fileSaveAsPixmapAction = new QAction(
79 "Save Chart As Bitmap", QPixmap( file_save ),
80 "Save As &Bitmap...", CTRL+Key_B, this, "save as bitmap" );
81 connect( fileSaveAsPixmapAction, SIGNAL( activated() ),
82 this, SLOT( fileSaveAsPixmap() ) );
83
84 filePrintAction = new QAction(
85 "Print Chart", QPixmap( file_print ),
86 "&Print Chart...", CTRL+Key_P, this, "print chart" );
87 connect( filePrintAction, SIGNAL( activated() ),
88 this, SLOT( filePrint() ) );
89
90 optionsSetDataAction = new QAction(
91 "Set Data", QPixmap( options_setdata ),
92 "Set &Data...", CTRL+Key_D, this, "set data" );
93 connect( optionsSetDataAction, SIGNAL( activated() ),
94 this, SLOT( optionsSetData() ) );
95
96
97 QActionGroup *chartGroup = new QActionGroup( this ); // Connected later
98 chartGroup->setExclusive( TRUE );
99
100 optionsPieChartAction = new QAction(
101 "Pie Chart", QPixmap( options_piechart ),
102 "&Pie Chart", CTRL+Key_I, chartGroup, "pie chart" );
103 optionsPieChartAction->setToggleAction( TRUE );
104
105 optionsHorizontalBarChartAction = new QAction(
106 "Horizontal Bar Chart", QPixmap( options_horizontalbarchart ),
107 "&Horizontal Bar Chart", CTRL+Key_H, chartGroup,
108 "horizontal bar chart" );
109 optionsHorizontalBarChartAction->setToggleAction( TRUE );
110
111 optionsVerticalBarChartAction = new QAction(
112 "Vertical Bar Chart", QPixmap( options_verticalbarchart ),
113 "&Vertical Bar Chart", CTRL+Key_V, chartGroup, "Vertical bar chart" );
114 optionsVerticalBarChartAction->setToggleAction( TRUE );
115
116
117 optionsSetFontAction = new QAction(
118 "Set Font", QPixmap( options_setfont ),
119 "Set &Font...", CTRL+Key_F, this, "set font" );
120 connect( optionsSetFontAction, SIGNAL( activated() ),
121 this, SLOT( optionsSetFont() ) );
122
123 optionsSetOptionsAction = new QAction(
124 "Set Options", QPixmap( options_setoptions ),
125 "Set &Options...", 0, this, "set options" );
126 connect( optionsSetOptionsAction, SIGNAL( activated() ),
127 this, SLOT( optionsSetOptions() ) );
128
129 fileQuitAction = new QAction( "Quit", "&Quit", CTRL+Key_Q, this, "quit" );
130 connect( fileQuitAction, SIGNAL( activated() ), this, SLOT( fileQuit() ) );
131
132
133 QToolBar* fileTools = new QToolBar( this, "file operations" );
134 fileTools->setLabel( "File Operations" );
135 fileNewAction->addTo( fileTools );
136 fileOpenAction->addTo( fileTools );
137 fileSaveAction->addTo( fileTools );
138 fileTools->addSeparator();
139 filePrintAction->addTo( fileTools );
140
141 QToolBar *optionsTools = new QToolBar( this, "options operations" );
142 optionsTools->setLabel( "Options Operations" );
143 optionsSetDataAction->addTo( optionsTools );
144 optionsTools->addSeparator();
145 optionsPieChartAction->addTo( optionsTools );
146 optionsHorizontalBarChartAction->addTo( optionsTools );
147 optionsVerticalBarChartAction->addTo( optionsTools );
148 optionsTools->addSeparator();
149 optionsSetFontAction->addTo( optionsTools );
150 optionsTools->addSeparator();
151 optionsSetOptionsAction->addTo( optionsTools );
152
153 fileMenu = new QPopupMenu( this );
154 menuBar()->insertItem( "&File", fileMenu );
155 fileNewAction->addTo( fileMenu );
156 fileOpenAction->addTo( fileMenu );
157 fileSaveAction->addTo( fileMenu );
158 fileSaveAsAction->addTo( fileMenu );
159 fileMenu->insertSeparator();
160 fileSaveAsPixmapAction->addTo( fileMenu );
161 fileMenu->insertSeparator();
162 filePrintAction->addTo( fileMenu );
163 fileMenu->insertSeparator();
164 fileQuitAction->addTo( fileMenu );
165
166 optionsMenu = new QPopupMenu( this );
167 menuBar()->insertItem( "&Options", optionsMenu );
168 optionsSetDataAction->addTo( optionsMenu );
169 optionsMenu->insertSeparator();
170 optionsPieChartAction->addTo( optionsMenu );
171 optionsHorizontalBarChartAction->addTo( optionsMenu );
172 optionsVerticalBarChartAction->addTo( optionsMenu );
173 optionsMenu->insertSeparator();
174 optionsSetFontAction->addTo( optionsMenu );
175 optionsMenu->insertSeparator();
176 optionsSetOptionsAction->addTo( optionsMenu );
177
178 menuBar()->insertSeparator();
179
180 QPopupMenu *helpMenu = new QPopupMenu( this );
181 menuBar()->insertItem( "&Help", helpMenu );
182 helpMenu->insertItem( "&Help", this, SLOT(helpHelp()), Key_F1 );
183 helpMenu->insertItem( "&About", this, SLOT(helpAbout()) );
184 helpMenu->insertItem( "About &Qt", this, SLOT(helpAboutQt()) );
185
186
187 m_printer = 0;
188 m_elements.resize( MAX_ELEMENTS );
189
190 QSettings settings;
191 settings.insertSearchPath( QSettings::Windows, WINDOWS_REGISTRY );
192 int windowWidth = settings.readNumEntry( APP_KEY + "WindowWidth", 460 );
193 int windowHeight = settings.readNumEntry( APP_KEY + "WindowHeight", 530 );
194 int windowX = settings.readNumEntry( APP_KEY + "WindowX", -1 );
195 int windowY = settings.readNumEntry( APP_KEY + "WindowY", -1 );
196 setChartType( ChartType(
197 settings.readNumEntry( APP_KEY + "ChartType", int(PIE) ) ) );
198 m_addValues = AddValuesType(
199 settings.readNumEntry( APP_KEY + "AddValues", int(NO) ));
200 m_decimalPlaces = settings.readNumEntry( APP_KEY + "Decimals", 2 );
201 m_font = QFont( "Helvetica", 18, QFont::Bold );
202 m_font.fromString(
203 settings.readEntry( APP_KEY + "Font", m_font.toString() ) );
204 for ( int i = 0; i < MAX_RECENTFILES; ++i ) {
205 QString filename = settings.readEntry( APP_KEY + "File" +
206 QString::number( i + 1 ) );
207 if ( !filename.isEmpty() )
208 m_recentFiles.push_back( filename );
209 }
210 if ( m_recentFiles.count() )
211 updateRecentFilesMenu();
212
213
214 // Connect *after* we've set the chart type on so we don't call
215 // drawElements() prematurely.
216 connect( chartGroup, SIGNAL( selected(QAction*) ),
217 this, SLOT( updateChartType(QAction*) ) );
218
219 resize( windowWidth, windowHeight );
220 if ( windowX != -1 || windowY != -1 )
221 move( windowX, windowY );
222
223 m_canvas = new QCanvas( this );
224 m_canvas->resize( width(), height() );
225 m_canvasView = new CanvasView( m_canvas, &m_elements, this );
226 setCentralWidget( m_canvasView );
227 m_canvasView->show();
228
229 if ( !filename.isEmpty() )
230 load( filename );
231 else {
232 init();
233 m_elements[0].set( 20, red, 14, "Red" );
234 m_elements[1].set( 70, cyan, 2, "Cyan", darkGreen );
235 m_elements[2].set( 35, blue, 11, "Blue" );
236 m_elements[3].set( 55, yellow, 1, "Yellow", darkBlue );
237 m_elements[4].set( 80, magenta, 1, "Magenta" );
238 drawElements();
239 }
240
241 statusBar()->message( "Ready", 2000 );
242}
243
244
245ChartForm::~ChartForm()
246{
247 delete m_printer;
248}
249
250
251void ChartForm::init()
252{
253 setCaption( "Chart" );
254 m_filename = QString::null;
255 m_changed = FALSE;
256
257 m_elements[0] = Element( Element::INVALID, red );
258 m_elements[1] = Element( Element::INVALID, cyan );
259 m_elements[2] = Element( Element::INVALID, blue );
260 m_elements[3] = Element( Element::INVALID, yellow );
261 m_elements[4] = Element( Element::INVALID, green );
262 m_elements[5] = Element( Element::INVALID, magenta );
263 m_elements[6] = Element( Element::INVALID, darkYellow );
264 m_elements[7] = Element( Element::INVALID, darkRed );
265 m_elements[8] = Element( Element::INVALID, darkCyan );
266 m_elements[9] = Element( Element::INVALID, darkGreen );
267 m_elements[10] = Element( Element::INVALID, darkMagenta );
268 m_elements[11] = Element( Element::INVALID, darkBlue );
269 for ( int i = 12; i < MAX_ELEMENTS; ++i ) {
270 double x = (double(i) / MAX_ELEMENTS) * 360;
271 int y = (int(x * 256) % 105) + 151;
272 int z = ((i * 17) % 105) + 151;
273 m_elements[i] = Element( Element::INVALID, QColor( int(x), y, z, QColor::Hsv ) );
274 }
275}
276
277void ChartForm::closeEvent( QCloseEvent * )
278{
279 fileQuit();
280}
281
282
283void ChartForm::fileNew()
284{
285 if ( okToClear() ) {
286 init();
287 drawElements();
288 }
289}
290
291
292void ChartForm::fileOpen()
293{
294 if ( !okToClear() )
295 return;
296
297 QString filename = QFileDialog::getOpenFileName(
298 QString::null, "Charts (*.cht)", this,
299 "file open", "Chart -- File Open" );
300 if ( !filename.isEmpty() )
301 load( filename );
302 else
303 statusBar()->message( "File Open abandoned", 2000 );
304}
305
306
307void ChartForm::fileSaveAs()
308{
309 QString filename = QFileDialog::getSaveFileName(
310 QString::null, "Charts (*.cht)", this,
311 "file save as", "Chart -- File Save As" );
312 if ( !filename.isEmpty() ) {
313 int answer = 0;
314 if ( QFile::exists( filename ) )
315 answer = QMessageBox::warning(
316 this, "Chart -- Overwrite File",
317 QString( "Overwrite\n\'%1\'?" ).
318 arg( filename ),
319 "&Yes", "&No", QString::null, 1, 1 );
320 if ( answer == 0 ) {
321 m_filename = filename;
322 updateRecentFiles( filename );
323 fileSave();
324 return;
325 }
326 }
327 statusBar()->message( "Saving abandoned", 2000 );
328}
329
330
331void ChartForm::fileOpenRecent( int index )
332{
333 if ( !okToClear() )
334 return;
335
336 load( m_recentFiles[index] );
337}
338
339
340void ChartForm::updateRecentFiles( const QString& filename )
341{
342 if ( m_recentFiles.find( filename ) != m_recentFiles.end() )
343 return;
344
345 m_recentFiles.push_back( filename );
346 if ( m_recentFiles.count() > MAX_RECENTFILES )
347 m_recentFiles.pop_front();
348
349 updateRecentFilesMenu();
350}
351
352
353void ChartForm::updateRecentFilesMenu()
354{
355 for ( int i = 0; i < MAX_RECENTFILES; ++i ) {
356 if ( fileMenu->findItem( i ) )
357 fileMenu->removeItem( i );
358 if ( i < int(m_recentFiles.count()) )
359 fileMenu->insertItem( QString( "&%1 %2" ).
360 arg( i + 1 ).arg( m_recentFiles[i] ),
361 this, SLOT( fileOpenRecent(int) ),
362 0, i );
363 }
364}
365
366
367void ChartForm::fileQuit()
368{
369 if ( okToClear() ) {
370 saveOptions();
371 qApp->exit( 0 );
372 }
373}
374
375
376bool ChartForm::okToClear()
377{
378 if ( m_changed ) {
379 QString msg;
380 if ( m_filename.isEmpty() )
381 msg = "Unnamed chart ";
382 else
383 msg = QString( "Chart '%1'\n" ).arg( m_filename );
384 msg += "has been changed.";
385
386 int x = QMessageBox::information( this, "Chart -- Unsaved Changes",
387 msg, "&Save", "Cancel", "&Abandon",
388 0, 1 );
389 switch( x ) {
390 case 0: // Save
391 fileSave();
392 break;
393 case 1: // Cancel
394 default:
395 return FALSE;
396 case 2: // Abandon
397 break;
398 }
399 }
400
401 return TRUE;
402}
403
404
405void ChartForm::saveOptions()
406{
407 QSettings settings;
408 settings.insertSearchPath( QSettings::Windows, WINDOWS_REGISTRY );
409 settings.writeEntry( APP_KEY + "WindowWidth", width() );
410 settings.writeEntry( APP_KEY + "WindowHeight", height() );
411 settings.writeEntry( APP_KEY + "WindowX", x() );
412 settings.writeEntry( APP_KEY + "WindowY", y() );
413 settings.writeEntry( APP_KEY + "ChartType", int(m_chartType) );
414 settings.writeEntry( APP_KEY + "AddValues", int(m_addValues) );
415 settings.writeEntry( APP_KEY + "Decimals", m_decimalPlaces );
416 settings.writeEntry( APP_KEY + "Font", m_font.toString() );
417 for ( int i = 0; i < int(m_recentFiles.count()); ++i )
418 settings.writeEntry( APP_KEY + "File" + QString::number( i + 1 ),
419 m_recentFiles[i] );
420}
421
422
423void ChartForm::optionsSetData()
424{
425 SetDataForm *setDataForm = new SetDataForm( &m_elements, m_decimalPlaces, this );
426 if ( setDataForm->exec() ) {
427 m_changed = TRUE;
428 drawElements();
429 }
430 delete setDataForm;
431}
432
433
434void ChartForm::setChartType( ChartType chartType )
435{
436 m_chartType = chartType;
437 switch ( m_chartType ) {
438 case PIE:
439 optionsPieChartAction->setOn( TRUE );
440 break;
441 case VERTICAL_BAR:
442 optionsVerticalBarChartAction->setOn( TRUE );
443 break;
444 case HORIZONTAL_BAR:
445 optionsHorizontalBarChartAction->setOn( TRUE );
446 break;
447 }
448}
449
450
451void ChartForm::updateChartType( QAction *action )
452{
453 if ( action == optionsPieChartAction ) {
454 m_chartType = PIE;
455 }
456 else if ( action == optionsHorizontalBarChartAction ) {
457 m_chartType = HORIZONTAL_BAR;
458 }
459 else if ( action == optionsVerticalBarChartAction ) {
460 m_chartType = VERTICAL_BAR;
461 }
462
463 drawElements();
464}
465
466
467void ChartForm::optionsSetFont()
468{
469 bool ok;
470 QFont font = QFontDialog::getFont( &ok, m_font, this );
471 if ( ok ) {
472 m_font = font;
473 drawElements();
474 }
475}
476
477
478void ChartForm::optionsSetOptions()
479{
480 OptionsForm *optionsForm = new OptionsForm( this );
481 optionsForm->chartTypeComboBox->setCurrentItem( m_chartType );
482 optionsForm->setFont( m_font );
483 switch ( m_addValues ) {
484 case NO:
485 optionsForm->noRadioButton->setChecked( TRUE );
486 break;
487 case YES:
488 optionsForm->yesRadioButton->setChecked( TRUE );
489 break;
490 case AS_PERCENTAGE:
491 optionsForm->asPercentageRadioButton->setChecked( TRUE );
492 break;
493 }
494 optionsForm->decimalPlacesSpinBox->setValue( m_decimalPlaces );
495 if ( optionsForm->exec() ) {
496 setChartType( ChartType(
497 optionsForm->chartTypeComboBox->currentItem()) );
498 m_font = optionsForm->font();
499 if ( optionsForm->noRadioButton->isChecked() )
500 m_addValues = NO;
501 else if ( optionsForm->yesRadioButton->isChecked() )
502 m_addValues = YES;
503 else if ( optionsForm->asPercentageRadioButton->isChecked() )
504 m_addValues = AS_PERCENTAGE;
505 m_decimalPlaces = optionsForm->decimalPlacesSpinBox->value();
506 drawElements();
507 }
508 delete optionsForm;
509}
510
511
512void ChartForm::helpHelp()
513{
514 statusBar()->message( "Help is not implemented yet", 2000 );
515}
516
517
518void ChartForm::helpAbout()
519{
520 QMessageBox::about( this, "Chart -- About",
521 "<center><h1><font color=blue>Chart<font></h1></center>"
522 "<p>Chart your data with <i>chart</i>.</p>"
523 );
524}
525
526
527void ChartForm::helpAboutQt()
528{
529 QMessageBox::aboutQt( this, "Chart -- About Qt" );
530}
531
Note: See TracBrowser for help on using the repository browser.