Movelist is now reenabled and working.
[kaya.git] / lib / plugins / movelist / simple_movelist.rb
bloba2c7902abc4eb8de691afc1e9ce852e524e69d72
1 require 'plugins/plugin'
3 class SimpleMoveList < Qt::ListView
4   include Plugin
5   include Observer
6   
7   plugin :name => 'Simple Move List',
8          :keywords => %w(movelist)
9          
10   class LinearHistoryModel < Qt::StringListModel
11     include Observer
12     include Observable
13     
14     def initialize(match)
15       super([])
16       @history = match.history
17       @serializer = match.game.serializer.new(:compact)
18       
19       @history.add_observer(self)
20       
21       insert_rows(0, @history.size)
22       (0...@history.size).each do |i|
23         update_row(i)
24       end
25     end
26     
27     def on_new_move
28       if @history.size > rowCount
29         insert_rows(rowCount, @history.size - rowCount)
30       else
31         remove_rows(@history.size, rowCount - @history.size)
32       end
33       update_row(@history.current)
34       fire_changed
35     end
36     
37     def on_forward
38       fire_changed
39     end
40     
41     def on_back
42       fire_changed
43     end
44     
45     def fire_changed
46       fire :change_current => index(@history.current, 0)
47     end
48     
49     def move(i)
50       if i == 0
51         'Mainline'
52       else
53         state = @history[i - 1].state
54         move = @history[i].move
55         san = @serializer.serialize(move, state)
56         
57         count = i / 2 + 1
58         dots = if i % 2 == 0
59           '.'
60         else
61           '...'
62         end
63         
64         "#{count}#{dots} #{san}"
65       end
66     end
67     
68     def update_row(i)
69       set_data(index(i, 0), move(i), Qt::DisplayRole)
70     end
71     
72     def flags(index)
73       if index.isValid
74         Qt::ItemIsSelectable | Qt::ItemIsEnabled
75       else
76         Qt::NoItemFlags
77       end
78     end
79   end
80   
81   def initialize(controller, opts = {})
82     super(controller.table)
83     @controller = controller
84     self.font = opts[:font] if opts[:font]
85     
86     @controller.table.add_observer(self)
87   end
88   
89   def on_reset(match)
90     self.model = LinearHistoryModel.new(match)
91     model.observe(:change_current) do |current|
92       selection_model.select(current, 
93         Qt::ItemSelectionModel::ClearAndSelect)
94     end
95     sig = 'selectionChanged(QItemSelection, QItemSelection)'
96     selection_model.on(sig) do |selected, deselected|
97       @controller.go_to(selected.indexes.first.row)
98     end
99   end