Skip to content

Commit

Permalink
Use QgsFileWdiget for geopackage database
Browse files Browse the repository at this point in the history
  • Loading branch information
lbartoletti authored and nyalldawson committed Oct 9, 2018
1 parent b74292a commit 0989e73
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 57 deletions.
52 changes: 12 additions & 40 deletions src/app/dwg/qgsdwgimportdialog.cpp
Expand Up @@ -46,26 +46,27 @@
#include "qgsproperty.h"
#include "qgslayertree.h"
#include "qgsguiutils.h"
#include "qgsfilewidget.h"

QgsDwgImportDialog::QgsDwgImportDialog( QWidget *parent, Qt::WindowFlags f )
: QDialog( parent, f )
{
setupUi( this );
connect( buttonBox, &QDialogButtonBox::accepted, this, &QgsDwgImportDialog::buttonBox_accepted );
connect( pbBrowseDatabase, &QPushButton::clicked, this, &QgsDwgImportDialog::pbBrowseDatabase_clicked );
connect( mDatabaseFileWidget, &QgsFileWidget::fileChanged, this, &QgsDwgImportDialog::mDatabaseFileWidget_textChanged );
connect( pbBrowseDrawing, &QPushButton::clicked, this, &QgsDwgImportDialog::pbBrowseDrawing_clicked );
connect( pbImportDrawing, &QPushButton::clicked, this, &QgsDwgImportDialog::pbImportDrawing_clicked );
connect( pbLoadDatabase, &QPushButton::clicked, this, &QgsDwgImportDialog::pbLoadDatabase_clicked );
connect( pbSelectAll, &QPushButton::clicked, this, &QgsDwgImportDialog::pbSelectAll_clicked );
connect( pbDeselectAll, &QPushButton::clicked, this, &QgsDwgImportDialog::pbDeselectAll_clicked );
connect( leDatabase, &QLineEdit::textChanged, this, &QgsDwgImportDialog::leDatabase_textChanged );
connect( leLayerGroup, &QLineEdit::textChanged, this, &QgsDwgImportDialog::leLayerGroup_textChanged );
connect( buttonBox, &QDialogButtonBox::helpRequested, this, &QgsDwgImportDialog::showHelp );

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

leDrawing->setReadOnly( true );
pbImportDrawing->setHidden( true );
Expand Down Expand Up @@ -100,9 +101,9 @@ void QgsDwgImportDialog::updateUI()
bool dbReadable = false;
bool dwgReadable = false;

if ( !leDatabase->text().isEmpty() )
if ( !mDatabaseFileWidget->filePath().isEmpty() )
{
QFileInfo fi( leDatabase->text() );
QFileInfo fi( mDatabaseFileWidget->filePath() );
dbAvailable = fi.exists() ? fi.isWritable() : QFileInfo( fi.path() ).isWritable();
dbReadable = fi.exists() && fi.isReadable();
}
Expand All @@ -121,39 +122,10 @@ void QgsDwgImportDialog::updateUI()
buttonBox->button( QDialogButtonBox::Ok )->setEnabled( mLayers->rowCount() > 0 && !leLayerGroup->text().isEmpty() );
}

