Class: MdiChild

Inherits:
Qt::TextEdit
  • Object
show all
Defined in:
ext/ruby/qtruby/examples/mainwindows/mdi/mdichild.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMdiChild

Returns a new instance of MdiChild.



34
35
36
37
38
# File 'ext/ruby/qtruby/examples/mainwindows/mdi/mdichild.rb', line 34

def initialize()
	super
    setAttribute(Qt::WA_DeleteOnClose)
    @isUntitled = true
end

Instance Attribute Details

#currentFileObject (readonly)

Returns the value of attribute currentFile.



30
31
32
# File 'ext/ruby/qtruby/examples/mainwindows/mdi/mdichild.rb', line 30

def currentFile
  @currentFile
end

Instance Method Details

#closeEvent(event) ⇒ Object



111
112
113
114
115
116
117
# File 'ext/ruby/qtruby/examples/mainwindows/mdi/mdichild.rb', line 111

def closeEvent(event)
    if maybeSave()
        event.accept()
    else
        event.ignore()
    end
end

#documentWasModifiedObject



119
120
121
# File 'ext/ruby/qtruby/examples/mainwindows/mdi/mdichild.rb', line 119

def documentWasModified()
    setWindowModified(document().isModified())
end

#loadFile(fileName) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'ext/ruby/qtruby/examples/mainwindows/mdi/mdichild.rb', line 51

def loadFile(fileName)
    file = Qt::File.new(fileName)
    if !file.open(Qt::File::ReadOnly | Qt::File::Text)
        Qt::MessageBox.warning(self, tr("MDI"),
                             tr("Cannot read file %s:\n%s." % [fileName, file.errorString]))
        return false
    end

    inf = Qt::TextStream.new(file)
    Qt::Application.overrideCursor = Qt::Cursor.new(Qt::WaitCursor)
    setPlainText(inf.readAll())
    Qt::Application.restoreOverrideCursor()

    setCurrentFile(fileName)

    connect(document(), SIGNAL('contentsChanged()'),
            self, SLOT('documentWasModified()'))

    return true
end

#maybeSaveObject



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'ext/ruby/qtruby/examples/mainwindows/mdi/mdichild.rb', line 123

def maybeSave()
    if document().isModified()
        ret = Qt::MessageBox::warning(self, tr("MDI"),
                     tr("'%s' has been modified.\n" \
                        "Do you want to save your changes?" % 
                             userFriendlyCurrentFile()),
                     Qt::MessageBox::Yes | Qt::MessageBox::Default,
                     Qt::MessageBox::No,
                     Qt::MessageBox::Cancel | Qt::MessageBox::Escape)
        if ret == Qt::MessageBox::Yes
            return save()
        elsif ret == Qt::MessageBox::Cancel
            return false
		end
    end
    return true
end

#newFileObject



40
41
42
43
44
45
46
47
48
49
# File 'ext/ruby/qtruby/examples/mainwindows/mdi/mdichild.rb', line 40

def newFile()
    @@sequenceNumber = 1

    @isUntitled = true
    @currentFile = tr("document%s.txt" % @@sequenceNumber += 1)
    setWindowTitle(@currentFile + "[*]")

    connect(document(), SIGNAL('contentsChanged()'),
            self, SLOT('documentWasModified()'))
end

#saveObject



72
73
74
75
76
77
78
# File 'ext/ruby/qtruby/examples/mainwindows/mdi/mdichild.rb', line 72

def save()
    if @isUntitled
        return saveAs()
    else
        return saveFile(@currentFile)
    end
end

#saveAsObject



80
81
82
83
84
85
86
87
88
# File 'ext/ruby/qtruby/examples/mainwindows/mdi/mdichild.rb', line 80

def saveAs()
    fileName = Qt::FileDialog.getSaveFileName(self, tr("Save As"),
                                                    @currentFile)
    if fileName.empty?
        return false
	end

    return saveFile(fileName)
end

#saveFile(fileName) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'ext/ruby/qtruby/examples/mainwindows/mdi/mdichild.rb', line 90

def saveFile(fileName)
    file = Qt::File.new(fileName)
    if !file.open(Qt::File::WriteOnly | Qt::File::Text)
        Qt::MessageBox::warning(self, tr("MDI"),
                             tr("Cannot write file %s:\n%s." % [fileName, file.errorString]))
        return false
    end

    outf = Qt::TextStream.new(file)
    Qt::Application.setOverrideCursor(Qt::WaitCursor)
    outf << toPlainText()
    Qt::Application.restoreOverrideCursor()

    setCurrentFile(fileName)
    return true
end

#setCurrentFile(fileName) ⇒ Object



141
142
143
144
145
146
147
# File 'ext/ruby/qtruby/examples/mainwindows/mdi/mdichild.rb', line 141

def setCurrentFile(fileName)
    @currentFile = Qt::FileInfo.new(fileName).canonicalFilePath()
    @isUntitled = false
    document().modified = false
    setWindowModified(false)
    setWindowTitle(userFriendlyCurrentFile() + "[*]")
end

#strippedName(fullFileName) ⇒ Object



149
150
151
# File 'ext/ruby/qtruby/examples/mainwindows/mdi/mdichild.rb', line 149

def strippedName(fullFileName)
    return Qt::FileInfo.new(fullFileName).fileName()
end

#userFriendlyCurrentFileObject



107
108
109
# File 'ext/ruby/qtruby/examples/mainwindows/mdi/mdichild.rb', line 107

def userFriendlyCurrentFile()
    return strippedName(@currentFile)
end