Class: ColorItem

Inherits:
Qt::GraphicsItem show all
Defined in:
ext/ruby/qtruby/examples/graphicsview/dragdroprobot/coloritem.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

Constant Summary collapse

@@n =
0

Instance Method Summary collapse

Methods inherited from Qt::GraphicsItem

#type

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

#initializeColorItem

Returns a new instance of ColorItem.



29
30
31
32
33
34
35
36
# File 'ext/ruby/qtruby/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

Instance Method Details

#boundingRectObject



38
39
40
# File 'ext/ruby/qtruby/examples/graphicsview/dragdroprobot/coloritem.rb', line 38

def boundingRect
    return Qt::RectF.new(-15.5, -15.5, 34, 34)
end

#mousePressEvent(event) ⇒ Object



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
# File 'ext/ruby/qtruby/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) ⇒ Object



42
43
44
45
46
47
48
49
# File 'ext/ruby/qtruby/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