15
15
* *
16
16
***************************************************************************/
17
17
18
+ #include < QFont>
18
19
19
20
#include " qgslocatormodel.h"
20
21
#include " qgslocator.h"
21
22
#include " qgsapplication.h"
22
23
#include " qgslogger.h"
23
24
25
+
24
26
//
25
27
// QgsLocatorModel
26
28
//
@@ -41,6 +43,7 @@ void QgsLocatorModel::clear()
41
43
beginResetModel ();
42
44
mResults .clear ();
43
45
mFoundResultsFromFilterNames .clear ();
46
+ mFoundResultsFilterGroups .clear ();
44
47
endResetModel ();
45
48
}
46
49
@@ -76,8 +79,14 @@ QVariant QgsLocatorModel::data( const QModelIndex &index, int role ) const
76
79
case Name:
77
80
if ( !mResults .at ( index.row () ).filter )
78
81
return mResults .at ( index.row () ).result .displayString ;
79
- else
82
+ else if ( mResults . at ( index. row () ). filter && mResults . at ( index. row () ). groupSorting == 0 )
80
83
return mResults .at ( index.row () ).filterTitle ;
84
+ else
85
+ {
86
+ QString groupTitle = mResults .at ( index.row () ).groupTitle ;
87
+ groupTitle.prepend ( " " );
88
+ return groupTitle;
89
+ }
81
90
case Description:
82
91
if ( !mResults .at ( index.row () ).filter )
83
92
return mResults .at ( index.row () ).result .description ;
@@ -87,6 +96,19 @@ QVariant QgsLocatorModel::data( const QModelIndex &index, int role ) const
87
96
break ;
88
97
}
89
98
99
+ case Qt::FontRole:
100
+ if ( index.column () == Name && !mResults .at ( index.row () ).groupTitle .isEmpty () )
101
+ {
102
+ QFont font;
103
+ font.setItalic ( true );
104
+ return font;
105
+ }
106
+ else
107
+ {
108
+ return QVariant ();
109
+ }
110
+ break ;
111
+
90
112
case Qt::DecorationRole:
91
113
switch ( index.column () )
92
114
{
@@ -112,10 +134,8 @@ QVariant QgsLocatorModel::data( const QModelIndex &index, int role ) const
112
134
return QVariant ();
113
135
114
136
case ResultTypeRole:
115
- if ( mResults .at ( index.row () ).filter )
116
- return 0 ;
117
- else
118
- return 1 ;
137
+ // 0 for filter title, the group otherwise, 9999 if no group
138
+ return mResults .at ( index.row () ).groupSorting ;
119
139
120
140
case ResultScoreRole:
121
141
if ( mResults .at ( index.row () ).filter )
@@ -134,6 +154,12 @@ QVariant QgsLocatorModel::data( const QModelIndex &index, int role ) const
134
154
return mResults .at ( index.row () ).result .filter ->displayName ();
135
155
else
136
156
return mResults .at ( index.row () ).filterTitle ;
157
+
158
+ case ResultFilterGroupSortingRole:
159
+ if ( mResults .at ( index.row () ).groupTitle .isEmpty () )
160
+ return 1 ;
161
+ else
162
+ return 0 ;
137
163
}
138
164
139
165
return QVariant ();
@@ -146,7 +172,7 @@ Qt::ItemFlags QgsLocatorModel::flags( const QModelIndex &index ) const
146
172
return QAbstractTableModel::flags ( index );
147
173
148
174
Qt::ItemFlags flags = QAbstractTableModel::flags ( index );
149
- if ( ! mResults .at ( index.row () ).filterTitle . isEmpty () )
175
+ if ( mResults .at ( index.row () ).filter )
150
176
{
151
177
flags = flags & ~( Qt::ItemIsSelectable | Qt::ItemIsEnabled );
152
178
}
@@ -159,20 +185,29 @@ void QgsLocatorModel::addResult( const QgsLocatorResult &result )
159
185
if ( mDeferredClear )
160
186
{
161
187
mFoundResultsFromFilterNames .clear ();
188
+ mFoundResultsFilterGroups .clear ();
162
189
}
163
190
164
191
int pos = mResults .size ();
165
192
bool addingFilter = !result.filter ->displayName ().isEmpty () && !mFoundResultsFromFilterNames .contains ( result.filter ->name () );
166
193
if ( addingFilter )
167
194
mFoundResultsFromFilterNames << result.filter ->name ();
168
195
196
+ bool addingGroup = !result.group .isEmpty () && ( !mFoundResultsFilterGroups .contains ( result.filter )
197
+ || !mFoundResultsFilterGroups .value ( result.filter ).contains ( result.group ) );
198
+ if ( addingGroup )
199
+ {
200
+ if ( !mFoundResultsFilterGroups .contains ( result.filter ) )
201
+ mFoundResultsFilterGroups [result.filter ] = QStringList ();
202
+ mFoundResultsFilterGroups [result.filter ] << result.group ;
203
+ }
169
204
if ( mDeferredClear )
170
205
{
171
206
beginResetModel ();
172
207
mResults .clear ();
173
208
}
174
209
else
175
- beginInsertRows ( QModelIndex (), pos, pos + ( addingFilter ? 1 : 0 ) );
210
+ beginInsertRows ( QModelIndex (), pos, pos + ( static_cast < int >( addingFilter ) + static_cast < int >( addingGroup ) ) );
176
211
177
212
if ( addingFilter )
178
213
{
@@ -181,8 +216,21 @@ void QgsLocatorModel::addResult( const QgsLocatorResult &result )
181
216
entry.filter = result.filter ;
182
217
mResults << entry;
183
218
}
219
+ if ( addingGroup )
220
+ {
221
+ Entry entry;
222
+ entry.filterTitle = result.filter ->displayName ();
223
+ entry.groupTitle = result.group ;
224
+ // the sorting of groups will be achieved by order of adding groups
225
+ // this could be customized by adding the extra info to QgsLocatorResult
226
+ entry.groupSorting = mFoundResultsFilterGroups [result.filter ].count ();
227
+ entry.filter = result.filter ;
228
+ mResults << entry;
229
+ }
184
230
Entry entry;
185
231
entry.result = result;
232
+ // keep the group title empty to allow differecing group title from results
233
+ entry.groupSorting = result.group .isEmpty () ? NoGroup : mFoundResultsFilterGroups [result.filter ].count ();
186
234
mResults << entry;
187
235
188
236
if ( mDeferredClear )
@@ -279,12 +327,18 @@ bool QgsLocatorProxyModel::lessThan( const QModelIndex &left, const QModelIndex
279
327
if ( leftFilter != rightFilter )
280
328
return QString::localeAwareCompare ( leftFilter, rightFilter ) < 0 ;
281
329
282
- // then make sure filter title appears before filter's results
330
+ // then make sure filter title or group appears before filter's results
283
331
int leftTypeRole = sourceModel ()->data ( left, QgsLocatorModel::ResultTypeRole ).toInt ();
284
332
int rightTypeRole = sourceModel ()->data ( right, QgsLocatorModel::ResultTypeRole ).toInt ();
285
333
if ( leftTypeRole != rightTypeRole )
286
334
return leftTypeRole < rightTypeRole;
287
335
336
+ // make sure group title are above
337
+ int leftGroupRole = sourceModel ()->data ( left, QgsLocatorModel::ResultFilterGroupSortingRole ).toInt ();
338
+ int rightGroupRole = sourceModel ()->data ( right, QgsLocatorModel::ResultFilterGroupSortingRole ).toInt ();
339
+ if ( leftGroupRole != rightGroupRole )
340
+ return leftGroupRole < rightGroupRole;
341
+
288
342
// sort filter's results by score
289
343
double leftScore = sourceModel ()->data ( left, QgsLocatorModel::ResultScoreRole ).toDouble ();
290
344
double rightScore = sourceModel ()->data ( right, QgsLocatorModel::ResultScoreRole ).toDouble ();
0 commit comments