Correctly format byte size units.

svn path=/trunk/extragear/sysadmin/partitionmanager/; revision=1280226
This commit is contained in:
Andrius Štikonas 2012-02-15 17:21:40 +00:00
parent 6e10d3b879
commit 98034975a7
16 changed files with 47 additions and 110 deletions

View File

@ -118,5 +118,5 @@ bool Device::operator!=(const Device& other) const
QString Device::prettyName() const
{
return QString("%1 (%2, %3)").arg(deviceNode()).arg(name()).arg(Capacity(*this).toString());
return QString("%1 (%2, %3)").arg(deviceNode()).arg(name()).arg(Capacity::formatByteSize(this->capacity()));
}

View File

@ -96,7 +96,8 @@ void DevicePropsDialog::setupDialog()
dialogWidget().hideTypeRadioButtons();
}
dialogWidget().capacity().setText(Capacity(device()).toString(Capacity::AppendUnit | Capacity::AppendBytes));
dialogWidget().capacity().setText(Capacity::formatByteSize(device().capacity()));
const QString cyls = KGlobal::locale()->formatNumber(device().cylinders(), 0);
const QString heads = QString::number(device().heads());
@ -105,8 +106,8 @@ void DevicePropsDialog::setupDialog()
dialogWidget().cylinderSize().setText(i18ncp("@label", "1 Sector", "%1 Sectors", device().cylinderSize()));
dialogWidget().primariesMax().setText(maxPrimaries);
dialogWidget().logicalSectorSize().setText(Capacity(device().logicalSectorSize()).toString(Capacity::Byte, Capacity::AppendUnit));
dialogWidget().physicalSectorSize().setText(Capacity(device().physicalSectorSize()).toString(Capacity::Byte, Capacity::AppendUnit));
dialogWidget().logicalSectorSize().setText(Capacity::formatByteSize(device().logicalSectorSize()));
dialogWidget().physicalSectorSize().setText(Capacity::formatByteSize(device().physicalSectorSize()));
dialogWidget().totalSectors().setText(KGlobal::locale()->formatNumber(device().totalSectors(), 0));
dialogWidget().type().setText(type);

View File

@ -114,9 +114,9 @@ void InfoPane::showPartition(Qt::DockWidgetArea area, const Partition& p)
int x = 0;
int y = createHeader(p.deviceNode(), cols(area));
createLabels(i18nc("@label partition", "File system:"), p.fileSystem().name(), cols(area), x, y);
createLabels(i18nc("@label partition", "Capacity:"), Capacity(p).toString(), cols(area), x, y);
createLabels(i18nc("@label partition", "Available:"), Capacity(p, Capacity::Available).toString(), cols(area), x, y);
createLabels(i18nc("@label partition", "Used:"), Capacity(p, Capacity::Used).toString(), cols(area), x, y);
createLabels(i18nc("@label partition", "Capacity:"), Capacity::formatByteSize(p.capacity()), cols(area), x, y);
createLabels(i18nc("@label partition", "Available:"), Capacity::formatByteSize(p.available()), cols(area), x, y);
createLabels(i18nc("@label partition", "Used:"), Capacity::formatByteSize(p.used()), cols(area), x, y);
createLabels(i18nc("@label partition", "First sector:"), KGlobal::locale()->formatNumber(p.firstSector(), 0), cols(area), x, y);
createLabels(i18nc("@label partition", "Last sector:"), KGlobal::locale()->formatNumber(p.lastSector(), 0), cols(area), x, y);
createLabels(i18nc("@label partition", "Number of sectors:"), KGlobal::locale()->formatNumber(p.length(), 0), cols(area), x, y);
@ -147,13 +147,13 @@ void InfoPane::showDevice(Qt::DockWidgetArea area, const Device& d)
}
createLabels(i18nc("@label device", "Type:"), type, cols(area), x, y);
createLabels(i18nc("@label device", "Capacity:"), Capacity(d).toString(), cols(area), x, y);
createLabels(i18nc("@label device", "Capacity:"), Capacity::formatByteSize(d.capacity()), cols(area), x, y);
createLabels(i18nc("@label device", "Total sectors:"), KGlobal::locale()->formatNumber(d.totalSectors(), 0), cols(area), x, y);
createLabels(i18nc("@label device", "Heads:"), QString::number(d.heads()), cols(area), x, y);
createLabels(i18nc("@label device", "Cylinders:"), KGlobal::locale()->formatNumber(d.cylinders(), 0), cols(area), x, y);
createLabels(i18nc("@label device", "Sectors:"), KGlobal::locale()->formatNumber(d.sectorsPerTrack(), 0), cols(area), x, y);
createLabels(i18nc("@label device", "Logical sector size:"), Capacity(d.logicalSectorSize()).toString(Capacity::Byte, Capacity::AppendUnit), cols(area), x, y);
createLabels(i18nc("@label device", "Physical sector size:"), Capacity(d.physicalSectorSize()).toString(Capacity::Byte, Capacity::AppendUnit), cols(area), x, y);
createLabels(i18nc("@label device", "Logical sector size:"), Capacity::formatByteSize(d.logicalSectorSize()), cols(area), x, y);
createLabels(i18nc("@label device", "Physical sector size:"), Capacity::formatByteSize(d.physicalSectorSize()), cols(area), x, y);
createLabels(i18nc("@label device", "Cylinder size:"), i18ncp("@label", "1 Sector", "%1 Sectors", d.cylinderSize()), cols(area), x, y);
createLabels(i18nc("@label device", "Primaries/Max:"), maxPrimaries, cols(area), x, y);
}

