From 758dd1c00ad947df966002ec8fdc0076b09afc54 Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Tue, 20 Dec 2016 11:00:14 +0100 Subject: [PATCH] unstable (refactoring) --- tests/CMakeLists.txt | 24 ++++++------ tests/src/app/App.cpp | 39 ++++++++----------- .../components/contacts/ContactsListModel.cpp | 2 + .../components/contacts/ContactsListModel.hpp | 15 ++++++- .../contacts/ContactsListProxyModel.cpp | 13 +------ .../contacts/ContactsListProxyModel.hpp | 18 +++------ .../src/components/timeline/TimelineModel.cpp | 4 +- .../src/components/timeline/TimelineModel.hpp | 2 +- 8 files changed, 55 insertions(+), 62 deletions(-) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index bc151ce7e..ba26caaf0 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,6 +1,6 @@ -# ==================================================================== +# ============================================================================== # CMakeLists.txt -# ==================================================================== +# ============================================================================== cmake_minimum_required(VERSION 3.1) project(linphone) @@ -31,9 +31,9 @@ set(CUSTOM_FLAGS "\ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CUSTOM_FLAGS}") set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DQT_QML_DEBUG -DQT_DECLARATIVE_DEBUG") -# -------------------------------------------------------------------- -# Define packages, libs, sources, headers, resources and languages -# -------------------------------------------------------------------- +# ------------------------------------------------------------------------------ +# Define packages, libs, sources, headers, resources and languages. +# ------------------------------------------------------------------------------ set(QT5_PACKAGES Core Gui Quick Widgets QuickControls2 LinguistTools) @@ -108,7 +108,7 @@ set(LANGUAGES_DIRECTORY assets/languages) set(I18N_FILENAME i18n.qrc) set(LANGUAGES en fr) -# -------------------------------------------------------------------- +# ------------------------------------------------------------------------------ function (PREPEND list prefix) set(new_list "") @@ -125,9 +125,9 @@ PREPEND(SOURCES "${CMAKE_SOURCE_DIR}/") PREPEND(HEADERS "${CMAKE_SOURCE_DIR}/") PREPEND(QRC_RESOURCES "${CMAKE_SOURCE_DIR}/") -# -------------------------------------------------------------------- +# ------------------------------------------------------------------------------ # Compute QML files list. -# -------------------------------------------------------------------- +# ------------------------------------------------------------------------------ set(QML_SOURCES) file(STRINGS ${QRC_RESOURCES} QRC_RESOURCES_CONTENT) @@ -149,18 +149,18 @@ add_custom_target( COMMAND "${CMAKE_SOURCE_DIR}/tools/check_qml_syntax" ) -# -------------------------------------------------------------------- +# ------------------------------------------------------------------------------ # Init git hooks. -# -------------------------------------------------------------------- +# ------------------------------------------------------------------------------ execute_process(COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_SOURCE_DIR}/tools/private/pre-commit" "${CMAKE_SOURCE_DIR}/../.git/hooks/pre-commit" ) -# -------------------------------------------------------------------- +# ------------------------------------------------------------------------------ # Build. -# -------------------------------------------------------------------- +# ------------------------------------------------------------------------------ find_package(Qt5 COMPONENTS ${QT5_PACKAGES}) diff --git a/tests/src/app/App.cpp b/tests/src/app/App.cpp index 2455f5e5f..f3a5ecffa 100644 --- a/tests/src/app/App.cpp +++ b/tests/src/app/App.cpp @@ -63,8 +63,9 @@ App::App (int &argc, char **argv) : QApplication(argc, argv) { void App::initContentApp () { qInfo() << "Initializing core manager..."; - // Init core. + // Init core & contacts. CoreManager::init(); + ContactsListModel::init(); // Register types and load context properties. registerTypes(); @@ -86,13 +87,18 @@ void App::initContentApp () { void App::registerTypes () { qInfo() << "Registering types..."; - // Register meta types. + qmlRegisterUncreatableType( + "Linphone", 1, 0, "ContactModel", "ContactModel is uncreatable" + ); + qmlRegisterUncreatableType( + "Linphone", 1, 0, "VcardModel", "VcardModel is uncreatable" + ); qmlRegisterUncreatableType( "Linphone", 1, 0, "Presence", "Presence is uncreatable" ); + qRegisterMetaType("ChatModel::EntryType"); - // Register Application/Core. qmlRegisterSingletonType( "Linphone", 1, 0, "App", [](QQmlEngine *, QJSEngine *) -> QObject *{ @@ -107,28 +113,10 @@ void App::registerTypes () { } ); - // Register models. - qmlRegisterType("Linphone", 1, 0, "Camera"); - - qmlRegisterUncreatableType( - "Linphone", 1, 0, "ContactModel", "ContactModel is uncreatable" - ); - - qmlRegisterUncreatableType( - "Linphone", 1, 0, "VcardModel", "VcardModel is uncreatable" - ); - - ContactsListProxyModel::initContactsListModel(new ContactsListModel()); - qmlRegisterType("Linphone", 1, 0, "ContactsListProxyModel"); - - qmlRegisterType("Linphone", 1, 0, "ChatModel"); - qmlRegisterType("Linphone", 1, 0, "ChatProxyModel"); - - // Register singletons. qmlRegisterSingletonType( "Linphone", 1, 0, "ContactsListModel", [](QQmlEngine *, QJSEngine *) -> QObject *{ - return ContactsListProxyModel::getContactsListModel(); + return ContactsListModel::getInstance(); } ); @@ -142,9 +130,14 @@ void App::registerTypes () { qmlRegisterSingletonType( "Linphone", 1, 0, "TimelineModel", [](QQmlEngine *, QJSEngine *) -> QObject *{ - return new TimelineModel(ContactsListProxyModel::getContactsListModel()); + return new TimelineModel(); } ); + + qmlRegisterType("Linphone", 1, 0, "Camera"); + qmlRegisterType("Linphone", 1, 0, "ContactsListProxyModel"); + qmlRegisterType("Linphone", 1, 0, "ChatModel"); + qmlRegisterType("Linphone", 1, 0, "ChatProxyModel"); } void App::addContextProperties () { diff --git a/tests/src/components/contacts/ContactsListModel.cpp b/tests/src/components/contacts/ContactsListModel.cpp index 0fb852936..28b528d9b 100644 --- a/tests/src/components/contacts/ContactsListModel.cpp +++ b/tests/src/components/contacts/ContactsListModel.cpp @@ -10,6 +10,8 @@ using namespace std; // ============================================================================= +ContactsListModel *ContactsListModel::m_instance = nullptr; + ContactsListModel::ContactsListModel (QObject *parent) : QAbstractListModel(parent) { m_linphone_friends = CoreManager::getInstance()->getCore()->getFriendsLists().front(); diff --git a/tests/src/components/contacts/ContactsListModel.hpp b/tests/src/components/contacts/ContactsListModel.hpp index a241b696c..6c0b017b9 100644 --- a/tests/src/components/contacts/ContactsListModel.hpp +++ b/tests/src/components/contacts/ContactsListModel.hpp @@ -14,7 +14,6 @@ class ContactsListModel : public QAbstractListModel { friend class ContactsListProxyModel; public: - ContactsListModel (QObject *parent = Q_NULLPTR); ~ContactsListModel () = default; int rowCount (const QModelIndex &index = QModelIndex()) const override; @@ -25,6 +24,16 @@ public: bool removeRow (int row, const QModelIndex &parent = QModelIndex()); bool removeRows (int row, int count, const QModelIndex &parent = QModelIndex()) override; + static void init () { + if (!ContactsListModel::m_instance) { + m_instance = new ContactsListModel(); + } + } + + static ContactsListModel *getInstance () { + return m_instance; + } + public slots: ContactModel *mapSipAddressToContact (const QString &sipAddress) const; @@ -32,8 +41,12 @@ public slots: void removeContact (ContactModel *contact); private: + ContactsListModel (QObject *parent = Q_NULLPTR); + QList m_list; std::shared_ptr m_linphone_friends; + + static ContactsListModel *m_instance; }; #endif // CONTACTS_LIST_MODEL_H_ diff --git a/tests/src/components/contacts/ContactsListProxyModel.cpp b/tests/src/components/contacts/ContactsListProxyModel.cpp index 81a78b224..a1048d3cc 100644 --- a/tests/src/components/contacts/ContactsListProxyModel.cpp +++ b/tests/src/components/contacts/ContactsListProxyModel.cpp @@ -1,6 +1,7 @@ #include #include "../../utils.hpp" +#include "ContactsListModel.hpp" #include "ContactsListProxyModel.hpp" @@ -17,8 +18,6 @@ using namespace std; // ============================================================================= -ContactsListModel *ContactsListProxyModel::m_list = nullptr; - // Notes: // // - First `^` is necessary to search two words with one separator @@ -33,8 +32,7 @@ const QRegExp ContactsListProxyModel::m_search_separators("^[^_.-;@ ][_.-;@ ]"); // ----------------------------------------------------------------------------- ContactsListProxyModel::ContactsListProxyModel (QObject *parent) : QSortFilterProxyModel(parent) { - if (m_list == nullptr) - qFatal("Contacts list model is undefined."); + m_list = ContactsListModel::getInstance(); setSourceModel(m_list); setFilterCaseSensitivity(Qt::CaseInsensitive); @@ -46,13 +44,6 @@ ContactsListProxyModel::ContactsListProxyModel (QObject *parent) : QSortFilterPr sort(0); } -void ContactsListProxyModel::initContactsListModel (ContactsListModel *list) { - if (!m_list) - m_list = list; - else - qWarning() << "Contacts list model is already defined."; -} - bool ContactsListProxyModel::filterAcceptsRow ( int source_row, const QModelIndex &source_parent diff --git a/tests/src/components/contacts/ContactsListProxyModel.hpp b/tests/src/components/contacts/ContactsListProxyModel.hpp index 17705026b..290ca01f6 100644 --- a/tests/src/components/contacts/ContactsListProxyModel.hpp +++ b/tests/src/components/contacts/ContactsListProxyModel.hpp @@ -3,10 +3,12 @@ #include -#include "ContactsListModel.hpp" +#include "../contact/ContactModel.hpp" // ============================================================================= +class ContactsListModel; + class ContactsListProxyModel : public QSortFilterProxyModel { Q_OBJECT; @@ -20,11 +22,6 @@ public: ContactsListProxyModel (QObject *parent = Q_NULLPTR); ~ContactsListProxyModel () = default; - static void initContactsListModel (ContactsListModel *list); - static ContactsListModel *getContactsListModel () { - return m_list; - } - public slots: void setFilter (const QString &pattern) { setFilterFixedString(pattern); @@ -45,17 +42,14 @@ private: void setConnectedFilter (bool use_connected_filter); + ContactsListModel *m_list; + bool m_use_connected_filter = false; + // It's just a cache to save values computed by `filterAcceptsRow` // and reused by `lessThan`. mutable QHash m_weights; - bool m_use_connected_filter = false; - static const QRegExp m_search_separators; - - // The contacts list is shared between `ContactsListProxyModel` - // it's necessary to initialize it with `initContactsListModel`. - static ContactsListModel *m_list; }; #endif // CONTACTS_LIST_PROXY_MODEL_H_ diff --git a/tests/src/components/timeline/TimelineModel.cpp b/tests/src/components/timeline/TimelineModel.cpp index 5a34f3c09..9e9179ef1 100644 --- a/tests/src/components/timeline/TimelineModel.cpp +++ b/tests/src/components/timeline/TimelineModel.cpp @@ -14,13 +14,13 @@ using namespace std; // =================================================================== -TimelineModel::TimelineModel (const ContactsListModel *contacts_list) { +TimelineModel::TimelineModel (QObject *parent) : QAbstractListModel(parent) { init_entries(); // Invalidate model if a contact is removed. // Better than compare each sip address. connect( - contacts_list, &ContactsListModel::rowsRemoved, this, + ContactsListModel::getInstance(), &ContactsListModel::rowsRemoved, this, [this](const QModelIndex &, int, int) { beginResetModel(); // Nothing. diff --git a/tests/src/components/timeline/TimelineModel.hpp b/tests/src/components/timeline/TimelineModel.hpp index 96193ec34..c8420d3fd 100644 --- a/tests/src/components/timeline/TimelineModel.hpp +++ b/tests/src/components/timeline/TimelineModel.hpp @@ -11,7 +11,7 @@ class TimelineModel : public QAbstractListModel { Q_OBJECT; public: - TimelineModel (const ContactsListModel *contacts_list); + TimelineModel (QObject *parent = Q_NULLPTR); int rowCount (const QModelIndex &) const override; QHash roleNames () const override;