Use integer coordinates everywhere.
[kaya.git] / lib / qtutils.rb
blobf89b1cd922bc458bfe4145e39a735b674dd547cd
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   
78   def to_f
79     Qt::PointF.new(x, y)
80   end
81 end
83 class Qt::PointF
84   include PrintablePoint
85   
86   def to_i
87     Qt::Point.new(x.to_i, y.to_i)
88   end
89 end
91 class Qt::Size
92   include PrintablePoint
93   
94   def x
95     width
96   end
97   
98   def y
99     height
100   end
103 class Qt::Rect
104   include PrintableRect
107 class Qt::RectF
108   include PrintableRect
111 class Qt::Pixmap
112   def self.from_svg(size, file, id = nil)
113     from_renderer(size, Qt::SvgRenderer.new(file), id)
114   end
115   
116   def self.from_renderer(size, renderer, id = nil)
117     Qt::Image.from_renderer(size, renderer, id).to_pix
118   end
121 class Qt::Base
122   def self.signal_map(sigmap)
123     @signal_map = sigmap
124     signals *sigmap.map{|k, v| v || k }
125   end
127   def self.get_signal(sig)
128     (@signal_map || {})[sig] || sig
129   end
131   def on(sig, &blk)
132     connect(SIGNAL(self.class.get_signal(sig)), &blk)
133   end  
135   def in(interval, &blk)
136     Qt::Timer.in(interval, self, &blk)
137   end
139   def run_later(&blk)
140     self.in(0, &blk)
141   end
144 class Qt::Timer
145   def self.every(interval, &blk)
146     time = Qt::Time.new
147     time.restart
148     
149     timer = new
150     timer.connect(SIGNAL('timeout()')) { blk[time.elapsed] }
151     timer.start(interval)
152     # return the timer, so that the caller
153     # has a chance to keep it referenced, so
154     # that it is not garbage collected
155     timer
156   end
158   def self.in(interval, target = nil, &blk)
159     single_shot(interval,
160                 Qt::BlockInvocation.new(target, blk, 'invoke()'),
161                 SLOT('invoke()'))
162   end
165 class KDE::Application
166   def self.init(data)
167     about = KDE::AboutData.new(
168       data[:id],
169       data[:id],
170       data[:name],
171       data[:version],
172       data[:description],
173       KDE::AboutData::License_GPL,
174       data[:copyright])
175     data[:authors].each do |name, email|
176       about.addAuthor(name, KDE::LocalizedString.new, email)
177     end
178     data[:contributors].each do |name, contribution|
179       about.addCredit(name, contribution)
180     end
181     about.bug_address = Qt::ByteArray.new(data[:bug_tracker])
182     
183     KDE::CmdLineArgs.init(ARGV, about)
184     KDE::CmdLineOptions.new.tap do |opts|
185       data[:options].each do |opt, desc|
186         opts.add(opt, desc)
187       end
188       KDE::CmdLineArgs.add_cmd_line_options opts
189     end
191     KDE::Application.new
192   end
195 module ActionHandler
196   def std_action(action, opts = {}, &blk)
197     target, slot = get_slot(opts[:slot], &blk)
198     KDE::StandardAction.send(action, target, slot, action_collection)
199   end
200   
201   def get_slot(s = nil, &blk)
202     target, slot = if block_given?
203       [Qt::BlockInvocation.new(self, blk, 'invoke()'), SLOT(:invoke)]
204     else
205       [self, SLOT(s)]
206     end
207   end
208   
209   def regular_action(name, opts, &blk)
210     icon = if opts[:icon]
211       case opts[:icon]
212       when Qt::Icon
213         opts[:icon]
214       else
215         KDE::Icon.new(opts[:icon].to_s)
216       end
217     end
218     
219     KDE::Action.new(icon, opts[:text], self).tap do |a|
220       action_collection.add_action(name.to_s, a)
221       target, slot = get_slot(opts[:slot], &blk)
222       connect(a, SIGNAL('triggered(bool)'), target, slot)
223     end
224   end