Don't let search results that arrive later appear before other search results - should make navigating the global search results less annoying when there are remote providers like spotify.

This commit is contained in:
David Sansome 2011-09-29 22:29:20 +01:00
parent 516190bfc2
commit 4282b6b68b
3 changed files with 28 additions and 10 deletions

View File

@ -31,23 +31,30 @@ bool GlobalSearchSortModel::lessThan(const QModelIndex& left, const QModelIndex&
const SearchProvider::Result r2 = right.data(GlobalSearchWidget::Role_PrimaryResult)
.value<SearchProvider::Result>();
int ret = 0;
// Order results that arrived first first, so that the results don't jump
// around while the user is trying to navigate through them.
const int order_left = left.data(GlobalSearchWidget::Role_OrderArrived).toInt();
const int order_right = right.data(GlobalSearchWidget::Role_OrderArrived).toInt();
if (order_left < order_right) return true;
if (order_left > order_right) return false;
#define CompareInt(field) \
if (r1.field < r2.field) return true; \
if (r1.field > r2.field) return false
int ret = 0;
#define CompareString(field) \
ret = QString::localeAwareCompare(r1.metadata_.field(), r2.metadata_.field()); \
if (ret < 0) return true; \
if (ret > 0) return false
// Compare match quality and types first
// If they arrived at the same time then sort by quality and type.
CompareInt(match_quality_);
CompareInt(type_);
// Then compare title, artist and album
// Failing that, compare title, artist and album
switch (r1.type_) {
case SearchProvider::Result::Type_Track:
case SearchProvider::Result::Type_Stream:

View File

@ -48,6 +48,7 @@ GlobalSearchWidget::GlobalSearchWidget(QWidget* parent)
engine_(NULL),
last_id_(0),
clear_model_on_next_result_(false),
order_arrived_counter_(0),
model_(new QStandardItemModel(this)),
proxy_(new GlobalSearchSortModel(this)),
view_(new QListView),
@ -192,6 +193,7 @@ void GlobalSearchWidget::TextEdited(const QString& text) {
void GlobalSearchWidget::Reset() {
model_->clear();
art_requests_.clear();
order_arrived_counter_ = 0;
}
void GlobalSearchWidget::SearchFinished(int id) {
@ -200,7 +202,7 @@ void GlobalSearchWidget::SearchFinished(int id) {
if (clear_model_on_next_result_) {
Reset();
clear_model_on_next_result_ = true;
clear_model_on_next_result_ = false;
}
RepositionPopup();
@ -219,6 +221,7 @@ void GlobalSearchWidget::AddResults(int id, const SearchProvider::ResultList& re
QStandardItem* item = new QStandardItem;
item->setData(QVariant::fromValue(result), Role_PrimaryResult);
item->setData(QVariant::fromValue(SearchProvider::ResultList() << result), Role_AllResults);
item->setData(order_arrived_counter_, Role_OrderArrived);
QPixmap pixmap;
if (engine_->FindCachedPixmap(result, &pixmap)) {
@ -229,12 +232,16 @@ void GlobalSearchWidget::AddResults(int id, const SearchProvider::ResultList& re
if (combine_identical_results_) {
// Maybe we can combine this result with an identical result from another
// provider. Only look at the results above and below this one in the
// sorted model.
// provider. We can use the sorted model to narrow the scope of the
// search a bit - look at the result after the current one, then all the
// results before.
QModelIndex my_proxy_index = proxy_->mapFromSource(item->index());
QModelIndexList candidates;
candidates << my_proxy_index.sibling(my_proxy_index.row() - 1, 0)
<< my_proxy_index.sibling(my_proxy_index.row() + 1, 0);
candidates << my_proxy_index.sibling(my_proxy_index.row() + 1, 0);
for (int i=my_proxy_index.row()-1 ; i>=0 ; --i) {
candidates << my_proxy_index.sibling(i, 0);
}
foreach (const QModelIndex& index, candidates) {
if (!index.isValid())
@ -261,6 +268,8 @@ void GlobalSearchWidget::AddResults(int id, const SearchProvider::ResultList& re
}
}
order_arrived_counter_ ++;
RepositionPopup();
}

View File

@ -50,7 +50,8 @@ public:
enum Role {
Role_PrimaryResult = Qt::UserRole + 1,
Role_AllResults,
Role_LazyLoadingArt
Role_LazyLoadingArt,
Role_OrderArrived
};
void Init(GlobalSearch* engine_);
@ -114,6 +115,7 @@ private:
GlobalSearch* engine_;
int last_id_;
bool clear_model_on_next_result_;
int order_arrived_counter_;
QMap<int, QModelIndex> art_requests_;
QMap<int, QAction*> track_requests_;