summaryrefslogtreecommitdiffstats
path: root/qtruby/rubylib/examples/qt-examples/chart/chartform.rb
blob: 0ebff47eeeca74a1aef4fe4731f69a4e0548c06a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
class ChartForm < TQt::MainWindow
    
    slots 'fileNew()',
        'fileOpen()',
        'fileOpenRecent( int )',
        'fileSave()',
        'fileSaveAs()',
        'fileSaveAsPixmap()',
        'filePrint()',
        'fileQuit()',
        'optionsSetData()',
        'updateChartType( TQAction * )',
        'optionsSetFont()',
        'optionsSetOptions()',
        'helpHelp()',
        'helpAbout()',
        'helpAboutQt()',
        'saveOptions()'

	attr_accessor :changed
	attr_reader :chartType, :optionsMenu
	
    MAX_ELEMENTS = 100
    MAX_RECENTFILES = 9 # Must not exceed 9
    
    PIE = 0
    VERTICAL_BAR = 1
    HORIZONTAL_BAR = 2
    
    NO = 0
    YES = 1
    AS_PERCENTAGE = 2
    
    WINDOWS_REGISTRY = "/Trolltech/QtExamples"
    APP_KEY = "/Chart/"

    def initialize( filename )
        super( nil, nil, WDestructiveClose )
        @filename = filename
        setIcon( TQt::Pixmap.new( "images/options_piechart.xpm" ) )
    
        fileNewAction = TQt::Action.new(
            "New Chart", TQt::IconSet.new(TQt::Pixmap.new( "images/file_new.xpm" )),
            "&New", TQt::KeySequence.new(CTRL+Key_N), self, "new" )
        connect( fileNewAction, SIGNAL( 'activated()' ), self, SLOT( 'fileNew()' ) )
    
        fileOpenAction = TQt::Action.new(
            "Open Chart", TQt::IconSet.new(TQt::Pixmap.new( "images/file_open.xpm" )),
            "&Open...", TQt::KeySequence.new(CTRL+Key_O), self, "open" )
        connect( fileOpenAction, SIGNAL( 'activated()' ), self, SLOT( 'fileOpen()' ) )
    
        fileSaveAction = TQt::Action.new(
            "Save Chart", TQt::IconSet.new(TQt::Pixmap.new( "images/file_save.xpm" )),
            "&Save", TQt::KeySequence.new(CTRL+Key_S), self, "save" )
        connect( fileSaveAction, SIGNAL( 'activated()' ), self, SLOT( 'fileSave()' ) )
    
        fileSaveAsAction = TQt::Action.new(
            "Save Chart As", TQt::IconSet.new(TQt::Pixmap.new( "images/file_save.xpm" )),
            "Save &As...", TQt::KeySequence.new(0), self, "save as" )
        connect( fileSaveAsAction, SIGNAL( 'activated()' ),
            self, SLOT( 'fileSaveAs()' ) )
    
        fileSaveAsPixmapAction = TQt::Action.new(
            "Save Chart As Bitmap", TQt::IconSet.new(TQt::Pixmap.new( "images/file_save.xpm" )),
            "Save As &Bitmap...", TQt::KeySequence.new(CTRL+Key_B), self, "save as bitmap" )
        connect( fileSaveAsPixmapAction, SIGNAL( 'activated()' ),
            self, SLOT( 'fileSaveAsPixmap()' ) )
    
        filePrintAction = TQt::Action.new(
            "Print Chart", TQt::IconSet.new(TQt::Pixmap.new( "images/file_print.xpm" )),
            "&Print Chart...", TQt::KeySequence.new(CTRL+Key_P), self, "print chart" )
        connect( filePrintAction, SIGNAL( 'activated()' ),
            self, SLOT( 'filePrint()' ) )
    
        optionsSetDataAction = TQt::Action.new(
            "Set Data", TQt::IconSet.new(TQt::Pixmap.new( "images/options_setdata.xpm" )),
            "Set &Data...", TQt::KeySequence.new(CTRL+Key_D), self, "set data" )
        connect( optionsSetDataAction, SIGNAL( 'activated()' ),
            self, SLOT( 'optionsSetData()' ) )
    
    
        chartGroup = TQt::ActionGroup.new( self ) # Connected later
        chartGroup.setExclusive( true )
    
        @optionsPieChartAction = TQt::Action.new(
            "Pie Chart", TQt::IconSet.new(TQt::Pixmap.new( "images/options_piechart.xpm" )),
            "&Pie Chart", TQt::KeySequence.new(CTRL+Key_I), chartGroup, "pie chart" )
        @optionsPieChartAction.setToggleAction( true )
    
        @optionsHorizontalBarChartAction = TQt::Action.new(
            "Horizontal Bar Chart", TQt::IconSet.new(TQt::Pixmap.new( "images/options_horizontalbarchart.xpm" )),
            "&Horizontal Bar Chart", TQt::KeySequence.new(CTRL+Key_H), chartGroup,
            "horizontal bar chart" )
        @optionsHorizontalBarChartAction.setToggleAction( true )
    
        @optionsVerticalBarChartAction = TQt::Action.new(
            "Vertical Bar Chart", TQt::IconSet.new(TQt::Pixmap.new( "images/options_verticalbarchart.xpm" )),
            "&Vertical Bar Chart", TQt::KeySequence.new(CTRL+Key_V), chartGroup, "Vertical bar chart" )
        @optionsVerticalBarChartAction.setToggleAction( true )
    
    
        optionsSetFontAction = TQt::Action.new(
            "Set Font", TQt::IconSet.new(TQt::Pixmap.new( "images/options_setfont.xpm" )),
            "Set &Font...", TQt::KeySequence.new(CTRL+Key_F), self, "set font" )
        connect( optionsSetFontAction, SIGNAL( 'activated()' ),
            self, SLOT( 'optionsSetFont()' ) )
    
        optionsSetOptionsAction = TQt::Action.new(
            "Set Options", TQt::IconSet.new(TQt::Pixmap.new( "images/options_setoptions.xpm" )),
            "Set &Options...", TQt::KeySequence.new(0), self, "set options" )
        connect( optionsSetOptionsAction, SIGNAL( 'activated()' ),
            self, SLOT( 'optionsSetOptions()' ) )
    
        fileQuitAction = TQt::Action.new( "Quit", "&Quit", TQt::KeySequence.new(CTRL+Key_Q), self, "quit" )
        connect( fileQuitAction, SIGNAL( 'activated()' ), self, SLOT( 'fileQuit()' ) )
    
    
        fileTools = TQt::ToolBar.new( self, "file operations" )
        fileTools.setLabel( "File Operations" )
        fileNewAction.addTo( fileTools )
        fileOpenAction.addTo( fileTools )
        fileSaveAction.addTo( fileTools )
        fileTools.addSeparator()
        filePrintAction.addTo( fileTools )
    
        optionsTools = TQt::ToolBar.new( self, "options operations" )
        optionsTools.setLabel( "Options Operations" )
        optionsSetDataAction.addTo( optionsTools )
        optionsTools.addSeparator()
        @optionsPieChartAction.addTo( optionsTools )
        @optionsHorizontalBarChartAction.addTo( optionsTools )
        @optionsVerticalBarChartAction.addTo( optionsTools )
        optionsTools.addSeparator()
        optionsSetFontAction.addTo( optionsTools )
        optionsTools.addSeparator()
        optionsSetOptionsAction.addTo( optionsTools )
    
        @fileMenu = TQt::PopupMenu.new( self )
        menuBar().insertItem( "&File", @fileMenu )
        fileNewAction.addTo( @fileMenu )
        fileOpenAction.addTo( @fileMenu )
        fileSaveAction.addTo( @fileMenu )
        fileSaveAsAction.addTo( @fileMenu )
        @fileMenu.insertSeparator()
        fileSaveAsPixmapAction.addTo( @fileMenu )
        @fileMenu.insertSeparator()
        filePrintAction.addTo( @fileMenu )
        @fileMenu.insertSeparator()
        fileQuitAction.addTo( @fileMenu )
    
        @optionsMenu = TQt::PopupMenu.new( self )
        menuBar().insertItem( "&Options", @optionsMenu )
        optionsSetDataAction.addTo( @optionsMenu )
        @optionsMenu.insertSeparator()
        @optionsPieChartAction.addTo( @optionsMenu )
        @optionsHorizontalBarChartAction.addTo( @optionsMenu )
        @optionsVerticalBarChartAction.addTo( @optionsMenu )
        @optionsMenu.insertSeparator()
        optionsSetFontAction.addTo( @optionsMenu )
        @optionsMenu.insertSeparator()
        optionsSetOptionsAction.addTo( @optionsMenu )
    
        menuBar().insertSeparator()
    
        helpMenu = TQt::PopupMenu.new( self )
        menuBar().insertItem( "&Help", helpMenu )
        helpMenu.insertItem( "&Help", self, SLOT('helpHelp()'), TQt::KeySequence.new(Key_F1) )
        helpMenu.insertItem( "&About", self, SLOT('helpAbout()') )
        helpMenu.insertItem( "About &Qt", self, SLOT('helpAboutQt()') )
    
    
        @printer = nil
		@elements = Array.new(MAX_ELEMENTS)
    
        settings = TQt::Settings.new
        settings.insertSearchPath( TQt::Settings::Windows, WINDOWS_REGISTRY )
        windowWidth = settings.readNumEntry( APP_KEY + "WindowWidth", 460 )
        windowHeight = settings.readNumEntry( APP_KEY + "WindowHeight", 530 )
        windowX = settings.readNumEntry( APP_KEY + "WindowX", -1 )
        windowY = settings.readNumEntry( APP_KEY + "WindowY", -1 )
        setChartType( settings.readNumEntry( APP_KEY + "ChartType", PIE )  )
        @addValues = settings.readNumEntry( APP_KEY + "AddValues", NO )
        @decimalPlaces = settings.readNumEntry( APP_KEY + "Decimals", 2 )
        @font = TQt::Font.new( "Helvetica", 18, TQt::Font::Bold )
        @font.fromString(
            settings.readEntry( APP_KEY + "Font", @font.toString() ) )
        @recentFiles = []
        for i in 0...MAX_RECENTFILES
            filename = settings.readEntry( APP_KEY + "File" + ( i + 1 ).to_s )
            if !filename.nil?
                @recentFiles.push( filename )
            end
        end
        if @recentFiles.length() > 0
            updateRecentFilesMenu()
        end
    
    
        # Connect *after* we've set the chart type on so we don't call
        # drawElements() prematurely.
        connect( chartGroup, SIGNAL( 'selected(TQAction*)' ),
            self, SLOT( 'updateChartType(TQAction*)' ) )
    
        resize( windowWidth, windowHeight )
        if windowX != -1 || windowY != -1
            move( windowX, windowY )
        end
    
        @canvas = TQt::Canvas.new( self )
        @canvas.resize( width(), height() )
        @canvasView = CanvasView.new( @canvas, @elements, self )
        setCentralWidget( @canvasView )
        @canvasView.show()
    
        if ! @filename.nil?
            load( @filename )
        else
            init()
            @elements[0].set( 20, red,    14, "Red" )
            @elements[1].set( 70, cyan,    2, "Cyan",   darkGreen )
            @elements[2].set( 35, blue,   11, "Blue" )
            @elements[3].set( 55, yellow,  1, "Yellow", darkBlue )
            @elements[4].set( 80, magenta, 1, "Magenta" )
            drawElements()
        end
    
        statusBar().message( "Ready", 2000 )
    end
    
    
    
    def init()
        setCaption( "Chart" )
        @filename = nil
        @changed = false
    
        @elements[0]  = Element.new( Element::INVALID, red )
        @elements[1]  = Element.new( Element::INVALID, cyan )
        @elements[2]  = Element.new( Element::INVALID, blue )
        @elements[3]  = Element.new( Element::INVALID, yellow )
        @elements[4]  = Element.new( Element::INVALID, green )
        @elements[5]  = Element.new( Element::INVALID, magenta )
        @elements[6]  = Element.new( Element::INVALID, darkYellow )
        @elements[7]  = Element.new( Element::INVALID, darkRed )
        @elements[8]  = Element.new( Element::INVALID, darkCyan )
        @elements[9]  = Element.new( Element::INVALID, darkGreen )
        @elements[10] = Element.new( Element::INVALID, darkMagenta )
        @elements[11] = Element.new( Element::INVALID, darkBlue )
        for i in 12...MAX_ELEMENTS
            x = (i.to_f / MAX_ELEMENTS) * 360
            y = ((x * 256) % 105) + 151
            z = ((i * 17) % 105) + 151;
            @elements[i] = Element.new( Element::INVALID, TQt::Color.new( x, y, z, TQt::Color::Hsv ) )
        end
    end
    
    def closeEvent( e )
        fileQuit()
    end
    
    
    def fileNew()
        if okToClear()
            init()
            drawElements()
        end
    end
    
    
    def fileOpen()
        if !okToClear()
            return
        end
    
        filename = TQt::FileDialog.getOpenFileName(
                    nil, "Charts (*.cht)", self,
                    "file open", "Chart -- File Open" )
        if !filename.nil?
            load( filename )
        else
            statusBar().message( "File Open abandoned", 2000 )
        end
    end
    
    
    def fileSaveAs()
        filename = TQt::FileDialog.getSaveFileName(
                    nil, "Charts (*.cht)", self,
                    "file save as", "Chart -- File Save As" )
        if !filename.nil?
            answer = 0
            if TQt::File.exists( filename )
                answer = TQt::MessageBox.warning(
                        self, "Chart -- Overwrite File",
                        "Overwrite\n\'#{filename}\'?", 
                        "&Yes", "&No", nil, 1, 1 )
            end
            if answer == 0 
                @filename = filename
                updateRecentFiles( filename )
                fileSave()
                return
            end
        end
        statusBar().message( "Saving abandoned", 2000 )
    end
    
    
    def fileOpenRecent( index )
        if !okToClear()
            return
        end
    
        load( @recentFiles[index] )
    end
    
    
    def updateRecentFiles( filename )
        if @recentFiles.include?( filename )
            return
        end
    
        @recentFiles.push( filename )
        if @recentFiles.length() > MAX_RECENTFILES
            @recentFiles.shift()
        end
    
        updateRecentFilesMenu()
    end
    
    
    def updateRecentFilesMenu()
        for i in 0...MAX_RECENTFILES
            if @fileMenu.findItem( i )
                @fileMenu.removeItem( i )
            end
            if i < @recentFiles.length()
                @fileMenu.insertItem( "&%d %s" % [i + 1, @recentFiles[i]],
                    self, SLOT( 'fileOpenRecent(int)' ),
                    TQt::KeySequence.new(0), i )
            end
        end
    end
    
    
    def fileQuit()
        if okToClear()
            saveOptions()
            $qApp.exit( 0 )
        end
    end
    
    
    def okToClear()
        if @changed
            if @filename.nil?
                msg = "Unnamed chart "
            else
                msg = "Chart '#{@filename}'\n"
            end
            msg += "has been changed."
        
            x = TQt::MessageBox.information( self, "Chart -- Unsaved Changes",
                            msg, "&Save", "Cancel", "&Abandon",
                            0, 1 )
            case x 
                when 0 # Save
                    fileSave()
                when 1 # Cancel
                when 2 # Abandon
                else
                    return false
            end
        end
        return true
    end
    
    
    def saveOptions()
        settings = TQt::Settings.new
        settings.insertSearchPath( TQt::Settings::Windows, WINDOWS_REGISTRY )
        settings.writeEntry( APP_KEY + "WindowWidth", width() )
        settings.writeEntry( APP_KEY + "WindowHeight", height() )
        settings.writeEntry( APP_KEY + "WindowX", x() )
        settings.writeEntry( APP_KEY + "WindowY", y() )
        settings.writeEntry( APP_KEY + "ChartType", @chartType )
        settings.writeEntry( APP_KEY + "AddValues", @addValues )
        settings.writeEntry( APP_KEY + "Decimals", @decimalPlaces )
        settings.writeEntry( APP_KEY + "Font", @font.toString() )
        for i in 0...@recentFiles.length
            settings.writeEntry( APP_KEY + "File" + ( i + 1 ).to_s,
                        @recentFiles[i] )
        end
    end
    
    
    def optionsSetData()
        setDataForm = SetDataForm.new( @elements, @decimalPlaces, self )
        if setDataForm.exec()
            @changed = true
            drawElements()
        end
    end
    
    
    def setChartType( chartType )
        @chartType = chartType;
        case @chartType
        when PIE
            @optionsPieChartAction.setOn( true )
        when VERTICAL_BAR:
            @optionsVerticalBarChartAction.setOn( true )
        when HORIZONTAL_BAR:
            @optionsHorizontalBarChartAction.setOn( true )
        end
    end
    
    
    def updateChartType( action )
        if action == @optionsPieChartAction
            @chartType = PIE
        elsif action == @optionsHorizontalBarChartAction
            @chartType = HORIZONTAL_BAR
        elsif action == @optionsVerticalBarChartAction
            @chartType = VERTICAL_BAR
        end
    
        drawElements()
    end
    
    
    def optionsSetFont()
        ok = TQt::Boolean.new
        font = TQt::FontDialog.getFont( ok, @font, self )
        if !ok.nil?
            @font = font
            drawElements()
        end
    end
    
    
    def optionsSetOptions()
        optionsForm = OptionsForm.new( self )
        optionsForm.chartTypeComboBox.setCurrentItem( @chartType )
        optionsForm.font = @font
        case @addValues
        when NO
            optionsForm.noRadioButton.setChecked( true )
        when YES
            optionsForm.yesRadioButton.setChecked( true )
        when AS_PERCENTAGE
            optionsForm.asPercentageRadioButton.setChecked( true )
        end
        optionsForm.decimalPlacesSpinBox.setValue( @decimalPlaces )
        if optionsForm.exec()
            setChartType( optionsForm.chartTypeComboBox.currentItem() )
            @font = optionsForm.font
            if optionsForm.noRadioButton.isChecked()
                @addValues = NO
            elsif optionsForm.yesRadioButton.isChecked()
                @addValues = YES
            elsif optionsForm.asPercentageRadioButton.isChecked()
                @addValues = AS_PERCENTAGE
            end
            @decimalPlaces = optionsForm.decimalPlacesSpinBox.value()
            drawElements()
        end
    end
    
    
    def helpHelp()
        statusBar().message( "Help is not implemented yet", 2000 )
    end
    
    
    def helpAbout()
        TQt::MessageBox.about( self, "Chart -- About",
                "<center><h1><font color=blue>Chart<font></h1></center>" +
                "<p>Chart your data with <i>chart</i>.</p>" )
    end
    
    
    def helpAboutQt()
        TQt::MessageBox.aboutTQt( self, "Chart -- About Qt" )
    end

end