Skip to content

Commit

Permalink
Allow reordering fields in new vector layer dialogs
Browse files Browse the repository at this point in the history
(including scratch, shp, gpkg and spatialite)

Fixes qgis#38241
  • Loading branch information
nyalldawson committed Aug 26, 2024
1 parent 450966f commit d9d2d5c
Show file tree
Hide file tree
Showing 12 changed files with 946 additions and 591 deletions.
27 changes: 26 additions & 1 deletion src/app/qgsnewspatialitelayerdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,14 @@ QgsNewSpatialiteLayerDialog::QgsNewSpatialiteLayerDialog( QWidget *parent, Qt::W
connect( toolButtonNewDatabase, &QToolButton::clicked, this, &QgsNewSpatialiteLayerDialog::createDb );
connect( buttonBox, &QDialogButtonBox::accepted, this, &QgsNewSpatialiteLayerDialog::buttonBox_accepted );
connect( buttonBox, &QDialogButtonBox::rejected, this, &QgsNewSpatialiteLayerDialog::buttonBox_rejected );

connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsNewSpatialiteLayerDialog::showHelp );
connect( mButtonUp, &QToolButton::clicked, this, &QgsNewSpatialiteLayerDialog::moveFieldsUp );
connect( mButtonDown, &QToolButton::clicked, this, &QgsNewSpatialiteLayerDialog::moveFieldsDown );

mAddAttributeButton->setEnabled( false );
mRemoveAttributeButton->setEnabled( false );
mButtonUp->setEnabled( false );
mButtonDown->setEnabled( false );
}

void QgsNewSpatialiteLayerDialog::mGeometryTypeBox_currentIndexChanged( int index )
Expand Down Expand Up @@ -253,6 +256,8 @@ void QgsNewSpatialiteLayerDialog::nameChanged( const QString &name )
void QgsNewSpatialiteLayerDialog::selectionChanged()
{
mRemoveAttributeButton->setDisabled( mAttributeView->selectedItems().isEmpty() );
mButtonUp->setDisabled( mAttributeView->selectedItems().isEmpty() );
mButtonDown->setDisabled( mAttributeView->selectedItems().isEmpty() );
}

bool QgsNewSpatialiteLayerDialog::createDb()
Expand Down Expand Up @@ -486,3 +491,23 @@ void QgsNewSpatialiteLayerDialog::showHelp()
{
QgsHelp::openHelp( QStringLiteral( "managing_data_source/create_layers.html#creating-a-new-spatialite-layer" ) );
}

void QgsNewSpatialiteLayerDialog::moveFieldsUp()
{
int currentRow = mAttributeView->currentIndex().row();
if ( currentRow == 0 )
return;

mAttributeView->insertTopLevelItem( currentRow - 1, mAttributeView->takeTopLevelItem( currentRow ) );
mAttributeView->setCurrentIndex( mAttributeView->model()->index( currentRow - 1, 0 ) );
}

void QgsNewSpatialiteLayerDialog::moveFieldsDown()
{
int currentRow = mAttributeView->currentIndex().row();
if ( currentRow == mAttributeView->topLevelItemCount() - 1 )
return;

mAttributeView->insertTopLevelItem( currentRow + 1, mAttributeView->takeTopLevelItem( currentRow ) );
mAttributeView->setCurrentIndex( mAttributeView->model()->index( currentRow + 1, 0 ) );
}
2 changes: 2 additions & 0 deletions src/app/qgsnewspatialitelayerdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class APP_EXPORT QgsNewSpatialiteLayerDialog: public QDialog, private Ui::QgsNew
bool apply();

void showHelp();
void moveFieldsUp();
void moveFieldsDown();

static QString quotedIdentifier( QString id );

Expand Down
26 changes: 26 additions & 0 deletions src/gui/qgsnewgeopackagelayerdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ QgsNewGeoPackageLayerDialog::QgsNewGeoPackageLayerDialog( QWidget *parent, Qt::W
connect( buttonBox, &QDialogButtonBox::accepted, this, &QgsNewGeoPackageLayerDialog::buttonBox_accepted );
connect( buttonBox, &QDialogButtonBox::rejected, this, &QgsNewGeoPackageLayerDialog::buttonBox_rejected );
connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsNewGeoPackageLayerDialog::showHelp );
connect( mButtonUp, &QToolButton::clicked, this, &QgsNewGeoPackageLayerDialog::moveFieldsUp );
connect( mButtonDown, &QToolButton::clicked, this, &QgsNewGeoPackageLayerDialog::moveFieldsDown );

mAddAttributeButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionNewAttribute.svg" ) ) );
mRemoveAttributeButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionDeleteAttribute.svg" ) ) );
Expand Down Expand Up @@ -120,6 +122,8 @@ QgsNewGeoPackageLayerDialog::QgsNewGeoPackageLayerDialog( QWidget *parent, Qt::W

mAddAttributeButton->setEnabled( false );
mRemoveAttributeButton->setEnabled( false );
mButtonUp->setEnabled( false );
mButtonDown->setEnabled( false );

mCheckBoxCreateSpatialIndex->setChecked( true );

Expand Down Expand Up @@ -251,6 +255,8 @@ void QgsNewGeoPackageLayerDialog::fieldNameChanged( const QString &name )
void QgsNewGeoPackageLayerDialog::selectionChanged()
{
mRemoveAttributeButton->setDisabled( mAttributeView->selectedItems().isEmpty() );
mButtonUp->setDisabled( mAttributeView->selectedItems().isEmpty() );
mButtonDown->setDisabled( mAttributeView->selectedItems().isEmpty() );
}

void QgsNewGeoPackageLayerDialog::buttonBox_accepted()
Expand All @@ -264,6 +270,26 @@ void QgsNewGeoPackageLayerDialog::buttonBox_rejected()
reject();
}

void QgsNewGeoPackageLayerDialog::moveFieldsUp()
{
int currentRow = mAttributeView->currentIndex().row();
if ( currentRow == 0 )
return;

mAttributeView->insertTopLevelItem( currentRow - 1, mAttributeView->takeTopLevelItem( currentRow ) );
mAttributeView->setCurrentIndex( mAttributeView->model()->index( currentRow - 1, 0 ) );
}

void QgsNewGeoPackageLayerDialog::moveFieldsDown()
{
int currentRow = mAttributeView->currentIndex().row();
if ( currentRow == mAttributeView->topLevelItemCount() - 1 )
return;

mAttributeView->insertTopLevelItem( currentRow + 1, mAttributeView->takeTopLevelItem( currentRow ) );
mAttributeView->setCurrentIndex( mAttributeView->model()->index( currentRow + 1, 0 ) );
}

