Skip to content
Permalink
Browse files

Use QgsFileWdiget for geopackage database

  • Loading branch information
lbartoletti authored and nyalldawson committed Oct 2, 2018
1 parent b74292a commit 0989e73d7298089d9c99ec95a50c68955ac1398a
Showing with 24 additions and 57 deletions.
  1. +12 −40 src/app/dwg/qgsdwgimportdialog.cpp
  2. +1 −2 src/app/dwg/qgsdwgimportdialog.h
  3. +11 −15 src/ui/qgsdwgimportbase.ui
@@ -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 );
@@ -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();
}
@@ -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();
}

@@ -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 );
@@ -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" ) );
@@ -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" ) );
@@ -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() ) )
@@ -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 )
@@ -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();

@@ -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>
@@ -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">
@@ -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>
@@ -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>
@@ -239,8 +237,6 @@
</customwidget>
</customwidgets>
<tabstops>
<tabstop>leDatabase</tabstop>
<tabstop>pbBrowseDatabase</tabstop>
<tabstop>pbLoadDatabase</tabstop>
<tabstop>mCrsSelector</tabstop>
<tabstop>leDrawing</tabstop>

0 comments on commit 0989e73

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