Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Allow filtering history results
  • Loading branch information
nyalldawson committed Apr 22, 2023
1 parent d2bcca0 commit 9e760fc
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 22 deletions.
9 changes: 9 additions & 0 deletions python/gui/auto_generated/history/qgshistoryentrynode.sip.in
Expand Up @@ -62,6 +62,15 @@ create the node's content.
%End


virtual bool matchesString( const QString &searchString ) const;
%Docstring
Returns true if the node matches the specified ``searchString``, and
should be shown in filtered results with that search string.

The default implementation returns ``True`` if the string is contained
within the node's DisplayRole.
%End

private:
QgsHistoryEntryNode( const QgsHistoryEntryNode &other );
};
Expand Down
10 changes: 7 additions & 3 deletions src/gui/history/qgshistoryentrynode.cpp
Expand Up @@ -41,12 +41,16 @@ QList<QAction *> QgsHistoryEntryNode::actions( QWidget * )
{
return {};
}
#endif

bool QgsHistoryEntryNode::matchesString( const QString & )
bool QgsHistoryEntryNode::matchesString( const QString &string ) const
{
return false;
if ( string.isEmpty() )
return true;

return data( Qt::DisplayRole ).toString().contains( string, Qt::CaseInsensitive );
}
#endif


//
// QgsHistoryEntryGroup
Expand Down
8 changes: 6 additions & 2 deletions src/gui/history/qgshistoryentrynode.h
Expand Up @@ -93,12 +93,16 @@ class GUI_EXPORT QgsHistoryEntryNode
*/
virtual QList< QAction * > actions( QWidget *parent );

#endif

/**
* Returns true if the node matches the specified \a searchString, and
* should be shown in filtered results with that search string.
*
* The default implementation returns TRUE if the string is contained
* within the node's DisplayRole.
*/
virtual bool matchesString( const QString &searchString );
#endif
virtual bool matchesString( const QString &searchString ) const;

private:
#ifdef SIP_RUN
Expand Down
45 changes: 43 additions & 2 deletions src/gui/history/qgshistorywidget.cpp
Expand Up @@ -26,15 +26,21 @@ QgsHistoryWidget::QgsHistoryWidget( const QString &providerId, Qgis::HistoryProv
setupUi( this );

mModel = new QgsHistoryEntryModel( providerId, backends, registry, this );
mTreeView->setModel( mModel );
mProxyModel = new QgsHistoryEntryProxyModel( this );
mProxyModel->setSourceModel( mModel );

mTreeView->setModel( mProxyModel );

mFilterEdit->setShowClearButton( true );
mFilterEdit->setShowSearchIcon( true );
connect( mFilterEdit, &QLineEdit::textChanged, mProxyModel, &QgsHistoryEntryProxyModel::setFilter );
connect( mTreeView->selectionModel(), &QItemSelectionModel::currentChanged, this, &QgsHistoryWidget::currentItemChanged );
}

void QgsHistoryWidget::currentItemChanged( const QModelIndex &selected, const QModelIndex & )
{
QWidget *newWidget = nullptr;
if ( QgsHistoryEntryNode *node = mModel->index2node( selected ) )
if ( QgsHistoryEntryNode *node = mModel->index2node( mProxyModel->mapToSource( selected ) ) )
{
newWidget = node->createWidget();
if ( !newWidget )
Expand Down Expand Up @@ -65,3 +71,38 @@ void QgsHistoryWidget::currentItemChanged( const QModelIndex &selected, const QM
}
}
}

//
// QgsHistoryEntryProxyModel
//
QgsHistoryEntryProxyModel::QgsHistoryEntryProxyModel( QObject *parent )
: QSortFilterProxyModel( parent )
{
setDynamicSortFilter( true );
setRecursiveFilteringEnabled( true );
}

void QgsHistoryEntryProxyModel::setFilter( const QString &filter )
{
if ( filter == mFilter )
return;

mFilter = filter;
invalidateFilter();
}

bool QgsHistoryEntryProxyModel::filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const
{
if ( mFilter.isEmpty() )
return true;

const QModelIndex sourceIndex = sourceModel()->index( source_row, 0, source_parent );
if ( QgsHistoryEntryNode *node = qobject_cast< QgsHistoryEntryModel * >( sourceModel() )->index2node( sourceIndex ) )
{
if ( !node->matchesString( mFilter ) )
{
return false;
}
}
return true;
}
22 changes: 22 additions & 0 deletions src/gui/history/qgshistorywidget.h
Expand Up @@ -21,9 +21,30 @@
#include "ui_qgshistorywidgetbase.h"
#include "qgspanelwidget.h"

#include <QSortFilterProxyModel>

class QgsHistoryProviderRegistry;
class QgsHistoryEntryModel;

#ifndef SIP_RUN

class GUI_EXPORT QgsHistoryEntryProxyModel : public QSortFilterProxyModel
{
Q_OBJECT
public:

QgsHistoryEntryProxyModel( QObject *parent = nullptr );

void setFilter( const QString &filter );
bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const override;

private:

QString mFilter;

};
#endif

/**
* A widget showing entries from a QgsHistoryProviderRegistry.
* \ingroup gui
Expand Down Expand Up @@ -57,6 +78,7 @@ class GUI_EXPORT QgsHistoryWidget : public QgsPanelWidget, private Ui::QgsHistor
private:

QgsHistoryEntryModel *mModel = nullptr;
QgsHistoryEntryProxyModel *mProxyModel = nullptr;

};

Expand Down
64 changes: 49 additions & 15 deletions src/ui/qgshistorywidgetbase.ui
Expand Up @@ -6,17 +6,14 @@
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>500</height>
<width>387</width>
<height>220</height>
</rect>
</property>
<property name="windowTitle">
<string>History</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="spacing">
<number>6</number>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<property name="leftMargin">
<number>0</number>
</property>
Expand All @@ -29,21 +26,53 @@
<property name="bottomMargin">
<number>0</number>
</property>
<property name="margin" stdset="0">
<number>9</number>
</property>
<item>
<widget class="QSplitter" name="mSplitter">
<widget class="QSplitter" name="splitter">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<widget class="QTreeView" name="mTreeView">
<attribute name="headerVisible">
<bool>false</bool>
</attribute>
<widget class="QWidget" name="">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QgsFilterLineEdit" name="mFilterEdit">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="baseSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="placeholderText">
<string>Search</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QTreeView" name="mTreeView">
<attribute name="headerVisible">
<bool>false</bool>
</attribute>
</widget>
</item>
</layout>
</widget>
<widget class="QStackedWidget" name="mContainerStackedWidget">
<widget class="QWidget" name="mEntryContainerStackedWidgetPage1" native="true"/>
<widget class="QWidget" name="mEntryContainerStackedWidgetPage1"/>
</widget>
</widget>
</item>
Expand All @@ -56,6 +85,11 @@
<header>qgspanelwidget.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>QgsFilterLineEdit</class>
<extends>QLineEdit</extends>
<header>qgsfilterlineedit.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
Expand Down

0 comments on commit 9e760fc

Please sign in to comment.