Skip to content

Commit

Permalink
enhance sorting capabilities in attribute table list/form view (#36199)
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed May 6, 2020
1 parent 3474934 commit 7e0c438
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 25 deletions.
1 change: 1 addition & 0 deletions images/images.qrc
Expand Up @@ -675,6 +675,7 @@
<file>themes/default/multieditSameValues.svg</file>
<file>themes/default/locked_repeating.svg</file>
<file>themes/default/sort.svg</file>
<file>themes/default/sort-reverse.svg</file>
<file>themes/default/styleicons/multibandcolor.svg</file>
<file>themes/default/styleicons/paletted.svg</file>
<file>themes/default/styleicons/singlebandgray.svg</file>
Expand Down
72 changes: 72 additions & 0 deletions images/themes/default/sort-reverse.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Expand Up @@ -62,6 +62,10 @@ If ``True`` is specified, a NULL value will be injected

:param injectNull: state of null value injection

.. note::

If set to ``True``, the sort by display expression cannot be used

.. versionadded:: 2.9
%End

Expand Down Expand Up @@ -139,7 +143,7 @@ Sort this model by its display expression.
.. versionadded:: 3.2
%End

void setSortByDisplayExpression( bool sortByDisplayExpression );
void setSortByDisplayExpression( bool sortByDisplayExpression, Qt::SortOrder order = Qt::AscendingOrder );
%Docstring
Sort this model by its display expression.

Expand Down
50 changes: 34 additions & 16 deletions src/gui/attributetable/qgsdualview.cpp
Expand Up @@ -209,9 +209,31 @@ void QgsDualView::columnBoxInit()
}
}

QAction *sortByPreviewExpression = new QAction( QgsApplication::getThemeIcon( QStringLiteral( "sort.svg" ) ), tr( "Sort by preview expression" ), this );
connect( sortByPreviewExpression, &QAction::triggered, this, &QgsDualView::sortByPreviewExpression );
mFeatureListPreviewButton->addAction( sortByPreviewExpression );
QMenu *sortMenu = new QMenu( this );
QAction *sortMenuAction = new QAction( QgsApplication::getThemeIcon( QStringLiteral( "sort.svg" ) ), tr( "Sort…" ), this );
sortMenuAction->setMenu( sortMenu );

QAction *sortByPreviewExpressionAsc = new QAction( QgsApplication::getThemeIcon( QStringLiteral( "sort.svg" ) ), tr( "By Preview Expression (ascending)" ), this );
connect( sortByPreviewExpressionAsc, &QAction::triggered, this, [ = ]()
{
mFeatureListModel->setSortByDisplayExpression( true, Qt::AscendingOrder );
} );
sortMenu->addAction( sortByPreviewExpressionAsc );
QAction *sortByPreviewExpressionDesc = new QAction( QgsApplication::getThemeIcon( QStringLiteral( "sort-reverse.svg" ) ), tr( "By Preview Expression (descending)" ), this );
connect( sortByPreviewExpressionDesc, &QAction::triggered, this, [ = ]()
{
mFeatureListModel->setSortByDisplayExpression( true, Qt::DescendingOrder );
} );
sortMenu->addAction( sortByPreviewExpressionDesc );
QAction *sortByPreviewExpressionCustom = new QAction( QgsApplication::getThemeIcon( QStringLiteral( "mIconExpressionPreview.svg" ) ), tr( "By Custom Expression" ), this );
connect( sortByPreviewExpressionCustom, &QAction::triggered, this, [ = ]()
{
if ( modifySort() )
mFeatureListModel->setSortByDisplayExpression( false );
} );
sortMenu->addAction( sortByPreviewExpressionCustom );

mFeatureListPreviewButton->addAction( sortMenuAction );

QAction *separator = new QAction( mFeatureListPreviewButton );
separator->setSeparator( true );
Expand Down Expand Up @@ -820,7 +842,7 @@ void QgsDualView::showViewHeaderMenu( QPoint point )
connect( organize, &QAction::triggered, this, &QgsDualView::organizeColumns );
mHorizontalHeaderMenu->addAction( organize );
QAction *sort = new QAction( tr( "&Sort…" ), mHorizontalHeaderMenu );
connect( sort, &QAction::triggered, this, &QgsDualView::modifySort );
connect( sort, &QAction::triggered, this, [ = ]() {modifySort();} );
mHorizontalHeaderMenu->addAction( sort );

