Skip to content

Commit 095bcae

Browse files
committed
use a QgsFileWidget in open vector layer UI
1 parent 3576532 commit 095bcae

File tree

5 files changed

+68
-93
lines changed

5 files changed

+68
-93
lines changed

src/providers/gdal/qgsgdalsourceselect.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ QgsGdalSourceSelect::QgsGdalSourceSelect( QWidget *parent, Qt::WindowFlags fl, Q
2323
{
2424
setupUi( this );
2525
setupButtons( buttonBox );
26-
mQgsFileWidget->setFilter( QgsProviderRegistry::instance()->fileRasterFilters() );
27-
mQgsFileWidget->setStorageMode( QgsFileWidget::GetMultipleFiles );
28-
connect( mQgsFileWidget, &QgsFileWidget::fileChanged, this, [ = ]( const QString & path )
26+
mFileWidget->setDialogTitle( tr( "Open GDAL Supported Raster Dataset(s)" ) );
27+
mFileWidget->setFilter( QgsProviderRegistry::instance()->fileRasterFilters() );
28+
mFileWidget->setStorageMode( QgsFileWidget::GetMultipleFiles );
29+
connect( mFileWidget, &QgsFileWidget::fileChanged, this, [ = ]( const QString & path )
2930
{
3031
mRasterPath = path;
3132
emit enableButtons( ! mRasterPath.isEmpty() );

src/providers/ogr/qgsogrsourceselect.cpp

Lines changed: 35 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -97,49 +97,22 @@ QgsOgrSourceSelect::QgsOgrSourceSelect( QWidget *parent, Qt::WindowFlags fl, Qgs
9797
}
9898
cmbDatabaseTypes->blockSignals( false );
9999
cmbConnections->blockSignals( false );
100-
}
101-
102-
QgsOgrSourceSelect::~QgsOgrSourceSelect()
103-
{
104-
QgsSettings settings;
105-
settings.setValue( QStringLiteral( "Windows/OpenVectorLayer/geometry" ), saveGeometry() );
106-
}
107100

108-
QStringList QgsOgrSourceSelect::openFile()
109-
{
110-
QStringList selectedFiles;
111-
QgsDebugMsg( "Vector file filters: " + mVectorFileFilter );
112-
QString enc = encoding();
113-
QString title = tr( "Open an OGR Supported Vector Layer" );
114-
QgsGuiUtils::openFilesRememberingFilter( QStringLiteral( "lastVectorFileFilter" ), mVectorFileFilter, selectedFiles, enc, title );
101+
mFileWidget->setDialogTitle( tr( "Open OGR Supported Vector Dataset(s)" ) );
102+
mFileWidget->setFilter( mVectorFileFilter );
103+
mFileWidget->setStorageMode( QgsFileWidget::GetMultipleFiles );
115104

116-
return selectedFiles;
105+
connect( mFileWidget, &QgsFileWidget::fileChanged, this, [ = ]( const QString & path )
106+
{
107+
mVectorPath = path;
108+
emit enableButtons( ! mVectorPath.isEmpty() );
109+
} );
117110
}
118111

119-
QString QgsOgrSourceSelect::openDirectory()
112+
QgsOgrSourceSelect::~QgsOgrSourceSelect()
120113
{
121114
QgsSettings settings;
122-
123-
bool haveLastUsedDir = settings.contains( QStringLiteral( "/UI/LastUsedDirectory" ) );
124-
QString lastUsedDir = settings.value( QStringLiteral( "UI/LastUsedDirectory" ), QDir::homePath() ).toString();
125-
if ( !haveLastUsedDir )
126-
lastUsedDir = QLatin1String( "" );
127-
128-
QString path = QFileDialog::getExistingDirectory( this,
129-
tr( "Open Directory" ), lastUsedDir,
130-
QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks );
131-
132-
settings.setValue( QStringLiteral( "UI/LastUsedDirectory" ), path );
133-
//process path if it is grass
134-
if ( cmbDirectoryTypes->currentText() == QLatin1String( "Grass Vector" ) )
135-
{
136-
#ifdef Q_OS_WIN
137-
//replace backslashes with forward slashes
138-
path.replace( '\\', '/' );
139-
#endif
140-
path = path + "/head";
141-
}
142-
return path;
115+
settings.setValue( QStringLiteral( "Windows/OpenVectorLayer/geometry" ), saveGeometry() );
143116
}
144117

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

275-
276-
void QgsOgrSourceSelect::on_buttonSelectSrc_clicked()
277-
{
278-
if ( radioSrcFile->isChecked() )
279-
{
280-
QStringList selected = openFile();
281-
if ( !selected.isEmpty() )
282-
{
283-
inputSrcDataset->setText( selected.join( QStringLiteral( ";" ) ) );
284-
addButton()->setFocus();
285-
emit enableButtons( true );
286-
}
287-
}
288-
else if ( radioSrcDirectory->isChecked() )
289-
{
290-
inputSrcDataset->setText( openDirectory() );
291-
}
292-
else if ( !radioSrcDatabase->isChecked() )
293-
{
294-
Q_ASSERT( !"SHOULD NEVER GET HERE" );
295-
}
296-
}
297-
298248
void QgsOgrSourceSelect::addButtonClicked()
299249
{
300250
QgsSettings settings;
@@ -359,27 +309,37 @@ void QgsOgrSourceSelect::addButtonClicked()
359309
}
360310
else if ( radioSrcFile->isChecked() )
361311
{
362-
if ( inputSrcDataset->text().isEmpty() )
312+
if ( mVectorPath.isEmpty() )
363313
{
364314
QMessageBox::information( this,
365315
tr( "Add vector layer" ),
366316
tr( "No layers selected." ) );
367317
return;
368318
}
369319

370-
mDataSources << inputSrcDataset->text().split( ';' );
320+
mDataSources << QgsFileWidget::splitFilePaths( mVectorPath );
371321
}
372322
else if ( radioSrcDirectory->isChecked() )
373323
{
374-
if ( inputSrcDataset->text().isEmpty() )
324+
if ( mVectorPath.isEmpty() )
375325
{
376326
QMessageBox::information( this,
377327
tr( "Add vector layer" ),
378328
tr( "No directory selected." ) );
379329
return;
380330
}
381331

382-
mDataSources << inputSrcDataset->text();
332+
//process path if it is grass
333+
if ( cmbDirectoryTypes->currentText() == QLatin1String( "Grass Vector" ) )
334+
{
335+
#ifdef Q_OS_WIN
336+
//replace backslashes with forward slashes
337+
mVectorPath.replace( '\\', '/' );
338+
#endif
339+
mVectorPath = mVectorPath + "/head";
340+
}
341+
342+
mDataSources << mVectorPath;
383343
}
384344

385345
// Save the used encoding
@@ -402,6 +362,12 @@ void QgsOgrSourceSelect::on_radioSrcFile_toggled( bool checked )
402362
fileGroupBox->show();
403363
dbGroupBox->hide();
404364
protocolGroupBox->hide();
365+
366+
mFileWidget->setDialogTitle( tr( "Open an OGR Supported Vector Layer" ) );
367+
mFileWidget->setFilter( mVectorFileFilter );
368+
mFileWidget->setStorageMode( QgsFileWidget::GetMultipleFiles );
369+
mFileWidget->setFilePath( QString() );
370+
405371
mDataSourceType = QStringLiteral( "file" );
406372
}
407373
}
@@ -415,6 +381,11 @@ void QgsOgrSourceSelect::on_radioSrcDirectory_toggled( bool checked )
415381
fileGroupBox->show();
416382
dbGroupBox->hide();
417383
protocolGroupBox->hide();
384+
385+
mFileWidget->setDialogTitle( tr( "Open Directory" ) );
386+
mFileWidget->setStorageMode( QgsFileWidget::GetDirectory );
387+
mFileWidget->setFilePath( QString() );
388+
418389
mDataSourceType = QStringLiteral( "directory" );
419390
}
420391
}

