Skip to content
Permalink
Browse files

use native file selection in new shapefile dialog (implements #9637)

  • Loading branch information
jef-n committed Feb 24, 2014
1 parent 5db13ab commit 5fd8567e598fb3c96a1346081b9abfcceb71026c
Showing with 81 additions and 60 deletions.
  1. +33 −33 src/gui/qgsnewvectorlayerdialog.cpp
  2. +2 −0 src/gui/qgsnewvectorlayerdialog.h
  3. +46 −27 src/ui/qgsnewvectorlayerdialogbase.ui
@@ -21,12 +21,15 @@
#include "qgslogger.h"
#include "qgscoordinatereferencesystem.h"
#include "qgsgenericprojectionselector.h"
#include <QPushButton>
#include "qgsproviderregistry.h"
#include "qgsvectordataprovider.h"
#include "qgsvectorfilewriter.h"

#include <QPushButton>
#include <QComboBox>
#include <QLibrary>
#include <QSettings>
#include "qgsencodingfiledialog.h"
#include "qgsproviderregistry.h"
#include <QFileDialog>


QgsNewVectorLayerDialog::QgsNewVectorLayerDialog( QWidget *parent, Qt::WFlags fl )
@@ -61,6 +64,21 @@ QgsNewVectorLayerDialog::QgsNewVectorLayerDialog( QWidget *parent, Qt::WFlags fl
mFileFormatLabel->setVisible( false );
}

mFileEncoding->addItems( QgsVectorDataProvider::availableEncodings() );

// Use default encoding if none supplied
QString enc = QSettings().value( "/UI/encoding", "System" ).toString();

// The specified decoding is added if not existing alread, and then set current.
// This should select it.
int encindex = mFileEncoding->findText( enc );
if ( encindex < 0 )
{
mFileEncoding->insertItem( 0, enc );
encindex = 0;
}
mFileEncoding->setCurrentIndex( encindex );

mOkButton = buttonBox->button( QDialogButtonBox::Ok );

mAttributeView->addTopLevelItem( new QTreeWidgetItem( QStringList() << "id" << "Integer" << "10" << "" ) );
@@ -203,6 +221,11 @@ QString QgsNewVectorLayerDialog::selectedFileFormat() const
return myType;
}

QString QgsNewVectorLayerDialog::selectedFileEncoding() const
{
return mFileEncoding->currentText();
}

