I'm writing a tutorial to implement a small Logo interpreter, and it would
be nice to use QtRuby as an output canvas for the turtle graphics bit. I
have a simple canvas class:
class Canvas < Qt::Widget
def initialize
super
@lines = []
end
def draw_line(x1, y1, x2, y2)
@lines << [x1, y1, x2, y2]
end
def paintEvent(event)
painter = Qt::Painter.new(self)
painter.pen = Qt::Pen.new(@color)
@lines.each do |x1, y1, x2, y2|
painter.drawLine x1, y1, x2, y2
end
painter.end
end
end
which I intend passing to the turtle to use via
def forward n
theta = direction * PI / 180
new_x = @x + n * cos(theta)
new_y = @y + n * sin(theta)
if @drawing
canvas.draw_line x, y, new_x, new_y
end
@x = new_x
@y = new_y
end
I'm trying to keep the code as straightforward as possible, and focused on
the interpreter rather than on the details of QtRuby. The problem is that
Qt insists on being the main loop, and of course an interpreter needs to
run in a loop over the input code too.
I'm trying to think of a nice way to write the interpreter as independently
of the Qt event loop as possible and send line drawing events to it by
simply calling canvas.draw_line, without having to change the interpreter
architecture in any way, or have my tutorial bogged down by the details of
threading and events.
One idea, e.g. would be to have the canvas object in the turtle be a
message queue, that the Qt canvas polled via a timer, but if someone can
think of something even simpler I'd love to hear it.
martin