EditTagDialog: Add button to fetch lyrics

Co-Authored-By: Jonas Kvinge <jonas@jkvinge.net>
This commit is contained in:
Célestin Matte
2024-03-06 23:16:29 +01:00
committed by Jonas Kvinge
parent a3207a5703
commit 27ee6e7643
5 changed files with 56 additions and 2 deletions

View File

@@ -631,7 +631,12 @@ void ContextView::UpdateLyrics(const quint64 id, const QString &provider, const
if (static_cast<qint64>(id) != lyrics_id_) return;
lyrics_ = lyrics + "\n\n(Lyrics from " + provider + ")\n";
if (lyrics.isEmpty()) {
lyrics_ = "No lyrics found.\n";
}
else {
lyrics_ = lyrics + "\n\n(Lyrics from " + provider + ")\n";
}
lyrics_id_ = -1;
if (action_show_lyrics_->isChecked() && !lyrics_.isEmpty()) {

View File

@@ -87,6 +87,7 @@
# include "musicbrainz/tagfetcher.h"
# include "trackselectiondialog.h"
#endif
#include "lyrics/lyricsfetcher.h"
#include "covermanager/albumcoverchoicecontroller.h"
#include "covermanager/albumcoverloader.h"
#include "covermanager/albumcoverloaderoptions.h"
@@ -112,13 +113,15 @@ EditTagDialog::EditTagDialog(Application *app, QWidget *parent)
tag_fetcher_(new TagFetcher(app->network(), this)),
results_dialog_(new TrackSelectionDialog(this)),
#endif
lyrics_fetcher_(new LyricsFetcher(app->lyrics_providers(), this)),
image_no_cover_thumbnail_(ImageUtils::GenerateNoCoverImage(QSize(128, 128), devicePixelRatioF())),
loading_(false),
ignore_edits_(false),
summary_cover_art_id_(-1),
tags_cover_art_id_(-1),
cover_art_is_set_(false),
save_tag_pending_(0) {
save_tag_pending_(0),
lyrics_id_(-1) {
QObject::connect(&*app_->album_cover_loader(), &AlbumCoverLoader::AlbumCoverLoaded, this, &EditTagDialog::AlbumCoverLoaded);
@@ -128,6 +131,7 @@ EditTagDialog::EditTagDialog(Application *app, QWidget *parent)
QObject::connect(results_dialog_, &TrackSelectionDialog::SongChosen, this, &EditTagDialog::FetchTagSongChosen);
QObject::connect(results_dialog_, &TrackSelectionDialog::finished, tag_fetcher_, &TagFetcher::Cancel);
#endif
QObject::connect(lyrics_fetcher_, &LyricsFetcher::LyricsFetched, this, &EditTagDialog::UpdateLyrics);
album_cover_choice_controller_->Init(app_);
@@ -193,6 +197,7 @@ EditTagDialog::EditTagDialog(Application *app, QWidget *parent)
#ifdef HAVE_MUSICBRAINZ
QObject::connect(ui_->fetch_tag, &QPushButton::clicked, this, &EditTagDialog::FetchTag);
#endif
QObject::connect(ui_->fetch_lyrics, &QPushButton::clicked, this, &EditTagDialog::FetchLyrics);
// Set up the album cover menu
cover_menu_ = new QMenu(this);
@@ -606,6 +611,8 @@ void EditTagDialog::SelectionChanged() {
// Set the editable fields
UpdateUI(indexes);
lyrics_id_ = -1;
// If we're editing multiple songs then we have to disable certain tabs
const bool multiple = indexes.count() > 1;
ui_->tab_widget->setTabEnabled(ui_->tab_widget->indexOf(ui_->tab_summary), !multiple);
@@ -1411,6 +1418,31 @@ void EditTagDialog::FetchTagSongChosen(const Song &original_song, const Song &ne
}
void EditTagDialog::FetchLyrics() {
if (ui_->song_list->selectionModel()->selectedIndexes().isEmpty()) return;
const Song &song = data_[ui_->song_list->selectionModel()->selectedIndexes().first().row()].current_;
lyrics_fetcher_->Clear();
ui_->lyrics->setPlainText(tr("loading..."));
lyrics_id_ = static_cast<qint64>(lyrics_fetcher_->Search(song.effective_albumartist(), song.artist(), song.album(), song.title()));
}
void EditTagDialog::UpdateLyrics(const quint64 id, const QString &provider, const QString &lyrics) {
Q_UNUSED(provider);
if (static_cast<qint64>(id) != lyrics_id_) return;
lyrics_id_ = -1;
if (lyrics.isEmpty()) {
ui_->lyrics->setPlainText(tr("Not found."));
}
else {
ui_->lyrics->setPlainText(lyrics);
}
}
void EditTagDialog::SongSaveTagsComplete(TagReaderReply *reply, const QString &filename, Song song, const UpdateCoverAction cover_action) {
--save_tag_pending_;

View File

@@ -57,6 +57,7 @@ class Ui_EditTagDialog;
#ifdef HAVE_MUSICBRAINZ
class TrackSelectionDialog;
class TagFetcher;
class LyricsFetcher;
#endif
class EditTagDialog : public QDialog {
@@ -120,6 +121,8 @@ class EditTagDialog : public QDialog {
void SongRated(const float rating);
void FetchTag();
void FetchTagSongChosen(const Song &original_song, const Song &new_metadata);
void FetchLyrics();
void UpdateLyrics(const quint64 id, const QString &provider, const QString &lyrics);
void AlbumCoverLoaded(const quint64 id, const AlbumCoverLoaderResult &cover_result);
@@ -185,6 +188,7 @@ class EditTagDialog : public QDialog {
TagFetcher *tag_fetcher_;
TrackSelectionDialog *results_dialog_;
#endif
LyricsFetcher *lyrics_fetcher_;
const QImage image_no_cover_thumbnail_;
@@ -210,6 +214,8 @@ class EditTagDialog : public QDialog {
QMap<int, Song> collection_songs_;
AlbumCoverLoaderOptions::Types cover_types_;
qint64 lyrics_id_;
};
#endif // EDITTAGDIALOG_H

View File

@@ -1131,6 +1131,13 @@
</property>
</widget>
</item>
<item row="21" column="1">
<widget class="QPushButton" name="fetch_lyrics">
<property name="text">
<string>Complete lyrics automatically</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
@@ -1213,6 +1220,7 @@
<tabstop>fetch_tag</tabstop>
<tabstop>comment</tabstop>
<tabstop>lyrics</tabstop>
<tabstop>fetch_lyrics</tabstop>
</tabstops>
<resources>
<include location="../../data/data.qrc"/>

View File

@@ -134,6 +134,9 @@ void LyricsFetcherSearch::AllProvidersFinished() {
qLog(Debug) << "Using lyrics from" << results_.last().provider << "for" << request_.artist << request_.title << "with score" << results_.last().score;
emit LyricsFetched(id_, results_.last().provider, results_.last().lyrics);
}
else {
emit LyricsFetched(id_, QString(), QString());
}
emit SearchFinished(id_, results_);