Commit 78d92559 authored by finetjul's avatar finetjul

ENH: Add qMRMLSortFilterProxyModel::filterType

Can temporarily force the filtering of a qMRMLTreeView.
Character ! on tree view switches filtering mode in debug mode.

git-svn-id: http://svn.slicer.org/Slicer4/trunk@21556 3bd1e089-480b-0410-8dfb-8563597acbee
parent a80dfa4a
......@@ -46,6 +46,7 @@ public:
QString HideNodesUnaffiliatedWithNodeID;
typedef QPair<QString, QVariant> AttributeType;
QHash<QString, AttributeType> Attributes;
qMRMLSortFilterProxyModel::FilterType Filter;
};
// -----------------------------------------------------------------------------
......@@ -53,6 +54,7 @@ qMRMLSortFilterProxyModelPrivate::qMRMLSortFilterProxyModelPrivate()
{
this->ShowHidden = false;
this->ShowChildNodeTypes = true;
this->Filter = qMRMLSortFilterProxyModel::UseFilters;
}
// -----------------------------------------------------------------------------
......@@ -171,6 +173,14 @@ bool qMRMLSortFilterProxyModel::filterAcceptsRow(int source_row, const QModelInd
{
return true;
}
if (this->showAll())
{
return true;
}
if (this->hideAll())
{
return false;
}
if (d->HiddenNodeIDs.contains(node->GetID()))
{
return false;
......@@ -408,6 +418,67 @@ QString qMRMLSortFilterProxyModel::hideNodesUnaffiliatedWithNodeID()const
return d->HideNodesUnaffiliatedWithNodeID;
}
// --------------------------------------------------------------------------
void qMRMLSortFilterProxyModel
::setFilterType(FilterType filterType)
{
Q_D(qMRMLSortFilterProxyModel);
if (filterType == d->Filter)
{
return;
}
d->Filter = filterType;
this->invalidateFilter();
}
// --------------------------------------------------------------------------
qMRMLSortFilterProxyModel::FilterType qMRMLSortFilterProxyModel
::filterType()const
{
Q_D(const qMRMLSortFilterProxyModel);
return d->Filter;
}
// --------------------------------------------------------------------------
void qMRMLSortFilterProxyModel
::setShowAll(bool show)
{
if (show == this->showAll())
{
return;
}
this->setFilterType((show ? qMRMLSortFilterProxyModel::ShowAll :
(this->hideAll() ? qMRMLSortFilterProxyModel::HideAll :
qMRMLSortFilterProxyModel::UseFilters)));
}
// --------------------------------------------------------------------------
bool qMRMLSortFilterProxyModel::showAll()const
{
Q_D(const qMRMLSortFilterProxyModel);
return d->Filter == qMRMLSortFilterProxyModel::ShowAll;
}
// --------------------------------------------------------------------------
void qMRMLSortFilterProxyModel
::setHideAll(bool hide)
{
if (hide == this->hideAll())
{
return;
}
this->setFilterType((hide ? qMRMLSortFilterProxyModel::HideAll :
(this->showAll() ? qMRMLSortFilterProxyModel:: ShowAll :
qMRMLSortFilterProxyModel::UseFilters)));
}
// --------------------------------------------------------------------------
bool qMRMLSortFilterProxyModel::hideAll()const
{
Q_D(const qMRMLSortFilterProxyModel);
return d->Filter == qMRMLSortFilterProxyModel::HideAll;
}
// --------------------------------------------------------------------------
qMRMLSceneModel* qMRMLSortFilterProxyModel::sceneModel()const
{
......
......@@ -84,7 +84,31 @@ class QMRML_WIDGETS_EXPORT qMRMLSortFilterProxyModel : public QSortFilterProxyMo
/// hidden or not.
/// All the nodes are visible (empty string) by default.
Q_PROPERTY(QString hideNodesUnaffiliatedWithNodeID READ hideNodesUnaffiliatedWithNodeID WRITE setHideNodesUnaffiliatedWithNodeID)
/// This property controls whether the proxy applies its filter or if it
/// shows or hides all the nodes.
/// UseFilters by defaults.
/// \sa showAll, hideAll
Q_PROPERTY(FilterType filterType READ filterType WRITE setFilterType)
Q_ENUMS(FilterType)
/// This property controls whether all the nodes are visible or not,
/// bypassing any filter.
/// False by default
/// \sa filterType, hideAll
Q_PROPERTY(bool showAll READ showAll WRITE setShowAll STORED false)
/// This property controls whether all the nodes are hidden or not,
/// bypassing any filter.
/// False by default.
Q_PROPERTY(bool hideAll READ hideAll WRITE setHideAll STORED false)
public:
enum FilterType
{
HideAll = 0,
ShowAll,
UseFilters
};
typedef QSortFilterProxyModel Superclass;
qMRMLSortFilterProxyModel(QObject *parent=0);
virtual ~qMRMLSortFilterProxyModel();
......@@ -155,11 +179,31 @@ public:
/// \sa hideNodesUnaffiliatedWithNodeID, setHideNodesUnaffiliatedWithNodeID()
QString hideNodesUnaffiliatedWithNodeID()const;
/// Return the current filter type.
/// \sa filterType, setFilterType()
FilterType filterType()const;
/// Return true if all the nodes are visible.
/// \sa showAll, setShowAll()
bool showAll()const;
/// Return true if all the nodes are hidden
/// \sa hideAll, setHideAll()
bool hideAll()const;
/// Return the scene model used as input if any.
Q_INVOKABLE qMRMLSceneModel* sceneModel()const;
public slots:
void setShowHidden(bool);
void setShowHidden(bool);
/// Set the filter type.
/// \sa filterType, filterType()
void setFilterType(FilterType filterType);
/// Set whether all the nodes should be visible or not.
/// \sa showAll, showAll()
void setShowAll(bool show);
/// Set whether all the nodes should be hidden or not.
/// \sa hideAll, hideAll()
void setHideAll(bool hide);
// TODO Add setMRMLScene() to propagate to the scene model
protected:
......
......@@ -22,6 +22,7 @@
#include <QDebug>
#include <QHeaderView>
#include <QInputDialog>
#include <QKeyEvent>
#include <QMenu>
#include <QMouseEvent>
#include <QScrollBar>
......@@ -596,7 +597,6 @@ void qMRMLTreeView::setRootNode(vtkMRMLNode* rootNode)
}
qvtkReconnect(this->rootNode(), rootNode, vtkCommand::ModifiedEvent,
this, SLOT(updateRootNode(vtkObject*)));
qDebug() << rootNode << treeRootIndex;
this->setRootIndex(treeRootIndex);
}
......@@ -713,7 +713,6 @@ void qMRMLTreeView::mousePressEvent(QMouseEvent* e)
{
return;
}
qDebug() << "Mouse press";
// get the index of the current column
QModelIndex index = this->indexAt(e->pos());
......@@ -754,6 +753,22 @@ void qMRMLTreeView::mouseReleaseEvent(QMouseEvent* e)
this->QTreeView::mouseReleaseEvent(e);
}
//------------------------------------------------------------------------------
void qMRMLTreeView::keyPressEvent(QKeyEvent* e)
{
#ifndef _NDEBUG
if (e->key() == Qt::Key_Exclam)
{
qMRMLSortFilterProxyModel::FilterType filter =
static_cast<qMRMLSortFilterProxyModel::FilterType>(
(this->sortFilterProxyModel()->filterType() + 1) % 3);
qDebug() << "Filter type: " << filter;
this->sortFilterProxyModel()->setFilterType(filter);
}
#endif
this->Superclass::keyPressEvent(e);
}
//------------------------------------------------------------------------------
bool qMRMLTreeView::clickDecoration(const QModelIndex& index)
{
......
......@@ -39,6 +39,9 @@ class vtkMRMLNode;
class vtkMRMLScene;
/// \todo Rename to qMRMLSceneTreeView
/// In debug mode, pressing the '!' key on the view switches of
/// qMRMLSortFilterProxyModel::filterType, it can be useful to debug the scene model
/// and filters applied to them.
class QMRML_WIDGETS_EXPORT qMRMLTreeView : public QTreeView
{
Q_OBJECT
......@@ -254,6 +257,16 @@ public slots:
void editCurrentNode();
void renameCurrentNode();
/// Bypass all the filters on the view and show all the nodes.
/// \sa setHideAll(), qMRMLSortFilterProxyModel::FilterType
inline void setShowAll(bool);
/// Bypass all the filters on the view and hide all the nodes.
/// \sa showAll(), setDontHideAll(), qMRMLSortFilterProxyModel::FilterType
inline void setHideAll(bool);
/// Convenient slot to call hideAll() with the opposite value.
/// \sa setHideAll()
inline void setDontHideAll(bool);
signals:
void currentNodeChanged(vtkMRMLNode* node);
void currentNodeDeleted(const QModelIndex& index);
......@@ -284,6 +297,7 @@ protected:
virtual void updateGeometries();
virtual void mousePressEvent(QMouseEvent* event);
virtual void mouseReleaseEvent(QMouseEvent* event);
virtual void keyPressEvent(QKeyEvent* event);
virtual void toggleVisibility(const QModelIndex& index);
......@@ -304,4 +318,23 @@ bool qMRMLTreeView::showHidden()const
return this->sortFilterProxyModel()->showHidden();
}
// --------------------------------------------------------------------------
void qMRMLTreeView::setShowAll(bool show)
{
this->sortFilterProxyModel()->setShowAll(show);
}
// --------------------------------------------------------------------------
void qMRMLTreeView::setHideAll(bool hide)
{
this->sortFilterProxyModel()->setHideAll(hide);
}
#include <QDebug>
// --------------------------------------------------------------------------
void qMRMLTreeView::setDontHideAll(bool dontHide)
{
qDebug() << "DontHide: " << dontHide;
this->setHideAll(!dontHide);
}
#endif
Markdown is supported
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