From bc93f24a22ca18232bec27134eedeb1316fb5e88 Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Wed, 25 Nov 2020 02:10:09 +0100 Subject: [PATCH] Remove redundant user search suggestion code --- src/Cache.cpp | 42 ------------------- src/Cache.h | 2 - src/CacheStructs.h | 6 --- src/Cache_p.h | 3 -- src/QuickSwitcher.cpp | 2 +- src/popups/SuggestionsPopup.cpp | 73 ++++++++++++++++----------------- src/popups/SuggestionsPopup.h | 36 +++------------- 7 files changed, 42 insertions(+), 122 deletions(-) diff --git a/src/Cache.cpp b/src/Cache.cpp index b37f69b3..3a6b0ca9 100644 --- a/src/Cache.cpp +++ b/src/Cache.cpp @@ -85,8 +85,6 @@ constexpr auto OUTBOUND_MEGOLM_SESSIONS_DB("outbound_megolm_sessions"); using CachedReceipts = std::multimap>; using Receipts = std::map>; -Q_DECLARE_METATYPE(SearchResult) -Q_DECLARE_METATYPE(std::vector) Q_DECLARE_METATYPE(RoomMember) Q_DECLARE_METATYPE(mtx::responses::Timeline) Q_DECLARE_METATYPE(RoomSearchResult) @@ -2334,39 +2332,6 @@ Cache::searchRooms(const std::string &query, std::uint8_t max_items) return results; } -std::vector -Cache::searchUsers(const std::string &room_id, const std::string &query, std::uint8_t max_items) -{ - std::multimap> items; - - auto txn = lmdb::txn::begin(env_, nullptr, MDB_RDONLY); - auto cursor = lmdb::cursor::open(txn, getMembersDb(txn, room_id)); - - std::string user_id, user_data; - while (cursor.get(user_id, user_data, MDB_NEXT)) { - const auto display_name = displayName(room_id, user_id); - const int score = utils::levenshtein_distance(query, display_name); - - items.emplace(score, std::make_pair(user_id, display_name)); - } - - auto end = items.begin(); - - if (items.size() >= max_items) - std::advance(end, max_items); - else if (items.size() > 0) - std::advance(end, items.size()); - - std::vector results; - for (auto it = items.begin(); it != end; it++) { - const auto user = it->second; - results.push_back(SearchResult{QString::fromStdString(user.first), - QString::fromStdString(user.second)}); - } - - return results; -} - std::vector Cache::getMembers(const std::string &room_id, std::size_t startIndex, std::size_t len) { @@ -3762,8 +3727,6 @@ namespace cache { void init(const QString &user_id) { - qRegisterMetaType(); - qRegisterMetaType>(); qRegisterMetaType(); qRegisterMetaType(); qRegisterMetaType(); @@ -4075,11 +4038,6 @@ calculateRoomReadStatus() instance_->calculateRoomReadStatus(); } -std::vector -searchUsers(const std::string &room_id, const std::string &query, std::uint8_t max_items) -{ - return instance_->searchUsers(room_id, query, max_items); -} std::vector searchRooms(const std::string &query, std::uint8_t max_items) { diff --git a/src/Cache.h b/src/Cache.h index 4c4f7071..573ab700 100644 --- a/src/Cache.h +++ b/src/Cache.h @@ -194,8 +194,6 @@ calculateRoomReadStatus(const std::string &room_id); void calculateRoomReadStatus(); -std::vector -searchUsers(const std::string &room_id, const std::string &query, std::uint8_t max_items = 5); std::vector searchRooms(const std::string &query, std::uint8_t max_items = 5); diff --git a/src/CacheStructs.h b/src/CacheStructs.h index ef08cfcb..b3d9acf1 100644 --- a/src/CacheStructs.h +++ b/src/CacheStructs.h @@ -24,12 +24,6 @@ struct RoomMember QImage avatar; }; -struct SearchResult -{ - QString user_id; - QString display_name; -}; - //! Used to uniquely identify a list of read receipts. struct ReadReceiptKey { diff --git a/src/Cache_p.h b/src/Cache_p.h index 05e13128..ffa7d509 100644 --- a/src/Cache_p.h +++ b/src/Cache_p.h @@ -164,9 +164,6 @@ public: bool calculateRoomReadStatus(const std::string &room_id); void calculateRoomReadStatus(); - std::vector searchUsers(const std::string &room_id, - const std::string &query, - std::uint8_t max_items = 5); std::vector searchRooms(const std::string &query, std::uint8_t max_items = 5); diff --git a/src/QuickSwitcher.cpp b/src/QuickSwitcher.cpp index 05a9f431..046c774d 100644 --- a/src/QuickSwitcher.cpp +++ b/src/QuickSwitcher.cpp @@ -118,7 +118,7 @@ QuickSwitcher::QuickSwitcher(QWidget *parent) connect(roomSearch_, &RoomSearchInput::hiding, this, [this]() { popup_.hide(); }); connect(roomSearch_, &QLineEdit::returnPressed, this, [this]() { reset(); - popup_.selectHoveredSuggestion(); + popup_.selectHoveredSuggestion(); }); } diff --git a/src/popups/SuggestionsPopup.cpp b/src/popups/SuggestionsPopup.cpp index e84435b7..2650b5e6 100644 --- a/src/popups/SuggestionsPopup.cpp +++ b/src/popups/SuggestionsPopup.cpp @@ -6,6 +6,8 @@ #include "../Utils.h" #include "../ui/Avatar.h" #include "../ui/DropShadow.h" +#include "ChatPage.h" +#include "PopupItem.h" #include "SuggestionsPopup.h" SuggestionsPopup::SuggestionsPopup(QWidget *parent) @@ -65,44 +67,6 @@ SuggestionsPopup::addRooms(const std::vector &rooms) selectNextSuggestion(); } -void -SuggestionsPopup::addUsers(const std::vector &users) -{ - if (users.empty()) { - hide(); - return; - } - - const size_t layoutCount = layout_->count(); - const size_t userCount = users.size(); - - // Remove the extra widgets from the layout. - if (userCount < layoutCount) - removeLayoutItemsAfter(userCount - 1); - - for (size_t i = 0; i < userCount; ++i) { - auto item = layout_->itemAt(i); - - // Create a new widget if there isn't already one in that - // layout position. - if (!item) { - auto user = new UserItem(this, users.at(i).user_id); - connect(user, &UserItem::clicked, this, &SuggestionsPopup::itemSelected); - layout_->addWidget(user); - } else { - // Update the current widget with the new data. - auto userWidget = qobject_cast(item->widget()); - if (userWidget) - userWidget->updateItem(users.at(i).user_id); - } - } - - resetSelection(); - adjustSize(); - - selectNextSuggestion(); -} - void SuggestionsPopup::hoverSelection() { @@ -111,6 +75,19 @@ SuggestionsPopup::hoverSelection() update(); } +void +SuggestionsPopup::selectHoveredSuggestion() +{ + const auto item = layout_->itemAt(selectedItem_); + if (!item) + return; + + const auto &widget = qobject_cast(item->widget()); + emit itemSelected(displayName(ChatPage::instance()->currentRoom(), widget->selectedText())); + + resetSelection(); +} + void SuggestionsPopup::selectNextSuggestion() { @@ -160,3 +137,23 @@ SuggestionsPopup::paintEvent(QPaintEvent *) QPainter p(this); style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); } + +void +SuggestionsPopup::selectLastItem() +{ + selectedItem_ = layout_->count() - 1; +} + +void +SuggestionsPopup::removeLayoutItemsAfter(size_t startingPos) +{ + size_t posToRemove = layout_->count() - 1; + + QLayoutItem *item; + while (startingPos <= posToRemove && (item = layout_->takeAt(posToRemove)) != nullptr) { + delete item->widget(); + delete item; + + posToRemove = layout_->count() - 1; + } +} diff --git a/src/popups/SuggestionsPopup.h b/src/popups/SuggestionsPopup.h index c66f2903..6a0157a1 100644 --- a/src/popups/SuggestionsPopup.h +++ b/src/popups/SuggestionsPopup.h @@ -3,8 +3,9 @@ #include #include "CacheStructs.h" -#include "ChatPage.h" -#include "PopupItem.h" + +class QVBoxLayout; +class QLayoutItem; class SuggestionsPopup : public QWidget { @@ -13,22 +14,9 @@ class SuggestionsPopup : public QWidget public: explicit SuggestionsPopup(QWidget *parent = nullptr); - template - void selectHoveredSuggestion() - { - const auto item = layout_->itemAt(selectedItem_); - if (!item) - return; - - const auto &widget = qobject_cast(item->widget()); - emit itemSelected( - displayName(ChatPage::instance()->currentRoom(), widget->selectedText())); - - resetSelection(); - } + void selectHoveredSuggestion(); public slots: - void addUsers(const std::vector &users); void addRooms(const std::vector &rooms); //! Move to the next available suggestion item. @@ -51,20 +39,8 @@ private: void hoverSelection(); void resetSelection() { selectedItem_ = -1; } void selectFirstItem() { selectedItem_ = 0; } - void selectLastItem() { selectedItem_ = layout_->count() - 1; } - void removeLayoutItemsAfter(size_t startingPos) - { - size_t posToRemove = layout_->count() - 1; - - QLayoutItem *item; - while (startingPos <= posToRemove && - (item = layout_->takeAt(posToRemove)) != nullptr) { - delete item->widget(); - delete item; - - posToRemove = layout_->count() - 1; - } - } + void selectLastItem(); + void removeLayoutItemsAfter(size_t startingPos); QVBoxLayout *layout_;