Skip to content

Commit 7e0c438

Browse files
authoredMay 6, 2020
enhance sorting capabilities in attribute table list/form view (#36199)
1 parent 3474934 commit 7e0c438

File tree

7 files changed

+121
-25
lines changed

7 files changed

+121
-25
lines changed
 

‎images/images.qrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,7 @@
675675
<file>themes/default/multieditSameValues.svg</file>
676676
<file>themes/default/locked_repeating.svg</file>
677677
<file>themes/default/sort.svg</file>
678+
<file>themes/default/sort-reverse.svg</file>
678679
<file>themes/default/styleicons/multibandcolor.svg</file>
679680
<file>themes/default/styleicons/paletted.svg</file>
680681
<file>themes/default/styleicons/singlebandgray.svg</file>
Lines changed: 72 additions & 0 deletions
Loading

‎python/gui/auto_generated/attributetable/qgsfeaturelistmodel.sip.in

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ If ``True`` is specified, a NULL value will be injected
6262

6363
:param injectNull: state of null value injection
6464

65+
.. note::
66+
67+
If set to ``True``, the sort by display expression cannot be used
68+
6569
.. versionadded:: 2.9
6670
%End
6771

@@ -139,7 +143,7 @@ Sort this model by its display expression.
139143
.. versionadded:: 3.2
140144
%End
141145

142-
void setSortByDisplayExpression( bool sortByDisplayExpression );
146+
void setSortByDisplayExpression( bool sortByDisplayExpression, Qt::SortOrder order = Qt::AscendingOrder );
143147
%Docstring
144148
Sort this model by its display expression.
145149

‎src/gui/attributetable/qgsdualview.cpp

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,31 @@ void QgsDualView::columnBoxInit()
209209
}
210210
}
211211

212-
QAction *sortByPreviewExpression = new QAction( QgsApplication::getThemeIcon( QStringLiteral( "sort.svg" ) ), tr( "Sort by preview expression" ), this );
213-
connect( sortByPreviewExpression, &QAction::triggered, this, &QgsDualView::sortByPreviewExpression );
214-
mFeatureListPreviewButton->addAction( sortByPreviewExpression );
212+
QMenu *sortMenu = new QMenu( this );
213+
QAction *sortMenuAction = new QAction( QgsApplication::getThemeIcon( QStringLiteral( "sort.svg" ) ), tr( "Sort…" ), this );
214+
sortMenuAction->setMenu( sortMenu );
215+
216+
QAction *sortByPreviewExpressionAsc = new QAction( QgsApplication::getThemeIcon( QStringLiteral( "sort.svg" ) ), tr( "By Preview Expression (ascending)" ), this );
217+
connect( sortByPreviewExpressionAsc, &QAction::triggered, this, [ = ]()
218+
{
219+
mFeatureListModel->setSortByDisplayExpression( true, Qt::AscendingOrder );
220+
} );
221+
sortMenu->addAction( sortByPreviewExpressionAsc );
222+
QAction *sortByPreviewExpressionDesc = new QAction( QgsApplication::getThemeIcon( QStringLiteral( "sort-reverse.svg" ) ), tr( "By Preview Expression (descending)" ), this );
223+
connect( sortByPreviewExpressionDesc, &QAction::triggered, this, [ = ]()
224+
{
225+
mFeatureListModel->setSortByDisplayExpression( true, Qt::DescendingOrder );
226+
} );
227+
sortMenu->addAction( sortByPreviewExpressionDesc );
228+
QAction *sortByPreviewExpressionCustom = new QAction( QgsApplication::getThemeIcon( QStringLiteral( "mIconExpressionPreview.svg" ) ), tr( "By Custom Expression" ), this );
229+
connect( sortByPreviewExpressionCustom, &QAction::triggered, this, [ = ]()
230+
{
231+
if ( modifySort() )
232+
mFeatureListModel->setSortByDisplayExpression( false );
233+
} );
234+
sortMenu->addAction( sortByPreviewExpressionCustom );
235+
236+
mFeatureListPreviewButton->addAction( sortMenuAction );
215237

216238
QAction *separator = new QAction( mFeatureListPreviewButton );
217239
separator->setSeparator( true );
@@ -820,7 +842,7 @@ void QgsDualView::showViewHeaderMenu( QPoint point )
820842
connect( organize, &QAction::triggered, this, &QgsDualView::organizeColumns );
821843
mHorizontalHeaderMenu->addAction( organize );
822844
QAction *sort = new QAction( tr( "&Sort…" ), mHorizontalHeaderMenu );
823-
connect( sort, &QAction::triggered, this, &QgsDualView::modifySort );
845+
connect( sort, &QAction::triggered, this, [ = ]() {modifySort();} );
824846
mHorizontalHeaderMenu->addAction( sort );
825847

