The clock is now skinnable.
[kaya.git] / lib / qtutils.rb
blob420c04fe49e829d2584bcf6642c6d57696af3c51
1 require 'korundum4'
3 class Object
4   def tap
5     yield self
6     self
7   end
8   
9   def metaclass
10     class << self
11       self
12     end
13   end
14   
15   def metaclass_eval(&blk)
16     metaclass.instance_eval(&blk)
17   end
18 end
20 module Enumerable
21   def detect_index
22     i = 0
23     each do |item|
24       return i if yield item
25       i += 1
26     end
27     
28     nil
29   end
30 end
32 class Qt::Painter
33   def paint
34     yield self
35   ensure
36     self.end
37   end
38   
39   def saving
40     save
41     yield self
42   ensure
43     restore
44   end
45 end
47 class Qt::Image
48   def to_pix
49     Qt::Pixmap.from_image self
50   end
51   
52   def self.painted(size, &blk)
53     Qt::Image.new(size.x, size.y, Qt::Image::Format_ARGB32_Premultiplied).tap do |img|
54       img.fill(0)
55       Qt::Painter.new(img).paint(&blk)
56     end
57   end
59   def self.from_renderer(size, renderer, id = nil)
60     img = Qt::Image.painted(size) do |p| 
61       if id
62         renderer.render(p, id)
63       else
64         renderer.render(p)
65       end
66     end
67     img
68   end
69 end
71 module PrintablePoint
72   def ==(other)
73     self.x == other.x and self.y == other.y
74   end
75   
76   def to_s
77     "(#{self.x}, #{self.y})"
78   end
79 end
81 module PrintableRect
82   def to_s
83     "[#{self.x}, #{self.y} - #{self.width}, #{self.height}]"
84   end
85 end
87 class Qt::Point
88   include PrintablePoint
89   
90   def to_f
91     Qt::PointF.new(x, y)
92   end
93 end
95 class Qt::PointF
96   include PrintablePoint
97   
98   def to_i
99     Qt::Point.new(x.to_i, y.to_i)
100   end
103 class Qt::Size
104   include PrintablePoint
105   
106   def x
107     width
108   end
109   
110   def y
111     height
112   end
115 class Qt::SizeF
116   include PrintablePoint
117   
118   def x
119     width
120   end
121   
122   def y
123     height
124   end
127 class Qt::Rect
128   include PrintableRect
131 class Qt::RectF
132   include PrintableRect
135 class Qt::Pixmap
136   def self.from_svg(size, file, id = nil)
137     from_renderer(size, Qt::SvgRenderer.new(file), id)
138   end
139   
140   def self.from_renderer(size, renderer, id = nil)
141     Qt::Image.from_renderer(size, renderer, id).to_pix
142   end
145 class Qt::Base
146   def self.signal_map(sigmap)
147     @signal_map = sigmap
148     signals *sigmap.map{|k, v| v || k }
149   end
151   def self.get_signal(sig)
152     (@signal_map || {})[sig] || sig
153   end
155   def on(sig, &blk)
156     connect(SIGNAL(self.class.get_signal(sig)), &blk)
157   end  
159   def in(interval, &blk)
160     Qt::Timer.in(interval, self, &blk)
161   end
163   def run_later(&blk)
164     self.in(0, &blk)
165   end
168 class Qt::Timer
169   def self.every(interval, &blk)
170     time = Qt::Time.new
171     time.restart
172     
173     timer = new
174     timer.connect(SIGNAL('timeout()')) { blk[time.elapsed] }
175     timer.start(interval)
176     # return the timer, so that the caller
177     # has a chance to keep it referenced, so
178     # that it is not garbage collected
179     timer
180   end
182   def self.in(interval, target = nil, &blk)
183     single_shot(interval,
184                 Qt::BlockInvocation.new(target, blk, 'invoke()'),
185                 SLOT('invoke()'))
186   end
189 class KDE::Application
190   def self.init(data)
191     about = KDE::AboutData.new(
192       data[:id],
193       data[:id],
194       data[:name],
195       data[:version],
196       data[:description],
197       KDE::AboutData::License_GPL,
198       data[:copyright])
199     data[:authors].each do |name, email|
200       about.addAuthor(name, KDE::LocalizedString.new, email)
201     end
202     data[:contributors].each do |name, contribution|
203       about.addCredit(name, contribution)
204     end
205     about.bug_address = Qt::ByteArray.new(data[:bug_tracker])
206     
207     KDE::CmdLineArgs.init(ARGV, about)
208     KDE::CmdLineOptions.new.tap do |opts|
209       data[:options].each do |opt, desc|
210         opts.add(opt, desc)
211       end
212       KDE::CmdLineArgs.add_cmd_line_options opts
213     end
215     KDE::Application.new
216   end
219 module ActionHandler
220   def std_action(action, opts = {}, &blk)
221     target, slot = get_slot(opts[:slot], &blk)
222     KDE::StandardAction.send(action, target, slot, action_collection)
223   end
224   
225   def get_slot(s = nil, &blk)
226     target, slot = if block_given?
227       [Qt::BlockInvocation.new(self, blk, 'invoke()'), SLOT(:invoke)]
228     else
229       [self, SLOT(s)]
230     end
231   end
232   
233   def regular_action(name, opts, &blk)
234     icon = if opts[:icon]
235       case opts[:icon]
236       when Qt::Icon
237         opts[:icon]
238       else
239         KDE::Icon.new(opts[:icon].to_s)
240       end
241     end
242     
243     KDE::Action.new(icon, opts[:text], self).tap do |a|
244       action_collection.add_action(name.to_s, a)
245       target, slot = get_slot(opts[:slot], &blk)
246       connect(a, SIGNAL('triggered(bool)'), target, slot)
247     end
248   end