Warning, file /utilities/krusader/app/DiskUsage/filelightParts/fileTree.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     SPDX-FileCopyrightText: 2004 Max Howell <max.howell@methylblue.com>
0003     SPDX-FileCopyrightText: 2004-2022 Krusader Krew <https://krusader.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "fileTree.h"
0009 
0010 // QtCore
0011 #include <QLocale>
0012 #include <QString>
0013 
0014 // static definitions
0015 const FileSize File::DENOMINATOR[4] = {1ull, 1ull << 10, 1ull << 20, 1ull << 30};
0016 const char File::PREFIX[5][2] = {"", "K", "M", "G", "T"};
0017 
0018 QString File::fullPath(const Directory *root /*= 0*/) const
0019 {
0020     QString path;
0021 
0022     if (root == this)
0023         root = nullptr; // prevent returning empty string when there is something we could return
0024 
0025     const File *d;
0026 
0027     for (d = this; d != root && d && d->parent() != nullptr; d = d->parent()) {
0028         if (!path.isEmpty())
0029             path = '/' + path;
0030 
0031         path = d->name() + path;
0032     }
0033 
0034     if (d) {
0035         while (d->parent())
0036             d = d->parent();
0037 
0038         if (d->directory().endsWith('/'))
0039             return d->directory() + path;
0040         else
0041             return d->directory() + '/' + path;
0042     } else
0043         return path;
0044 }
0045 
0046 QString File::humanReadableSize(UnitPrefix key /*= mega*/) const // FIXME inline
0047 {
0048     return humanReadableSize(m_size, key);
0049 }
0050 
0051 QString File::humanReadableSize(FileSize size, UnitPrefix key /*= mega*/) // static
0052 {
0053     QString s;
0054     double prettySize = (double)size / (double)DENOMINATOR[key];
0055     const QLocale locale;
0056 
0057     if (prettySize >= 0.01) {
0058         if (prettySize < 1)
0059             s = locale.toString(prettySize, 'f', 2);
0060         else if (prettySize < 100)
0061             s = locale.toString(prettySize, 'f', 1);
0062         else
0063             s = locale.toString(prettySize, 'f', 0);
0064 
0065         s += ' ';
0066         s += PREFIX[key];
0067         s += 'B';
0068     }
0069 
0070     if (prettySize < 0.1) {
0071         s += " (";
0072         s += locale.toString(size / DENOMINATOR[key ? key - 1 : 0]);
0073         s += ' ';
0074         s += PREFIX[key];
0075         s += "B)";
0076     }
0077 
0078     return s;
0079 }