Skip to content

Commit

Permalink
Save attribute table sort order persistently
Browse files Browse the repository at this point in the history
Fix #15235
  • Loading branch information
m-kuhn committed Jul 7, 2016
1 parent febcabb commit b83aab7
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 15 deletions.
12 changes: 12 additions & 0 deletions python/core/qgsattributetableconfig.sip
Expand Up @@ -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 );
};
3 changes: 2 additions & 1 deletion python/gui/attributetable/qgsdualview.sip
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
15 changes: 14 additions & 1 deletion src/core/qgsattributetableconfig.cpp
Expand Up @@ -196,6 +196,7 @@ void QgsAttributeTableConfig::readXml( const QDomNode& node )
}

mSortExpression = configNode.toElement().attribute( "sortExpression" );
mSortOrder = static_cast<Qt::SortOrder>( configNode.toElement().attribute( "sortOrder" ).toInt() );
}

QString QgsAttributeTableConfig::sortExpression() const
Expand Down Expand Up @@ -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
Expand All @@ -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 )
Expand Down
13 changes: 13 additions & 0 deletions src/core/qgsattributetableconfig.h
Expand Up @@ -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.
*/
Expand All @@ -176,6 +188,7 @@ class CORE_EXPORT QgsAttributeTableConfig
QVector<ColumnConfig> mColumns;
ActionWidgetStyle mActionWidgetStyle;
QString mSortExpression;
Qt::SortOrder mSortOrder;
};

Q_DECLARE_METATYPE( QgsAttributeTableConfig::ColumnConfig )
Expand Down
2 changes: 1 addition & 1 deletion src/gui/attributetable/qgsattributetablefiltermodel.cpp
Expand Up @@ -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 )
Expand Down
33 changes: 22 additions & 11 deletions src/gui/attributetable/qgsdualview.cpp
Expand Up @@ -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 );
Expand All @@ -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 );
}
}

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/gui/attributetable/qgsdualview.h
Expand Up @@ -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.
Expand Down

0 comments on commit b83aab7

Please sign in to comment.