summaryrefslogtreecommitdiffstats
path: root/qtruby/rubylib/examples/qt-examples/chart/chartform_files.rb
blob: 648aba62faa42f749342df0b6d267fe5eabba92a (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
class ChartForm

    def load( filename )
        file = Qt::File.new( filename )
        if  !file.open( Qt::IO_ReadOnly ) 
            statusBar().message( "Failed to load \'%s\'" % filename, 2000 )
            return
        end
    
        init() # Make sure we have colours
        @filename = filename
        ts = Qt::TextStream.new( file )
        errors = 0
        i = 0
        while !ts.eof() 
            element = Element.new
            ts >> element
            if  element.isValid()
                @elements[i] = element
                i += 1
            else
                errors += 1
            end
            if  i == MAX_ELEMENTS 
                statusBar().message("Read maximum number of elements (%d) discarding others" % i, 2000 )
                break
            end
        end
    
        file.close()
    
        bad = ""
        if errors > 0
            bad = " skipped %d bad record" % errors
            if  errors > 1
                bad += "s"
            end
        end
        statusBar().message( "Read %d values from \'%s\'" % [i, filename], 3000 )
    
        setCaption( "Chart -- %s" % filename )
        updateRecentFiles( filename )
    
        drawElements()
        @changed = false
    end
    
    
    def fileSave()
        if  @filename.nil? 
            fileSaveAs()
            return
        end
    
        file = Qt::File.new( @filename )
        if  !file.open( Qt::IO_WriteOnly ) 
            statusBar().message( "Failed to save \'%s\'" % @filename, 2000 )
            return
        end
        ts = Qt::TextStream.new( file )
        for i in 0...MAX_ELEMENTS
            if  @elements[i].isValid()
                ts << @elements[i]
            end
        end
    
        file.close()
    
        setCaption( "Chart -- %s" % @filename )
        statusBar().message( "Saved \'%s\'" % @filename, 2000 )
        @changed = false
    end
    
    
    def fileSaveAsPixmap()
        filename = Qt::FileDialog.getSaveFileName(nil, "Images (*.png *.xpm *.jpg)",
                                self, "file save as bitmap",
                                "Chart -- File Save As Bitmap" )
        if Qt::Pixmap.grabWidget( @canvasView ).save( filename,
                    filename.sub(/.*\.([^.]*)$/, '\1').upcase() ) 
            statusBar().message( "Wrote \'%s\'" % filename, 2000 )
        else
            statusBar().message( "Failed to write \'%s\'" % filename, 2000 )
        end
    end
    
    def filePrint()
        if  !@printer
            @printer = Qt::Printer.new
        end
        if  @printer.setup() 
            painter = Qt::Painter.new( @printer )
            @canvas.drawArea( Qt::Rect.new( 0, 0, @canvas.width(), @canvas.height() ),
                                painter, false )
            if  !@printer.outputFileName().empty? 
                statusBar().message( "Printed \'%s\'" % @printer.outputFileName(), 2000 )
            end
        end
    end

end