Skip to content

Commit d7b7474

Browse files
committedAug 22, 2015
Fix filtering of highlighted variables
1 parent 4bf8b13 commit d7b7474

File tree

4 files changed

+87
-72
lines changed

4 files changed

+87
-72
lines changed
 

‎python/gui/qgsexpressionbuilderwidget.sip

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,3 @@
1-
/** Search proxy used to filter the QgsExpressionBuilderWidget tree.
2-
* The default search for a tree model only searches top level this will handle one
3-
* level down
4-
*/
5-
class QgsExpressionItemSearchProxy : QSortFilterProxyModel
6-
{
7-
%TypeHeaderCode
8-
#include <qgsexpressionbuilderwidget.h>
9-
%End
10-
11-
public:
12-
QgsExpressionItemSearchProxy();
13-
14-
bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const;
15-
16-
protected:
17-
18-
bool lessThan( const QModelIndex &left, const QModelIndex &right ) const;
19-
};
20-
211
/** An expression item that can be used in the QgsExpressionBuilderWidget tree.
222
*/
233
class QgsExpressionItem : QStandardItem
@@ -43,13 +23,13 @@ class QgsExpressionItem : QStandardItem
4323
QString expressionText,
4424
QgsExpressionItem::ItemType itemType = ExpressionNode );
4525

46-
QString getExpressionText();
26+
QString getExpressionText() const;
4727

4828
/** Get the help text that is associated with this expression item.
4929
*
5030
* @return The help text.
5131
*/
52-
QString getHelpText();
32+
QString getHelpText() const;
5333
/** Set the help text for the current item
5434
*
5535
* @note The help text can be set as a html string.
@@ -60,7 +40,32 @@ class QgsExpressionItem : QStandardItem
6040
*
6141
* @return The QgsExpressionItem::ItemType
6242
*/
63-
QgsExpressionItem::ItemType getItemType();
43+
QgsExpressionItem::ItemType getItemType() const;
44+
45+
//! Custom sort order role
46+
static const int CustomSortRole;
47+
//! Item type role
48+
static const int ItemTypeRole;
49+
};
50+
51+
/** Search proxy used to filter the QgsExpressionBuilderWidget tree.
52+
* The default search for a tree model only searches top level this will handle one
53+
* level down
54+
*/
55+
class QgsExpressionItemSearchProxy : QSortFilterProxyModel
56+
{
57+
%TypeHeaderCode
58+
#include <qgsexpressionbuilderwidget.h>
59+
%End
60+
61+
public:
62+
QgsExpressionItemSearchProxy();
63+
64+
bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const;
65+
66+
protected:
67+
68+
bool lessThan( const QModelIndex &left, const QModelIndex &right ) const;
6469
};
6570

6671
/** A reusable widget that can be used to build a expression string.

‎src/gui/qgsexpressionbuilderwidget.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ void QgsExpressionBuilderWidget::registerItem( QString group,
345345
{
346346
QgsExpressionItem* item = new QgsExpressionItem( label, expressionText, helpText, type );
347347
item->setData( label, Qt::UserRole );
348-
item->setData( sortOrder, Qt::UserRole + 1 );
348+
item->setData( sortOrder, QgsExpressionItem::CustomSortRole );
349349

350350
// Look up the group and insert the new function.
351351
if ( mExpressionGroups.contains( group ) )
@@ -358,7 +358,7 @@ void QgsExpressionBuilderWidget::registerItem( QString group,
358358
// If the group doesn't exist yet we make it first.
359359
QgsExpressionItem *newgroupNode = new QgsExpressionItem( QgsExpression::group( group ), "", QgsExpressionItem::Header );
360360
newgroupNode->setData( group, Qt::UserRole );
361-
newgroupNode->setData( group == "Recent (Selection)" ? 2 : 1, Qt::UserRole + 1 );
361+
newgroupNode->setData( group == "Recent (Selection)" ? 2 : 1, QgsExpressionItem::CustomSortRole );
362362
newgroupNode->appendRow( item );
363363
mModel->appendRow( newgroupNode );
364364
mExpressionGroups.insert( group, newgroupNode );
@@ -369,7 +369,7 @@ void QgsExpressionBuilderWidget::registerItem( QString group,
369369
//insert a copy as a top level item
370370
QgsExpressionItem* topLevelItem = new QgsExpressionItem( label, expressionText, helpText, type );
371371
topLevelItem->setData( label, Qt::UserRole );
372-
item->setData( 0, Qt::UserRole + 1 );
372+
item->setData( 0, QgsExpressionItem::CustomSortRole );
373373
QFont font = topLevelItem->font();
374374
font.setBold( true );
375375
topLevelItem->setFont( font );

‎src/gui/qgsexpressionbuilderwidget.h

Lines changed: 56 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -26,48 +26,6 @@
2626
#include "QStandardItem"
2727
#include "QSortFilterProxyModel"
2828

29-
/** Search proxy used to filter the QgsExpressionBuilderWidget tree.
30-
* The default search for a tree model only searches top level this will handle one
31-
* level down
32-
*/
33-
class QgsExpressionItemSearchProxy : public QSortFilterProxyModel
34-
{
35-
public:
36-
QgsExpressionItemSearchProxy()
37-
{
38-
setFilterCaseSensitivity( Qt::CaseInsensitive );
39-
}
40-
41-
bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const override
42-
{
43-
if ( source_parent == qobject_cast<QStandardItemModel*>( sourceModel() )->invisibleRootItem()->index() )
44-
return true;
45-
46-
return QSortFilterProxyModel::filterAcceptsRow( source_row, source_parent );
47-
}
48-
49-
protected:
50-
51-
bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override
52-
{
53-
int leftSort = sourceModel()->data( left, Qt::UserRole + 1 ).toInt();
54-
int rightSort = sourceModel()->data( right, Qt::UserRole + 1 ).toInt();
55-
if ( leftSort != rightSort )
56-
return leftSort < rightSort;
57-
58-
QString leftString = sourceModel()->data( left, Qt::DisplayRole ).toString();
59-
QString rightString = sourceModel()->data( right, Qt::DisplayRole ).toString();
60-
61-
//ignore $ prefixes when sorting
62-
if ( leftString.startsWith( "$" ) )
63-
leftString = leftString.mid( 1 );
64-
if ( rightString.startsWith( "$" ) )
65-
rightString = rightString.mid( 1 );
66-
67-
return QString::localeAwareCompare( leftString, rightString ) < 0;
68-
}
69-
};
70-
7129
/** An expression item that can be used in the QgsExpressionBuilderWidget tree.
7230
*/
7331
class QgsExpressionItem : public QStandardItem
@@ -89,6 +47,7 @@ class QgsExpressionItem : public QStandardItem
8947
mExpressionText = expressionText;
9048
mHelpText = helpText;
9149
mType = itemType;
50+
setData( itemType, ItemTypeRole );
9251
}
9352

9453
QgsExpressionItem( QString label,
@@ -98,15 +57,16 @@ class QgsExpressionItem : public QStandardItem
9857
{
9958
mExpressionText = expressionText;
10059
mType = itemType;
60+
setData( itemType, ItemTypeRole );
10161
}
10262

103-
QString getExpressionText() { return mExpressionText; }
63+
QString getExpressionText() const { return mExpressionText; }
10464

10565
/** Get the help text that is associated with this expression item.
10666
*
10767
* @return The help text.
10868
*/
109-
QString getHelpText() { return mHelpText; }
69+
QString getHelpText() const { return mHelpText; }
11070
/** Set the help text for the current item
11171
*
11272
* @note The help text can be set as a html string.
@@ -117,12 +77,63 @@ class QgsExpressionItem : public QStandardItem
11777
*
11878
* @return The QgsExpressionItem::ItemType
11979
*/
120-
QgsExpressionItem::ItemType getItemType() { return mType; }
80+
QgsExpressionItem::ItemType getItemType() const { return mType; }
81+
82+
//! Custom sort order role
83+
static const int CustomSortRole = Qt::UserRole + 1;
84+
//! Item type role
85+
static const int ItemTypeRole = Qt::UserRole + 2;
12186

12287
private:
12388
QString mExpressionText;
12489
QString mHelpText;
12590
QgsExpressionItem::ItemType mType;
91+
92+
};
93+
94+
/** Search proxy used to filter the QgsExpressionBuilderWidget tree.
95+
* The default search for a tree model only searches top level this will handle one
96+
* level down
97+
*/
98+
class QgsExpressionItemSearchProxy : public QSortFilterProxyModel
99+
{
100+
public:
101+
QgsExpressionItemSearchProxy()
102+
{
103+
setFilterCaseSensitivity( Qt::CaseInsensitive );
104+
}
105+
106+
bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const override
107+
{
108+
QModelIndex index = sourceModel()->index( source_row, 0, source_parent );
109+
QgsExpressionItem::ItemType itemType = QgsExpressionItem::ItemType( sourceModel()->data( index, QgsExpressionItem::ItemTypeRole ).toInt() );
110+
111+
if ( itemType == QgsExpressionItem::Header )
112+
return true;
113+
114+
return QSortFilterProxyModel::filterAcceptsRow( source_row, source_parent );
115+
}
116+
117+
protected:
118+
119+
bool lessThan( const QModelIndex &left, const QModelIndex &right ) const override
120+
{
121+
int leftSort = sourceModel()->data( left, QgsExpressionItem::CustomSortRole ).toInt();
122+
int rightSort = sourceModel()->data( right, QgsExpressionItem::CustomSortRole ).toInt();
123+
if ( leftSort != rightSort )
124+
return leftSort < rightSort;
125+
126+
QString leftString = sourceModel()->data( left, Qt::DisplayRole ).toString();
127+
QString rightString = sourceModel()->data( right, Qt::DisplayRole ).toString();
128+
129+
//ignore $ prefixes when sorting
130+
if ( leftString.startsWith( "$" ) )
131+
leftString = leftString.mid( 1 );
132+
if ( rightString.startsWith( "$" ) )
133+
rightString = rightString.mid( 1 );
134+
135+
return QString::localeAwareCompare( leftString, rightString ) < 0;
136+
}
126137
};
127138

128139
/** A reusable widget that can be used to build a expression string.

‎src/gui/qgsexpressionselectiondialog.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ QgsExpressionSelectionDialog::QgsExpressionSelectionDialog( QgsVectorLayer* laye
4747
context << QgsExpressionContextUtils::globalScope()
4848
<< QgsExpressionContextUtils::projectScope()
4949
<< QgsExpressionContextUtils::layerScope( mLayer );
50-
context.setHighlightedVariables( QStringList() << "layer_id" << "layer_name" );
5150
mExpressionBuilder->setExpressionContext( context );
5251

5352
QSettings settings;

0 commit comments

Comments
 (0)
Please sign in to comment.