Add implicit conversion of popularity to integer to generalize usage of popularity

* Propose the usage of the popularity type in general for the rating field
  so GUIs can show an appropriate UI element
* Do not just propose the popularity type for ID3v2 tags so a uniform UI
  element can be shown accross tag formats; and API to convert from a
  uniform scale is still TODO
This commit is contained in:
Martchus 2022-06-19 14:31:12 +02:00
parent c41046bd24
commit f52b2958df
3 changed files with 11 additions and 3 deletions

8
tag.h
View File

@ -291,7 +291,6 @@ inline TagDataType Tag::proposedDataType(KnownField field) const
switch (field) {
case KnownField::Bpm:
case KnownField::Bps:
case KnownField::Rating:
case KnownField::PartNumber:
case KnownField::TotalParts:
return TagDataType::Integer;
@ -306,8 +305,13 @@ inline TagDataType Tag::proposedDataType(KnownField field) const
return TagDataType::StandardGenreIndex;
case KnownField::MCDI:
return TagDataType::Binary;
case KnownField::Rating:
// could also be a plain integer but popularity should generally be used (and can be converted
// to an integer)
return TagDataType::Popularity;
case KnownField::SynchronizedLyrics:
return TagDataType::Undefined; // not supported so far
// not supported
return TagDataType::Undefined;
default:
return TagDataType::Text;
}

View File

@ -365,6 +365,8 @@ std::int32_t TagValue::toInteger() const
return *reinterpret_cast<std::int32_t *>(m_ptr.get());
}
throw ConversionException("Can not convert assigned data to integer because the data size is not appropriate.");
case TagDataType::Popularity:
return static_cast<std::int32_t>(toPopularity().rating);
default:
throw ConversionException(argsToString("Can not convert ", tagDataTypeString(m_type), " to integer."));
}

View File

@ -157,7 +157,9 @@ void TagValueTests::testPopularity()
CPPUNIT_ASSERT_EQUAL_MESSAGE("conversion to popularity (rating)", 42.0, popularity.rating);
CPPUNIT_ASSERT_EQUAL_MESSAGE("conversion to popularity (play counter)", std::uint64_t(123), popularity.playCounter);
CPPUNIT_ASSERT_EQUAL_MESSAGE("conversion to string", "foo|42|123"s, tagValue.toString());
CPPUNIT_ASSERT_THROW_MESSAGE("failing conversion to other type", TagValue("foo|bar"sv, TagTextEncoding::Latin1).toInteger(), ConversionException);
CPPUNIT_ASSERT_EQUAL_MESSAGE("conversion to string", 42, tagValue.toInteger());
CPPUNIT_ASSERT_THROW_MESSAGE(
"failing conversion to other type", TagValue("foo|bar"sv, TagTextEncoding::Latin1).toPopularity(), ConversionException);
}
void TagValueTests::testString()