2012-06-04 19:18:37 +02:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2012, David Sansome <me@davidsansome.com>
|
2014-02-06 14:48:00 +01:00
|
|
|
|
2012-06-04 19:18:37 +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.
|
2014-02-06 14:48:00 +01:00
|
|
|
|
2012-06-04 19:18:37 +02:00
|
|
|
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.
|
2014-02-06 14:48:00 +01:00
|
|
|
|
2012-06-04 19:18:37 +02:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2014-02-06 14:48:00 +01:00
|
|
|
#include "globalsearchview.h"
|
|
|
|
|
|
|
|
#include <QMenu>
|
|
|
|
#include <QSortFilterProxyModel>
|
|
|
|
#include <QStandardItem>
|
|
|
|
#include <QTimer>
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
|
2012-06-04 19:18:37 +02:00
|
|
|
#include "globalsearch.h"
|
2012-06-10 18:15:32 +02:00
|
|
|
#include "globalsearchitemdelegate.h"
|
2012-06-10 22:11:55 +02:00
|
|
|
#include "globalsearchmodel.h"
|
2012-06-04 19:18:37 +02:00
|
|
|
#include "globalsearchsortmodel.h"
|
|
|
|
#include "searchprovider.h"
|
2012-06-10 20:00:51 +02:00
|
|
|
#include "searchproviderstatuswidget.h"
|
2012-06-11 00:05:30 +02:00
|
|
|
#include "suggestionwidget.h"
|
2012-06-04 19:18:37 +02:00
|
|
|
#include "ui_globalsearchview.h"
|
|
|
|
#include "core/application.h"
|
|
|
|
#include "core/logging.h"
|
|
|
|
#include "core/mimedata.h"
|
2012-06-11 00:05:30 +02:00
|
|
|
#include "core/timeconstants.h"
|
2012-06-17 17:20:40 +02:00
|
|
|
#include "library/libraryfilterwidget.h"
|
2012-06-04 19:18:37 +02:00
|
|
|
#include "library/librarymodel.h"
|
2012-06-17 17:20:40 +02:00
|
|
|
#include "library/groupbydialog.h"
|
2012-06-04 19:18:37 +02:00
|
|
|
|
2014-02-06 14:48:00 +01:00
|
|
|
using std::placeholders::_1;
|
|
|
|
using std::placeholders::_2;
|
2012-06-04 19:18:37 +02:00
|
|
|
|
|
|
|
const int GlobalSearchView::kSwapModelsTimeoutMsec = 250;
|
2012-06-11 00:05:30 +02:00
|
|
|
const int GlobalSearchView::kMaxSuggestions = 10;
|
|
|
|
const int GlobalSearchView::kUpdateSuggestionsTimeoutMsec = 60 * kMsecPerSec;
|
2012-06-04 19:18:37 +02:00
|
|
|
|
|
|
|
GlobalSearchView::GlobalSearchView(Application* app, QWidget* parent)
|
2014-02-07 16:34:20 +01:00
|
|
|
: QWidget(parent),
|
|
|
|
app_(app),
|
|
|
|
engine_(app_->global_search()),
|
|
|
|
ui_(new Ui_GlobalSearchView),
|
|
|
|
context_menu_(nullptr),
|
|
|
|
last_search_id_(0),
|
|
|
|
front_model_(new GlobalSearchModel(engine_, this)),
|
|
|
|
back_model_(new GlobalSearchModel(engine_, this)),
|
|
|
|
current_model_(front_model_),
|
|
|
|
front_proxy_(new GlobalSearchSortModel(this)),
|
|
|
|
back_proxy_(new GlobalSearchSortModel(this)),
|
|
|
|
current_proxy_(front_proxy_),
|
|
|
|
swap_models_timer_(new QTimer(this)),
|
|
|
|
update_suggestions_timer_(new QTimer(this)),
|
|
|
|
search_icon_(IconLoader::Load("search")),
|
|
|
|
warning_icon_(IconLoader::Load("dialog-warning")),
|
|
|
|
show_providers_(true),
|
2014-10-05 15:06:56 +02:00
|
|
|
show_suggestions_(true) {
|
2012-06-04 19:18:37 +02:00
|
|
|
ui_->setupUi(this);
|
|
|
|
|
2012-06-24 22:08:33 +02:00
|
|
|
front_model_->set_proxy(front_proxy_);
|
|
|
|
back_model_->set_proxy(back_proxy_);
|
|
|
|
|
2012-06-10 22:56:17 +02:00
|
|
|
ui_->search->installEventFilter(this);
|
2012-06-16 22:17:13 +02:00
|
|
|
ui_->results_stack->installEventFilter(this);
|
2012-06-10 22:56:17 +02:00
|
|
|
|
2012-06-17 17:20:40 +02:00
|
|
|
ui_->settings->setIcon(IconLoader::Load("configure"));
|
|
|
|
|
2012-06-10 20:00:51 +02:00
|
|
|
// Must be a queued connection to ensure the GlobalSearch handles it first.
|
2014-02-07 16:34:20 +01:00
|
|
|
connect(app_, SIGNAL(SettingsChanged()), SLOT(ReloadSettings()),
|
|
|
|
Qt::QueuedConnection);
|
2012-06-10 20:00:51 +02:00
|
|
|
|
2012-06-04 19:18:37 +02:00
|
|
|
connect(ui_->search, SIGNAL(textChanged(QString)), SLOT(TextEdited(QString)));
|
2014-02-07 16:34:20 +01:00
|
|
|
connect(ui_->results, SIGNAL(AddToPlaylistSignal(QMimeData*)),
|
|
|
|
SIGNAL(AddToPlaylist(QMimeData*)));
|
|
|
|
connect(ui_->results, SIGNAL(FocusOnFilterSignal(QKeyEvent*)),
|
|
|
|
SLOT(FocusOnFilter(QKeyEvent*)));
|
2012-06-04 19:18:37 +02:00
|
|
|
|
2012-06-10 20:00:51 +02:00
|
|
|
// Set the appearance of the results list
|
2012-06-10 18:15:32 +02:00
|
|
|
ui_->results->setItemDelegate(new GlobalSearchItemDelegate(this));
|
2012-06-10 20:00:51 +02:00
|
|
|
ui_->results->setAttribute(Qt::WA_MacShowFocusRect, false);
|
|
|
|
ui_->results->setStyleSheet("QTreeView::item{padding-top:1px;}");
|
|
|
|
|
|
|
|
// Show the help page initially
|
2012-06-16 22:17:13 +02:00
|
|
|
ui_->results_stack->setCurrentWidget(ui_->help_page);
|
2012-06-10 20:00:51 +02:00
|
|
|
ui_->help_frame->setBackgroundRole(QPalette::Base);
|
|
|
|
QVBoxLayout* enabled_layout = new QVBoxLayout(ui_->enabled_list);
|
|
|
|
QVBoxLayout* disabled_layout = new QVBoxLayout(ui_->disabled_list);
|
2012-06-11 00:05:30 +02:00
|
|
|
QVBoxLayout* suggestions_layout = new QVBoxLayout(ui_->suggestions_list);
|
2012-06-10 20:00:51 +02:00
|
|
|
enabled_layout->setContentsMargins(16, 0, 16, 6);
|
2012-06-11 00:05:30 +02:00
|
|
|
disabled_layout->setContentsMargins(16, 0, 16, 32);
|
|
|
|
suggestions_layout->setContentsMargins(16, 0, 16, 6);
|
2012-06-04 19:18:37 +02:00
|
|
|
|
2012-06-10 21:14:16 +02:00
|
|
|
// Set the colour of the help text to the disabled text colour
|
|
|
|
QPalette help_palette = ui_->help_text->palette();
|
2014-02-07 16:34:20 +01:00
|
|
|
const QColor help_color =
|
|
|
|
help_palette.color(QPalette::Disabled, QPalette::Text);
|
2012-06-10 21:14:16 +02:00
|
|
|
help_palette.setColor(QPalette::Normal, QPalette::Text, help_color);
|
|
|
|
help_palette.setColor(QPalette::Inactive, QPalette::Text, help_color);
|
|
|
|
ui_->help_text->setPalette(help_palette);
|
|
|
|
|
2012-06-11 00:05:30 +02:00
|
|
|
// Create suggestion widgets
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < kMaxSuggestions; ++i) {
|
2012-06-11 00:05:30 +02:00
|
|
|
SuggestionWidget* widget = new SuggestionWidget(search_icon_);
|
2014-02-07 16:34:20 +01:00
|
|
|
connect(widget, SIGNAL(SuggestionClicked(QString)),
|
|
|
|
SLOT(StartSearch(QString)));
|
2012-06-11 00:05:30 +02:00
|
|
|
suggestions_layout->addWidget(widget);
|
|
|
|
suggestion_widgets_ << widget;
|
|
|
|
}
|
|
|
|
|
2012-06-10 21:14:16 +02:00
|
|
|
// Make it bold
|
|
|
|
QFont help_font = ui_->help_text->font();
|
|
|
|
help_font.setBold(true);
|
|
|
|
ui_->help_text->setFont(help_font);
|
|
|
|
|
2012-06-04 19:18:37 +02:00
|
|
|
// Set up the sorting proxy model
|
|
|
|
front_proxy_->setSourceModel(front_model_);
|
|
|
|
front_proxy_->setDynamicSortFilter(true);
|
|
|
|
front_proxy_->sort(0);
|
|
|
|
|
|
|
|
back_proxy_->setSourceModel(back_model_);
|
|
|
|
back_proxy_->setDynamicSortFilter(true);
|
|
|
|
back_proxy_->sort(0);
|
|
|
|
|
|
|
|
swap_models_timer_->setSingleShot(true);
|
|
|
|
swap_models_timer_->setInterval(kSwapModelsTimeoutMsec);
|
|
|
|
connect(swap_models_timer_, SIGNAL(timeout()), SLOT(SwapModels()));
|
|
|
|
|
2012-06-11 00:05:30 +02:00
|
|
|
update_suggestions_timer_->setInterval(kUpdateSuggestionsTimeoutMsec);
|
2014-02-07 16:34:20 +01:00
|
|
|
connect(update_suggestions_timer_, SIGNAL(timeout()),
|
|
|
|
SLOT(UpdateSuggestions()));
|
2012-06-11 00:05:30 +02:00
|
|
|
|
2012-06-17 17:20:40 +02:00
|
|
|
// Add actions to the settings menu
|
|
|
|
group_by_actions_ = LibraryFilterWidget::CreateGroupByActions(this);
|
|
|
|
QMenu* settings_menu = new QMenu(this);
|
|
|
|
settings_menu->addActions(group_by_actions_->actions());
|
|
|
|
settings_menu->addSeparator();
|
|
|
|
settings_menu->addAction(IconLoader::Load("configure"),
|
2014-02-07 16:34:20 +01:00
|
|
|
tr("Configure global search..."), this,
|
|
|
|
SLOT(OpenSettingsDialog()));
|
2012-06-17 17:20:40 +02:00
|
|
|
ui_->settings->setMenu(settings_menu);
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
connect(group_by_actions_, SIGNAL(triggered(QAction*)),
|
|
|
|
SLOT(GroupByClicked(QAction*)));
|
2012-06-17 17:20:40 +02:00
|
|
|
|
2012-06-04 19:18:37 +02:00
|
|
|
// These have to be queued connections because they may get emitted before
|
|
|
|
// our call to Search() (or whatever) returns and we add the ID to the map.
|
2014-02-07 16:34:20 +01:00
|
|
|
connect(engine_, SIGNAL(ResultsAvailable(int, SearchProvider::ResultList)),
|
|
|
|
SLOT(AddResults(int, SearchProvider::ResultList)),
|
2012-06-04 19:18:37 +02:00
|
|
|
Qt::QueuedConnection);
|
2014-02-07 16:34:20 +01:00
|
|
|
connect(engine_, SIGNAL(ArtLoaded(int, QPixmap)),
|
|
|
|
SLOT(ArtLoaded(int, QPixmap)), Qt::QueuedConnection);
|
2012-06-04 19:18:37 +02:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
GlobalSearchView::~GlobalSearchView() { delete ui_; }
|
2012-06-04 19:18:37 +02:00
|
|
|
|
2012-06-10 20:00:51 +02:00
|
|
|
namespace {
|
2014-02-07 16:34:20 +01:00
|
|
|
bool CompareProvider(const QStringList& provider_order, SearchProvider* left,
|
|
|
|
SearchProvider* right) {
|
|
|
|
const int left_index = provider_order.indexOf(left->id());
|
|
|
|
const int right_index = provider_order.indexOf(right->id());
|
|
|
|
if (left_index == -1 && right_index == -1) {
|
|
|
|
// None are in our provider list: compare name instead
|
|
|
|
return left->name() < right->name();
|
|
|
|
} else if (left_index == -1) {
|
|
|
|
// Left provider not in provider list
|
|
|
|
return false;
|
|
|
|
} else if (right_index == -1) {
|
|
|
|
// Right provider not in provider list
|
|
|
|
return true;
|
2012-06-10 20:00:51 +02:00
|
|
|
}
|
2014-02-07 16:34:20 +01:00
|
|
|
return left_index < right_index;
|
|
|
|
}
|
2012-06-10 20:00:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalSearchView::ReloadSettings() {
|
2014-03-30 08:08:30 +02:00
|
|
|
const bool old_show_suggestions = show_suggestions_;
|
|
|
|
|
2012-06-10 23:08:00 +02:00
|
|
|
QSettings s;
|
2014-02-06 14:48:00 +01:00
|
|
|
|
2012-06-11 17:26:33 +02:00
|
|
|
// Library settings
|
2012-06-10 23:08:00 +02:00
|
|
|
s.beginGroup(LibraryView::kSettingsGroup);
|
|
|
|
const bool pretty = s.value("pretty_covers", true).toBool();
|
2012-06-10 22:11:55 +02:00
|
|
|
front_model_->set_use_pretty_covers(pretty);
|
|
|
|
back_model_->set_use_pretty_covers(pretty);
|
2012-06-11 17:26:33 +02:00
|
|
|
s.endGroup();
|
2014-02-06 14:48:00 +01:00
|
|
|
|
2012-06-11 17:26:33 +02:00
|
|
|
// Global search settings
|
|
|
|
s.beginGroup(GlobalSearch::kSettingsGroup);
|
|
|
|
const QStringList provider_order =
|
|
|
|
s.value("provider_order", QStringList() << "library").toStringList();
|
|
|
|
front_model_->set_provider_order(provider_order);
|
|
|
|
back_model_->set_provider_order(provider_order);
|
|
|
|
show_providers_ = s.value("show_providers", true).toBool();
|
|
|
|
show_suggestions_ = s.value("show_suggestions", true).toBool();
|
2012-06-17 17:20:40 +02:00
|
|
|
SetGroupBy(LibraryModel::Grouping(
|
2014-02-07 16:34:20 +01:00
|
|
|
LibraryModel::GroupBy(
|
|
|
|
s.value("group_by1", int(LibraryModel::GroupBy_Artist)).toInt()),
|
|
|
|
LibraryModel::GroupBy(
|
|
|
|
s.value("group_by2", int(LibraryModel::GroupBy_Album)).toInt()),
|
|
|
|
LibraryModel::GroupBy(
|
|
|
|
s.value("group_by3", int(LibraryModel::GroupBy_None)).toInt())));
|
2012-06-11 17:26:33 +02:00
|
|
|
s.endGroup();
|
2014-02-06 14:48:00 +01:00
|
|
|
|
2012-06-11 17:26:33 +02:00
|
|
|
// Delete any old status widgets
|
|
|
|
qDeleteAll(provider_status_widgets_);
|
|
|
|
provider_status_widgets_.clear();
|
2014-02-06 14:48:00 +01:00
|
|
|
|
2012-06-11 17:26:33 +02:00
|
|
|
// Toggle visibility of the providers group
|
|
|
|
ui_->providers_group->setVisible(show_providers_);
|
|
|
|
|
|
|
|
if (show_providers_) {
|
2013-01-19 16:06:48 +01:00
|
|
|
// Sort the list of providers
|
2012-06-11 17:26:33 +02:00
|
|
|
QList<SearchProvider*> providers = engine_->providers();
|
2013-01-19 16:06:48 +01:00
|
|
|
qSort(providers.begin(), providers.end(),
|
2014-02-06 14:48:00 +01:00
|
|
|
std::bind(&CompareProvider, std::cref(provider_order), _1, _2));
|
|
|
|
|
2012-06-11 17:26:33 +02:00
|
|
|
bool any_disabled = false;
|
2014-02-06 14:48:00 +01:00
|
|
|
|
2014-02-10 14:29:07 +01:00
|
|
|
for (SearchProvider* provider : providers) {
|
2012-06-11 17:26:33 +02:00
|
|
|
QWidget* parent = ui_->enabled_list;
|
|
|
|
if (!engine_->is_provider_usable(provider)) {
|
|
|
|
parent = ui_->disabled_list;
|
|
|
|
any_disabled = true;
|
|
|
|
}
|
2014-02-06 14:48:00 +01:00
|
|
|
|
2012-06-11 17:26:33 +02:00
|
|
|
SearchProviderStatusWidget* widget =
|
|
|
|
new SearchProviderStatusWidget(warning_icon_, engine_, provider);
|
2014-02-06 14:48:00 +01:00
|
|
|
|
2012-06-11 17:26:33 +02:00
|
|
|
parent->layout()->addWidget(widget);
|
|
|
|
provider_status_widgets_ << widget;
|
|
|
|
}
|
2014-02-06 14:48:00 +01:00
|
|
|
|
2012-06-11 17:26:33 +02:00
|
|
|
ui_->disabled_label->setVisible(any_disabled);
|
|
|
|
}
|
2014-02-06 14:48:00 +01:00
|
|
|
|
2012-06-11 17:26:33 +02:00
|
|
|
ui_->suggestions_group->setVisible(show_suggestions_);
|
|
|
|
if (!show_suggestions_) {
|
|
|
|
update_suggestions_timer_->stop();
|
|
|
|
}
|
2014-03-30 08:08:30 +02:00
|
|
|
|
|
|
|
if (!old_show_suggestions && show_suggestions_) {
|
|
|
|
UpdateSuggestions();
|
|
|
|
}
|
2012-06-10 20:00:51 +02:00
|
|
|
}
|
|
|
|
|
2012-06-11 00:05:30 +02:00
|
|
|
void GlobalSearchView::UpdateSuggestions() {
|
|
|
|
const QStringList suggestions = engine_->GetSuggestions(kMaxSuggestions);
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < suggestions.count(); ++i) {
|
2012-06-11 00:05:30 +02:00
|
|
|
suggestion_widgets_[i]->SetText(suggestions[i]);
|
|
|
|
suggestion_widgets_[i]->show();
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = suggestions.count(); i < kMaxSuggestions; ++i) {
|
2012-06-11 00:05:30 +02:00
|
|
|
suggestion_widgets_[i]->hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-04 19:18:37 +02:00
|
|
|
void GlobalSearchView::StartSearch(const QString& query) {
|
2012-06-28 11:22:31 +02:00
|
|
|
ui_->search->setText(query);
|
2012-06-10 20:00:51 +02:00
|
|
|
TextEdited(query);
|
2012-06-04 19:18:37 +02:00
|
|
|
|
|
|
|
// Swap models immediately
|
|
|
|
swap_models_timer_->stop();
|
|
|
|
SwapModels();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalSearchView::TextEdited(const QString& text) {
|
|
|
|
const QString trimmed(text.trimmed());
|
|
|
|
|
|
|
|
// Add results to the back model, switch models after some delay.
|
2012-06-10 22:11:55 +02:00
|
|
|
back_model_->Clear();
|
2012-06-04 19:18:37 +02:00
|
|
|
current_model_ = back_model_;
|
|
|
|
current_proxy_ = back_proxy_;
|
|
|
|
swap_models_timer_->start();
|
|
|
|
|
|
|
|
// Cancel the last search (if any) and start the new one.
|
|
|
|
engine_->CancelSearch(last_search_id_);
|
|
|
|
// If text query is empty, don't start a new search
|
|
|
|
if (trimmed.isEmpty()) {
|
|
|
|
last_search_id_ = -1;
|
2012-06-10 20:00:51 +02:00
|
|
|
} else {
|
|
|
|
last_search_id_ = engine_->SearchAsync(trimmed);
|
2012-06-04 19:18:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void GlobalSearchView::AddResults(int id,
|
|
|
|
const SearchProvider::ResultList& results) {
|
|
|
|
if (id != last_search_id_ || results.isEmpty()) return;
|
2012-06-04 19:18:37 +02:00
|
|
|
|
2012-06-10 22:11:55 +02:00
|
|
|
current_model_->AddResults(results);
|
2012-06-04 19:18:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalSearchView::SwapModels() {
|
|
|
|
art_requests_.clear();
|
|
|
|
|
|
|
|
qSwap(front_model_, back_model_);
|
|
|
|
qSwap(front_proxy_, back_proxy_);
|
|
|
|
|
|
|
|
ui_->results->setModel(front_proxy_);
|
2012-06-10 20:00:51 +02:00
|
|
|
|
|
|
|
if (ui_->search->text().trimmed().isEmpty()) {
|
2012-06-16 22:17:13 +02:00
|
|
|
ui_->results_stack->setCurrentWidget(ui_->help_page);
|
2012-06-10 20:00:51 +02:00
|
|
|
} else {
|
2012-06-16 22:17:13 +02:00
|
|
|
ui_->results_stack->setCurrentWidget(ui_->results_page);
|
2012-06-10 20:00:51 +02:00
|
|
|
}
|
2012-06-04 19:18:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalSearchView::LazyLoadArt(const QModelIndex& proxy_index) {
|
2012-06-10 18:36:40 +02:00
|
|
|
if (!proxy_index.isValid() || proxy_index.model() != front_proxy_) {
|
2012-06-04 19:18:37 +02:00
|
|
|
return;
|
|
|
|
}
|
2012-06-10 18:36:40 +02:00
|
|
|
|
|
|
|
// Already loading art for this item?
|
2012-06-10 22:11:55 +02:00
|
|
|
if (proxy_index.data(GlobalSearchModel::Role_LazyLoadingArt).isValid()) {
|
2012-06-04 19:18:37 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-10 18:36:40 +02:00
|
|
|
// Should we even load art at all?
|
|
|
|
if (!app_->library_model()->use_pretty_covers()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is this an album?
|
2012-06-10 18:15:32 +02:00
|
|
|
const LibraryModel::GroupBy container_type = LibraryModel::GroupBy(
|
2014-02-07 16:34:20 +01:00
|
|
|
proxy_index.data(LibraryModel::Role_ContainerType).toInt());
|
2012-06-10 18:15:32 +02:00
|
|
|
if (container_type != LibraryModel::GroupBy_Album &&
|
|
|
|
container_type != LibraryModel::GroupBy_AlbumArtist &&
|
|
|
|
container_type != LibraryModel::GroupBy_YearAlbum) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark the item as loading art
|
2012-06-04 19:18:37 +02:00
|
|
|
const QModelIndex source_index = front_proxy_->mapToSource(proxy_index);
|
2012-06-10 18:15:32 +02:00
|
|
|
QStandardItem* item = front_model_->itemFromIndex(source_index);
|
2012-06-10 22:11:55 +02:00
|
|
|
item->setData(true, GlobalSearchModel::Role_LazyLoadingArt);
|
2012-06-10 18:15:32 +02:00
|
|
|
|
|
|
|
// Walk down the item's children until we find a track
|
|
|
|
while (item->rowCount()) {
|
|
|
|
item = item->child(0);
|
|
|
|
}
|
2012-06-04 19:18:37 +02:00
|
|
|
|
2012-06-10 18:15:32 +02:00
|
|
|
// Get the track's Result
|
2012-06-04 19:18:37 +02:00
|
|
|
const SearchProvider::Result result =
|
2014-02-07 16:34:20 +01:00
|
|
|
item->data(GlobalSearchModel::Role_Result)
|
|
|
|
.value<SearchProvider::Result>();
|
2012-06-04 19:18:37 +02:00
|
|
|
|
2012-06-10 18:15:32 +02:00
|
|
|
// Load the art.
|
2012-06-04 19:18:37 +02:00
|
|
|
int id = engine_->LoadArtAsync(result);
|
|
|
|
art_requests_[id] = source_index;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalSearchView::ArtLoaded(int id, const QPixmap& pixmap) {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!art_requests_.contains(id)) return;
|
2012-06-04 19:18:37 +02:00
|
|
|
QModelIndex index = art_requests_.take(id);
|
|
|
|
|
2012-06-10 18:36:40 +02:00
|
|
|
if (!pixmap.isNull()) {
|
|
|
|
front_model_->itemFromIndex(index)->setData(pixmap, Qt::DecorationRole);
|
|
|
|
}
|
2012-06-04 19:18:37 +02:00
|
|
|
}
|
|
|
|
|
2012-06-10 22:56:17 +02:00
|
|
|
MimeData* GlobalSearchView::SelectedMimeData() {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!ui_->results->selectionModel()) return nullptr;
|
2012-06-16 22:17:13 +02:00
|
|
|
|
2012-06-10 21:55:51 +02:00
|
|
|
// Get all selected model indexes
|
|
|
|
QModelIndexList indexes = ui_->results->selectionModel()->selectedRows();
|
|
|
|
if (indexes.isEmpty()) {
|
|
|
|
// There's nothing selected - take the first thing in the model that isn't
|
|
|
|
// a divider.
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < front_proxy_->rowCount(); ++i) {
|
2012-06-10 21:55:51 +02:00
|
|
|
QModelIndex index = front_proxy_->index(i, 0);
|
|
|
|
if (!index.data(LibraryModel::Role_IsDivider).toBool()) {
|
|
|
|
indexes << index;
|
2012-09-01 00:41:31 +02:00
|
|
|
ui_->results->setCurrentIndex(index);
|
2012-06-10 21:55:51 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Still got nothing? Give up.
|
|
|
|
if (indexes.isEmpty()) {
|
2014-02-06 16:49:49 +01:00
|
|
|
return nullptr;
|
2012-06-10 21:55:51 +02:00
|
|
|
}
|
|
|
|
|
2012-06-10 22:27:00 +02:00
|
|
|
// Get items for these indexes
|
|
|
|
QList<QStandardItem*> items;
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const QModelIndex& index : indexes) {
|
2012-06-10 22:27:00 +02:00
|
|
|
items << (front_model_->itemFromIndex(front_proxy_->mapToSource(index)));
|
2012-06-10 21:55:51 +02:00
|
|
|
}
|
2012-06-04 19:18:37 +02:00
|
|
|
|
2012-06-10 22:27:00 +02:00
|
|
|
// Get a MimeData for these items
|
|
|
|
return engine_->LoadTracks(front_model_->GetChildResults(items));
|
2012-06-04 19:18:37 +02:00
|
|
|
}
|
2012-06-10 22:56:17 +02:00
|
|
|
|
|
|
|
bool GlobalSearchView::eventFilter(QObject* object, QEvent* event) {
|
|
|
|
if (object == ui_->search && event->type() == QEvent::KeyRelease) {
|
|
|
|
if (SearchKeyEvent(static_cast<QKeyEvent*>(event))) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-02-07 16:34:20 +01:00
|
|
|
} else if (object == ui_->results_stack &&
|
|
|
|
event->type() == QEvent::ContextMenu) {
|
2012-06-10 22:56:17 +02:00
|
|
|
if (ResultsContextMenuEvent(static_cast<QContextMenuEvent*>(event))) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return QWidget::eventFilter(object, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GlobalSearchView::SearchKeyEvent(QKeyEvent* event) {
|
|
|
|
switch (event->key()) {
|
2014-02-07 16:34:20 +01:00
|
|
|
case Qt::Key_Up:
|
|
|
|
ui_->results->UpAndFocus();
|
|
|
|
break;
|
2012-06-10 22:56:17 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
case Qt::Key_Down:
|
|
|
|
ui_->results->DownAndFocus();
|
|
|
|
break;
|
2012-06-10 22:56:17 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
case Qt::Key_Escape:
|
|
|
|
ui_->search->clear();
|
|
|
|
break;
|
2012-06-10 22:56:17 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
case Qt::Key_Return:
|
|
|
|
AddSelectedToPlaylist();
|
|
|
|
break;
|
2012-09-01 00:29:31 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
default:
|
|
|
|
return false;
|
2012-06-10 22:56:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
event->accept();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GlobalSearchView::ResultsContextMenuEvent(QContextMenuEvent* event) {
|
|
|
|
if (!context_menu_) {
|
|
|
|
context_menu_ = new QMenu(this);
|
2014-02-07 16:34:20 +01:00
|
|
|
context_actions_ << context_menu_->addAction(
|
|
|
|
IconLoader::Load("media-playback-start"),
|
|
|
|
tr("Append to current playlist"), this,
|
|
|
|
SLOT(AddSelectedToPlaylist()));
|
|
|
|
context_actions_ << context_menu_->addAction(
|
|
|
|
IconLoader::Load("media-playback-start"),
|
|
|
|
tr("Replace current playlist"), this,
|
|
|
|
SLOT(LoadSelected()));
|
|
|
|
context_actions_ << context_menu_->addAction(
|
|
|
|
IconLoader::Load("document-new"),
|
|
|
|
tr("Open in new playlist"), this,
|
|
|
|
SLOT(OpenSelectedInNewPlaylist()));
|
2012-06-10 22:56:17 +02:00
|
|
|
|
|
|
|
context_menu_->addSeparator();
|
2014-02-07 16:34:20 +01:00
|
|
|
context_actions_ << context_menu_->addAction(
|
|
|
|
IconLoader::Load("go-next"), tr("Queue track"),
|
|
|
|
this, SLOT(AddSelectedToPlaylistEnqueue()));
|
2012-06-16 22:17:13 +02:00
|
|
|
|
|
|
|
context_menu_->addSeparator();
|
2014-02-07 16:34:20 +01:00
|
|
|
context_menu_->addMenu(tr("Group by"))
|
|
|
|
->addActions(group_by_actions_->actions());
|
2012-06-16 22:17:13 +02:00
|
|
|
context_menu_->addAction(IconLoader::Load("configure"),
|
2014-02-07 16:34:20 +01:00
|
|
|
tr("Configure global search..."), this,
|
|
|
|
SLOT(OpenSettingsDialog()));
|
2012-06-16 22:17:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const bool enable_context_actions =
|
|
|
|
ui_->results->selectionModel() &&
|
|
|
|
ui_->results->selectionModel()->hasSelection();
|
|
|
|
|
2014-02-10 14:29:07 +01:00
|
|
|
for (QAction* action : context_actions_) {
|
2012-06-16 22:17:13 +02:00
|
|
|
action->setEnabled(enable_context_actions);
|
2012-06-10 22:56:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
context_menu_->popup(event->globalPos());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalSearchView::AddSelectedToPlaylist() {
|
|
|
|
emit AddToPlaylist(SelectedMimeData());
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalSearchView::LoadSelected() {
|
|
|
|
MimeData* data = SelectedMimeData();
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!data) return;
|
2012-06-16 22:17:13 +02:00
|
|
|
|
2012-06-10 22:56:17 +02:00
|
|
|
data->clear_first_ = true;
|
|
|
|
emit AddToPlaylist(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalSearchView::AddSelectedToPlaylistEnqueue() {
|
|
|
|
MimeData* data = SelectedMimeData();
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!data) return;
|
2012-06-16 22:17:13 +02:00
|
|
|
|
2012-06-10 22:56:17 +02:00
|
|
|
data->enqueue_now_ = true;
|
|
|
|
emit AddToPlaylist(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalSearchView::OpenSelectedInNewPlaylist() {
|
|
|
|
MimeData* data = SelectedMimeData();
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!data) return;
|
2012-06-16 22:17:13 +02:00
|
|
|
|
2012-06-10 22:56:17 +02:00
|
|
|
data->open_in_new_playlist_ = true;
|
|
|
|
emit AddToPlaylist(data);
|
|
|
|
}
|
2012-06-11 00:05:30 +02:00
|
|
|
|
|
|
|
void GlobalSearchView::showEvent(QShowEvent* e) {
|
2012-06-11 17:26:33 +02:00
|
|
|
if (show_suggestions_) {
|
|
|
|
UpdateSuggestions();
|
|
|
|
update_suggestions_timer_->start();
|
|
|
|
}
|
2012-06-11 00:05:30 +02:00
|
|
|
QWidget::showEvent(e);
|
2012-06-16 22:17:13 +02:00
|
|
|
|
|
|
|
FocusSearchField();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalSearchView::FocusSearchField() {
|
2012-06-28 11:22:31 +02:00
|
|
|
ui_->search->setFocus();
|
2012-06-28 12:27:32 +02:00
|
|
|
ui_->search->selectAll();
|
2012-06-11 00:05:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalSearchView::hideEvent(QHideEvent* e) {
|
|
|
|
update_suggestions_timer_->stop();
|
|
|
|
QWidget::hideEvent(e);
|
|
|
|
}
|
2012-06-11 22:06:26 +02:00
|
|
|
|
|
|
|
void GlobalSearchView::FocusOnFilter(QKeyEvent* event) {
|
2012-06-28 11:22:31 +02:00
|
|
|
ui_->search->setFocus();
|
2012-06-11 22:06:26 +02:00
|
|
|
QApplication::sendEvent(ui_->search, event);
|
|
|
|
}
|
2012-06-16 22:17:13 +02:00
|
|
|
|
|
|
|
void GlobalSearchView::OpenSettingsDialog() {
|
2012-12-05 10:36:22 +01:00
|
|
|
app_->OpenSettingsDialogAtPage(SettingsDialog::Page_GlobalSearch);
|
2012-06-16 22:17:13 +02:00
|
|
|
}
|
2012-06-17 17:20:40 +02:00
|
|
|
|
|
|
|
void GlobalSearchView::GroupByClicked(QAction* action) {
|
|
|
|
if (action->property("group_by").isNull()) {
|
|
|
|
if (!group_by_dialog_) {
|
|
|
|
group_by_dialog_.reset(new GroupByDialog);
|
|
|
|
connect(group_by_dialog_.data(), SIGNAL(Accepted(LibraryModel::Grouping)),
|
|
|
|
SLOT(SetGroupBy(LibraryModel::Grouping)));
|
|
|
|
}
|
|
|
|
|
|
|
|
group_by_dialog_->show();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetGroupBy(action->property("group_by").value<LibraryModel::Grouping>());
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalSearchView::SetGroupBy(const LibraryModel::Grouping& g) {
|
|
|
|
// Update the models
|
|
|
|
front_model_->SetGroupBy(g, true);
|
|
|
|
back_model_->SetGroupBy(g, false);
|
|
|
|
|
|
|
|
// Save the setting
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(GlobalSearch::kSettingsGroup);
|
|
|
|
s.setValue("group_by1", int(g.first));
|
|
|
|
s.setValue("group_by2", int(g.second));
|
|
|
|
s.setValue("group_by3", int(g.third));
|
|
|
|
|
|
|
|
// Make sure the correct action is checked.
|
2014-02-10 14:29:07 +01:00
|
|
|
for (QAction* action : group_by_actions_->actions()) {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (action->property("group_by").isNull()) continue;
|
2012-06-17 17:20:40 +02:00
|
|
|
|
|
|
|
if (g == action->property("group_by").value<LibraryModel::Grouping>()) {
|
|
|
|
action->setChecked(true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the advanced action
|
|
|
|
group_by_actions_->actions().last()->setChecked(true);
|
|
|
|
}
|