From b83aab7d74be273724f94e2a734eb7b6397cb9a6 Mon Sep 17 00:00:00 2001 From: Matthias Kuhn Date: Thu, 7 Jul 2016 17:25:54 +0200 Subject: [PATCH] Save attribute table sort order persistently Fix #15235 --- python/core/qgsattributetableconfig.sip | 12 +++++++ python/gui/attributetable/qgsdualview.sip | 3 +- src/core/qgsattributetableconfig.cpp | 15 ++++++++- src/core/qgsattributetableconfig.h | 13 ++++++++ .../qgsattributetablefiltermodel.cpp | 2 +- src/gui/attributetable/qgsdualview.cpp | 33 ++++++++++++------- src/gui/attributetable/qgsdualview.h | 2 +- 7 files changed, 65 insertions(+), 15 deletions(-) diff --git a/python/core/qgsattributetableconfig.sip b/python/core/qgsattributetableconfig.sip index 7884aaeee84..9c7290ff98e 100644 --- a/python/core/qgsattributetableconfig.sip +++ b/python/core/qgsattributetableconfig.sip @@ -154,4 +154,16 @@ class QgsAttributeTableConfig * @see columnHidden() */ void setColumnHidden( int column, bool hidden ); + + /** + * Get the sort order + * @note Added in 2.16 + */ + Qt::SortOrder sortOrder() const; + + /** + * Set the sort order + * @note Added in 2.16 + */ + void setSortOrder( const Qt::SortOrder& sortOrder ); }; diff --git a/python/gui/attributetable/qgsdualview.sip b/python/gui/attributetable/qgsdualview.sip index cbb110f9e69..d67105b1561 100644 --- a/python/gui/attributetable/qgsdualview.sip +++ b/python/gui/attributetable/qgsdualview.sip @@ -135,6 +135,7 @@ class QgsDualView : QStackedWidget * @return The table view */ QgsAttributeTableView* tableView(); + /** * Set the attribute table config which should be used to control * the appearance of the attribute table. @@ -144,7 +145,7 @@ class QgsDualView : QStackedWidget /** * Set the expression used for sorting the table and feature list. */ - void setSortExpression( const QString& sortExpression ); + void setSortExpression( const QString& sortExpression , Qt::SortOrder sortOrder = Qt::AscendingOrder ); /** * Get the expression used for sorting the table and feature list. diff --git a/src/core/qgsattributetableconfig.cpp b/src/core/qgsattributetableconfig.cpp index cbaf47efb0f..6f16208307a 100644 --- a/src/core/qgsattributetableconfig.cpp +++ b/src/core/qgsattributetableconfig.cpp @@ -196,6 +196,7 @@ void QgsAttributeTableConfig::readXml( const QDomNode& node ) } mSortExpression = configNode.toElement().attribute( "sortExpression" ); + mSortOrder = static_cast( configNode.toElement().attribute( "sortOrder" ).toInt() ); } QString QgsAttributeTableConfig::sortExpression() const @@ -230,7 +231,17 @@ void QgsAttributeTableConfig::setColumnHidden( int column, bool hidden ) bool QgsAttributeTableConfig::operator!=( const QgsAttributeTableConfig& other ) const { - return mSortExpression != other.mSortExpression || mColumns != other.mColumns || mActionWidgetStyle != other.mActionWidgetStyle; + return mSortExpression != other.mSortExpression || mColumns != other.mColumns || mActionWidgetStyle != other.mActionWidgetStyle || mSortOrder != other.mSortOrder; +} + +Qt::SortOrder QgsAttributeTableConfig::sortOrder() const +{ + return mSortOrder; +} + +void QgsAttributeTableConfig::setSortOrder( const Qt::SortOrder& sortOrder ) +{ + mSortOrder = sortOrder; } void QgsAttributeTableConfig::writeXml( QDomNode& node ) const @@ -242,6 +253,8 @@ void QgsAttributeTableConfig::writeXml( QDomNode& node ) const configElement.setAttribute( "sortExpression", mSortExpression ); + configElement.setAttribute( "sortOrder", mSortOrder ); + QDomElement columnsElement = doc.createElement( "columns" ); Q_FOREACH ( const ColumnConfig& column, mColumns ) diff --git a/src/core/qgsattributetableconfig.h b/src/core/qgsattributetableconfig.h index 5036300ded0..9621ffd72d7 100644 --- a/src/core/qgsattributetableconfig.h +++ b/src/core/qgsattributetableconfig.h @@ -167,6 +167,18 @@ class CORE_EXPORT QgsAttributeTableConfig */ void setColumnHidden( int column, bool hidden ); + /** + * Get the sort order + * @note Added in 2.16 + */ + Qt::SortOrder sortOrder() const; + + /** + * Set the sort order + * @note Added in 2.16 + */ + void setSortOrder( const Qt::SortOrder& sortOrder ); + /** * Compare this configuration to other. */ @@ -176,6 +188,7 @@ class CORE_EXPORT QgsAttributeTableConfig QVector mColumns; ActionWidgetStyle mActionWidgetStyle; QString mSortExpression; + Qt::SortOrder mSortOrder; }; Q_DECLARE_METATYPE( QgsAttributeTableConfig::ColumnConfig ) diff --git a/src/gui/attributetable/qgsattributetablefiltermodel.cpp b/src/gui/attributetable/qgsattributetablefiltermodel.cpp index 2a3b1f6c48c..9ade8a23def 100644 --- a/src/gui/attributetable/qgsattributetablefiltermodel.cpp +++ b/src/gui/attributetable/qgsattributetablefiltermodel.cpp @@ -205,7 +205,7 @@ void QgsAttributeTableFilterModel::setAttributeTableConfig( const QgsAttributeTa } } - sort( config.sortExpression() ); + sort( config.sortExpression(), config.sortOrder() ); } void QgsAttributeTableFilterModel::sort( QString expression, Qt::SortOrder order ) diff --git a/src/gui/attributetable/qgsdualview.cpp b/src/gui/attributetable/qgsdualview.cpp index 0f1dec4e310..63968b713be 100644 --- a/src/gui/attributetable/qgsdualview.cpp +++ b/src/gui/attributetable/qgsdualview.cpp @@ -591,7 +591,7 @@ void QgsDualView::modifySort() orderByDlg.setLayout( layout ); QGroupBox* sortingGroupBox = new QGroupBox(); - sortingGroupBox->setTitle( tr( "Enable sorting order in attribute table" ) ); + sortingGroupBox->setTitle( tr( "Defined sort order in attribute table" ) ); sortingGroupBox->setCheckable( true ); sortingGroupBox->setChecked( !sortExpression().isEmpty() ); layout->addWidget( sortingGroupBox ); @@ -610,22 +610,27 @@ void QgsDualView::modifySort() sortingGroupBox->layout()->addWidget( expressionBuilder ); + QCheckBox* cbxSortAscending = new QCheckBox( tr( "Sort ascending" ) ); + cbxSortAscending->setChecked( config.sortOrder() == Qt::AscendingOrder ); + sortingGroupBox->layout()->addWidget( cbxSortAscending ); + layout->addWidget( dialogButtonBox ); if ( orderByDlg.exec() ) { + Qt::SortOrder sortOrder = cbxSortAscending->isChecked() ? Qt::AscendingOrder : Qt::DescendingOrder; if ( sortingGroupBox->isChecked() ) { - setSortExpression( expressionBuilder->expressionText() ); + setSortExpression( expressionBuilder->expressionText(), sortOrder ); config.setSortExpression( expressionBuilder->expressionText() ); + config.setSortOrder( sortOrder ); } else { - setSortExpression( QString() ); + setSortExpression( QString(), sortOrder ); config.setSortExpression( QString() ); } - layer->setAttributeTableConfig( config ); - mConfig = config; + setAttributeTableConfig( config ); } } @@ -655,12 +660,18 @@ void QgsDualView::onSortColumnChanged() { QgsAttributeTableConfig cfg = mLayerCache->layer()->attributeTableConfig(); cfg.setSortExpression( mFilterModel->sortExpression() ); + cfg.setSortOrder( mFilterModel->sortOrder() ); mLayerCache->layer()->setAttributeTableConfig( cfg ); } void QgsDualView::sortByPreviewExpression() { - setSortExpression( mFeatureList->displayExpression() ); + Qt::SortOrder sortOrder = Qt::AscendingOrder; + if ( mFeatureList->displayExpression() == sortExpression() ) + { + sortOrder = mConfig.sortOrder() == Qt::AscendingOrder ? Qt::DescendingOrder : Qt::AscendingOrder; + } + setSortExpression( mFeatureList->displayExpression(), sortOrder ); } void QgsDualView::featureFormAttributeChanged() @@ -697,16 +708,16 @@ void QgsDualView::setAttributeTableConfig( const QgsAttributeTableConfig& config mConfig = config; } -void QgsDualView::setSortExpression( const QString& sortExpression ) +void QgsDualView::setSortExpression( const QString& sortExpression, Qt::SortOrder sortOrder ) { if ( sortExpression.isNull() ) mFilterModel->sort( -1 ); else - mFilterModel->sort( sortExpression ); + mFilterModel->sort( sortExpression, sortOrder ); - QgsAttributeTableConfig cfg = mLayerCache->layer()->attributeTableConfig(); - cfg.setSortExpression( sortExpression ); - mLayerCache->layer()->setAttributeTableConfig( cfg ); + mConfig.setSortExpression( sortExpression ); + mConfig.setSortOrder( sortOrder ); + mLayerCache->layer()->setAttributeTableConfig( mConfig ); } QString QgsDualView::sortExpression() const diff --git a/src/gui/attributetable/qgsdualview.h b/src/gui/attributetable/qgsdualview.h index e10a21bdbb7..4e6a96e0de4 100644 --- a/src/gui/attributetable/qgsdualview.h +++ b/src/gui/attributetable/qgsdualview.h @@ -185,7 +185,7 @@ class GUI_EXPORT QgsDualView : public QStackedWidget, private Ui::QgsDualViewBas /** * Set the expression used for sorting the table and feature list. */ - void setSortExpression( const QString& sortExpression ); + void setSortExpression( const QString& sortExpression , Qt::SortOrder sortOrder = Qt::AscendingOrder ); /** * Get the expression used for sorting the table and feature list.