Update connect/disconnect action states.
[kaya.git] / lib / toolkits / compat / qtkde.rb
blob7493c12413f641c38823abba7e3e36620cad20a6
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(name)
153     @action_collection[name.to_sym]
154   end
156   def action_list_entries
157     @action_list_entries ||= Hash.new {|h, x| h[x] = [] }
158   end
160   def plug_action_list(name, actions)
161     action_list_entries[name].each do |entry|
162       actions.each do |action|
163         puts "adding action to #{entry.parent}: #{action.text}"
164         entry.add_action(action)
165       end
166     end
167   end
169   def unplug_action_list(name)
170     action_list_entries[name].each do |entry|
171       entry.clear
172     end
173   end
174   
175   def add_action(name, a)
176     action_parent.action_collection[name] = a
177   end
178   
179   def std_action(name, &blk)
180     text, icon_name = Qt::STD_ACTIONS[name]
181     if text
182       icon = Qt::Icon.from_theme(icon_name)
183       a = Qt::Action.new(icon, text, action_parent)
184       add_action(name, a)
185       a.on(:triggered, &blk)
186       a
187     end
188   end
189   
190   def regular_action(name, opts = { }, &blk)
191     a = Qt::Action.new(opts[:text], action_parent)
192     add_action(name, a)
193     a.on(:triggered, &blk)
194     if (opts[:icon])
195       a.icon = Qt::Icon.from_theme(opts[:icon])
196     end
197     a.shortcut = opts[:shortcut] if opts[:shortcut]
198     a.tool_tip = opts[:tooltip] if opts[:tooltip]
199     a.enabled = opts.fetch(:enabled, true)
200     a
201   end
202   
203   def action_parent
204     self
205   end
208 module Qt
209   STD_ACTIONS = {
210 #     :open_new => [KDE::i18n("&New..."), 'document-new'],
211 #     :open => [KDE::i18n("&Open..."), 'document-open'],
212 #     :quit => [KDE::i18n("&Quit"), 'application-exit'],
213 #     :save => [KDE::i18n("&Save"), 'document-save'],
214 #     :save_as => [KDE::i18n("S&ave as..."), 'document-save-as'],
215     :undo => [KDE::i18n("&Undo"), 'edit-undo'],
216     :redo => [KDE::i18n("&Redo"), 'edit-redo']
217   }
218   
219   def self.gui(name, opts = { }, &blk)
220     self.autogui(name, opts, &blk)
221   end
224 class Qt::Application
225   attr_accessor :data
226   
227   def self.init(data)
228     new(ARGV).tap do |app|
229       app.application_name = data[:id]
230       app.organization_name = data[:id]
231       app.data = data
232     end
233   end
236 class KDE::CmdLineArgs
237   def self.parsed_args
238     new(ARGV)
239   end
240   
241   def initialize(args)
242     @args = args
243   end
244   
245   def [](i)
246     @args[i]
247   end
248   
249   def count
250     @args.size
251   end
252   
253   def is_set(name)
254     false
255   end
258 class KDE::Global
259   def self.config
260     Qt::Settings::Group.new(Qt::Settings.new, "")
261   end
264 class Qt::Settings
265   class Group
266     def initialize(settings, prefix)
267       @settings = settings
268       @prefix = prefix
269     end
270     
271     def exists
272       in_group do
273         not @settings.all_keys.empty?
274       end
275     end
276     
277     def delete_group
278       @settings.remove(@prefix)
279     end
280       
281     def group(name)
282       Group.new(@settings, prefixed(name))
283     end
284     
285     def write_entry(key, value)
286       @settings.set_value(prefixed(key), 
287                           Qt::Variant.new(value))
288     end
289     
290     def read_entry(key, default_value = nil)
291       @settings.value(prefixed(key)).toString || default_value
292     end
293     
294     def sync
295       @settings.sync
296     end
297     
298     def group_list
299       in_group do
300         @settings.child_groups
301       end
302     end
303     
304     def entry_map
305       in_group do
306         @settings.child_keys.inject({}) do |res, key|
307           res[key] = @settings.value(key).toString
308           res
309         end
310       end
311     end
312     
313     def each_group
314       names = in_group do
315         @settings.child_groups
316       end
317       names.each do |name|
318         yield group(name)
319       end
320     end
321     
322     def name
323       if @prefix =~ /\/([^\/]+)$/
324         $1
325       else
326         @prefix
327       end
328     end
329     
330     private
331     
332     def prefixed(key)
333       if @prefix.empty?
334         key
335       else
336         [@prefix, key].join('/')
337       end
338     end
339     
340     def in_group
341       @settings.begin_group(@prefix)
342       result = yield
343       @settings.end_group
344       result
345     end
346   end
349 class Qt::TabWidget
350   include Layoutable
353 class Qt::Process
354   def output_channel_mode=(val)
355     case val
356     when :only_stdout
357       setProcessChannelMode(Qt::Process::SeparateChannels)
358       setReadChannel(Qt::Process::StandardOutput)
359     else
360       raise "Unsupported output mode #{val}"
361     end
362   end
363   
364   def self.split_args(str)
365     str.split(/\s+/)
366   end
367   
368   def run(path, args)
369     start(path, args)
370   end