Skip to content
Permalink
Browse files

Add new actions to the attribute table header context menu

Add 2 new actions to the attribute table header context menu.
The first allows setting a width to be applied to all columns,
the second optimizes column width for all columns by autosizing all
columns to contents.

These actions complement the existing actions "Set Width" and "Autosize"
but are applied to all columns instead of only the column which is
right-clicked on to show the context menu.
  • Loading branch information
benwirf committed Feb 23, 2021
1 parent 43cbe1e commit 7e94cba23cc4ccdd187e7d5892868d04bba6160c
Showing with 47 additions and 0 deletions.
  1. +43 −0 src/gui/attributetable/qgsdualview.cpp
  2. +4 −0 src/gui/attributetable/qgsdualview.h
@@ -878,11 +878,22 @@ void QgsDualView::showViewHeaderMenu( QPoint point )
connect( setWidth, &QAction::triggered, this, &QgsDualView::resizeColumn );
setWidth->setData( col );
mHorizontalHeaderMenu->addAction( setWidth );

QAction *setWidthAllColumns = new QAction( tr( "&Set All Column Widths…" ), mHorizontalHeaderMenu );
connect( setWidthAllColumns, &QAction::triggered, this, &QgsDualView::resizeAllColumns );
setWidthAllColumns->setData( col );
mHorizontalHeaderMenu->addAction( setWidthAllColumns );

QAction *optimizeWidth = new QAction( tr( "&Autosize" ), mHorizontalHeaderMenu );
connect( optimizeWidth, &QAction::triggered, this, &QgsDualView::autosizeColumn );
optimizeWidth->setData( col );
mHorizontalHeaderMenu->addAction( optimizeWidth );

QAction *optimizeWidthAllColumns = new QAction( tr( "&Autosize All Columns" ), mHorizontalHeaderMenu );
connect( optimizeWidthAllColumns, &QAction::triggered, this, &QgsDualView::autosizeAllColumns );
mHorizontalHeaderMenu->addAction( optimizeWidthAllColumns );


mHorizontalHeaderMenu->addSeparator();
QAction *organize = new QAction( tr( "&Organize Columns…" ), mHorizontalHeaderMenu );
connect( organize, &QAction::triggered, this, &QgsDualView::organizeColumns );
@@ -956,13 +967,45 @@ void QgsDualView::resizeColumn()
}
}

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

QgsAttributeTableConfig config = mConfig;

bool ok = false;
int width = QInputDialog::getInt( this, tr( "Set Column Width" ), tr( "Enter column width" ),
mTableView->columnWidth( col ),
1, 1000, 10, &ok );
if ( ok )
{
const int colCount = mTableView->model()->columnCount();
if ( colCount > 0 )
{
for ( int i = 0; i < colCount; i++ )
{
config.setColumnWidth( i, width );
}
setAttributeTableConfig( config );
}
}
}

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

void QgsDualView::autosizeAllColumns()
{
mTableView->resizeColumnsToContents();
}

bool QgsDualView::modifySort()
{
if ( !mLayer )
@@ -344,8 +344,12 @@ class GUI_EXPORT QgsDualView : public QStackedWidget, private Ui::QgsDualViewBas

void resizeColumn();

void resizeAllColumns();

void autosizeColumn();

void autosizeAllColumns();

void previewExpressionChanged( const QString &expression );

void onSortColumnChanged();

0 comments on commit 7e94cba

Please sign in to comment.