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
|
=begin
**
** Copyright (C) 2004-2005 Trolltech AS. All rights reserved.
**
** This file is part of the example classes of the Qt Toolkit.
**
** This file may be used under the terms of the GNU General Public
** License version 2.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of
** this file. Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
** http://www.trolltech.com/products/qt/opensource.html
**
** If you are unsure which license is appropriate for your use, please
** review the following information:
** http://www.trolltech.com/products/qt/licensing.html or contact the
** sales department at sales@trolltech.com.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** Translated to QtRuby by Richard Dale
=end
require 'scribblearea.rb'
class MainWindow < Qt::MainWindow
slots 'open()',
'save()',
'penColor()',
'penWidth()',
'about()'
def initialize(parent = nil)
super(parent)
@scribbleArea = ScribbleArea.new
@saveAsActs = []
setCentralWidget(@scribbleArea)
createActions()
createMenus()
setWindowTitle(tr("Scribble"))
resize(500, 500)
end
def closeEvent(event)
if maybeSave()
event.accept()
else
event.ignore()
end
end
def open()
if maybeSave()
fileName = Qt::FileDialog::getOpenFileName(self,
tr("Open File"), Qt::Dir::currentPath())
if !fileName.nil?
@scribbleArea.openImage(fileName)
end
end
end
def save()
action = sender()
fileFormat = action.data().toByteArray()
saveFile(fileFormat)
end
def penColor()
newColor = Qt::ColorDialog.getColor(Qt::Color.new(@scribbleArea.penColor))
if newColor.isValid()
@scribbleArea.penColor = newColor
end
end
def penWidth()
ok = Qt::Boolean.new
newWidth = Qt::InputDialog::getInteger(self, tr("Scribble"),
tr("Select pen width:"),
@scribbleArea.penWidth(),
1, 50, 1, ok)
if ok
@scribbleArea.penWidth = newWidth
end
end
def about()
Qt::MessageBox.about(self, tr("About Scribble"),
tr("<p>The <b>Scribble</b> example shows how to use Qt::MainWindow as the " +
"base widget for an application, and how to reimplement some of " +
"Qt::Widget's event handlers to receive the events generated for " +
"the application's widgets:</p><p> We reimplement the mouse event " +
"handlers to facilitate drawing, the paint event handler to " +
"update the application and the resize event handler to optimize " +
"the application's appearance. In addition we reimplement the " +
"close event handler to intercept the close events before " +
"terminating the application.</p><p> The example also demonstrates " +
"how to use Qt::Painter to draw an image in real time, as well as " +
"to repaint widgets.</p>"))
end
def createActions()
@openAct = Qt::Action.new(tr("&Open..."), self)
@openAct.shortcut = Qt::KeySequence.new(tr("Ctrl+O"))
connect(@openAct, SIGNAL('triggered()'), self, SLOT('open()'))
Qt::ImageWriter.supportedImageFormats().each do |format|
text = tr("%s..." % format.upcase)
action = Qt::Action.new(text, self)
action.data = Qt::Variant.new(format)
connect(action, SIGNAL('triggered()'), self, SLOT('save()'))
@saveAsActs.push action
end
@exitAct = Qt::Action.new(tr("E&xit"), self)
@exitAct.shortcut = Qt::KeySequence.new(tr("Ctrl+Q"))
connect(@exitAct, SIGNAL('triggered()'), self, SLOT('close()'))
@penColorAct = Qt::Action.new(tr("&Pen Color..."), self)
connect(@penColorAct, SIGNAL('triggered()'), self, SLOT('penColor()'))
@penWidthAct = Qt::Action.new(tr("Pen &Width..."), self)
connect(@penWidthAct, SIGNAL('triggered()'), self, SLOT('penWidth()'))
@clearScreenAct = Qt::Action.new(tr("&Clear Screen"), self)
@clearScreenAct.shortcut = Qt::KeySequence.new(tr("Ctrl+L"))
connect(@clearScreenAct, SIGNAL('triggered()'),
@scribbleArea, SLOT('clearImage()'))
@aboutAct = Qt::Action.new(tr("&About"), self)
connect(@aboutAct, SIGNAL('triggered()'), self, SLOT('about()'))
@aboutQtAct = Qt::Action.new(tr("About &Qt"), self)
connect(@aboutQtAct, SIGNAL('triggered()'), $qApp, SLOT('aboutQt()'))
end
def createMenus()
@saveAsMenu = Qt::Menu.new(tr("&Save As"), self)
@saveAsActs.each do |action|
@saveAsMenu.addAction(action)
end
@fileMenu = Qt::Menu.new(tr("&File"), self) do |f|
f.addAction(@openAct)
f.addMenu(@saveAsMenu)
f.addSeparator()
f.addAction(@exitAct)
end
@optionMenu = Qt::Menu.new(tr("&Options"), self) do |o|
o.addAction(@penColorAct)
o.addAction(@penWidthAct)
o.addSeparator()
o.addAction(@clearScreenAct)
end
@helpMenu = Qt::Menu.new(tr("&Help"), self) do |h|
h.addAction(@aboutAct)
h.addAction(@aboutQtAct)
end
menuBar().addMenu(@fileMenu)
menuBar().addMenu(@optionMenu)
menuBar().addMenu(@helpMenu)
end
def maybeSave()
if @scribbleArea.modified?
ret = Qt::MessageBox::warning(self, tr("Scribble"),
tr("The image has been modified.\n" +
"Do you want to save your changes?"),
Qt::MessageBox::Yes | Qt::MessageBox::Default,
Qt::MessageBox::No,
Qt::MessageBox::Cancel | Qt::MessageBox::Escape)
if ret == Qt::MessageBox::Yes
return saveFile("png")
elsif ret == Qt::MessageBox::Cancel
return false
end
end
return true
end
def saveFile(fileFormat)
initialPath = Qt::Dir.currentPath() + "/untitled." + fileFormat
fileName = Qt::FileDialog.getSaveFileName(self, tr("Save As"),
initialPath,
tr("%s Files (*.%s);;All Files (*)" %
[fileFormat.upcase, fileFormat] ) )
if fileName.nil?
return false
else
return @scribbleArea.saveImage(fileName, fileFormat)
end
end
end
|