Skip to content
Permalink
Browse files

[ui] use qgsfilewidget in the create geopackage layer dialog

  • Loading branch information
nirvn committed Dec 7, 2017
1 parent c2acb5f commit 38c96fe3732e7df12c5818129caf25823b7903c4
@@ -51,8 +51,6 @@ QgsNewGeoPackageLayerDialog::QgsNewGeoPackageLayerDialog( QWidget *parent, Qt::W
connect( mRemoveAttributeButton, &QToolButton::clicked, this, &QgsNewGeoPackageLayerDialog::mRemoveAttributeButton_clicked );
connect( mFieldTypeBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsNewGeoPackageLayerDialog::mFieldTypeBox_currentIndexChanged );
connect( mGeometryTypeBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsNewGeoPackageLayerDialog::mGeometryTypeBox_currentIndexChanged );
connect( mSelectDatabaseButton, &QToolButton::clicked, this, &QgsNewGeoPackageLayerDialog::mSelectDatabaseButton_clicked );
connect( mDatabaseEdit, &QLineEdit::textChanged, this, &QgsNewGeoPackageLayerDialog::mDatabaseEdit_textChanged );
connect( mTableNameEdit, &QLineEdit::textChanged, this, &QgsNewGeoPackageLayerDialog::mTableNameEdit_textChanged );
connect( mTableNameEdit, &QLineEdit::textEdited, this, &QgsNewGeoPackageLayerDialog::mTableNameEdit_textEdited );
connect( mLayerIdentifierEdit, &QLineEdit::textEdited, this, &QgsNewGeoPackageLayerDialog::mLayerIdentifierEdit_textEdited );
@@ -102,12 +100,29 @@ QgsNewGeoPackageLayerDialog::QgsNewGeoPackageLayerDialog( QWidget *parent, Qt::W
connect( mFieldNameEdit, &QLineEdit::textChanged, this, &QgsNewGeoPackageLayerDialog::fieldNameChanged );
connect( mAttributeView, &QTreeWidget::itemSelectionChanged, this, &QgsNewGeoPackageLayerDialog::selectionChanged );
connect( mTableNameEdit, &QLineEdit::textChanged, this, &QgsNewGeoPackageLayerDialog::checkOk );
connect( mDatabaseEdit, &QLineEdit::textChanged, this, &QgsNewGeoPackageLayerDialog::checkOk );

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

mCheckBoxCreateSpatialIndex->setChecked( true );

mDatabase->setStorageMode( QgsFileWidget::SaveFile );
mDatabase->setFilter( tr( "GeoPackage" ) + " (*.gpkg)" );
mDatabase->setDialogTitle( tr( "Select Existing or Create a New GeoPackage Database File..." ) );
mDatabase->setDefaultRoot( settings.value( QStringLiteral( "UI/lastVectorFileFilterDir" ), QDir::homePath() ).toString() );
mDatabase->setConfirmOverwrite( false );
connect( mDatabase, &QgsFileWidget::fileChanged, this, [ = ]( const QString & filePath )
{
QgsSettings settings;
QFileInfo tmplFileInfo( filePath );
settings.setValue( QStringLiteral( "UI/lastVectorFileFilterDir" ), tmplFileInfo.absolutePath() );
if ( !filePath.isEmpty() && !mTableNameEdited )
{
QFileInfo fileInfo( filePath );
mTableNameEdit->setText( fileInfo.baseName() );
}
checkOk();
} );
}

QgsNewGeoPackageLayerDialog::~QgsNewGeoPackageLayerDialog()
@@ -123,10 +138,7 @@ void QgsNewGeoPackageLayerDialog::setCrs( const QgsCoordinateReferenceSystem &cr

void QgsNewGeoPackageLayerDialog::lockDatabasePath()
{
mDatabaseEdit->setReadOnly( true );
mSelectDatabaseButton->hide();
mSelectDatabaseButton->deleteLater();
mSelectDatabaseButton = nullptr;
mDatabase->setReadOnly( true );
}

void QgsNewGeoPackageLayerDialog::mFieldTypeBox_currentIndexChanged( int )
@@ -150,34 +162,6 @@ void QgsNewGeoPackageLayerDialog::mGeometryTypeBox_currentIndexChanged( int )
mCrsSelector->setEnabled( isSpatial );
}

void QgsNewGeoPackageLayerDialog::mSelectDatabaseButton_clicked()
{
QString fileName = QFileDialog::getSaveFileName( this, tr( "Select existing or create new GeoPackage Database File" ),
QDir::homePath(),
tr( "GeoPackage" ) + " (*.gpkg)",
nullptr,
QFileDialog::DontConfirmOverwrite );

if ( fileName.isEmpty() )
return;

if ( !fileName.endsWith( QLatin1String( ".gpkg" ), Qt::CaseInsensitive ) )
{
fileName += QLatin1String( ".gpkg" );
}

mDatabaseEdit->setText( fileName );
}

void QgsNewGeoPackageLayerDialog::mDatabaseEdit_textChanged( const QString &text )
{
if ( !text.isEmpty() && !mTableNameEdited )
{
QFileInfo fileInfo( text );
mTableNameEdit->setText( fileInfo.baseName() );
}
}

void QgsNewGeoPackageLayerDialog::mTableNameEdit_textChanged( const QString &text )
{
mTableNameEdited = !text.isEmpty();
@@ -201,7 +185,7 @@ void QgsNewGeoPackageLayerDialog::mLayerIdentifierEdit_textEdited( const QString

void QgsNewGeoPackageLayerDialog::checkOk()
{
bool ok = !mDatabaseEdit->text().isEmpty() &&
bool ok = !mDatabase->filePath().isEmpty() &&
!mTableNameEdit->text().isEmpty();
mOkButton->setEnabled( ok );
}
@@ -258,7 +242,10 @@ void QgsNewGeoPackageLayerDialog::buttonBox_rejected()

bool QgsNewGeoPackageLayerDialog::apply()
{
QString fileName( mDatabaseEdit->text() );
QString fileName( mDatabase->filePath() );
if ( !fileName.endsWith( QLatin1String( ".gpkg" ), Qt::CaseInsensitive ) )
fileName += QLatin1String( ".gpkg" );

bool createNewDb = false;

if ( QFile( fileName ).exists( fileName ) )
@@ -490,7 +477,7 @@ bool QgsNewGeoPackageLayerDialog::apply()
}
hDS.reset();

QString uri( QStringLiteral( "%1|layername=%2" ).arg( mDatabaseEdit->text(), tableName ) );
QString uri( QStringLiteral( "%1|layername=%2" ).arg( fileName, tableName ) );
QString userVisiblelayerName( layerIdentifier.isEmpty() ? tableName : layerIdentifier );
QgsVectorLayer *layer = new QgsVectorLayer( uri, userVisiblelayerName, QStringLiteral( "ogr" ) );
if ( layer->isValid() )
@@ -54,13 +54,13 @@ class GUI_EXPORT QgsNewGeoPackageLayerDialog: public QDialog, private Ui::QgsNew
* Returns the database path
* \since QGIS 3.0
*/
QString databasePath() const { return mDatabaseEdit->text(); }
QString databasePath() const { return mDatabase->filePath(); }

/**
* Sets the initial database \a path
* \since QGIS 3.0
*/
void setDatabasePath( const QString &path ) { mDatabaseEdit->setText( path ); }
void setDatabasePath( const QString &path ) { mDatabase->setFilePath( path ); }

/**
* Sets the database path widgets to a locked and read-only mode.
@@ -82,8 +82,6 @@ class GUI_EXPORT QgsNewGeoPackageLayerDialog: public QDialog, private Ui::QgsNew
void mRemoveAttributeButton_clicked();
void mFieldTypeBox_currentIndexChanged( int index );
void mGeometryTypeBox_currentIndexChanged( int index );
void mSelectDatabaseButton_clicked();
void mDatabaseEdit_textChanged( const QString &text );
void mTableNameEdit_textChanged( const QString &text );
void mTableNameEdit_textEdited( const QString &text );
void mLayerIdentifierEdit_textEdited( const QString &text );
@@ -149,7 +149,7 @@
<item row="1" column="2">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLineEdit" name="mDatabaseEdit">
<widget class="QgsFileWidget" name="mDatabase">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
@@ -161,16 +161,6 @@
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="mSelectDatabaseButton">
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Select an existing or create a new database&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string>…</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="3" column="0">
@@ -478,11 +468,16 @@
<header>qgsscrollarea.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>QgsFileWidget</class>
<extends>QWidget</extends>
<header>qgsfilewidget.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>scrollArea</tabstop>
<tabstop>mDatabaseEdit</tabstop>
<tabstop>mSelectDatabaseButton</tabstop>
<tabstop>mDatabase</tabstop>
<tabstop>mTableNameEdit</tabstop>
<tabstop>mLayerIdentifierEdit</tabstop>
<tabstop>mLayerDescriptionEdit</tabstop>
@@ -63,7 +63,7 @@ def test(self):
dialog = QgsNewGeoPackageLayerDialog()
dialog.setProperty("hideDialogs", True)

mDatabaseEdit = dialog.findChild(QLineEdit, "mDatabaseEdit")
mDatabase = dialog.findChild(QLineEdit, "mDatabase")
buttonBox = dialog.findChild(QDialogButtonBox, "buttonBox")
ok_button = buttonBox.button(QDialogButtonBox.Ok)
mTableNameEdit = dialog.findChild(QLineEdit, "mTableNameEdit")
@@ -86,7 +86,7 @@ def test(self):
self.assertFalse(ok_button.isEnabled())

dbname = os.path.join(self.basetestpath, 'test.gpkg')
mDatabaseEdit.setText(dbname)
mDatabase.setFilePath(dbname)
self.assertEqual(mTableNameEdit.text(), 'test')
self.assertEqual(mLayerIdentifierEdit.text(), 'test')
self.assertTrue(ok_button.isEnabled())
@@ -261,7 +261,7 @@ def test(self):
QgsProject.instance().removeAllMapLayers()

# Try invalid path
mDatabaseEdit.setText('/this/is/invalid/test.gpkg')
mDatabase.setFilePath('/this/is/invalid/test.gpkg')
self.accepted = False
QTest.mouseClick(ok_button, Qt.LeftButton)
self.assertFalse(self.accepted)

0 comments on commit 38c96fe

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