Skip to content

Commit

Permalink
use a QgsFileWidget in open vector layer UI
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Aug 28, 2017
1 parent 3576532 commit 095bcae
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 93 deletions.
7 changes: 4 additions & 3 deletions src/providers/gdal/qgsgdalsourceselect.cpp
Expand Up @@ -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() );
Expand Down
99 changes: 35 additions & 64 deletions src/providers/ogr/qgsogrsourceselect.cpp
Expand Up @@ -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()
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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" );
}
}
Expand All @@ -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" );
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/providers/ogr/qgsogrsourceselect.h
Expand Up @@ -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 );
Expand All @@ -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
4 changes: 2 additions & 2 deletions src/ui/qgsgdalsourceselectbase.ui
Expand Up @@ -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>
Expand Down
45 changes: 22 additions & 23 deletions src/ui/qgsogrsourceselectbase.ui
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Expand All @@ -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>
Expand Down

0 comments on commit 095bcae

Please sign in to comment.