#include #include #include #include #include #include #include #include #include #include #include #include #include "team.h" #include "localplayer.h" #include "newgame.h" newGameDialog::newGameDialog(TQWidget *parent, const char *name) : KDialogBase(parent, name, true, i18n("New Game"), Ok|Cancel) { _page = new TQWidget( this ); // Just makes it look nicer setMinimumSize(400, 300); setMainWidget(_page); _top_layout = new TQVBoxLayout(_page, 0, KDialogBase::spacingHint()); _game_type = new KComboBox(_page); // Get a list of available rules plugins _plugins_list = KuePluginLoader::available(); TQValueList::const_iterator plugin = _plugins_list.begin(); // Go over the plugins that KuePluginLoader::available claims we have for(;plugin != _plugins_list.end();plugin++) { // We only want plugins of the type "rulesengine" if ((*plugin).type == "rulesengine") _game_type->insertItem((*plugin).name); } connect(_game_type, TQ_SIGNAL(activated(int)), this, TQ_SLOT(slotNewPluginSelection())); // Set up an hbox to put the combo and its label in TQHBoxLayout *hbox = new TQHBoxLayout(_top_layout, KDialogBase::spacingHint()); hbox->addWidget(new TQLabel(_game_type, i18n("Game &rules:"), _page)); hbox->addWidget(_game_type); hbox->setStretchFactor(_game_type, 2); // Add a separator _top_layout->addWidget(new KSeparator(KSeparator::HLine, _page)); // Add the players widget _player_widget = new TQWidget(_page); _player_layout = new TQGridLayout(_player_widget); _player_layout->setSpacing(KDialogBase::spacingHint()); _top_layout->addWidget(_player_widget); // Buttons to add/remove players _add_player_button = new TQPushButton(i18n("&Add Player"), _page); connect(_add_player_button, TQ_SIGNAL(clicked()), this, TQ_SLOT(addPlayerRow())); _remove_player_button = new TQPushButton(i18n("Remove &Player"), _page); connect(_remove_player_button, TQ_SIGNAL(clicked()), this, TQ_SLOT(removePlayerRow())); // Set up a second hbox for the add/remove player buttons hbox = new TQHBoxLayout(_top_layout, KDialogBase::spacingHint()); hbox->addStretch(2); hbox->addWidget(_add_player_button); hbox->addWidget(_remove_player_button); // This dialog is slightly less confusing with separator enableButtonSeparator(true); // Populates the player widget initially, and sets up the player // buttons' enabled state slotNewPluginSelection(); } newGameDialog::~newGameDialog() { } void newGameDialog::slotOk() { KDialogBase::slotOk(); } KuePluginInfo newGameDialog::selectedPlugin() { // Find the plugin the user requested TQValueList::const_iterator plugin = _plugins_list.begin(); for(;plugin != _plugins_list.end();plugin++) { if (((*plugin).name == _game_type->currentText()) && ((*plugin).type == "rulesengine")) return *plugin; } kdWarning() << "Unable to find KuePluginInfo matching user selection" << endl; // Oops return KuePluginInfo(); } TQValueList newGameDialog::selectedTeams() { TQValueList ret; while (_players.count()) { KueTeam *temp = new KueTeam; playerRow *row = _players.first(); // Add the player temp->addPlayer(new KueLocalPlayer(row->nameEdit->text(), row->colorButton->color())); ret.append(temp); // Remove the row removePlayerRow(row); } return ret; } void newGameDialog::addPlayerRow() { playerRow *row = new playerRow; int new_row = _players.count(); row->label = new TQLabel(i18n("Player %1:").arg(new_row + 1), _player_widget); row->nameEdit = new KLineEdit(i18n("Player %1").arg(new_row + 1), _player_widget); row->colorButton = new KColorButton(defaultPlayerColor(new_row), _player_widget); _player_layout->addWidget(row->label, new_row, 0); _player_layout->addWidget(row->nameEdit, new_row, 1); _player_layout->addWidget(row->colorButton, new_row, 2); _player_layout->setRowStretch(new_row, 0); _player_layout->setRowStretch(new_row + 1, 1); row->label->show(); row->nameEdit->show(); row->colorButton->show(); _players.append(row); updateButtons(); } void newGameDialog::removePlayerRow(playerRow *row) { delete row->label; delete row->nameEdit; delete row->colorButton; delete row; _players.remove(row); updateButtons(); } void newGameDialog::removePlayerRow() { playerRow *row = _players.last(); removePlayerRow(row); } void newGameDialog::slotNewPluginSelection() { KuePluginInfo info = selectedPlugin(); // Cache the result so we don't have to call the (relatively) expensive // selectedPlugin() every time we need these values _min_players = info.minTeams; _max_players = info.maxTeams; assert(_min_players <= _max_players); while (_players.count() < _min_players) { addPlayerRow(); } while (_players.count() > _max_players) { removePlayerRow(); } updateButtons(); } void newGameDialog::updateButtons() { _remove_player_button->setEnabled(_players.count() > _min_players); _add_player_button->setEnabled(_players.count() < _max_players); } TQColor newGameDialog::defaultPlayerColor(unsigned int player) { // We only have 16 colors :( player = player % 16; switch (player) { case 0: return TQt::white; case 1: return TQt::black; case 2: return TQt::red; case 3: return TQt::green; case 4: return TQt::blue; case 5: return TQt::cyan; case 6: return TQt::magenta; case 7: return TQt::yellow; case 8: return TQt::gray; case 9: return TQt::darkRed; case 10: return TQt::darkGreen; case 11: return TQt::darkBlue; case 12: return TQt::darkCyan; case 13: return TQt::darkMagenta; case 14: return TQt::darkYellow; default: return TQt::darkGray; } } #include "newgame.moc"