Skip to content

Commit 0989e73

Browse files
lbartolettinyalldawson
authored andcommitted
Use QgsFileWdiget for geopackage database
1 parent b74292a commit 0989e73

File tree

3 files changed

+24
-57
lines changed

3 files changed

+24
-57
lines changed

src/app/dwg/qgsdwgimportdialog.cpp

+12-40
Original file line numberDiff line numberDiff line change
@@ -46,26 +46,27 @@
4646
#include "qgsproperty.h"
4747
#include "qgslayertree.h"
4848
#include "qgsguiutils.h"
49+
#include "qgsfilewidget.h"
4950

5051
QgsDwgImportDialog::QgsDwgImportDialog( QWidget *parent, Qt::WindowFlags f )
5152
: QDialog( parent, f )
5253
{
5354
setupUi( this );
5455
connect( buttonBox, &QDialogButtonBox::accepted, this, &QgsDwgImportDialog::buttonBox_accepted );
55-
connect( pbBrowseDatabase, &QPushButton::clicked, this, &QgsDwgImportDialog::pbBrowseDatabase_clicked );
56+
connect( mDatabaseFileWidget, &QgsFileWidget::fileChanged, this, &QgsDwgImportDialog::mDatabaseFileWidget_textChanged );
5657
connect( pbBrowseDrawing, &QPushButton::clicked, this, &QgsDwgImportDialog::pbBrowseDrawing_clicked );
5758
connect( pbImportDrawing, &QPushButton::clicked, this, &QgsDwgImportDialog::pbImportDrawing_clicked );
5859
connect( pbLoadDatabase, &QPushButton::clicked, this, &QgsDwgImportDialog::pbLoadDatabase_clicked );
5960
connect( pbSelectAll, &QPushButton::clicked, this, &QgsDwgImportDialog::pbSelectAll_clicked );
6061
connect( pbDeselectAll, &QPushButton::clicked, this, &QgsDwgImportDialog::pbDeselectAll_clicked );
61-
connect( leDatabase, &QLineEdit::textChanged, this, &QgsDwgImportDialog::leDatabase_textChanged );
6262
connect( leLayerGroup, &QLineEdit::textChanged, this, &QgsDwgImportDialog::leLayerGroup_textChanged );
6363
connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsDwgImportDialog::showHelp );
6464

6565
QgsSettings s;
6666
cbExpandInserts->setChecked( s.value( QStringLiteral( "/DwgImport/lastExpandInserts" ), true ).toBool() );
6767
cbMergeLayers->setChecked( s.value( QStringLiteral( "/DwgImport/lastMergeLayers" ), false ).toBool() );
6868
cbUseCurves->setChecked( s.value( QStringLiteral( "/DwgImport/lastUseCurves" ), true ).toBool() );
69+
mDatabaseFileWidget->setDefaultRoot( s.value( QStringLiteral( "/DwgImport/lastDirDatabase" ), QDir::homePath() ).toString() );
6970

7071
leDrawing->setReadOnly( true );
7172
pbImportDrawing->setHidden( true );
@@ -100,9 +101,9 @@ void QgsDwgImportDialog::updateUI()
100101
bool dbReadable = false;
101102
bool dwgReadable = false;
102103

103-
if ( !leDatabase->text().isEmpty() )
104+
if ( !mDatabaseFileWidget->filePath().isEmpty() )
104105
{
105-
QFileInfo fi( leDatabase->text() );
106+
QFileInfo fi( mDatabaseFileWidget->filePath() );
106107
dbAvailable = fi.exists() ? fi.isWritable() : QFileInfo( fi.path() ).isWritable();
107108
dbReadable = fi.exists() && fi.isReadable();
108109
}
@@ -121,39 +122,10 @@ void QgsDwgImportDialog::updateUI()
121122
buttonBox->button( QDialogButtonBox::Ok )->setEnabled( mLayers->rowCount() > 0 && !leLayerGroup->text().isEmpty() );
122123
}
123124