src/providers/ogr/qgsogrsourceselect.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ class QgsOgrSourceSelect : public QgsAbstractDataSourceWidget, private Ui::QgsOg
9191
//! Sets the selected connection
9292
void setSelectedConnection();
9393

94-
void on_buttonSelectSrc_clicked();
9594
void on_radioSrcFile_toggled( bool checked );
9695
void on_radioSrcDirectory_toggled( bool checked );
9796
void on_radioSrcDatabase_toggled( bool checked );
@@ -102,6 +101,11 @@ class QgsOgrSourceSelect : public QgsAbstractDataSourceWidget, private Ui::QgsOg
102101
void on_cmbDatabaseTypes_currentIndexChanged( const QString &text );
103102
void on_cmbConnections_currentIndexChanged( const QString &text );
104103
void showHelp();
104+
105+
private:
106+
107+
QString mVectorPath;
108+
105109
};
106110

107111
#endif // QGSOGRSOURCESELECT_H

src/ui/qgsgdalsourceselectbase.ui

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@
3939
<item>
4040
<widget class="QLabel" name="label">
4141
<property name="text">
42-
<string>Dataset</string>
42+
<string>Raster Dataset(s)</string>
4343
</property>
4444
</widget>
4545
</item>
4646
<item>
47-
<widget class="QgsFileWidget" name="mQgsFileWidget"/>
47+
<widget class="QgsFileWidget" name="mFileWidget"/>
4848
</item>
4949
</layout>
5050
</widget>

