Add pie chart to StoragePage and improve UX :^)

This patch adds a nice little pie chart to visually show which parts of
the instance take most space. It also refactors some code and improve
the UX a bit more.

Signed-off-by: xSlendiX <slendi@socopon.com>
This commit is contained in:
xSlendiX 2022-11-17 17:50:30 +02:00
parent 16a4a4f811
commit 3187b5c1c2
4 changed files with 269 additions and 158 deletions

View File

@ -996,6 +996,7 @@ target_link_libraries(Launcher_logic
Qt${QT_VERSION_MAJOR}::Concurrent
Qt${QT_VERSION_MAJOR}::Gui
Qt${QT_VERSION_MAJOR}::Widgets
Qt${QT_VERSION_MAJOR}Charts
${Launcher_QT_LIBS}
)
target_link_libraries(Launcher_logic

View File

@ -17,40 +17,65 @@
*/
#include "StoragePage.h"
#include <QMessageBox>
#include <QStorageInfo>
#include <QTabBar>
#include "ui_StoragePage.h"
#include <filesystem>
#include <string>
unsigned get_directory_size(std::string path)
qint64 getDirectorySize(QString path)
{
if (!std::filesystem::exists(path))
return 0;
unsigned file_size_total = 0;
for (auto const& entry : std::filesystem::directory_iterator(path)) {
if (entry.is_directory())
file_size_total += get_directory_size(entry.path().string());
else
file_size_total += entry.file_size();
QDirIterator it(path, QDirIterator::Subdirectories);
qint64 fileSizeTotal = 0;
while (it.hasNext()) {
it.next();
fileSizeTotal += it.fileInfo().size();
}
return file_size_total;
return fileSizeTotal;
}
void clear_directory_inner(std::string path)
void clearDirectoryInner(QString path)
{
if (!std::filesystem::exists(path))
return;
for (auto const& entry : std::filesystem::directory_iterator(path))
std::filesystem::remove_all(entry);
QDir(path).removeRecursively();
}
StoragePage::StoragePage(BaseInstance* inst, QWidget* parent) : QWidget(parent), ui(new Ui::StoragePage), m_inst(inst)
{
ui->setupUi(this);
update_calculations();
m_confirmation_box = new QMessageBox(this);
m_confirmation_box->setWindowTitle("Confirmation");
m_confirmation_box->setIcon(QMessageBox::Warning);
m_confirmation_box->setText("Are you sure you want to proceed?");
m_confirmation_box->setStandardButtons(QMessageBox::Yes);
m_confirmation_box->addButton(QMessageBox::No);
m_confirmation_box->setDefaultButton(QMessageBox::No);
m_series = new QtCharts::QPieSeries(this);
m_series->setLabelsVisible();
m_series->setLabelsPosition(QtCharts::QPieSlice::LabelInsideHorizontal);
m_chart_view = new QtCharts::QChartView(this);
m_chart = new QtCharts::QChart();
m_chart->setParent(this);
m_chart->addSeries(m_series);
m_chart->setBackgroundVisible(false);
m_chart->setMargins(QMargins(0, 0, 0, 0));
m_chart->legend()->setAlignment(Qt::AlignLeft);
m_chart_view->setRenderHint(QPainter::Antialiasing);
ui->verticalLayout->addWidget(m_chart_view);
ui->retranslateUi(this);
updateCalculations();
connect(ui->button_goto_resouce_packs, &QPushButton::clicked, this, [&] { m_container->selectPage("resourcepacks"); });
connect(ui->button_goto_mods, &QPushButton::clicked, this, [&] { m_container->selectPage("mods"); });
connect(ui->button_goto_worlds, &QPushButton::clicked, this, [&] { m_container->selectPage("worlds"); });
connect(ui->button_goto_screenshots, &QPushButton::clicked, this, [&] { m_container->selectPage("screenshots"); });
connect(ui->button_goto_other_logs, &QPushButton::clicked, this, [&] { m_container->selectPage("logs"); });
connect(ui->button_clear_screenshots, &QPushButton::clicked, this, &StoragePage::handleClearScreenshotsButton);
connect(ui->button_clear_logs, &QPushButton::clicked, this, &StoragePage::handleClearLogsButton);
connect(ui->button_clear_all, &QPushButton::clicked, this, &StoragePage::handleClearAllButton);
}
StoragePage::~StoragePage()
@ -68,45 +93,67 @@ void StoragePage::retranslate()
ui->retranslateUi(this);
}
void StoragePage::HandleClearScreenshotsButton()
void StoragePage::handleClearScreenshotsButton()
{
auto path = (m_inst->gameRoot() + "/screenshots").toStdString();
clear_directory_inner(path);
update_calculations();
if (m_confirmation_box->exec() != QMessageBox::Yes)
return;
auto path = m_inst->gameRoot() + "/screenshots";
QDir(path).removeRecursively();
updateCalculations();
}
void StoragePage::HandleClearLogsButton()
void StoragePage::handleClearLogsButton()
{
auto path = (m_inst->gameRoot() + "/logs").toStdString();
clear_directory_inner(path);
update_calculations();
if (m_confirmation_box->exec() != QMessageBox::Yes)
return;
auto path = m_inst->gameRoot() + "/logs";
QDir(path).removeRecursively();
updateCalculations();
}
void StoragePage::HandleClearAllButton()
void StoragePage::handleClearAllButton()
{
HandleClearScreenshotsButton();
HandleClearLogsButton();
if (m_confirmation_box->exec() != QMessageBox::Yes)
return;
handleClearScreenshotsButton();
handleClearLogsButton();
}
void StoragePage::update_calculations()
void StoragePage::updateCalculations()
{
m_size_resource_packs = get_directory_size((m_inst->gameRoot() + "/texturepacks").toStdString()) +
get_directory_size((m_inst->gameRoot() + "/resourcepacks").toStdString());
m_size_mods = get_directory_size(m_inst->modsRoot().toStdString());
m_size_saves = get_directory_size((m_inst->gameRoot() + "/saves").toStdString());
m_size_screenshots = get_directory_size((m_inst->gameRoot() + "/screenshots").toStdString());
m_size_logs = get_directory_size((m_inst->gameRoot() + "/logs").toStdString());
auto size_resource_packs =
getDirectorySize((m_inst->gameRoot() + "/texturepacks")) + getDirectorySize((m_inst->gameRoot() + "/resourcepacks"));
auto size_mods = getDirectorySize(m_inst->modsRoot());
auto size_saves = getDirectorySize((m_inst->gameRoot() + "/saves"));
auto size_screenshots = getDirectorySize((m_inst->gameRoot() + "/screenshots"));
auto size_logs = getDirectorySize((m_inst->gameRoot() + "/logs"));
QLocale locale = this->locale();
ui->label_resource_packs->setText(locale.formattedDataSize(m_size_resource_packs));
ui->label_mods->setText(locale.formattedDataSize(m_size_mods));
ui->label_saves->setText(locale.formattedDataSize(m_size_saves));
ui->label_screenshots->setText(locale.formattedDataSize(m_size_screenshots));
ui->label_logs->setText(locale.formattedDataSize(m_size_logs));
ui->label_combined->setText(
locale.formattedDataSize(m_size_resource_packs + m_size_mods + m_size_saves + m_size_screenshots + m_size_logs));
auto storage_info = QStorageInfo(QDir(m_inst->gameRoot()));
auto size_remaining = storage_info.bytesAvailable();
auto size_used = storage_info.bytesTotal() - size_remaining;
connect(ui->button_clear_screenshots, &QPushButton::clicked, this, &StoragePage::HandleClearScreenshotsButton);
connect(ui->button_clear_logs, &QPushButton::clicked, this, &StoragePage::HandleClearLogsButton);
connect(ui->button_clear_all, &QPushButton::clicked, this, &StoragePage::HandleClearAllButton);
auto locale = this->locale();
ui->label_resource_packs->setText(locale.formattedDataSize(size_resource_packs));
ui->label_mods->setText(locale.formattedDataSize(size_mods));
ui->label_saves->setText(locale.formattedDataSize(size_saves));
ui->label_screenshots->setText(locale.formattedDataSize(size_screenshots));
ui->label_logs->setText(locale.formattedDataSize(size_logs));
ui->label_combined->setText(locale.formattedDataSize(size_resource_packs + size_mods + size_saves + size_screenshots + size_logs));
ui->label_used->setText(locale.formattedDataSize(size_used));
ui->label_remaining->setText(locale.formattedDataSize(size_remaining));
m_series->clear();
m_series->append("Resource packs", size_resource_packs);
m_series->append("Mods", size_mods);
m_series->append("Saves", size_saves);
m_series->append("Screenshots", size_screenshots);
m_series->append("Logs", size_logs);
m_chart_view->setChart(m_chart);
for (auto slice : m_series->slices())
slice->setLabel(slice->label() + " " + QString("%1%").arg(100 * slice->percentage(), 0, 'f', 1));
}

View File

@ -22,10 +22,14 @@
#pragma once
#include <QWidget>
#include <QtCharts/QChartView>
#include <QtCharts/QPieSeries>
#include <QtCharts/QPieSlice>
#include "BaseInstance.h"
#include "ui/pages/BasePage.h"
#include <Application.h>
#include <qmessagebox.h>
namespace Ui
{
@ -61,15 +65,19 @@ public:
}
void retranslate() override;
void HandleClearScreenshotsButton();
void HandleClearLogsButton();
void HandleClearAllButton();
void handleClearScreenshotsButton();
void handleClearLogsButton();
void handleClearAllButton();
void update_calculations();
void updateCalculations();
private:
Ui::StoragePage *ui;
BaseInstance *m_inst;
unsigned m_size_resource_packs, m_size_mods, m_size_saves, m_size_screenshots, m_size_logs;
QtCharts::QPieSeries *m_series;
QtCharts::QChart *m_chart;
QtCharts::QChartView *m_chart_view;
QMessageBox *m_confirmation_box;
};

