1 require 'observer_utils.rb'
3 class History < Qt::AbstractListModel
10 Item = Struct.new(:state, :move, :text)
11 OutOfBound = Class.new(Exception)
15 @history = [Item.new(state.dup, nil, "Mainline")]
20 @history.each {|item| yield item.state, item.move }
23 def add_move(state, move)
24 item = Item.new(state.dup, move, nil)
26 removing_rows(nil, @current + 1, @history.size - 1) do
27 @history = @history[0..@current]
30 inserting_rows(nil, @current + 1, @current + 1) do
32 @current = @history.size - 1
37 raise OutOfBound if @current >= @history.size - 1
39 item = @history[@current]
40 [item.state, item.move]
44 raise OutOfBound if @current <= 0
45 move = @history[@current].move
47 [@history[@current].state, move]
51 @history[current].state
55 @history[current].move
68 # set a serializer for this model
69 # if no serializer has been set, it will
70 # be impossible to use it with a view
76 if @serializer and role == Qt::DisplayRole
77 unless @history[index.row].text
78 state = @history[index.row - 1].state
79 move = @history[index.row].move
80 san = @serializer.serialize(move, state)
82 count = index.row / 2 + 1
83 dots = if index.row % 2 == 0
89 @history[index.row].text = "#{count}#{dots} #{san}"
91 @history[index.row].text