Add licence and installation instructions.
[kaya.git] / lib / plugins / movelist / simple_movelist.rb
blob0807d064803e56042b680941dc860e077028a4f0
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 'plugins/plugin'
9 require 'observer_utils'
11 class SimpleMoveList < Qt::ListView
12   include Plugin
13   include Observer
14   
15   plugin :name => 'Simple Move List',
16          :interface => :movelist
17          
18   class LinearHistoryModel < Qt::StringListModel
19     include Observer
20     include Observable
21     
22     def initialize(match)
23       super([])
24       @history = match.history
25       @serializer = match.game.serializer.new(:compact)
26       
27       @history.add_observer(self)
28       
29       insert_rows(0, @history.size)
30       (0...@history.size).each do |i|
31         update_row(i)
32       end
33     end
34     
35     def on_new_move
36       if @history.size > rowCount
37         insert_rows(rowCount, @history.size - rowCount)
38       else
39         remove_rows(@history.size, rowCount - @history.size)
40       end
41       update_row(@history.current)
42       on_current_changed
43     end
45     def on_current_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 + 1) / 2
58         dots = if i % 2 == 1
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     if match.game.respond_to?(:serializer)
91       self.model = LinearHistoryModel.new(match)
92       model.observe(:change_current) do |current|
93         select(current)
94       end
95       sig = 'selectionChanged(QItemSelection, QItemSelection)'
96       selection_model.on(sig) do |selected, deselected|
97         match.history.go_to(selected.indexes.first.row)
98       end
99       # select last item
100       select(model.index(model.row_count - 1, 0))
101     else
102       self.model = nil
103     end
104   end
105   
106   def select(index)
107     selection_model.select(index, 
108       Qt::ItemSelectionModel::ClearAndSelect)
109     scroll_to(index)    
110   end