View File

@ -13,6 +13,62 @@
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QGridLayout" name="gridLayout">
<item row="3" column="3">
<widget class="QPushButton" name="button_clear_screenshots">
<property name="text">
<string>Clear Screenshots</string>
</property>
</widget>
</item>
<item row="7" column="1">
<widget class="QLabel" name="label_remaining">
<property name="text">
<string>0 B</string>
</property>
</widget>
</item>
<item row="6" column="0">
<widget class="QLabel" name="label_7">
<property name="text">
<string>Disk space used</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QLabel" name="label_screenshots">
<property name="text">
<string>0 B</string>
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QLabel" name="label_combined">
<property name="text">
<string>0 B</string>
</property>
</widget>
</item>
<item row="7" column="0">
<widget class="QLabel" name="label_8">
<property name="text">
<string>Disk space remaining</string>
</property>
</widget>
</item>
<item row="6" column="1">
<widget class="QLabel" name="label_used">
<property name="text">
<string>0 B</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLabel" name="label_saves">
<property name="text">
<string>0 B</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="sizePolicy">
@ -26,39 +82,6 @@
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLabel" name="label_resource_packs">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>0 B</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Mods</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLabel" name="label_mods">
<property name="text">
<string>0 B</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_3">
<property name="sizePolicy">
@ -68,14 +91,20 @@
</sizepolicy>
</property>
<property name="text">
<string>Saves</string>
<string>Worlds</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLabel" name="label_saves">
<item row="7" column="3">
<widget class="QPushButton" name="pushButton_5">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>0 B</string>
<string/>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
@ -92,44 +121,10 @@
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QLabel" name="label_screenshots">
<item row="5" column="3">
<widget class="QPushButton" name="button_clear_all">
<property name="text">
<string>0 B</string>
</property>
</widget>
</item>
<item row="3" column="2">
<widget class="QPushButton" name="button_clear_screenshots">
<property name="text">
<string>Clear Screenshots</string>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="label_5">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Logs</string>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QLabel" name="label_logs">
<property name="text">
<string>0 B</string>
</property>
</widget>
</item>
<item row="4" column="2">
<widget class="QPushButton" name="button_clear_logs">
<property name="text">
<string>Clear Logs</string>
<string>Clear All</string>
</property>
</widget>
</item>
@ -146,22 +141,48 @@
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QLabel" name="label_combined">
<item row="4" column="1">
<widget class="QLabel" name="label_logs">
<property name="text">
<string>0 B</string>
</property>
</widget>
</item>
<item row="5" column="2">
<widget class="QPushButton" name="button_clear_all">
<item row="4" column="0">
<widget class="QLabel" name="label_5">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Clear all</string>
<string>Logs</string>
</property>
</widget>
</item>
<item row="2" column="2">
<widget class="QPushButton" name="pushButton">
<item row="1" column="1">
<widget class="QLabel" name="label_mods">
<property name="text">
<string>0 B</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLabel" name="label_resource_packs">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>0 B</string>
</property>
</widget>
</item>
<item row="6" column="3">
<widget class="QPushButton" name="pushButton_4">
<property name="enabled">
<bool>false</bool>
</property>
@ -173,47 +194,81 @@
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QPushButton" name="pushButton_3">
<property name="enabled">
<bool>false</bool>
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string/>
<string>Mods</string>
</property>
<property name="flat">
<bool>true</bool>
</widget>
</item>
<item row="4" column="3">
<widget class="QPushButton" name="button_clear_logs">
<property name="text">
<string>Clear Logs</string>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QPushButton" name="pushButton_2">
<widget class="QPushButton" name="button_goto_resouce_packs">
<property name="enabled">
<bool>false</bool>
<bool>true</bool>
</property>
<property name="text">
<string/>
<string>Go to Resource packs</string>
</property>
<property name="flat">
<bool>false</bool>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QPushButton" name="button_goto_mods">
<property name="enabled">
<bool>true</bool>
</property>
<property name="text">
<string>Go to Mods</string>
</property>
<property name="flat">
<bool>false</bool>
</property>
</widget>
</item>
<item row="2" column="2">
<widget class="QPushButton" name="button_goto_worlds">
<property name="enabled">
<bool>true</bool>
</property>
<property name="text">
<string>Go to Worlds</string>
</property>
<property name="flat">
<bool>false</bool>
</property>
</widget>
</item>
<item row="4" column="2">
<widget class="QPushButton" name="button_goto_other_logs">
<property name="text">
<string>Go to Other Logs</string>
</property>
</widget>
</item>
<item row="3" column="2">
<widget class="QPushButton" name="button_goto_screenshots">
<property name="text">
<string>Go to Screenshots</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<resources/>