Skip to content
Permalink
Browse files

[ui] use qgsfilewidget in the create shapefile layer dialog

  • Loading branch information
nirvn committed Dec 7, 2017
1 parent cddca2f commit c2acb5feeca1ece4b47dff2cf70bbc87599268b6
@@ -31,6 +31,7 @@ class QgsFileWidget : QWidget
GetFile,
GetDirectory,
GetMultipleFiles,
SaveFile,
};

enum RelativeStorage
@@ -97,6 +98,18 @@ 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
\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
@@ -17,6 +17,7 @@

#include "qgsnewvectorlayerdialog.h"
#include "qgsapplication.h"
#include "qgsfilewidget.h"
#include "qgis.h"
#include "qgslogger.h"
#include "qgscoordinatereferencesystem.h"
@@ -35,10 +36,9 @@ QgsNewVectorLayerDialog::QgsNewVectorLayerDialog( QWidget *parent, Qt::WindowFla
: QDialog( parent, fl )
{
setupUi( this );

connect( mAddAttributeButton, &QToolButton::clicked, this, &QgsNewVectorLayerDialog::mAddAttributeButton_clicked );
connect( mRemoveAttributeButton, &QToolButton::clicked, this, &QgsNewVectorLayerDialog::mRemoveAttributeButton_clicked );
connect( mFileNameEdit, &QLineEdit::textChanged, this, &QgsNewVectorLayerDialog::checkOk );
connect( mBrowseFileName, &QToolButton::clicked, this, &QgsNewVectorLayerDialog::selectFileName );
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 );
@@ -100,6 +100,18 @@ QgsNewVectorLayerDialog::QgsNewVectorLayerDialog( QWidget *parent, Qt::WindowFla

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

mFileName->setStorageMode( QgsFileWidget::SaveFile );
mFileName->setFilter( QgsVectorFileWriter::filterForDriver( mFileFormatComboBox->currentData( Qt::UserRole ).toString() ) );
mFileName->setDialogTitle( tr( "Select Layer as..." ) );
mFileName->setDefaultRoot( settings.value( QStringLiteral( "UI/lastVectorFileFilterDir" ), QDir::homePath() ).toString() );
connect( mFileName, &QgsFileWidget::fileChanged, this, [ = ]
{
QgsSettings settings;
QFileInfo tmplFileInfo( mFileName->filePath() );
settings.setValue( QStringLiteral( "UI/lastVectorFileFilterDir" ), tmplFileInfo.absolutePath() );
checkOk();
} );
}

QgsNewVectorLayerDialog::~QgsNewVectorLayerDialog()
@@ -224,29 +236,14 @@ void QgsNewVectorLayerDialog::selectionChanged()
mRemoveAttributeButton->setDisabled( mAttributeView->selectedItems().isEmpty() );
}

void QgsNewVectorLayerDialog::selectFileName()
{
QString fileformat = mFileFormatComboBox->currentData( Qt::UserRole ).toString();
QgsSettings settings;
QString lastUsedDir = settings.value( QStringLiteral( "UI/lastVectorFileFilterDir" ), QDir::homePath() ).toString();
QString filterString = QgsVectorFileWriter::filterForDriver( fileformat );
QString fileName = QFileDialog::getSaveFileName( nullptr, tr( "Save Layer as..." ), lastUsedDir, filterString );
if ( fileName.isEmpty() )
return;

if ( fileformat == QLatin1String( "ESRI Shapefile" ) && !fileName.endsWith( QLatin1String( ".shp" ), Qt::CaseInsensitive ) )
fileName += QLatin1String( ".shp" );
mFileNameEdit->setText( fileName );
}

QString QgsNewVectorLayerDialog::filename() const
{
return mFileNameEdit->text();
return mFileName->filePath();
}

void QgsNewVectorLayerDialog::checkOk()
{
bool ok = ( !mFileNameEdit->text().isEmpty() && mAttributeView->topLevelItemCount() > 0 );
bool ok = ( !mFileName->filePath().isEmpty() && mAttributeView->topLevelItemCount() > 0 );
mOkButton->setEnabled( ok );
}

@@ -271,6 +268,8 @@ QString QgsNewVectorLayerDialog::runAndCreateLayer( QWidget *parent, QString *pE
QgsSettings settings;
QString filterString = QgsVectorFileWriter::filterForDriver( fileformat );
QString fileName = geomDialog.filename();
if ( fileformat == QLatin1String( "ESRI Shapefile" ) && !fileName.endsWith( QLatin1String( ".shp" ), Qt::CaseInsensitive ) )
fileName += QLatin1String( ".shp" );

settings.setValue( QStringLiteral( "UI/lastVectorFileFilterDir" ), QFileInfo( fileName ).absolutePath() );
settings.setValue( QStringLiteral( "UI/encoding" ), enc );
@@ -67,7 +67,6 @@ class GUI_EXPORT QgsNewVectorLayerDialog: public QDialog, private Ui::QgsNewVect
void setCrs( const QgsCoordinateReferenceSystem &crs );

private slots:
void selectFileName();
void mAddAttributeButton_clicked();
void mRemoveAttributeButton_clicked();
void mFileFormatComboBox_currentIndexChanged( int index );
@@ -279,23 +279,23 @@
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="mFileNameEdit"/>
<widget class="QgsFileWidget" name="mFileName"/>
</item>
<item row="1" column="1" colspan="2">
<item row="1" column="1">
<widget class="QComboBox" name="mFileEncoding">
<property name="enabled">
<bool>true</bool>
</property>
</widget>
</item>
<item row="2" column="1" colspan="2">
<item row="2" column="1">
<widget class="QComboBox" name="mFileFormatComboBox">
<property name="enabled">
<bool>true</bool>
</property>
</widget>
</item>
<item row="3" column="1" colspan="2">
<item row="3" column="1">
<widget class="QComboBox" name="mGeometryTypeBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
@@ -305,20 +305,13 @@
</property>
</widget>
</item>
<item row="4" column="1" colspan="2">
<item row="4" column="1">
<widget class="QCheckBox" name="mGeometryWithZCheckBox">
<property name="text">
<string>Include Z dimension</string>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QToolButton" name="mBrowseFileName">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
@@ -331,9 +324,15 @@
<header location="global">qgsprojectionselectionwidget.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>QgsFileWidget</class>
<extends>QWidget</extends>
<header>qgsfilewidget.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>mFileNameEdit</tabstop>
<tabstop>mFileName</tabstop>
<tabstop>mFileEncoding</tabstop>
<tabstop>mFileFormatComboBox</tabstop>
<tabstop>mGeometryTypeBox</tabstop>

0 comments on commit c2acb5f

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