void QgsDwgImportDialog::pbBrowseDatabase_clicked()
void QgsDwgImportDialog::mDatabaseFileWidget_textChanged( const QString &filename )
{
QgsSettings s;
QString dir( s.value( QStringLiteral( "/DwgImport/lastDirDatabase" ), QDir::homePath() ).toString() );
QString filename = QFileDialog::getSaveFileName( this, tr( "Specify GeoPackage database" ), dir, tr( "GeoPackage database" ) + " (*.gpkg *.GPKG)", nullptr, QFileDialog::DontConfirmOverwrite );
if ( filename.isEmpty() )
return;

QFileInfo fi( filename );
if ( fi.exists() && ( QMessageBox::question( this,
tr( "File exists" ),
tr( "The file already exists. Do you want to overwrite the existing file?" ),
QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Cancel ) )
{
leDatabase->clear();
return;
}
else
{

if ( !( filename.endsWith( ".gpkg" ) || filename.endsWith( ".GPKG" ) ) )
filename.append( ".gpkg" );

leDatabase->setText( filename );
s.setValue( QStringLiteral( "/DwgImport/lastDirDatabase" ), QFileInfo( filename ).canonicalPath() );
}

updateUI();
}

void QgsDwgImportDialog::leDatabase_textChanged( const QString &text )
{
Q_UNUSED( text );
s.setValue( QStringLiteral( "/DwgImport/lastDirDatabase" ), QFileInfo( filename ).canonicalPath() );
updateUI();
}

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

void QgsDwgImportDialog::pbLoadDatabase_clicked()
{
if ( !QFileInfo::exists( leDatabase->text() ) )
if ( !QFileInfo::exists( mDatabaseFileWidget->filePath() ) )
return;

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

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

lblMessage->setVisible( lblVisible );

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

QgsDwgImporter importer( leDatabase->text(), mCrsSelector->crs() );
QgsDwgImporter importer( mDatabaseFileWidget->filePath(), mCrsSelector->crs() );

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

if ( l->featureCount() == 0 )
Expand Down
3 changes: 1 addition & 2 deletions src/app/dwg/qgsdwgimportdialog.h
Expand Up @@ -33,13 +33,12 @@ class QgsDwgImportDialog : public QDialog, private Ui::QgsDwgImportBase

private slots:
void buttonBox_accepted();
void pbBrowseDatabase_clicked();
void pbBrowseDrawing_clicked();
void pbImportDrawing_clicked();
void pbLoadDatabase_clicked();
void pbSelectAll_clicked();
void pbDeselectAll_clicked();
void leDatabase_textChanged( const QString &text );
void mDatabaseFileWidget_textChanged( const QString &filename );
void leLayerGroup_textChanged( const QString &text );
void showHelp();

Expand Down
26 changes: 11 additions & 15 deletions src/ui/qgsdwgimportbase.ui
Expand Up @@ -114,7 +114,7 @@
</property>
<layout class="QGridLayout" name="gridLayout_4">
<item row="1" column="1">
<widget class="QgsProjectionSelectionWidget" name="mCrsSelector" native="true">
<widget class="QgsProjectionSelectionWidget" name="mCrsSelector">
<property name="focusPolicy">
<enum>Qt::StrongFocus</enum>
</property>
Expand Down Expand Up @@ -160,9 +160,6 @@
<property name="text">
<string>Target package</string>
</property>
<property name="buddy">
<cstring>leDatabase</cstring>
</property>
</widget>
</item>
<item row="1" column="0">
Expand All @@ -178,16 +175,12 @@
<item row="0" column="1">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLineEdit" name="leDatabase">
<property name="readOnly">
<bool>true</bool>
<widget class="QgsFileWidget" name="mDatabaseFileWidget">
<property name="dialogTitle">
<string>Select GeoPackage Database</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pbBrowseDatabase">
<property name="text">
<string>…</string>
<property name="filter">
<string notr="true">*.gpkg;;*.GPKG</string>
</property>
</widget>
</item>
Expand Down Expand Up @@ -231,6 +224,11 @@
</layout>
</widget>
<customwidgets>
<customwidget>
<class>QgsFileWidget</class>
<extends>QWidget</extends>
<header>qgsfilewidget.h</header>
</customwidget>
<customwidget>
<class>QgsProjectionSelectionWidget</class>
<extends>QWidget</extends>
Expand All @@ -239,8 +237,6 @@
</customwidget>
</customwidgets>
<tabstops>
<tabstop>leDatabase</tabstop>
<tabstop>pbBrowseDatabase</tabstop>
<tabstop>pbLoadDatabase</tabstop>
<tabstop>mCrsSelector</tabstop>
<tabstop>leDrawing</tabstop>
Expand Down

0 comments on commit 0989e73

Please sign in to comment.