Favorite players: new "Default" column, for automatic inclusion in a new game

This commit is contained in:
Olivier Teulière 2012-04-27 20:08:42 +02:00
parent 094af979c7
commit eaf536b3ba
4 changed files with 47 additions and 18 deletions

View file

@ -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());

View file

@ -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<PlayerDef> 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<PlayerDef> fav = m_helper->getFavPlayers();
QList<PlayerDef> fav = PlayersTableHelper::getFavPlayers();
const QList<PlayerDef> &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)
{

View file

@ -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<PlayerDef> 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<PlayerDef> 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<PlayerDef> &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();
}

View file

@ -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<PlayerDef> getPlayers(bool onlySelected) const;
void addPlayers(const QList<PlayerDef> &iList);
@ -90,6 +92,7 @@ private:
QPushButton *m_buttonAdd;
QPushButton *m_buttonRemove;
QList<QAction*> m_popupActions;
bool m_showDefaultColumn;
};