Skip to content

Commit 571bf54

Browse files
DelazJnirvn
authored andcommitted
[needs-docs] Refactor the New Shapefile dialog to mimic the GeoPackage sequence (#5803)
1 parent a12673c commit 571bf54

File tree

4 files changed

+174
-120
lines changed

4 files changed

+174
-120
lines changed

python/gui/qgsnewvectorlayerdialog.sip

+5
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ Returns the file format for storage
4545
Returns the file format for storage
4646
:rtype: str
4747
%End
48+
QString filename() const;
49+
%Docstring
50+
Returns the name for the new layer
51+
:rtype: str
52+
%End
4853

4954
QgsCoordinateReferenceSystem crs() const;
5055
%Docstring

src/gui/qgsnewvectorlayerdialog.cpp

+40-33
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ QgsNewVectorLayerDialog::QgsNewVectorLayerDialog( QWidget *parent, Qt::WindowFla
3737
setupUi( this );
3838
connect( mAddAttributeButton, &QToolButton::clicked, this, &QgsNewVectorLayerDialog::mAddAttributeButton_clicked );
3939
connect( mRemoveAttributeButton, &QToolButton::clicked, this, &QgsNewVectorLayerDialog::mRemoveAttributeButton_clicked );
40+
connect( mFileNameEdit, &QLineEdit::textChanged, this, &QgsNewVectorLayerDialog::checkOk );
41+
connect( mBrowseFileName, &QToolButton::clicked, this, &QgsNewVectorLayerDialog::selectFileName );
4042
connect( mFileFormatComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsNewVectorLayerDialog::mFileFormatComboBox_currentIndexChanged );
4143
connect( mTypeBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsNewVectorLayerDialog::mTypeBox_currentIndexChanged );
4244
connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsNewVectorLayerDialog::showHelp );
@@ -54,7 +56,13 @@ QgsNewVectorLayerDialog::QgsNewVectorLayerDialog( QWidget *parent, Qt::WindowFla
5456
mWidth->setValidator( new QIntValidator( 1, 255, this ) );
5557
mPrecision->setValidator( new QIntValidator( 0, 15, this ) );
5658

57-
mPointRadioButton->setChecked( true );
59+
mGeometryTypeBox->addItem( tr( "Point" ), QgsWkbTypes::Point );
60+
mGeometryTypeBox->addItem( tr( "Line" ), QgsWkbTypes::LineString );
61+
mGeometryTypeBox->addItem( tr( "Polygon" ), QgsWkbTypes::Polygon );
62+
63+
mOkButton = buttonBox->button( QDialogButtonBox::Ok );
64+
mOkButton->setEnabled( false );
65+
5866
mFileFormatComboBox->addItem( tr( "ESRI Shapefile" ), "ESRI Shapefile" );
5967
#if 0
6068
// Disabled until provider properly supports editing the created file formats
@@ -76,7 +84,7 @@ QgsNewVectorLayerDialog::QgsNewVectorLayerDialog( QWidget *parent, Qt::WindowFla
7684
// Use default encoding if none supplied
7785
QString enc = QgsSettings().value( QStringLiteral( "/UI/encoding" ), "System" ).toString();
7886

79-
// The specified decoding is added if not existing alread, and then set current.
87+
// The specified decoding is added if not existing already, and then set current.
8088
// This should select it.
8189
int encindex = mFileEncoding->findText( enc );
8290
if ( encindex < 0 )
@@ -86,8 +94,6 @@ QgsNewVectorLayerDialog::QgsNewVectorLayerDialog( QWidget *parent, Qt::WindowFla
8694
}
8795
mFileEncoding->setCurrentIndex( encindex );
8896

89-
mOkButton = buttonBox->button( QDialogButtonBox::Ok );
90-
9197
mAttributeView->addTopLevelItem( new QTreeWidgetItem( QStringList() << QStringLiteral( "id" ) << QStringLiteral( "Integer" ) << QStringLiteral( "10" ) << QLatin1String( "" ) ) );
9298
connect( mNameEdit, &QLineEdit::textChanged, this, &QgsNewVectorLayerDialog::nameChanged );
9399
connect( mAttributeView, &QTreeWidget::itemSelectionChanged, this, &QgsNewVectorLayerDialog::selectionChanged );
@@ -146,18 +152,8 @@ void QgsNewVectorLayerDialog::mTypeBox_currentIndexChanged( int index )
146152
QgsWkbTypes::Type QgsNewVectorLayerDialog::selectedType() const
147153
{
148154
QgsWkbTypes::Type wkbType = QgsWkbTypes::Unknown;
149-
if ( mPointRadioButton->isChecked() )
150-
{
151-
wkbType = QgsWkbTypes::Point;
152-
}
153-
else if ( mLineRadioButton->isChecked() )
154-
{
155-
wkbType = QgsWkbTypes::LineString;
156-
}
157-
else if ( mPolygonRadioButton->isChecked() )
158-
{
159-
wkbType = QgsWkbTypes::Polygon;
160-
}
155+
wkbType = static_cast<QgsWkbTypes::Type>
156+
( mGeometryTypeBox->currentData( Qt::UserRole ).toInt() );
161157

162158
if ( mGeometryWithZCheckBox->isChecked() && wkbType != QgsWkbTypes::Unknown )
163159
wkbType = QgsWkbTypes::to25D( wkbType );
@@ -183,20 +179,14 @@ void QgsNewVectorLayerDialog::mAddAttributeButton_clicked()
183179
//use userrole to avoid translated type string
184180
QString myType = mTypeBox->currentData( Qt::UserRole ).toString();
185181
mAttributeView->addTopLevelItem( new QTreeWidgetItem( QStringList() << myName << myType << myWidth << myPrecision ) );
186-
if ( mAttributeView->topLevelItemCount() > 0 )
187-
{
188-
mOkButton->setEnabled( true );
189-
}
182+
checkOk();
190183
mNameEdit->clear();
191184
}
192185

193186
void QgsNewVectorLayerDialog::mRemoveAttributeButton_clicked()
194187
{
195188
delete mAttributeView->currentItem();
196-
if ( mAttributeView->topLevelItemCount() == 0 )
197-
{
198-
mOkButton->setEnabled( false );
199-
}
189+
checkOk();
200190
}
201191

202192
void QgsNewVectorLayerDialog::attributes( QList< QPair<QString, QString> > &at ) const
@@ -234,6 +224,31 @@ void QgsNewVectorLayerDialog::selectionChanged()
234224
mRemoveAttributeButton->setDisabled( mAttributeView->selectedItems().isEmpty() );
235225
}
236226

227+
void QgsNewVectorLayerDialog::selectFileName()
228+
{
229+
QString fileformat = mFileFormatComboBox->currentData( Qt::UserRole ).toString();
230+
QgsSettings settings;
231+
QString lastUsedDir = settings.value( QStringLiteral( "UI/lastVectorFileFilterDir" ), QDir::homePath() ).toString();
232+
QString filterString = QgsVectorFileWriter::filterForDriver( fileformat );
233+
QString fileName = QFileDialog::getSaveFileName( nullptr, tr( "Save Layer as..." ), lastUsedDir, filterString );
234+
if ( fileName.isEmpty() )
235+
return;
236+
237+
if ( fileformat == QLatin1String( "ESRI Shapefile" ) && !fileName.endsWith( QLatin1String( ".shp" ), Qt::CaseInsensitive ) )
238+
fileName += QLatin1String( ".shp" );
239+
mFileNameEdit->setText( fileName );
240+
}
241+
242+
QString QgsNewVectorLayerDialog::filename() const
243+
{
244+
return mFileNameEdit->text();
245+
}
246+
247+
void QgsNewVectorLayerDialog::checkOk()
248+
{
249+
bool ok = ( !mFileNameEdit->text().isEmpty() && mAttributeView->topLevelItemCount() > 0 );
250+
mOkButton->setEnabled( ok );
251+
}
237252

238253
// this is static
239254
QString QgsNewVectorLayerDialog::runAndCreateLayer( QWidget *parent, QString *pEnc, const QgsCoordinateReferenceSystem &crs )
@@ -254,16 +269,8 @@ QString QgsNewVectorLayerDialog::runAndCreateLayer( QWidget *parent, QString *pE
254269
geomDialog.attributes( attributes );
255270

256271
QgsSettings settings;
257-
QString lastUsedDir = settings.value( QStringLiteral( "UI/lastVectorFileFilterDir" ), QDir::homePath() ).toString();
258272
QString filterString = QgsVectorFileWriter::filterForDriver( fileformat );
259-
QString fileName = QFileDialog::getSaveFileName( nullptr, tr( "Save layer as..." ), lastUsedDir, filterString );
260-
if ( fileName.isNull() )
261-
{
262-
return QLatin1String( "" );
263-
}
264-
265-
if ( fileformat == QLatin1String( "ESRI Shapefile" ) && !fileName.endsWith( QLatin1String( ".shp" ), Qt::CaseInsensitive ) )
266-
fileName += QLatin1String( ".shp" );
273+
QString fileName = geomDialog.filename();
267274

268275
settings.setValue( QStringLiteral( "UI/lastVectorFileFilterDir" ), QFileInfo( fileName ).absolutePath() );
269276
settings.setValue( QStringLiteral( "UI/encoding" ), enc );

src/gui/qgsnewvectorlayerdialog.h

+4
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ class GUI_EXPORT QgsNewVectorLayerDialog: public QDialog, private Ui::QgsNewVect
5050
QString selectedFileFormat() const;
5151
//! Returns the file format for storage
5252
QString selectedFileEncoding() const;
53+
//! Returns the name for the new layer
54+
QString filename() const;
5355

5456
/**
5557
* Returns the selected CRS for the new layer.
@@ -65,10 +67,12 @@ class GUI_EXPORT QgsNewVectorLayerDialog: public QDialog, private Ui::QgsNewVect
6567
void setCrs( const QgsCoordinateReferenceSystem &crs );
6668

6769
private slots:
70+
void selectFileName();
6871
void mAddAttributeButton_clicked();
6972
void mRemoveAttributeButton_clicked();
7073
void mFileFormatComboBox_currentIndexChanged( int index );
7174
void mTypeBox_currentIndexChanged( int index );
75+
void checkOk();
7276

7377
//! Open the associated help
7478
void showHelp();

0 commit comments

Comments
 (0)