class Car

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.

Public Class Methods

new() click to toggle source
Calls superclass method
# File 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

Public Instance Methods

accelerate() click to toggle source
# File examples/qdbus/remotecontrolledcar/car/car.rb, line 79
def accelerate()
    if @speed < 10
        @speed += 1
    end
end
boundingRect() click to toggle source
# File examples/qdbus/remotecontrolledcar/car/car.rb, line 66
def boundingRect()
    return Qt::RectF.new(-35, -81, 70, 115)
end
decelerate() click to toggle source
# File examples/qdbus/remotecontrolledcar/car/car.rb, line 85
def decelerate()
    if @speed > -10
        @speed -= 1
    end
end
paint(painter, option, widget) click to toggle source
# File 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) click to toggle source
# File 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
turnLeft() click to toggle source
# File examples/qdbus/remotecontrolledcar/car/car.rb, line 91
def turnLeft()
    if @wheelsAngle > -30
        @wheelsAngle -= 5
    end
end
turnRight() click to toggle source
# File examples/qdbus/remotecontrolledcar/car/car.rb, line 97
def turnRight()
    if @wheelsAngle < 30
       @wheelsAngle += 5
    end
end