Add more human readable size

This commit is contained in:
Maijin 2018-02-04 18:27:48 +01:00
parent 3712cf1529
commit 330e230572
5 changed files with 22 additions and 15 deletions

View File

@ -3,6 +3,7 @@
#include "dialogs/NewFileDialog.h"
#include "dialogs/AboutDialog.h"
#include "ui_NewfileDialog.h"
#include "utils/Helpers.h"
#include <QFileDialog>
#include <QtGui>
@ -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);

View File

@ -1,8 +1,10 @@
#include "utils/Helpers.h"
#include <cmath>
#include <QPlainTextEdit>
#include <QTextEdit>
#include <QFileInfo>
#include <QtCore>
#include <QCryptographicHash>
#include <QTreeWidget>
#include <QString>
@ -11,6 +13,7 @@
#include <QDockWidget>
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)

View File

@ -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);

View File

@ -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());

View File

@ -1,3 +1,4 @@
#include "utils/Helpers.h"
#include "ResourcesWidget.h"
#include <QVBoxLayout>
@ -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: