Skip to content

Commit

Permalink
Gui: use ExpressionLineEdit for tree view object search
Browse files Browse the repository at this point in the history
Tree view is already using expression for object search. This patch
adds the expression completer feature for search result suggestion.
  • Loading branch information
realthunder committed Aug 22, 2019
1 parent 939d5f6 commit b392ba1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
48 changes: 32 additions & 16 deletions src/Gui/Tree.cpp
Expand Up @@ -63,6 +63,7 @@
#include "Macro.h"
#include "Workbench.h"
#include "Widgets.h"
#include "ExpressionCompleter.h"

FC_LOG_LEVEL_INIT("Tree",false,true,true);

Expand Down Expand Up @@ -625,24 +626,30 @@ void TreeWidget::resetItemSearch() {
searchObject = 0;
}

void TreeWidget::startItemSearch() {
void TreeWidget::startItemSearch(QLineEdit *edit) {
resetItemSearch();
searchDoc = 0;
searchContextDoc = 0;
if (contextItem) {
if(contextItem->type() == DocumentType) {
searchDoc = static_cast<DocumentItem*>(contextItem)->document();
} else if(contextItem->type() == ObjectType) {
searchDoc = static_cast<DocumentObjectItem*>(contextItem)->object()->getDocument();
}
}else{
auto sels = selectedItems();
if(sels.size() == 1) {
auto sels = selectedItems();
if(sels.size() == 1) {
if(sels.front()->type() == DocumentType) {
searchDoc = static_cast<DocumentItem*>(sels.front())->document();
} else if(sels.front()->type() == ObjectType) {
auto item = static_cast<DocumentObjectItem*>(sels.front());
searchDoc = item->object()->getDocument();
searchContextDoc = item->getOwnerDocument()->document();
}
}
}else
searchDoc = Application::Instance->activeDocument();

App::DocumentObject *obj = 0;
if(searchContextDoc && searchContextDoc->getDocument()->getObjects().size())
obj = searchContextDoc->getDocument()->getObjects().front();
else if(searchDoc && searchDoc->getDocument()->getObjects().size())
obj = searchDoc->getDocument()->getObjects().front();

if(obj)
static_cast<ExpressionLineEdit*>(edit)->setDocumentObject(obj);
}

void TreeWidget::itemSearch(const QString &text, bool select) {
Expand Down Expand Up @@ -2701,8 +2708,10 @@ void TreeWidget::onItemSelectionChanged ()
else if(selItems.front()->type() == DocumentType) {
auto ditem = static_cast<DocumentItem*>(selItems.front());
if(FC_TREEPARAM(SyncView)) {
bool focus = hasFocus();
ditem->document()->setActiveView();
setFocus();
if(focus)
setFocus();
}
// For triggering property editor refresh
Gui::Selection().signalSelectionChanged(SelectionChanges());
Expand Down Expand Up @@ -2787,7 +2796,7 @@ TreePanel::TreePanel(const char *name, QWidget* parent)
connect(this->treeWidget, SIGNAL(emitSearchObjects()),
this, SLOT(showEditor()));

this->searchBox = new Gui::ClearLineEdit(this);
this->searchBox = new Gui::ExpressionLineEdit(this,true);
pLayout->addWidget(this->searchBox);
this->searchBox->hide();
this->searchBox->installEventFilter(this);
Expand All @@ -2796,7 +2805,7 @@ TreePanel::TreePanel(const char *name, QWidget* parent)
#endif
connect(this->searchBox, SIGNAL(returnPressed()),
this, SLOT(accept()));
connect(this->searchBox, SIGNAL(textEdited(QString)),
connect(this->searchBox, SIGNAL(textChanged(QString)),
this, SLOT(itemSearch(QString)));
}

Expand All @@ -2808,6 +2817,7 @@ void TreePanel::accept()
{
QString text = this->searchBox->text();
hideEditor();
this->treeWidget->setFocus();
this->treeWidget->itemSearch(text,true);
}

Expand All @@ -2823,6 +2833,7 @@ bool TreePanel::eventFilter(QObject *obj, QEvent *ev)
case Qt::Key_Escape:
hideEditor();
consumed = true;
treeWidget->setFocus();
break;

default:
Expand All @@ -2839,11 +2850,12 @@ void TreePanel::showEditor()
{
this->searchBox->show();
this->searchBox->setFocus();
this->treeWidget->startItemSearch();
this->treeWidget->startItemSearch(searchBox);
}

void TreePanel::hideEditor()
{
static_cast<ExpressionLineEdit*>(this->searchBox)->setDocumentObject(0);
this->searchBox->clear();
this->searchBox->hide();
this->treeWidget->resetItemSearch();
Expand Down Expand Up @@ -2913,8 +2925,12 @@ void TreeWidget::selectLinkedObject(App::DocumentObject *linked) {
if(linkedDoc->showItem(linkedItem,true))
scrollToItem(linkedItem);

if(linkedDoc->document()->getDocument() != App::GetApplication().getActiveDocument())
if(linkedDoc->document()->getDocument() != App::GetApplication().getActiveDocument()) {
bool focus = hasFocus();
linkedDoc->document()->setActiveView(linkedItem->object());
if(focus)
setFocus();
}
}

// ----------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion src/Gui/Tree.h
Expand Up @@ -122,7 +122,7 @@ class TreeWidget : public QTreeWidget, public SelectionObserver
void startDragging();

void resetItemSearch();
void startItemSearch();
void startItemSearch(QLineEdit*);
void itemSearch(const QString &text, bool select);

protected:
Expand Down

0 comments on commit b392ba1

Please sign in to comment.