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.
10 class NewGame < KDE::Dialog
13 def initialize(parent, engine_loader, current_game)
15 @engine_loader = engine_loader
18 gui = KDE::autogui(:new_game, :caption => KDE::i18n("New game")) do |g|
19 g.layout(:type => :vertical) do |l|
20 l.check_box(:new_tab, :text => KDE::i18n("Open in new &tab"))
22 l.layout(:type => :horizontal) do |h|
23 h.label(:text => KDE::i18n("&Game:"), :buddy => :games)
24 h.widget(:games, :factory => Factory.new{|p| Game.new_combo(p)})
27 l.widget(:player_widget, :factory => Qt::Widget)
33 current = (0...games.count).
34 map{|i| games.item_data(i).toString }.
35 index(current_game.class.data(:id).to_s)
37 games.current_index = current if current
38 games.on(:current_index_changed) do |index|
42 update_players(games.current_index)
48 g.players.each do |player|
49 # iterate over the player array to preserve order
50 combo = @players[player]
51 data = combo.item_data(combo.current_index).to_a
52 if data.first == 'engine'
54 engines[player] = engine_loader[engine_name]
63 :new_tab => new_tab.checked? }
67 def update_players(index)
68 parent = player_widget.parent
70 player_widget = Qt::Widget.new(parent)
71 main_layout.add_widget(player_widget)
75 engines = @engine_loader.find_by_game(g)
77 gui = KDE::autogui(:player_widget) do |b|
78 b.layout(:type => :vertical, :margin => 0) do |l|
79 g.players.each do |player|
80 l.layout(:type => :horizontal) do |h|
81 h.label(:text => "&#{g.translate(player)}:",
88 player_widget.setGUI(gui)
89 parent.owner.add_accessor(:player_widget, player_widget)
91 g.players.each do |player|
92 combo = player_widget.send(player)
93 combo.editable = false
94 combo.add_item(KDE.i18n('Human'), Qt::Variant.new(['human']))
96 engines.each do |id, engine|
97 combo.add_item(engine.name, Qt::Variant.new(['engine', engine.name]))
99 @players[player] = combo
103 def game(index = nil)
104 index = games.current_index if index.nil?
105 game_id = games.item_data(index).toString.to_sym