Improve the handling of favorite players

This commit is contained in:
Olivier Teulière 2012-06-27 21:33:48 +02:00
parent ee31179864
commit aa557f8121
3 changed files with 37 additions and 9 deletions

View file

@ -129,7 +129,8 @@ void FavPlayersDialog::importPlayers()
{
if (row.size() < 4)
throw BaseException(_("Invalid file (not enough values)"));
PlayerDef def(qfl(row[0]), qfl(row[1]), qfl(row[2]), row[3] != "0");
// The player is not a default one if the corresponding flag is incorrect
PlayerDef def(qfl(row[0]), qfl(row[1]), qfl(row[2]), row[3] == "1");
players.push_back(def);
}
m_helper->addPlayers(players);

View file

@ -212,15 +212,9 @@ int PlayersTableHelper::getRowCount() const
void PlayersTableHelper::addPlayers(const QList<PlayerDef> &iList)
{
// Only add players which are not already there
QSet<PlayerDef> tmpSet = getPlayers(false).toSet();
Q_FOREACH(const PlayerDef &player, iList)
{
if (!tmpSet.contains(player))
{
addRow(player);
tmpSet.insert(player);
}
addPlayer(player, false, false);
}
}
@ -230,7 +224,7 @@ void PlayersTableHelper::addPlayer(const PlayerDef &iPlayer,
bool renameIfDuplicate)
{
QSet<PlayerDef> tmpSet = getPlayers(false).toSet();
PlayerDef def = iPlayer;
PlayerDef def = normalize(iPlayer);
// Generate a unique name if needed
if (renameIfDuplicate)
{
@ -267,9 +261,11 @@ void PlayersTableHelper::addRow(const PlayerDef &iDef)
const int row = m_tablePlayers->rowCount();
m_tablePlayers->setRowCount(row + 1);
m_tablePlayers->setRowHeight(row, 24);
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);
@ -277,6 +273,32 @@ void PlayersTableHelper::addRow(const PlayerDef &iDef)
}
PlayerDef PlayersTableHelper::normalize(const PlayerDef &iDef) const
{
PlayerDef defCopy = iDef;
defCopy.type = iDef.type == _q(kAI) ? _q(kAI) : _q(kHUMAN);
QString level;
if (defCopy.type == _q(kAI))
{
bool ok = false;
int value = iDef.level.toInt(&ok);
if (!ok)
value = 100;
if (value < 0)
value = 0;
if (value > 100)
value = 100;
level = QString("%1").arg(value);
}
else
level = "";
defCopy.level = level;
return defCopy;
}
QList<PlayerDef> PlayersTableHelper::getFavPlayers()
{
QList<PlayerDef> playersList;
@ -299,9 +321,11 @@ QList<PlayerDef> PlayersTableHelper::getFavPlayers()
void PlayersTableHelper::saveFavPlayers(const QList<PlayerDef> &iFavPlayers)
{
QSettings qs;
qs.remove("FavPlayers");
qs.beginWriteArray("FavPlayers");
for (int i = 0; i < iFavPlayers.size(); ++i)
{
LOG_DEBUG("Writing fav player");
qs.setArrayIndex(i);
qs.setValue("name", iFavPlayers.at(i).name);
qs.setValue("type", iFavPlayers.at(i).type);

View file

@ -94,6 +94,9 @@ private:
QList<QAction*> m_popupActions;
bool m_showDefaultColumn;
/// Return a "normalized" player def, i.e. with correct values
PlayerDef normalize(const PlayerDef &iDef) const;
};