From 330e230572d80cd19f7f8d8d6d4a0109fefa0457 Mon Sep 17 00:00:00 2001 From: Maijin Date: Sun, 4 Feb 2018 18:27:48 +0100 Subject: [PATCH] Add more human readable size --- src/dialogs/NewFileDialog.cpp | 15 ++------------- src/utils/Helpers.cpp | 15 +++++++++++++++ src/utils/Helpers.h | 1 + src/widgets/Dashboard.cpp | 3 ++- src/widgets/ResourcesWidget.cpp | 3 ++- 5 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/dialogs/NewFileDialog.cpp b/src/dialogs/NewFileDialog.cpp index c4050115..35184214 100644 --- a/src/dialogs/NewFileDialog.cpp +++ b/src/dialogs/NewFileDialog.cpp @@ -3,6 +3,7 @@ #include "dialogs/NewFileDialog.h" #include "dialogs/AboutDialog.h" #include "ui_NewfileDialog.h" +#include "utils/Helpers.h" #include #include @@ -49,18 +50,6 @@ static QIcon getIconFor(const QString& str, int pos) return QIcon(pixmap); } -static QString formatBytecount(const long bytecount) -{ - const int exp = log(bytecount) / log(1000); - constexpr char suffixes[] = {' ', 'k', 'M', 'G', 'T', 'P', 'E'}; - - QString str; - QTextStream stream(&str); - stream << qSetRealNumberPrecision(3) << bytecount / pow(1000, exp) - << ' ' << suffixes[exp] << 'B'; - return stream.readAll(); -} - NewFileDialog::NewFileDialog(QWidget *parent) : QDialog(parent), ui(new Ui::NewFileDialog) @@ -264,7 +253,7 @@ bool NewFileDialog::fillRecentFilesList() { QListWidgetItem *item = new QListWidgetItem( getIconFor(name, i++), - file + "\nCreated: " + info.created().toString() + "\nSize: " + formatBytecount(info.size()) + file + "\nCreated: " + info.created().toString() + "\nSize: " + qhelpers::formatBytecount(info.size()) ); //":/img/icons/target.svg"), name ); item->setData(Qt::UserRole, file); diff --git a/src/utils/Helpers.cpp b/src/utils/Helpers.cpp index 5a887b1a..f61fdd08 100644 --- a/src/utils/Helpers.cpp +++ b/src/utils/Helpers.cpp @@ -1,8 +1,10 @@ #include "utils/Helpers.h" +#include #include #include #include +#include #include #include #include @@ -11,6 +13,7 @@ #include + static QAbstractItemView::ScrollMode scrollMode() { const bool use_scrollperpixel = true; @@ -20,6 +23,18 @@ static QAbstractItemView::ScrollMode scrollMode() namespace qhelpers { + + QString formatBytecount(const long bytecount) + { + const int exp = log(bytecount) / log(1000); + constexpr char suffixes[] = {' ', 'k', 'M', 'G', 'T', 'P', 'E'}; + + QString str; + QTextStream stream(&str); + stream << qSetRealNumberPrecision(3) << bytecount / pow(1000, exp) + << ' ' << suffixes[exp] << 'B'; + return stream.readAll(); + } void adjustColumns(QTreeView *tv, int columnCount, int padding) { for (int i = 0; i != columnCount; ++i) diff --git a/src/utils/Helpers.h b/src/utils/Helpers.h index 969c1846..775ce917 100644 --- a/src/utils/Helpers.h +++ b/src/utils/Helpers.h @@ -17,6 +17,7 @@ class QTreeView; namespace qhelpers { + QString formatBytecount(const long bytecount); void adjustColumns(QTreeView *tv, int columnCount, int padding); void adjustColumns(QTreeWidget *tw, int padding); diff --git a/src/widgets/Dashboard.cpp b/src/widgets/Dashboard.cpp index a9f7c7e7..0f82ce5b 100644 --- a/src/widgets/Dashboard.cpp +++ b/src/widgets/Dashboard.cpp @@ -1,5 +1,6 @@ #include "Dashboard.h" #include "ui_Dashboard.h" +#include "utils/Helpers.h" #include "MainWindow.h" @@ -44,7 +45,7 @@ void Dashboard::updateContents() this->ui->formatEdit->setText(item["format"].toString()); this->ui->modeEdit->setText(item["mode"].toString()); this->ui->typeEdit->setText(item["type"].toString()); - this->ui->sizeEdit->setText(QString::number(item["size"].toDouble())); + this->ui->sizeEdit->setText(qhelpers::formatBytecount(item["size"].toDouble())); this->ui->fdEdit->setText(QString::number(item["fd"].toDouble())); this->ui->archEdit->setText(item2["arch"].toString()); diff --git a/src/widgets/ResourcesWidget.cpp b/src/widgets/ResourcesWidget.cpp index 95e2b993..98bfbd8d 100644 --- a/src/widgets/ResourcesWidget.cpp +++ b/src/widgets/ResourcesWidget.cpp @@ -1,3 +1,4 @@ +#include "utils/Helpers.h" #include "ResourcesWidget.h" #include @@ -34,7 +35,7 @@ QVariant ResourcesModel::data(const QModelIndex &index, int role) const case TYPE: return res->type; case SIZE: - return res->size; + return qhelpers::formatBytecount(res->size); case LANG: return res->lang; default: