Class: ScribbleArea
- Inherits:
-
Qt::Widget
- Object
- Qt::Base
- Qt::Widget
- ScribbleArea
- Defined in:
- ext/ruby/qtruby/examples/widgets/scribble/scribblearea.rb,
ext/ruby/qtruby/examples/qtscribble/scribble.rb
Overview
** ** Copyright © 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: ** www.trolltech.com/products/qt/opensource.html ** ** If you are unsure which license is appropriate for your use, please ** review the following information: ** www.trolltech.com/products/qt/licensing.html or contact the ** sales department at [email protected]. ** ** 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
Instance Method Summary collapse
- #clearImage ⇒ Object
- #drawLineTo(endPoint) ⇒ Object
-
#initialize(parent = nil) ⇒ ScribbleArea
constructor
The constructor.
- #modified? ⇒ Boolean
-
#mouseMoveEvent(event) ⇒ Object
The method is called whenever the usr moves the mouse while the mouse button is pressed.
-
#mousePressEvent(event) ⇒ Object
This method is called whenever the user presses the mouse over the window.
- #mouseReleaseEvent(event) ⇒ Object
- #openImage(fileName) ⇒ Object
-
#paintEvent(event) ⇒ Object
This method is called whenever the widget needs painting, for example when it has been obscured and then revealed again.
- #penColor ⇒ Object
- #penColor=(newColor) ⇒ Object
- #penWidth ⇒ Object
- #penWidth=(newWidth) ⇒ Object
-
#resizeEvent(event) ⇒ Object
This method get called whenever the widget needs painting, for example, when it has been obscured and then revealed again.
- #resizeImage(image, newSize) ⇒ Object
- #saveImage(fileName, fileFormat) ⇒ Object
-
#setColor(new_color) ⇒ Object
This slot sets the curren color for the scribble area.
-
#slotClearArea ⇒ Object
This slot clears the drawing area by filling the off-screen buffer with white and copying it over to the window.
-
#slotLoad(filename) ⇒ Object
This method does the actual loading.
-
#slotSave(filename) ⇒ Object
This method does the actual saving.
Methods inherited from Qt::Widget
Methods inherited from Qt::Base
#%, #&, #*, #**, #+, #-, #-@, #/, #<, #<<, #<=, #==, #>, #>=, #>>, #QCOMPARE, #QEXPECT_FAIL, #QFAIL, #QSKIP, #QTEST, #QVERIFY, #QVERIFY2, #QWARN, #^, ancestors, #is_a?, #methods, private_slots, #protected_methods, #public_methods, q_classinfo, q_signal, q_slot, signals, #singleton_methods, slots, #|, #~
Constructor Details
#initialize(parent = nil) ⇒ ScribbleArea
The constructor. Initializes the member variables.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'ext/ruby/qtruby/examples/qtscribble/scribble.rb', line 17 def initialize(parent) super(parent) # initialize member variables @_buffer = Qt::Pixmap.new() @_last = Qt::Point.new() @_currentcolor = Qt::black # don't blank the window before repainting setAttribute( Qt::WA_NoBackground ) # create a pop-up menu @_popupmenu = Qt::Menu.new() @_popupmenu.addAction( "&Clear", self, SLOT( "slotClearArea()" ) ) end |
Instance Method Details
#clearImage ⇒ Object
87 88 89 90 91 |
# File 'ext/ruby/qtruby/examples/widgets/scribble/scribblearea.rb', line 87 def clearImage() @image.fill(qRgb(255, 255, 255)) @modified = true update() end |
#drawLineTo(endPoint) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'ext/ruby/qtruby/examples/widgets/scribble/scribblearea.rb', line 129 def drawLineTo(endPoint) painter = Qt::Painter.new(@image) painter.pen = Qt::Pen.new(Qt::Brush.new(@myPenColor), @myPenWidth, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin) painter.drawLine(@lastPoint, endPoint) @modified = true rad = @myPenWidth / 2 update(Qt::Rect.new(@lastPoint, endPoint).normalized().adjusted(-rad, -rad, +rad, +rad)) @lastPoint = endPoint painter.end end |
#modified? ⇒ Boolean
67 68 69 |
# File 'ext/ruby/qtruby/examples/widgets/scribble/scribblearea.rb', line 67 def modified? return @modified end |
#mouseMoveEvent(event) ⇒ Object
The method is called whenever the usr moves the mouse while the mouse button is pressed. If we had called setMouseTracking(true) before, the method would also be called when the mouse was moved with any button pressed. We know that we haven’t, and thus don’t have to check whether any buttons are pressed.
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 |
# File 'ext/ruby/qtruby/examples/qtscribble/scribble.rb', line 100 def mouseMoveEvent(event) # create a Qt::Painter object for drawing onto the window windowpainter = Qt::Painter.new() # and another Qt::Painter object for drawing int an off-screen pixmap bufferpainter = Qt::Painter.new() # start painting windowpainter.begin( self ) # This painter paints onto the window bufferpainter.begin( @_buffer ) # and this one paints in the buffer # set a standard pen with the currently selected color windowpainter.setPen( @_currentcolor ) bufferpainter.setPen( @_currentcolor ) # draw a line in both the window and the buffer windowpainter.drawLine( @_last, event.pos() ) bufferpainter.drawLine( @_last, event.pos() ) # done with painting windowpainter.end() bufferpainter.end() # remember the current mouse position @_last = event.pos() end |
#mousePressEvent(event) ⇒ Object
This method is called whenever the user presses the mouse over the window. It just records the position of the mouse at the time of the click.
83 84 85 86 87 88 89 |
# File 'ext/ruby/qtruby/examples/qtscribble/scribble.rb', line 83 def mousePressEvent(event) if event.() == RightButton @_popupmenu.exec( Qt::Cursor.pos() ) else @_last = event.pos() # retrieve the coordinates from the event end end |
#mouseReleaseEvent(event) ⇒ Object
106 107 108 109 110 111 |
# File 'ext/ruby/qtruby/examples/widgets/scribble/scribblearea.rb', line 106 def mouseReleaseEvent(event) if event.() == Qt::LeftButton && @scribbling drawLineTo(event.pos()) @scribbling = false end end |
#openImage(fileName) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'ext/ruby/qtruby/examples/widgets/scribble/scribblearea.rb', line 41 def openImage(fileName) loadedImage = Qt::Image.new if !loadedImage.load(fileName) return false end newSize = loadedImage.size().(size()) resizeImage(loadedImage, newSize) @image = loadedImage @modified = false update() return true end |
#paintEvent(event) ⇒ Object
This method is called whenever the widget needs painting, for example when it has been obscured and then revealed again.
130 131 132 |
# File 'ext/ruby/qtruby/examples/qtscribble/scribble.rb', line 130 def paintEvent(event) bitBlt(self, 0, 0, @_buffer) end |
#penColor ⇒ Object
71 72 73 |
# File 'ext/ruby/qtruby/examples/widgets/scribble/scribblearea.rb', line 71 def penColor return @myPenColor end |
#penColor=(newColor) ⇒ Object
75 76 77 |
# File 'ext/ruby/qtruby/examples/widgets/scribble/scribblearea.rb', line 75 def penColor=(newColor) @myPenColor = newColor end |
#penWidth ⇒ Object
79 80 81 |
# File 'ext/ruby/qtruby/examples/widgets/scribble/scribblearea.rb', line 79 def penWidth return @myPenWidth end |
#penWidth=(newWidth) ⇒ Object
83 84 85 |
# File 'ext/ruby/qtruby/examples/widgets/scribble/scribblearea.rb', line 83 def penWidth=(newWidth) @myPenWidth = newWidth end |
#resizeEvent(event) ⇒ Object
This method get called whenever the widget needs painting, for example, when it has been obscured and then revealed again.
138 139 140 141 142 143 |
# File 'ext/ruby/qtruby/examples/qtscribble/scribble.rb', line 138 def resizeEvent(event) save = Qt::Pixmap.new( @_buffer ) @_buffer = save.scaled(event.size.width, event.size.height) @_buffer.fill( Qt::Color.new(Qt::white) ) drawPixmap( @_buffer, 0, 0, save ) end |
#resizeImage(image, newSize) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'ext/ruby/qtruby/examples/widgets/scribble/scribblearea.rb', line 142 def resizeImage(image, newSize) if image.size == newSize return end newImage = Qt::Image.new(newSize, Qt::Image::Format_RGB32) newImage.fill(qRgb(255, 255, 255)) painter = Qt::Painter.new(newImage) painter.drawImage(Qt::Point.new(0, 0), image) @image = newImage painter.end end |
#saveImage(fileName, fileFormat) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 |
# File 'ext/ruby/qtruby/examples/widgets/scribble/scribblearea.rb', line 55 def saveImage(fileName, fileFormat) visibleImage = @image resizeImage(visibleImage, size()) if visibleImage.save(fileName, fileFormat.to_s) @modified = false return true else return false end end |
#setColor(new_color) ⇒ Object
This slot sets the curren color for the scribble area. It will be connected with the colorChanged( Qt::Color ) signal from the ScribbleWindow.
37 38 39 |
# File 'ext/ruby/qtruby/examples/qtscribble/scribble.rb', line 37 def setColor( new_color ) @_currentcolor = new_color end |
#slotClearArea ⇒ Object
This slot clears the drawing area by filling the off-screen buffer with white and copying it over to the window.
45 46 47 48 49 50 51 |
# File 'ext/ruby/qtruby/examples/qtscribble/scribble.rb', line 45 def slotClearArea() # fill the off screen buffer with plain white @_buffer.fill( white ) # and copy it over to the window bitBlt( self, 0, 0, @_buffer ) end |
#slotLoad(filename) ⇒ Object
This method does the actual loading. It relies on Qt::Pixmap (and the underlying I/O machinery) to determine the filetype.
58 59 60 61 62 63 64 |
# File 'ext/ruby/qtruby/examples/qtscribble/scribble.rb', line 58 def slotLoad( filename ) if !@_buffer.load( filename ) Qt::MessageBox.warning( nil, "Load error", "Could not load file" ) end repaint() # refresh the window end |
#slotSave(filename) ⇒ Object
This method does the actual saving. We hard-code the file type as BMP. Unix users might want to replace this with something like XPM.
71 72 73 74 75 |
# File 'ext/ruby/qtruby/examples/qtscribble/scribble.rb', line 71 def slotSave( filename ) if !@_buffer.save( filename, "BMP" ) Qt::MessageBox.warning( nil, "Save error", "Could not save file" ) end end |