Skip to content
Permalink
Browse files

use a QgsFileWidget in open vector layer UI

  • Loading branch information
nirvn committed Aug 28, 2017
1 parent 3576532 commit 095bcae01d76976ee0b4e86166a48de9f050a014
@@ -23,9 +23,10 @@ QgsGdalSourceSelect::QgsGdalSourceSelect( QWidget *parent, Qt::WindowFlags fl, Q
{
setupUi( this );
setupButtons( buttonBox );
mQgsFileWidget->setFilter( QgsProviderRegistry::instance()->fileRasterFilters() );
mQgsFileWidget->setStorageMode( QgsFileWidget::GetMultipleFiles );
connect( mQgsFileWidget, &QgsFileWidget::fileChanged, this, [ = ]( const QString & path )
mFileWidget->setDialogTitle( tr( "Open GDAL Supported Raster Dataset(s)" ) );
mFileWidget->setFilter( QgsProviderRegistry::instance()->fileRasterFilters() );
mFileWidget->setStorageMode( QgsFileWidget::GetMultipleFiles );
connect( mFileWidget, &QgsFileWidget::fileChanged, this, [ = ]( const QString & path )
{
mRasterPath = path;
emit enableButtons( ! mRasterPath.isEmpty() );
@@ -97,49 +97,22 @@ QgsOgrSourceSelect::QgsOgrSourceSelect( QWidget *parent, Qt::WindowFlags fl, Qgs
}
cmbDatabaseTypes->blockSignals( false );
cmbConnections->blockSignals( false );
}

QgsOgrSourceSelect::~QgsOgrSourceSelect()
{
QgsSettings settings;
settings.setValue( QStringLiteral( "Windows/OpenVectorLayer/geometry" ), saveGeometry() );
}

QStringList QgsOgrSourceSelect::openFile()
{
QStringList selectedFiles;
QgsDebugMsg( "Vector file filters: " + mVectorFileFilter );
QString enc = encoding();
QString title = tr( "Open an OGR Supported Vector Layer" );
QgsGuiUtils::openFilesRememberingFilter( QStringLiteral( "lastVectorFileFilter" ), mVectorFileFilter, selectedFiles, enc, title );
mFileWidget->setDialogTitle( tr( "Open OGR Supported Vector Dataset(s)" ) );
mFileWidget->setFilter( mVectorFileFilter );
mFileWidget->setStorageMode( QgsFileWidget::GetMultipleFiles );

return selectedFiles;
connect( mFileWidget, &QgsFileWidget::fileChanged, this, [ = ]( const QString & path )
{
mVectorPath = path;
emit enableButtons( ! mVectorPath.isEmpty() );
} );
}

QString QgsOgrSourceSelect::openDirectory()
QgsOgrSourceSelect::~QgsOgrSourceSelect()
{
QgsSettings settings;

bool haveLastUsedDir = settings.contains( QStringLiteral( "/UI/LastUsedDirectory" ) );
QString lastUsedDir = settings.value( QStringLiteral( "UI/LastUsedDirectory" ), QDir::homePath() ).toString();
if ( !haveLastUsedDir )
lastUsedDir = QLatin1String( "" );

QString path = QFileDialog::getExistingDirectory( this,
tr( "Open Directory" ), lastUsedDir,
QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks );

settings.setValue( QStringLiteral( "UI/LastUsedDirectory" ), path );
//process path if it is grass
if ( cmbDirectoryTypes->currentText() == QLatin1String( "Grass Vector" ) )
{
#ifdef Q_OS_WIN
//replace backslashes with forward slashes
path.replace( '\\', '/' );
#endif
path = path + "/head";
}
return path;
settings.setValue( QStringLiteral( "Windows/OpenVectorLayer/geometry" ), saveGeometry() );
}

QStringList QgsOgrSourceSelect::dataSources()
@@ -272,29 +245,6 @@ void QgsOgrSourceSelect::setSelectedConnection()
QgsDebugMsg( "Setting selected connection to " + cmbConnections->currentText() );
}


void QgsOgrSourceSelect::on_buttonSelectSrc_clicked()
{
if ( radioSrcFile->isChecked() )
{
QStringList selected = openFile();
if ( !selected.isEmpty() )
{
inputSrcDataset->setText( selected.join( QStringLiteral( ";" ) ) );
addButton()->setFocus();
emit enableButtons( true );
}
}
else if ( radioSrcDirectory->isChecked() )
{
inputSrcDataset->setText( openDirectory() );
}
else if ( !radioSrcDatabase->isChecked() )
{
Q_ASSERT( !"SHOULD NEVER GET HERE" );
}
}

void QgsOgrSourceSelect::addButtonClicked()
{
QgsSettings settings;
@@ -359,27 +309,37 @@ void QgsOgrSourceSelect::addButtonClicked()
}
else if ( radioSrcFile->isChecked() )
{
if ( inputSrcDataset->text().isEmpty() )
if ( mVectorPath.isEmpty() )
{
QMessageBox::information( this,
tr( "Add vector layer" ),
tr( "No layers selected." ) );
return;
}

mDataSources << inputSrcDataset->text().split( ';' );
mDataSources << QgsFileWidget::splitFilePaths( mVectorPath );
}
else if ( radioSrcDirectory->isChecked() )
{
if ( inputSrcDataset->text().isEmpty() )
if ( mVectorPath.isEmpty() )
{
QMessageBox::information( this,
tr( "Add vector layer" ),
tr( "No directory selected." ) );
return;
}

mDataSources << inputSrcDataset->text();
//process path if it is grass
if ( cmbDirectoryTypes->currentText() == QLatin1String( "Grass Vector" ) )
{
#ifdef Q_OS_WIN
//replace backslashes with forward slashes
mVectorPath.replace( '\\', '/' );
#endif
mVectorPath = mVectorPath + "/head";
}

mDataSources << mVectorPath;
}

// Save the used encoding
@@ -402,6 +362,12 @@ void QgsOgrSourceSelect::on_radioSrcFile_toggled( bool checked )
fileGroupBox->show();
dbGroupBox->hide();
protocolGroupBox->hide();

mFileWidget->setDialogTitle( tr( "Open an OGR Supported Vector Layer" ) );
mFileWidget->setFilter( mVectorFileFilter );
mFileWidget->setStorageMode( QgsFileWidget::GetMultipleFiles );
mFileWidget->setFilePath( QString() );

mDataSourceType = QStringLiteral( "file" );
}
}
@@ -415,6 +381,11 @@ void QgsOgrSourceSelect::on_radioSrcDirectory_toggled( bool checked )
fileGroupBox->show();
dbGroupBox->hide();
protocolGroupBox->hide();

mFileWidget->setDialogTitle( tr( "Open Directory" ) );
mFileWidget->setStorageMode( QgsFileWidget::GetDirectory );
mFileWidget->setFilePath( QString() );

mDataSourceType = QStringLiteral( "directory" );
}
}
@@ -91,7 +91,6 @@ class QgsOgrSourceSelect : public QgsAbstractDataSourceWidget, private Ui::QgsOg
//! Sets the selected connection
void setSelectedConnection();

