class ScribbleArea

** ** 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: ** 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 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

Public Class Methods

new(parent) click to toggle source

The constructor. Initializes the member variables.

Calls superclass method
# File 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

Public Instance Methods

clearImage() click to toggle source
# File examples/widgets/scribble/scribblearea.rb, line 87
def clearImage()
        @image.fill(Qt::Color.new(255,255,255)) #qRgb(255, 255, 255))
        @modified = true
        update()
end
drawLineTo(endPoint) click to toggle source
# File 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?() click to toggle source
# File examples/widgets/scribble/scribblearea.rb, line 67
def modified?
        return @modified
end
mouseMoveEvent(event) click to toggle source

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.

# File 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) click to toggle source

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.

# File examples/qtscribble/scribble.rb, line 83
def mousePressEvent(event)
        if event.button() == RightButton
                @_popupmenu.exec( Qt::Cursor.pos() )
        else
                @_last = event.pos()        # retrieve the coordinates from the event
        end
end
mouseReleaseEvent(event) click to toggle source
# File examples/widgets/scribble/scribblearea.rb, line 106
def mouseReleaseEvent(event)
        if event.button() == Qt::LeftButton && @scribbling
                drawLineTo(event.pos())
                @scribbling = false
        end
end
openImage(fileName) click to toggle source
# File examples/widgets/scribble/scribblearea.rb, line 41
def openImage(fileName)
        loadedImage = Qt::Image.new
        if !loadedImage.load(fileName)
                return false
        end

        newSize = loadedImage.size().expandedTo(size())
        resizeImage(loadedImage, newSize)
        @image = loadedImage
        @modified = false
        update()
        return true
end
paintEvent(event) click to toggle source

This method is called whenever the widget needs painting, for example when it has been obscured and then revealed again.

# File examples/qtscribble/scribble.rb, line 130
def paintEvent(event)
        bitBlt(self, 0, 0, @_buffer)
end
penColor() click to toggle source
# File examples/widgets/scribble/scribblearea.rb, line 71
def penColor
        return @myPenColor
end
penColor=(newColor) click to toggle source
# File examples/widgets/scribble/scribblearea.rb, line 75
def penColor=(newColor)
        @myPenColor = newColor
end
penWidth() click to toggle source
# File examples/widgets/scribble/scribblearea.rb, line 79
def penWidth
        return @myPenWidth
end
penWidth=(newWidth) click to toggle source
# File examples/widgets/scribble/scribblearea.rb, line 83
def penWidth=(newWidth)
        @myPenWidth = newWidth
end
resizeEvent(event) click to toggle source

This method get called whenever the widget needs painting, for example, when it has been obscured and then revealed again.

# File 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) click to toggle source
# File 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(Qt::Color.new(255,255,255)) #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) click to toggle source
# File 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 ) click to toggle source

This slot sets the curren color for the scribble area. It will be connected with the colorChanged( Qt::Color ) signal from the ScribbleWindow.

# File examples/qtscribble/scribble.rb, line 37
def setColor( new_color )
        @_currentcolor = new_color
end
slotClearArea() click to toggle source

This slot clears the drawing area by filling the off-screen buffer with white and copying it over to the window.

# File 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 ) click to toggle source

This method does the actual loading. It relies on Qt::Pixmap (and the underlying I/O machinery) to determine the filetype.

# File 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 ) click to toggle source

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.

# File 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