mHorizontalHeaderMenu->popup( mTableView->horizontalHeader()->mapToGlobal( point ) );
Expand Down Expand Up @@ -895,10 +917,10 @@ void QgsDualView::autosizeColumn()
mTableView->resizeColumnToContents( col );
}

void QgsDualView::modifySort()
bool QgsDualView::modifySort()
{
if ( !mLayer )
return;
return false;

QgsAttributeTableConfig config = mConfig;

Expand Down Expand Up @@ -946,7 +968,13 @@ void QgsDualView::modifySort()
}

setAttributeTableConfig( config );
return true;
}
else
{
return false;
}

}

void QgsDualView::zoomToCurrentFeature()
Expand Down Expand Up @@ -1025,16 +1053,6 @@ void QgsDualView::onSortColumnChanged()
}
}

void QgsDualView::sortByPreviewExpression()
{
Qt::SortOrder sortOrder = Qt::AscendingOrder;
if ( mFeatureListView->displayExpression() == sortExpression() )
{
sortOrder = mConfig.sortOrder() == Qt::AscendingOrder ? Qt::DescendingOrder : Qt::AscendingOrder;
}
setSortExpression( mFeatureListView->displayExpression(), sortOrder );
}

void QgsDualView::updateSelectedFeatures()
{
QgsFeatureRequest r = mMasterModel->request();
Expand Down
8 changes: 4 additions & 4 deletions src/gui/attributetable/qgsdualview.h
Expand Up @@ -346,14 +346,10 @@ class GUI_EXPORT QgsDualView : public QStackedWidget, private Ui::QgsDualViewBas

void autosizeColumn();

void modifySort();

void previewExpressionChanged( const QString &expression );

void onSortColumnChanged();

void sortByPreviewExpression();

void updateSelectedFeatures();

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

//! Returns TRUE if the expression dialog has been accepted
bool modifySort();


QgsFieldConditionalFormatWidget *mConditionalFormatWidget = nullptr;
QgsAttributeEditorContext mEditorContext;
QgsAttributeTableModel *mMasterModel = nullptr;
Expand Down
6 changes: 3 additions & 3 deletions src/gui/attributetable/qgsfeaturelistmodel.cpp
Expand Up @@ -281,17 +281,17 @@ bool QgsFeatureListModel::sortByDisplayExpression() const
return mSortByDisplayExpression;
}

void QgsFeatureListModel::setSortByDisplayExpression( bool sortByDisplayExpression )
void QgsFeatureListModel::setSortByDisplayExpression( bool sortByDisplayExpression, Qt::SortOrder order )
{
mSortByDisplayExpression = sortByDisplayExpression;

// If we are sorting by display expression, we do not support injected null
if ( sortByDisplayExpression )
if ( mSortByDisplayExpression )
setInjectNull( false );

setSortRole( QgsAttributeTableModel::SortRole + 1 );
setDynamicSortFilter( mSortByDisplayExpression );
sort( 0 );
sort( 0, order );
}

QModelIndex QgsFeatureListModel::mapToMaster( const QModelIndex &proxyIndex ) const
Expand Down
3 changes: 2 additions & 1 deletion src/gui/attributetable/qgsfeaturelistmodel.h
Expand Up @@ -80,6 +80,7 @@ class GUI_EXPORT QgsFeatureListModel : public QSortFilterProxyModel, public QgsF
/**
* \brief If TRUE is specified, a NULL value will be injected
* \param injectNull state of null value injection
* \note If set to TRUE, the sort by display expression cannot be used
* \since QGIS 2.9
*/
void setInjectNull( bool injectNull );
Expand Down Expand Up @@ -153,7 +154,7 @@ class GUI_EXPORT QgsFeatureListModel : public QSortFilterProxyModel, public QgsF
*
* \since QGIS 3.2
*/
void setSortByDisplayExpression( bool sortByDisplayExpression );
void setSortByDisplayExpression( bool sortByDisplayExpression, Qt::SortOrder order = Qt::AscendingOrder );

public slots:

Expand Down

0 comments on commit 7e0c438

Please sign in to comment.