Backported de09752972bd6e1c8a68d8c1d06c3311721dc1bd.
[kaya.git] / lib / plugins / movelist / simple_movelist.rb
blob57a213e7df1e0d43ba4a3822af1dee415e93ab36
1 # Copyright (c) 2009 Paolo Capriotti <p.capriotti@gmail.com>
2
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 require 'qtutils'
9 require 'plugins/plugin'
10 require 'observer_utils'
12 class SimpleMoveList < Qt::ListView
13   include Plugin
14   include Observer
15   
16   plugin :name => 'Simple Move List',
17          :interface => :movelist
18          
19   class LinearHistoryModel < Qt::StringListModel
20     include Observer
21     include Observable
22     
23     def initialize(match)
24       super([])
25       @history = match.history
26       @serializer = match.game.serializer.new(:compact)
27       
28       @history.add_observer(self)
29       
30       insert_rows(0, @history.size)
31       (0...@history.size).each do |i|
32         update_row(i)
33       end
34     end
35     
36     def on_new_move
37       if @history.size > rowCount
38         insert_rows(rowCount, @history.size - rowCount)
39       else
40         remove_rows(@history.size, rowCount - @history.size)
41       end
42       update_row(@history.current)
43       on_current_changed
44     end
46     def on_current_changed
47       fire :change_current => index(@history.current, 0)
48     end
49     
50     def move(i)
51       if i == 0
52         'Mainline'
53       else
54         state = @history[i - 1].state
55         move = @history[i].move
56         san = @serializer.serialize(move, state)
57         
58         count = (i + 1) / 2
59         dots = if i % 2 == 1
60           '.'
61         else
62           '...'
63         end
64         
65         "#{count}#{dots} #{san}"
66       end
67     end
68     
69     def update_row(i)
70       set_data(index(i, 0), move(i), Qt::DisplayRole)
71     end
72     
73     def flags(index)
74       if index.isValid
75         Qt::ItemIsSelectable | Qt::ItemIsEnabled
76       else
77         Qt::NoItemFlags
78       end
79     end
80   end
81   
82   def initialize(controller, opts = {})
83     super(controller.table)
84     @controller = controller
85     self.font = opts[:font] if opts[:font]
86     
87     @controller.table.add_observer(self)
88   end
89   
90   def on_reset(match)
91     if match.game.respond_to?(:serializer)
92       self.model = LinearHistoryModel.new(match)
93       model.observe(:change_current) do |current|
94         select(current)
95       end
96       sig = 'selectionChanged(QItemSelection, QItemSelection)'
97       selection_model.on(sig) do |selected, deselected|
98         match.history.go_to(selected.indexes.first.row)
99       end
100       # select last item
101       select(model.index(model.row_count - 1, 0))
102     else
103       self.model = nil
104     end
105   end
106   
107   def select(index)
108     selection_model.select(index, 
109       Qt::ItemSelectionModel::ClearAndSelect)
110     scroll_to(index)    
111   end