Skip to content

Commit

Permalink
[FEATURE] Add hide column, set column width and autosize to
Browse files Browse the repository at this point in the history
attribute table header right click menu
  • Loading branch information
nyalldawson committed Jun 3, 2016
1 parent 5dc1c52 commit 384a280
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 6 deletions.
23 changes: 23 additions & 0 deletions python/core/qgsattributetableconfig.sip
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ class QgsAttributeTableConfig
*/
QVector<QgsAttributeTableConfig::ColumnConfig> columns() const;

/** Returns true if the configuration is empty, ie it contains no columns.
*/
bool isEmpty() const;

/** Maps a visible column index to its original column index.
* @param visibleColumn index of visible column
* @returns corresponding index when hidden columns are considered
*/
int mapVisibleColumnToIndex( int visibleColumn ) const;

/**
* Set the list of columns visible in the attribute table.
* The list order defines the order of appearance.
Expand Down Expand Up @@ -131,4 +141,17 @@ class QgsAttributeTableConfig
* @see columnWidth()
*/
void setColumnWidth( int column, int width );

/** Returns true if the specified column is hidden.
* @param column column index
* @see setColumnHidden()
*/
bool columnHidden( int column ) const;

/** Sets whether the specified column should be hidden.
* @param column column index
* @param hidden set to true to hide column
* @see columnHidden()
*/
void setColumnHidden( int column, bool hidden );
};
6 changes: 5 additions & 1 deletion src/app/qgsattributetabledialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,11 @@ QgsAttributeTableDialog::QgsAttributeTableDialog( QgsVectorLayer *theLayer, QWid

// Initialize dual view
mMainView->init( mLayer, QgisApp::instance()->mapCanvas(), r, mEditorContext );
mMainView->setAttributeTableConfig( mLayer->attributeTableConfig() );

QgsAttributeTableConfig config = mLayer->attributeTableConfig();
if ( config.isEmpty() )
config.update( mLayer->fields() );
mMainView->setAttributeTableConfig( config );

// Initialize filter gui elements
mFilterActionMapper = new QSignalMapper( this );
Expand Down
30 changes: 30 additions & 0 deletions src/core/qgsattributetableconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,26 @@ QVector<QgsAttributeTableConfig::ColumnConfig> QgsAttributeTableConfig::columns(
return mColumns;
}

bool QgsAttributeTableConfig::isEmpty() const
{
return mColumns.isEmpty();
}

int QgsAttributeTableConfig::mapVisibleColumnToIndex( int visibleColumn ) const
{
for ( int i = 0; i < mColumns.size(); ++i )
{
if ( mColumns.at( i ).hidden )
{
visibleColumn++;
continue;
}
if ( visibleColumn == i )
return i;
}
return -1;
}

void QgsAttributeTableConfig::setColumns( const QVector<ColumnConfig>& columns )
{
mColumns = columns;
Expand Down Expand Up @@ -198,6 +218,16 @@ void QgsAttributeTableConfig::setColumnWidth( int column, int width )
mColumns[ column ].width = width;
}

bool QgsAttributeTableConfig::columnHidden( int column ) const
{
return mColumns.at( column ).hidden;
}

void QgsAttributeTableConfig::setColumnHidden( int column, bool hidden )
{
mColumns[ column ].hidden = hidden;
}

void QgsAttributeTableConfig::writeXml( QDomNode& node ) const
{
QDomDocument doc( node.ownerDocument() );
Expand Down
23 changes: 23 additions & 0 deletions src/core/qgsattributetableconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ class CORE_EXPORT QgsAttributeTableConfig
*/
QVector<ColumnConfig> columns() const;

/** Returns true if the configuration is empty, ie it contains no columns.
*/
bool isEmpty() const;

/** Maps a visible column index to its original column index.
* @param visibleColumn index of visible column
* @returns corresponding index when hidden columns are considered
*/
int mapVisibleColumnToIndex( int visibleColumn ) const;

/**
* Set the list of columns visible in the attribute table.
* The list order defines the order of appearance.
Expand Down Expand Up @@ -142,6 +152,19 @@ class CORE_EXPORT QgsAttributeTableConfig
*/
void setColumnWidth( int column, int width );

/** Returns true if the specified column is hidden.
* @param column column index
* @see setColumnHidden()
*/
bool columnHidden( int column ) const;

/** Sets whether the specified column should be hidden.
* @param column column index
* @param hidden set to true to hide column
* @see columnHidden()
*/
void setColumnHidden( int column, bool hidden );

private:
QVector<ColumnConfig> mColumns;
ActionWidgetStyle mActionWidgetStyle;
Expand Down
4 changes: 3 additions & 1 deletion src/gui/attributetable/qgsattributetablefiltermodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,9 @@ void QgsAttributeTableFilterModel::onColumnsChanged()
int QgsAttributeTableFilterModel::mapColumnToSource( int column ) const
{
if ( mColumnMapping.isEmpty() )
return 0;
return column;
if ( column < 0 || column >= mColumnMapping.size() )
return -1;
else
return mColumnMapping.at( column );
}
Expand Down
3 changes: 3 additions & 0 deletions src/gui/attributetable/qgsattributetableview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ void QgsAttributeTableView::setAttributeTableConfig( const QgsAttributeTableConf
int i = 0;
Q_FOREACH ( const QgsAttributeTableConfig::ColumnConfig& columnConfig, config.columns() )
{
if ( columnConfig.hidden )
continue;

if ( columnConfig.width >= 0 )
{
setColumnWidth( i, columnConfig.width );
Expand Down
79 changes: 75 additions & 4 deletions src/gui/attributetable/qgsdualview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <QProgressDialog>
#include <QSettings>
#include <QGroupBox>
#include <QInputDialog>

QgsDualView::QgsDualView( QWidget* parent )
: QStackedWidget( parent )
Expand Down Expand Up @@ -461,14 +462,32 @@ void QgsDualView::viewWillShowContextMenu( QMenu* menu, const QModelIndex& atInd

void QgsDualView::showViewHeaderMenu( QPoint point )
{
int col = mTableView->columnAt( point.x() );

delete mHorizontalHeaderMenu;
mHorizontalHeaderMenu = new QMenu( this );

QAction* hide = new QAction( tr( "&Hide column" ), mHorizontalHeaderMenu );
connect( hide, SIGNAL( triggered( bool ) ), this, SLOT( hideColumn() ) );
hide->setData( col );
mHorizontalHeaderMenu->addAction( hide );
QAction* setWidth = new QAction( tr( "&Set width..." ), mHorizontalHeaderMenu );
connect( setWidth, SIGNAL( triggered( bool ) ), this, SLOT( resizeColumn() ) );
setWidth->setData( col );
mHorizontalHeaderMenu->addAction( setWidth );
QAction* optimizeWidth = new QAction( tr( "&Autosize" ), mHorizontalHeaderMenu );
connect( optimizeWidth, SIGNAL( triggered( bool ) ), this, SLOT( autosizeColumn() ) );
optimizeWidth->setData( col );
mHorizontalHeaderMenu->addAction( optimizeWidth );

mHorizontalHeaderMenu->addSeparator();
QAction* organize = new QAction( tr( "&Organize columns..." ), mHorizontalHeaderMenu );
connect( organize, SIGNAL( triggered( bool ) ), this, SLOT( organizeColumns() ) );
mHorizontalHeaderMenu->addAction( organize );
QAction* sort = new QAction( tr( "&Sort..." ), mHorizontalHeaderMenu );
connect( sort, SIGNAL( triggered( bool ) ), this, SLOT( modifySort() ) );
mHorizontalHeaderMenu->addAction( sort );

mHorizontalHeaderMenu->popup( mTableView->horizontalHeader()->mapToGlobal( point ) );
}

Expand All @@ -490,9 +509,59 @@ void QgsDualView::organizeColumns()

void QgsDualView::tableColumnResized( int column, int width )
{
QgsAttributeTableConfig config = mLayerCache->layer()->attributeTableConfig();
config.setColumnWidth( column, width );
mLayerCache->layer()->setAttributeTableConfig( config );
QgsAttributeTableConfig config = mConfig;
int sourceCol = config.mapVisibleColumnToIndex( column );
if ( sourceCol >= 0 )
{
config.setColumnWidth( sourceCol, width );
mLayerCache->layer()->setAttributeTableConfig( config );
mConfig = config;
}
}

void QgsDualView::hideColumn()
{
QAction* action = qobject_cast<QAction*>( sender() );
int col = action->data().toInt();
QgsAttributeTableConfig config = mConfig;
int sourceCol = mConfig.mapVisibleColumnToIndex( col );
if ( sourceCol >= 0 )
{
config.setColumnHidden( sourceCol, true );
mLayerCache->layer()->setAttributeTableConfig( config );
setAttributeTableConfig( config );
}
}

void QgsDualView::resizeColumn()
{
QAction* action = qobject_cast<QAction*>( sender() );
int col = action->data().toInt();
if ( col < 0 )
return;

QgsAttributeTableConfig config = mConfig;
int sourceCol = config.mapVisibleColumnToIndex( col );
if ( sourceCol >= 0 )
{
bool ok = false;
int width = QInputDialog::getInt( this, tr( "Set column width" ), tr( "Enter column width" ),
mTableView->columnWidth( col ),
0, 1000, 10, &ok );
if ( ok )
{
config.setColumnWidth( sourceCol, width );
mLayerCache->layer()->setAttributeTableConfig( config );
setAttributeTableConfig( config );
}
}
}

void QgsDualView::autosizeColumn()
{
QAction* action = qobject_cast<QAction*>( sender() );
int col = action->data().toInt();
mTableView->resizeColumnToContents( col );
}

void QgsDualView::modifySort()
Expand All @@ -501,7 +570,7 @@ void QgsDualView::modifySort()
if ( !layer )
return;

QgsAttributeTableConfig config = layer->attributeTableConfig();
QgsAttributeTableConfig config = mConfig;

QDialog orderByDlg;
orderByDlg.setWindowTitle( tr( "Configure attribute table sort order" ) );
Expand Down Expand Up @@ -546,6 +615,7 @@ void QgsDualView::modifySort()
}

layer->setAttributeTableConfig( config );
mConfig = config;
}
}

Expand Down Expand Up @@ -606,6 +676,7 @@ void QgsDualView::setAttributeTableConfig( const QgsAttributeTableConfig& config
{
mFilterModel->setAttributeTableConfig( config );
mTableView->setAttributeTableConfig( config );
mConfig = config;
}

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

void tableColumnResized( int column, int width );

void hideColumn();

void resizeColumn();

void autosizeColumn();

void modifySort();

void previewExpressionChanged( const QString& expression );
Expand Down Expand Up @@ -326,6 +332,7 @@ class GUI_EXPORT QgsDualView : public QStackedWidget, private Ui::QgsDualViewBas
QgsIFeatureSelectionManager* mFeatureSelectionManager;
QgsDistanceArea mDistanceArea;
QString mDisplayExpression;
QgsAttributeTableConfig mConfig;

friend class TestQgsDualView;
};
Expand Down

0 comments on commit 384a280

Please sign in to comment.