Fix behavior of Object#alter.
[kaya.git] / lib / plugins / clocks / xboard.rb
blob69cc472ce5ebe7a7e8f00139ef4164d85e118b5d
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 'toolkit'
9 require 'plugins/plugin'
10 require 'board/item_bag'
11 require 'observer_utils'
12 require_bundle 'clocks', 'clock_display'
14 class XBoardClock < Qt::GraphicsItemGroup
15   include Plugin
16   include ItemBag
17   include ItemUtils
18   include Observer
19   include ClockDisplay
20   
21   plugin :name => 'XBoard Clock Skin',
22          :interface => :clock
24   attr_reader :items, :rect, :clock, :translate
25   
26   OFF_TEXT = '-'
27           
28   def initialize(scene, translate)
29     super(nil, @scene = scene)
30     @translate = translate
31     @items = create_display_items
32     @active = false
33   end
34     
35   def redraw
36     if @rect
37       add_item :skin, 
38                :pixmap => skin, 
39                :z => BACKGROUND_ZVALUE
40       text_color = Qt::Color.new(@active ? Qt::white : Qt::black)
41       @items[:time].constraint = Qt::RectF.new(
42         @rect.width * 0.4, @rect.height * 0.05, 
43         @rect.width * 0.7, @rect.height * 0.8)
44       @items[:caption].constraint = Qt::RectF.new(
45         0, @rect.height * 0.1,
46         @rect.width * 0.5, @rect.height * 0.7)
47       @items[:player].constraint = Qt::RectF.new(
48         0, @rect.height * 0.68,
49         @rect.width, @rect.height * 0.28)
50         
51       @items.each do |name, item|
52         if item.respond_to? 'color='
53           item.color = text_color
54         end
55       end
56     end
57   end
58   
59   def skin
60     color = Qt::Color.new(@active ? Qt::black : Qt::white)
61     Qt::Image.painted(@rect.size) do |p|
62       p.fill_rect(Qt::RectF.new(Qt::PointF.new(0, 0), @rect.size), 
63                   color)
64       p.alter(:pen) {|pen| pen.style = Qt::SolidLine }
65       p.draw_line(0, 0, @rect.width, 0)
66       p.draw_line(0, 0, 0, @rect.height)
67       p.draw_line(@rect.width - 1, 0, @rect.width - 1, @rect.height)
68       p.draw_line(0, @rect.height - 1, @rect.width, @rect.height - 1)
69     end.to_pix
70   end
71 end