Skip to content

Commit

Permalink
Persist attribute table sort order when clicking on column header
Browse files Browse the repository at this point in the history
And when sorting by preview expression
  • Loading branch information
m-kuhn committed Jun 23, 2016
1 parent 4d48072 commit 3661f92
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 2 deletions.
1 change: 0 additions & 1 deletion src/app/qgsattributetabledialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "qgsattributetablemodel.h"
#include "qgsattributetablefiltermodel.h"
#include "qgsattributetableview.h"
#include "qgsorganizetablecolumnsdialog.h"

#include "qgsapplication.h"
#include "qgsvectorlayer.h"
Expand Down
1 change: 1 addition & 0 deletions src/gui/attributetable/qgsattributetablefiltermodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ void QgsAttributeTableFilterModel::sort( int column, Qt::SortOrder order )
int myColumn = mColumnMapping.at( column );
masterModel()->prefetchColumnData( myColumn );
QSortFilterProxyModel::sort( myColumn, order );
emit sortColumnChanged( column, order );
}

QVariant QgsAttributeTableFilterModel::data( const QModelIndex& index, int role ) const
Expand Down
8 changes: 8 additions & 0 deletions src/gui/attributetable/qgsattributetablefiltermodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,14 @@ class GUI_EXPORT QgsAttributeTableFilterModel: public QSortFilterProxyModel, pub
*/
void setAttributeTableConfig( const QgsAttributeTableConfig& config );

signals:
/**
* Is emitted whenever the sort column is changed
* @param column The sort column
* @param order The sort order
*/
void sortColumnChanged( int column, Qt::SortOrder order );

protected:
/**
* Returns true if the source row will be accepted
Expand Down
14 changes: 13 additions & 1 deletion src/gui/attributetable/qgsdualview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ void QgsDualView::init( QgsVectorLayer* layer, QgsMapCanvas* mapCanvas, const Qg
connect( mAttributeForm, SIGNAL( modeChanged( QgsAttributeForm::Mode ) ), this, SIGNAL( formModeChanged( QgsAttributeForm::Mode ) ) );
connect( mMasterModel, SIGNAL( modelChanged() ), mAttributeForm, SLOT( refreshFeature() ) );
connect( mAttributeForm, SIGNAL( filterExpressionSet( QString, QgsAttributeForm::FilterType ) ), this, SIGNAL( filterExpressionSet( QString, QgsAttributeForm::FilterType ) ) );
connect( mFilterModel, SIGNAL( sortColumnChanged( int, Qt::SortOrder ) ), this, SLOT( onSortColumnChanged() ) );
if ( mFeatureListPreviewButton->defaultAction() )
mFeatureList->setDisplayExpression( mDisplayExpression );
else
Expand Down Expand Up @@ -512,7 +513,6 @@ void QgsDualView::organizeColumns()
if ( dialog.exec() == QDialog::Accepted )
{
QgsAttributeTableConfig config = dialog.config();
mLayerCache->layer()->setAttributeTableConfig( config );
setAttributeTableConfig( config );
}
}
Expand Down Expand Up @@ -651,6 +651,13 @@ void QgsDualView::previewExpressionChanged( const QString& expression )
mLayerCache->layer()->setDisplayExpression( expression );
}

void QgsDualView::onSortColumnChanged()
{
QgsAttributeTableConfig cfg = mLayerCache->layer()->attributeTableConfig();
cfg.setSortExpression( mFilterModel->sortExpression() );
mLayerCache->layer()->setAttributeTableConfig( cfg );
}

void QgsDualView::sortByPreviewExpression()
{
setSortExpression( mFeatureList->displayExpression() );
Expand Down Expand Up @@ -684,6 +691,7 @@ void QgsDualView::setFeatureSelectionManager( QgsIFeatureSelectionManager* featu

void QgsDualView::setAttributeTableConfig( const QgsAttributeTableConfig& config )
{
mLayerCache->layer()->setAttributeTableConfig( config );
mFilterModel->setAttributeTableConfig( config );
mTableView->setAttributeTableConfig( config );
mConfig = config;
Expand All @@ -695,6 +703,10 @@ void QgsDualView::setSortExpression( const QString& sortExpression )
mFilterModel->sort( -1 );
else
mFilterModel->sort( sortExpression );

QgsAttributeTableConfig cfg = mLayerCache->layer()->attributeTableConfig();
cfg.setSortExpression( sortExpression );
mLayerCache->layer()->setAttributeTableConfig( cfg );
}

QString QgsDualView::sortExpression() const
Expand Down
2 changes: 2 additions & 0 deletions src/gui/attributetable/qgsdualview.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,8 @@ class GUI_EXPORT QgsDualView : public QStackedWidget, private Ui::QgsDualViewBas

void previewExpressionChanged( const QString& expression );

void onSortColumnChanged();

void sortByPreviewExpression();

/**
Expand Down

0 comments on commit 3661f92

Please sign in to comment.