Skip to content

Commit

Permalink
Show search hits for tables and tree/table headers
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 29, 2021
1 parent 43439b7 commit 0a8f24e
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 3 deletions.
Expand Up @@ -142,6 +142,32 @@ constructs a highlight widget for a tree view or widget.

virtual void reset();

};

class QgsOptionsDialogHighlightTable : QgsOptionsDialogHighlightWidget
{
%Docstring(signature="appended")
A highlight widget for table widgets.
This is used to search and highlight text in :py:class:`QgsOptionsDialogBase` implementations.

.. versionadded:: 3.22
%End

%TypeHeaderCode
#include "qgsoptionsdialoghighlightwidgetsimpl.h"
%End
public:
QgsOptionsDialogHighlightTable( QTableView *tableView );
%Docstring
constructs a highlight widget for a table view or widget.
%End
protected:
virtual bool searchText( const QString &text );

virtual bool highlightText( const QString &text );

virtual void reset();

};
/************************************************************************
* This file has been generated automatically from *
Expand Down
6 changes: 3 additions & 3 deletions src/gui/qgsoptionsdialogbase.cpp
Expand Up @@ -507,10 +507,10 @@ void QgsOptionsDialogBase::registerTextSearchWidgets()

for ( int i = 0; i < mOptStackedWidget->count(); i++ )
{
const auto constWidget = mOptStackedWidget->widget( i )->findChildren<QWidget *>();
for ( QWidget *w : constWidget )
{

const QList< QWidget * > widgets = mOptStackedWidget->widget( i )->findChildren<QWidget *>();
for ( QWidget *w : widgets )
{
// get custom highlight widget in user added pages
QHash<QWidget *, QgsOptionsDialogHighlightWidget *> customHighlightWidgets;
QgsOptionsPageWidget *opw = qobject_cast<QgsOptionsPageWidget *>( mOptStackedWidget->widget( i ) );
Expand Down
5 changes: 5 additions & 0 deletions src/gui/qgsoptionsdialoghighlightwidget.cpp
Expand Up @@ -21,6 +21,7 @@
#include <QTimer>
#include <QTreeView>
#include <QTreeWidget>
#include <QTableView>

#include "qgsoptionsdialoghighlightwidget.h"
#include "qgsmessagebaritem.h"
Expand Down Expand Up @@ -71,6 +72,10 @@ QgsOptionsDialogHighlightWidget *QgsOptionsDialogHighlightWidget::createWidget(
{
return new QgsOptionsDialogHighlightTree( qobject_cast<QTreeView *>( widget ) );
}
else if ( qobject_cast<QTableView *>( widget ) )
{
return new QgsOptionsDialogHighlightTable( qobject_cast<QTableView *>( widget ) );
}
else
{
// return invalid widget
Expand Down
45 changes: 45 additions & 0 deletions src/gui/qgsoptionsdialoghighlightwidgetsimpl.cpp
Expand Up @@ -22,6 +22,7 @@
#include <QTreeView>
#include <QTreeWidget>
#include <QAbstractItemModel>
#include <QTableView>

#include "qgsoptionsdialoghighlightwidget.h"
#include "qgsmessagebaritem.h"
Expand Down Expand Up @@ -212,6 +213,15 @@ bool QgsOptionsDialogHighlightTree::searchText( const QString &text )
{
if ( !mTreeView || !mTreeView->model() )
return false;

// search headers too!
for ( int col = 0; col < mTreeView->model()->columnCount(); ++col )
{
const QString headerText = mTreeView->model()->headerData( col, Qt::Horizontal ).toString();
if ( headerText.contains( text, Qt::CaseInsensitive ) )
return true;
}

QModelIndexList hits = mTreeView->model()->match( mTreeView->model()->index( 0, 0 ), Qt::DisplayRole, text, 1, Qt::MatchContains | Qt::MatchRecursive );
return !hits.isEmpty();
}
Expand Down Expand Up @@ -284,3 +294,38 @@ void QgsOptionsDialogHighlightTree::reset()
mTreeInitialExpand.clear();
}
}


// ****************
// QTableView
QgsOptionsDialogHighlightTable::QgsOptionsDialogHighlightTable( QTableView *tableView )
: QgsOptionsDialogHighlightWidget( tableView )
, mTableView( tableView )
{
}

bool QgsOptionsDialogHighlightTable::searchText( const QString &text )
{
if ( !mTableView || !mTableView->model() )
return false;

// search headers too!
for ( int col = 0; col < mTableView->model()->columnCount(); ++col )
{
const QString headerText = mTableView->model()->headerData( col, Qt::Horizontal ).toString();
if ( headerText.contains( text, Qt::CaseInsensitive ) )
return true;
}

QModelIndexList hits = mTableView->model()->match( mTableView->model()->index( 0, 0 ), Qt::DisplayRole, text, 1, Qt::MatchContains | Qt::MatchRecursive );
return !hits.isEmpty();
}

bool QgsOptionsDialogHighlightTable::highlightText( const QString & )
{
return false;
}

void QgsOptionsDialogHighlightTable::reset()
{
}
21 changes: 21 additions & 0 deletions src/gui/qgsoptionsdialoghighlightwidgetsimpl.h
Expand Up @@ -31,6 +31,7 @@ class QAbstractButton;
class QGroupBox;
class QTreeView;
class QTreeWidgetItem;
class QTableView;


/**
Expand Down Expand Up @@ -141,4 +142,24 @@ class GUI_EXPORT QgsOptionsDialogHighlightTree : public QgsOptionsDialogHighligh
QMap<QTreeWidgetItem *, bool> mTreeInitialExpand = QMap<QTreeWidgetItem *, bool>();
QMap<QTreeWidgetItem *, bool> mTreeInitialVisible = QMap<QTreeWidgetItem *, bool>();
};

/**
* \ingroup gui
* \class QgsOptionsDialogHighlightTable
* \brief A highlight widget for table widgets.
* This is used to search and highlight text in QgsOptionsDialogBase implementations.
* \since QGIS 3.22
*/
class GUI_EXPORT QgsOptionsDialogHighlightTable : public QgsOptionsDialogHighlightWidget
{
Q_OBJECT
public:
//! constructs a highlight widget for a table view or widget.
QgsOptionsDialogHighlightTable( QTableView *tableView );
protected:
bool searchText( const QString &text ) override;
bool highlightText( const QString &text ) override;
void reset() override;
QPointer<QTableView> mTableView;
};
#endif // QGSOPTIONSDIALOGHIGHLIGHTWIDGETSIMPL_H

0 comments on commit 0a8f24e

Please sign in to comment.