Replays split up, one for each subgame

This commit is contained in:
Max-Wilhelm Bruker 2012-03-02 20:45:04 +01:00
parent 4dc712286f
commit 8481e61e8f
15 changed files with 278 additions and 119 deletions

View file

@ -9,10 +9,24 @@
#include "pb/response_replay_list.pb.h"
#include "pb/serverinfo_replay.pb.h"
RemoteReplayList_TreeModel::MatchNode::MatchNode(const ServerInfo_ReplayMatch &_matchInfo)
: RemoteReplayList_TreeModel::Node(QString::fromStdString(_matchInfo.game_name())), matchInfo(_matchInfo)
{
for (int i = 0; i < matchInfo.replay_list_size(); ++i)
append(new ReplayNode(matchInfo.replay_list(i)));
}
RemoteReplayList_TreeModel::MatchNode::~MatchNode()
{
for (int i = 0; i < size(); ++i)
delete at(i);
}
RemoteReplayList_TreeModel::RemoteReplayList_TreeModel(AbstractClient *_client, QObject *parent)
: QAbstractItemModel(parent), client(_client)
{
QFileIconProvider fip;
dirIcon = fip.icon(QFileIconProvider::Folder);
fileIcon = fip.icon(QFileIconProvider::File);
refreshTree();
@ -20,47 +34,74 @@ RemoteReplayList_TreeModel::RemoteReplayList_TreeModel(AbstractClient *_client,
RemoteReplayList_TreeModel::~RemoteReplayList_TreeModel()
{
clearTree();
}
int RemoteReplayList_TreeModel::rowCount(const QModelIndex &parent) const
{
return parent.isValid() ? 0 : replays.size();
if (!parent.isValid())
return replayMatches.size();
MatchNode *matchNode = dynamic_cast<MatchNode *>(static_cast<Node *>(parent.internalPointer()));
if (matchNode)
return matchNode->size();
else
return 0;
}
int RemoteReplayList_TreeModel::columnCount(const QModelIndex &/*parent*/) const
{
return 6;
return 5;
}
QVariant RemoteReplayList_TreeModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (index.column() > 5)
if (index.column() > 4)
return QVariant();
ServerInfo_Replay *replayInfo = static_cast<ServerInfo_Replay *>(index.internalPointer());
switch (role) {
case Qt::TextAlignmentRole:
return index.column() == 0 ? Qt::AlignRight : Qt::AlignLeft;
case Qt::DisplayRole: {
switch (index.column()) {
case 0: return replayInfo->game_id();
case 1: return QString::fromStdString(replayInfo->game_name());
case 2: return QString::fromStdString(replayInfo->replay_name());
case 3: {
QStringList playerList;
for (int i = 0; i < replayInfo->player_names_size(); ++i)
playerList.append(QString::fromStdString(replayInfo->player_names(i)));
return playerList.join(", ");
ReplayNode *replayNode = dynamic_cast<ReplayNode *>(static_cast<Node *>(index.internalPointer()));
if (replayNode) {
const ServerInfo_Replay &replayInfo = replayNode->getReplayInfo();
switch (role) {
case Qt::TextAlignmentRole:
return index.column() == 0 ? Qt::AlignRight : Qt::AlignLeft;
case Qt::DisplayRole: {
switch (index.column()) {
case 0: return replayInfo.replay_id();
case 1: return QString::fromStdString(replayInfo.replay_name());
case 4: return replayInfo.duration();
default: return QVariant();
}
case 4: return QDateTime::fromTime_t(replayInfo->time_started());
case 5: return replayInfo->length();
default: return QVariant();
}
case Qt::DecorationRole:
return index.column() == 0 ? fileIcon : QVariant();
}
} else {
MatchNode *matchNode = dynamic_cast<MatchNode *>(static_cast<Node *>(index.internalPointer()));
const ServerInfo_ReplayMatch &matchInfo = matchNode->getMatchInfo();
switch (role) {
case Qt::TextAlignmentRole:
return index.column() == 0 ? Qt::AlignRight : Qt::AlignLeft;
case Qt::DisplayRole: {
switch (index.column()) {
case 0: return matchInfo.game_id();
case 1: return QString::fromStdString(matchInfo.game_name());
case 2: {
QStringList playerList;
for (int i = 0; i < matchInfo.player_names_size(); ++i)
playerList.append(QString::fromStdString(matchInfo.player_names(i)));
return playerList.join(", ");
}
case 3: return QDateTime::fromTime_t(matchInfo.time_started());
case 4: return matchInfo.length();
default: return QVariant();
}
}
case Qt::DecorationRole:
return index.column() == 0 ? dirIcon : QVariant();
}
case Qt::DecorationRole:
return index.column() == 0 ? fileIcon : QVariant();
}
return QVariant();
}
@ -74,12 +115,11 @@ QVariant RemoteReplayList_TreeModel::headerData(int section, Qt::Orientation ori
return section == 0 ? Qt::AlignRight : Qt::AlignLeft;
case Qt::DisplayRole: {
switch (section) {
case 0: return tr("Game ID");
case 1: return tr("Game name");
case 2: return tr("Replay name");
case 3: return tr("Players");
case 4: return tr("Time started");
case 5: return tr("Duration (sec)");
case 0: return tr("ID");
case 1: return tr("Name");
case 2: return tr("Players");
case 3: return tr("Time started");
case 4: return tr("Duration (sec)");
default: return QVariant();
}
}
@ -91,13 +131,28 @@ QModelIndex RemoteReplayList_TreeModel::index(int row, int column, const QModelI
{
if (!hasIndex(row, column, parent))
return QModelIndex();
return createIndex(row, column, (void *) &(replays[row]));
MatchNode *matchNode = dynamic_cast<MatchNode *>(static_cast<Node *>(parent.internalPointer()));
if (matchNode) {
if (row >= matchNode->size())
return QModelIndex();
return createIndex(row, column, (void *) matchNode->at(row));
} else {
if (row >= replayMatches.size())
return QModelIndex();
return createIndex(row, column, (void *) replayMatches[row]);
}
}
QModelIndex RemoteReplayList_TreeModel::parent(const QModelIndex &ind) const
{
return QModelIndex();
MatchNode const *matchNode = dynamic_cast<MatchNode *>(static_cast<Node *>(ind.internalPointer()));
if (matchNode)
return QModelIndex();
else {
ReplayNode *replayNode = dynamic_cast<ReplayNode *>(static_cast<Node *>(ind.internalPointer()));
return createIndex(replayNode->getParent()->indexOf(replayNode), 0, replayNode);
}
}
Qt::ItemFlags RemoteReplayList_TreeModel::flags(const QModelIndex &index) const
@ -108,12 +163,33 @@ Qt::ItemFlags RemoteReplayList_TreeModel::flags(const QModelIndex &index) const
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
ServerInfo_Replay const* RemoteReplayList_TreeModel::getNode(const QModelIndex &index) const
ServerInfo_Replay const* RemoteReplayList_TreeModel::getReplay(const QModelIndex &index) const
{
if (!index.isValid())
return 0;
return &(replays[index.row()]);
ReplayNode *node = dynamic_cast<ReplayNode *>(static_cast<Node *>(index.internalPointer()));
if (!node)
return 0;
return &node->getReplayInfo();
}
ServerInfo_ReplayMatch const* RemoteReplayList_TreeModel::getReplayMatch(const QModelIndex &index) const
{
if (!index.isValid())
return 0;
MatchNode *node = dynamic_cast<MatchNode *>(static_cast<Node *>(index.internalPointer()));
if (!node)
return 0;
return &node->getMatchInfo();
}
void RemoteReplayList_TreeModel::clearTree()
{
for (int i = 0; i < replayMatches.size(); ++i)
delete replayMatches[i];
replayMatches.clear();
}
void RemoteReplayList_TreeModel::refreshTree()
@ -129,10 +205,10 @@ void RemoteReplayList_TreeModel::replayListFinished(const Response &r)
const Response_ReplayList &resp = r.GetExtension(Response_ReplayList::ext);
beginResetModel();
replays.clear();
clearTree();
for (int i = 0; i < resp.replay_list_size(); ++i)
replays.append(resp.replay_list(i));
for (int i = 0; i < resp.match_list_size(); ++i)
replayMatches.append(new MatchNode(resp.match_list(i)));
endResetModel();
emit treeRefreshed();
@ -156,12 +232,7 @@ RemoteReplayList_TreeWidget::RemoteReplayList_TreeWidget(AbstractClient *_client
header()->setSortIndicator(0, Qt::AscendingOrder);
}
ServerInfo_Replay const *RemoteReplayList_TreeWidget::getNode(const QModelIndex &ind) const
ServerInfo_Replay const *RemoteReplayList_TreeWidget::getCurrentReplay() const
{
return treeModel->getNode(proxyModel->mapToSource(ind));
}
ServerInfo_Replay const *RemoteReplayList_TreeWidget::getCurrentItem() const
{
return getNode(selectionModel()->currentIndex());
return treeModel->getReplay(proxyModel->mapToSource(selectionModel()->currentIndex()));
}

View file

@ -5,6 +5,7 @@
#include <QDateTime>
#include <QTreeView>
#include "pb/serverinfo_replay.pb.h"
#include "pb/serverinfo_replay_match.pb.h"
class Response;
class AbstractClient;
@ -13,10 +14,42 @@ class QSortFilterProxyModel;
class RemoteReplayList_TreeModel : public QAbstractItemModel {
Q_OBJECT
private:
AbstractClient *client;
QList<ServerInfo_Replay> replays;
class MatchNode;
class ReplayNode;
class Node {
protected:
QString name;
public:
Node(const QString &_name)
: name(_name) { }
virtual ~Node() { };
QString getName() const { return name; }
};
class MatchNode : public Node, public QList<ReplayNode *> {
private:
ServerInfo_ReplayMatch matchInfo;
public:
MatchNode(const ServerInfo_ReplayMatch &_matchInfo);
~MatchNode();
void clearTree();
const ServerInfo_ReplayMatch &getMatchInfo() { return matchInfo; }
};
class ReplayNode : public Node {
private:
MatchNode *parent;
ServerInfo_Replay replayInfo;
public:
ReplayNode(const ServerInfo_Replay &_replayInfo, MatchNode *_parent = 0)
: Node(QString::fromStdString(_replayInfo.replay_name())), parent(_parent), replayInfo(_replayInfo) { }
MatchNode *getParent() const { return parent; }
const ServerInfo_Replay &getReplayInfo() { return replayInfo; }
};
QIcon fileIcon;
AbstractClient *client;
QList<MatchNode *> replayMatches;
QIcon dirIcon, fileIcon;
void clearTree();
signals:
void treeRefreshed();
private slots:
@ -32,7 +65,8 @@ public:
QModelIndex parent(const QModelIndex &index) const;
Qt::ItemFlags flags(const QModelIndex &index) const;
void refreshTree();
ServerInfo_Replay const *getNode(const QModelIndex &index) const;
ServerInfo_Replay const* getReplay(const QModelIndex &index) const;
ServerInfo_ReplayMatch const* getReplayMatch(const QModelIndex &index) const;
};
class RemoteReplayList_TreeWidget : public QTreeView {
@ -42,7 +76,8 @@ private:
ServerInfo_Replay const *getNode(const QModelIndex &ind) const;
public:
RemoteReplayList_TreeWidget(AbstractClient *_client, QWidget *parent = 0);
ServerInfo_Replay const *getCurrentItem() const;
ServerInfo_Replay const *getCurrentReplay() const;
ServerInfo_ReplayMatch const *getCurrentReplayMatch() const;
void refreshTree();
};

View file

@ -198,7 +198,7 @@ void DeckViewContainer::setDeck(DeckList *deck)
deckView->setDeck(deck);
readyStartButton->setEnabled(true);
}
#include <QDebug>
TabGame::TabGame(GameReplay *_replay)
: Tab(0),
hostId(-1),

View file

@ -123,12 +123,12 @@ void TabReplays::actOpenLocalReplay()
void TabReplays::actOpenRemoteReplay()
{
ServerInfo_Replay const *curRight = serverDirView->getCurrentItem();
ServerInfo_Replay const *curRight = serverDirView->getCurrentReplay();
if (!curRight)
return;
Command_ReplayDownload cmd;
cmd.set_game_id(curRight->game_id());
cmd.set_replay_id(curRight->replay_id());
PendingCommand *pend = client->prepareSessionCommand(cmd);
connect(pend, SIGNAL(finished(const Response &)), this, SLOT(openRemoteReplayFinished(const Response &)));
@ -156,13 +156,13 @@ void TabReplays::actDownload()
filePath = localDirModel->filePath(curLeft);
}
ServerInfo_Replay const *curRight = serverDirView->getCurrentItem();
ServerInfo_Replay const *curRight = serverDirView->getCurrentReplay();
if (!curRight)
return;
filePath += QString("/game_%1.cor").arg(curRight->game_id());
filePath += QString("/replay_%1.cor").arg(curRight->replay_id());
Command_ReplayDownload cmd;
cmd.set_game_id(curRight->game_id());
cmd.set_replay_id(curRight->replay_id());
PendingCommand *pend = client->prepareSessionCommand(cmd);
pend->setExtraData(filePath);