From: APTX Date: Mon, 11 Oct 2010 14:57:51 +0000 (+0200) Subject: Add model for viewing commands sent by the client. That is commands, which were sent... X-Git-Url: https://gitweb.tyo.aptx.org/?a=commitdiff_plain;h=1f221d034f03cd48bfaf8c7e049219493281d478;p=anidbudpclient.git Add model for viewing commands sent by the client. That is commands, which were sent and are waiting for a reply. --- diff --git a/anidbudpclient.pro b/anidbudpclient.pro index eabee97..8023a97 100644 --- a/anidbudpclient.pro +++ b/anidbudpclient.pro @@ -1,61 +1,63 @@ -# ------------------------------------------------- -# Project created by QtCreator 2009-03-22T14:53:52 -# ------------------------------------------------- -QT += network \ - script -QT -= gui -TEMPLATE = lib -TARGET = anidbudpclient -static { - message(anidbpudpclinet: Static build) - DESTDIR = build-static -} -!static { - message(anidbpudpclinet: Dynamic build) - DESTDIR = build -} -INCLUDEPATH += $$PWD -DEPENDPATH += $$PWD -DEFINES += ANIDBUDPCLIENT_LIBRARY -SOURCES += client.cpp \ - abstractcommand.cpp \ - authcommand.cpp \ - rawcommand.cpp \ - mylistaddcommand.cpp \ - logoutcommand.cpp \ - uptimecommand.cpp \ - mylistcommand.cpp \ - filecommand.cpp \ - votecommand.cpp \ - file.cpp \ - hash.cpp \ - hashproducer.cpp \ - hashconsumer.cpp - -HEADERS += client.h \ - anidbudpclient_global.h \ - abstractcommand.h \ - authcommand.h \ - rawcommand.h \ - mylistaddcommand.h \ - logoutcommand.h \ - uptimecommand.h \ - mylistcommand.h \ - filecommand.h \ - votecommand.h \ - file.h \ - hash.h \ - hashproducer.h \ - hashconsumer.h \ - circularbuffer.h - -CONV_HEADERS += include/AniDBUdpClient/Client \ - include/AniDBUdpClient/AbstractCommand \ - include/AniDBUdpClient/RawCommand \ - include/AniDBUdpClient/MyListCommand \ - include/AniDBUdpClient/MyListAddCommand \ - include/AniDBUdpClient/FileCommand \ - include/AniDBUdpClient/VoteCommand \ - include/AniDBUdpClient/UptimeCommand \ - include/AniDBUdpClient/File \ - include/AniDBUdpClient/Hash +# ------------------------------------------------- +# Project created by QtCreator 2009-03-22T14:53:52 +# ------------------------------------------------- +QT += network \ + script +QT -= gui +TEMPLATE = lib +TARGET = anidbudpclient +static { + message(anidbpudpclinet: Static build) + DESTDIR = build-static +} +!static { + message(anidbpudpclinet: Dynamic build) + DESTDIR = build +} +INCLUDEPATH += $$PWD +DEPENDPATH += $$PWD +DEFINES += ANIDBUDPCLIENT_LIBRARY +SOURCES += client.cpp \ + abstractcommand.cpp \ + authcommand.cpp \ + rawcommand.cpp \ + mylistaddcommand.cpp \ + logoutcommand.cpp \ + uptimecommand.cpp \ + mylistcommand.cpp \ + filecommand.cpp \ + votecommand.cpp \ + file.cpp \ + hash.cpp \ + hashproducer.cpp \ + hashconsumer.cpp \ + clientcommandmodel.cpp + +HEADERS += client.h \ + anidbudpclient_global.h \ + abstractcommand.h \ + authcommand.h \ + rawcommand.h \ + mylistaddcommand.h \ + logoutcommand.h \ + uptimecommand.h \ + mylistcommand.h \ + filecommand.h \ + votecommand.h \ + file.h \ + hash.h \ + hashproducer.h \ + hashconsumer.h \ + circularbuffer.h \ + clientcommandmodel.h + +CONV_HEADERS += include/AniDBUdpClient/Client \ + include/AniDBUdpClient/AbstractCommand \ + include/AniDBUdpClient/RawCommand \ + include/AniDBUdpClient/MyListCommand \ + include/AniDBUdpClient/MyListAddCommand \ + include/AniDBUdpClient/FileCommand \ + include/AniDBUdpClient/VoteCommand \ + include/AniDBUdpClient/UptimeCommand \ + include/AniDBUdpClient/File \ + include/AniDBUdpClient/Hash diff --git a/clientcommandmodel.cpp b/clientcommandmodel.cpp new file mode 100644 index 0000000..8410a97 --- /dev/null +++ b/clientcommandmodel.cpp @@ -0,0 +1,89 @@ +#include "clientcommandmodel.h" + +#include "client.h" + +namespace AniDBUdpClient { + +ClientCommandModel::ClientCommandModel(Client *client, QObject *parent) : + QAbstractTableModel(parent), m_client(client) +{ +} + +Client *ClientCommandModel::client() const +{ + return m_client; +} + +void ClientCommandModel::setClient(Client *client) +{ + beginResetModel(); + m_client = client; + endResetModel(); +} + +int ClientCommandModel::rowCount(const QModelIndex &/*parent*/) const +{ + if (!m_client) + return 0; + return m_client->sentCommands.count(); +} + +int ClientCommandModel::columnCount(const QModelIndex &/*parent*/) const +{ + return 3; +} + +QVariant ClientCommandModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + if (role != Qt::DisplayRole) + return QVariant(); + + if (orientation == Qt::Vertical) + return section + 1; + + switch (section) + { + case 0: + return tr("Time Sent"); + case 1: + return tr("Command"); + case 2: + return tr("Arguments"); + } + + return QVariant(); +} + +QVariant ClientCommandModel::data(const QModelIndex &index, int role) const +{ + if (role != Qt::DisplayRole) + return QVariant(); + + AbstractReply *currentReply = m_client->sentCommands[m_client->sentCommandOrder[index.row()]]; + + switch (index.column()) + { + case 0: + return currentReply->timeSent(); + case 1: + return currentReply->command().rawCommand().first; + case 2: + return currentReply->command().rawCommand().second; + } + return QVariant(); +} + + +void ClientCommandModel::commandAdded(int index) +{ + beginInsertRows(QModelIndex(), index, index + 1); + endInsertRows(); +} + +void ClientCommandModel::commandRemoved(int index) +{ + beginRemoveRows(QModelIndex(), index, index + 1); + endRemoveRows(); +} + +} // namespace AniDBUdpClient diff --git a/clientcommandmodel.h b/clientcommandmodel.h new file mode 100644 index 0000000..306743e --- /dev/null +++ b/clientcommandmodel.h @@ -0,0 +1,40 @@ +#ifndef CLIENTCOMMANDMODEL_H +#define CLIENTCOMMANDMODEL_H + +#include "anidbudpclient_global.h" + +#include + +namespace AniDBUdpClient { + +class Client; + +class ANIDBUDPCLIENTSHARED_EXPORT ClientCommandModel : public QAbstractTableModel +{ + Q_OBJECT +public: + explicit ClientCommandModel(Client *client = 0, QObject *parent = 0); + + Client *client() const; + void setClient(Client *client); + + int rowCount(const QModelIndex &parent = QModelIndex()) const; + int columnCount(const QModelIndex &/*parent*/) const; + + QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const; + QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; + +signals: + +public slots: + +private: + void commandAdded(int index); + void commandRemoved(int index); + + Client *m_client; +}; + +} // namespace AniDBUdpClient + +#endif // CLIENTCOMMANDMODEL_H