Make models ready for use with Qt Quick
by adding roleNames()
This commit is contained in:
parent
856a899c4b
commit
c8b93fc472
|
@ -18,6 +18,26 @@ SyncthingDeviceModel::SyncthingDeviceModel(SyncthingConnection &connection, QObj
|
|||
connect(&m_connection, &SyncthingConnection::devStatusChanged, this, &SyncthingDeviceModel::devStatusChanged);
|
||||
}
|
||||
|
||||
QHash<int, QByteArray> SyncthingDeviceModel::initRoleNames()
|
||||
{
|
||||
QHash<int, QByteArray> roles;
|
||||
roles[Qt::DisplayRole] = "name";
|
||||
roles[DeviceStatus] = "status";
|
||||
roles[Qt::DecorationRole] = "statusIcon";
|
||||
roles[DevicePaused] = "paused";
|
||||
roles[DeviceStatusString] = "statusString";
|
||||
roles[DeviceStatusColor] = "statusColor";
|
||||
roles[DeviceId] = "devId";
|
||||
roles[DeviceDetail] = "detail";
|
||||
return roles;
|
||||
}
|
||||
|
||||
QHash<int, QByteArray> SyncthingDeviceModel::roleNames() const
|
||||
{
|
||||
const static QHash<int, QByteArray> roles(initRoleNames());
|
||||
return roles;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Returns the device info for the spcified \a index. The returned object is not persistent.
|
||||
*/
|
||||
|
@ -78,8 +98,8 @@ QVariant SyncthingDeviceModel::data(const QModelIndex &index, int role) const
|
|||
switch (role) {
|
||||
case Qt::DisplayRole:
|
||||
case Qt::EditRole:
|
||||
switch (index.column()) {
|
||||
case 0: // attribute names
|
||||
if (index.column() == 0) {
|
||||
// attribute names
|
||||
switch (index.row()) {
|
||||
case 0:
|
||||
return tr("ID");
|
||||
|
@ -95,7 +115,11 @@ QVariant SyncthingDeviceModel::data(const QModelIndex &index, int role) const
|
|||
return tr("Introducer");
|
||||
}
|
||||
break;
|
||||
case 1: // attribute values
|
||||
}
|
||||
FALLTHROUGH;
|
||||
case DeviceDetail:
|
||||
if (index.column() == 1 || role == DeviceDetail) {
|
||||
// attribute values
|
||||
const SyncthingDev &dev = m_devs[static_cast<size_t>(index.parent().row())];
|
||||
switch (index.row()) {
|
||||
case 0:
|
||||
|
@ -112,7 +136,6 @@ QVariant SyncthingDeviceModel::data(const QModelIndex &index, int role) const
|
|||
case 5:
|
||||
return dev.introducer ? tr("yes") : tr("no");
|
||||
}
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case Qt::ForegroundRole:
|
||||
|
@ -145,6 +168,7 @@ QVariant SyncthingDeviceModel::data(const QModelIndex &index, int role) const
|
|||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:;
|
||||
}
|
||||
}
|
||||
|
@ -158,29 +182,8 @@ QVariant SyncthingDeviceModel::data(const QModelIndex &index, int role) const
|
|||
case 0:
|
||||
return dev.name.isEmpty() ? dev.id : dev.name;
|
||||
case 1:
|
||||
if (dev.paused) {
|
||||
return tr("Paused");
|
||||
} else {
|
||||
switch (dev.status) {
|
||||
case SyncthingDevStatus::Unknown:
|
||||
return tr("Unknown status");
|
||||
case SyncthingDevStatus::OwnDevice:
|
||||
return tr("Own device");
|
||||
case SyncthingDevStatus::Idle:
|
||||
return tr("Idle");
|
||||
case SyncthingDevStatus::Disconnected:
|
||||
return tr("Disconnected");
|
||||
case SyncthingDevStatus::Synchronizing:
|
||||
return dev.progressPercentage > 0 ? tr("Synchronizing (%1 %)").arg(dev.progressPercentage) : tr("Synchronizing");
|
||||
case SyncthingDevStatus::OutOfSync:
|
||||
return tr("Out of sync");
|
||||
case SyncthingDevStatus::Rejected:
|
||||
return tr("Rejected");
|
||||
}
|
||||
}
|
||||
break;
|
||||
return devStatusString(dev);
|
||||
}
|
||||
break;
|
||||
case Qt::DecorationRole:
|
||||
switch (index.column()) {
|
||||
case 0:
|
||||
|
@ -217,23 +220,7 @@ QVariant SyncthingDeviceModel::data(const QModelIndex &index, int role) const
|
|||
case 0:
|
||||
break;
|
||||
case 1:
|
||||
if (!dev.paused) {
|
||||
switch (dev.status) {
|
||||
case SyncthingDevStatus::Unknown:
|
||||
break;
|
||||
case SyncthingDevStatus::Disconnected:
|
||||
break;
|
||||
case SyncthingDevStatus::OwnDevice:
|
||||
case SyncthingDevStatus::Idle:
|
||||
return Colors::green(m_brightColors);
|
||||
case SyncthingDevStatus::Synchronizing:
|
||||
return Colors::blue(m_brightColors);
|
||||
case SyncthingDevStatus::OutOfSync:
|
||||
case SyncthingDevStatus::Rejected:
|
||||
return Colors::red(m_brightColors);
|
||||
}
|
||||
}
|
||||
break;
|
||||
return devStatusColor(dev);
|
||||
}
|
||||
break;
|
||||
case DeviceStatus:
|
||||
|
@ -242,6 +229,12 @@ QVariant SyncthingDeviceModel::data(const QModelIndex &index, int role) const
|
|||
return dev.paused;
|
||||
case IsOwnDevice:
|
||||
return dev.status == SyncthingDevStatus::OwnDevice;
|
||||
case DeviceStatusString:
|
||||
return devStatusString(dev);
|
||||
case DeviceStatusColor:
|
||||
return devStatusColor(dev);
|
||||
case DeviceId:
|
||||
return dev.id;
|
||||
default:;
|
||||
}
|
||||
}
|
||||
|
@ -289,9 +282,57 @@ void SyncthingDeviceModel::newDevices()
|
|||
void SyncthingDeviceModel::devStatusChanged(const SyncthingDev &, int index)
|
||||
{
|
||||
const QModelIndex modelIndex1(this->index(index, 0, QModelIndex()));
|
||||
emit dataChanged(modelIndex1, modelIndex1, QVector<int>() << Qt::DecorationRole);
|
||||
static const QVector<int> modelRoles1({ Qt::DecorationRole, DevicePaused, DeviceStatus, DeviceStatusString, DeviceStatusColor, DeviceId });
|
||||
emit dataChanged(modelIndex1, modelIndex1, modelRoles1);
|
||||
const QModelIndex modelIndex2(this->index(index, 1, QModelIndex()));
|
||||
emit dataChanged(modelIndex2, modelIndex2, QVector<int>() << Qt::DisplayRole << Qt::ForegroundRole << DeviceStatus);
|
||||
static const QVector<int> modelRoles2({ Qt::DisplayRole, Qt::ForegroundRole });
|
||||
emit dataChanged(modelIndex2, modelIndex2, modelRoles2);
|
||||
}
|
||||
|
||||
QString SyncthingDeviceModel::devStatusString(const SyncthingDev &dev)
|
||||
{
|
||||
if (dev.paused) {
|
||||
return tr("Paused");
|
||||
}
|
||||
switch (dev.status) {
|
||||
case SyncthingDevStatus::Unknown:
|
||||
return tr("Unknown status");
|
||||
case SyncthingDevStatus::OwnDevice:
|
||||
return tr("Own device");
|
||||
case SyncthingDevStatus::Idle:
|
||||
return tr("Idle");
|
||||
case SyncthingDevStatus::Disconnected:
|
||||
return tr("Disconnected");
|
||||
case SyncthingDevStatus::Synchronizing:
|
||||
return dev.progressPercentage > 0 ? tr("Synchronizing (%1 %)").arg(dev.progressPercentage) : tr("Synchronizing");
|
||||
case SyncthingDevStatus::OutOfSync:
|
||||
return tr("Out of sync");
|
||||
case SyncthingDevStatus::Rejected:
|
||||
return tr("Rejected");
|
||||
}
|
||||
return QString();
|
||||
}
|
||||
|
||||
QColor SyncthingDeviceModel::devStatusColor(const SyncthingDev &dev) const
|
||||
{
|
||||
if (dev.paused) {
|
||||
return QColor();
|
||||
}
|
||||
switch (dev.status) {
|
||||
case SyncthingDevStatus::Unknown:
|
||||
break;
|
||||
case SyncthingDevStatus::Disconnected:
|
||||
break;
|
||||
case SyncthingDevStatus::OwnDevice:
|
||||
case SyncthingDevStatus::Idle:
|
||||
return Colors::green(m_brightColors);
|
||||
case SyncthingDevStatus::Synchronizing:
|
||||
return Colors::blue(m_brightColors);
|
||||
case SyncthingDevStatus::OutOfSync:
|
||||
case SyncthingDevStatus::Rejected:
|
||||
return Colors::red(m_brightColors);
|
||||
}
|
||||
return QColor();
|
||||
}
|
||||
|
||||
} // namespace Data
|
||||
|
|
|
@ -14,11 +14,20 @@ struct SyncthingDev;
|
|||
class LIB_SYNCTHING_MODEL_EXPORT SyncthingDeviceModel : public SyncthingModel {
|
||||
Q_OBJECT
|
||||
public:
|
||||
enum SyncthingDeviceModelRole { DeviceStatus = Qt::UserRole + 1, DevicePaused, IsOwnDevice };
|
||||
enum SyncthingDeviceModelRole {
|
||||
DeviceStatus = Qt::UserRole + 1,
|
||||
DevicePaused,
|
||||
IsOwnDevice,
|
||||
DeviceStatusString,
|
||||
DeviceStatusColor,
|
||||
DeviceId,
|
||||
DeviceDetail
|
||||
};
|
||||
|
||||
explicit SyncthingDeviceModel(SyncthingConnection &connection, QObject *parent = nullptr);
|
||||
|
||||
public Q_SLOTS:
|
||||
QHash<int, QByteArray> roleNames() const;
|
||||
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
|
||||
QModelIndex parent(const QModelIndex &child) const;
|
||||
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
|
||||
|
@ -34,6 +43,10 @@ private Q_SLOTS:
|
|||
void devStatusChanged(const SyncthingDev &, int index);
|
||||
|
||||
private:
|
||||
static QHash<int, QByteArray> initRoleNames();
|
||||
static QString devStatusString(const SyncthingDev &dev);
|
||||
QColor devStatusColor(const SyncthingDev &dev) const;
|
||||
|
||||
const std::vector<SyncthingDev> &m_devs;
|
||||
};
|
||||
|
||||
|
|
|
@ -23,6 +23,27 @@ SyncthingDirectoryModel::SyncthingDirectoryModel(SyncthingConnection &connection
|
|||
connect(&m_connection, &SyncthingConnection::dirStatusChanged, this, &SyncthingDirectoryModel::dirStatusChanged);
|
||||
}
|
||||
|
||||
QHash<int, QByteArray> SyncthingDirectoryModel::initRoleNames()
|
||||
{
|
||||
QHash<int, QByteArray> roles;
|
||||
roles[Qt::DisplayRole] = "name";
|
||||
roles[DirectoryStatus] = "status";
|
||||
roles[Qt::DecorationRole] = "statusIcon";
|
||||
roles[DirectoryStatusString] = "statusString";
|
||||
roles[DirectoryStatusColor] = "statusColor";
|
||||
roles[DirectoryPaused] = "paused";
|
||||
roles[DirectoryId] = "dirId";
|
||||
roles[DirectoryPath] = "path";
|
||||
roles[DirectoryDetail] = "detail";
|
||||
return roles;
|
||||
}
|
||||
|
||||
QHash<int, QByteArray> SyncthingDirectoryModel::roleNames() const
|
||||
{
|
||||
const static QHash<int, QByteArray> roles(initRoleNames());
|
||||
return roles;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Returns the directory info for the spcified \a index. The returned object is not persistent.
|
||||
*/
|
||||
|
@ -83,8 +104,8 @@ QVariant SyncthingDirectoryModel::data(const QModelIndex &index, int role) const
|
|||
switch (role) {
|
||||
case Qt::DisplayRole:
|
||||
case Qt::EditRole:
|
||||
switch (index.column()) {
|
||||
case 0: // attribute names
|
||||
if (index.column() == 0) {
|
||||
// attribute names
|
||||
switch (index.row()) {
|
||||
case 0:
|
||||
return tr("ID");
|
||||
|
@ -108,7 +129,11 @@ QVariant SyncthingDirectoryModel::data(const QModelIndex &index, int role) const
|
|||
return tr("Errors");
|
||||
}
|
||||
break;
|
||||
case 1: // attribute values
|
||||
}
|
||||
FALLTHROUGH;
|
||||
case DirectoryDetail:
|
||||
if (index.column() == 1 || role == DirectoryDetail) {
|
||||
// attribute values
|
||||
const SyncthingDir &dir = m_dirs[static_cast<size_t>(index.parent().row())];
|
||||
switch (index.row()) {
|
||||
case 0:
|
||||
|
@ -153,7 +178,6 @@ QVariant SyncthingDirectoryModel::data(const QModelIndex &index, int role) const
|
|||
return tr("none");
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case Qt::ForegroundRole:
|
||||
|
@ -232,25 +256,7 @@ QVariant SyncthingDirectoryModel::data(const QModelIndex &index, int role) const
|
|||
case 0:
|
||||
return dir.label.isEmpty() ? dir.id : dir.label;
|
||||
case 1:
|
||||
if (dir.paused && dir.status != SyncthingDirStatus::OutOfSync) {
|
||||
return tr("Paused");
|
||||
} else {
|
||||
switch (dir.status) {
|
||||
case SyncthingDirStatus::Unknown:
|
||||
return tr("Unknown status");
|
||||
case SyncthingDirStatus::Unshared:
|
||||
return tr("Unshared");
|
||||
case SyncthingDirStatus::Idle:
|
||||
return tr("Idle");
|
||||
case SyncthingDirStatus::Scanning:
|
||||
return dir.progressPercentage > 0 ? tr("Scanning (%1 %)").arg(dir.progressPercentage) : tr("Scanning");
|
||||
case SyncthingDirStatus::Synchronizing:
|
||||
return dir.progressPercentage > 0 ? tr("Synchronizing (%1 %)").arg(dir.progressPercentage) : tr("Synchronizing");
|
||||
case SyncthingDirStatus::OutOfSync:
|
||||
return tr("Out of sync");
|
||||
}
|
||||
}
|
||||
break;
|
||||
return dirStatusString(dir);
|
||||
}
|
||||
break;
|
||||
case Qt::DecorationRole:
|
||||
|
@ -289,30 +295,21 @@ QVariant SyncthingDirectoryModel::data(const QModelIndex &index, int role) const
|
|||
case 0:
|
||||
break;
|
||||
case 1:
|
||||
if (dir.paused && dir.status != SyncthingDirStatus::OutOfSync) {
|
||||
break;
|
||||
} else {
|
||||
switch (dir.status) {
|
||||
case SyncthingDirStatus::Unknown:
|
||||
break;
|
||||
case SyncthingDirStatus::Idle:
|
||||
return Colors::green(m_brightColors);
|
||||
case SyncthingDirStatus::Unshared:
|
||||
return Colors::orange(m_brightColors);
|
||||
case SyncthingDirStatus::Scanning:
|
||||
case SyncthingDirStatus::Synchronizing:
|
||||
return Colors::blue(m_brightColors);
|
||||
case SyncthingDirStatus::OutOfSync:
|
||||
return Colors::red(m_brightColors);
|
||||
}
|
||||
}
|
||||
break;
|
||||
return dirStatusColor(dir);
|
||||
}
|
||||
break;
|
||||
case DirectoryStatus:
|
||||
return static_cast<int>(dir.status);
|
||||
case DirectoryPaused:
|
||||
return dir.paused;
|
||||
case DirectoryStatusString:
|
||||
return dirStatusString(dir);
|
||||
case DirectoryStatusColor:
|
||||
return dirStatusColor(dir);
|
||||
case DirectoryId:
|
||||
return dir.id;
|
||||
case DirectoryPath:
|
||||
return dir.path;
|
||||
default:;
|
||||
}
|
||||
}
|
||||
|
@ -360,10 +357,57 @@ void SyncthingDirectoryModel::newDirs()
|
|||
void SyncthingDirectoryModel::dirStatusChanged(const SyncthingDir &, int index)
|
||||
{
|
||||
const QModelIndex modelIndex1(this->index(index, 0, QModelIndex()));
|
||||
emit dataChanged(modelIndex1, modelIndex1, QVector<int>() << Qt::DecorationRole);
|
||||
static const QVector<int> modelRoles1(
|
||||
{ Qt::DecorationRole, DirectoryPaused, DirectoryStatusString, DirectoryStatusColor, DirectoryId, DirectoryPath });
|
||||
emit dataChanged(modelIndex1, modelIndex1, modelRoles1);
|
||||
const QModelIndex modelIndex2(this->index(index, 1, QModelIndex()));
|
||||
emit dataChanged(modelIndex2, modelIndex2, QVector<int>() << Qt::DisplayRole << Qt::ForegroundRole);
|
||||
emit dataChanged(this->index(0, 1, modelIndex1), this->index(7, 1, modelIndex1), QVector<int>() << Qt::DisplayRole);
|
||||
static const QVector<int> modelRoles2({ Qt::DisplayRole, Qt::ForegroundRole });
|
||||
emit dataChanged(modelIndex2, modelIndex2, modelRoles2);
|
||||
static const QVector<int> modelRoles3({ Qt::DisplayRole });
|
||||
emit dataChanged(this->index(0, 1, modelIndex1), this->index(7, 1, modelIndex1), modelRoles3);
|
||||
}
|
||||
|
||||
QString SyncthingDirectoryModel::dirStatusString(const SyncthingDir &dir)
|
||||
{
|
||||
if (dir.paused && dir.status != SyncthingDirStatus::OutOfSync) {
|
||||
return tr("Paused");
|
||||
}
|
||||
switch (dir.status) {
|
||||
case SyncthingDirStatus::Unknown:
|
||||
return tr("Unknown status");
|
||||
case SyncthingDirStatus::Unshared:
|
||||
return tr("Unshared");
|
||||
case SyncthingDirStatus::Idle:
|
||||
return tr("Idle");
|
||||
case SyncthingDirStatus::Scanning:
|
||||
return dir.progressPercentage > 0 ? tr("Scanning (%1 %)").arg(dir.progressPercentage) : tr("Scanning");
|
||||
case SyncthingDirStatus::Synchronizing:
|
||||
return dir.progressPercentage > 0 ? tr("Synchronizing (%1 %)").arg(dir.progressPercentage) : tr("Synchronizing");
|
||||
case SyncthingDirStatus::OutOfSync:
|
||||
return tr("Out of sync");
|
||||
}
|
||||
return QString();
|
||||
}
|
||||
|
||||
QColor SyncthingDirectoryModel::dirStatusColor(const SyncthingDir &dir) const
|
||||
{
|
||||
if (dir.paused && dir.status != SyncthingDirStatus::OutOfSync) {
|
||||
return QColor();
|
||||
}
|
||||
switch (dir.status) {
|
||||
case SyncthingDirStatus::Unknown:
|
||||
break;
|
||||
case SyncthingDirStatus::Idle:
|
||||
return Colors::green(m_brightColors);
|
||||
case SyncthingDirStatus::Unshared:
|
||||
return Colors::orange(m_brightColors);
|
||||
case SyncthingDirStatus::Scanning:
|
||||
case SyncthingDirStatus::Synchronizing:
|
||||
return Colors::blue(m_brightColors);
|
||||
case SyncthingDirStatus::OutOfSync:
|
||||
return Colors::red(m_brightColors);
|
||||
}
|
||||
return QColor();
|
||||
}
|
||||
|
||||
QString SyncthingDirectoryModel::statusLabel(quint64 files, quint64 dirs, quint64 size)
|
||||
|
|
|
@ -14,11 +14,20 @@ struct SyncthingDir;
|
|||
class LIB_SYNCTHING_MODEL_EXPORT SyncthingDirectoryModel : public SyncthingModel {
|
||||
Q_OBJECT
|
||||
public:
|
||||
enum SyncthingDirectoryModelRole { DirectoryStatus = Qt::UserRole + 1, DirectoryPaused };
|
||||
enum SyncthingDirectoryModelRole {
|
||||
DirectoryStatus = Qt::UserRole + 1,
|
||||
DirectoryPaused,
|
||||
DirectoryStatusString,
|
||||
DirectoryStatusColor,
|
||||
DirectoryId,
|
||||
DirectoryPath,
|
||||
DirectoryDetail
|
||||
};
|
||||
|
||||
explicit SyncthingDirectoryModel(SyncthingConnection &connection, QObject *parent = nullptr);
|
||||
|
||||
public Q_SLOTS:
|
||||
QHash<int, QByteArray> roleNames() const;
|
||||
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
|
||||
QModelIndex parent(const QModelIndex &child) const;
|
||||
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
|
||||
|
@ -35,10 +44,15 @@ private Q_SLOTS:
|
|||
|
||||
private:
|
||||
static QString statusLabel(quint64 files, quint64 dirs, quint64 size);
|
||||
static QHash<int, QByteArray> initRoleNames();
|
||||
static QString dirStatusString(const SyncthingDir &dir);
|
||||
QColor dirStatusColor(const SyncthingDir &dir) const;
|
||||
|
||||
const std::vector<SyncthingDir> &m_dirs;
|
||||
};
|
||||
|
||||
} // namespace Data
|
||||
|
||||
Q_DECLARE_METATYPE(QModelIndex)
|
||||
|
||||
#endif // DATA_SYNCTHINGDIRECTORYMODEL_H
|
||||
|
|
|
@ -22,6 +22,20 @@ SyncthingDownloadModel::SyncthingDownloadModel(SyncthingConnection &connection,
|
|||
connect(&m_connection, &SyncthingConnection::downloadProgressChanged, this, &SyncthingDownloadModel::downloadProgressChanged);
|
||||
}
|
||||
|
||||
QHash<int, QByteArray> SyncthingDownloadModel::initRoleNames()
|
||||
{
|
||||
QHash<int, QByteArray> roles;
|
||||
roles[Qt::DisplayRole] = "name";
|
||||
roles[Qt::DecorationRole] = "fileIcon";
|
||||
return roles;
|
||||
}
|
||||
|
||||
QHash<int, QByteArray> SyncthingDownloadModel::roleNames() const
|
||||
{
|
||||
const static QHash<int, QByteArray> roles(initRoleNames());
|
||||
return roles;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Returns the directory info for the spcified \a index. The returned object is not persistent.
|
||||
*/
|
||||
|
|
|
@ -23,6 +23,7 @@ public:
|
|||
enum SyncthingDownloadModelRole { ItemPercentage = Qt::UserRole + 1, ItemProgressLabel };
|
||||
|
||||
public Q_SLOTS:
|
||||
QHash<int, QByteArray> roleNames() const;
|
||||
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
|
||||
QModelIndex parent(const QModelIndex &child) const;
|
||||
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
|
||||
|
@ -45,6 +46,8 @@ private Q_SLOTS:
|
|||
void downloadProgressChanged();
|
||||
|
||||
private:
|
||||
static QHash<int, QByteArray> initRoleNames();
|
||||
|
||||
const std::vector<SyncthingDir> &m_dirs;
|
||||
const QIcon m_unknownIcon;
|
||||
const QFileIconProvider m_fileIconProvider;
|
||||
|
|
Loading…
Reference in New Issue