forked from mirror/nheko
parent
a54a973ad6
commit
f9c0f4dd54
@ -0,0 +1,66 @@ |
||||
#include "UserProfileModel.h" |
||||
#include "UserProfile.h" |
||||
#include <QModelIndex> |
||||
|
||||
UserProfileModel::UserProfileModel(QObject *parent) |
||||
: QAbstractListModel(parent) |
||||
, deviceList(nullptr) |
||||
{} |
||||
|
||||
int |
||||
UserProfileModel::rowCount(const QModelIndex &parent) const |
||||
{ |
||||
if (parent.isValid() || !this->deviceList) |
||||
return 0; |
||||
return this->deviceList->getDeviceList().size(); |
||||
} |
||||
|
||||
QVariant |
||||
UserProfileModel::data(const QModelIndex &index, int role) const |
||||
{ |
||||
if (!index.isValid() || !this->deviceList) |
||||
return QVariant(); |
||||
|
||||
const DeviceInfo device = this->deviceList->getDeviceList().at(index.row()); |
||||
switch (role) { |
||||
case DEVICEID: |
||||
return QVariant(device.device_id); |
||||
case DISPLAYNAME: |
||||
return QVariant(device.display_name); |
||||
} |
||||
return QVariant(); |
||||
} |
||||
|
||||
QHash<int, QByteArray> |
||||
UserProfileModel::roleNames() const |
||||
{ |
||||
QHash<int, QByteArray> names; |
||||
names[DEVICEID] = "deviceID"; |
||||
names[DISPLAYNAME] = "displayName"; |
||||
return names; |
||||
} |
||||
|
||||
UserProfile * |
||||
UserProfileModel::getList() const |
||||
{ |
||||
return (this->deviceList); |
||||
} |
||||
|
||||
void |
||||
UserProfileModel::setList(UserProfile *devices) |
||||
{ |
||||
beginResetModel(); |
||||
|
||||
if (devices) |
||||
devices->disconnect(this); |
||||
|
||||
if (this->deviceList) { |
||||
const int index = this->deviceList->getDeviceList().size(); |
||||
beginInsertRows(QModelIndex(), index, index); |
||||
endInsertRows(); |
||||
} |
||||
|
||||
this->deviceList = devices; |
||||
|
||||
endResetModel(); |
||||
} |
@ -0,0 +1,29 @@ |
||||
#pragma once |
||||
|
||||
#include <QAbstractListModel> |
||||
|
||||
class UserProfile; // forward declaration of the class UserProfile
|
||||
|
||||
class UserProfileModel : public QAbstractListModel |
||||
{ |
||||
Q_OBJECT |
||||
Q_PROPERTY(UserProfile *deviceList READ getList WRITE setList) |
||||
|
||||
public: |
||||
explicit UserProfileModel(QObject *parent = nullptr); |
||||
|
||||
enum |
||||
{ |
||||
DEVICEID, |
||||
DISPLAYNAME |
||||
}; |
||||
UserProfile *getList() const; |
||||
void setList(UserProfile *devices); |
||||
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override; |
||||
QVariant data(const QModelIndex &index, int role) const override; |
||||
virtual QHash<int, QByteArray> roleNames() const override; |
||||
|
||||
private: |
||||
UserProfile *deviceList; |
||||
}; |
Loading…
Reference in new issue