Initial implementation of XBoard theme plugins.
[kaya/ydirson.git] / lib / plugins / clocks / lib / clock_display.rb
blobafb623322c0681b158a7fe6fa073cc710e7d4bc6
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 'constrained_text_item'
10 module ClockDisplay
11   OFF_TEXT = '-'
12   
13   def set_geometry(rect)
14     @rect = Qt::RectF.new(rect)
15     self.pos = @rect.top_left
16     redraw
17   end
18   
19   def create_display_items
20     {
21       :time => ConstrainedTextItem.new(OFF_TEXT, self),
22       :player => ConstrainedTextItem.new('', self),
23       :caption => ConstrainedTextItem.new('', self)
24     }
25   end
26   
27   def start
28     @clock.start if @clock
29     self.active = true
30   end
31   
32   def stop
33     @clock.stop if @clock
34     self.active = false
35   end
36   
37   def active=(value)
38     @active = value
39     redraw
40   end
41   
42   def active?
43     @active
44   end
45   
46   def data=(d)
47     @caption = d[:color].to_s.capitalize
48     @player = d[:player] || '(unknown)'
49     
50     items[:caption].text = @caption
51     items[:player].text = @player
52   end
53   
54   def clock=(clock)
55     if @clock
56       @clock.delete_observer(self)
57     end
58     
59     @clock = clock
60     clock.add_observer(self)
61     on_timer(clock.timer)
62   end
63   
64   def on_timer(data)
65     min = data[:main] / 60
66     sec = data[:main] % 60
67     
68     @items[:time].text = "%02d:%02d" % [min, sec]
69   end
70 end