Move the BookmarkFilter's matching into the main thread

We can't operate directly on the BookmarkManager and Bookmark instances
in the other thread, as they may be freely modified in the gui
thread in the same time. Since it's uncommon to have
hundreds of bookmarks set, we prepare all the needed data in the
main thread instead, which shouldn't slow down the locator
significantly.

Change-Id: I9df0a8425694632c40138fbe3667499f7fc03432
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
Jarek Kobus
2021-01-29 11:41:53 +01:00
parent 64fc8a1fb6
commit 9c646d55cd
2 changed files with 14 additions and 7 deletions

View File

@@ -43,12 +43,11 @@ BookmarkFilter::BookmarkFilter(BookmarkManager *manager)
setShortcutString("b"); setShortcutString("b");
} }
QList<LocatorFilterEntry> BookmarkFilter::matchesFor(QFutureInterface<LocatorFilterEntry> &future, void BookmarkFilter::prepareSearch(const QString &entry)
const QString &entry)
{ {
Q_UNUSED(future) m_results = {};
if (m_manager->rowCount() == 0) if (m_manager->rowCount() == 0)
return QList<LocatorFilterEntry>(); return;
auto match = [this](const QString &name, BookmarkManager::Roles role) { auto match = [this](const QString &name, BookmarkManager::Roles role) {
return m_manager->match(m_manager->index(0, 0), role, name, -1, return m_manager->match(m_manager->index(0, 0), role, name, -1,
@@ -74,7 +73,6 @@ QList<LocatorFilterEntry> BookmarkFilter::matchesFor(QFutureInterface<LocatorFil
+ match(entry, BookmarkManager::Note) + match(entry, BookmarkManager::Note)
+ match(entry, BookmarkManager::LineText)); + match(entry, BookmarkManager::LineText));
QList<LocatorFilterEntry> entries;
for (const QModelIndex &idx : matches) { for (const QModelIndex &idx : matches) {
const Bookmark *bookmark = m_manager->bookmarkForIndex(idx); const Bookmark *bookmark = m_manager->bookmarkForIndex(idx);
const QString filename = bookmark->fileName().fileName(); const QString filename = bookmark->fileName().fileName();
@@ -112,9 +110,16 @@ QList<LocatorFilterEntry> BookmarkFilter::matchesFor(QFutureInterface<LocatorFil
} }
filterEntry.displayIcon = bookmark->icon(); filterEntry.displayIcon = bookmark->icon();
entries.append(filterEntry); m_results.append(filterEntry);
} }
return entries; }
QList<LocatorFilterEntry> BookmarkFilter::matchesFor(QFutureInterface<LocatorFilterEntry> &future,
const QString &entry)
{
Q_UNUSED(future)
Q_UNUSED(entry)
return m_results;
} }
void BookmarkFilter::accept(LocatorFilterEntry selection, QString *newText, void BookmarkFilter::accept(LocatorFilterEntry selection, QString *newText,

View File

@@ -37,6 +37,7 @@ class BookmarkFilter : public Core::ILocatorFilter
Q_OBJECT Q_OBJECT
public: public:
explicit BookmarkFilter(BookmarkManager *manager); explicit BookmarkFilter(BookmarkManager *manager);
void prepareSearch(const QString &entry) override;
QList<Core::LocatorFilterEntry> matchesFor(QFutureInterface<Core::LocatorFilterEntry> &future, QList<Core::LocatorFilterEntry> matchesFor(QFutureInterface<Core::LocatorFilterEntry> &future,
const QString &entry) override; const QString &entry) override;
void accept(Core::LocatorFilterEntry selection, QString *newText, void accept(Core::LocatorFilterEntry selection, QString *newText,
@@ -45,6 +46,7 @@ public:
private: private:
BookmarkManager *m_manager = nullptr; // not owned BookmarkManager *m_manager = nullptr; // not owned
QList<Core::LocatorFilterEntry> m_results;
}; };
} // namespace Internal } // namespace Internal