View File

@ -221,9 +221,9 @@ static QTreeWidgetItem* createTreeWidgetItem(const Partition& p)
item->setText(i++, p.fileSystem().label());
item->setText(i++, p.fileSystem().uuid());
item->setText(i++, Capacity(p).toString());
item->setText(i++, Capacity(p, Capacity::Used).toString());
item->setText(i++, Capacity(p, Capacity::Available).toString());
item->setText(i++, Capacity::formatByteSize(p.capacity()));
item->setText(i++, Capacity::formatByteSize(p.used()));
item->setText(i++, Capacity::formatByteSize(p.available()));
item->setText(i++, KGlobal::locale()->formatNumber(p.firstSector(), 0));
item->setText(i++, KGlobal::locale()->formatNumber(p.lastSector(), 0));

View File

@ -123,7 +123,7 @@ void PartPropsDialog::setupDialog()
// don't do this before the file system combo box has been set up!
dialogWidget().label().setText(newLabel().isEmpty() ? partition().fileSystem().label() : newLabel());
dialogWidget().capacity().setText(Capacity(partition()).toString(Capacity::AppendUnit | Capacity::AppendBytes));
dialogWidget().capacity().setText(Capacity::formatByteSize(partition().capacity()));
if (Capacity(partition(), Capacity::Available).isValid())
{
@ -131,10 +131,10 @@ void PartPropsDialog::setupDialog()
const QString availString = QString("%1% - %2")
.arg(availPercent)
.arg(Capacity(partition(), Capacity::Available).toString(Capacity::AppendUnit | Capacity::AppendBytes));
.arg(Capacity::formatByteSize(partition().available()));
const QString usedString = QString("%1% - %2")
.arg(100 - availPercent)
.arg(Capacity(partition(), Capacity::Used).toString(Capacity::AppendUnit | Capacity::AppendBytes));
.arg(Capacity::formatByteSize(partition().used()));
dialogWidget().available().setText(availString);
dialogWidget().used().setText(usedString);

View File

@ -65,7 +65,7 @@ void PartWidget::init(const Partition* p)
m_Partition = p;
if (partition())
setToolTip(partition()->deviceNode() + '\n' + partition()->fileSystem().name() + ' ' + Capacity(*partition()).toString());
setToolTip(partition()->deviceNode() + '\n' + partition()->fileSystem().name() + ' ' + Capacity::formatByteSize(partition()->capacity()));
else
setToolTip(QString());
@ -139,7 +139,7 @@ void PartWidget::paintEvent(QPaintEvent*)
drawGradient(&painter, base, QRect(0, 0, width(), height()), isActive());
// draw name and size
QString text = partition()->deviceNode().remove("/dev/") + '\n' + Capacity(*partition()).toString();
QString text = partition()->deviceNode().remove("/dev/") + '\n' + Capacity::formatByteSize(partition()->capacity());
const QRect textRect(0, 0, width() - 1, height() - 1);
const QRect boundingRect = painter.boundingRect(textRect, Qt::AlignVCenter | Qt::AlignHCenter, text);

View File

@ -86,8 +86,8 @@ void SizeDialogBase::setupConstraints()
dialogWidget().partResizerWidget().setMinimumLength(minimumLength());
dialogWidget().partResizerWidget().setMaximumLength(maximumLength());
dialogWidget().labelMinSize().setText(Capacity(minimumLength() * device().logicalSectorSize()).toString());
dialogWidget().labelMaxSize().setText(Capacity(maximumLength() * device().logicalSectorSize()).toString());
dialogWidget().labelMinSize().setText(Capacity::formatByteSize(minimumLength() * device().logicalSectorSize()));
dialogWidget().labelMaxSize().setText(Capacity::formatByteSize(maximumLength() * device().logicalSectorSize()));
dialogWidget().spinCapacity().setEnabled(canShrink() || canGrow());

View File

@ -48,7 +48,7 @@ BackupOperation::BackupOperation(Device& d, Partition& p, const QString& filenam
QString BackupOperation::description() const
{
return QString(i18nc("@info/plain", "Backup partition <filename>%1</filename> (%2, %3) to <filename>%4</filename>", backupPartition().deviceNode(), Capacity(backupPartition()).toString(), backupPartition().fileSystem().name(), fileName()));
return QString(i18nc("@info/plain", "Backup partition <filename>%1</filename> (%2, %3) to <filename>%4</filename>", backupPartition().deviceNode(), Capacity::formatByteSize(backupPartition().capacity()), backupPartition().fileSystem().name(), fileName()));
}
/** Can the given Partition be backed up?

View File

@ -59,7 +59,7 @@ bool CheckOperation::targets(const Partition& p) const
QString CheckOperation::description() const
{
return QString(i18nc("@info/plain", "Check and repair partition <filename>%1</filename> (%2, %3)", checkedPartition().deviceNode(), Capacity(checkedPartition()).toString(), checkedPartition().fileSystem().name()));
return QString(i18nc("@info/plain", "Check and repair partition <filename>%1</filename> (%2, %3)", checkedPartition().deviceNode(), Capacity::formatByteSize(checkedPartition().capacity()), checkedPartition().fileSystem().name()));
}
/** Can a Partition be checked?

View File

@ -203,40 +203,40 @@ QString CopyOperation::updateDescription() const
if (copiedPartition().length() == overwrittenPartition()->length())
return QString(i18nc("@info/plain", "Copy partition <filename>%1</filename> (%2, %3) to <filename>%4</filename> (%5, %6)",
sourcePartition().deviceNode(),
Capacity(sourcePartition()).toString(),
Capacity::formatByteSize(sourcePartition().capacity()),
sourcePartition().fileSystem().name(),
overwrittenPartition()->deviceNode(),
Capacity(*overwrittenPartition()).toString(),
Capacity::formatByteSize(overwrittenPartition()->capacity()),
overwrittenPartition()->fileSystem().name()
));
return QString(i18nc("@info/plain", "Copy partition <filename>%1</filename> (%2, %3) to <filename>%4</filename> (%5, %6) and grow it to %7",
sourcePartition().deviceNode(),
Capacity(sourcePartition()).toString(),
Capacity::formatByteSize(sourcePartition().capacity()),
sourcePartition().fileSystem().name(),
overwrittenPartition()->deviceNode(),
Capacity(*overwrittenPartition()).toString(),
Capacity::formatByteSize(overwrittenPartition()->capacity()),
overwrittenPartition()->fileSystem().name(),
Capacity(copiedPartition()).toString()
Capacity::formatByteSize(copiedPartition().capacity())
));
}
if (copiedPartition().length() == sourcePartition().length())
return QString(i18nc("@info/plain", "Copy partition <filename>%1</filename> (%2, %3) to unallocated space (starting at %4) on <filename>%5</filename>",
sourcePartition().deviceNode(),
Capacity(sourcePartition()).toString(),
Capacity::formatByteSize(sourcePartition().capacity()),
sourcePartition().fileSystem().name(),
Capacity(copiedPartition().firstSector() * targetDevice().logicalSectorSize()).toString(),
Capacity::formatByteSize(copiedPartition().firstSector() * targetDevice().logicalSectorSize()),
targetDevice().deviceNode()
));
return QString(i18nc("@info/plain", "Copy partition <filename>%1</filename> (%2, %3) to unallocated space (starting at %4) on <filename>%5</filename> and grow it to %6",
sourcePartition().deviceNode(),
Capacity(sourcePartition()).toString(),
Capacity::formatByteSize(sourcePartition().capacity()),
sourcePartition().fileSystem().name(),
Capacity(copiedPartition().firstSector() * targetDevice().logicalSectorSize()).toString(),
Capacity::formatByteSize(copiedPartition().firstSector() * targetDevice().logicalSectorSize()),
targetDevice().deviceNode(),
Capacity(copiedPartition()).toString()
Capacity::formatByteSize(copiedPartition().capacity())
));
}

View File

@ -83,9 +83,9 @@ void DeleteOperation::undo()
QString DeleteOperation::description() const
{
if (isSecure())
return QString(i18nc("@info/plain", "Shred partition <filename>%1</filename> (%2, %3)", deletedPartition().deviceNode(), Capacity(deletedPartition()).toString(), deletedPartition().fileSystem().name()));
return QString(i18nc("@info/plain", "Shred partition <filename>%1</filename> (%2, %3)", deletedPartition().deviceNode(), Capacity::formatByteSize(deletedPartition().capacity()), deletedPartition().fileSystem().name()));
else
return QString(i18nc("@info/plain", "Delete partition <filename>%1</filename> (%2, %3)", deletedPartition().deviceNode(), Capacity(deletedPartition()).toString(), deletedPartition().fileSystem().name()));
return QString(i18nc("@info/plain", "Delete partition <filename>%1</filename> (%2, %3)", deletedPartition().deviceNode(), Capacity::formatByteSize(deletedPartition().capacity()), deletedPartition().fileSystem().name()));
}
void DeleteOperation::checkAdjustLogicalNumbers(Partition& p, bool undo)

View File

@ -102,7 +102,7 @@ void NewOperation::undo()
QString NewOperation::description() const
{
return QString(i18nc("@info/plain", "Create a new partition (%1, %2) on <filename>%3</filename>", Capacity(newPartition()).toString(), newPartition().fileSystem().name(), targetDevice().deviceNode()));
return QString(i18nc("@info/plain", "Create a new partition (%1, %2) on <filename>%3</filename>", Capacity::formatByteSize(newPartition().capacity()), newPartition().fileSystem().name(), targetDevice().deviceNode()));
}
/** Can a Partition be created somewhere?

View File

@ -200,10 +200,10 @@ QString ResizeOperation::description() const
// Each of these needs a different description. And for reasons of i18n, we cannot
// just concatenate strings together...
const QString moveDelta = Capacity(qAbs(newFirstSector() - origFirstSector()) * targetDevice().logicalSectorSize()).toString();
const QString moveDelta = Capacity::formatByteSize(qAbs(newFirstSector() - origFirstSector()) * targetDevice().logicalSectorSize());
const QString origCapacity = Capacity(origLength() * targetDevice().logicalSectorSize()).toString();
const QString newCapacity = Capacity(newLength() * targetDevice().logicalSectorSize()).toString();
const QString origCapacity = Capacity::formatByteSize(origLength() * targetDevice().logicalSectorSize());
const QString newCapacity = Capacity::formatByteSize(newLength() * targetDevice().logicalSectorSize());
switch(resizeAction())
{

View File

@ -172,7 +172,7 @@ QString RestoreOperation::description() const
if (overwrittenPartition())
return QString(i18nc("@info/plain", "Restore partition from <filename>%1</filename> to <filename>%2</filename>", fileName(), overwrittenPartition()->deviceNode()));
return QString(i18nc("@info/plain", "Restore partition on <filename>%1</filename> at %2 from <filename>%3</filename>", targetDevice().deviceNode(), Capacity(restorePartition().firstSector() * targetDevice().logicalSectorSize()).toString(), fileName()));
return QString(i18nc("@info/plain", "Restore partition on <filename>%1</filename> at %2 from <filename>%3</filename>", targetDevice().deviceNode(), Capacity::formatByteSize(restorePartition().firstSector() * targetDevice().logicalSectorSize()), fileName()));
}
void RestoreOperation::setOverwrittenPartition(Partition* p)

View File

@ -62,35 +62,6 @@ Capacity::Capacity(const Device& d) :
{
}
/** Returns the Capacity as string.
@param f flags to set how to format the Capacity
@return the Capacity as string */
QString Capacity::toString(Flags f) const
{
return toString(bestUnit(), f);
}
/** Returns the Capacity as string.
@param u the unit to use
@param f flags to set how to format the Capacity
@return the Capacity as string */
QString Capacity::toString(Unit u, Flags f) const
{
QString s = toStringInternal(u);
if ((f & AppendUnit) && s != invalidString())
s += ' ' + unitName(u, m_Size);
if ((f & AppendBytes) && s != invalidString())
s += " (" + KGlobal::locale()->formatNumber(m_Size, 0) + ' ' + unitName(Byte, m_Size) + ')';
return s;
}
/** @return the Capacity as bytes */
qint64 Capacity::toInt() const
{
return static_cast<qint64>(m_Size / unitFactor(Byte, bestUnit()));
}
/** Returns the Capacity as qint64 converted to the given Unit.
@param u the Unit to use
@return the Capacity in the given Unit as qint64
@ -135,29 +106,6 @@ qint64 Capacity::unitFactor(Unit from, Unit to)
return result;
}
/** Determine the best Unit to use for the Capacity.
@return the best Unit to use
*/
Capacity::Unit Capacity::bestUnit() const
{
qint32 u = Byte;
qint64 size = m_Size;
while (size > 850)
{
size /= 1024;
u++;
}
return static_cast<Unit>(u);
}
/** @return the name of the Unit used in bestUnit() */
QString Capacity::unitName() const
{
return unitName(bestUnit(), m_Size);
}
/** Returns the name of a given Unit.
@param u the Unit to find the name for
@return the name
@ -191,18 +139,14 @@ bool Capacity::isValid() const
return m_Size >= 0;
}
QString Capacity::toStringInternal(Unit u) const
{
if (m_Size < 0)
return invalidString();
qint64 unitSize = unitFactor(Byte, u);
const double num = static_cast<double>(m_Size) / static_cast<double>(unitSize);
return KGlobal::locale()->formatNumber(num, u == Byte ? 0 : -1);
}
Capacity::Unit Capacity::preferredUnit()
{
return static_cast<Unit>(Config::preferredUnit());
}
QString Capacity::formatByteSize(double size, int precision)
{
if (size < 0)
return invalidString();
return KGlobal::locale()->formatByteSize(size, precision);
}

View File

@ -57,25 +57,17 @@ class Capacity
bool operator>=(const Capacity& other) const { return other.m_Size >= m_Size; }
bool operator<=(const Capacity& other) const { return other.m_Size <= m_Size; }
QString toString(Flags f = AppendUnit) const;
QString toString(Unit u, Flags f) const;
qint64 toInt() const;
qint64 toInt(Unit u) const;
double toDouble(Unit u) const;
QString unitName() const;
Unit bestUnit() const;
bool isValid() const;
static QString formatByteSize(double size, int precision = 2);
static const QString& invalidString() { return m_InvalidString; } /**< @return string representing an invalid capacity */
static QString unitName(Unit u, qint64 val = 1);
static qint64 unitFactor(Unit from, Unit to);
static Unit preferredUnit();
protected:
QString toStringInternal(Unit u) const;
private:
qint64 m_Size;
static const QString m_InvalidString;