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.
9 require 'plugins/loader'
11 class EngineData < KDE::Dialog
14 def initialize(caption, parent, engine = nil)
17 @gui = KDE::autogui(:engine_data,
18 :caption => caption) do |g|
19 g.layout(:type => :vertical) do |l|
20 labelled(l, :name, KDE::i18n("&Name:")) do |h|
23 labelled(l, :type, KDE::i18n("&Type:")) do |h|
26 labelled(l, :games, KDE::i18n("&Games:")) do |h|
27 h.widget(:games, :factory => Factory.new {|p| Game.new_combo(p) })
29 labelled(l, :path, KDE::i18n("&Path:")) do |h|
30 h.url_requester(:path)
32 labelled(l, :args, KDE::i18n("&Arguments:")) do |h|
35 labelled(l, :workdir, KDE::i18n("&Work directory:")) do |h|
36 h.url_requester(:workdir)
42 loader = PluginLoader.new
43 protocols = loader.get_all_matching(:engine).map do |klass|
46 protocols.each do |prot|
51 name.text = engine.name
52 path.url = KDE::Url.new(engine.path) if engine.path
53 args.text = engine.arguments if engine.arguments
54 workdir.url = KDE::Url.new(engine.workdir) if engine.workdir
55 current = (0...games.count).
56 map{|i| games.item_data(i).toString }.
57 index(engine.game.class.data(:id).to_s)
58 games.current_index = current if current
59 current = protocols.index(engine.protocol)
60 type.current_index = current if current
66 unless path.text.empty? or name.text.empty?
67 protocol = type.current_text
68 game = games.item_data(games.current_index).toString.to_sym
71 :protocol => type.current_text,
72 :arguments => args.text,
74 :path => path.url.path,
75 :workdir => workdir.url.path }
80 def labelled(builder, name, label)
81 builder.layout(:type => :horizontal) do |l|
82 l.label(:text => label, :buddy => name)
88 class EnginePrefs < KDE::Dialog
89 def initialize(loader, parent)
93 @gui = KDE::autogui(:engine_prefs,
94 :caption => KDE.i18n("Configure Engines")) do |g|
95 g.layout(:type => :horizontal) do |l|
97 l.layout(:type => :vertical) do |buttons|
98 buttons.button(:add_engine,
99 :text => KDE.i18nc("engine", "&New..."),
101 buttons.button(:edit_engine,
102 :text => KDE.i18nc("engine", "&Edit..."),
103 :icon => 'configure')
104 buttons.button(:delete_engine,
105 :text => KDE.i18nc("engine", "&Delete"),
106 :icon => 'list-remove')
113 list.model = Qt::StringListModel.new(
114 @loader.map{|name, engine| name }, self)
118 @loader.each do |name, engine|
119 @engines[name] = engine
122 add_engine.on(:clicked) { do_add_engine }
123 edit_engine.on(:clicked) { do_edit_engine }
124 delete_engine.on(:clicked) { do_delete_engine }
127 @loader.update_entries(@engines)
134 name = list.model.data(index, Qt::DisplayRole).toString
135 list.model.remove_rows(index.row, 1)
136 @engines.delete(name)
141 dialog = EngineData.new(KDE.i18n("New Engine"), self)
142 dialog.on(:ok) do |data|
143 engine = @loader.engine.new(data)
144 @engines[engine.name] = engine
145 index = list.model.row_count
146 list.model.insert_rows(index, 1)
147 list.model.set_data(list.model.index(index, 0),
148 Qt::Variant.new(engine.name),
157 old_name = list.model.data(index, Qt::DisplayRole).toString
158 old_engine = @engines[old_name]
159 dialog = EngineData.new(KDE.i18n("Edit Engine"), self, old_engine)
160 dialog.on(:ok) do |data|
161 engine = @loader.engine.new(data)
162 if engine.name != old_name
163 @engines.delete(old_name)
165 @engines[engine.name] = engine
166 list.model.set_data(index, Qt::Variant.new(engine.name), Qt::DisplayRole)
173 indexes = list.selection_model.selection.indexes
174 unless indexes.empty?