826848
mHorizontalHeaderMenu->popup( mTableView->horizontalHeader()->mapToGlobal( point ) );
@@ -895,10 +917,10 @@ void QgsDualView::autosizeColumn()
895917
mTableView->resizeColumnToContents( col );
896918
}
897919

898-
void QgsDualView::modifySort()
920+
bool QgsDualView::modifySort()
899921
{
900922
if ( !mLayer )
901-
return;
923+
return false;
902924

903925
QgsAttributeTableConfig config = mConfig;
904926

@@ -946,7 +968,13 @@ void QgsDualView::modifySort()
946968
}
947969

948970
setAttributeTableConfig( config );
971+
return true;
949972
}
973+
else
974+
{
975+
return false;
976+
}
977+
950978
}
951979

952980
void QgsDualView::zoomToCurrentFeature()
@@ -1025,16 +1053,6 @@ void QgsDualView::onSortColumnChanged()
10251053
}
10261054
}
10271055

1028-
void QgsDualView::sortByPreviewExpression()
1029-
{
1030-
Qt::SortOrder sortOrder = Qt::AscendingOrder;
1031-
if ( mFeatureListView->displayExpression() == sortExpression() )
1032-
{
1033-
sortOrder = mConfig.sortOrder() == Qt::AscendingOrder ? Qt::DescendingOrder : Qt::AscendingOrder;
1034-
}
1035-
setSortExpression( mFeatureListView->displayExpression(), sortOrder );
1036-
}
1037-
10381056
void QgsDualView::updateSelectedFeatures()
10391057
{
10401058
QgsFeatureRequest r = mMasterModel->request();

‎src/gui/attributetable/qgsdualview.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,14 +346,10 @@ class GUI_EXPORT QgsDualView : public QStackedWidget, private Ui::QgsDualViewBas
346346

347347
void autosizeColumn();
348348

349-
void modifySort();
350-
351349
void previewExpressionChanged( const QString &expression );
352350

353351
void onSortColumnChanged();
354352

355-
void sortByPreviewExpression();
356-
357353
void updateSelectedFeatures();
358354

359355
void extentChanged();
@@ -409,6 +405,10 @@ class GUI_EXPORT QgsDualView : public QStackedWidget, private Ui::QgsDualViewBas
409405
//! disable/enable the buttons of the browsing toolbar (feature list view)
410406
void setBrowsingAutoPanScaleAllowed( bool allowed );
411407

408+
//! Returns TRUE if the expression dialog has been accepted
409+
bool modifySort();
410+
411+
412412
QgsFieldConditionalFormatWidget *mConditionalFormatWidget = nullptr;
413413
QgsAttributeEditorContext mEditorContext;
414414
QgsAttributeTableModel *mMasterModel = nullptr;

‎src/gui/attributetable/qgsfeaturelistmodel.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,17 +281,17 @@ bool QgsFeatureListModel::sortByDisplayExpression() const
281281
return mSortByDisplayExpression;
282282
}
283283

284-
void QgsFeatureListModel::setSortByDisplayExpression( bool sortByDisplayExpression )
284+
void QgsFeatureListModel::setSortByDisplayExpression( bool sortByDisplayExpression, Qt::SortOrder order )
285285
{
286286
mSortByDisplayExpression = sortByDisplayExpression;
287287

288288
// If we are sorting by display expression, we do not support injected null
289-
if ( sortByDisplayExpression )
289+
if ( mSortByDisplayExpression )
290290
setInjectNull( false );
291291

292292
setSortRole( QgsAttributeTableModel::SortRole + 1 );
293293
setDynamicSortFilter( mSortByDisplayExpression );
294-
sort( 0 );
294+
sort( 0, order );
295295
}
296296

297297
QModelIndex QgsFeatureListModel::mapToMaster( const QModelIndex &proxyIndex ) const

‎src/gui/attributetable/qgsfeaturelistmodel.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class GUI_EXPORT QgsFeatureListModel : public QSortFilterProxyModel, public QgsF
8080
/**
8181
* \brief If TRUE is specified, a NULL value will be injected
8282
* \param injectNull state of null value injection
83+
* \note If set to TRUE, the sort by display expression cannot be used
8384
* \since QGIS 2.9
8485
*/
8586
void setInjectNull( bool injectNull );
@@ -153,7 +154,7 @@ class GUI_EXPORT QgsFeatureListModel : public QSortFilterProxyModel, public QgsF
153154
*
154155
* \since QGIS 3.2
155156
*/
156-
void setSortByDisplayExpression( bool sortByDisplayExpression );
157+
void setSortByDisplayExpression( bool sortByDisplayExpression, Qt::SortOrder order = Qt::AscendingOrder );
157158

158159
public slots:
159160

0 commit comments

Comments
 (0)
Please sign in to comment.