Updated game view

+ removed password column
+ added a lock svg to restrictions if pw is needed
+ added "password" to restrictions if pw is needed
+ added user pawn to creator tab
+ reformatted code for easy adaptation later
This commit is contained in:
Matt Lowe 2015-01-22 20:46:24 +01:00
parent 0daa7a8809
commit 1406a27775
2 changed files with 133 additions and 63 deletions

View file

@ -1,11 +1,15 @@
#include "gamesmodel.h" #include "gamesmodel.h"
#include "pb/serverinfo_game.pb.h" #include "pb/serverinfo_game.pb.h"
#include "pixmapgenerator.h"
#include <QDebug> #include <QDebug>
#include <QIcon>
#include <QStringList> #include <QStringList>
#include <QDateTime> #include <QDateTime>
#include <QSettings> #include <QSettings>
#include <QCryptographicHash> #include <QCryptographicHash>
static const enum {ROOM, CREATED, DESCRIPTION, CREATOR, GAME_TYPE, RESTRICTIONS, PLAYERS, SPECTATORS};
namespace { namespace {
const unsigned SECS_PER_MIN = 60; const unsigned SECS_PER_MIN = 60;
const unsigned SECS_PER_HOUR = 60 * 60; const unsigned SECS_PER_HOUR = 60 * 60;
@ -75,15 +79,16 @@ QVariant GamesModel::data(const QModelIndex &index, int role) const
return QVariant(); return QVariant();
if (role == Qt::UserRole) if (role == Qt::UserRole)
return index.row(); return index.row();
if (role != Qt::DisplayRole && role != SORT_ROLE) if (role != Qt::DisplayRole && role != SORT_ROLE && role != Qt::DecorationRole && role != Qt::TextAlignmentRole)
return QVariant(); return QVariant();
if ((index.row() >= gameList.size()) || (index.column() >= columnCount())) if ((index.row() >= gameList.size()) || (index.column() >= columnCount()))
return QVariant(); return QVariant();
const ServerInfo_Game &g = gameList[index.row()]; const ServerInfo_Game &g = gameList[index.row()];
switch (index.column()) { switch (index.column()) {
case 0: return rooms.value(g.room_id()); case ROOM:
case 1: { return rooms.value(g.room_id());
case CREATED: {
QDateTime then; QDateTime then;
then.setTime_t(g.start_time()); then.setTime_t(g.start_time());
unsigned int secs = then.secsTo(QDateTime::currentDateTime()); unsigned int secs = then.secsTo(QDateTime::currentDateTime());
@ -91,32 +96,98 @@ QVariant GamesModel::data(const QModelIndex &index, int role) const
switch (role) { switch (role) {
case Qt::DisplayRole: return prettyPrintSecsAgo(secs); case Qt::DisplayRole: return prettyPrintSecsAgo(secs);
case SORT_ROLE: return QVariant(secs); case SORT_ROLE: return QVariant(secs);
default: { default: return QVariant();
qDebug() << "Returning data for col 1 of games model when role != display, role != sort";
return QVariant(); // Shouldn't ever be reached.
} }
} }
case DESCRIPTION:
switch(role) {
case SORT_ROLE:
case Qt::DisplayRole:
return QString::fromStdString(g.description());
case Qt::TextAlignmentRole:
Qt::AlignLeft;
default:
return QVariant();
} }
case 2: return QString::fromStdString(g.description()); case CREATOR: {
case 3: return QString::fromStdString(g.creator_info().name()); switch(role) {
case 4: { case SORT_ROLE:
case Qt::DisplayRole:
return QString::fromStdString(g.creator_info().name());
case Qt::DecorationRole:
switch(g.creator_info().user_level()) {
case 1: {
QPixmap avatarPixmap = UserLevelPixmapGenerator::generatePixmap(13, (UserLevelFlags)g.creator_info().user_level());
return QIcon(avatarPixmap);
}
case 3:{
QPixmap avatarPixmap = UserLevelPixmapGenerator::generatePixmap(13, (UserLevelFlags)g.creator_info().user_level());
return QIcon(avatarPixmap);
}
default:
break;
}
default:
return QVariant();
}
}
case GAME_TYPE:
switch (role) {
case SORT_ROLE:
case Qt::DisplayRole: {
QStringList result; QStringList result;
GameTypeMap gameTypeMap = gameTypes.value(g.room_id()); GameTypeMap gameTypeMap = gameTypes.value(g.room_id());
for (int i = g.game_types_size() - 1; i >= 0; --i) for (int i = g.game_types_size() - 1; i >= 0; --i)
result.append(gameTypeMap.value(g.game_types(i))); result.append(gameTypeMap.value(g.game_types(i)));
return result.join(", "); return result.join(", ");
} }
case 5: return g.with_password() ? ((g.spectators_need_password() || !g.spectators_allowed()) ? tr("yes") : tr("yes, free for spectators")) : tr("no"); case Qt::TextAlignmentRole:
case 6: { return Qt::AlignLeft;
default:
return QVariant();
}
case RESTRICTIONS:
switch(role) {
case SORT_ROLE:
case Qt::DisplayRole: {
QStringList result; QStringList result;
if (g.only_buddies()) if (g.only_buddies())
result.append(tr("buddies only")); result.append(tr("buddies only"));
if (g.only_registered()) if (g.only_registered())
result.append(tr("reg. users only")); result.append(tr("reg. users only"));
if (g.with_password())
result.append(tr("password"));
return result.join(", "); return result.join(", ");
} }
case 7: return QString("%1/%2").arg(g.player_count()).arg(g.max_players()); case Qt::DecorationRole:{
case 8: return g.spectators_allowed() ? QVariant(g.spectators_count()) : QVariant(tr("not allowed")); return g.with_password() ? QIcon(":/resources/lock.svg") : QVariant();
case Qt::TextAlignmentRole:
return Qt::AlignLeft;
default:
return QVariant();
}
}
case PLAYERS:
switch(role) {
case SORT_ROLE:
case Qt::DisplayRole:
return QString("%1/%2").arg(g.player_count()).arg(g.max_players());
case Qt::TextAlignmentRole:
return Qt::AlignLeft;
default:
return QVariant();
}
case SPECTATORS:
switch(role) {
case SORT_ROLE:
case Qt::DisplayRole:
return g.spectators_allowed() ? QVariant(g.spectators_count()) : QVariant(tr("not allowed"));
case Qt::TextAlignmentRole:
return Qt::AlignLeft;
default:
return QVariant();
}
default: return QVariant(); default: return QVariant();
} }
} }
@ -131,10 +202,9 @@ QVariant GamesModel::headerData(int section, Qt::Orientation orientation, int ro
case 2: return tr("Description"); case 2: return tr("Description");
case 3: return tr("Creator"); case 3: return tr("Creator");
case 4: return tr("Game Type"); case 4: return tr("Game Type");
case 5: return tr("Password"); case 5: return tr("Restrictions");
case 6: return tr("Restrictions"); case 6: return tr("Players");
case 7: return tr("Players"); case 7: return tr("Spectators");
case 8: return tr("Spectators");
default: return QVariant(); default: return QVariant();
} }
} }

View file

@ -17,7 +17,7 @@ private:
QMap<int, QString> rooms; QMap<int, QString> rooms;
QMap<int, GameTypeMap> gameTypes; QMap<int, GameTypeMap> gameTypes;
static const int NUM_COLS = 9; static const int NUM_COLS = 8;
public: public:
static const int SORT_ROLE = Qt::UserRole+1; static const int SORT_ROLE = Qt::UserRole+1;