diff --git a/SoundBand/serverlistmodel.cpp b/SoundBand/serverlistmodel.cpp index 4dea77e..2adfddc 100644 --- a/SoundBand/serverlistmodel.cpp +++ b/SoundBand/serverlistmodel.cpp @@ -1,6 +1,69 @@ #include "serverlistmodel.h" +#include "syncengine.h" -ServerListModel::ServerListModel(QObject *parent) : QObject(parent) +ServerListModel::ServerListModel(QObject *parent) : + QAbstractListModel(parent), + syncEngine(nullptr), + servers(nullptr) { - + itemCount = 0; +} + +void ServerListModel::setSource(const SyncEngine *engine){ + if(syncEngine) + disconnect(syncEngine, SIGNAL(serversCountChanged()) ,this, SLOT(onPlayListsChanged())); + syncEngine = engine; + connect(syncEngine, SIGNAL(serversCountChanged()),this ,SLOT(onPlayListsChanged())); +} + +QHash ServerListModel::roleNames()const{ + QHash roles; + roles[nameRole] = "name"; + return roles; +} + +void ServerListModel::onPlayListsChanged(){ + beginResetModel(); + servers = syncEngine->getServerList(); + endResetModel(); +} + +bool ServerListModel::canFetchMore(const QModelIndex & /* index */) const +{ + if (itemCount < servers->size()) + return true; + else + return false; +} + +void ServerListModel::fetchMore(const QModelIndex & /* index */) +{ + int remainder = servers->size() - itemCount; + int itemsToFetch = qMin(100, remainder); + + beginInsertRows(QModelIndex(), itemCount, itemCount + itemsToFetch - 1); + + itemCount += itemsToFetch; + + endInsertRows(); +} + +int ServerListModel::rowCount(const QModelIndex & /* parent */) const +{ + return itemCount; +} + +QVariant ServerListModel::data(const QModelIndex &index, int role) const +{ + if (!index.isValid()) + return QVariant(); + + if (index.row() >= servers->size() || index.row() < 0) + return QVariant(); + + if (role == nameRole) { + return servers->at(index.row())->peerName(); + + } + return QVariant(); } diff --git a/SoundBand/serverlistmodel.h b/SoundBand/serverlistmodel.h index 8ae10ef..87a2b87 100644 --- a/SoundBand/serverlistmodel.h +++ b/SoundBand/serverlistmodel.h @@ -1,17 +1,75 @@ #ifndef SERVERLISTMODEL_H #define SERVERLISTMODEL_H -#include +#include -class ServerListModel : public QObject +class SyncEngine; +class ETcpSocket; + +/** + * @brief The ServerListModel class + */ +class ServerListModel : public QAbstractListModel { Q_OBJECT + +private: + const SyncEngine * syncEngine; + const QList *servers; + int itemCount; + +private slots: + /** + * @brief onPlayListsChanged check new playLists + */ + void onPlayListsChanged(); +protected: + bool canFetchMore(const QModelIndex &parent) const override; + void fetchMore(const QModelIndex &parent) override; + public: explicit ServerListModel(QObject *parent = nullptr); + /** + * @brief The ServerListRoles enum + * nameRole - name of playlist + * imageRole - image of playlist + */ + enum ServerListRoles { + nameRole = Qt::UserRole + 1 + }; + + /** + * @brief setSource + * @param engine + */ + void setSource(const SyncEngine* engine); + + /** + * @brief AnimalModel::roleNames + * @return pair of roles and value + */ + QHash roleNames() const; + + /** + * @brief rowCount + * @param parent + * @return count of playlists + */ + int rowCount(const QModelIndex &parent = QModelIndex()) const override; + + /** + * @brief data standart dataselector + * @param index + * @param role + * @return + */ + QVariant data(const QModelIndex &index, int role = nameRole) const override; + + signals: public slots: }; -#endif // SERVERLISTMODEL_H \ No newline at end of file +#endif // SERVERLISTMODEL_H diff --git a/SoundBand/syncengine.cpp b/SoundBand/syncengine.cpp index 052c68d..577b183 100644 --- a/SoundBand/syncengine.cpp +++ b/SoundBand/syncengine.cpp @@ -6,6 +6,8 @@ SyncEngine::SyncEngine() { sync = new syncLib::Sync(); sqlApi = sync->getSqlApi(); + + connect(sync, SIGNAL(networkStateChange()), this, SIGNAL(serversCountChanged())); } int SyncEngine::curentSongIndex()const{ @@ -129,16 +131,8 @@ bool SyncEngine::listen(int index){ } } - QStringList SyncEngine::getServerList(){ - const QList& list = sync->getServersList(); - - QStringList tempList; - - for(ETcpSocket* socket : list){ - tempList.push_back(socket->peerName()); - } - - return tempList; + const QList* SyncEngine::getServerList() const{ + return &sync->getServersList(); } @@ -168,6 +162,7 @@ double SyncEngine::pos()const{ } SyncEngine::~SyncEngine(){ + disconnect(sync, SIGNAL(networkStateChange()), this, SIGNAL(serversCountChanged())); delete sync; } diff --git a/SoundBand/syncengine.h b/SoundBand/syncengine.h index ac999b2..3885fc0 100644 --- a/SoundBand/syncengine.h +++ b/SoundBand/syncengine.h @@ -146,7 +146,7 @@ public slots: * @brief getServerList * @return list of servers */ - QStringList getServerList(); + const QList *getServerList() const; signals: @@ -174,6 +174,12 @@ signals: */ void repeatChanged(); + /** + * @brief serversCountChanged + * This signal can be emitted when finded new servers or removed old servers + */ + void serversCountChanged(); + }; #endif // SYNCENGINE_H