From 7821bf168d6335f0241c9add8f7424b0c6aff658 Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Thu, 5 Jan 2017 14:01:45 +0100 Subject: [PATCH] fix(src/components/smart-search-bar/SmartSearchBarModel): use contact (if exists) username on search --- .../smart-search-bar/SmartSearchBarModel.cpp | 17 ++++++++++++++--- .../smart-search-bar/SmartSearchBarModel.hpp | 1 + 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/tests/src/components/smart-search-bar/SmartSearchBarModel.cpp b/tests/src/components/smart-search-bar/SmartSearchBarModel.cpp index a21b24bd0..ca3e885df 100644 --- a/tests/src/components/smart-search-bar/SmartSearchBarModel.cpp +++ b/tests/src/components/smart-search-bar/SmartSearchBarModel.cpp @@ -36,7 +36,7 @@ void SmartSearchBarModel::setFilter (const QString &pattern) { bool SmartSearchBarModel::filterAcceptsRow (int source_row, const QModelIndex &source_parent) const { const QModelIndex &index = sourceModel()->index(source_row, 0, source_parent); - return computeStringWeight(index.data().toMap()["sipAddress"].toString()) > 0; + return computeEntryWeight(index.data().toMap()) > 0; } bool SmartSearchBarModel::lessThan (const QModelIndex &left, const QModelIndex &right) const { @@ -46,8 +46,9 @@ bool SmartSearchBarModel::lessThan (const QModelIndex &left, const QModelIndex & const QString &sip_address_a = map_a["sipAddress"].toString(); const QString &sip_address_b = map_b["sipAddress"].toString(); - int weight_a = computeStringWeight(sip_address_a); - int weight_b = computeStringWeight(sip_address_b); + // TODO: Use a cache, do not compute the same value as `filterAcceptsRow`. + int weight_a = computeEntryWeight(map_a); + int weight_b = computeEntryWeight(map_b); // 1. Not the same weight. if (weight_a != weight_b) @@ -77,6 +78,16 @@ bool SmartSearchBarModel::lessThan (const QModelIndex &left, const QModelIndex & return sip_address_a <= sip_address_b; } +int SmartSearchBarModel::computeEntryWeight (const QVariantMap &entry) const { + int weight = computeStringWeight(entry["sipAddress"].toString().mid(4)); + + const ContactModel *contact = entry.value("contact").value(); + if (contact) + weight += computeStringWeight(contact->getVcardModel()->getUsername()); + + return weight; +} + int SmartSearchBarModel::computeStringWeight (const QString &string) const { int index = -1; int offset = -1; diff --git a/tests/src/components/smart-search-bar/SmartSearchBarModel.hpp b/tests/src/components/smart-search-bar/SmartSearchBarModel.hpp index 48ef732d2..c64e46c09 100644 --- a/tests/src/components/smart-search-bar/SmartSearchBarModel.hpp +++ b/tests/src/components/smart-search-bar/SmartSearchBarModel.hpp @@ -23,6 +23,7 @@ protected: bool lessThan (const QModelIndex &left, const QModelIndex &right) const override; private: + int computeEntryWeight (const QVariantMap &entry) const; int computeStringWeight (const QString &string) const; QString m_filter;