Add action list support to qtonly.
[kaya.git] / lib / toolkits / compat / qtkde.rb
blob9290e55003d188a5b4263b5747e66d877f8eaff0
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 'toolkits/qt'
10 module KDE
11   def self.ki18n(str)
12     str
13   end
15   def self.i18n(str)
16     str
17   end
18   
19   def self.i18nc(context, str)
20     str
21   end
22   
23   def self.active_color
24     $qApp.palette.color(Qt::Palette::Highlight)
25   end
26   
27   def self.std_shortcut(name)
28     code = Qt::KeySequence.send(name.to_s.capitalize)
29     Qt::KeySequence.new(code)
30   end
31 end
33 Qt::XmlGuiWindow = Qt::MainWindow
35 class Qt::UrlRequester < Qt::LineEdit
36   def url=(val)
37     self.text = val.to_string
38   end
39   
40   def url
41     Qt::Url.new(text)
42   end
43 end
45 class Qt::MainWindow
46   attr_reader :guis
47   
48   def initialize(parent)
49     super(parent)
50     
51     setToolButtonStyle(Qt::ToolButtonFollowStyle)
52     
53     # create basic GUI
54     @guis = []
55     @gui = Qt::gui(:qt_base) do |g|
56       g.menu_bar do |mb|
57         mb.merge_point
58         mb.menu(:settings, :text => KDE::i18n("&Settings"))
59         mb.menu(:help, :text => KDE::i18n("&Help")) do |m|
60           m.action :about
61           m.action :about_qt
62         end
63       end
64     end
65   end
66   
67   def setGUI(gui)
68     regular_action(:about, :text => KDE::i18n("&About")) do
69       Qt::MessageBox.about(nil,
70                            $qApp.data[:name],
71                            [$qApp.data[:description],
72                             $qApp.data[:copyright]].join("\n"))
73     end
74     regular_action(:about_qt, :text => KDE::i18n("About &Qt")) { $qApp.about_qt }
75     
76     @gui.merge!(gui)
77     @guis.each {|g| @gui.merge! g }
78     Qt::GuiBuilder.build(self, @gui)
79     
80     # restore state
81     settings = Qt::Settings.new
82     state = nil
83     geometry = nil
84     if settings.contains("mainwindow/state")
85       state = settings.value("mainwindow/state").toByteArray
86       geometry = settings.value("mainwindow/geometry").toByteArray
87     else
88       # reasonable default values
89       state = Qt::ByteArray.from_hex(%{
90         000000ff00000000fd0000000100000000000000b2000002e4fc0200000001fc000000
91         33000002e4000000a60100001efa000000010100000002fb0000000e0063006f006e00
92         73006f006c00650100000000ffffffff0000005101000005fb00000010006d006f0076
93         0065006c0069007300740100000000000000510000004e01000005000002d8000002e4
94         00000004000000040000000800000008fc000000010000000200000002000000160067
95         0061006d00650054006f006f006c0062006100720100000000ffffffff000000000000
96         000000000016006d00610069006e0054006f006f006c004200610072010000007b0000
97         03120000000000000000})
98       geometry = Qt::ByteArray.from_hex(%{
99         01d9d0cb0001000000000000000000000000039400000335000000040000001b000003
100         9000000331000000000000})
101     end
102     restore_geometry(geometry)
103     restore_state(state)
104   end
106   def saveGUI
107     settings = Qt::Settings.new
108     settings.begin_group("mainwindow")
109     settings.set_value("geometry", Qt::Variant.new(save_geometry))
110     settings.set_value("state", Qt::Variant.new(save_state))
111     settings.end_group
112     settings.sync
113   end
114   
115   def caption=(title)
116     self.window_title = $qApp.application_name.capitalize + 
117         " - " + title
118   end
121 class Qt::Dialog
122   include Layoutable
123   
124   def setGUI(gui)
125     self.window_title = gui.opts[:caption]
126     layout = Qt::VBoxLayout.new(self)
127     widget = Qt::Widget.new(self)
128     widget.owner = self
129     widget.setGUI(gui)
130     buttons = Qt::DialogButtonBox.new
131     buttons.add_button(Qt::DialogButtonBox::Ok)
132     buttons.add_button(Qt::DialogButtonBox::Cancel)
133     layout.add_widget(widget)
134     layout.add_widget(buttons)
135     
136     buttons.on(:accepted) { fire :ok_clicked; accept }
137     buttons.on(:rejected) { reject }
138   end
141 class Qt::XMLGUIClient < Qt::Object
142   def setGUI(gui)
143     parent.guis << gui
144   end
147 module ActionHandler
148   def action_collection
149     @action_collection ||= { }
150   end
152   def action_list_entries
153     @action_list_entries ||= Hash.new {|h, x| h[x] = [] }
154   end
156   def plug_action_list(name, actions)
157     action_list_entries[name].each do |entry|
158       actions.each do |action|
159         puts "adding action to #{entry.parent}: #{action.text}"
160         entry.add_action(action)
161       end
162     end
163   end
165   def unplug_action_list(name)
166     action_list_entries[name].each do |entry|
167       entry.clear
168     end
169   end
170   
171   def add_action(name, a)
172     action_parent.action_collection[name] = a
173   end
174   
175   def std_action(name, &blk)
176     text, icon_name = Qt::STD_ACTIONS[name]
177     if text
178       icon = Qt::Icon.from_theme(icon_name)
179       a = Qt::Action.new(icon, text, action_parent)
180       add_action(name, a)
181       a.on(:triggered, &blk)
182       a
183     end
184   end
185   
186   def regular_action(name, opts = { }, &blk)
187     a = Qt::Action.new(opts[:text], action_parent)
188     add_action(name, a)
189     a.on(:triggered, &blk)
190     if (opts[:icon])
191       a.icon = Qt::Icon.from_theme(opts[:icon])
192     end
193     a.shortcut = opts[:shortcut] if opts[:shortcut]
194     a.tool_tip = opts[:tooltip] if opts[:tooltip]
195     a
196   end
197   
198   def action_parent
199     self
200   end
203 module Qt
204   STD_ACTIONS = {
205 #     :open_new => [KDE::i18n("&New..."), 'document-new'],
206 #     :open => [KDE::i18n("&Open..."), 'document-open'],
207 #     :quit => [KDE::i18n("&Quit"), 'application-exit'],
208 #     :save => [KDE::i18n("&Save"), 'document-save'],
209 #     :save_as => [KDE::i18n("S&ave as..."), 'document-save-as'],
210     :undo => [KDE::i18n("&Undo"), 'edit-undo'],
211     :redo => [KDE::i18n("&Redo"), 'edit-redo']
212   }
213   
214   def self.gui(name, opts = { }, &blk)
215     self.autogui(name, opts, &blk)
216   end
219 class Qt::Application
220   attr_accessor :data
221   
222   def self.init(data)
223     new(ARGV).tap do |app|
224       app.application_name = data[:id]
225       app.organization_name = data[:id]
226       app.data = data
227     end
228   end
231 class KDE::CmdLineArgs
232   def self.parsed_args
233     new(ARGV)
234   end
235   
236   def initialize(args)
237     @args = args
238   end
239   
240   def [](i)
241     @args[i]
242   end
243   
244   def count
245     @args.size
246   end
247   
248   def is_set(name)
249     false
250   end
253 class KDE::Global
254   def self.config
255     Qt::Settings::Group.new(Qt::Settings.new, "")
256   end
259 class Qt::Settings
260   class Group
261     def initialize(settings, prefix)
262       @settings = settings
263       @prefix = prefix
264     end
265     
266     def exists
267       in_group do
268         not @settings.all_keys.empty?
269       end
270     end
271     
272     def delete_group
273       @settings.remove(@prefix)
274     end
275       
276     def group(name)
277       Group.new(@settings, prefixed(name))
278     end
279     
280     def write_entry(key, value)
281       @settings.set_value(prefixed(key), 
282                           Qt::Variant.new(value))
283     end
284     
285     def read_entry(key, default_value = nil)
286       @settings.value(prefixed(key)).toString || default_value
287     end
288     
289     def sync
290       @settings.sync
291     end
292     
293     def group_list
294       in_group do
295         @settings.child_groups
296       end
297     end
298     
299     def entry_map
300       in_group do
301         @settings.child_keys.inject({}) do |res, key|
302           res[key] = @settings.value(key).toString
303           res
304         end
305       end
306     end
307     
308     def each_group
309       names = in_group do
310         @settings.child_groups
311       end
312       names.each do |name|
313         yield group(name)
314       end
315     end
316     
317     def name
318       if @prefix =~ /\/([^\/]+)$/
319         $1
320       else
321         @prefix
322       end
323     end
324     
325     private
326     
327     def prefixed(key)
328       if @prefix.empty?
329         key
330       else
331         [@prefix, key].join('/')
332       end
333     end
334     
335     def in_group
336       @settings.begin_group(@prefix)
337       result = yield
338       @settings.end_group
339       result
340     end
341   end
344 class Qt::TabWidget
345   include Layoutable
348 class Qt::Process
349   def output_channel_mode=(val)
350     case val
351     when :only_stdout
352       setProcessChannelMode(Qt::Process::SeparateChannels)
353       setReadChannel(Qt::Process::StandardOutput)
354     else
355       raise "Unsupported output mode #{val}"
356     end
357   end
358   
359   def self.split_args(str)
360     str.split(/\s+/)
361   end
362   
363   def run(path, args)
364     start(path, args)
365   end