124-
void QgsDwgImportDialog::pbBrowseDatabase_clicked()
125+
void QgsDwgImportDialog::mDatabaseFileWidget_textChanged( const QString &filename )
125126
{
126127
QgsSettings s;
127-
QString dir( s.value( QStringLiteral( "/DwgImport/lastDirDatabase" ), QDir::homePath() ).toString() );
128-
QString filename = QFileDialog::getSaveFileName( this, tr( "Specify GeoPackage database" ), dir, tr( "GeoPackage database" ) + " (*.gpkg *.GPKG)", nullptr, QFileDialog::DontConfirmOverwrite );
129-
if ( filename.isEmpty() )
130-
return;
131-
132-
QFileInfo fi( filename );
133-
if ( fi.exists() && ( QMessageBox::question( this,
134-
tr( "File exists" ),
135-
tr( "The file already exists. Do you want to overwrite the existing file?" ),
136-
QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Cancel ) )
137-
{
138-
leDatabase->clear();
139-
return;
140-
}
141-
else
142-
{
143-
144-
if ( !( filename.endsWith( ".gpkg" ) || filename.endsWith( ".GPKG" ) ) )
145-
filename.append( ".gpkg" );
146-
147-
leDatabase->setText( filename );
148-
s.setValue( QStringLiteral( "/DwgImport/lastDirDatabase" ), QFileInfo( filename ).canonicalPath() );
149-
}
150-
151-
updateUI();
152-
}
153-
154-
void QgsDwgImportDialog::leDatabase_textChanged( const QString &text )
155-
{
156-
Q_UNUSED( text );
128+
s.setValue( QStringLiteral( "/DwgImport/lastDirDatabase" ), QFileInfo( filename ).canonicalPath() );
157129
updateUI();
158130
}
159131

@@ -165,7 +137,7 @@ void QgsDwgImportDialog::leLayerGroup_textChanged( const QString &text )
165137

166138
void QgsDwgImportDialog::pbLoadDatabase_clicked()
167139
{
168-
if ( !QFileInfo::exists( leDatabase->text() ) )
140+
if ( !QFileInfo::exists( mDatabaseFileWidget->filePath() ) )
169141
return;
170142

171143
QgsTemporaryCursorOverride waitCursor( Qt::BusyCursor );
@@ -174,7 +146,7 @@ void QgsDwgImportDialog::pbLoadDatabase_clicked()
174146

175147
QgsVectorLayer::LayerOptions options;
176148
options.loadDefaultStyle = false;
177-
std::unique_ptr<QgsVectorLayer> d( new QgsVectorLayer( QStringLiteral( "%1|layername=drawing" ).arg( leDatabase->text() ), QStringLiteral( "layers" ), QStringLiteral( "ogr" ), options ) );
149+
std::unique_ptr<QgsVectorLayer> d( new QgsVectorLayer( QStringLiteral( "%1|layername=drawing" ).arg( mDatabaseFileWidget->filePath() ), QStringLiteral( "layers" ), QStringLiteral( "ogr" ), options ) );
178150
if ( d && d->isValid() )
179151
{
180152
int idxPath = d->fields().lookupField( QStringLiteral( "path" ) );
@@ -209,7 +181,7 @@ void QgsDwgImportDialog::pbLoadDatabase_clicked()
209181

210182
lblMessage->setVisible( lblVisible );
211183

212-
std::unique_ptr<QgsVectorLayer> l( new QgsVectorLayer( QStringLiteral( "%1|layername=layers" ).arg( leDatabase->text() ), QStringLiteral( "layers" ), QStringLiteral( "ogr" ), options ) );
184+
std::unique_ptr<QgsVectorLayer> l( new QgsVectorLayer( QStringLiteral( "%1|layername=layers" ).arg( mDatabaseFileWidget->filePath() ), QStringLiteral( "layers" ), QStringLiteral( "ogr" ), options ) );
213185
if ( l && l->isValid() )
214186
{
215187
int idxName = l->fields().lookupField( QStringLiteral( "name" ) );
@@ -268,7 +240,7 @@ void QgsDwgImportDialog::pbImportDrawing_clicked()
268240
{
269241
QgsTemporaryCursorOverride waitCursor( Qt::BusyCursor );
270242

271-
QgsDwgImporter importer( leDatabase->text(), mCrsSelector->crs() );
243+
QgsDwgImporter importer( mDatabaseFileWidget->filePath(), mCrsSelector->crs() );
272244

273245
QString error;
274246
if ( importer.import( leDrawing->text(), error, cbExpandInserts->isChecked(), cbUseCurves->isChecked() ) )
@@ -287,7 +259,7 @@ QgsVectorLayer *QgsDwgImportDialog::layer( QgsLayerTreeGroup *layerGroup, const
287259
{
288260
QgsVectorLayer::LayerOptions options;
289261
options.loadDefaultStyle = false;
290-
QgsVectorLayer *l = new QgsVectorLayer( QStringLiteral( "%1|layername=%2" ).arg( leDatabase->text(), table ), table, QStringLiteral( "ogr" ), options );
262+
QgsVectorLayer *l = new QgsVectorLayer( QStringLiteral( "%1|layername=%2" ).arg( mDatabaseFileWidget->filePath(), table ), table, QStringLiteral( "ogr" ), options );
291263
l->setSubsetString( QStringLiteral( "%1space=0 AND block=-1" ).arg( layerFilter ) );
292264

293265
if ( l->featureCount() == 0 )

src/app/dwg/qgsdwgimportdialog.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,12 @@ class QgsDwgImportDialog : public QDialog, private Ui::QgsDwgImportBase
3333

3434
private slots:
3535
void buttonBox_accepted();
36-
void pbBrowseDatabase_clicked();
3736
void pbBrowseDrawing_clicked();
3837
void pbImportDrawing_clicked();
3938
void pbLoadDatabase_clicked();
4039
void pbSelectAll_clicked();
4140
void pbDeselectAll_clicked();
42-
void leDatabase_textChanged( const QString &text );
41+
void mDatabaseFileWidget_textChanged( const QString &filename );
4342
void leLayerGroup_textChanged( const QString &text );
4443
void showHelp();
4544

src/ui/qgsdwgimportbase.ui

+11-15
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114
</property>
115115
<layout class="QGridLayout" name="gridLayout_4">
116116
<item row="1" column="1">
117-
<widget class="QgsProjectionSelectionWidget" name="mCrsSelector" native="true">
117+
<widget class="QgsProjectionSelectionWidget" name="mCrsSelector">
118118
<property name="focusPolicy">
119119
<enum>Qt::StrongFocus</enum>
120120
</property>
@@ -160,9 +160,6 @@
160160
<property name="text">
161161
<string>Target package</string>
162162
</property>
163-
<property name="buddy">
164-
<cstring>leDatabase</cstring>
165-
</property>
166163
</widget>
167164
</item>
168165
<item row="1" column="0">
@@ -178,16 +175,12 @@
178175
<item row="0" column="1">
179176
<layout class="QHBoxLayout" name="horizontalLayout">
180177
<item>
181-
<widget class="QLineEdit" name="leDatabase">
182-
<property name="readOnly">
183-
<bool>true</bool>
178+
<widget class="QgsFileWidget" name="mDatabaseFileWidget">
179+
<property name="dialogTitle">
180+
<string>Select GeoPackage Database</string>
184181
</property>
185-
</widget>
186-
</item>
187-
<item>
188-
<widget class="QPushButton" name="pbBrowseDatabase">
189-
<property name="text">
190-
<string>…</string>
182+
<property name="filter">
183+
<string notr="true">*.gpkg;;*.GPKG</string>
191184
</property>
192185
</widget>
193186
</item>
@@ -231,6 +224,11 @@
231224
</layout>
232225
</widget>
233226
<customwidgets>
227+
<customwidget>
228+
<class>QgsFileWidget</class>
229+
<extends>QWidget</extends>
230+
<header>qgsfilewidget.h</header>
231+
</customwidget>
234232
<customwidget>
235233
<class>QgsProjectionSelectionWidget</class>
236234
<extends>QWidget</extends>
@@ -239,8 +237,6 @@
239237
</customwidget>
240238
</customwidgets>
241239
<tabstops>
242-
<tabstop>leDatabase</tabstop>
243-
<tabstop>pbBrowseDatabase</tabstop>
244240
<tabstop>pbLoadDatabase</tabstop>
245241
<tabstop>mCrsSelector</tabstop>
246242
<tabstop>leDrawing</tabstop>

0 commit comments

Comments
 (0)