Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix filtering of highlighted variables
  • Loading branch information
nyalldawson committed Aug 22, 2015
1 parent 4bf8b13 commit d7b7474
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 72 deletions.
51 changes: 28 additions & 23 deletions python/gui/qgsexpressionbuilderwidget.sip
@@ -1,23 +1,3 @@
/** Search proxy used to filter the QgsExpressionBuilderWidget tree.
* The default search for a tree model only searches top level this will handle one
* level down
*/
class QgsExpressionItemSearchProxy : QSortFilterProxyModel
{
%TypeHeaderCode
#include <qgsexpressionbuilderwidget.h>
%End

public:
QgsExpressionItemSearchProxy();

bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const;

protected:

bool lessThan( const QModelIndex &left, const QModelIndex &right ) const;
};

/** An expression item that can be used in the QgsExpressionBuilderWidget tree.
*/
class QgsExpressionItem : QStandardItem
Expand All @@ -43,13 +23,13 @@ class QgsExpressionItem : QStandardItem
QString expressionText,
QgsExpressionItem::ItemType itemType = ExpressionNode );

QString getExpressionText();
QString getExpressionText() const;

/** Get the help text that is associated with this expression item.
*
* @return The help text.
*/
QString getHelpText();
QString getHelpText() const;
/** Set the help text for the current item
*
* @note The help text can be set as a html string.
Expand All @@ -60,7 +40,32 @@ class QgsExpressionItem : QStandardItem
*
* @return The QgsExpressionItem::ItemType
*/
QgsExpressionItem::ItemType getItemType();
QgsExpressionItem::ItemType getItemType() const;

//! Custom sort order role
static const int CustomSortRole;
//! Item type role
static const int ItemTypeRole;
};

/** Search proxy used to filter the QgsExpressionBuilderWidget tree.
* The default search for a tree model only searches top level this will handle one
* level down
*/
class QgsExpressionItemSearchProxy : QSortFilterProxyModel
{
%TypeHeaderCode
#include <qgsexpressionbuilderwidget.h>
%End

public:
QgsExpressionItemSearchProxy();

bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const;

protected:

bool lessThan( const QModelIndex &left, const QModelIndex &right ) const;
};

/** A reusable widget that can be used to build a expression string.
Expand Down
6 changes: 3 additions & 3 deletions src/gui/qgsexpressionbuilderwidget.cpp
Expand Up @@ -345,7 +345,7 @@ void QgsExpressionBuilderWidget::registerItem( QString group,
{
QgsExpressionItem* item = new QgsExpressionItem( label, expressionText, helpText, type );
item->setData( label, Qt::UserRole );
item->setData( sortOrder, Qt::UserRole + 1 );
item->setData( sortOrder, QgsExpressionItem::CustomSortRole );

// Look up the group and insert the new function.
if ( mExpressionGroups.contains( group ) )
Expand All @@ -358,7 +358,7 @@ void QgsExpressionBuilderWidget::registerItem( QString group,
// If the group doesn't exist yet we make it first.
QgsExpressionItem *newgroupNode = new QgsExpressionItem( QgsExpression::group( group ), "", QgsExpressionItem::Header );
newgroupNode->setData( group, Qt::UserRole );
newgroupNode->setData( group == "Recent (Selection)" ? 2 : 1, Qt::UserRole + 1 );
newgroupNode->setData( group == "Recent (Selection)" ? 2 : 1, QgsExpressionItem::CustomSortRole );
newgroupNode->appendRow( item );
mModel->appendRow( newgroupNode );
mExpressionGroups.insert( group, newgroupNode );
Expand All @@ -369,7 +369,7 @@ void QgsExpressionBuilderWidget::registerItem( QString group,
//insert a copy as a top level item
QgsExpressionItem* topLevelItem = new QgsExpressionItem( label, expressionText, helpText, type );
topLevelItem->setData( label, Qt::UserRole );
item->setData( 0, Qt::UserRole + 1 );
item->setData( 0, QgsExpressionItem::CustomSortRole );
QFont font = topLevelItem->font();
font.setBold( true );
topLevelItem->setFont( font );
Expand Down
101 changes: 56 additions & 45 deletions src/gui/qgsexpressionbuilderwidget.h
Expand Up @@ -26,48 +26,6 @@
#include "QStandardItem"
#include "QSortFilterProxyModel"

/** Search proxy used to filter the QgsExpressionBuilderWidget tree.
* The default search for a tree model only searches top level this will handle one
* level down
*/
class QgsExpressionItemSearchProxy : public QSortFilterProxyModel
{
public:
QgsExpressionItemSearchProxy()
{
setFilterCaseSensitivity( Qt::CaseInsensitive );
}

bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const override
{
if ( source_parent == qobject_cast<QStandardItemModel*>( sourceModel() )->invisibleRootItem()->index() )
return true;

return QSortFilterProxyModel::filterAcceptsRow( source_row, source_parent );
}

protected:

bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override
{
int leftSort = sourceModel()->data( left, Qt::UserRole + 1 ).toInt();
int rightSort = sourceModel()->data( right, Qt::UserRole + 1 ).toInt();
if ( leftSort != rightSort )
return leftSort < rightSort;

QString leftString = sourceModel()->data( left, Qt::DisplayRole ).toString();
QString rightString = sourceModel()->data( right, Qt::DisplayRole ).toString();

//ignore $ prefixes when sorting
if ( leftString.startsWith( "$" ) )
leftString = leftString.mid( 1 );
if ( rightString.startsWith( "$" ) )
rightString = rightString.mid( 1 );

return QString::localeAwareCompare( leftString, rightString ) < 0;
}
};

/** An expression item that can be used in the QgsExpressionBuilderWidget tree.
*/
class QgsExpressionItem : public QStandardItem
Expand All @@ -89,6 +47,7 @@ class QgsExpressionItem : public QStandardItem
mExpressionText = expressionText;
mHelpText = helpText;
mType = itemType;
setData( itemType, ItemTypeRole );
}

QgsExpressionItem( QString label,
Expand All @@ -98,15 +57,16 @@ class QgsExpressionItem : public QStandardItem
{
mExpressionText = expressionText;
mType = itemType;
setData( itemType, ItemTypeRole );
}

QString getExpressionText() { return mExpressionText; }
QString getExpressionText() const { return mExpressionText; }

/** Get the help text that is associated with this expression item.
*
* @return The help text.
*/
QString getHelpText() { return mHelpText; }
QString getHelpText() const { return mHelpText; }
/** Set the help text for the current item
*
* @note The help text can be set as a html string.
Expand All @@ -117,12 +77,63 @@ class QgsExpressionItem : public QStandardItem
*
* @return The QgsExpressionItem::ItemType
*/
QgsExpressionItem::ItemType getItemType() { return mType; }
QgsExpressionItem::ItemType getItemType() const { return mType; }

//! Custom sort order role
static const int CustomSortRole = Qt::UserRole + 1;
//! Item type role
static const int ItemTypeRole = Qt::UserRole + 2;

private:
QString mExpressionText;
QString mHelpText;
QgsExpressionItem::ItemType mType;

};

/** Search proxy used to filter the QgsExpressionBuilderWidget tree.
* The default search for a tree model only searches top level this will handle one
* level down
*/
class QgsExpressionItemSearchProxy : public QSortFilterProxyModel
{
public:
QgsExpressionItemSearchProxy()
{
setFilterCaseSensitivity( Qt::CaseInsensitive );
}

bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const override
{
QModelIndex index = sourceModel()->index( source_row, 0, source_parent );
QgsExpressionItem::ItemType itemType = QgsExpressionItem::ItemType( sourceModel()->data( index, QgsExpressionItem::ItemTypeRole ).toInt() );

if ( itemType == QgsExpressionItem::Header )
return true;

return QSortFilterProxyModel::filterAcceptsRow( source_row, source_parent );
}

protected:

bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override
{
int leftSort = sourceModel()->data( left, QgsExpressionItem::CustomSortRole ).toInt();
int rightSort = sourceModel()->data( right, QgsExpressionItem::CustomSortRole ).toInt();
if ( leftSort != rightSort )
return leftSort < rightSort;

QString leftString = sourceModel()->data( left, Qt::DisplayRole ).toString();
QString rightString = sourceModel()->data( right, Qt::DisplayRole ).toString();

//ignore $ prefixes when sorting
if ( leftString.startsWith( "$" ) )
leftString = leftString.mid( 1 );
if ( rightString.startsWith( "$" ) )
rightString = rightString.mid( 1 );

return QString::localeAwareCompare( leftString, rightString ) < 0;
}
};

/** A reusable widget that can be used to build a expression string.
Expand Down
1 change: 0 additions & 1 deletion src/gui/qgsexpressionselectiondialog.cpp
Expand Up @@ -47,7 +47,6 @@ QgsExpressionSelectionDialog::QgsExpressionSelectionDialog( QgsVectorLayer* laye
context << QgsExpressionContextUtils::globalScope()
<< QgsExpressionContextUtils::projectScope()
<< QgsExpressionContextUtils::layerScope( mLayer );
context.setHighlightedVariables( QStringList() << "layer_id" << "layer_name" );
mExpressionBuilder->setExpressionContext( context );

QSettings settings;
Expand Down

0 comments on commit d7b7474

Please sign in to comment.