Use `float` for rating

This commit is contained in:
Jonas Kvinge 2021-10-30 18:53:14 +02:00
parent 3d0b6e6ea1
commit 5eae3ddd8a
18 changed files with 70 additions and 70 deletions

View File

@ -69,7 +69,7 @@ message SongMetadata {
optional string art_automatic = 31;
optional double rating = 32;
optional float rating = 32;
optional bool suspicious_tags = 40;

View File

@ -321,7 +321,7 @@ void TagReaderTagLib::ReadFile(const QString &filename, spb::tagreader::SongMeta
if (TagLib::ID3v2::UserTextIdentificationFrame *frame_fmps_rating = TagLib::ID3v2::UserTextIdentificationFrame::find(file_mpeg->ID3v2Tag(), "FMPS_Rating")) {
TagLib::StringList frame_field_list = frame_fmps_rating->fieldList();
if (frame_field_list.size() > 1) {
double rating = TStringToQString(frame_field_list[1]).toDouble();
float rating = TStringToQString(frame_field_list[1]).toFloat();
if (song->rating() <= 0 && rating > 0 && rating <= 1.0) {
song->set_rating(rating);
}
@ -394,7 +394,7 @@ void TagReaderTagLib::ReadFile(const QString &filename, spb::tagreader::SongMeta
{
TagLib::MP4::Item item = mp4_tag->item(kMP4_FMPS_Rating_ID);
if (item.isValid()) {
const double rating = TStringToQString(item.toStringList().toString('\n')).toDouble();
const float rating = TStringToQString(item.toStringList().toString('\n')).toFloat();
if (song->rating() <= 0 && rating > 0) {
song->set_rating(rating);
}
@ -440,7 +440,7 @@ void TagReaderTagLib::ReadFile(const QString &filename, spb::tagreader::SongMeta
if (attributes_map.contains("FMPS/Rating")) {
const TagLib::ASF::AttributeList& attributes = attributes_map["FMPS/Rating"];
if (!attributes.isEmpty()) {
double rating = TStringToQString(attributes.front().toString()).toDouble();
float rating = TStringToQString(attributes.front().toString()).toFloat();
if (song->rating() <= 0 && rating > 0) {
song->set_rating(rating);
}
@ -531,7 +531,7 @@ void TagReaderTagLib::ParseOggTag(const TagLib::Ogg::FieldListMap &map, QString
if (!map["METADATA_BLOCK_PICTURE"].isEmpty()) song->set_art_automatic(kEmbeddedCover);
if (!map["FMPS_PLAYCOUNT"].isEmpty() && song->playcount() <= 0) song->set_playcount(TStringToQString(map["FMPS_PLAYCOUNT"].front()).trimmed().toInt());
if (!map["FMPS_RATING"].isEmpty() && song->rating() <= 0) song->set_rating(TStringToQString(map["FMPS_RATING"].front()).trimmed().toDouble());
if (!map["FMPS_RATING"].isEmpty() && song->rating() <= 0) song->set_rating(TStringToQString(map["FMPS_RATING"].front()).trimmed().toFloat());
if (!map["LYRICS"].isEmpty()) Decode(map["LYRICS"].front(), song->mutable_lyrics());
else if (!map["UNSYNCEDLYRICS"].isEmpty()) Decode(map["UNSYNCEDLYRICS"].front(), song->mutable_lyrics());
@ -581,7 +581,7 @@ void TagReaderTagLib::ParseAPETag(const TagLib::APE::ItemListMap &map, QString *
}
if (map.contains("FMPS_RATING")) {
const double rating = TStringToQString(map["FMPS_RATING"].toString()).toDouble();
const float rating = TStringToQString(map["FMPS_RATING"].toString()).toFloat();
if (song->rating() <= 0 && rating > 0) {
song->set_rating(rating);
}
@ -1023,7 +1023,7 @@ TagLib::ID3v2::PopularimeterFrame *TagReaderTagLib::GetPOPMFrameFromTag(TagLib::
}
double TagReaderTagLib::ConvertPOPMRating(const int POPM_rating) {
float TagReaderTagLib::ConvertPOPMRating(const int POPM_rating) {
if (POPM_rating < 0x01) return 0.0;
else if (POPM_rating < 0x40) return 0.20;
@ -1035,7 +1035,7 @@ double TagReaderTagLib::ConvertPOPMRating(const int POPM_rating) {
}
int TagReaderTagLib::ConvertToPOPMRating(const double rating) {
int TagReaderTagLib::ConvertToPOPMRating(const float rating) {
if (rating < 0.20) return 0x00;
else if (rating < 0.40) return 0x01;

View File

@ -79,8 +79,8 @@ class TagReaderTagLib : public TagReaderBase {
QByteArray LoadEmbeddedAPEArt(const TagLib::APE::ItemListMap &map) const;
static double ConvertPOPMRating(const int POPM_rating);
static int ConvertToPOPMRating(const double rating);
static float ConvertPOPMRating(const int POPM_rating);
static int ConvertToPOPMRating(const float rating);
static TagLib::ID3v2::PopularimeterFrame *GetPOPMFrameFromTag(TagLib::ID3v2::Tag* tag);
private:

View File

@ -1948,7 +1948,7 @@ void CollectionBackend::UpdatePlayCount(const QString &artist, const QString &ti
}
void CollectionBackend::UpdateSongRating(const int id, const double rating, const bool save_tags) {
void CollectionBackend::UpdateSongRating(const int id, const float rating, const bool save_tags) {
if (id == -1) return;
@ -1956,7 +1956,7 @@ void CollectionBackend::UpdateSongRating(const int id, const double rating, cons
}
void CollectionBackend::UpdateSongsRating(const QList<int> &id_list, const double rating, const bool save_tags) {
void CollectionBackend::UpdateSongsRating(const QList<int> &id_list, const float rating, const bool save_tags) {
if (id_list.isEmpty()) return;
@ -1983,12 +1983,12 @@ void CollectionBackend::UpdateSongsRating(const QList<int> &id_list, const doubl
}
void CollectionBackend::UpdateSongRatingAsync(const int id, const double rating, const bool save_tags) {
QMetaObject::invokeMethod(this, "UpdateSongRating", Qt::QueuedConnection, Q_ARG(int, id), Q_ARG(double, rating), Q_ARG(bool, save_tags));
void CollectionBackend::UpdateSongRatingAsync(const int id, const float rating, const bool save_tags) {
QMetaObject::invokeMethod(this, "UpdateSongRating", Qt::QueuedConnection, Q_ARG(int, id), Q_ARG(float, rating), Q_ARG(bool, save_tags));
}
void CollectionBackend::UpdateSongsRatingAsync(const QList<int> &ids, const double rating, const bool save_tags) {
QMetaObject::invokeMethod(this, "UpdateSongsRating", Qt::QueuedConnection, Q_ARG(QList<int>, ids), Q_ARG(double, rating), Q_ARG(bool, save_tags));
void CollectionBackend::UpdateSongsRatingAsync(const QList<int> &ids, const float rating, const bool save_tags) {
QMetaObject::invokeMethod(this, "UpdateSongsRating", Qt::QueuedConnection, Q_ARG(QList<int>, ids), Q_ARG(float, rating), Q_ARG(bool, save_tags));
}
void CollectionBackend::UpdateLastSeen(const int directory_id, const int expire_unavailable_songs_days) {

View File

@ -212,8 +212,8 @@ class CollectionBackend : public CollectionBackendInterface {
void AddOrUpdateSongsAsync(const SongList &songs);
void UpdateSongsBySongIDAsync(const SongMap &new_songs);
void UpdateSongRatingAsync(const int id, const double rating, const bool save_tags = false);
void UpdateSongsRatingAsync(const QList<int> &ids, const double rating, const bool save_tags = false);
void UpdateSongRatingAsync(const int id, const float rating, const bool save_tags = false);
void UpdateSongsRatingAsync(const QList<int> &ids, const float rating, const bool save_tags = false);
public slots:
void Exit();
@ -240,8 +240,8 @@ class CollectionBackend : public CollectionBackendInterface {
void UpdateLastPlayed(const QString &artist, const QString &album, const QString &title, const qint64 lastplayed);
void UpdatePlayCount(const QString &artist, const QString &title, const int playcount);
void UpdateSongRating(const int id, const double rating, const bool save_tags = false);
void UpdateSongsRating(const QList<int> &id_list, const double rating, const bool save_tags = false);
void UpdateSongRating(const int id, const float rating, const bool save_tags = false);
void UpdateSongsRating(const QList<int> &id_list, const float rating, const bool save_tags = false);
void UpdateLastSeen(const int directory_id, const int expire_unavailable_songs_days);
void ExpireSongs(const int directory_id, const int expire_unavailable_songs_days);

View File

@ -224,7 +224,7 @@ struct Song::Private : public QSharedData {
QString cue_path_; // If the song has a CUE, this contains it's path.
double rating_; // Database rating, not read from tags.
float rating_; // Database rating, initial rating read from tag.
QUrl stream_url_; // Temporary stream url set by url handler.
QImage image_; // Album Cover image set by album cover loader.
@ -373,7 +373,7 @@ bool Song::init_from_file() const { return d->init_from_file_; }
const QString &Song::cue_path() const { return d->cue_path_; }
bool Song::has_cue() const { return !d->cue_path_.isEmpty(); }
double Song::rating() const { return d->rating_; }
float Song::rating() const { return d->rating_; }
bool Song::is_collection_song() const { return d->source_ == Source_Collection; }
bool Song::is_metadata_good() const { return !d->url_.isEmpty() && !d->artist_.isEmpty() && !d->title_.isEmpty(); }
@ -478,7 +478,7 @@ void Song::set_art_automatic(const QUrl &v) { d->art_automatic_ = v; }
void Song::set_art_manual(const QUrl &v) { d->art_manual_ = v; }
void Song::set_cue_path(const QString &v) { d->cue_path_ = v; }
void Song::set_rating(double v) { d->rating_ = v; }
void Song::set_rating(float v) { d->rating_ = v; }
void Song::set_stream_url(const QUrl &v) { d->stream_url_ = v; }
void Song::set_image(const QImage &i) { d->image_ = i; }
@ -922,7 +922,7 @@ void Song::ToProtobuf(spb::tagreader::SongMetadata *pb) const {
#define tostr(n) (q.value(n).isNull() ? QString() : q.value(n).toString())
#define toint(n) (q.value(n).isNull() ? -1 : q.value(n).toInt())
#define tolonglong(n) (q.value(n).isNull() ? -1 : q.value(n).toLongLong())
#define todouble(n) (q.value(n).isNull() ? -1 : q.value(n).toDouble())
#define tofloat(n) (q.value(n).isNull() ? -1 : q.value(n).toFloat())
void Song::InitFromQuery(const SqlRow &q, bool reliable_metadata, int col) {
@ -1098,7 +1098,7 @@ void Song::InitFromQuery(const SqlRow &q, bool reliable_metadata, int col) {
}
else if (Song::kColumns.value(i) == "rating") {
d->rating_ = todouble(x);
d->rating_ = tofloat(x);
}
else {
@ -1114,7 +1114,7 @@ void Song::InitFromQuery(const SqlRow &q, bool reliable_metadata, int col) {
#undef tostr
#undef toint
#undef tolonglong
#undef todouble
#undef tofloat
}
@ -1555,7 +1555,7 @@ QString Song::SampleRateBitDepthToText() const {
QString Song::PrettyRating() const {
double rating = d->rating_;
float rating = d->rating_;
if (rating == -1.0F) return "0";

View File

@ -258,7 +258,7 @@ class Song {
const QString &cue_path() const;
bool has_cue() const;
double rating() const;
float rating() const;
const QString &effective_album() const;
int effective_originalyear() const;
@ -375,7 +375,7 @@ class Song {
void set_cue_path(const QString &v);
void set_rating(const double v);
void set_rating(const float v);
void set_stream_url(const QUrl &v);
void set_image(const QImage &i);

View File

@ -504,7 +504,7 @@ void EditTagDialog::Data::set_value(const QString &id, const QVariant &value) {
else if (id == "disc") current_.set_disc(value.toInt());
else if (id == "year") current_.set_year(value.toInt());
else if (id == "compilation") current_.set_compilation(value.toBool());
else if (id == "rating") { current_.set_rating(value.toDouble()); }
else if (id == "rating") { current_.set_rating(value.toFloat()); }
else qLog(Warning) << "Unknown ID" << id;
}

View File

@ -2330,7 +2330,7 @@ void Playlist::TurnOffDynamicPlaylist() {
}
void Playlist::RateSong(const QModelIndex &idx, const double rating) {
void Playlist::RateSong(const QModelIndex &idx, const float rating) {
if (has_item_at(idx.row())) {
PlaylistItemPtr item = item_at(idx.row());
@ -2341,7 +2341,7 @@ void Playlist::RateSong(const QModelIndex &idx, const double rating) {
}
void Playlist::RateSongs(const QModelIndexList &index_list, const double rating) {
void Playlist::RateSongs(const QModelIndexList &index_list, const float rating) {
QList<int> id_list;
for (const QModelIndex &idx : index_list) {

View File

@ -295,8 +295,8 @@ class Playlist : public QAbstractListModel {
void ItemChanged(const int row);
// Changes rating of a song to the given value asynchronously
void RateSong(const QModelIndex &idx, const double rating);
void RateSongs(const QModelIndexList &index_list, const double rating);
void RateSong(const QModelIndex &idx, const float rating);
void RateSongs(const QModelIndexList &index_list, const float rating);
void set_auto_sort(const bool auto_sort) { auto_sort_ = auto_sort; }

View File

@ -522,7 +522,7 @@ void RatingItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &op
const bool hover = mouse_over_index_.isValid() && (mouse_over_index_ == idx || (selected_indexes_.contains(mouse_over_index_) && selected_indexes_.contains(idx)));
const double rating = (hover ? RatingPainter::RatingForPos(mouse_over_pos_, option.rect) : idx.data().toDouble());
const float rating = (hover ? RatingPainter::RatingForPos(mouse_over_pos_, option.rect) : idx.data().toFloat());
painter_.Paint(painter, option.rect, rating);
@ -538,10 +538,10 @@ QSize RatingItemDelegate::sizeHint(const QStyleOptionViewItem &option, const QMo
QString RatingItemDelegate::displayText(const QVariant &value, const QLocale&) const {
if (value.isNull() || value.toDouble() <= 0) return QString();
if (value.isNull() || value.toFloat() <= 0) return QString();
// Round to the nearest 0.5
const double rating = static_cast<double>(lround(value.toDouble() * RatingPainter::kStarCount * 2)) / 2;
const float rating = static_cast<float>(lround(value.toFloat() * RatingPainter::kStarCount * 2)) / 2;
return QString::number(rating, 'f', 1);

View File

@ -643,10 +643,10 @@ void PlaylistManager::PlaySmartPlaylist(PlaylistGeneratorPtr generator, bool as_
}
void PlaylistManager::RateCurrentSong(const double rating) {
void PlaylistManager::RateCurrentSong(const float rating) {
active()->RateSong(active()->current_index(), rating);
}
void PlaylistManager::RateCurrentSong(const int rating) {
RateCurrentSong(rating / 5.0);
void PlaylistManager::RateCurrentSong2(const int rating) {
RateCurrentSong(static_cast<float>(rating) / 5.0F);
}

View File

@ -108,9 +108,9 @@ class PlaylistManagerInterface : public QObject {
virtual void SetActiveStopped() = 0;
// Rate current song using 0.0 - 1.0 scale.
virtual void RateCurrentSong(const double rating) = 0;
virtual void RateCurrentSong(const float rating) = 0;
// Rate current song using 0 - 5 scale.
virtual void RateCurrentSong(const int rating) = 0;
virtual void RateCurrentSong2(const int rating) = 0;
signals:
void PlaylistManagerInitialized();
@ -218,9 +218,9 @@ class PlaylistManager : public PlaylistManagerInterface {
void PlaySmartPlaylist(PlaylistGeneratorPtr generator, const bool as_new, const bool clear) override;
// Rate current song using 0.0 - 1.0 scale.
void RateCurrentSong(const double rating) override;
void RateCurrentSong(const float rating) override;
// Rate current song using 0 - 5 scale.
void RateCurrentSong(const int rating) override;
void RateCurrentSong2(const int rating) override;
private slots:
void SetActivePlaying() override;

View File

@ -859,7 +859,7 @@ void PlaylistView::mousePressEvent(QMouseEvent *event) {
case Qt::LeftButton:{
if (idx.data(Playlist::Role_CanSetRating).toBool() && !rating_locked_) {
// Calculate which star was clicked
double new_rating = RatingPainter::RatingForPos(event->pos(), visualRect(idx));
float new_rating = RatingPainter::RatingForPos(event->pos(), visualRect(idx));
if (selectedIndexes().contains(idx)) {
// Update all the selected item ratings
QModelIndexList src_index_list;

View File

@ -244,9 +244,9 @@ class RatingBox : public RatingWidget, public ExtendedEditor {
void set_enabled(bool enabled) override { RatingWidget::setEnabled(enabled); }
QVariant value() const override { return RatingWidget::rating(); }
void set_value(const QVariant &value) override { RatingWidget::set_rating(value.toDouble()); }
void set_value(const QVariant &value) override { RatingWidget::set_rating(value.toFloat()); }
void set_partially() override { RatingWidget::set_rating(0.0); }
void set_partially() override { RatingWidget::set_rating(0.0F); }
public slots:
void set_focus() override { RatingWidget::setFocus(); }

View File

@ -44,7 +44,7 @@ RatingPainter::RatingPainter() {
// Generate the 10 states, better to do it now than on the fly
for (int i = 0; i < kStarCount * 2 + 1; ++i) {
const double rating = static_cast<double>(i) / static_cast<double>(2.0);
const float rating = static_cast<float>(i) / 2.0F;
// Clear the pixmap
stars_[i] = QPixmap(kStarSize * kStarCount, kStarSize);
@ -83,21 +83,21 @@ QRect RatingPainter::Contents(const QRect rect) {
}
double RatingPainter::RatingForPos(const QPoint pos, const QRect rect) {
float RatingPainter::RatingForPos(const QPoint pos, const QRect rect) {
const QRect contents = Contents(rect);
const double raw = static_cast<double>(pos.x() - contents.left()) / contents.width();
const float raw = static_cast<float>(pos.x() - contents.left()) / static_cast<float>(contents.width());
// Check if the position was to the right or left of the rectangle.
if (raw < 0) return 0;
if (raw > 1) return 1;
// Round to the nearest 0.1
return static_cast<double>(lround(raw * kStarCount * 2)) / (kStarCount * 2);
return static_cast<float>(lround(raw * kStarCount * 2)) / (kStarCount * 2);
}
void RatingPainter::Paint(QPainter *painter, const QRect rect, double rating) const {
void RatingPainter::Paint(QPainter *painter, const QRect rect, float rating) const {
QSize size(qMin(kStarSize * kStarCount, rect.width()), qMin(kStarSize, rect.height()));
QPoint pos(rect.center() - QPoint(size.width() / 2, size.height() / 2));
@ -124,7 +124,7 @@ QSize RatingWidget::sizeHint() const {
}
void RatingWidget::set_rating(const double rating) {
void RatingWidget::set_rating(const float rating) {
rating_ = rating;
update();

View File

@ -33,9 +33,9 @@ class RatingPainter {
static const int kStarCount = 5;
static const int kStarSize = 16;
static QRect Contents(const QRect rect);
static double RatingForPos(const QPoint pos, const QRect rect);
static float RatingForPos(const QPoint pos, const QRect rect);
void Paint(QPainter *painter, const QRect rect, double rating) const;
void Paint(QPainter *painter, const QRect rect, float rating) const;
private:
QPixmap stars_[kStarCount * 2 + 1];
@ -44,18 +44,18 @@ class RatingPainter {
class RatingWidget : public QWidget {
Q_OBJECT
Q_PROPERTY(double rating READ rating WRITE set_rating)
Q_PROPERTY(float rating READ rating WRITE set_rating)
public:
RatingWidget(QWidget *parent = nullptr);
QSize sizeHint() const override;
double rating() const { return rating_; }
void set_rating(const double rating);
float rating() const { return rating_; }
void set_rating(const float rating);
signals:
void RatingChanged(double);
void RatingChanged(float);
protected:
void paintEvent(QPaintEvent*) override;
@ -65,8 +65,8 @@ class RatingWidget : public QWidget {
private:
RatingPainter painter_;
double rating_;
double hover_rating_;
float rating_;
float hover_rating_;
};
#endif // RATINGWIDGET_H

View File

@ -1882,7 +1882,7 @@ TEST_F(TagReaderTest, TestFLACAudioFileRating) {
{
Song song = ReadSongFromFile(r.fileName());
EXPECT_EQ(0.4, song.rating());
EXPECT_EQ(0.4F, song.rating());
}
}
@ -1899,7 +1899,7 @@ TEST_F(TagReaderTest, TestWavPackAudioFileRating) {
{
Song song = ReadSongFromFile(r.fileName());
EXPECT_EQ(0.4, song.rating());
EXPECT_EQ(0.4F, song.rating());
}
}
@ -1916,7 +1916,7 @@ TEST_F(TagReaderTest, TestOggFLACAudioFileRating) {
{
Song song = ReadSongFromFile(r.fileName());
EXPECT_EQ(0.4, song.rating());
EXPECT_EQ(0.4F, song.rating());
}
}
@ -1933,7 +1933,7 @@ TEST_F(TagReaderTest, TestOggVorbisAudioFileRating) {
{
Song song = ReadSongFromFile(r.fileName());
EXPECT_EQ(0.4, song.rating());
EXPECT_EQ(0.4F, song.rating());
}
}
@ -1950,7 +1950,7 @@ TEST_F(TagReaderTest, TestOggOpusAudioFileRating) {
{
Song song = ReadSongFromFile(r.fileName());
EXPECT_EQ(0.4, song.rating());
EXPECT_EQ(0.4F, song.rating());
}
}
@ -1967,7 +1967,7 @@ TEST_F(TagReaderTest, TestOggSpeexAudioFileRating) {
{
Song song = ReadSongFromFile(r.fileName());
EXPECT_EQ(0.4, song.rating());
EXPECT_EQ(0.4F, song.rating());
}
}
@ -1984,7 +1984,7 @@ TEST_F(TagReaderTest, TestASFAudioFileRating) {
{
Song song = ReadSongFromFile(r.fileName());
EXPECT_EQ(0.4, song.rating());
EXPECT_EQ(0.4F, song.rating());
}
}
@ -2001,7 +2001,7 @@ TEST_F(TagReaderTest, TestMP3AudioFileRating) {
{
Song song = ReadSongFromFile(r.fileName());
EXPECT_EQ(0.4, song.rating());
EXPECT_EQ(0.4F, song.rating());
}
}
@ -2018,7 +2018,7 @@ TEST_F(TagReaderTest, TestMP4AudioFileRating) {
{
Song song = ReadSongFromFile(r.fileName());
EXPECT_EQ(0.4, song.rating());
EXPECT_EQ(0.4F, song.rating());
}
}