From c27e33a1633a3d14aee0b18ae78fe8c279ee3ae7 Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Thu, 22 Dec 2016 11:07:24 +0100 Subject: [PATCH] feat(ui/views/App/MainWindow/MainWindow): it uses a `SmartSearchBarProxyModel` component (unstable) --- tests/CMakeLists.txt | 2 ++ tests/src/app/App.cpp | 9 ++------- .../components/contacts/ContactsListModel.hpp | 4 ++-- .../UnregisteredSipAddressesProxyModel.cpp | 12 +++++++---- .../smart-search-bar/SmartSearchBarModel.hpp | 4 ++-- .../SmartSearchBarProxyModel.cpp | 8 ++++++++ .../SmartSearchBarProxyModel.hpp | 20 +++++++++++++++++++ tests/ui/modules/Common/SearchBox.qml | 2 +- tests/ui/views/App/MainWindow/MainWindow.qml | 2 +- 9 files changed, 46 insertions(+), 17 deletions(-) create mode 100644 tests/src/components/smart-search-bar/SmartSearchBarProxyModel.cpp create mode 100644 tests/src/components/smart-search-bar/SmartSearchBarProxyModel.hpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 30ecfa53d..be6a0ce7b 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -70,6 +70,7 @@ set(SOURCES src/components/sip-addresses/UnregisteredSipAddressesModel.cpp src/components/sip-addresses/UnregisteredSipAddressesProxyModel.cpp src/components/smart-search-bar/SmartSearchBarModel.cpp + src/components/smart-search-bar/SmartSearchBarProxyModel.cpp src/components/timeline/TimelineModel.cpp src/main.cpp ) @@ -96,6 +97,7 @@ set(HEADERS src/components/sip-addresses/UnregisteredSipAddressesModel.hpp src/components/sip-addresses/UnregisteredSipAddressesProxyModel.hpp src/components/smart-search-bar/SmartSearchBarModel.hpp + src/components/smart-search-bar/SmartSearchBarProxyModel.hpp src/components/timeline/TimelineModel.hpp src/utils.hpp ) diff --git a/tests/src/app/App.cpp b/tests/src/app/App.cpp index 2c63bc0c9..6bac00b52 100644 --- a/tests/src/app/App.cpp +++ b/tests/src/app/App.cpp @@ -12,7 +12,7 @@ #include "../components/settings/AccountSettingsModel.hpp" #include "../components/sip-addresses/UnregisteredSipAddressesProxyModel.hpp" #include "../components/timeline/TimelineModel.hpp" -#include "../components/smart-search-bar/SmartSearchBarModel.hpp" +#include "../components/smart-search-bar/SmartSearchBarProxyModel.hpp" #include "App.hpp" @@ -139,17 +139,12 @@ void App::registerTypes () { } ); - qmlRegisterSingletonType( - "Linphone", 1, 0, "SmartSearchBarModel", - [](QQmlEngine *, QJSEngine *) -> QObject *{ - return new SmartSearchBarModel(); - } - ); qmlRegisterType("Linphone", 1, 0, "Camera"); qmlRegisterType("Linphone", 1, 0, "ContactsListProxyModel"); qmlRegisterType("Linphone", 1, 0, "ChatModel"); qmlRegisterType("Linphone", 1, 0, "ChatProxyModel"); qmlRegisterType("Linphone", 1, 0, "UnregisteredSipAddressesProxyModel"); + qmlRegisterType("Linphone", 1, 0, "SmartSearchBarProxyModel"); qRegisterMetaType("ChatModel::EntryType"); } diff --git a/tests/src/components/contacts/ContactsListModel.hpp b/tests/src/components/contacts/ContactsListModel.hpp index 133eaeb17..159edca4e 100644 --- a/tests/src/components/contacts/ContactsListModel.hpp +++ b/tests/src/components/contacts/ContactsListModel.hpp @@ -9,11 +9,11 @@ // ============================================================================= class ContactsListModel : public QAbstractListModel { - Q_OBJECT; - friend class ContactsListProxyModel; friend class SipAddressesModel; + Q_OBJECT; + public: ContactsListModel (QObject *parent = Q_NULLPTR); ~ContactsListModel () = default; diff --git a/tests/src/components/sip-addresses/UnregisteredSipAddressesProxyModel.cpp b/tests/src/components/sip-addresses/UnregisteredSipAddressesProxyModel.cpp index 84924a761..526eedcb8 100644 --- a/tests/src/components/sip-addresses/UnregisteredSipAddressesProxyModel.cpp +++ b/tests/src/components/sip-addresses/UnregisteredSipAddressesProxyModel.cpp @@ -27,10 +27,14 @@ bool UnregisteredSipAddressesProxyModel::filterAcceptsRow (int source_row, const } bool UnregisteredSipAddressesProxyModel::lessThan (const QModelIndex &left, const QModelIndex &right) const { - return computeStringWeight( - sourceModel()->data(left).toMap()["sipAddress"].toString() - ) > computeStringWeight( - sourceModel()->data(right).toMap()["sipAddress"].toString() + QString sip_address_a = sourceModel()->data(left).toMap()["sipAddress"].toString(); + QString sip_address_b = sourceModel()->data(right).toMap()["sipAddress"].toString(); + + int weight_a = computeStringWeight(sip_address_a); + int weight_b = computeStringWeight(sip_address_b); + + return weight_a > weight_b || ( + weight_a == weight_b && sip_address_a >= sip_address_b ); } diff --git a/tests/src/components/smart-search-bar/SmartSearchBarModel.hpp b/tests/src/components/smart-search-bar/SmartSearchBarModel.hpp index c0659a53d..4771a769b 100644 --- a/tests/src/components/smart-search-bar/SmartSearchBarModel.hpp +++ b/tests/src/components/smart-search-bar/SmartSearchBarModel.hpp @@ -14,14 +14,14 @@ class SmartSearchBarModel : public QAbstractListModel { public: SmartSearchBarModel (QObject *parent = Q_NULLPTR) : QAbstractListModel(parent) {} - ~SmartSearchBarModel () = default; + virtual ~SmartSearchBarModel () = default; int rowCount (const QModelIndex &index = QModelIndex()) const override; QHash roleNames () const override; QVariant data (const QModelIndex &index, int role) const override; -private: +protected: ContactsListProxyModel m_contacts; UnregisteredSipAddressesProxyModel m_sip_addresses; }; diff --git a/tests/src/components/smart-search-bar/SmartSearchBarProxyModel.cpp b/tests/src/components/smart-search-bar/SmartSearchBarProxyModel.cpp new file mode 100644 index 000000000..11d8881d0 --- /dev/null +++ b/tests/src/components/smart-search-bar/SmartSearchBarProxyModel.cpp @@ -0,0 +1,8 @@ +#include "SmartSearchBarProxyModel.hpp" + +// ============================================================================= + +void SmartSearchBarProxyModel::setFilter (const QString &pattern) { + m_contacts.setFilter(pattern); + m_sip_addresses.setFilter(pattern); +} diff --git a/tests/src/components/smart-search-bar/SmartSearchBarProxyModel.hpp b/tests/src/components/smart-search-bar/SmartSearchBarProxyModel.hpp new file mode 100644 index 000000000..d52757861 --- /dev/null +++ b/tests/src/components/smart-search-bar/SmartSearchBarProxyModel.hpp @@ -0,0 +1,20 @@ +#ifndef SMART_SEARCH_BAR_PROXY_MODEL_H_ +#define SMART_SEARCH_BAR_PROXY_MODEL_H_ + +#include "SmartSearchBarModel.hpp" + +// ============================================================================= + +class SmartSearchBarProxyModel : public SmartSearchBarModel { + Q_OBJECT; + +public: + SmartSearchBarProxyModel (QObject *parent = Q_NULLPTR) : SmartSearchBarModel(parent) {} + + ~SmartSearchBarProxyModel () = default; + +public slots: + void setFilter (const QString &pattern); +}; + +#endif // SMART_SEARCH_BAR_PROXY_MODEL_H_ diff --git a/tests/ui/modules/Common/SearchBox.qml b/tests/ui/modules/Common/SearchBox.qml index e69fad060..0dbbbcaaf 100644 --- a/tests/ui/modules/Common/SearchBox.qml +++ b/tests/ui/modules/Common/SearchBox.qml @@ -70,7 +70,7 @@ Item { Keys.onEscapePressed: searchBox.hideMenu() onActiveFocusChanged: activeFocus && searchBox.showMenu() - onTextChanged: _filter() + onTextChanged: _filter(text) } // Wrap the search box menu in a window. diff --git a/tests/ui/views/App/MainWindow/MainWindow.qml b/tests/ui/views/App/MainWindow/MainWindow.qml index a57001e47..775cbf7e7 100644 --- a/tests/ui/views/App/MainWindow/MainWindow.qml +++ b/tests/ui/views/App/MainWindow/MainWindow.qml @@ -145,7 +145,7 @@ ApplicationWindow { maxMenuHeight: MainWindowStyle.searchBox.maxHeight placeholderText: qsTr('mainSearchBarPlaceholder') - model: SmartSearchBarModel + model: SmartSearchBarProxyModel {} } } }