2010-10-02 15:37:10 +02:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-10-02 15:37:10 +02:00
|
|
|
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "songinfobase.h"
|
2010-12-30 18:35:10 +01:00
|
|
|
#include "core/network.h"
|
2010-10-02 15:37:10 +02:00
|
|
|
|
2010-10-10 18:09:20 +02:00
|
|
|
#include <QFile>
|
|
|
|
#include <QScrollArea>
|
2010-10-11 21:49:12 +02:00
|
|
|
#include <QSettings>
|
2010-10-10 18:09:20 +02:00
|
|
|
#include <QSpacerItem>
|
2010-10-10 22:58:49 +02:00
|
|
|
#include <QTimer>
|
2010-10-10 18:09:20 +02:00
|
|
|
#include <QVBoxLayout>
|
|
|
|
|
2010-10-11 21:49:12 +02:00
|
|
|
const char* SongInfoBase::kSettingsGroup = "SongInfo";
|
|
|
|
|
2010-10-16 19:20:54 +02:00
|
|
|
SongInfoBase::SongInfoBase(QWidget* parent)
|
2010-10-02 15:37:10 +02:00
|
|
|
: QWidget(parent),
|
2010-12-30 18:35:10 +01:00
|
|
|
network_(new NetworkAccessManager(this)),
|
2010-10-10 18:09:20 +02:00
|
|
|
fetcher_(new SongInfoFetcher(this)),
|
|
|
|
current_request_id_(-1),
|
|
|
|
scroll_area_(new QScrollArea),
|
|
|
|
container_(new QVBoxLayout),
|
|
|
|
section_container_(NULL),
|
|
|
|
fader_(new WidgetFadeHelper(this, 1000)),
|
2010-10-02 15:37:10 +02:00
|
|
|
dirty_(false)
|
|
|
|
{
|
2010-10-10 18:09:20 +02:00
|
|
|
// Add the top-level scroll area
|
|
|
|
setLayout(new QVBoxLayout);
|
|
|
|
layout()->setContentsMargins(0, 0, 0, 0);
|
|
|
|
layout()->addWidget(scroll_area_);
|
|
|
|
|
|
|
|
// Add a container widget to the scroll area
|
|
|
|
QWidget* container_widget = new QWidget;
|
|
|
|
container_widget->setLayout(container_);
|
2010-10-17 13:01:46 +02:00
|
|
|
container_widget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
|
2010-10-10 18:09:20 +02:00
|
|
|
container_widget->setBackgroundRole(QPalette::Base);
|
|
|
|
container_->setSizeConstraint(QLayout::SetMinAndMaxSize);
|
|
|
|
container_->setContentsMargins(0, 0, 0, 0);
|
|
|
|
container_->setSpacing(6);
|
|
|
|
scroll_area_->setWidget(container_widget);
|
|
|
|
scroll_area_->setWidgetResizable(true);
|
|
|
|
|
|
|
|
// Add a spacer to the bottom of the container
|
2010-10-17 13:01:46 +02:00
|
|
|
container_->addStretch();
|
2010-10-10 18:09:20 +02:00
|
|
|
|
|
|
|
// Set stylesheet
|
|
|
|
QFile stylesheet(":/songinfo.css");
|
|
|
|
stylesheet.open(QIODevice::ReadOnly);
|
|
|
|
setStyleSheet(QString::fromAscii(stylesheet.readAll()));
|
|
|
|
|
|
|
|
connect(fetcher_, SIGNAL(ResultReady(int,SongInfoFetcher::Result)),
|
|
|
|
SLOT(ResultReady(int,SongInfoFetcher::Result)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SongInfoBase::Clear() {
|
2010-10-10 22:53:49 +02:00
|
|
|
fader_->StartFade();
|
2010-10-10 18:09:20 +02:00
|
|
|
|
|
|
|
qDeleteAll(widgets_);
|
|
|
|
widgets_.clear();
|
|
|
|
delete section_container_;
|
|
|
|
sections_.clear();
|
|
|
|
|
|
|
|
// Container for collapsable sections goes below
|
|
|
|
section_container_ = new QWidget;
|
|
|
|
section_container_->setLayout(new QVBoxLayout);
|
|
|
|
section_container_->layout()->setContentsMargins(0, 0, 0, 0);
|
|
|
|
section_container_->layout()->setSpacing(1);
|
|
|
|
section_container_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);
|
|
|
|
container_->insertWidget(0, section_container_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SongInfoBase::AddSection(CollapsibleInfoPane* section) {
|
|
|
|
int index = 0;
|
|
|
|
for ( ; index<sections_.count() ; ++index) {
|
|
|
|
if (section->data() < sections_[index]->data())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-10-16 16:45:23 +02:00
|
|
|
ConnectWidget(section->data().contents_);
|
|
|
|
|
2010-10-10 18:09:20 +02:00
|
|
|
sections_.insert(index, section);
|
|
|
|
qobject_cast<QVBoxLayout*>(section_container_->layout())->insertWidget(index, section);
|
|
|
|
section->show();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SongInfoBase::AddWidget(QWidget* widget) {
|
2010-10-16 16:45:23 +02:00
|
|
|
ConnectWidget(widget);
|
|
|
|
|
2010-10-10 18:09:20 +02:00
|
|
|
container_->insertWidget(container_->count() - 2, widget);
|
|
|
|
widgets_ << widget;
|
2010-10-02 15:37:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SongInfoBase::SongChanged(const Song& metadata) {
|
|
|
|
if (isVisible()) {
|
2010-10-09 14:39:49 +02:00
|
|
|
MaybeUpdate(metadata);
|
2010-10-02 15:37:10 +02:00
|
|
|
dirty_ = false;
|
|
|
|
} else {
|
|
|
|
queued_metadata_ = metadata;
|
|
|
|
dirty_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SongInfoBase::SongFinished() {
|
|
|
|
dirty_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SongInfoBase::showEvent(QShowEvent* e) {
|
|
|
|
if (dirty_) {
|
2010-10-09 14:39:49 +02:00
|
|
|
MaybeUpdate(queued_metadata_);
|
2010-10-02 15:37:10 +02:00
|
|
|
dirty_ = false;
|
|
|
|
}
|
|
|
|
QWidget::showEvent(e);
|
|
|
|
}
|
2010-10-09 14:39:49 +02:00
|
|
|
|
|
|
|
void SongInfoBase::MaybeUpdate(const Song& metadata) {
|
|
|
|
if (old_metadata_.is_valid()) {
|
|
|
|
if (!NeedsUpdate(old_metadata_, metadata)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Update(metadata);
|
|
|
|
old_metadata_ = metadata;
|
|
|
|
}
|
2010-10-10 18:09:20 +02:00
|
|
|
|
|
|
|
void SongInfoBase::Update(const Song& metadata) {
|
|
|
|
current_request_id_ = fetcher_->FetchInfo(metadata);
|
2010-10-10 22:58:49 +02:00
|
|
|
|
|
|
|
// Do this after the new pane has been shown otherwise it'll just grab a
|
|
|
|
// black rectangle.
|
|
|
|
QTimer::singleShot(0, fader_, SLOT(StartBlur()));
|
2010-10-10 18:09:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SongInfoBase::ResultReady(int id, const SongInfoFetcher::Result& result) {
|
|
|
|
foreach (const CollapsibleInfoPane::Data& data, result.info_) {
|
|
|
|
delete data.contents_;
|
|
|
|
}
|
|
|
|
}
|
2010-10-11 21:49:12 +02:00
|
|
|
|
|
|
|
void SongInfoBase::CollapseSections() {
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(kSettingsGroup);
|
|
|
|
|
|
|
|
// Sections are already sorted by type and relevance, so the algorithm we use
|
|
|
|
// to determine which ones to show by default is:
|
|
|
|
// * In the absense of any user preference, show the first (highest
|
|
|
|
// relevance section of each type and hide the rest)
|
|
|
|
// * If one or more sections in a type have been explicitly hidden/shown
|
|
|
|
// by the user before then hide all sections in that type and show only
|
2010-10-11 23:42:31 +02:00
|
|
|
// the ones that are explicitly shown.
|
2010-10-11 21:49:12 +02:00
|
|
|
|
|
|
|
QMap<CollapsibleInfoPane::Data::Type, CollapsibleInfoPane*> types_;
|
|
|
|
QSet<CollapsibleInfoPane::Data::Type> has_user_preference_;
|
|
|
|
foreach (CollapsibleInfoPane* pane, sections_) {
|
|
|
|
const CollapsibleInfoPane::Data::Type type = pane->data().type_;
|
|
|
|
types_.insertMulti(type, pane);
|
|
|
|
|
|
|
|
QVariant preference = s.value(pane->data().id_);
|
|
|
|
if (preference.isValid()) {
|
|
|
|
has_user_preference_.insert(type);
|
|
|
|
if (preference.toBool()) {
|
|
|
|
pane->Expand();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (CollapsibleInfoPane::Data::Type type, types_.keys()) {
|
2010-10-11 23:42:31 +02:00
|
|
|
if (!has_user_preference_.contains(type)) {
|
2010-10-11 21:49:12 +02:00
|
|
|
// Expand the first one
|
|
|
|
types_.values(type).last()->Expand();
|
|
|
|
}
|
|
|
|
}
|
2010-10-11 23:42:31 +02:00
|
|
|
|
|
|
|
foreach (CollapsibleInfoPane* pane, sections_) {
|
|
|
|
connect(pane, SIGNAL(Toggled(bool)), SLOT(SectionToggled(bool)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SongInfoBase::SectionToggled(bool value) {
|
|
|
|
CollapsibleInfoPane* pane = qobject_cast<CollapsibleInfoPane*>(sender());
|
|
|
|
if (!pane || !sections_.contains(pane))
|
|
|
|
return;
|
|
|
|
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(kSettingsGroup);
|
|
|
|
s.setValue(pane->data().id_, value);
|
2010-10-11 21:49:12 +02:00
|
|
|
}
|
2010-10-16 15:56:49 +02:00
|
|
|
|
|
|
|
void SongInfoBase::ReloadSettings() {
|
|
|
|
foreach (CollapsibleInfoPane* pane, sections_) {
|
|
|
|
QWidget* contents = pane->data().contents_;
|
|
|
|
if (!contents)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
QMetaObject::invokeMethod(contents, "ReloadSettings");
|
|
|
|
}
|
2010-12-19 14:58:53 +01:00
|
|
|
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(kSettingsGroup);
|
|
|
|
fetcher_->set_timeout(s.value("timeout", SongInfoFetcher::kDefaultTimeoutDuration).toInt());
|
2010-10-16 15:56:49 +02:00
|
|
|
}
|
2010-10-16 16:45:23 +02:00
|
|
|
|
|
|
|
void SongInfoBase::ConnectWidget(QWidget* widget) {
|
2010-12-04 17:19:30 +01:00
|
|
|
const QMetaObject* m = widget->metaObject();
|
|
|
|
|
|
|
|
if (m->indexOfSignal("ShowSettingsDialog()") != -1) {
|
2010-10-16 16:45:23 +02:00
|
|
|
connect(widget, SIGNAL(ShowSettingsDialog()), SIGNAL(ShowSettingsDialog()));
|
|
|
|
}
|
2010-10-16 17:22:14 +02:00
|
|
|
|
2011-01-10 23:26:13 +01:00
|
|
|
if (m->indexOfSignal("AddToPlaylist(QMimeData*)") != -1) {
|
|
|
|
connect(widget, SIGNAL(AddToPlaylist(QMimeData*)), SIGNAL(AddToPlaylist(QMimeData*)));
|
2010-12-04 17:19:30 +01:00
|
|
|
}
|
2010-10-16 16:45:23 +02:00
|
|
|
}
|