void on_buttonSelectSrc_clicked();
void on_radioSrcFile_toggled( bool checked );
void on_radioSrcDirectory_toggled( bool checked );
void on_radioSrcDatabase_toggled( bool checked );
@@ -102,6 +101,11 @@ class QgsOgrSourceSelect : public QgsAbstractDataSourceWidget, private Ui::QgsOg
void on_cmbDatabaseTypes_currentIndexChanged( const QString &text );
void on_cmbConnections_currentIndexChanged( const QString &text );
void showHelp();

private:

QString mVectorPath;

};

#endif // QGSOGRSOURCESELECT_H
@@ -39,12 +39,12 @@
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Dataset</string>
<string>Raster Dataset(s)</string>
</property>
</widget>
</item>
<item>
<widget class="QgsFileWidget" name="mQgsFileWidget"/>
<widget class="QgsFileWidget" name="mFileWidget"/>
</item>
</layout>
</widget>
@@ -103,35 +103,28 @@
</property>
</widget>
</item>
<item row="0" column="2" colspan="2">
<widget class="QComboBox" name="cmbDirectoryTypes"/>
<item row="0" column="1">
<widget class="QComboBox" name="cmbDirectoryTypes">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item row="1" column="0" colspan="2">
<item row="1" column="0">
<widget class="QLabel" name="labelSrcDataset">
<property name="text">
<string>Dataset</string>
<string>Vector Dataset(s)</string>
</property>
<property name="buddy">
<cstring>inputSrcDataset</cstring>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QLineEdit" name="inputSrcDataset">
<property name="minimumSize">
<size>
<width>200</width>
<height>0</height>
</size>
<cstring>mFileWidget</cstring>
</property>
</widget>
</item>
<item row="1" column="3">
<widget class="QToolButton" name="buttonSelectSrc">
<property name="text">
<string>…</string>
</property>
</widget>
<item row="1" column="1">
<widget class="QgsFileWidget" name="mFileWidget"/>
</item>
</layout>
</widget>
@@ -297,6 +290,13 @@
</layout>
</widget>
<layoutdefault spacing="6" margin="11"/>
<customwidgets>
<customwidget>
<class>QgsFileWidget</class>
<extends>QWidget</extends>
<header>qgsfilewidget.h</header>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>radioSrcFile</tabstop>
<tabstop>radioSrcDirectory</tabstop>
@@ -306,8 +306,7 @@
<tabstop>cmbProtocolTypes</tabstop>
<tabstop>protocolURI</tabstop>
<tabstop>cmbDirectoryTypes</tabstop>
<tabstop>inputSrcDataset</tabstop>
<tabstop>buttonSelectSrc</tabstop>
<tabstop>mFileWidget</tabstop>
<tabstop>cmbDatabaseTypes</tabstop>
<tabstop>cmbConnections</tabstop>
<tabstop>btnNew</tabstop>

0 comments on commit 095bcae

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