From 17fece804b48ebc1e74b98b29590f7bcd75151fb Mon Sep 17 00:00:00 2001 From: Eberhard Graether Date: Sat, 6 Aug 2016 16:39:43 +0200 Subject: [PATCH] ui: Tab in search completes up to next ::, Delete erases to last :: bug id = 133 --- src/lib/data/HierarchyCache.cpp | 11 +++++ src/lib/data/HierarchyCache.h | 1 + src/lib/data/PersistentStorage.cpp | 5 +++ src/lib/data/search/SearchMatch.cpp | 2 + src/lib/data/search/SearchMatch.h | 2 + src/lib_gui/qt/element/QtSmartSearchBox.cpp | 47 +++++++++++++++++++-- src/lib_gui/qt/element/QtSmartSearchBox.h | 4 +- 7 files changed, 67 insertions(+), 5 deletions(-) diff --git a/src/lib/data/HierarchyCache.cpp b/src/lib/data/HierarchyCache.cpp index 649c141f..8511cb2e 100644 --- a/src/lib/data/HierarchyCache.cpp +++ b/src/lib/data/HierarchyCache.cpp @@ -177,6 +177,17 @@ bool HierarchyCache::isChildOfVisibleNodeOrInvisible(Id nodeId) const return false; } +bool HierarchyCache::nodeHasChildren(Id nodeId) const +{ + HierarchyNode* node = getNode(nodeId); + if (node) + { + return node->getChildren().size(); + } + + return false; +} + HierarchyCache::HierarchyNode* HierarchyCache::getNode(Id nodeId) const { std::map>::const_iterator it = m_nodes.find(nodeId); diff --git a/src/lib/data/HierarchyCache.h b/src/lib/data/HierarchyCache.h index 76a2ad73..ee45ba18 100644 --- a/src/lib/data/HierarchyCache.h +++ b/src/lib/data/HierarchyCache.h @@ -21,6 +21,7 @@ public: void addFirstVisibleChildIdsForNodeId(Id nodeId, std::vector* nodeIds) const; bool isChildOfVisibleNodeOrInvisible(Id nodeId) const; + bool nodeHasChildren(Id nodeId) const; private: class HierarchyNode diff --git a/src/lib/data/PersistentStorage.cpp b/src/lib/data/PersistentStorage.cpp index 1a1bf90a..37f51e3c 100644 --- a/src/lib/data/PersistentStorage.cpp +++ b/src/lib/data/PersistentStorage.cpp @@ -589,6 +589,11 @@ std::vector PersistentStorage::getAutocompletionMatches(const std:: const StorageNode& node = storageNodesMap[elementId]; match.nameHierarchies.push_back(NameHierarchy::deserialize(node.serializedName)); + if (!match.hasChildren) + { + match.hasChildren = m_hierarchyCache.nodeHasChildren(node.id); + } + if (!firstNode) { firstNode = &node; diff --git a/src/lib/data/search/SearchMatch.cpp b/src/lib/data/search/SearchMatch.cpp index 19429ddf..61f1df30 100644 --- a/src/lib/data/search/SearchMatch.cpp +++ b/src/lib/data/search/SearchMatch.cpp @@ -91,6 +91,7 @@ SearchMatch::CommandType SearchMatch::getCommandType(const std::string& name) SearchMatch::SearchMatch() : typeName("") , searchType(SEARCH_NONE) + , hasChildren(false) { } @@ -98,6 +99,7 @@ SearchMatch::SearchMatch(const std::string& query) : text(query) , typeName("") , searchType(SEARCH_NONE) + , hasChildren(false) { } diff --git a/src/lib/data/search/SearchMatch.h b/src/lib/data/search/SearchMatch.h index 326bc0ef..b8768a99 100644 --- a/src/lib/data/search/SearchMatch.h +++ b/src/lib/data/search/SearchMatch.h @@ -56,6 +56,8 @@ struct SearchMatch std::vector indices; std::vector nameHierarchies; + + bool hasChildren; }; diff --git a/src/lib_gui/qt/element/QtSmartSearchBox.cpp b/src/lib_gui/qt/element/QtSmartSearchBox.cpp index 9c0937cd..696df644 100644 --- a/src/lib_gui/qt/element/QtSmartSearchBox.cpp +++ b/src/lib_gui/qt/element/QtSmartSearchBox.cpp @@ -17,6 +17,8 @@ #include "component/view/GraphViewStyle.h" #include "settings/ColorScheme.h" +std::string QtSmartSearchBox::s_delimiter = "::"; + QtSearchElement::QtSearchElement(const QString& text, QWidget* parent) : QPushButton(text, parent) { @@ -144,7 +146,16 @@ bool QtSmartSearchBox::event(QEvent *event) { if (m_completer->popup()->isVisible()) { - addMatchAndUpdate(m_highlightedMatch); + if (m_highlightedMatch.hasChildren) + { + setEditText((m_highlightedMatch.text + s_delimiter).c_str()); + } + else + { + setEditText(m_highlightedMatch.text.c_str()); + } + + requestAutoCompletions(); } else if (m_allowMultipleElements) { @@ -193,7 +204,7 @@ void QtSmartSearchBox::keyPressEvent(QKeyEvent* event) return; } } - else if (event->matches(QKeySequence::Delete)) + else if (event->matches(QKeySequence::Delete) || event->matches(QKeySequence::DeleteStartOfWord)) { if (hasSelectedElements()) { @@ -206,6 +217,27 @@ void QtSmartSearchBox::keyPressEvent(QKeyEvent* event) deleteSelectedElements(); return; } + else + { + std::vector names = utility::splitToVector(text().toStdString(), s_delimiter); + if (names.back() == "") + { + names.pop_back(); + } + + if (names.size() < 2) + { + setEditText(""); + } + else + { + names.back() = ""; + setEditText(utility::join(names, s_delimiter).c_str()); + } + + requestAutoCompletions(); + return; + } } else if (event->matches(QKeySequence::MoveToPreviousChar)) { @@ -863,9 +895,16 @@ void QtSmartSearchBox::clearLineEdit() hideAutoCompletions(); } -void QtSmartSearchBox::requestAutoCompletions() const +void QtSmartSearchBox::requestAutoCompletions() { - MessageSearchAutocomplete(text().toStdString()).dispatch(); + if (text().size()) + { + MessageSearchAutocomplete(text().toStdString()).dispatch(); + } + else + { + hideAutoCompletions(); + } } void QtSmartSearchBox::hideAutoCompletions() diff --git a/src/lib_gui/qt/element/QtSmartSearchBox.h b/src/lib_gui/qt/element/QtSmartSearchBox.h index f4cebceb..f971d6c0 100644 --- a/src/lib_gui/qt/element/QtSmartSearchBox.h +++ b/src/lib_gui/qt/element/QtSmartSearchBox.h @@ -66,6 +66,8 @@ private slots: void onElementSelected(QtSearchElement* element); private: + static std::string s_delimiter; + void moveCursor(int offset); void moveCursorTo(int goal); @@ -90,7 +92,7 @@ private: void updatePlaceholder(); void clearLineEdit(); - void requestAutoCompletions() const; + void requestAutoCompletions(); void hideAutoCompletions(); std::deque getMatchesForInput(const std::string& text) const;