void QgsNewVectorLayerDialog::nameChanged( QString name )
{
mAddAttributeButton->setDisabled( name.isEmpty() || mAttributeView->findItems( name, Qt::MatchExactly ).size() > 0 );
@@ -225,50 +248,27 @@ QString QgsNewVectorLayerDialog::runAndCreateLayer( QWidget* parent, QString* pE

QGis::WkbType geometrytype = geomDialog.selectedType();
QString fileformat = geomDialog.selectedFileFormat();
QString enc = geomDialog.selectedFileEncoding();
int crsId = geomDialog.selectedCrsId();
QgsDebugMsg( QString( "New file format will be: %1" ).arg( fileformat ) );

QList< QPair<QString, QString> > attributes;
geomDialog.attributes( attributes );

QString enc;
QString fileName;

QSettings settings;
QString lastUsedDir = settings.value( "/UI/lastVectorFileFilterDir", "." ).toString();

QgsDebugMsg( "Saving vector file dialog without filters: " );

QgsEncodingFileDialog* openFileDialog =
new QgsEncodingFileDialog( parent, tr( "Save As" ), lastUsedDir, "", QString( "" ) );

openFileDialog->setFileMode( QFileDialog::AnyFile );
openFileDialog->setAcceptMode( QFileDialog::AcceptSave );
openFileDialog->setConfirmOverwrite( true );

if ( settings.contains( "/UI/lastVectorFileFilter" ) )
{
QString lastUsedFilter = settings.value( "/UI/lastVectorFileFilter", QVariant( QString::null ) ).toString();
openFileDialog->selectFilter( lastUsedFilter );
}

if ( openFileDialog->exec() == QDialog::Rejected )
QString filterString = QgsVectorFileWriter::filterForDriver( fileformat );
QString fileName = QFileDialog::getSaveFileName( 0, tr( "Save layer as..." ), lastUsedDir, filterString );
if ( fileName.isNull() )
{
delete openFileDialog;
return QString();
return fileName;
}

fileName = openFileDialog->selectedFiles().first();

if ( fileformat == "ESRI Shapefile" && !fileName.endsWith( ".shp", Qt::CaseInsensitive ) )
fileName += ".shp";

enc = openFileDialog->encoding();

settings.setValue( "/UI/lastVectorFileFilter", openFileDialog->selectedFilter() );
settings.setValue( "/UI/lastVectorFileFilterDir", openFileDialog->directory().absolutePath() );

delete openFileDialog;
settings.setValue( "/UI/lastVectorFileFilterDir", QFileInfo( fileName ).absolutePath() );
settings.setValue( "/UI/encoding", enc );

//try to create the new layer with OGRProvider instead of QgsVectorFileWriter
QgsProviderRegistry * pReg = QgsProviderRegistry::instance();
@@ -40,6 +40,8 @@ class GUI_EXPORT QgsNewVectorLayerDialog: public QDialog, private Ui::QgsNewVect
void attributes( QList< QPair<QString, QString> >& at ) const;
/**Returns the file format for storage*/
QString selectedFileFormat() const;
/**Returns the file format for storage*/
QString selectedFileEncoding() const;
/**Returns the selected crs id*/
int selectedCrsId() const;

@@ -17,27 +17,7 @@
<bool>true</bool>
</property>
<layout class="QGridLayout">
<item row="0" column="0">
<widget class="QLabel" name="mFileFormatLabel">
<property name="enabled">
<bool>true</bool>
</property>
<property name="text">
<string>File format</string>
</property>
<property name="buddy">
<cstring>mFileFormatComboBox</cstring>
</property>
</widget>
</item>
<item row="0" column="1" colspan="2">
<widget class="QComboBox" name="mFileFormatComboBox">
<property name="enabled">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="0" colspan="3">
<item row="2" column="0" colspan="3">
<widget class="QGroupBox" name="buttonGroup1">
<property name="title">
<string>Type</string>
@@ -67,7 +47,7 @@
</layout>
</widget>
</item>
<item row="4" column="0" colspan="3">
<item row="9" column="0" colspan="3">
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>New attribute</string>
@@ -154,7 +134,7 @@
</layout>
</widget>
</item>
<item row="6" column="0" colspan="3">
<item row="11" column="0" colspan="3">
<widget class="QGroupBox" name="groupBox_2">
<property name="title">
<string>Attributes list</string>
@@ -238,7 +218,7 @@
</layout>
</widget>
</item>
<item row="8" column="0" colspan="3">
<item row="13" column="0" colspan="3">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
@@ -248,7 +228,7 @@
</property>
</widget>
</item>
<item row="3" column="2">
<item row="8" column="2">
<widget class="QPushButton" name="pbnChangeSpatialRefSys">
<property name="toolTip">
<string>Specify the coordinate reference system of the layer's geometry.</string>
@@ -261,18 +241,57 @@
</property>
</widget>
</item>
<item row="3" column="0" colspan="2">
<item row="8" column="0" colspan="2">
<widget class="QLineEdit" name="leSpatialRefSys">
<property name="readOnly">
<bool>true</bool>
</property>
</widget>
</item>
<item row="5" column="0">
<widget class="QLabel" name="mFileFormatLabel">
<property name="enabled">
<bool>true</bool>
</property>
<property name="text">
<string>File format</string>
</property>
<property name="buddy">
<cstring>mFileFormatComboBox</cstring>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="mFileFormatLabel_2">
<property name="enabled">
<bool>true</bool>
</property>
<property name="text">
<string>File encoding</string>
</property>
<property name="buddy">
<cstring>mFileFormatComboBox</cstring>
</property>
</widget>
</item>
<item row="4" column="1" colspan="2">
<widget class="QComboBox" name="mFileFormatComboBox">
<property name="enabled">
<bool>true</bool>
</property>
</widget>
</item>
<item row="5" column="1" colspan="2">
<widget class="QComboBox" name="mFileEncoding">
<property name="enabled">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</widget>
<layoutdefault spacing="6" margin="11"/>
<tabstops>
<tabstop>mFileFormatComboBox</tabstop>
<tabstop>mPointRadioButton</tabstop>
<tabstop>mLineRadioButton</tabstop>
<tabstop>mPolygonRadioButton</tabstop>

0 comments on commit 5fd8567

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