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,
17 self.caption = KDE.i18n("New game")
18 self.buttons = KDE::Dialog::Ok | KDE::Dialog::Cancel
20 @widget = Qt::Widget.new(self)
22 @engine_loader = engine_loader
23 label = Qt::Label.new(KDE.i18n("&Game:"), @widget)
24 @games = Game.new_combo(@widget)
27 hlayout = Qt::HBoxLayout.new
28 hlayout.add_widget(label)
29 hlayout.add_widget(@games)
32 @layout = Qt::VBoxLayout.new(@widget)
33 @layout.add_layout(hlayout)
35 current = (0...@games.count).
36 map{|i| @games.item_data(i).toString }.
37 index(current_game.class.data(:id).to_s)
38 @games.current_index = current if current
39 @games.on('currentIndexChanged(int)') do |index|
43 update_players(@games.current_index)
45 self.main_widget = @widget
51 g.players.each do |player|
52 # iterate over the player array to preserve order
53 combo = @players[player]
54 data = combo.item_data(combo.current_index).to_a
55 if data.first == 'engine'
57 engines[player] = engine_loader[engine_name]
69 def update_players(index)
70 @player_widget.dispose if @player_widget
71 @player_widget = Qt::Widget.new(@widget)
72 layout = Qt::VBoxLayout.new(@player_widget)
74 @layout.add_widget(@player_widget)
78 engines = @engine_loader.find_by_game(g)
79 g.players.each do |player|
80 label = Qt::Label.new(player.to_s.capitalize + ':', @player_widget)
81 combo = KDE::ComboBox.new(@player_widget) do
83 add_item(KDE.i18n('Human'), Qt::Variant.new(['human']))
85 engines.each do |id, engine|
86 combo.add_item(engine.name, Qt::Variant.new(['engine', engine.name]))
88 @players[player] = combo
90 hlayout = Qt::HBoxLayout.new
91 hlayout.add_widget(label)
92 hlayout.add_widget(combo)
93 layout.add_layout(hlayout)
98 index = @games.current_index if index.nil?
99 game_id = @games.item_data(index).toString.to_sym