From f6205c6b934a23a4c319c75ed079d79a3b3538e6 Mon Sep 17 00:00:00 2001 From: Yorik van Havre Date: Wed, 14 Nov 2018 15:11:54 -0200 Subject: [PATCH] Selection view: only select search results when enter is pressed --- src/Gui/SelectionView.cpp | 51 +++++++++++++++++++++++++++++++-------- src/Gui/SelectionView.h | 3 +++ 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/src/Gui/SelectionView.cpp b/src/Gui/SelectionView.cpp index 7a602c2449..471dcbc00b 100644 --- a/src/Gui/SelectionView.cpp +++ b/src/Gui/SelectionView.cpp @@ -89,9 +89,10 @@ SelectionView::SelectionView(Gui::Document* pcDocument, QWidget *parent) connect(clearButton, SIGNAL(clicked()), searchBox, SLOT(clear())); connect(searchBox, SIGNAL(textChanged(QString)), this, SLOT(search(QString))); + connect(searchBox, SIGNAL(editingFinished()), this, SLOT(validateSearch())); connect(selectionView, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(select(QListWidgetItem*))); connect(selectionView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(onItemContextMenu(QPoint))); - + Gui::Selection().Attach(this); } @@ -104,7 +105,7 @@ SelectionView::~SelectionView() void SelectionView::OnChange(Gui::SelectionSingleton::SubjectType &rCaller, Gui::SelectionSingleton::MessageType Reason) { - Q_UNUSED(rCaller); + Q_UNUSED(rCaller); QString selObject; QTextStream str(&selObject); if (Reason.Type == SelectionChanges::AddSelection) { @@ -128,7 +129,7 @@ void SelectionView::OnChange(Gui::SelectionSingleton::SubjectType &rCaller, str << " ("; str << QString::fromUtf8(obj->Label.getValue()); str << ")"; - + QListWidgetItem* item = new QListWidgetItem(selObject, selectionView); item->setData(Qt::UserRole, list); } @@ -182,7 +183,7 @@ void SelectionView::OnChange(Gui::SelectionSingleton::SubjectType &rCaller, str << " ("; str << QString::fromUtf8(obj->Label.getValue()); str << ")"; - + QListWidgetItem* item = new QListWidgetItem(selObject, selectionView); item->setData(Qt::UserRole, list); selObject.clear(); @@ -195,17 +196,47 @@ void SelectionView::OnChange(Gui::SelectionSingleton::SubjectType &rCaller, void SelectionView::search(const QString& text) { if (!text.isEmpty()) { + searchList.clear(); App::Document* doc = App::GetApplication().getActiveDocument(); std::vector objects; if (doc) { - Gui::Selection().clearSelection(); objects = doc->getObjects(); + selectionView->clear(); for (std::vector::iterator it = objects.begin(); it != objects.end(); ++it) { QString label = QString::fromUtf8((*it)->Label.getValue()); if (label.contains(text,Qt::CaseInsensitive)) { - if (!Gui::Selection().hasSelection((*it)->getNameInDocument())) { - Gui::Selection().addSelection(doc->getName(),(*it)->getNameInDocument(),0); - } + searchList.push_back(*it); + // save as user data + QString selObject; + QTextStream str(&selObject); + QStringList list; + list << QString::fromLatin1(doc->getName()); + list << QString::fromLatin1((*it)->getNameInDocument()); + // build name + str << doc->getName(); + str << "."; + str << (*it)->getNameInDocument(); + str << " ("; + str << label; + str << ")"; + QListWidgetItem* item = new QListWidgetItem(selObject, selectionView); + item->setData(Qt::UserRole, list); + } + } + countLabel->setText(QString::number(selectionView->count())); + } + } +} + +void SelectionView::validateSearch(void) +{ + if (!searchList.empty()) { + App::Document* doc = App::GetApplication().getActiveDocument(); + if (doc) { + Gui::Selection().clearSelection(); + for (std::vector::iterator it = searchList.begin(); it != searchList.end(); ++it) { + if (!Gui::Selection().hasSelection((*it)->getNameInDocument())) { + Gui::Selection().addSelection(doc->getName(),(*it)->getNameInDocument(),0); } } } @@ -246,13 +277,13 @@ void SelectionView::deselect(void) void SelectionView::zoom(void) { select(); - Gui::Command::runCommand(Gui::Command::Gui,"Gui.SendMsgToActiveView(\"ViewSelection\")"); + Gui::Command::runCommand(Gui::Command::Gui,"Gui.SendMsgToActiveView(\"ViewSelection\")"); } void SelectionView::treeSelect(void) { select(); - Gui::Command::runCommand(Gui::Command::Gui,"Gui.runCommand(\"Std_TreeSelection\")"); + Gui::Command::runCommand(Gui::Command::Gui,"Gui.runCommand(\"Std_TreeSelection\")"); } void SelectionView::touch(void) diff --git a/src/Gui/SelectionView.h b/src/Gui/SelectionView.h index ff10405532..108a94e565 100644 --- a/src/Gui/SelectionView.h +++ b/src/Gui/SelectionView.h @@ -77,6 +77,8 @@ public: public Q_SLOTS: /// get called when text is entered in the search box void search(const QString& text); + /// get called when enter is pressed in the search box + void validateSearch(void); /// get called when the list is right-clicked void onItemContextMenu(const QPoint& point); /// different actions @@ -92,6 +94,7 @@ private: QString getModule(const char* type) const; QString getProperty(App::DocumentObject* obj) const; bool supportPart(App::DocumentObject* obj, const QString& part) const; + std::vector searchList; }; } // namespace DockWnd