feat: formatSize() implementation

This commit is contained in:
Gary Wang 2019-08-01 17:18:43 +08:00
parent 2bab3ea399
commit 41eb6531fe
3 changed files with 38 additions and 0 deletions

View file

@ -37,6 +37,15 @@ enum DGlibUserDirectory
}; };
Q_ENUMS(DGlibUserDirectory); Q_ENUMS(DGlibUserDirectory);
enum DGlibFormatSizeFlag
{
FORMAT_SIZE_DEFAULT = 0x0,
FORMAT_SIZE_LONG_FORMAT = 1 << 0,
FORMAT_SIZE_IEC_UNITS = 1 << 1,
FORMAT_SIZE_BITS = 1 << 2
};
Q_DECLARE_FLAGS(DGlibFormatSizeFlags, DGlibFormatSizeFlag)
class DGlibUtils class DGlibUtils
{ {
public: public:
@ -44,6 +53,7 @@ public:
static QStringList systemDataDirs(); static QStringList systemDataDirs();
static QString userDataDir(); static QString userDataDir();
static QString tmpDir(); static QString tmpDir();
static QString formatSize(quint64 size, DGlibFormatSizeFlags flags = FORMAT_SIZE_DEFAULT);
}; };
#endif // DGLIBUTILS_H #endif // DGLIBUTILS_H

View file

@ -93,3 +93,21 @@ QString DGlibUtils::tmpDir()
{ {
return QString::fromStdString(Glib::get_tmp_dir()); return QString::fromStdString(Glib::get_tmp_dir());
} }
/*!
* \brief Formats a size (for example the size of a file) into a human readable string.
*
* Sizes are rounded to the nearest size prefix (kB, MB, GB) and are displayed rounded to the nearest tenth. E.g. the file
* size 3292528 bytes will be converted into the string "3.2 MB".
*
* The prefix units base is 1000 (i.e. 1 kB is 1000 bytes), unless the DGlibFormatSizeFlags::FORMAT_SIZE_IEC_UNITS flag is set.
*
* \param size A size in bytes.
* \param flags Flags to modify the output.
* \return A formatted string containing a human readable file size.
*/
QString DGlibUtils::formatSize(quint64 size, DGlibFormatSizeFlags flags)
{
unsigned int flagValue = flags;
return QString::fromStdString(Glib::format_size(size, static_cast<Glib::FormatSizeFlags>(flagValue)));
}

View file

@ -4,6 +4,7 @@
#include <dgiofile.h> #include <dgiofile.h>
#include <dgiofileinfo.h> #include <dgiofileinfo.h>
#include <dgiomountoperation.h> #include <dgiomountoperation.h>
#include <dglibutils.h>
#include <giomm.h> #include <giomm.h>
@ -15,6 +16,7 @@ public:
DGioMatchGioEnumTest(); DGioMatchGioEnumTest();
private Q_SLOTS: private Q_SLOTS:
void testCase_DGlibUtilsClass();
void testCase_DGioFileClass(); void testCase_DGioFileClass();
void testCase_DGioFileInfoClass(); void testCase_DGioFileInfoClass();
void testCase_DGioMountOperationClass(); void testCase_DGioMountOperationClass();
@ -25,6 +27,14 @@ DGioMatchGioEnumTest::DGioMatchGioEnumTest()
// //
} }
void DGioMatchGioEnumTest::testCase_DGlibUtilsClass()
{
QCOMPARE(DGlibFormatSizeFlag::FORMAT_SIZE_DEFAULT, Glib::FORMAT_SIZE_DEFAULT);
QCOMPARE(DGlibFormatSizeFlag::FORMAT_SIZE_LONG_FORMAT, Glib::FORMAT_SIZE_LONG_FORMAT);
QCOMPARE(DGlibFormatSizeFlag::FORMAT_SIZE_IEC_UNITS, Glib::FORMAT_SIZE_IEC_UNITS);
QCOMPARE(DGlibFormatSizeFlag::FORMAT_SIZE_BITS, Glib::FORMAT_SIZE_BITS);
}
void DGioMatchGioEnumTest::testCase_DGioFileClass() void DGioMatchGioEnumTest::testCase_DGioFileClass()
{ {
QCOMPARE(DGioFileQueryInfoFlag::FILE_QUERY_INFO_NONE, Gio::FILE_QUERY_INFO_NONE); QCOMPARE(DGioFileQueryInfoFlag::FILE_QUERY_INFO_NONE, Gio::FILE_QUERY_INFO_NONE);