Backported de09752972bd6e1c8a68d8c1d06c3311721dc1bd.
[kaya.git] / lib / plugins / clocks / digital.rb
blob463699f5c9a0dfea9f2d0d2224b0e15c3300ca32
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 'constrained_text_item'
11 require 'board/item_bag'
12 require 'observer_utils'
14 class DigitalClock < Qt::GraphicsItemGroup
15   include Plugin
16   include ItemBag
17   include ItemUtils
18   include Observer
19   
20   plugin :name => 'Digital Clock',
21          :interface => :clock
23   attr_reader :items, :rect, :clock
24   
25   OFF_TEXT = '-'
26   BASE_DIR = File.dirname(__FILE__)
27   ACTIVE_SKIN_RENDERER = Qt::SvgRenderer.new(
28       File.join(BASE_DIR, 'active_clock.svg'))
29   INACTIVE_SKIN_RENDERER = Qt::SvgRenderer.new(
30       File.join(BASE_DIR, 'inactive_clock.svg'))
31           
32   def initialize(scene)
33     super(nil, @scene = scene)
34     
35     @items = {
36       :time => ConstrainedTextItem.new(OFF_TEXT, self),
37       :player => ConstrainedTextItem.new('', self),
38       :caption => ConstrainedTextItem.new('', self)
39     }
40     
41     @active = false
42   end
43   
44   def set_geometry(rect)
45     @rect = Qt::RectF.new(rect)
46     self.pos = @rect.top_left
47     redraw
48   end
49   
50   def redraw
51     if @rect
52       add_item :skin, skin, :z => BACKGROUND_ZVALUE
53       @items[:time].constraint = Qt::RectF.new(
54         @rect.width * 0.4, @rect.height * 0.1, 
55         @rect.width * 0.6, @rect.height * 0.62)
56       @items[:caption].constraint = Qt::RectF.new(
57         @rect.width * 0.02, @rect.height * 0.22,
58         @rect.width * 0.4, @rect.height * 0.38)
59       @items[:player].constraint = Qt::RectF.new(
60         @rect.width * 0.14, @rect.height * 0.68,
61         @rect.width * 0.69, @rect.height * 0.28)
62     end
63   end
64   
65   def clock=(clock)
66     if @clock
67       @clock.delete_observer(self)
68     end
69     
70     @clock = clock
71     clock.add_observer(self)
72     on_timer(clock.timer)
73   end
74   
75   def on_timer(data)
76     min = data[:main] / 60
77     sec = data[:main] % 60
78     
79     @items[:time].text = "%02d:%02d" % [min, sec]
80   end
81   
82   def skin
83     renderer = if @active
84       ACTIVE_SKIN_RENDERER
85     else
86       INACTIVE_SKIN_RENDERER
87     end
88     Qt::Image.from_renderer(@rect.size, renderer).to_pix
89   end
90   
91   def start
92     @clock.start if @clock
93     self.active = true
94   end
95   
96   def stop
97     @clock.stop if @clock
98     self.active = false
99   end
100   
101   def active=(value)
102     @active = value
103     redraw
104   end
105   
106   def active?
107     @active
108   end
109   
110   def data=(d)
111     @caption = d[:color].to_s.capitalize
112     @player = d[:player] || '(unknown)'
113     
114     items[:caption].text = @caption
115     items[:player].text = @player
116   end