diff --git a/autotests/propertyinfotest.cpp b/autotests/propertyinfotest.cpp index f753c6f..55fa7a9 100644 --- a/autotests/propertyinfotest.cpp +++ b/autotests/propertyinfotest.cpp @@ -1,89 +1,91 @@ /* * This file is part of the KDE KFileMetaData project * Copyright (C) 2014 Vishesh Handa * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * */ #include "propertyinfotest.h" #include "propertyinfo.h" #include using namespace KFileMetaData; void PropertyInfoTest::testNameIdMapping() { // The +1 is to avoid the Empty Property int i = static_cast(Property::FirstProperty) + 1; int e = static_cast(Property::LastProperty); for (; i <= e; i++) { Property::Property p = static_cast(i); PropertyInfo pi(p); // qDebug() << pi.name(); QCOMPARE(pi.property(), p); QVERIFY(!pi.name().isEmpty()); QVERIFY(!pi.displayName().isEmpty()); PropertyInfo pi2 = PropertyInfo::fromName(pi.name()); QCOMPARE(pi2.property(), p); } } void PropertyInfoTest::testFormatAsDisplayString() { auto emptyProperty = PropertyInfo::fromName(QStringLiteral("no valid property name")); QCOMPARE(emptyProperty.formatAsDisplayString(QVariant("empty")), QStringLiteral("empty")); PropertyInfo year(Property::DiscNumber); QCOMPARE(year.formatAsDisplayString(QVariant(2018)), QStringLiteral("2018")); QStringList artistList = {"Artist1", "Artist2"}; PropertyInfo artist(Property::Artist); QCOMPARE(artist.formatAsDisplayString(QVariant(artistList)), QStringLiteral("Artist1, Artist2")); QStringList authorList = {"Author1"}; PropertyInfo author(Property::Author); QCOMPARE(artist.formatAsDisplayString(QVariant(authorList)), QStringLiteral("Author1")); PropertyInfo duration(Property::Duration); QCOMPARE(duration.formatAsDisplayString(QVariant(1800)), QStringLiteral("0:30:00")); PropertyInfo sampleRate(Property::SampleRate); QCOMPARE(sampleRate.formatAsDisplayString(QVariant(44100)), QString(QLocale().toString(44.1) + QStringLiteral(" kHz"))); PropertyInfo bitRate(Property::BitRate); - QCOMPARE(bitRate.formatAsDisplayString(QVariant(128000)), QStringLiteral("128 kB/s")); + QCOMPARE(bitRate.formatAsDisplayString(QVariant(128000)), QStringLiteral("128 kbit/s")); + QCOMPARE(bitRate.formatAsDisplayString(QVariant(1350000)), QString(QLocale().toString(1.35) + QStringLiteral(" Mbit/s"))); + QCOMPARE(bitRate.formatAsDisplayString(QVariant(14700000)), QString(QLocale().toString(14.7) + QStringLiteral(" Mbit/s"))); PropertyInfo orientation(Property::ImageOrientation); QCOMPARE(orientation.formatAsDisplayString(QVariant(5)), QStringLiteral("Transposed")); PropertyInfo altitude(Property::PhotoGpsAltitude); QCOMPARE(altitude.formatAsDisplayString(QVariant(1.1)), QString(QLocale().toString(1.1) + QStringLiteral(" m"))); PropertyInfo latitude(Property::PhotoGpsLatitude); QCOMPARE(latitude.formatAsDisplayString(QVariant(25)), QString(QLocale().toString(25) + QStringLiteral("°"))); PropertyInfo longitude(Property::PhotoGpsLongitude); QCOMPARE(longitude.formatAsDisplayString(QVariant(13.5)), QString(QLocale().toString(13.5) + QStringLiteral("°"))); PropertyInfo framerate(Property::FrameRate); QCOMPARE(framerate.formatAsDisplayString(QVariant(23)), QStringLiteral("23 fps")); } QTEST_GUILESS_MAIN(PropertyInfoTest) diff --git a/src/formatstrings.cpp b/src/formatstrings.cpp index c6e7602..69cb12d 100644 --- a/src/formatstrings.cpp +++ b/src/formatstrings.cpp @@ -1,101 +1,113 @@ /* * Copyright (C) 2018 Alexander Stippich * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * */ #include "formatstrings_p.h" +#include #include #include using namespace KFileMetaData; +int significantDigits(int value) +{ + if (value == 0) { + return 0; + } + int before_decimal_point = static_cast(log10(value > 0 ? value : -value)) % 3; + return 2 - before_decimal_point; +} + QString FormatStrings::toStringFunction(const QVariant& value) { return value.toString(); } QString FormatStrings::joinStringListFunction(const QVariant& value) { return value.toStringList().join(i18nc("Separation between multiple entries in a list", ", ")); } QString FormatStrings::formatDate(const QVariant& value) { KFormat form; QDateTime dt; if (value.type() == QVariant::DateTime) { dt = value.toDateTime(); } else { dt = QDateTime::fromString(value.toString(), Qt::ISODate); } if (dt.isValid()) { return form.formatRelativeDateTime(dt, QLocale::LongFormat); } return QString(); } QString FormatStrings::formatDuration(const QVariant& value) { KFormat form; return form.formatDuration(value.toInt() * 1000); } QString FormatStrings::formatBitRate(const QVariant& value) { KFormat form; - return i18nc("@label bitrate (per second)", "%1/s", form.formatByteSize(value.toInt(), 0, KFormat::MetricBinaryDialect)); + return i18nc("@label bitrate (per second)", "%1/s", form.formatValue(value.toInt(), + KFormat::Unit::Bit, significantDigits(value.toInt()), KFormat::UnitPrefix::AutoAdjust, KFormat::MetricBinaryDialect)); } QString FormatStrings::formatSampleRate(const QVariant& value) { - return i18nc("@label samplerate in kilohertz", "%1 kHz", QLocale().toString(value.toDouble() / 1000)); + KFormat form; + return form.formatValue(value.toInt(), KFormat::Unit::Hertz, significantDigits(value.toInt()), KFormat::UnitPrefix::AutoAdjust, KFormat::MetricBinaryDialect); } QString FormatStrings::formatOrientationValue(const QVariant& value) { QString string; switch (value.toInt()) { case 1: string = i18nc("Description of image orientation", "Unchanged"); break; case 2: string = i18nc("Description of image orientation", "Horizontally flipped"); break; case 3: string = i18nc("Description of image orientation", "180° rotated"); break; case 4: string = i18nc("Description of image orientation", "Vertically flipped"); break; case 5: string = i18nc("Description of image orientation", "Transposed"); break; case 6: string = i18nc("Description of image orientation, counter clock-wise rotated", "90° rotated CCW "); break; case 7: string = i18nc("Description of image orientation", "Transversed"); break; case 8: string = i18nc("Description of image orientation, counter clock-wise rotated", "270° rotated CCW"); break; default: break; } return string; } QString FormatStrings::formatAsDegree(const QVariant& value) { return QString(QLocale().toString(value.toDouble()) + i18nc("Symbol of degree, no space", "°")); } QString FormatStrings::formatAsMeter(const QVariant& value) { KFormat form; return form.formatValue(value.toDouble(), KFormat::Unit::Meter, 1, KFormat::UnitPrefix::AutoAdjust, KFormat::MetricBinaryDialect); } QString FormatStrings::formatAsFrameRate(const QVariant& value) { return QString(value.toString() + i18nc("Symbol of frames per second, with space", " fps")); }