Render the moodbar data in the slider widget, and write .mood files alongside music files.

This commit is contained in:
David Sansome 2012-05-25 22:02:10 +01:00
parent f98d5e8fb6
commit e3cfd64764
10 changed files with 513 additions and 14 deletions

View File

@ -201,6 +201,7 @@ set(SOURCES
moodbar/moodbarcontroller.cpp
moodbar/moodbarloader.cpp
moodbar/moodbarpipeline.cpp
moodbar/moodbarproxystyle.cpp
musicbrainz/acoustidclient.cpp
musicbrainz/chromaprinter.cpp
@ -466,6 +467,7 @@ set(HEADERS
moodbar/moodbarcontroller.h
moodbar/moodbarloader.h
moodbar/moodbarpipeline.h
moodbar/moodbarproxystyle.h
musicbrainz/acoustidclient.h
musicbrainz/musicbrainzclient.h

View File

@ -17,8 +17,11 @@
#include "moodbarcontroller.h"
#include "moodbarloader.h"
#include "moodbarpipeline.h"
#include "core/application.h"
#include "core/closure.h"
#include "core/logging.h"
#include "core/player.h"
#include "playlist/playlistmanager.h"
MoodbarController::MoodbarController(Application* app, QObject* parent)
@ -27,10 +30,47 @@ MoodbarController::MoodbarController(Application* app, QObject* parent)
{
connect(app_->playlist_manager(),
SIGNAL(CurrentSongChanged(Song)), SLOT(CurrentSongChanged(Song)));
connect(app_->player(), SIGNAL(Stopped()), SLOT(PlaybackStopped()));
}
void MoodbarController::CurrentSongChanged(const Song& song) {
QByteArray data;
MoodbarPipeline* pipeline = NULL;
app_->moodbar_loader()->Load(song.url(), &data, &pipeline);
const MoodbarLoader::Result result =
app_->moodbar_loader()->Load(song.url(), &data, &pipeline);
switch (result) {
case MoodbarLoader::CannotLoad:
emit CurrentMoodbarDataChanged(QByteArray());
break;
case MoodbarLoader::Loaded:
emit CurrentMoodbarDataChanged(data);
break;
case MoodbarLoader::WillLoadAsync:
// Emit an empty array for now so the GUI reverts to a normal progress
// bar. Our slot will be called when the data is actually loaded.
emit CurrentMoodbarDataChanged(QByteArray());
NewClosure(pipeline, SIGNAL(Finished(bool)),
this, SLOT(AsyncLoadComplete(MoodbarPipeline*,QUrl)),
pipeline, song.url());
break;
}
}
void MoodbarController::PlaybackStopped() {
emit CurrentMoodbarDataChanged(QByteArray());
}
void MoodbarController::AsyncLoadComplete(MoodbarPipeline* pipeline,
const QUrl& url) {
// Is this song still playing?
PlaylistItemPtr current_item = app_->player()->GetCurrentItem();
if (current_item && current_item->Url() != url) {
return;
}
emit CurrentMoodbarDataChanged(pipeline->data());
}

View File

@ -21,16 +21,24 @@
#include <QObject>
class Application;
class MoodbarPipeline;
class Song;
class QUrl;
class MoodbarController : public QObject {
Q_OBJECT
public:
MoodbarController(Application* app, QObject* parent = 0);
signals:
void CurrentMoodbarDataChanged(const QByteArray& data);
private slots:
void CurrentSongChanged(const Song& song);
void PlaybackStopped();
void AsyncLoadComplete(MoodbarPipeline* pipeline, const QUrl& url);
private:
Application* app_;

View File

@ -29,7 +29,8 @@
MoodbarLoader::MoodbarLoader(QObject* parent)
: QObject(parent),
cache_(new QNetworkDiskCache(this))
cache_(new QNetworkDiskCache(this)),
save_alongside_originals_(true)
{
cache_->setCacheDirectory(Utilities::GetConfigPath(Utilities::Path_MoodbarCache));
cache_->setMaximumCacheSize(1024 * 1024); // 1MB - enough for 333 moodbars
@ -38,6 +39,19 @@ MoodbarLoader::MoodbarLoader(QObject* parent)
MoodbarLoader::~MoodbarLoader() {
}
QStringList MoodbarLoader::MoodFilenames(const QString& song_filename) {
const QFileInfo file_info(song_filename);
const QString dir_path(file_info.dir().path());
QStringList parts(file_info.fileName().split('.'));
parts.removeLast();
parts.append("mood");
const QString mood_filename(parts.join("."));
return QStringList() << dir_path + "/." + mood_filename
<< dir_path + "/" + mood_filename;
}
MoodbarLoader::Result MoodbarLoader::Load(
const QUrl& url, QByteArray* data, MoodbarPipeline** async_pipeline) {
if (url.scheme() != "file") {
@ -52,19 +66,8 @@ MoodbarLoader::Result MoodbarLoader::Load(
// Check if a mood file exists for this file already
const QString filename(url.toLocalFile());
const QFileInfo file_info(filename);
const QString dir_path(file_info.dir().path());
QStringList parts(file_info.fileName().split('.'));
parts.removeLast();
parts.append("mood");
const QString mood_filename(parts.join("."));
QStringList possible_mood_files;
possible_mood_files << dir_path + "/." + mood_filename
<< dir_path + "/" + mood_filename;
foreach (const QString& possible_mood_file, possible_mood_files) {
foreach (const QString& possible_mood_file, MoodFilenames(filename)) {
QFile f(possible_mood_file);
if (f.open(QIODevice::ReadOnly)) {
qLog(Info) << "Loading moodbar data from" << possible_mood_file;
@ -110,6 +113,17 @@ void MoodbarLoader::RequestFinished(MoodbarPipeline* request, const QUrl& url) {
QIODevice* cache_file = cache_->prepare(metadata);
cache_file->write(request->data());
cache_->insert(cache_file);
// Save the data alongside the original as well if we're configured to.
if (save_alongside_originals_) {
const QString mood_filename(MoodFilenames(url.toLocalFile())[0]);
QFile mood_file(mood_filename);
if (mood_file.open(QIODevice::WriteOnly)) {
mood_file.write(request->data());
} else {
qLog(Warning) << "Error opening mood file for writing" << mood_filename;
}
}
}
// Remove the request from the active list and delete it

View File

@ -50,11 +50,16 @@ public:
private slots:
void RequestFinished(MoodbarPipeline* request, const QUrl& filename);
private:
static QStringList MoodFilenames(const QString& song_filename);
private:
QNetworkDiskCache* cache_;
QMap<QUrl, MoodbarPipeline*> active_requests_;
bool save_alongside_originals_;
};
#endif // MOODBARLOADER_H

View File

@ -0,0 +1,304 @@
/* This file is part of Clementine.
Copyright 2012, 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/>.
*/
#include "moodbarproxystyle.h"
#include "core/logging.h"
#include <QEvent>
#include <QPainter>
#include <QSlider>
#include <QStyleOptionComplex>
#include <QStyleOptionSlider>
#include <QTimeLine>
const int MoodbarProxyStyle::kNumHues = 12;
MoodbarProxyStyle::MoodbarProxyStyle(QSlider* slider)
: QProxyStyle(slider->style()),
slider_(slider),
enabled_(true),
moodbar_style_(Style_SystemDefault),
state_(MoodbarOff),
fade_timeline_(new QTimeLine(1000, this)),
moodbar_colors_dirty_(true),
moodbar_pixmap_dirty_(true)
{
slider->setStyle(this);
slider->installEventFilter(this);
connect(fade_timeline_, SIGNAL(valueChanged(qreal)), SLOT(FaderValueChanged(qreal)));
}
void MoodbarProxyStyle::SetMoodbarData(const QByteArray& data) {
data_ = data;
moodbar_colors_dirty_ = true; // Redraw next time
NextState();
}
void MoodbarProxyStyle::SetMoodbarEnabled(bool enabled) {
enabled_ = enabled;
NextState();
}
void MoodbarProxyStyle::NextState() {
const bool visible = enabled_ && !data_.isEmpty();
if ((visible && (state_ == MoodbarOn || state_ == FadingToOn)) ||
(!visible && (state_ == MoodbarOff || state_ == FadingToOff))) {
return;
}
const QTimeLine::Direction direction =
visible ? QTimeLine::Forward : QTimeLine::Backward;
if (state_ == MoodbarOn || state_ == MoodbarOff) {
// Start the fade from the beginning.
fade_timeline_->setDirection(direction);
fade_timeline_->start();
fade_source_ = QPixmap();
fade_target_ = QPixmap();
} else {
// Stop an existing fade and start fading the other direction from the
// same place.
fade_timeline_->stop();
fade_timeline_->setDirection(direction);
fade_timeline_->resume();
}
state_ = visible ? FadingToOn : FadingToOff;
}
void MoodbarProxyStyle::FaderValueChanged(qreal value) {
slider_->update();
}
bool MoodbarProxyStyle::eventFilter(QObject* object, QEvent* event) {
if (object == slider_ && event->type() == QEvent::Resize) {
// The widget was resized, we've got to render a new pixmap.
moodbar_pixmap_dirty_ = true;
}
return QProxyStyle::eventFilter(object, event);
}
void MoodbarProxyStyle::drawComplexControl(
ComplexControl control, const QStyleOptionComplex* option,
QPainter* painter, const QWidget* widget) const {
if (control != CC_Slider || widget != slider_) {
QProxyStyle::drawComplexControl(control, option, painter, widget);
return;
}
const_cast<MoodbarProxyStyle*>(this)->Render(
control, qstyleoption_cast<const QStyleOptionSlider*>(option),
painter, widget);
}
void MoodbarProxyStyle::Render(
ComplexControl control, const QStyleOptionSlider* option,
QPainter* painter, const QWidget* widget) {
const qreal fade_value = fade_timeline_->currentValue();
// Have we finished fading?
if (state_ == FadingToOn && fade_value == 1.0) {
state_ = MoodbarOn;
} else if (state_ == FadingToOff && fade_value == 0.0) {
state_ = MoodbarOff;
}
switch (state_) {
case FadingToOn:
case FadingToOff:
// Update the cached pixmaps if necessary
if (fade_source_.isNull()) {
// Draw the normal slider into the fade source pixmap.
fade_source_ = QPixmap(option->rect.size());
fade_source_.fill(option->palette.color(QPalette::Background));
QPainter p(&fade_source_);
QStyleOptionSlider opt_copy(*option);
opt_copy.rect.moveTo(0, 0);
QProxyStyle::drawComplexControl(control, &opt_copy, &p, widget);
p.end();
}
if (fade_target_.isNull()) {
if (state_ == FadingToOn) {
EnsureMoodbarRendered();
}
fade_target_ = moodbar_pixmap_;
}
// Blend the pixmaps into each other
painter->drawPixmap(option->rect, fade_source_);
painter->setOpacity(fade_value);
painter->drawPixmap(option->rect, fade_target_);
painter->setOpacity(1.0);
break;
case MoodbarOff:
// It's a normal slider widget.
QProxyStyle::drawComplexControl(control, option, painter, widget);
break;
case MoodbarOn:
EnsureMoodbarRendered();
painter->drawPixmap(option->rect, moodbar_pixmap_);
break;
}
}
void MoodbarProxyStyle::EnsureMoodbarRendered() {
if (moodbar_colors_dirty_) {
moodbar_colors_ = MoodbarColors(data_, moodbar_style_, slider_->palette());
moodbar_colors_dirty_ = false;
moodbar_pixmap_dirty_ = true;
}
if (moodbar_pixmap_dirty_) {
moodbar_pixmap_ = MoodbarPixmap(moodbar_colors_, slider_->size());
moodbar_pixmap_dirty_ = false;
}
}
MoodbarProxyStyle::ColorList MoodbarProxyStyle::MoodbarColors(
const QByteArray& data, MoodbarStyle style, const QPalette& palette) {
const int samples = data.size() / 3;
// Set some parameters based on the moodbar style
StyleProperties properties;
switch(style) {
case Style_Angry: properties = StyleProperties(samples / 360 * 9, 45, -45, 200, 100); break;
case Style_Frozen: properties = StyleProperties(samples / 360 * 1, 140, 160, 50, 100); break;
case Style_Happy: properties = StyleProperties(samples / 360 * 2, 0, 359, 150, 250); break;
case Style_SystemDefault:
default: {
const QColor highlight_color(palette.color(QPalette::Highlight));
properties.threshold_ = samples / 360 * 3;
properties.range_start_ = (highlight_color.hsvHue() - 20 + 360) % 360;
properties.range_delta_ = 20;
properties.sat_ = highlight_color.hsvSaturation();
properties.val_ = highlight_color.value() / 2;
}
}
const unsigned char* data_p =
reinterpret_cast<const unsigned char*>(data.constData());
int hue_distribution[360];
int total = 0;
memset(hue_distribution, 0, sizeof(hue_distribution));
ColorList colors;
// Read the colors, keeping track of some histograms
for (int i=0; i<samples; ++i) {
QColor color;
color.setRed(int(*data_p++));
color.setGreen(int(*data_p++));
color.setBlue(int(*data_p++));
colors << color;
const int hue = qMax(0, color.hue());
if (hue_distribution[hue]++ == properties.threshold_) {
total ++;
}
}
// Remap the hue values to be between rangeStart and
// rangeStart + rangeDelta. Every time we see an input hue
// above the threshold, increment the output hue by
// (1/total) * rangeDelta.
for (int i=0, n=0 ; i<360; i++) {
hue_distribution[i] =
((hue_distribution[i] > properties.threshold_ ? n++ : n )
* properties.range_delta_ / total + properties.range_start_) % 360;
}
// Now huedist is a hue mapper: huedist[h] is the new hue value
// for a bar with hue h
for (ColorList::iterator it = colors.begin() ; it != colors.end() ; ++it) {
const int hue = qMax(0, it->hue());
*it = QColor::fromHsv(
qBound(0, hue_distribution[hue], 359),
qBound(0, it->saturation() * properties.sat_ / 100, 255),
qBound(0, it->value() * properties.val_ / 100, 255));
}
return colors;
}
QPixmap MoodbarProxyStyle::MoodbarPixmap(const ColorList& colors, const QSize& size) {
// Sample the colors and map them to screen pixels.
ColorList screen_colors;
for (int x=0; x<size.width(); ++x) {
int r = 0;
int g = 0;
int b = 0;
uint start = x * colors.size() / size.width();
uint end = (x + 1) * colors.size() / size.width();
if (start == end)
end = start + 1;
for (uint j=start; j<end; j++) {
r += colors[j].red();
g += colors[j].green();
b += colors[j].blue();
}
const uint n = end - start;
screen_colors.append(QColor(r/n, g/n, b/n));
}
QPixmap ret(size);
QPainter p(&ret);
for (int x=0; x<size.width(); x++) {
int h, s, v;
screen_colors[x].getHsv( &h, &s, &v );
for (int y=0; y<=size.height()/2; y++) {
float coeff = float(y) / float(size.height()/2);
float coeff2 = 1.0f - ((1.0f - coeff) * (1.0f - coeff));
coeff = 1.0f - (1.0f - coeff) / 2.0f;
coeff2 = 1.f - (1.f - coeff2) / 2.0f;
p.setPen(QColor::fromHsv(
h,
qBound(0, int(float(s) * coeff), 255),
qBound(0, int(255.f - (255.f - float(v)) * coeff2), 255)));
p.drawPoint(x, y);
p.drawPoint(x, size.height() - 1 - y);
}
}
p.end();
return ret;
}

View File

@ -0,0 +1,112 @@
/* This file is part of Clementine.
Copyright 2012, 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/>.
*/
#ifndef MOODBARPROXYSTYLE_H
#define MOODBARPROXYSTYLE_H
#include <QProxyStyle>
class QSlider;
class QStyleOptionSlider;
class QTimeLine;
class MoodbarProxyStyle : public QProxyStyle {
Q_OBJECT
public:
MoodbarProxyStyle(QSlider* slider);
enum MoodbarStyle {
Style_Angry,
Style_Frozen,
Style_Happy,
Style_SystemDefault
};
// QProxyStyle
void drawComplexControl(ComplexControl control, const QStyleOptionComplex* option,
QPainter* painter, const QWidget* widget) const;
// QObject
bool eventFilter(QObject* object, QEvent* event);
public slots:
// An empty byte array means there's no moodbar, so just show a normal slider.
void SetMoodbarData(const QByteArray& data);
// If the moodbar is disabled then a normal slider will always be shown.
void SetMoodbarEnabled(bool enabled);
private:
static const int kNumHues;
enum State {
MoodbarOn,
MoodbarOff,
FadingToOn,
FadingToOff
};
struct StyleProperties {
StyleProperties(int threshold = 0, int range_start = 0, int range_delta = 0,
int sat = 0, int val = 0)
: threshold_(threshold), range_start_(range_start), range_delta_(range_delta),
sat_(sat), val_(val) {}
int threshold_;
int range_start_;
int range_delta_;
int sat_;
int val_;
};
typedef QVector<QColor> ColorList;
private:
void NextState();
void Render(ComplexControl control, const QStyleOptionSlider* option,
QPainter* painter, const QWidget* widget);
void EnsureMoodbarRendered();
static ColorList MoodbarColors(const QByteArray& data, MoodbarStyle style,
const QPalette& palette);
static QPixmap MoodbarPixmap(const ColorList& colors, const QSize& size);
private slots:
void FaderValueChanged(qreal value);
private:
QSlider* slider_;
bool enabled_;
QByteArray data_;
MoodbarStyle moodbar_style_;
State state_;
QTimeLine* fade_timeline_;
QPixmap fade_source_;
QPixmap fade_target_;
bool moodbar_colors_dirty_;
bool moodbar_pixmap_dirty_;
ColorList moodbar_colors_;
QPixmap moodbar_pixmap_;
};
#endif // MOODBARPROXYSTYLE_H

View File

@ -55,6 +55,8 @@
#include "library/librarydirectorymodel.h"
#include "library/libraryfilterwidget.h"
#include "library/libraryviewcontainer.h"
#include "moodbar/moodbarcontroller.h"
#include "moodbar/moodbarproxystyle.h"
#include "musicbrainz/tagfetcher.h"
#include "playlist/playlistbackend.h"
#include "playlist/playlist.h"
@ -616,6 +618,10 @@ MainWindow::MainWindow(Application* app,
ui_->status_bar_stack->setCurrentWidget(ui_->playlist_summary_page);
connect(ui_->multi_loading_indicator, SIGNAL(TaskCountChange(int)), SLOT(TaskCountChanged(int)));
// Moodbar connections
connect(app_->moodbar_controller(), SIGNAL(CurrentMoodbarDataChanged(QByteArray)),
ui_->track_slider->moodbar_style(), SLOT(SetMoodbarData(QByteArray)));
// Now playing widget
qLog(Debug) << "Creating now playing widget";
ui_->now_playing->set_ideal_height(ui_->status_bar->sizeHint().height() +

View File

@ -18,6 +18,7 @@
#include "trackslider.h"
#include "ui_trackslider.h"
#include "core/utilities.h"
#include "moodbar/moodbarproxystyle.h"
#include <QSettings>
@ -26,11 +27,13 @@ const char* TrackSlider::kSettingsGroup = "MainWindow";
TrackSlider::TrackSlider(QWidget* parent)
: QWidget(parent),
ui_(new Ui_TrackSlider),
moodbar_style_(NULL),
setting_value_(false),
show_remaining_time_(true),
slider_maximum_value_(0)
{
ui_->setupUi(this);
moodbar_style_ = new MoodbarProxyStyle(ui_->slider);
QFont font("Courier");
ui_->elapsed->setFont(font);

View File

@ -22,6 +22,7 @@
class QLabel;
class MoodbarProxyStyle;
class Ui_TrackSlider;
class TrackSlider : public QWidget {
@ -37,6 +38,8 @@ class TrackSlider : public QWidget {
// QObject
bool event(QEvent *);
MoodbarProxyStyle* moodbar_style() const { return moodbar_style_; }
static const char* kSettingsGroup;
public slots:
@ -60,6 +63,8 @@ class TrackSlider : public QWidget {
private:
Ui_TrackSlider* ui_;
MoodbarProxyStyle* moodbar_style_;
bool setting_value_;
bool show_remaining_time_;
int slider_maximum_value_; //we cache it to avoid unnecessary updates