From 1edfc63cbcf446e2ff3a67b41303a0663e5c3acc Mon Sep 17 00:00:00 2001 From: lennlouis Date: Fri, 8 Nov 2024 09:24:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=A3=81=E7=9B=98=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E9=87=8F=E8=A1=A8=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- disksizetable/disksizetable/disksizetable.cpp | 294 ++++++++++++++++++ disksizetable/disksizetable/disksizetable.h | 60 ++++ disksizetable/disksizetable/disksizetable.pro | 33 ++ disksizetable/disksizetable/main.cpp | 11 + disksizetable/disksizetable/widget.cpp | 15 + disksizetable/disksizetable/widget.h | 21 ++ disksizetable/disksizetable/widget.ui | 31 ++ 7 files changed, 465 insertions(+) create mode 100644 disksizetable/disksizetable/disksizetable.cpp create mode 100644 disksizetable/disksizetable/disksizetable.h create mode 100644 disksizetable/disksizetable/disksizetable.pro create mode 100644 disksizetable/disksizetable/main.cpp create mode 100644 disksizetable/disksizetable/widget.cpp create mode 100644 disksizetable/disksizetable/widget.h create mode 100644 disksizetable/disksizetable/widget.ui diff --git a/disksizetable/disksizetable/disksizetable.cpp b/disksizetable/disksizetable/disksizetable.cpp new file mode 100644 index 0000000..229ae3e --- /dev/null +++ b/disksizetable/disksizetable/disksizetable.cpp @@ -0,0 +1,294 @@ +#pragma executioncharacterset("utf-8") +#include "disksizetable.h" +#include +#include +#include +#include +#include +#include +#include +#include +#ifdef Q_OS_WIN +#include "windows.h" +#endif + +#define KB (1024) +#define MB (1024 * 1024) +#define GB (1024 * 1024 *1024) + +DiskSizeTable::DiskSizeTable(QWidget *parent) +{ + bgColor = QColor(255, 255, 255); +// bgColor = QColor(0, 255, 255); + pgrColor1 = QColor(24, 189, 155); + pgrColor2 = QColor(255, 107, 107); + + textColor1 = QColor(10, 10, 10); + textColor2 = QColor(255, 255, 255); + textColor3 = QColor(255, 255, 255); + + process = new QProcess(this); + connect(process, SIGNAL(readyRead()), this, SLOT(readData())); + + this->clear(); + + this->setColumnCount(5); + this->setColumnWidth(0, 50); + this->setColumnWidth(1, 120); + this->setColumnWidth(2, 120); + this->setColumnWidth(3, 120); + this->setColumnWidth(4, 120); + + this->setStyleSheet("QTableWidget::item{padding:0px}"); + + QStringList headText; + headText << "Disk" << "Used" << "Unused" << "Total" << "Used Percent"; + + this->setHorizontalHeaderLabels(headText); + this->setSelectionBehavior(QAbstractItemView::SelectRows); + this->setEditTriggers(QAbstractItemView::NoEditTriggers); + this->setSelectionMode(QAbstractItemView::SingleSelection); + this->verticalHeader()->setVisible(true); + this->horizontalHeader()->setStretchLastSection(true); + + QTimer::singleShot(0, this, SLOT(loadData())); +} + +QColor DiskSizeTable::getBgColor() +{ + return bgColor; +} + +QColor DiskSizeTable::getPgrColor1() +{ + return pgrColor1; +} + +QColor DiskSizeTable::getPgrColor2() +{ + return pgrColor2; +} + +QColor DiskSizeTable::getPgrColor3() +{ + return pgrColor3; +} + +QColor DiskSizeTable::getTextColor1() +{ + return textColor1; +} + +QColor DiskSizeTable::getTextColor2() +{ + return textColor2; +} + +QColor DiskSizeTable::getTextColor3() +{ + return textColor3; +} + +QSize DiskSizeTable::sizeHint() +{ + return QSize(500, 300); +} + +QSize DiskSizeTable::minimumSizeHint() +{ + return QSize(200, 150); +} + +void DiskSizeTable::loadData() +{ + int row = this->rowCount(); + for (size_t i = 0; i < row; i++) { + this->removeRow(0); + } + +#ifdef Q_OS_WIN + QFileInfoList list = QDir::drives(); + + foreach (QFileInfo dir, list) { + QString dirName = dir.absolutePath(); + LPCWSTR lpcwstrDriver = (LPCWSTR)dirName.utf16(); + ULARGE_INTEGER liFreeBytesAvailable, liTotalBytes, liTotalFreeBytes; + + if (GetDiskFreeSpaceEx(lpcwstrDriver, &liFreeBytesAvailable, &liTotalBytes, &liTotalFreeBytes)) { + QString use = QString::number((double)(liTotalBytes.QuadPart - liTotalFreeBytes.QuadPart) / GB, 'f', 1); + use += "G"; + QString free = QString::number((double)liTotalFreeBytes.QuadPart / GB, 'f', 1); + free += "G"; + QString all = QString::number((double)liTotalBytes.QuadPart / GB, 'f', 1); + all += "G"; + int percent = 100 - ((double)liTotalFreeBytes.QuadPart / liTotalBytes.QuadPart) * 100; + + insertSize(dirName, use, free, all, percent); + } + } +#else + process->start("df -h"); + +#endif +} + +void DiskSizeTable::setBgColor(const QColor &color) +{ + if (this->bgColor != color) { + this->bgColor = color; + this->loadData(); + } +} + +void DiskSizeTable::setPgrColor1(const QColor &color) +{ + if (this->pgrColor1 != color) { + this->pgrColor1 = color; + this->loadData(); + } +} + +void DiskSizeTable::setPgrColor2(const QColor &color) +{ + if (this->pgrColor2 != color) { + this->pgrColor2 = color; + this->loadData(); + } +} + +void DiskSizeTable::setPgrCOlor3(const QColor &color) +{ + if (this->pgrColor3 != color) { + this->pgrColor3 = color; + this->loadData(); + } +} + +void DiskSizeTable::setTextColor1(const QColor &color) +{ + if (this->textColor1 != color) { + this->textColor1 = color; + this->loadData(); + } +} + +void DiskSizeTable::setTextColor2(const QColor &color) +{ + if (this->textColor2 != color) { + this->textColor2 = color; + this->loadData(); + } +} + +void DiskSizeTable::setTextColor3(const QColor &color) +{ + if (this->textColor3 != color) { + this->textColor3 = color; + this->loadData(); + } +} + +void DiskSizeTable::readData() +{ + while (!process->atEnd()) { + QString result = QLatin1String(process->readLine()); +#ifdef __arm__ + if (result.startsWith("/dev/root")) { + checkSize(result, "Local Storage"); + } + else if (result.startsWith("/dev/mmcblk")) { + checkSize(result, "Local Storage"); + } + else if (result.startsWith("/dev/mmcblk1p")) { + checkSize(result, "SD"); + QStringList list = result.split(" "); + emit sdDiskReceive(list.at(0)); + } + else if (result.startsWith("/dev/sd")) { + checkSize(result, "U Disk"); + QStringList list = result.split(" "); + emit uDiskReceive(list.at(0)); + } +#endif + } +} + +void DiskSizeTable::checkSize(const QString &result, const QString &name) +{ + QString dev, use, free, all; + int percent = 0; + QStringList list = result.split(" "); + int index = 0; + + for (int i = 0; i < list.count(); i++) { + QString s = list.at(i).trimmed(); + + if (s == "") { + continue; + } + + index++; + + if (index == 1) { + dev = s; + } + else if (index == 2) { + all = s; + } + else if (index == 3) { + use = s; + } + else if (index == 4) { + free = s; + } + else if (index == 5) { + percent = s.left(s.length() - 1).toInt(); + break; + } + } + + if (name.length() > 0) { + dev = name; + } + + insertSize(dev, use, free, all, percent); +} + +void DiskSizeTable::insertSize(const QString &name, const QString &use, const QString &free, const QString &all, int percent) +{ + int row = this->rowCount(); + this->insertRow(row); + + QTableWidgetItem* itemname = new QTableWidgetItem(name); + QTableWidgetItem* itemuse = new QTableWidgetItem(use); + itemuse->setTextAlignment(Qt::AlignCenter); + QTableWidgetItem* itemfree = new QTableWidgetItem(free); + itemfree->setTextAlignment(Qt::AlignCenter); + QTableWidgetItem* itemall = new QTableWidgetItem(all); + itemall->setTextAlignment(Qt::AlignCenter); + + this->setItem(row, 0, itemname); + this->setItem(row, 1, itemuse); + this->setItem(row, 2, itemfree); + this->setItem(row, 3, itemall); + + QProgressBar* bar = new QProgressBar; + bar->setRange(0, 100); + bar->setValue(percent); + + QString qss = QString("QProgressBar{background:%1;border-width:0px;border-radius:0px;text-align:center;}" + "QProgress::chunk{border-radius:0px;}").arg(bgColor.name()); + + if (percent < 50) { + qss += QString("QProgressBar{color:%1;}QProgressBar::chunk{background:%2;}").arg(textColor1.name()).arg(pgrColor1.name()); + } + else if (percent < 90) { + qss += QString("QProgressBar{color:%1;}QProgressBar::chunk{background:%2;}").arg(textColor2.name()).arg(pgrColor2.name()); + } + else { + qss += QString("QProgressBar{color:%1;}QProgressBar::chunk{background:%2;}").arg(textColor3.name()).arg(pgrColor3.name()); + } + + bar->setStyleSheet(qss); + this->setCellWidget(row, 4, bar); +} diff --git a/disksizetable/disksizetable/disksizetable.h b/disksizetable/disksizetable/disksizetable.h new file mode 100644 index 0000000..52083a6 --- /dev/null +++ b/disksizetable/disksizetable/disksizetable.h @@ -0,0 +1,60 @@ +#ifndef DISKSIZETABLE_H +#define DISKSIZETABLE_H + +#include +#include +#include + +class DiskSizeTable : public QTableWidget +{ + Q_OBJECT +public: + explicit DiskSizeTable(QWidget* parent = 0); + + QColor getBgColor(); + QColor getPgrColor1(); + QColor getPgrColor2(); + QColor getPgrColor3(); + QColor getTextColor1(); + QColor getTextColor2(); + QColor getTextColor3(); + + QSize sizeHint(); + QSize minimumSizeHint(); + +public slots: + void loadData(); + void setBgColor(const QColor& color); + + void setPgrColor1(const QColor& color); + void setPgrColor2(const QColor& color); + void setPgrCOlor3(const QColor& color); + + void setTextColor1(const QColor& color); + void setTextColor2(const QColor& color); + void setTextColor3(const QColor& color); + +signals: + void sdDiskReceive(const QString& diskName); + void uDiskReceive(const QString& diskName); + +private: + QProcess* process; + + QColor bgColor; + QColor pgrColor1; + QColor pgrColor2; + QColor pgrColor3; + QColor textColor1; + QColor textColor2; + QColor textColor3; + +private slots: + void readData(); + void checkSize(const QString& result, const QString& name); + void insertSize(const QString& name, const QString& use, + const QString& free, const QString& all, int percent); + +}; + +#endif // DISKSIZETABLE_H diff --git a/disksizetable/disksizetable/disksizetable.pro b/disksizetable/disksizetable/disksizetable.pro new file mode 100644 index 0000000..46e3587 --- /dev/null +++ b/disksizetable/disksizetable/disksizetable.pro @@ -0,0 +1,33 @@ +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +CONFIG += c++11 + +# The following define makes your compiler emit warnings if you use +# any Qt feature that has been marked deprecated (the exact warnings +# depend on your compiler). Please consult the documentation of the +# deprecated API in order to know how to port your code away from it. +DEFINES += QT_DEPRECATED_WARNINGS + +# You can also make your code fail to compile if it uses deprecated APIs. +# In order to do so, uncomment the following line. +# You can also select to disable deprecated APIs only up to a certain version of Qt. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +SOURCES += \ + disksizetable.cpp \ + main.cpp \ + widget.cpp + +HEADERS += \ + disksizetable.h \ + widget.h + +FORMS += \ + widget.ui + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target diff --git a/disksizetable/disksizetable/main.cpp b/disksizetable/disksizetable/main.cpp new file mode 100644 index 0000000..b0a4ec2 --- /dev/null +++ b/disksizetable/disksizetable/main.cpp @@ -0,0 +1,11 @@ +#include "widget.h" + +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + Widget w; + w.show(); + return a.exec(); +} diff --git a/disksizetable/disksizetable/widget.cpp b/disksizetable/disksizetable/widget.cpp new file mode 100644 index 0000000..815d5f8 --- /dev/null +++ b/disksizetable/disksizetable/widget.cpp @@ -0,0 +1,15 @@ +#include "widget.h" +#include "ui_widget.h" + +Widget::Widget(QWidget *parent) + : QWidget(parent) + , ui(new Ui::Widget) +{ + ui->setupUi(this); +} + +Widget::~Widget() +{ + delete ui; +} + diff --git a/disksizetable/disksizetable/widget.h b/disksizetable/disksizetable/widget.h new file mode 100644 index 0000000..1fe2322 --- /dev/null +++ b/disksizetable/disksizetable/widget.h @@ -0,0 +1,21 @@ +#ifndef WIDGET_H +#define WIDGET_H + +#include + +QT_BEGIN_NAMESPACE +namespace Ui { class Widget; } +QT_END_NAMESPACE + +class Widget : public QWidget +{ + Q_OBJECT + +public: + Widget(QWidget *parent = nullptr); + ~Widget(); + +private: + Ui::Widget *ui; +}; +#endif // WIDGET_H diff --git a/disksizetable/disksizetable/widget.ui b/disksizetable/disksizetable/widget.ui new file mode 100644 index 0000000..3dd905e --- /dev/null +++ b/disksizetable/disksizetable/widget.ui @@ -0,0 +1,31 @@ + + + Widget + + + + 0 + 0 + 800 + 600 + + + + Widget + + + + + + + + + + DiskSizeTable + QTableWidget +
disksizetable.h
+
+
+ + +