1
0
mirror of https://gitlab.com/kelteseth/ScreenPlay.git synced 2024-09-15 06:52:34 +02:00

Add storage list model based on QStorageInfo

This commit is contained in:
Elias Steurer 2020-01-12 17:07:05 +01:00
parent de926550c2
commit 20d2667548
5 changed files with 157 additions and 8 deletions

View File

@ -6,19 +6,22 @@ CONFIG += plugin c++17
TARGET = $$qtLibraryTarget($$TARGET)
uri = ScreenPlay.Sysinfo
# Input
SOURCES += \
screenplaysysinfo_plugin.cpp \
sysinfo.cpp \
cpu.cpp \
ram.cpp
cpu.cpp \
ram.cpp \
storage.cpp
HEADERS += \
screenplaysysinfo_plugin.h \
sysinfo.h \
cpu.h \
ram.h \
mathhelper.h
cpu.h \
ram.h \
mathhelper.h \
storage.h
DISTFILES = qmldir

View File

@ -0,0 +1,95 @@
#include "storage.h"
Storage::Storage(QObject* parent)
: QAbstractListModel(parent)
{
loadStorageDevices();
}
QHash<int, QByteArray> Storage::roleNames() const
{
static const QHash<int, QByteArray> roles {
{ static_cast<int>(StorageRole::Name), "name" },
{ static_cast<int>(StorageRole::DisplayName), "displayName" },
{ static_cast<int>(StorageRole::IsReadOnly), "isReadOnly" },
{ static_cast<int>(StorageRole::IsReady), "isReady" },
{ static_cast<int>(StorageRole::IsRoot), "isRoot" },
{ static_cast<int>(StorageRole::IsValid), "isValid" },
{ static_cast<int>(StorageRole::BytesAvailable), "bytesAvailable" },
{ static_cast<int>(StorageRole::BytesTotal), "bytesTotal" },
{ static_cast<int>(StorageRole::BytesFree), "bytesFree" },
{ static_cast<int>(StorageRole::FileSystemType), "fileSystemType" },
};
return roles;
}
int Storage::rowCount(const QModelIndex& parent) const
{
if (parent.isValid())
return 0;
return m_storageInfoList.count();
}
QVariant Storage::data(const QModelIndex& index, int role) const
{
if (!index.isValid())
return QVariant();
int row = index.row();
if (row < 0 || row >= m_storageInfoList.count()) {
return QVariant();
}
auto roleEnum = static_cast<StorageRole>(role);
if (index.row() < rowCount()) {
switch (roleEnum) {
case StorageRole::Name:
return m_storageInfoList.at(row).name();
case StorageRole::DisplayName:
return m_storageInfoList.at(row).displayName();
case StorageRole::IsReadOnly:
return m_storageInfoList.at(row).isReady();
case StorageRole::IsReady:
return m_storageInfoList.at(row).isReady();
case StorageRole::IsRoot:
return m_storageInfoList.at(row).isRoot();
case StorageRole::IsValid:
return m_storageInfoList.at(row).isValid();
case StorageRole::BytesAvailable:
return m_storageInfoList.at(row).bytesAvailable();
case StorageRole::BytesFree:
return m_storageInfoList.at(row).bytesFree();
case StorageRole::BytesTotal:
return m_storageInfoList.at(row).bytesTotal();
case StorageRole::FileSystemType:
return m_storageInfoList.at(row).fileSystemType();
}
}
return QVariant();
}
void Storage::refresh()
{
reset();
loadStorageDevices();
}
void Storage::reset()
{
beginResetModel();
m_storageInfoList.clear();
m_storageInfoList.squeeze();
endResetModel();
}
void Storage::loadStorageDevices()
{
beginInsertRows(QModelIndex(), 0, rowCount());
for (auto storage : QStorageInfo::mountedVolumes()) {
m_storageInfoList.append(storage);
}
endInsertRows();
}

View File

@ -0,0 +1,42 @@
#pragma once
#include <QAbstractListModel>
#include <QObject>
#include <QStorageInfo>
#include <QTimer>
#include <QVector>
class Storage : public QAbstractListModel {
Q_OBJECT
public:
explicit Storage(QObject* parent = nullptr);
enum class StorageRole {
BytesAvailable = Qt::UserRole,
BytesFree,
BytesTotal,
DisplayName,
FileSystemType,
IsReadOnly,
IsReady,
IsRoot,
IsValid,
Name,
};
Q_ENUM(StorageRole)
QHash<int, QByteArray> roleNames() const override;
int rowCount(const QModelIndex& parent = QModelIndex()) const override;
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
public slots:
void refresh();
void reset();
signals:
private:
void loadStorageDevices();
private:
QVector<QStorageInfo> m_storageInfoList;
};

View File

@ -4,5 +4,6 @@ SysInfo::SysInfo(QQuickItem* parent)
: QQuickItem(parent)
, m_ram(std::make_unique<RAM>())
, m_cpu(std::make_unique<CPU>())
, m_storage(std::make_unique<Storage>())
{
}

View File

@ -5,16 +5,18 @@
#include "cpu.h"
#include "ram.h"
#include "storage.h"
class SysInfo : public QQuickItem {
Q_OBJECT
Q_PROPERTY(RAM* ram READ ram NOTIFY ramChanged)
Q_PROPERTY(CPU* cpu READ cpu NOTIFY cpuChanged)
Q_PROPERTY(Storage* storage READ storage NOTIFY storageChanged)
public:
SysInfo(QQuickItem* parent = nullptr);
~SysInfo(){}
~SysInfo() { }
RAM* ram() const
{
@ -26,13 +28,19 @@ public:
return m_cpu.get();
}
public slots:
Storage* storage() const
{
return m_storage.get();
}
signals:
void ramChanged(RAM* ram);
void cpuChanged(CPU* cpu);
void storageChanged(Storage* storage);
private:
std::unique_ptr<RAM> m_ram;
std::unique_ptr<CPU> m_cpu;
std::unique_ptr<Storage> m_storage;
};