From 2b2762c045a106e4171e23b3e199b8ee27f39993 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luis=20=C3=81ngel=20San=20Mart=C3=ADn?= Date: Tue, 13 Feb 2024 18:41:23 +0100 Subject: [PATCH] Improve and fix comic file size format --- CHANGELOG.md | 1 + YACReaderLibrary/db/comic_model.cpp | 13 ++++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7206e4fb..45d296b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ Version counting is based on semantic versioning (Major.Feature.Patch) * Fix columns in the search results. * Fix type not being propagated to new folders from their parents. * Fix default type set to folders in the root folder when they are added. +* Improve and fix comic file size format. ## 9.14.1 diff --git a/YACReaderLibrary/db/comic_model.cpp b/YACReaderLibrary/db/comic_model.cpp index 23e4cd78..a32cc1bd 100644 --- a/YACReaderLibrary/db/comic_model.cpp +++ b/YACReaderLibrary/db/comic_model.cpp @@ -290,7 +290,18 @@ QVariant ComicModel::data(const QModelIndex &index, int role) const auto item = static_cast(index.internalPointer()); auto sizeString = [=] { - return QString::number(item->data(ComicModel::Hash).toString().right(item->data(ComicModel::Hash).toString().length() - 40).toInt() / 1024.0 / 1024.0, 'f', 2) + "Mb"; + auto bytes = item->data(ComicModel::Hash).toString().right(item->data(ComicModel::Hash).toString().length() - 40).toULongLong(); + + QStringList units = { "B", "KB", "MB", "GB", "TB" }; + int i; + double outputSize = bytes; + for (i = 0; i < units.size() - 1; i++) { + if (outputSize < 1024) { + break; + } + outputSize = outputSize / 1024; + } + return QString("%1 %2").arg(outputSize, 0, 'f', 2).arg(units[i]); }; if (role == NumberRole)