bool QgsNewGeoPackageLayerDialog::apply()
{
if ( !mFieldNameEdit->text().trimmed().isEmpty() )
Expand Down
2 changes: 2 additions & 0 deletions src/gui/qgsnewgeopackagelayerdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ class GUI_EXPORT QgsNewGeoPackageLayerDialog: public QDialog, private Ui::QgsNew
void showHelp();
void buttonBox_accepted();
void buttonBox_rejected();
void moveFieldsUp();
void moveFieldsDown();

private:
bool apply();
Expand Down
26 changes: 26 additions & 0 deletions src/gui/qgsnewmemorylayerdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ QgsNewMemoryLayerDialog::QgsNewMemoryLayerDialog( QWidget *parent, Qt::WindowFla

mAddAttributeButton->setEnabled( false );
mRemoveAttributeButton->setEnabled( false );
mButtonUp->setEnabled( false );
mButtonDown->setEnabled( false );

mOkButton = mButtonBox->button( QDialogButtonBox::Ok );
mOkButton->setEnabled( false );
Expand All @@ -111,6 +113,8 @@ QgsNewMemoryLayerDialog::QgsNewMemoryLayerDialog( QWidget *parent, Qt::WindowFla
connect( mAttributeView, &QTreeWidget::itemSelectionChanged, this, &QgsNewMemoryLayerDialog::selectionChanged );
connect( mAddAttributeButton, &QToolButton::clicked, this, &QgsNewMemoryLayerDialog::mAddAttributeButton_clicked );
connect( mRemoveAttributeButton, &QToolButton::clicked, this, &QgsNewMemoryLayerDialog::mRemoveAttributeButton_clicked );
connect( mButtonUp, &QToolButton::clicked, this, &QgsNewMemoryLayerDialog::moveFieldsUp );
connect( mButtonDown, &QToolButton::clicked, this, &QgsNewMemoryLayerDialog::moveFieldsDown );

connect( mButtonBox, &QDialogButtonBox::helpRequested, this, &QgsNewMemoryLayerDialog::showHelp );
connect( mButtonBox, &QDialogButtonBox::accepted, this, &QgsNewMemoryLayerDialog::accept );
Expand Down Expand Up @@ -252,6 +256,8 @@ void QgsNewMemoryLayerDialog::fieldNameChanged( const QString &name )
void QgsNewMemoryLayerDialog::selectionChanged()
{
mRemoveAttributeButton->setDisabled( mAttributeView->selectedItems().isEmpty() );
mButtonUp->setDisabled( mAttributeView->selectedItems().isEmpty() );
mButtonDown->setDisabled( mAttributeView->selectedItems().isEmpty() );
}

QgsFields QgsNewMemoryLayerDialog::fields() const
Expand Down Expand Up @@ -358,3 +364,23 @@ void QgsNewMemoryLayerDialog::showHelp()
{
QgsHelp::openHelp( QStringLiteral( "managing_data_source/create_layers.html#creating-a-new-temporary-scratch-layer" ) );
}

void QgsNewMemoryLayerDialog::moveFieldsUp()
{
int currentRow = mAttributeView->currentIndex().row();
if ( currentRow == 0 )
return;

mAttributeView->insertTopLevelItem( currentRow - 1, mAttributeView->takeTopLevelItem( currentRow ) );
mAttributeView->setCurrentIndex( mAttributeView->model()->index( currentRow - 1, 0 ) );
}

void QgsNewMemoryLayerDialog::moveFieldsDown()
{
int currentRow = mAttributeView->currentIndex().row();
if ( currentRow == mAttributeView->topLevelItemCount() - 1 )
return;

mAttributeView->insertTopLevelItem( currentRow + 1, mAttributeView->takeTopLevelItem( currentRow ) );
mAttributeView->setCurrentIndex( mAttributeView->model()->index( currentRow + 1, 0 ) );
}
2 changes: 2 additions & 0 deletions src/gui/qgsnewmemorylayerdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ class GUI_EXPORT QgsNewMemoryLayerDialog: public QDialog, private Ui::QgsNewMemo
void mRemoveAttributeButton_clicked();
void selectionChanged();
void showHelp();
void moveFieldsUp();
void moveFieldsDown();
};

#endif //QGSNEWMEMORYLAYERDIALOG_H
26 changes: 26 additions & 0 deletions src/gui/qgsnewvectorlayerdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ QgsNewVectorLayerDialog::QgsNewVectorLayerDialog( QWidget *parent, Qt::WindowFla
connect( mFileFormatComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsNewVectorLayerDialog::mFileFormatComboBox_currentIndexChanged );
connect( mTypeBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsNewVectorLayerDialog::mTypeBox_currentIndexChanged );
connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsNewVectorLayerDialog::showHelp );
connect( mButtonUp, &QToolButton::clicked, this, &QgsNewVectorLayerDialog::moveFieldsUp );
connect( mButtonDown, &QToolButton::clicked, this, &QgsNewVectorLayerDialog::moveFieldsDown );

mAddAttributeButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionNewAttribute.svg" ) ) );
mRemoveAttributeButton->setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mActionDeleteAttribute.svg" ) ) );
Expand Down Expand Up @@ -118,6 +120,8 @@ QgsNewVectorLayerDialog::QgsNewVectorLayerDialog( QWidget *parent, Qt::WindowFla

mAddAttributeButton->setEnabled( false );
mRemoveAttributeButton->setEnabled( false );
mButtonUp->setEnabled( false );
mButtonDown->setEnabled( false );

mFileName->setStorageMode( QgsFileWidget::SaveFile );
mFileName->setFilter( QgsVectorFileWriter::filterForDriver( mFileFormatComboBox->currentData( Qt::UserRole ).toString() ) );
Expand Down Expand Up @@ -254,6 +258,28 @@ void QgsNewVectorLayerDialog::nameChanged( const QString &name )
void QgsNewVectorLayerDialog::selectionChanged()
{
mRemoveAttributeButton->setDisabled( mAttributeView->selectedItems().isEmpty() );
mButtonUp->setDisabled( mAttributeView->selectedItems().isEmpty() );
mButtonDown->setDisabled( mAttributeView->selectedItems().isEmpty() );
}

void QgsNewVectorLayerDialog::moveFieldsUp()
{
int currentRow = mAttributeView->currentIndex().row();
if ( currentRow == 0 )
return;

mAttributeView->insertTopLevelItem( currentRow - 1, mAttributeView->takeTopLevelItem( currentRow ) );
mAttributeView->setCurrentIndex( mAttributeView->model()->index( currentRow - 1, 0 ) );
}

void QgsNewVectorLayerDialog::moveFieldsDown()
{
int currentRow = mAttributeView->currentIndex().row();
if ( currentRow == mAttributeView->topLevelItemCount() - 1 )
return;

mAttributeView->insertTopLevelItem( currentRow + 1, mAttributeView->takeTopLevelItem( currentRow ) );
mAttributeView->setCurrentIndex( mAttributeView->model()->index( currentRow + 1, 0 ) );
}

QString QgsNewVectorLayerDialog::filename() const
Expand Down
2 changes: 2 additions & 0 deletions src/gui/qgsnewvectorlayerdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ class GUI_EXPORT QgsNewVectorLayerDialog: public QDialog, private Ui::QgsNewVect
void showHelp();
void nameChanged( const QString & );
void selectionChanged();
void moveFieldsUp();
void moveFieldsDown();

private:
QPushButton *mOkButton = nullptr;
Expand Down
Loading

0 comments on commit d9d2d5c

Please sign in to comment.