From eaf536b3ba1c487fcc6a07925616c67513dedca3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Teuli=C3=A8re?= Date: Fri, 27 Apr 2012 20:08:42 +0200 Subject: [PATCH] Favorite players: new "Default" column, for automatic inclusion in a new game --- qt/main_window.cpp | 11 +++++++---- qt/new_game.cpp | 19 +++++++++++++++---- qt/players_table_helper.cpp | 28 ++++++++++++++++++++-------- qt/players_table_helper.h | 7 +++++-- 4 files changed, 47 insertions(+), 18 deletions(-) diff --git a/qt/main_window.cpp b/qt/main_window.cpp index 9001979..5acd26f 100644 --- a/qt/main_window.cpp +++ b/qt/main_window.cpp @@ -1035,13 +1035,16 @@ void MainWindow::onSettingsFavPlayers() { QDialog *dialog = new QDialog(this); dialog->setWindowTitle(_q("Favorite players")); - dialog->resize(400, 500); + dialog->resize(480, 550); + dialog->setMinimumSize(200, 200); QVBoxLayout *vLayout = new QVBoxLayout; dialog->setLayout(vLayout); - QLabel *label = new QLabel(_q("The favorite players listed below are " + QLabel *label = new QLabel(_q("The favorite players listed below can be " "used in the \"New game\" dialog, to add " - "players quickly.")); + "players quickly. Those marked as \"Default\" " + "will appear there directly (useful if you " + "often play with the same players).")); label->setWordWrap(true); vLayout->addWidget(label); QLabel *label2 = new QLabel(_q("To add or remove a player, use the buttons " @@ -1068,7 +1071,7 @@ void MainWindow::onSettingsFavPlayers() connect(buttonBox, SIGNAL(rejected()), dialog, SLOT(reject())); PlayersTableHelper *helper = - new PlayersTableHelper(dialog, tableFav, buttonAdd, buttonRemove); + new PlayersTableHelper(dialog, tableFav, buttonAdd, buttonRemove, true); helper->addPopupRemoveAction(); helper->addPlayers(helper->getFavPlayers()); diff --git a/qt/new_game.cpp b/qt/new_game.cpp index b11db5f..f2551da 100644 --- a/qt/new_game.cpp +++ b/qt/new_game.cpp @@ -77,8 +77,19 @@ NewGame::NewGame(QWidget *iParent) defLevel = 100; // Initialize the model of the default players - m_helper->addPlayer(PlayerDef(_q("Player %1").arg(1), _q(kHUMAN), "")); - m_helper->addPlayer(PlayerDef(_q("Eliot"), _q(kAI), QString("%1").arg(defLevel))); + QList fav = PlayersTableHelper::getFavPlayers(); + Q_FOREACH(const PlayerDef &def, fav) + { + if (def.isDefault) + m_helper->addPlayer(def); + } + + // Default of the default :) + if (m_helper->getRowCount() == 0) + { + m_helper->addPlayer(PlayerDef(_q("Player %1").arg(1), _q(kHUMAN), "", false)); + m_helper->addPlayer(PlayerDef(_q("Eliot"), _q(kAI), QString("%1").arg(defLevel), false)); + } // Enable the Ok button only if there are enough players for the // current mode @@ -198,7 +209,7 @@ void NewGame::enablePlayers(bool checked) void NewGame::addSelectedToFav() { - QList fav = m_helper->getFavPlayers(); + QList fav = PlayersTableHelper::getFavPlayers(); const QList &selected = m_helper->getPlayers(true); Q_FOREACH(const PlayerDef &def, selected) { @@ -223,7 +234,7 @@ void NewGame::addFavoritePlayers() connect(buttonBox, SIGNAL(rejected()), dialog, SLOT(reject())); PlayersTableHelper *helper = new PlayersTableHelper(dialog, tableFav); - helper->addPlayers(m_helper->getFavPlayers()); + helper->addPlayers(PlayersTableHelper::getFavPlayers()); if (dialog->exec() == QDialog::Accepted) { diff --git a/qt/players_table_helper.cpp b/qt/players_table_helper.cpp index 9da9312..f2c5ed7 100644 --- a/qt/players_table_helper.cpp +++ b/qt/players_table_helper.cpp @@ -49,13 +49,14 @@ uint qHash(const PlayerDef &key) } -PlayerDef::PlayerDef(QString iName, QString iType, QString iLevel) - : name(iName), type(iType), level(iLevel) +PlayerDef::PlayerDef(QString iName, QString iType, QString iLevel, bool iIsDefault) + : name(iName), type(iType), level(iLevel), isDefault(iIsDefault) { } bool PlayerDef::operator==(const PlayerDef &iOther) const { + // Ignore the "isDefault" flag return name == iOther.name && type == iOther.type && level == iOther.level; @@ -65,15 +66,18 @@ bool PlayerDef::operator==(const PlayerDef &iOther) const PlayersTableHelper::PlayersTableHelper(QObject *parent, QTableWidget *tablePlayers, QPushButton *addButton, - QPushButton *removeButton) + QPushButton *removeButton, + bool showDefaultColumn) : QObject(parent), m_tablePlayers(tablePlayers), - m_buttonAdd(addButton), m_buttonRemove(removeButton) + m_buttonAdd(addButton), m_buttonRemove(removeButton), + m_showDefaultColumn(showDefaultColumn) { // Initialize the table headers - tablePlayers->setColumnCount(3); + tablePlayers->setColumnCount(m_showDefaultColumn ? 4 : 3); tablePlayers->setHorizontalHeaderItem(0, new QTableWidgetItem(_("Name"))); tablePlayers->setHorizontalHeaderItem(1, new QTableWidgetItem(_("Type"))); tablePlayers->setHorizontalHeaderItem(2, new QTableWidgetItem(_("Level"))); + tablePlayers->setHorizontalHeaderItem(3, new QTableWidgetItem(_("Default"))); QHeaderView *header = tablePlayers->horizontalHeader(); header->setHighlightSections(false); header->setStretchLastSection(true); @@ -81,6 +85,7 @@ PlayersTableHelper::PlayersTableHelper(QObject *parent, header->resizeSection(0, 200); header->resizeSection(1, 100); header->resizeSection(2, 50); + header->resizeSection(3, 70); tablePlayers->verticalHeader()->setVisible(false); // Set other table properties @@ -175,7 +180,7 @@ void PlayersTableHelper::removeSelectedRows() void PlayersTableHelper::addRow() { - addPlayer(PlayerDef(_q("New player"), _q(kHUMAN), ""), + addPlayer(PlayerDef(_q("New player"), _q(kHUMAN), "", false), true, true); } @@ -188,9 +193,11 @@ QList PlayersTableHelper::getPlayers(bool onlySelected) const { if (onlySelected && !selModel->isRowSelected(i, QModelIndex())) continue; + const QVariant & data = m_tablePlayers->model()->data(m_tablePlayers->model()->index(i, 3)); PlayerDef playerDef(m_tablePlayers->item(i, 0)->text(), m_tablePlayers->item(i, 1)->text(), - m_tablePlayers->item(i, 2)->text()); + m_tablePlayers->item(i, 2)->text(), + data.toBool()); playersList.push_back(playerDef); } return playersList; @@ -263,6 +270,9 @@ void PlayersTableHelper::addRow(const PlayerDef &iDef) m_tablePlayers->setItem(row, 0, new QTableWidgetItem(iDef.name)); m_tablePlayers->setItem(row, 1, new QTableWidgetItem(iDef.type)); m_tablePlayers->setItem(row, 2, new QTableWidgetItem(iDef.level)); + QTableWidgetItem *item = new QTableWidgetItem; + item->setData(Qt::DisplayRole, iDef.isDefault); + m_tablePlayers->setItem(row, 3, item); emit rowCountChanged(); } @@ -277,7 +287,8 @@ QList PlayersTableHelper::getFavPlayers() qs.setArrayIndex(i); PlayerDef playerDef(qs.value("name").toString(), qs.value("type").toString(), - qs.value("level").toString()); + qs.value("level").toString(), + qs.value("default").toBool()); playersList.push_back(playerDef); } qs.endArray(); @@ -295,6 +306,7 @@ void PlayersTableHelper::saveFavPlayers(const QList &iFavPlayers) qs.setValue("name", iFavPlayers.at(i).name); qs.setValue("type", iFavPlayers.at(i).type); qs.setValue("level", iFavPlayers.at(i).level); + qs.setValue("default", iFavPlayers.at(i).isDefault); } qs.endArray(); } diff --git a/qt/players_table_helper.h b/qt/players_table_helper.h index fc67be6..7210176 100644 --- a/qt/players_table_helper.h +++ b/qt/players_table_helper.h @@ -37,10 +37,11 @@ class QPoint; struct PlayerDef { - PlayerDef(QString name, QString type, QString level); + PlayerDef(QString name, QString type, QString level, bool isDefault); QString name; QString type; QString level; + bool isDefault; bool operator==(const PlayerDef &) const; }; @@ -59,7 +60,8 @@ public: PlayersTableHelper(QObject *parent, QTableWidget *tableWidget, QPushButton *addButton = 0, - QPushButton *removeButton = 0); + QPushButton *removeButton = 0, + bool showDefaultColumn = false); QList getPlayers(bool onlySelected) const; void addPlayers(const QList &iList); @@ -90,6 +92,7 @@ private: QPushButton *m_buttonAdd; QPushButton *m_buttonRemove; QList m_popupActions; + bool m_showDefaultColumn; };