Skip to content
Permalink
Browse files

[FEATURE] Add hide column, set column width and autosize to

attribute table header right click menu
  • Loading branch information
nyalldawson committed Jun 3, 2016
1 parent 5dc1c52 commit 384a280f7131820d80bee027d446c1da568742d2
@@ -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.
@@ -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 );
};
@@ -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 );
@@ -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;
@@ -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() );
@@ -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.
@@ -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;
@@ -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 );
}
@@ -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 );
@@ -34,6 +34,7 @@
#include <QProgressDialog>
#include <QSettings>
#include <QGroupBox>
#include <QInputDialog>

QgsDualView::QgsDualView( QWidget* parent )
: QStackedWidget( parent )
@@ -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 ) );
}

@@ -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()
@@ -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" ) );
@@ -546,6 +615,7 @@ void QgsDualView::modifySort()
}

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

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

void QgsDualView::setSortExpression( const QString& sortExpression )
@@ -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 );
@@ -326,6 +332,7 @@ class GUI_EXPORT QgsDualView : public QStackedWidget, private Ui::QgsDualViewBas
QgsIFeatureSelectionManager* mFeatureSelectionManager;
QgsDistanceArea mDistanceArea;
QString mDisplayExpression;
QgsAttributeTableConfig mConfig;

friend class TestQgsDualView;
};

0 comments on commit 384a280

Please sign in to comment.
You can’t perform that action at this time.