added server model

This commit is contained in:
Andrei Yankovich 2018-03-04 21:47:57 +03:00
parent c31e1042c0
commit 032136bcb7
4 changed files with 138 additions and 16 deletions

View File

@ -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<int, QByteArray> ServerListModel::roleNames()const{
QHash<int, QByteArray> 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();
}

View File

@ -1,17 +1,75 @@
#ifndef SERVERLISTMODEL_H
#define SERVERLISTMODEL_H
#include <QObject>
#include <QAbstractListModel>
class ServerListModel : public QObject
class SyncEngine;
class ETcpSocket;
/**
* @brief The ServerListModel class
*/
class ServerListModel : public QAbstractListModel
{
Q_OBJECT
private:
const SyncEngine * syncEngine;
const QList<ETcpSocket*> *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<int, QByteArray> 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
#endif // SERVERLISTMODEL_H

View File

@ -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<ETcpSocket*>& list = sync->getServersList();
QStringList tempList;
for(ETcpSocket* socket : list){
tempList.push_back(socket->peerName());
}
return tempList;
const QList<ETcpSocket*>* 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;
}

View File

@ -146,7 +146,7 @@ public slots:
* @brief getServerList
* @return list of servers
*/
QStringList getServerList();
const QList<ETcpSocket *> *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