1515 * *
1616 ***************************************************************************/
1717
18+ #include < QFont>
1819
1920#include " qgslocatormodel.h"
2021#include " qgslocator.h"
2122#include " qgsapplication.h"
2223#include " qgslogger.h"
2324
25+
2426//
2527// QgsLocatorModel
2628//
@@ -41,6 +43,7 @@ void QgsLocatorModel::clear()
4143 beginResetModel ();
4244 mResults .clear ();
4345 mFoundResultsFromFilterNames .clear ();
46+ mFoundResultsFilterGroups .clear ();
4447 endResetModel ();
4548}
4649
@@ -76,8 +79,14 @@ QVariant QgsLocatorModel::data( const QModelIndex &index, int role ) const
7679 case Name:
7780 if ( !mResults .at ( index.row () ).filter )
7881 return mResults .at ( index.row () ).result .displayString ;
79- else
82+ else if ( mResults . at ( index. row () ). filter && mResults . at ( index. row () ). groupSorting == 0 )
8083 return mResults .at ( index.row () ).filterTitle ;
84+ else
85+ {
86+ QString groupTitle = mResults .at ( index.row () ).groupTitle ;
87+ groupTitle.prepend ( " " );
88+ return groupTitle;
89+ }
8190 case Description:
8291 if ( !mResults .at ( index.row () ).filter )
8392 return mResults .at ( index.row () ).result .description ;
@@ -87,6 +96,19 @@ QVariant QgsLocatorModel::data( const QModelIndex &index, int role ) const
8796 break ;
8897 }
8998
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+
90112 case Qt::DecorationRole:
91113 switch ( index.column () )
92114 {
@@ -112,10 +134,8 @@ QVariant QgsLocatorModel::data( const QModelIndex &index, int role ) const
112134 return QVariant ();
113135
114136 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 ;
119139
120140 case ResultScoreRole:
121141 if ( mResults .at ( index.row () ).filter )
@@ -134,6 +154,12 @@ QVariant QgsLocatorModel::data( const QModelIndex &index, int role ) const
134154 return mResults .at ( index.row () ).result .filter ->displayName ();
135155 else
136156 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 ;
137163 }
138164
139165 return QVariant ();
@@ -146,7 +172,7 @@ Qt::ItemFlags QgsLocatorModel::flags( const QModelIndex &index ) const
146172 return QAbstractTableModel::flags ( index );
147173
148174 Qt::ItemFlags flags = QAbstractTableModel::flags ( index );
149- if ( ! mResults .at ( index.row () ).filterTitle . isEmpty () )
175+ if ( mResults .at ( index.row () ).filter )
150176 {
151177 flags = flags & ~( Qt::ItemIsSelectable | Qt::ItemIsEnabled );
152178 }
@@ -159,20 +185,29 @@ void QgsLocatorModel::addResult( const QgsLocatorResult &result )
159185 if ( mDeferredClear )
160186 {
161187 mFoundResultsFromFilterNames .clear ();
188+ mFoundResultsFilterGroups .clear ();
162189 }
163190
164191 int pos = mResults .size ();
165192 bool addingFilter = !result.filter ->displayName ().isEmpty () && !mFoundResultsFromFilterNames .contains ( result.filter ->name () );
166193 if ( addingFilter )
167194 mFoundResultsFromFilterNames << result.filter ->name ();
168195
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+ }
169204 if ( mDeferredClear )
170205 {
171206 beginResetModel ();
172207 mResults .clear ();
173208 }
174209 else
175- beginInsertRows ( QModelIndex (), pos, pos + ( addingFilter ? 1 : 0 ) );
210+ beginInsertRows ( QModelIndex (), pos, pos + ( static_cast < int >( addingFilter ) + static_cast < int >( addingGroup ) ) );
176211
177212 if ( addingFilter )
178213 {
@@ -181,8 +216,21 @@ void QgsLocatorModel::addResult( const QgsLocatorResult &result )
181216 entry.filter = result.filter ;
182217 mResults << entry;
183218 }
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+ }
184230 Entry entry;
185231 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 ();
186234 mResults << entry;
187235
188236 if ( mDeferredClear )
@@ -279,12 +327,18 @@ bool QgsLocatorProxyModel::lessThan( const QModelIndex &left, const QModelIndex
279327 if ( leftFilter != rightFilter )
280328 return QString::localeAwareCompare ( leftFilter, rightFilter ) < 0 ;
281329
282- // then make sure filter title appears before filter's results
330+ // then make sure filter title or group appears before filter's results
283331 int leftTypeRole = sourceModel ()->data ( left, QgsLocatorModel::ResultTypeRole ).toInt ();
284332 int rightTypeRole = sourceModel ()->data ( right, QgsLocatorModel::ResultTypeRole ).toInt ();
285333 if ( leftTypeRole != rightTypeRole )
286334 return leftTypeRole < rightTypeRole;
287335
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+
288342 // sort filter's results by score
289343 double leftScore = sourceModel ()->data ( left, QgsLocatorModel::ResultScoreRole ).toDouble ();
290344 double rightScore = sourceModel ()->data ( right, QgsLocatorModel::ResultScoreRole ).toDouble ();
0 commit comments