Add licence and installation instructions.
[kaya.git] / lib / item.rb
blob5b20b028430baf74ddae543111a9c5caefacf2f9
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'
10 class Item < Qt::GraphicsPixmapItem
11   attr_reader :name, :item
12   attr_reader :opacity
13   
14   # name is whatever information the caller needs
15   # to recreate this piece with a different size
16   # 
17   def initialize(name, pixmap, parent)
18     super pixmap, parent
19     @name = name
20     @opacity = 1.0
21   end
22   
23   def paint(p, options, widget)
24     p.saving do |p|
25       p.opacity = @opacity
26       super p, options, widget
27     end
28   end
29   
30   def opacity=(value)
31     @opacity = value
32     update
33   end
34   
35   def remove
36     scene.remove_item self
37   end
38 end
40 module ItemUtils
41   BACKGROUND_ZVALUE = -10
42   TEMP_ZVALUE = 10
43   
44   def create_item(key, pix, opts = {})
45     name = opts[:name] || key.to_s
46     item = Item.new(name, pix, self)
47     item.pos = opts[:pos] || Qt::PointF.new(0, 0)
48     item.z_value = opts[:z] || 0
49     item.visible = false if opts[:hidden]
50     item
51   end
52   
53   def destroy_item(item)
54     scene.remove_item item
55   end
56   
57   def raise(item)
58     item.z_value = TEMP_ZVALUE
59   end
60   
61   def lower(item)
62     item.z_value = 0
63   end
64 end