Add navigation actions.
[kaya.git] / lib / qtutils.rb
blob98d2b252bd44a40035edb1bc16eb11cac72335f5
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 class Qt::Painter
21   def paint
22     yield self
23   ensure
24     self.end
25   end
26   
27   def saving
28     save
29     yield self
30   ensure
31     restore
32   end
33 end
35 class Qt::Image
36   def to_pix
37     Qt::Pixmap.from_image self
38   end
39   
40   def self.painted(size, &blk)
41     Qt::Image.new(size.x, size.y, Qt::Image::Format_ARGB32_Premultiplied).tap do |img|
42       img.fill(0)
43       Qt::Painter.new(img).paint(&blk)
44     end
45   end
47   def self.from_renderer(size, renderer, id = nil)
48     img = Qt::Image.painted(size) do |p| 
49       if id
50         renderer.render(p, id)
51       else
52         renderer.render(p)
53       end
54     end
55     img
56   end
57 end
59 module PrintablePoint
60   def ==(other)
61     self.x == other.x and self.y == other.y
62   end
63   
64   def to_s
65     "(#{self.x}, #{self.y})"
66   end
67 end
69 module PrintableRect
70   def to_s
71     "[#{self.x}, #{self.y} - #{self.width}, #{self.height}]"
72   end
73 end
75 class Qt::Point
76   include PrintablePoint
77 end
79 class Qt::PointF
80   include PrintablePoint
81 end
83 class Qt::Size
84   include PrintablePoint
85   
86   def x
87     width
88   end
89   
90   def y
91     height
92   end
93 end
95 class Qt::Rect
96   include PrintableRect
97 end
99 class Qt::RectF
100   include PrintableRect
103 class Qt::Pixmap
104   def self.from_svg(size, file, id = nil)
105     from_renderer(size, Qt::SvgRenderer.new(file), id)
106   end
107   
108   def self.from_renderer(size, renderer, id = nil)
109     Qt::Image.from_renderer(size, renderer, id).to_pix
110   end
113 class Qt::Base
114   def self.signal_map(sigmap)
115     @signal_map = sigmap
116     signals *sigmap.map{|k, v| v || k }
117   end
119   def self.get_signal(sig)
120     (@signal_map || {})[sig] || sig
121   end
123   def on(sig, &blk)
124     connect(SIGNAL(self.class.get_signal(sig)), &blk)
125   end  
127   def in(interval, &blk)
128     Qt::Timer.in(interval, self, &blk)
129   end
131   def run_later(&blk)
132     self.in(0, &blk)
133   end
136 class Qt::Timer
137   def self.every(interval, &blk)
138     time = Qt::Time.new
139     time.restart
140     
141     timer = new
142     timer.connect(SIGNAL('timeout()')) { blk[time.elapsed] }
143     timer.start(interval)
144     # return the timer, so that the caller
145     # has a chance to keep it referenced, so
146     # that it is not garbage collected
147     timer
148   end
150   def self.in(interval, target = nil, &blk)
151     single_shot(interval,
152                 Qt::BlockInvocation.new(target, blk, 'invoke()'),
153                 SLOT('invoke()'))
154   end
157 class KDE::Application
158   def self.init(data)
159     about = KDE::AboutData.new(
160       data[:id],
161       data[:id],
162       data[:name],
163       data[:version],
164       data[:description],
165       KDE::AboutData::License_GPL,
166       data[:copyright])
167     data[:authors].each do |name, email|
168       about.addAuthor(name, KDE::LocalizedString.new, email)
169     end
170     data[:contributors].each do |name, contribution|
171       about.addCredit(name, contribution)
172     end
173     about.bug_address = Qt::ByteArray.new(data[:bug_tracker])
174     
175     KDE::CmdLineArgs.init(ARGV, about)
176     KDE::CmdLineOptions.new.tap do |opts|
177       data[:options].each do |opt, desc|
178         opts.add(opt, desc)
179       end
180       KDE::CmdLineArgs.add_cmd_line_options opts
181     end
183     KDE::Application.new
184   end
187 module ActionHandler
188   def std_action(action, opts = {}, &blk)
189     target, slot = get_slot(opts[:slot], &blk)
190     KDE::StandardAction.send(action, target, slot, action_collection)
191   end
192   
193   def get_slot(s = nil, &blk)
194     target, slot = if block_given?
195       [Qt::BlockInvocation.new(self, blk, 'invoke()'), SLOT(:invoke)]
196     else
197       [self, SLOT(s)]
198     end
199   end
200   
201   def regular_action(name, opts, &blk)
202     icon = if opts[:icon]
203       case opts[:icon]
204       when Qt::Icon
205         opts[:icon]
206       else
207         KDE::Icon.new(opts[:icon].to_s)
208       end
209     end
210     
211     KDE::Action.new(icon, opts[:text], self).tap do |a|
212       action_collection.add_action(name.to_s, a)
213       target, slot = get_slot(opts[:slot], &blk)
214       connect(a, SIGNAL('triggered(bool)'), target, slot)
215     end
216   end