class ColorItem

Public Class Methods

new() click to toggle source
Calls superclass method
# File examples/graphicsview/dragdroprobot/coloritem.rb, line 29
def initialize
    super
    @color = Qt::Color.new(rand(256), rand(256), rand(256))
    setToolTip("Qt::Color(%d, %d, %d)\n%s" %
           [@color.red, @color.green, @color.blue,
           "Click and drag this color onto the robot!"])
    setCursor(Qt::Cursor.new(Qt::OpenHandCursor))
end

Public Instance Methods

boundingRect() click to toggle source
# File examples/graphicsview/dragdroprobot/coloritem.rb, line 38
def boundingRect
    return Qt::RectF.new(-15.5, -15.5, 34, 34)
end
mousePressEvent(event) click to toggle source
# File examples/graphicsview/dragdroprobot/coloritem.rb, line 51
def mousePressEvent(event)
    if event.button != Qt::LeftButton
        event.ignore
        return
    end

    drag = Qt::Drag.new(event.widget)
    mime = Qt::MimeData.new
    drag.mimeData = mime

    @@n += 1
    if @@n > 2 && rand(3) == 0
        image = Qt::Image.new(":/images/head.png")
        mime.imageData = qVariantFromValue(image)

        drag.pixmap = Qt::Pixmap.fromImage(image.scaled(30, 40))
        drag.setHotSpot(Qt::Point.new(30, 40))
    else
        mime.colorData = qVariantFromValue(@color)
        mime.text = "#%2.2x%2.2x%2.2x" % [@color.red, @color.green, @color.blue]

        pixmap = Qt::Pixmap.new(34, 34)
        pixmap.fill(Qt::Color.new(Qt::transparent))
        painter = Qt::Painter.new(pixmap)
        painter.translate(15, 15)
        painter.renderHint = Qt::Painter::Antialiasing
        paint(painter, 0, 0)
        painter.end
    
        drag.pixmap = pixmap
        drag.hotSpot = Qt::Point.new(15, 20)
    end
    drag.start
end
paint(painter, option, widget) click to toggle source
# File examples/graphicsview/dragdroprobot/coloritem.rb, line 42
def paint(painter, option, widget)
    painter.pen = Qt::NoPen
    painter.brush = Qt::Brush.new(Qt::darkGray)
    painter.drawEllipse(-12, -12, 30, 30)
    painter.pen = Qt::Pen.new(Qt::Brush.new(Qt::black), 1)
    painter.brush = Qt::Brush.new(@color)
    painter.drawEllipse(-15, -15, 30, 30)
end