2011-08-28 22:33:59 +02: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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "searchprovider.h"
|
|
|
|
#include "core/boundfuturewatcher.h"
|
|
|
|
|
2011-08-29 00:59:18 +02:00
|
|
|
#include <QPainter>
|
2011-08-28 22:33:59 +02:00
|
|
|
#include <QtConcurrentRun>
|
|
|
|
|
|
|
|
const int SearchProvider::kArtHeight = 32;
|
|
|
|
|
|
|
|
|
2011-08-29 01:13:36 +02:00
|
|
|
SearchProvider::SearchProvider(QObject* parent)
|
2011-10-20 00:01:10 +02:00
|
|
|
: QObject(parent),
|
|
|
|
hints_(0)
|
2011-08-28 22:33:59 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-10-20 00:01:10 +02:00
|
|
|
void SearchProvider::Init(const QString& name, const QString& id,
|
|
|
|
const QIcon& icon, Hints hints) {
|
2011-08-29 01:13:36 +02:00
|
|
|
name_ = name;
|
2011-09-17 18:42:14 +02:00
|
|
|
id_ = id;
|
2011-08-29 01:13:36 +02:00
|
|
|
icon_ = icon;
|
2011-10-20 00:01:10 +02:00
|
|
|
hints_ = hints;
|
2011-08-29 01:13:36 +02:00
|
|
|
}
|
|
|
|
|
2011-08-28 22:33:59 +02:00
|
|
|
QStringList SearchProvider::TokenizeQuery(const QString& query) {
|
|
|
|
QStringList tokens(query.split(QRegExp("\\s+")));
|
|
|
|
|
|
|
|
for (QStringList::iterator it = tokens.begin() ; it != tokens.end() ; ++it) {
|
|
|
|
(*it).remove('(');
|
|
|
|
(*it).remove(')');
|
|
|
|
(*it).remove('"');
|
|
|
|
|
|
|
|
const int colon = (*it).indexOf(":");
|
|
|
|
if (colon != -1) {
|
|
|
|
(*it).remove(0, colon + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tokens;
|
|
|
|
}
|
|
|
|
|
2011-10-16 22:57:53 +02:00
|
|
|
globalsearch::MatchQuality SearchProvider::MatchQuality(
|
2011-08-29 00:24:54 +02:00
|
|
|
const QStringList& tokens, const QString& string) {
|
2011-10-16 22:57:53 +02:00
|
|
|
globalsearch::MatchQuality ret = globalsearch::Quality_None;
|
2011-08-29 00:24:54 +02:00
|
|
|
|
2011-08-28 22:33:59 +02:00
|
|
|
foreach (const QString& token, tokens) {
|
2011-08-29 00:24:54 +02:00
|
|
|
const int index = string.indexOf(token, 0, Qt::CaseInsensitive);
|
|
|
|
if (index == 0) {
|
2011-10-16 22:57:53 +02:00
|
|
|
return globalsearch::Quality_AtStart;
|
2011-08-29 00:24:54 +02:00
|
|
|
} else if (index != -1) {
|
2011-10-16 22:57:53 +02:00
|
|
|
ret = globalsearch::Quality_Middle;
|
2011-08-28 22:33:59 +02:00
|
|
|
}
|
|
|
|
}
|
2011-08-29 00:24:54 +02:00
|
|
|
|
2011-08-28 22:33:59 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-08-29 01:13:36 +02:00
|
|
|
BlockingSearchProvider::BlockingSearchProvider(QObject* parent)
|
|
|
|
: SearchProvider(parent) {
|
2011-08-28 22:33:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void BlockingSearchProvider::SearchAsync(int id, const QString& query) {
|
|
|
|
QFuture<ResultList> future = QtConcurrent::run(
|
|
|
|
this, &BlockingSearchProvider::Search, id, query);
|
|
|
|
|
|
|
|
BoundFutureWatcher<ResultList, int>* watcher =
|
|
|
|
new BoundFutureWatcher<ResultList, int>(id);
|
|
|
|
watcher->setFuture(future);
|
|
|
|
connect(watcher, SIGNAL(finished()), SLOT(BlockingSearchFinished()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void BlockingSearchProvider::BlockingSearchFinished() {
|
|
|
|
BoundFutureWatcher<ResultList, int>* watcher =
|
|
|
|
static_cast<BoundFutureWatcher<ResultList, int>*>(sender());
|
|
|
|
watcher->deleteLater();
|
|
|
|
|
|
|
|
const int id = watcher->data();
|
|
|
|
emit ResultsAvailable(id, watcher->result());
|
|
|
|
emit SearchFinished(id);
|
|
|
|
}
|
2011-08-29 00:59:18 +02:00
|
|
|
|
|
|
|
QImage SearchProvider::ScaleAndPad(const QImage& image) {
|
|
|
|
if (image.isNull())
|
|
|
|
return QImage();
|
|
|
|
|
|
|
|
const QSize target_size = QSize(kArtHeight, kArtHeight);
|
|
|
|
|
|
|
|
if (image.size() == target_size)
|
|
|
|
return image;
|
|
|
|
|
|
|
|
// Scale the image down
|
|
|
|
QImage copy;
|
|
|
|
copy = image.scaled(target_size, Qt::KeepAspectRatio, Qt::SmoothTransformation);
|
|
|
|
|
|
|
|
// Pad the image to kHeight x kHeight
|
|
|
|
if (copy.size() == target_size)
|
|
|
|
return copy;
|
|
|
|
|
|
|
|
QImage padded_image(kArtHeight, kArtHeight, QImage::Format_ARGB32);
|
|
|
|
padded_image.fill(0);
|
|
|
|
|
|
|
|
QPainter p(&padded_image);
|
|
|
|
p.drawImage((kArtHeight - copy.width()) / 2, (kArtHeight - copy.height()) / 2,
|
|
|
|
copy);
|
|
|
|
p.end();
|
|
|
|
|
|
|
|
return padded_image;
|
|
|
|
}
|
2011-09-18 01:06:07 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
bool SortSongsCompare(const Song& left, const Song& right) {
|
|
|
|
if (left.disc() < right.disc())
|
|
|
|
return true;
|
|
|
|
if (left.disc() > right.disc())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return left.track() < right.track();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SearchProvider::SortSongs(SongList* list) {
|
|
|
|
qStableSort(list->begin(), list->end(), SortSongsCompare);
|
|
|
|
}
|
2011-11-04 23:31:19 +01:00
|
|
|
|
|
|
|
void SearchProvider::LoadArtAsync(int id, const Result& result) {
|
|
|
|
emit ArtLoaded(id, QImage());
|
|
|
|
}
|