mirror of
https://github.com/QuasarApp/SoundBand.git
synced 2025-04-26 15:24:31 +00:00
added server model
This commit is contained in:
parent
c31e1042c0
commit
032136bcb7
@ -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();
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user