Simple linear move list.
[kaya.git] / lib / qtutils.rb
blob772cbd146765674cfc96dd9bcdef4ae7750d5e21
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 module ModelUtils
190   def removing_rows(parent, first, last)
191     if first > last
192       yield
193     else
194       begin
195         begin_remove_rows(parent || Qt::ModelIndex.new, first, last)
196         yield
197       ensure
198         end_remove_rows
199       end
200     end
201   end
202   
203   def inserting_rows(parent, first, last)
204     if first > last
205       yield
206     else
207       begin
208         begin_insert_rows(parent || Qt::ModelIndex.new, first, last)
209         yield
210       ensure
211         end_insert_rows
212       end
213     end
214   end
217 class KDE::Application
218   def self.init(data)
219     about = KDE::AboutData.new(
220       data[:id],
221       data[:id],
222       data[:name],
223       data[:version],
224       data[:description],
225       KDE::AboutData::License_GPL,
226       data[:copyright])
227     data[:authors].each do |name, email|
228       about.addAuthor(name, KDE::LocalizedString.new, email)
229     end
230     data[:contributors].each do |name, contribution|
231       about.addCredit(name, contribution)
232     end
233     about.bug_address = Qt::ByteArray.new(data[:bug_tracker])
234     
235     KDE::CmdLineArgs.init(ARGV, about)
236     KDE::CmdLineOptions.new.tap do |opts|
237       data[:options].each do |opt, desc|
238         opts.add(opt, desc)
239       end
240       KDE::CmdLineArgs.add_cmd_line_options opts
241     end
243     KDE::Application.new
244   end
247 module ActionHandler
248   def std_action(action, opts = {}, &blk)
249     target, slot = get_slot(opts[:slot], &blk)
250     KDE::StandardAction.send(action, target, slot, action_collection)
251   end
252   
253   def get_slot(s = nil, &blk)
254     target, slot = if block_given?
255       [Qt::BlockInvocation.new(self, blk, 'invoke()'), SLOT(:invoke)]
256     else
257       [self, SLOT(s)]
258     end
259   end
260   
261   def regular_action(name, opts, &blk)
262     icon = if opts[:icon]
263       case opts[:icon]
264       when Qt::Icon
265         opts[:icon]
266       else
267         KDE::Icon.new(opts[:icon].to_s)
268       end
269     end
270     
271     KDE::Action.new(icon, opts[:text], self).tap do |a|
272       action_collection.add_action(name.to_s, a)
273       target, slot = get_slot(opts[:slot], &blk)
274       connect(a, SIGNAL('triggered(bool)'), target, slot)
275     end
276   end