Commit 20bb5fbb authored by Aron Helser's avatar Aron Helser
Browse files

Add a optional search bar to qtResourceBrowser.

The search bar is added and a `QSortFilterProxyModel` is used if
the "SearchBar" flag is set in the configuration attributes.
parent 2656eae5
......@@ -7,7 +7,8 @@
"Attributes": {
"Title": "Resources",
"TopLevel": true,
"Widget": "ResourceTree"
"Widget": "ResourceTree",
"SearchBar": true
},
"Children": [
{
......
......@@ -75,6 +75,7 @@ qtResourceBrowser::qtResourceBrowser(const smtk::view::Information& info)
QAbstractItemModel* qtPhraseModel = nullptr;
smtk::view::SelectionPtr selection;
const auto& view = this->configuration();
bool searchBar = false;
if (view)
{
// empty Widget attribute is OK, will use default.
......@@ -83,8 +84,10 @@ qtResourceBrowser::qtResourceBrowser(const smtk::view::Information& info)
phraseModel = manager->phraseModelFactory().createFromConfiguration(view.get());
qtPhraseModel = new smtk::extension::qtDescriptivePhraseModel;
selection = this->uiManager()->selection();
searchBar = view->details().attributeAsBool("SearchBar");
}
m_p->setup(this, phraseModel, modelViewType, qtPhraseModel, this->parentWidget(), selection);
m_p->setup(
this, phraseModel, modelViewType, qtPhraseModel, this->parentWidget(), selection, searchBar);
this->Widget = m_p->m_container;
}
......
......@@ -18,7 +18,9 @@
#include "smtk/io/Logger.h"
#include <QAbstractProxyModel>
#include <QLineEdit>
#include <QMetaObject>
#include <QSortFilterProxyModel>
#include <QTreeView>
#include <QVBoxLayout>
......@@ -51,7 +53,8 @@ void qtResourceBrowser::Internal::setup(
const std::string& viewName,
QAbstractItemModel* qmodel,
QWidget* parent,
const std::shared_ptr<smtk::view::Selection>& selection)
const std::shared_ptr<smtk::view::Selection>& selection,
bool searchBar)
{
m_self = self;
if (m_container)
......@@ -80,6 +83,19 @@ void qtResourceBrowser::Internal::setup(
m_container->setObjectName("qtResourceBrowser");
m_layout = new QVBoxLayout(m_container);
m_layout->setObjectName("m_layout");
if (searchBar)
{
m_filter = new QSortFilterProxyModel(m_container);
m_filter->setFilterCaseSensitivity(Qt::CaseInsensitive);
m_filter->setSortCaseSensitivity(Qt::CaseInsensitive);
m_search = new QLineEdit(m_container);
m_search->setObjectName("Search");
m_search->setPlaceholderText("Search");
m_layout->addWidget(m_search);
QObject::connect(
m_search, &QLineEdit::textChanged, m_filter, &QSortFilterProxyModel::setFilterWildcard);
}
m_view = ctor(parent);
......@@ -110,7 +126,15 @@ void qtResourceBrowser::Internal::setup(
m_delegate->setDrawSubtitle(false);
m_delegate->setSelection(selection); // Used for badge actions
m_view->setModel(m_model);
if (searchBar)
{
m_filter->setSourceModel(m_model);
m_view->setModel(m_filter);
}
else
{
m_view->setModel(m_model);
}
m_view->setItemDelegate(m_delegate);
m_view->setMouseTracking(true); // Needed to receive hover events.
// Connect signals
......@@ -140,7 +164,15 @@ qtDescriptivePhraseModel* qtResourceBrowser::Internal::descriptivePhraseModel()
void qtResourceBrowser::Internal::setDescriptivePhraseModel(QAbstractItemModel* qmodel)
{
m_model = qmodel;
m_view->setModel(m_model);
if (m_filter)
{
m_filter->setSourceModel(m_model);
m_view->setModel(m_filter);
}
else
{
m_view->setModel(m_model);
}
QObject::connect(
m_view->selectionModel(),
SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
......
......@@ -19,6 +19,8 @@
class QAbstractItemModel;
class QAbstractItemView;
class QLineEdit;
class QSortFilterProxyModel;
class QTreeView;
class QVBoxLayout;
......@@ -43,7 +45,8 @@ public:
QAbstractItemModel* qmodel,
QWidget* parent,
const std::shared_ptr<smtk::view::Selection>& selection =
std::shared_ptr<smtk::view::Selection>());
std::shared_ptr<smtk::view::Selection>(),
bool searchBar = false);
smtk::extension::qtDescriptivePhraseModel* descriptivePhraseModel() const;
void setDescriptivePhraseModel(QAbstractItemModel* qmodel);
......@@ -66,6 +69,10 @@ public:
std::string m_viewName;
std::string m_resourceTreeType; // "default" or specific type.
int m_resourceTreeStyle{ -1 }; // Which default subphrase generator should be used?
/// A proxy model for users to search by name.
QPointer<QSortFilterProxyModel> m_filter;
/// An optional search bar for users.
QPointer<QLineEdit> m_search;
// Set to true when inside sendSMTKSelectionToPanel.
// Used to avoid updating the SMTK selection from the panel while
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment