26
26
#include " QStandardItem"
27
27
#include " QSortFilterProxyModel"
28
28
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
-
71
29
/* * An expression item that can be used in the QgsExpressionBuilderWidget tree.
72
30
*/
73
31
class QgsExpressionItem : public QStandardItem
@@ -89,6 +47,7 @@ class QgsExpressionItem : public QStandardItem
89
47
mExpressionText = expressionText;
90
48
mHelpText = helpText;
91
49
mType = itemType;
50
+ setData ( itemType, ItemTypeRole );
92
51
}
93
52
94
53
QgsExpressionItem ( QString label,
@@ -98,15 +57,16 @@ class QgsExpressionItem : public QStandardItem
98
57
{
99
58
mExpressionText = expressionText;
100
59
mType = itemType;
60
+ setData ( itemType, ItemTypeRole );
101
61
}
102
62
103
- QString getExpressionText () { return mExpressionText ; }
63
+ QString getExpressionText () const { return mExpressionText ; }
104
64
105
65
/* * Get the help text that is associated with this expression item.
106
66
*
107
67
* @return The help text.
108
68
*/
109
- QString getHelpText () { return mHelpText ; }
69
+ QString getHelpText () const { return mHelpText ; }
110
70
/* * Set the help text for the current item
111
71
*
112
72
* @note The help text can be set as a html string.
@@ -117,12 +77,63 @@ class QgsExpressionItem : public QStandardItem
117
77
*
118
78
* @return The QgsExpressionItem::ItemType
119
79
*/
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 ;
121
86
122
87
private:
123
88
QString mExpressionText ;
124
89
QString mHelpText ;
125
90
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
+ }
126
137
};
127
138
128
139
/* * A reusable widget that can be used to build a expression string.
0 commit comments