From 0e3f59402840cc5a63d48a40110605d2d3ac35f4 Mon Sep 17 00:00:00 2001 From: iaom Date: Wed, 12 Jan 2022 18:01:59 +0800 Subject: [PATCH] Optimized search interface frame, add some annotation. --- libsearch/pluginmanage/search-task-plugin-manager.h | 1 + libsearch/searchinterface/result-item.cpp | 2 +- .../searchinterface/search-controller-private.h | 2 ++ libsearch/searchinterface/search-controller.cpp | 12 +++++++++--- libsearch/searchinterface/search-controller.h | 5 +++++ .../searchinterface/searchtasks/file-search-task.h | 3 +++ libsearch/searchinterface/ukui-search-task.cpp | 4 ++-- libsearch/searchinterface/ukui-search-task.h | 2 +- 8 files changed, 24 insertions(+), 7 deletions(-) diff --git a/libsearch/pluginmanage/search-task-plugin-manager.h b/libsearch/pluginmanage/search-task-plugin-manager.h index dcee0bb..0435b21 100644 --- a/libsearch/pluginmanage/search-task-plugin-manager.h +++ b/libsearch/pluginmanage/search-task-plugin-manager.h @@ -20,6 +20,7 @@ Q_SIGNALS: private: explicit SearchTaskPluginManager(QObject *parent = nullptr); + //这里初衷是把内外部插件分开管理,内部插件可以增加枚举值,外部插件似乎只能用编写者自定义的字符串区分? QHash m_buildinPlugin; QHash m_loadedPlugin; diff --git a/libsearch/searchinterface/result-item.cpp b/libsearch/searchinterface/result-item.cpp index a7b9e34..5910a50 100644 --- a/libsearch/searchinterface/result-item.cpp +++ b/libsearch/searchinterface/result-item.cpp @@ -33,7 +33,7 @@ ResultItem::ResultItem(const QString itemKey) : d(new ResultItemPrivate(this)) d->setItemKey(itemKey); } -ResultItem::ResultItem(const size_t searchId, const QString itemKey) +ResultItem::ResultItem(const size_t searchId, const QString itemKey) : d(new ResultItemPrivate(this)) { d->setSearchId(searchId); d->setItemKey(itemKey); diff --git a/libsearch/searchinterface/search-controller-private.h b/libsearch/searchinterface/search-controller-private.h index 706af3a..7fae89c 100644 --- a/libsearch/searchinterface/search-controller-private.h +++ b/libsearch/searchinterface/search-controller-private.h @@ -37,7 +37,9 @@ public: private: void copyData(); + //TODO: 这里是否可以改为字节对齐的写法? DataQueue* m_dataQueue = nullptr ; + std::shared_ptr> m_sharedDataueue = nullptr; size_t m_searchId = 0; QMutex m_searchIdMutex; SearchController *q = nullptr; diff --git a/libsearch/searchinterface/search-controller.cpp b/libsearch/searchinterface/search-controller.cpp index 3d840ec..aa3d47a 100644 --- a/libsearch/searchinterface/search-controller.cpp +++ b/libsearch/searchinterface/search-controller.cpp @@ -4,8 +4,10 @@ #include using namespace UkuiSearch; SearchControllerPrivate::SearchControllerPrivate(SearchController *parent) - : q(parent), m_formerController(q->m_parent) + : q(parent), + m_formerController(q->m_parent) //如果构造参数里包含父节点智能指针,则子节点带有一个智能指针指向父节点 { + copyData(); } SearchControllerPrivate::~SearchControllerPrivate() @@ -24,6 +26,7 @@ DataQueue *SearchControllerPrivate::refreshDataqueue() { if(!m_dataQueue) { m_dataQueue = new DataQueue; + m_sharedDataueue = std::shared_ptr>(m_dataQueue); return m_dataQueue; } m_dataQueue->clear(); @@ -34,6 +37,7 @@ DataQueue *SearchControllerPrivate::initDataQueue() { if(!m_dataQueue) { m_dataQueue = new DataQueue; + m_sharedDataueue = std::shared_ptr>(m_dataQueue); return m_dataQueue; } return m_dataQueue; @@ -81,7 +85,7 @@ size_t SearchControllerPrivate::getCurrentSearchId() DataQueue *SearchControllerPrivate::getDataQueue() { - return m_dataQueue; + return m_sharedDataueue.get(); } bool SearchControllerPrivate::beginSearchIdCheck(size_t searchId) @@ -146,9 +150,11 @@ bool SearchControllerPrivate::isSearchDirOnly() void SearchControllerPrivate::copyData() { + if(m_formerController.get()) { m_searchId = m_formerController.get()->getCurrentSearchId(); - m_dataQueue = m_formerController.get()->getDataQueue(); + //所有子节点都有一个指向根节点的队列的智能指针 + m_sharedDataueue = m_formerController.get()->d->m_sharedDataueue; m_keywords = m_formerController.get()->getKeyword(); m_searchDirs = m_formerController.get()->getSearchDir(); m_FileLabels = m_formerController.get()->getFileLabel(); diff --git a/libsearch/searchinterface/search-controller.h b/libsearch/searchinterface/search-controller.h index 0efc16e..866dbad 100644 --- a/libsearch/searchinterface/search-controller.h +++ b/libsearch/searchinterface/search-controller.h @@ -9,6 +9,11 @@ namespace UkuiSearch { class UkuiSearchTask; class ResultItem; class SearchControllerPrivate; +/* + *搜索控制,用于传递搜索条件,搜索唯一ID,以及管理队列等。 + *为树形结构,所有子节点含有智能指针指向父节点。 + *只要有插件还在使用资源,其对应的子节点以及通向根节点上所有的实例就不会被释放,析构时,从下往上由智能指针自动析构 + */ class SearchController { friend class SearchControllerPrivate; diff --git a/libsearch/searchinterface/searchtasks/file-search-task.h b/libsearch/searchinterface/searchtasks/file-search-task.h index a83d610..5323b13 100644 --- a/libsearch/searchinterface/searchtasks/file-search-task.h +++ b/libsearch/searchinterface/searchtasks/file-search-task.h @@ -8,6 +8,9 @@ #include "search-controller.h" #include "result-item.h" namespace UkuiSearch { +/* + * 这里只写了大概框架,具体逻辑未实现,可以当成伪代码参考。 +*/ class FileSearchTask : public SearchTaskPluginIface { Q_OBJECT diff --git a/libsearch/searchinterface/ukui-search-task.cpp b/libsearch/searchinterface/ukui-search-task.cpp index 7cc6179..acdd2b0 100644 --- a/libsearch/searchinterface/ukui-search-task.cpp +++ b/libsearch/searchinterface/ukui-search-task.cpp @@ -127,9 +127,9 @@ void UkuiSearchTask::setSearchOnlineApps(bool searchOnlineApps) d->setSearchOnlineApps(searchOnlineApps); } -size_t UkuiSearchTask::startSearch(SearchType searchtype, QString customSearchTYpe) +size_t UkuiSearchTask::startSearch(SearchType searchtype, QString customSearchType) { - return d->startSearch(searchtype, customSearchTYpe); + return d->startSearch(searchtype, customSearchType); } void UkuiSearchTask::stop() diff --git a/libsearch/searchinterface/ukui-search-task.h b/libsearch/searchinterface/ukui-search-task.h index ad15a42..6f6fc9e 100644 --- a/libsearch/searchinterface/ukui-search-task.h +++ b/libsearch/searchinterface/ukui-search-task.h @@ -20,7 +20,7 @@ public: void setOnlySearchFile(bool onlySearchFile); void setOnlySearchDir(bool onlySearchDir); void setSearchOnlineApps(bool searchOnlineApps); - size_t startSearch(SearchType searchtype, QString customSearchTYpe = QString()); + size_t startSearch(SearchType searchtype, QString customSearchType = QString()); void stop(); Q_SIGNALS: