Skip to content

Commit

Permalink
[ui] use qgsfilewidget in the create shapefile layer dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Dec 7, 2017
1 parent cddca2f commit c2acb5f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 33 deletions.
13 changes: 13 additions & 0 deletions python/gui/qgsfilewidget.sip
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class QgsFileWidget : QWidget
GetFile,
GetDirectory,
GetMultipleFiles,
SaveFile,
};

enum RelativeStorage
Expand Down Expand Up @@ -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
Expand Down
37 changes: 18 additions & 19 deletions src/gui/qgsnewvectorlayerdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "qgsnewvectorlayerdialog.h"
#include "qgsapplication.h"
#include "qgsfilewidget.h"
#include "qgis.h"
#include "qgslogger.h"
#include "qgscoordinatereferencesystem.h"
Expand All @@ -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 );
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 );
}

Expand All @@ -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 );
Expand Down
1 change: 0 additions & 1 deletion src/gui/qgsnewvectorlayerdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
25 changes: 12 additions & 13 deletions src/ui/qgsnewvectorlayerdialogbase.ui
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand All @@ -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>
Expand All @@ -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>
Expand Down

0 comments on commit c2acb5f

Please sign in to comment.