Add action list support to qtonly.
[kaya.git] / lib / toolkits / kde.rb
blob0c6f32534f1a49d85494430fc25554e9cf7e5feb
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'
9 require 'builder'
10 begin 
11   require 'kio' 
12 rescue LoadError
13 end
15 class KDE::Dialog
16   include Layoutable
17   
18   def setGUI(gui)
19     self.caption = gui.opts[:caption]
20     widget = Qt::Widget.new(self)
21     widget.owner = self
22     widget.setGUI(gui)
23     self.main_widget = widget
24   end
25 end
27 class KDE::Application
28   # 
29   # Initialize an application.
30   # 
31   def self.init(data)
32     about = KDE::AboutData.new(
33       data[:id],
34       data[:id],
35       data[:name] || KDE::ki18n(data[:id]),
36       data[:version] || "0.1",
37       data[:description] || KDE::ki18n(""),
38       KDE::AboutData::License_GPL,
39       data[:copyright] || KDE::ki18n(""))
40     (data[:authors] || []).each do |name, email|
41       about.addAuthor(name, KDE::LocalizedString.new, email)
42     end
43     (data[:contributors] || []).each do |name, contribution|
44       about.addCredit(name, contribution)
45     end
46     about.bug_address = Qt::ByteArray.new(data[:bug_tracker] || "")
47     
48     KDE::CmdLineArgs.init(ARGV, about)
49     KDE::CmdLineOptions.new.tap do |opts|
50       (data[:options] || []).each do |args|
51         case args.size
52         when 2
53           opts.add(args[0], args[1])
54         when 3
55           opts.add(args[0], args[1], args[2])
56         end
57       end
58       KDE::CmdLineArgs.add_cmd_line_options opts
59     end
61     KDE::Application.new
62   end
63 end
65 class KDE::CmdLineArgs
66   def [](i)
67     arg(i)
68   end
69   
70   def is_set(name)
71     isSet(Qt::ByteArray.new(name))
72   end
73 end
75 class KDE::ActionCollection  
76   def []=(name, action)
77     unless action.is_a? KDE::Action
78       orig_action = action
79       action = KDE::Action.new(action.text, action.parent)
80       action.icon = orig_action.icon
81       action.checkable = orig_action.checkable
82       action.checked = orig_action.checked
83       action.on(:triggered) { orig_action.trigger }
84       orig_action.on(:changed) { action.checked = orig_action.checked }
85     end
86     add_action(name.to_s, action)
87   end
88 end
90 class KDE::XmlGuiWindow
91   def setGUI(gui)
92     KDE::with_xml_gui(gui) do |file|
93       setupGUI(KDE::XmlGuiWindow::Default, file)
94     end
95   end
96   
97   def saveGUI
98   end
99 end
101 class KDE::XMLGUIClient
102   def setGUI(gui)
103     KDE::with_xml_gui(gui) do |file|
104       setXMLFile(file)
105     end
106   end
109 module ActionHandler
110   def std_action(action, opts = {}, &blk)
111     target, slot = get_slot(opts[:slot], &blk)
112     KDE::StandardAction.method_missing(action, target, slot, action_collection)
113   end
114   
115   def get_slot(s = nil, &blk)
116     target, slot = if block_given?
117       [Qt::SignalBlockInvocation.new(action_parent, blk, 'invoke()'), SLOT('invoke()')]
118     else
119       [action_parent, SLOT(s)]
120     end
121   end
122   
123   def regular_action(name, opts, &blk)
124     KDE::Action.new(KDE::Icon.from_theme(opts[:icon]), 
125                     opts[:text], action_parent).tap do |a|
126       action_collection.add_action(name.to_s, a)  
127       a.connect(SIGNAL('triggered(bool)'), &blk)
128       a.tool_tip = opts[:tooltip] if opts[:tooltip]
129       a.shortcut = opts[:shortcut] if opts[:shortcut]
130     end
131   end
132   
133   def action_parent
134     self
135   end
137   def plug_action_list(name, actions)
138     plugActionList(name.to_s, actions)
139   end
141   def unplug_action_list(name)
142     unplugActionList(name.to_s)
143   end
146 class KDE::Icon
147   def self.from_theme(name)
148     if name
149       new(name.to_s)
150     else
151       new
152     end
153   end
156 class KDE::ConfigGroup
157   def each_group
158     group_list.each do |g|
159       yield group(g)
160     end
161   end
164 module KDE
165   def self.gui(name, &blk)
166     "<!DOCTYPE kpartgui SYSTEM \"kpartgui.dtd\">\n" + 
167     GuiBuilder.new.gui({ :version => 2, :name => name }, &blk)
168   end
169   
170   def self.with_xml_gui(xml, &blk)
171     tmp = TemporaryFile.new
172     tmp.open
173     
174     ::File.open(tmp.file_name, 'w') do |f|
175       f.write(xml)
176     end
177     blk[tmp.file_name]
178   ensure
179     tmp.close
180     ::File.unlink(tmp.file_name)
181   end
182   
183   class GuiBuilder < Builder::XmlMarkup
184     def initialize
185       super
186       @action_opts = { }
187     end
189     def menu_bar(&blk)
190       MenuBar(&blk)
191     end
192     
193     def menu(name, opts = {}, &blk)
194       Menu(:name => name) do |m|
195         m.text(opts[:text]) if opts[:text]
196         blk[m] if block_given?
197       end
198     end
199     
200     def action(name, opts = {})
201       Action(opts.merge(@action_opts).merge(:name => name))
202     end
203     
204     def separator
205       self.Separator
206     end
207     
208     def tool_bar(name, opts = { }, &blk)
209       ToolBar(:name => name) do |tb|
210         tb.text(opts[:text]) if opts[:text]
211         blk[tb] if block_given?
212       end
213     end
214     
215     def action_list(name)
216       ActionList(:name => name)
217     end
218     
219     def group(name, &blk)
220       if block_given?
221         @action_opts = { :group => name }
222         blk[self]
223         @action_opts = { }
224       else
225         DefineGroup(:name => name)
226       end
227     end
228   end
229   
230   def self.active_color
231     scheme = KDE::ColorScheme.new(Qt::Palette::Active, KDE::ColorScheme::Window)
232     color = scheme.foreground(KDE::ColorScheme::PositiveText).color
233   end
234   
235   def self.std_shortcut(name)
236     code = KDE::StandardShortcut.send(name.to_s.capitalize)
237     StandardShortcut::shortcut(code)
238   end
241 class KDE::TabWidget
242   include Layoutable
245 class KDE::Process
246   def output_channel_mode=(value)
247     c = self.class.const_get("#{value.to_s.capitalize.camelize}Channel")
248     setOutputChannelMode(c)
249   end
250   
251   def self.split_args(str)
252     KDE::Shell.split_args(str)
253   end
254   
255   def run(path, args)
256     set_program(path, args)
257     start
258   end
261 def KDE.download_tempfile(url, parent)
262   result = ""
263   if KIO::NetAccess.download(url, result, parent)
264     result
265   end