Class: Car

Inherits:
Qt::GraphicsItem show all
Defined in:
ext/ruby/qtruby/examples/qdbus/remotecontrolledcar/car/car.rb

Overview

Note that as Ruby doesn’t have multiple inheritance, and ‘Car’, can’t inherit from Qt::Object as well as Qt::Graphics item. So the timerEvent event handling is done in the CarAdaptor instance which is a Qt::Object, which calls back to the Car instance.

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

#initializeCar

Returns a new instance of Car.



70
71
72
73
74
75
76
77
# File 'ext/ruby/qtruby/examples/qdbus/remotecontrolledcar/car/car.rb', line 70

def initialize()
    super
    @color = Qt::Brush.new(Qt::green)
    @wheelsAngle = 0.0 
    @speed = 0.0
    setFlag(Qt::GraphicsItem::ItemIsMovable, true)
    setFlag(Qt::GraphicsItem::ItemIsFocusable, true)
end

Instance Method Details

#accelerateObject



79
80
81
82
83
# File 'ext/ruby/qtruby/examples/qdbus/remotecontrolledcar/car/car.rb', line 79

def accelerate()
    if @speed < 10
        @speed += 1
    end
end

#boundingRectObject



66
67
68
# File 'ext/ruby/qtruby/examples/qdbus/remotecontrolledcar/car/car.rb', line 66

def boundingRect()
    return Qt::RectF.new(-35, -81, 70, 115)
end

#decelerateObject



85
86
87
88
89
# File 'ext/ruby/qtruby/examples/qdbus/remotecontrolledcar/car/car.rb', line 85

def decelerate()
    if @speed > -10
        @speed -= 1
    end
end

#paint(painter, option, widget) ⇒ Object



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
# File 'ext/ruby/qtruby/examples/qdbus/remotecontrolledcar/car/car.rb', line 103

def paint(painter, option, widget)
    painter.brush = Qt::Brush.new(Qt::gray)
    painter.drawRect(-20, -58, 40, 2) # front axel
    painter.drawRect(-20, 7, 40, 2) # rear axel

    painter.brush = @color
    painter.drawRect(-25, -79, 50, 10) # front wing

    painter.drawEllipse(-25, -48, 50, 20) # side pods
    painter.drawRect(-25, -38, 50, 35) # side pods
    painter.drawRect(-5, 9, 10, 10) # back pod

    painter.drawEllipse(-10, -81, 20, 100) # main body

    painter.drawRect(-17, 19, 34, 15) # rear wing

    painter.brush = Qt::Brush.new(Qt::black)
    painter.drawPie(-5, -51, 10, 15, 0, 180 * 16)
    painter.drawRect(-5, -44, 10, 10) # cocpit

    painter.save()
    painter.translate(-20, -58)
    painter.rotate(@wheelsAngle)
    painter.drawRect(-10, -7, 10, 15) # front left
    painter.restore()

    painter.save()
    painter.translate(20, -58)
    painter.rotate(@wheelsAngle)
    painter.drawRect(0, -7, 10, 15) # front left
    painter.restore()

    painter.drawRect(-30, 0, 12, 17) # rear left
    painter.drawRect(19, 0, 12, 17)  # rear right
end

#timerEvent(event) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
# File 'ext/ruby/qtruby/examples/qdbus/remotecontrolledcar/car/car.rb', line 139

def timerEvent(event)
    axelDistance = 54
    wheelsAngleRads = (@wheelsAngle * Math::PI) / 180
    turnDistance = Math.cos(wheelsAngleRads) * axelDistance * 2
    turnRateRads = wheelsAngleRads / turnDistance  # rough estimate
    turnRate = (turnRateRads * 180) / Math::PI
    rotation = @speed * turnRate
    
    rotate(rotation)
    translate(0, -@speed)
    update()
end

#turnLeftObject



91
92
93
94
95
# File 'ext/ruby/qtruby/examples/qdbus/remotecontrolledcar/car/car.rb', line 91

def turnLeft()
    if @wheelsAngle > -30
        @wheelsAngle -= 5
    end
end

#turnRightObject



97
98
99
100
101
# File 'ext/ruby/qtruby/examples/qdbus/remotecontrolledcar/car/car.rb', line 97

def turnRight()
    if @wheelsAngle < 30
       @wheelsAngle += 5
    end
end