Skip to content
Permalink
Browse files

Merge pull request #5818 from nirvn/filewidget_fest

Use QgsFileWidget in several dialogs
  • Loading branch information
nirvn committed Dec 7, 2017
2 parents bac80aa + de0ac14 commit 7e0c149b0c50dd7683d1ab0839a04675e75bf311
@@ -31,6 +31,7 @@ class QgsFileWidget : QWidget
GetFile,
GetDirectory,
GetMultipleFiles,
SaveFile,
};

enum RelativeStorage
@@ -97,6 +98,19 @@ returns the filters used for QDialog.getOpenFileName
\param filter Only files that match the given filter are shown, it may be an empty string. If you want multiple filters, separate them with ';;',
%End

void setConfirmOverwrite( bool confirmOverwrite );
%Docstring
Sets whether a confirmation to overwrite an existing file will appear.
By default, a confirmation will appear.
\param confirmOverwrite If set to true, an overwrite confirmation will be shown
%End

bool confirmOverwrite() const;
%Docstring
Returns whether a confirmation will be shown when overwriting an existing file
:rtype: bool
%End

bool fileWidgetButtonVisible() const;
%Docstring
determines if the tool button is shown
@@ -73,8 +73,6 @@ void QgsVectorLayerSaveAsDialog::setup()
{
setupUi( this );
connect( mFormatComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsVectorLayerSaveAsDialog::mFormatComboBox_currentIndexChanged );
connect( leFilename, &QLineEdit::textChanged, this, &QgsVectorLayerSaveAsDialog::leFilename_textChanged );
connect( browseFilename, &QPushButton::clicked, this, &QgsVectorLayerSaveAsDialog::browseFilename_clicked );
connect( mCrsSelector, &QgsProjectionSelectionWidget::crsChanged, this, &QgsVectorLayerSaveAsDialog::mCrsSelector_crsChanged );
connect( mSymbologyExportComboBox, static_cast<void ( QComboBox::* )( const QString & )>( &QComboBox::currentIndexChanged ), this, &QgsVectorLayerSaveAsDialog::mSymbologyExportComboBox_currentIndexChanged );
connect( mGeometryTypeComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, &QgsVectorLayerSaveAsDialog::mGeometryTypeComboBox_currentIndexChanged );
@@ -139,6 +137,24 @@ void QgsVectorLayerSaveAsDialog::setup()
mExtentGroupBox->setCheckable( true );
mExtentGroupBox->setChecked( false );
mExtentGroupBox->setCollapsed( true );

mFilename->setStorageMode( QgsFileWidget::SaveFile );
mFilename->setDialogTitle( tr( "Select layer as..." ) );
mFilename->setDefaultRoot( settings.value( QStringLiteral( "UI/lastVectorFileFilterDir" ), QDir::homePath() ).toString() );
mFilename->setConfirmOverwrite( false );
connect( mFilename, &QgsFileWidget::fileChanged, this, [ = ]( const QString & filePath )
{
QgsSettings settings;
QFileInfo tmplFileInfo( filePath );
settings.setValue( QStringLiteral( "UI/lastVectorFileFilterDir" ), tmplFileInfo.absolutePath() );
if ( !filePath.isEmpty() && leLayername->isEnabled() )
{
QFileInfo fileInfo( filePath );
leLayername->setText( fileInfo.baseName() );
}
buttonBox->button( QDialogButtonBox::Ok )->setEnabled(
!filePath.isEmpty() && QFileInfo( filePath ).absoluteDir().exists() );
} );
}

QList<QPair<QLabel *, QWidget *> > QgsVectorLayerSaveAsDialog::createControls( const QMap<QString, QgsVectorFileWriter::Option *> &options )
@@ -352,8 +368,9 @@ void QgsVectorLayerSaveAsDialog::mFormatComboBox_currentIndexChanged( int idx )
{
Q_UNUSED( idx );

browseFilename->setEnabled( true );
leFilename->setEnabled( true );
mFilename->setEnabled( true );
mFilename->setFilter( QgsVectorFileWriter::filterForDriver( format() ) );

bool selectAllFields = true;
bool fieldsAsDisplayedValues = false;

@@ -379,9 +396,9 @@ void QgsVectorLayerSaveAsDialog::mFormatComboBox_currentIndexChanged( int idx )
if ( !leLayername->isEnabled() )
leLayername->setText( QString() );
else if ( leLayername->text().isEmpty() &&
!leFilename->text().isEmpty() )
!mFilename->filePath().isEmpty() )
{
QString layerName = QFileInfo( leFilename->text() ).baseName();
QString layerName = QFileInfo( mFilename->filePath() ).baseName();
leLayername->setText( layerName );
}

@@ -626,30 +643,6 @@ void QgsVectorLayerSaveAsDialog::mAttributeTable_itemChanged( QTableWidgetItem *
mReplaceRawFieldValuesStateChangedSlotEnabled = true;
}

void QgsVectorLayerSaveAsDialog::leFilename_textChanged( const QString &text )
{
buttonBox->button( QDialogButtonBox::Ok )->setEnabled(
!text.isEmpty() && QFileInfo( text ).absoluteDir().exists() );

if ( leLayername->isEnabled() )
{
QString layerName = QFileInfo( text ).baseName();
leLayername->setText( layerName );
}
}

void QgsVectorLayerSaveAsDialog::browseFilename_clicked()
{
QgsSettings settings;
QString dirName = leFilename->text().isEmpty() ? settings.value( QStringLiteral( "UI/lastVectorFileFilterDir" ), QDir::homePath() ).toString() : leFilename->text();
QString filterString = QgsVectorFileWriter::filterForDriver( format() );
QString outputFile = QFileDialog::getSaveFileName( nullptr, tr( "Save layer as..." ), dirName, filterString, nullptr, QFileDialog::DontConfirmOverwrite );
if ( !outputFile.isNull() )
{
leFilename->setText( outputFile );
}
}

void QgsVectorLayerSaveAsDialog::mCrsSelector_crsChanged( const QgsCoordinateReferenceSystem &crs )
{
mCRS = crs.srsid();
@@ -658,7 +651,7 @@ void QgsVectorLayerSaveAsDialog::mCrsSelector_crsChanged( const QgsCoordinateRef

QString QgsVectorLayerSaveAsDialog::filename() const
{
return leFilename->text();
return mFilename->filePath();
}

QString QgsVectorLayerSaveAsDialog::layername() const
@@ -126,8 +126,6 @@ class GUI_EXPORT QgsVectorLayerSaveAsDialog : public QDialog, private Ui::QgsVec
private slots:

void mFormatComboBox_currentIndexChanged( int idx );
void leFilename_textChanged( const QString &text );
void browseFilename_clicked();
void mCrsSelector_crsChanged( const QgsCoordinateReferenceSystem &crs );
void showHelp();
void mSymbologyExportComboBox_currentIndexChanged( const QString &text );
@@ -262,6 +262,17 @@ void QgsFileWidget::openFileDialog()
title = !mDialogTitle.isEmpty() ? mDialogTitle : tr( "Select a directory" );
fileName = QFileDialog::getExistingDirectory( this, title, QFileInfo( oldPath ).absoluteFilePath(), QFileDialog::ShowDirsOnly );
break;
case SaveFile:
title = !mDialogTitle.isEmpty() ? mDialogTitle : tr( "Create or select a file" );
if ( !confirmOverwrite() )
{
fileName = QFileDialog::getSaveFileName( this, title, QFileInfo( oldPath ).absoluteFilePath(), mFilter, nullptr, QFileDialog::DontConfirmOverwrite );
}
else
{
fileName = QFileDialog::getSaveFileName( this, title, QFileInfo( oldPath ).absoluteFilePath(), mFilter );
}
break;
}

if ( fileName.isEmpty() && fileNames.isEmpty( ) )
@@ -283,6 +294,7 @@ void QgsFileWidget::openFileDialog()
switch ( mStorageMode )
{
case GetFile:
case SaveFile:
settings.setValue( QStringLiteral( "UI/lastFileNameWidgetDir" ), QFileInfo( fileName ).absolutePath() );
break;
case GetDirectory:
@@ -65,6 +65,7 @@ class GUI_EXPORT QgsFileWidget : public QWidget
GetFile, //! Select a single file
GetDirectory, //! Select a directory
GetMultipleFiles, //! Select multiple files
SaveFile, //! Select a single new or pre-existing file
};

/**
@@ -120,6 +121,18 @@ class GUI_EXPORT QgsFileWidget : public QWidget
*/
void setFilter( const QString &filter );

/**
* Sets whether a confirmation to overwrite an existing file will appear.
* By default, a confirmation will appear.
* \param confirmOverwrite If set to true, an overwrite confirmation will be shown
*/
void setConfirmOverwrite( bool confirmOverwrite ) { mConfirmOverwrite = confirmOverwrite; }

/**
* Returns whether a confirmation will be shown when overwriting an existing file
*/
bool confirmOverwrite() const { return mConfirmOverwrite; }

//! determines if the tool button is shown
bool fileWidgetButtonVisible() const;
//! determines if the tool button is shown
@@ -173,6 +186,7 @@ class GUI_EXPORT QgsFileWidget : public QWidget
QString mDialogTitle;
QString mFilter;
QString mDefaultRoot;
bool mConfirmOverwrite = true;
StorageMode mStorageMode = GetFile;
RelativeStorage mRelativeStorage = Absolute;

@@ -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 );

0 comments on commit 7e0c149

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