2011-01-21 00:42:28 +01:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
|
|
|
|
|
|
|
Clementine is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Clementine 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 General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2015-11-27 15:22:59 +01:00
|
|
|
#include "ui/trackselectiondialog.h"
|
2011-07-13 01:43:02 +02:00
|
|
|
#include "ui_trackselectiondialog.h"
|
2011-01-21 00:42:28 +01:00
|
|
|
|
2011-03-05 20:24:44 +01:00
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QPushButton>
|
|
|
|
#include <QShortcut>
|
2011-07-13 01:43:02 +02:00
|
|
|
#include <QTreeWidget>
|
2011-11-28 14:51:35 +01:00
|
|
|
#include <QUrl>
|
2011-03-06 15:07:41 +01:00
|
|
|
#include <QtConcurrentRun>
|
2011-01-21 00:42:28 +01:00
|
|
|
#include <QtDebug>
|
|
|
|
|
2015-11-27 15:22:59 +01:00
|
|
|
#include "core/tagreaderclient.h"
|
|
|
|
#include "ui/iconloader.h"
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
TrackSelectionDialog::TrackSelectionDialog(QWidget* parent)
|
|
|
|
: QDialog(parent), ui_(new Ui_TrackSelectionDialog), save_on_close_(false) {
|
2011-01-21 00:42:28 +01:00
|
|
|
// Setup dialog window
|
|
|
|
ui_->setupUi(this);
|
2011-03-05 20:24:44 +01:00
|
|
|
|
|
|
|
connect(ui_->song_list, SIGNAL(currentRowChanged(int)), SLOT(UpdateStack()));
|
2014-02-07 16:34:20 +01:00
|
|
|
connect(ui_->results,
|
|
|
|
SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)),
|
2011-03-05 20:24:44 +01:00
|
|
|
SLOT(ResultSelected()));
|
|
|
|
|
|
|
|
ui_->splitter->setSizes(QList<int>() << 200 << width() - 200);
|
2011-03-06 15:07:41 +01:00
|
|
|
SetLoading(QString());
|
2011-03-05 20:24:44 +01:00
|
|
|
|
|
|
|
// Add the next/previous buttons
|
2015-11-27 15:22:59 +01:00
|
|
|
previous_button_ = new QPushButton(
|
|
|
|
IconLoader::Load("go-previous", IconLoader::Base), tr("Previous"), this);
|
|
|
|
next_button_ = new QPushButton(IconLoader::Load("go-next", IconLoader::Base),
|
2015-10-14 03:01:08 +02:00
|
|
|
tr("Next"), this);
|
2011-03-05 20:24:44 +01:00
|
|
|
ui_->button_box->addButton(previous_button_, QDialogButtonBox::ResetRole);
|
|
|
|
ui_->button_box->addButton(next_button_, QDialogButtonBox::ResetRole);
|
|
|
|
|
|
|
|
connect(previous_button_, SIGNAL(clicked()), SLOT(PreviousSong()));
|
|
|
|
connect(next_button_, SIGNAL(clicked()), SLOT(NextSong()));
|
|
|
|
|
|
|
|
// Set some shortcuts for the buttons
|
|
|
|
new QShortcut(QKeySequence::Back, previous_button_, SLOT(click()));
|
|
|
|
new QShortcut(QKeySequence::Forward, next_button_, SLOT(click()));
|
2014-02-07 16:34:20 +01:00
|
|
|
new QShortcut(QKeySequence::MoveToPreviousPage, previous_button_,
|
|
|
|
SLOT(click()));
|
2011-03-05 20:24:44 +01:00
|
|
|
new QShortcut(QKeySequence::MoveToNextPage, next_button_, SLOT(click()));
|
|
|
|
|
|
|
|
// Resize columns
|
2014-02-07 16:34:20 +01:00
|
|
|
ui_->results->setColumnWidth(0, 50); // Track column
|
|
|
|
ui_->results->setColumnWidth(1, 50); // Year column
|
|
|
|
ui_->results->setColumnWidth(2, 160); // Title column
|
|
|
|
ui_->results->setColumnWidth(3, 160); // Artist column
|
|
|
|
ui_->results->setColumnWidth(4, 160); // Album column
|
2011-01-21 00:42:28 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
TrackSelectionDialog::~TrackSelectionDialog() { delete ui_; }
|
2011-01-21 00:42:28 +01:00
|
|
|
|
2011-03-05 20:24:44 +01:00
|
|
|
void TrackSelectionDialog::Init(const SongList& songs) {
|
|
|
|
ui_->song_list->clear();
|
|
|
|
ui_->stack->setCurrentWidget(ui_->loading_page);
|
|
|
|
data_.clear();
|
2011-01-21 00:42:28 +01:00
|
|
|
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const Song& song : songs) {
|
2011-03-05 20:24:44 +01:00
|
|
|
Data data;
|
|
|
|
data.original_song_ = song;
|
|
|
|
data_ << data;
|
|
|
|
|
|
|
|
QListWidgetItem* item = new QListWidgetItem(ui_->song_list);
|
2011-04-28 14:27:53 +02:00
|
|
|
item->setText(QFileInfo(song.url().toLocalFile()).fileName());
|
2011-03-05 20:24:44 +01:00
|
|
|
item->setForeground(palette().color(QPalette::Disabled, QPalette::Text));
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool multiple = songs.count() > 1;
|
|
|
|
ui_->song_list->setVisible(multiple);
|
|
|
|
next_button_->setEnabled(multiple);
|
|
|
|
previous_button_->setEnabled(multiple);
|
|
|
|
|
|
|
|
ui_->song_list->setCurrentRow(0);
|
|
|
|
}
|
|
|
|
|
2011-03-12 22:19:41 +01:00
|
|
|
void TrackSelectionDialog::FetchTagProgress(const Song& original_song,
|
|
|
|
const QString& progress) {
|
|
|
|
// Find the item with this filename
|
|
|
|
int row = -1;
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < data_.count(); ++i) {
|
2011-04-28 14:27:53 +02:00
|
|
|
if (data_[i].original_song_.url() == original_song.url()) {
|
2011-03-12 22:19:41 +01:00
|
|
|
row = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (row == -1) return;
|
2011-03-12 22:19:41 +01:00
|
|
|
|
|
|
|
data_[row].progress_string_ = progress;
|
|
|
|
|
|
|
|
// If it's the current item, update the display
|
|
|
|
if (ui_->song_list->currentIndex().row() == row) {
|
|
|
|
UpdateStack();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TrackSelectionDialog::FetchTagFinished(const Song& original_song,
|
2011-03-05 20:24:44 +01:00
|
|
|
const SongList& songs_guessed) {
|
|
|
|
// Find the item with this filename
|
|
|
|
int row = -1;
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < data_.count(); ++i) {
|
2011-04-28 14:27:53 +02:00
|
|
|
if (data_[i].original_song_.url() == original_song.url()) {
|
2011-03-05 20:24:44 +01:00
|
|
|
row = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (row == -1) return;
|
2011-03-05 20:24:44 +01:00
|
|
|
|
|
|
|
// Set the color back to black
|
|
|
|
ui_->song_list->item(row)->setForeground(palette().text());
|
|
|
|
|
|
|
|
// Add the results to the list
|
|
|
|
data_[row].pending_ = false;
|
|
|
|
data_[row].results_ = songs_guessed;
|
|
|
|
|
|
|
|
// If it's the current item, update the display
|
|
|
|
if (ui_->song_list->currentIndex().row() == row) {
|
|
|
|
UpdateStack();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TrackSelectionDialog::UpdateStack() {
|
|
|
|
const int row = ui_->song_list->currentRow();
|
2014-02-07 16:34:20 +01:00
|
|
|
if (row < 0 || row >= data_.count()) return;
|
2011-03-05 20:24:44 +01:00
|
|
|
|
|
|
|
const Data& data = data_[row];
|
|
|
|
|
|
|
|
if (data.pending_) {
|
|
|
|
ui_->stack->setCurrentWidget(ui_->loading_page);
|
2011-08-27 16:00:49 +02:00
|
|
|
ui_->progress->set_text(data.progress_string_ + "...");
|
2011-03-05 20:24:44 +01:00
|
|
|
return;
|
|
|
|
} else if (data.results_.isEmpty()) {
|
|
|
|
ui_->stack->setCurrentWidget(ui_->error_page);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ui_->stack->setCurrentWidget(ui_->results_page);
|
2011-01-21 00:42:28 +01:00
|
|
|
|
2011-01-24 18:03:36 +01:00
|
|
|
// Clear tree widget
|
2011-03-05 20:24:44 +01:00
|
|
|
ui_->results->clear();
|
|
|
|
|
|
|
|
// Put the original tags at the top
|
|
|
|
AddDivider(tr("Original tags"), ui_->results);
|
|
|
|
AddSong(data.original_song_, -1, ui_->results);
|
2011-01-24 18:03:36 +01:00
|
|
|
|
2011-01-21 00:42:28 +01:00
|
|
|
// Fill tree view with songs
|
2011-03-05 20:24:44 +01:00
|
|
|
AddDivider(tr("Suggested tags"), ui_->results);
|
|
|
|
|
2011-01-21 00:42:28 +01:00
|
|
|
int song_index = 0;
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const Song& song : data.results_) {
|
2011-03-05 20:24:44 +01:00
|
|
|
AddSong(song, song_index++, ui_->results);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the item that was selected last time
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < ui_->results->model()->rowCount(); ++i) {
|
2011-03-05 20:24:44 +01:00
|
|
|
const QModelIndex index = ui_->results->model()->index(i, 0);
|
|
|
|
const QVariant id = index.data(Qt::UserRole);
|
|
|
|
if (!id.isNull() && id.toInt() == data.selected_result_) {
|
|
|
|
ui_->results->setCurrentIndex(index);
|
|
|
|
break;
|
2011-01-21 00:42:28 +01:00
|
|
|
}
|
|
|
|
}
|
2011-03-05 20:24:44 +01:00
|
|
|
}
|
2011-01-21 00:42:28 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void TrackSelectionDialog::AddDivider(const QString& text,
|
|
|
|
QTreeWidget* parent) const {
|
2011-03-05 20:24:44 +01:00
|
|
|
QTreeWidgetItem* item = new QTreeWidgetItem(parent);
|
|
|
|
item->setFirstColumnSpanned(true);
|
|
|
|
item->setText(0, text);
|
|
|
|
item->setFlags(Qt::NoItemFlags);
|
|
|
|
item->setForeground(0, palette().color(QPalette::Disabled, QPalette::Text));
|
|
|
|
|
|
|
|
QFont bold_font(font());
|
|
|
|
bold_font.setBold(true);
|
|
|
|
item->setFont(0, bold_font);
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void TrackSelectionDialog::AddSong(const Song& song, int result_index,
|
|
|
|
QTreeWidget* parent) const {
|
2011-03-05 20:24:44 +01:00
|
|
|
QStringList values;
|
2012-01-07 22:51:02 +01:00
|
|
|
values << ((song.track() > 0) ? QString::number(song.track()) : QString())
|
|
|
|
<< ((song.year() > 0) ? QString::number(song.year()) : QString())
|
|
|
|
<< song.title() << song.artist() << song.album();
|
2011-03-05 20:24:44 +01:00
|
|
|
|
|
|
|
QTreeWidgetItem* item = new QTreeWidgetItem(parent, values);
|
|
|
|
item->setData(0, Qt::UserRole, result_index);
|
|
|
|
item->setData(0, Qt::TextAlignmentRole, Qt::AlignRight);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TrackSelectionDialog::ResultSelected() {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!ui_->results->currentItem()) return;
|
2011-03-05 20:24:44 +01:00
|
|
|
|
|
|
|
const int song_row = ui_->song_list->currentRow();
|
2014-02-07 16:34:20 +01:00
|
|
|
if (song_row == -1) return;
|
2011-03-05 20:24:44 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
const int result_index =
|
|
|
|
ui_->results->currentItem()->data(0, Qt::UserRole).toInt();
|
2011-03-05 20:24:44 +01:00
|
|
|
data_[song_row].selected_result_ = result_index;
|
2011-01-21 00:42:28 +01:00
|
|
|
}
|
|
|
|
|
2011-03-06 15:07:41 +01:00
|
|
|
void TrackSelectionDialog::SetLoading(const QString& message) {
|
|
|
|
const bool loading = !message.isEmpty();
|
|
|
|
|
|
|
|
ui_->button_box->setEnabled(!loading);
|
|
|
|
ui_->splitter->setEnabled(!loading);
|
2011-08-27 16:00:49 +02:00
|
|
|
ui_->loading_label->setVisible(loading);
|
|
|
|
ui_->loading_label->set_text(message);
|
2011-03-06 15:07:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TrackSelectionDialog::SaveData(const QList<Data>& data) {
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < data.count(); ++i) {
|
2011-03-06 15:07:41 +01:00
|
|
|
const Data& ref = data[i];
|
|
|
|
if (ref.pending_ || ref.results_.isEmpty() || ref.selected_result_ == -1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const Song& new_metadata = ref.results_[ref.selected_result_];
|
|
|
|
|
|
|
|
Song copy(ref.original_song_);
|
|
|
|
copy.set_title(new_metadata.title());
|
|
|
|
copy.set_artist(new_metadata.artist());
|
|
|
|
copy.set_album(new_metadata.album());
|
|
|
|
copy.set_track(new_metadata.track());
|
2012-03-04 14:51:08 +01:00
|
|
|
copy.set_year(new_metadata.year());
|
2011-03-06 15:07:41 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!TagReaderClient::Instance()->SaveFileBlocking(copy.url().toLocalFile(),
|
|
|
|
copy)) {
|
2015-12-20 18:25:45 +01:00
|
|
|
emit Error(tr("Failed to write new auto-tags to '%1'")
|
|
|
|
.arg(copy.url().toLocalFile()));
|
2012-11-10 08:27:49 +01:00
|
|
|
}
|
2011-03-06 15:07:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-21 00:42:28 +01:00
|
|
|
void TrackSelectionDialog::accept() {
|
2011-03-06 15:07:41 +01:00
|
|
|
if (save_on_close_) {
|
|
|
|
SetLoading(tr("Saving tracks") + "...");
|
|
|
|
|
|
|
|
// Save tags in the background
|
2014-02-07 16:34:20 +01:00
|
|
|
QFuture<void> future =
|
2015-12-20 18:25:45 +01:00
|
|
|
QtConcurrent::run(this, &TrackSelectionDialog::SaveData, data_);
|
2015-11-27 15:22:59 +01:00
|
|
|
NewClosure(future, this, SLOT(AcceptFinished()));
|
2011-03-06 15:07:41 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-01-21 00:42:28 +01:00
|
|
|
QDialog::accept();
|
2011-03-05 20:24:44 +01:00
|
|
|
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const Data& data : data_) {
|
2011-03-05 20:24:44 +01:00
|
|
|
if (data.pending_ || data.results_.isEmpty() || data.selected_result_ == -1)
|
|
|
|
continue;
|
|
|
|
|
2011-03-06 15:07:41 +01:00
|
|
|
const Song& new_metadata = data.results_[data.selected_result_];
|
|
|
|
|
|
|
|
emit SongChosen(data.original_song_, new_metadata);
|
2011-01-21 00:42:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-06 15:07:41 +01:00
|
|
|
void TrackSelectionDialog::AcceptFinished() {
|
|
|
|
SetLoading(QString());
|
|
|
|
QDialog::accept();
|
|
|
|
}
|
|
|
|
|
2011-03-05 20:24:44 +01:00
|
|
|
void TrackSelectionDialog::NextSong() {
|
|
|
|
int row = (ui_->song_list->currentRow() + 1) % ui_->song_list->count();
|
|
|
|
ui_->song_list->setCurrentRow(row);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TrackSelectionDialog::PreviousSong() {
|
2014-02-07 16:34:20 +01:00
|
|
|
int row = (ui_->song_list->currentRow() - 1 + ui_->song_list->count()) %
|
|
|
|
ui_->song_list->count();
|
2011-03-05 20:24:44 +01:00
|
|
|
ui_->song_list->setCurrentRow(row);
|
|
|
|
}
|