1 # Copyright (c) 2009 Paolo Capriotti <p.capriotti@gmail.com>
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.
19 def self.i18nc(context, str)
24 $qApp.palette.color(Qt::Palette::Highlight)
27 def self.std_shortcut(name)
28 code = Qt::KeySequence.send(name.to_s.capitalize)
29 Qt::KeySequence.new(code)
33 Qt::XmlGuiWindow = Qt::MainWindow
35 class Qt::UrlRequester < Qt::LineEdit
37 self.text = val.to_string
48 def initialize(parent)
51 setToolButtonStyle(Qt::ToolButtonFollowStyle)
55 @gui = Qt::gui(:qt_base) do |g|
58 mb.menu(:settings, :text => KDE::i18n("&Settings"))
59 mb.menu(:help, :text => KDE::i18n("&Help")) do |m|
68 regular_action(:about, :text => KDE::i18n("&About")) do
69 Qt::MessageBox.about(nil,
71 [$qApp.data[:description],
72 $qApp.data[:copyright]].join("\n"))
74 regular_action(:about_qt, :text => KDE::i18n("About &Qt")) { $qApp.about_qt }
77 @guis.each {|g| @gui.merge! g }
78 Qt::GuiBuilder.build(self, @gui)
81 settings = Qt::Settings.new
84 if settings.contains("mainwindow/state")
85 state = settings.value("mainwindow/state").toByteArray
86 geometry = settings.value("mainwindow/geometry").toByteArray
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})
102 restore_geometry(geometry)
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))
116 self.window_title = $qApp.application_name.capitalize +
125 self.window_title = gui.opts[:caption]
126 layout = Qt::VBoxLayout.new(self)
127 widget = Qt::Widget.new(self)
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)
136 buttons.on(:accepted) { fire :ok_clicked; accept }
137 buttons.on(:rejected) { reject }
141 class Qt::XMLGUIClient < Qt::Object
148 def action_collection
149 @action_collection ||= { }
152 def action_list_entries
153 @action_list_entries ||= Hash.new {|h, x| h[x] = [] }
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)
165 def unplug_action_list(name)
166 action_list_entries[name].each do |entry|
171 def add_action(name, a)
172 action_parent.action_collection[name] = a
175 def std_action(name, &blk)
176 text, icon_name = Qt::STD_ACTIONS[name]
178 icon = Qt::Icon.from_theme(icon_name)
179 a = Qt::Action.new(icon, text, action_parent)
181 a.on(:triggered, &blk)
186 def regular_action(name, opts = { }, &blk)
187 a = Qt::Action.new(opts[:text], action_parent)
189 a.on(:triggered, &blk)
191 a.icon = Qt::Icon.from_theme(opts[:icon])
193 a.shortcut = opts[:shortcut] if opts[:shortcut]
194 a.tool_tip = opts[:tooltip] if opts[:tooltip]
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']
214 def self.gui(name, opts = { }, &blk)
215 self.autogui(name, opts, &blk)
219 class Qt::Application
223 new(ARGV).tap do |app|
224 app.application_name = data[:id]
225 app.organization_name = data[:id]
231 class KDE::CmdLineArgs
255 Qt::Settings::Group.new(Qt::Settings.new, "")
261 def initialize(settings, prefix)
268 not @settings.all_keys.empty?
273 @settings.remove(@prefix)
277 Group.new(@settings, prefixed(name))
280 def write_entry(key, value)
281 @settings.set_value(prefixed(key),
282 Qt::Variant.new(value))
285 def read_entry(key, default_value = nil)
286 @settings.value(prefixed(key)).toString || default_value
295 @settings.child_groups
301 @settings.child_keys.inject({}) do |res, key|
302 res[key] = @settings.value(key).toString
310 @settings.child_groups
318 if @prefix =~ /\/([^\/]+)$/
331 [@prefix, key].join('/')
336 @settings.begin_group(@prefix)
349 def output_channel_mode=(val)
352 setProcessChannelMode(Qt::Process::SeparateChannels)
353 setReadChannel(Qt::Process::StandardOutput)
355 raise "Unsupported output mode #{val}"
359 def self.split_args(str)