src/ui/qgsogrsourceselectbase.ui

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -103,35 +103,28 @@
103103
</property>
104104
</widget>
105105
</item>
106-
<item row="0" column="2" colspan="2">
107-
<widget class="QComboBox" name="cmbDirectoryTypes"/>
106+
<item row="0" column="1">
107+
<widget class="QComboBox" name="cmbDirectoryTypes">
108+
<property name="sizePolicy">
109+
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
110+
<horstretch>0</horstretch>
111+
<verstretch>0</verstretch>
112+
</sizepolicy>
113+
</property>
114+
</widget>
108115
</item>
109-
<item row="1" column="0" colspan="2">
116+
<item row="1" column="0">
110117
<widget class="QLabel" name="labelSrcDataset">
111118
<property name="text">
112-
<string>Dataset</string>
119+
<string>Vector Dataset(s)</string>
113120
</property>
114121
<property name="buddy">
115-
<cstring>inputSrcDataset</cstring>
116-
</property>
117-
</widget>
118-
</item>
119-
<item row="1" column="2">
120-
<widget class="QLineEdit" name="inputSrcDataset">
121-
<property name="minimumSize">
122-
<size>
123-
<width>200</width>
124-
<height>0</height>
125-
</size>
122+
<cstring>mFileWidget</cstring>
126123
</property>
127124
</widget>
128125
</item>
129-
<item row="1" column="3">
130-
<widget class="QToolButton" name="buttonSelectSrc">
131-
<property name="text">
132-
<string>…</string>
133-
</property>
134-
</widget>
126+
<item row="1" column="1">
127+
<widget class="QgsFileWidget" name="mFileWidget"/>
135128
</item>
136129
</layout>
137130
</widget>
@@ -297,6 +290,13 @@
297290
</layout>
298291
</widget>
299292
<layoutdefault spacing="6" margin="11"/>
293+
<customwidgets>
294+
<customwidget>
295+
<class>QgsFileWidget</class>
296+
<extends>QWidget</extends>
297+
<header>qgsfilewidget.h</header>
298+
</customwidget>
299+
</customwidgets>
300300
<tabstops>
301301
<tabstop>radioSrcFile</tabstop>
302302
<tabstop>radioSrcDirectory</tabstop>
@@ -306,8 +306,7 @@
306306
<tabstop>cmbProtocolTypes</tabstop>
307307
<tabstop>protocolURI</tabstop>
308308
<tabstop>cmbDirectoryTypes</tabstop>
309-
<tabstop>inputSrcDataset</tabstop>
310-
<tabstop>buttonSelectSrc</tabstop>
309+
<tabstop>mFileWidget</tabstop>
311310
<tabstop>cmbDatabaseTypes</tabstop>
312311
<tabstop>cmbConnections</tabstop>
313312
<tabstop>btnNew</tabstop>

0 commit comments

Comments
 (0)