Skip to content

Commit

Permalink
fix connection dialogs (apply/fix #2217, #2231, #2232)
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@12390 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Dec 9, 2009
1 parent e988374 commit 7d573ce
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 94 deletions.
5 changes: 3 additions & 2 deletions src/app/ogr/qgsnewogrconnection.h
Expand Up @@ -37,12 +37,13 @@ class QgsNewOgrConnection : public QDialog, private Ui::QgsNewOgrConnectionBase
~QgsNewOgrConnection();
//! Tests the connection using the parameters supplied
void testConnection();
//! Saves the connection to ~/.qt/qgisrc
void saveConnection();
public slots:
void accept();
void on_btnConnect_clicked();
void on_buttonBox_helpRequested() { QgsContextHelp::run( metaObject()->className() ); }

private:
QString mOriginalConnName;
};

#endif // QGSNEWOGRCONNECTIONBASE_H
52 changes: 42 additions & 10 deletions src/app/ogr/qgsopenvectorlayerdialog.cpp
Expand Up @@ -150,20 +150,19 @@ QString QgsOpenVectorLayerDialog::dataSourceType()
void QgsOpenVectorLayerDialog::addNewConnection()
{
QgsNewOgrConnection *nc = new QgsNewOgrConnection( this );
if ( nc->exec() )
{
populateConnectionList();
}
nc->exec();
delete nc;

populateConnectionList();
}

void QgsOpenVectorLayerDialog::editConnection()
{
QgsNewOgrConnection *nc = new QgsNewOgrConnection( this, cmbDatabaseTypes->currentText(), cmbConnections->currentText() );
nc->exec();
delete nc;

if ( nc->exec() )
{
nc->saveConnection();
}
populateConnectionList();
}

void QgsOpenVectorLayerDialog::deleteConnection()
Expand Down Expand Up @@ -309,12 +308,22 @@ void QgsOpenVectorLayerDialog::on_buttonSelectSrc_clicked()


//********************auto connected slots *****************/
void QgsOpenVectorLayerDialog::on_buttonBox_accepted()
void QgsOpenVectorLayerDialog::accept()
{
QSettings settings;
QgsDebugMsg( "dialog button accepted" );
if ( radioSrcDatabase->isChecked() )
{
if ( !settings.contains( "/" + cmbDatabaseTypes->currentText()
+ "/connections/" + cmbConnections->currentText()
+ "/host" ) )
{
QMessageBox::information( this,
tr( "Add vector layer" ),
tr( "No database selected." ) );
return;
}

mDataSources.clear();
QString baseKey = "/" + cmbDatabaseTypes->currentText() + "/connections/";
baseKey += cmbConnections->currentText();
Expand All @@ -340,16 +349,39 @@ void QgsOpenVectorLayerDialog::on_buttonBox_accepted()
}
else if ( radioSrcProtocol->isChecked() )
{
if ( protocolURI->text().isEmpty() )
{
QMessageBox::information( this,
tr( "Add vector layer" ),
tr( "No protocol URI entered." ) );
return;
}

mDataSources.clear();
mDataSources.append( createProtocolURI(
cmbProtocolTypes->currentText(),
protocolURI->text()
) );
}
else if ( radioSrcFile->isChecked() && inputSrcDataset->text().isEmpty() )
{
QMessageBox::information( this,
tr( "Add vector layer" ),
tr( "No layers selected." ) );
return;
}
else if ( radioSrcDirectory->isChecked() && inputSrcDataset->text().isEmpty() )
{
QMessageBox::information( this,
tr( "Add vector layer" ),
tr( "No directory selected." ) );
return;
}

// Save the used encoding
settings.setValue( "/UI/encoding", encoding() );

accept();
QDialog::accept();
}

void QgsOpenVectorLayerDialog::on_radioSrcFile_toggled( bool checked )
Expand Down
3 changes: 2 additions & 1 deletion src/app/ogr/qgsopenvectorlayerdialog.h
Expand Up @@ -73,7 +73,8 @@ class QgsOpenVectorLayerDialog : public QDialog, private Ui::QgsOpenVectorLayerD
//! Sets the selected connection
void setSelectedConnection();

void on_buttonBox_accepted();
void accept();

void on_buttonSelectSrc_clicked();
void on_radioSrcFile_toggled( bool checked );
void on_radioSrcDirectory_toggled( bool checked );
Expand Down
49 changes: 30 additions & 19 deletions src/app/qgisapp.cpp
Expand Up @@ -2106,19 +2106,17 @@ void QgisApp::about()
.arg( QGis::QGIS_VERSION )
.arg( QGis::QGIS_SVN_VERSION );
#ifdef HAVE_POSTGRESQL

versionString += tr( " This copy of QGIS has been built with PostgreSQL support." );
#else

versionString += tr( " This copy of QGIS has been built without PostgreSQL support." );
#endif
#ifdef HAVE_SPATIALITE

#ifdef HAVE_SPATIALITE
versionString += tr( "\nThis copy of QGIS has been built with SpatiaLite support." );
#else

versionString += tr( "\nThis copy of QGIS has been built without SpatiaLite support." );
#endif

versionString += tr( "\nThis binary was compiled against Qt %1,"
"and is currently running against Qt %2" )
.arg( QT_VERSION_STR )
Expand Down Expand Up @@ -2399,16 +2397,16 @@ void QgisApp::loadOGRSublayers( QString layertype, QString uri, QStringList list
}

/** This helper checks to see whether the file name appears to be a valid vector file name */
bool QgisApp::isValidVectorFileName( QString theFileNameQString )
bool QgisApp::isValidShapeFileName( QString theFileNameQString )
{
return ( theFileNameQString.toLower().endsWith( ".shp" ) );
return theFileNameQString.endsWith( ".shp", Qt::CaseInsensitive );
}

/** Overloaded of the above function provided for convenience that takes a qstring pointer */
bool QgisApp::isValidVectorFileName( QString * theFileNameQString )
bool QgisApp::isValidShapeFileName( QString * theFileNameQString )
{
//dereference and delegate
return isValidVectorFileName( *theFileNameQString );
return isValidShapeFileName( *theFileNameQString );
}

#ifndef HAVE_POSTGRESQL
Expand Down Expand Up @@ -2524,7 +2522,6 @@ void QgisApp::addSpatiaLiteLayer()
QStringList::Iterator it = tables.begin();
while ( it != tables.end() )
{

// normalizing the layer name
QString layername = *it;
layername = layername.mid( 1 );
Expand Down Expand Up @@ -2736,13 +2733,28 @@ void QgisApp::newVectorLayer()
openFileDialog->selectFilter( lastUsedFilter );
}

if ( openFileDialog->exec() != QDialog::Accepted )
int res;
while (( res = openFileDialog->exec() ) == QDialog::Accepted )
{
fileName = openFileDialog->selectedFiles().first();

if ( fileformat == "ESRI Shapefile" && !isValidShapeFileName( fileName ) )
{
QMessageBox::information( this,
tr( "New Shapefile" ),
tr( "Shapefiles must end on .shp" ) );
continue;
}

break;
}

if ( res == QDialog::Rejected )
{
delete openFileDialog;
return;
}

fileName = openFileDialog->selectedFiles().first();
enc = openFileDialog->encoding();

// If the file exists, delete it otherwise we'll end up loading that
Expand All @@ -2751,9 +2763,6 @@ void QgisApp::newVectorLayer()
// with a linestring file).
if ( fileformat == "ESRI Shapefile" )
{
if ( !fileName.endsWith( ".shp", Qt::CaseInsensitive ) )
fileName += ".shp";

QgsVectorFileWriter::deleteShapeFile( fileName );
}
else
Expand Down Expand Up @@ -3505,15 +3514,17 @@ void QgisApp::deleteSelected( QgsMapLayer *layer )

if ( !layer )
{
QMessageBox::information( this, tr( "No Layer Selected" ),
QMessageBox::information( this,
tr( "No Layer Selected" ),
tr( "To delete features, you must select a vector layer in the legend" ) );
return;
}

QgsVectorLayer* vlayer = qobject_cast<QgsVectorLayer *>( layer );
if ( !vlayer )
{
QMessageBox::information( this, tr( "No Vector Layer Selected" ),
QMessageBox::information( this,
tr( "No Vector Layer Selected" ),
tr( "Deleting features only works on vector layers" ) );
return;
}
Expand Down Expand Up @@ -3957,7 +3968,7 @@ void QgisApp::editCut( QgsMapLayer * layerContainingSelection )
if ( selectionVectorLayer != 0 )
{
QgsFeatureList features = selectionVectorLayer->selectedFeatures();
clipboard()->replaceWithCopyOf( selectionVectorLayer->dataProvider()->fields(), features );
clipboard()->replaceWithCopyOf( selectionVectorLayer->pendingFields(), features );
clipboard()->setCRS( selectionVectorLayer->srs() );
selectionVectorLayer->beginEditCommand( tr( "Features cut" ) );
selectionVectorLayer->deleteSelectedFeatures();
Expand Down Expand Up @@ -3986,14 +3997,14 @@ void QgisApp::editCopy( QgsMapLayer * layerContainingSelection )
if ( selectionVectorLayer != 0 )
{
QgsFeatureList features = selectionVectorLayer->selectedFeatures();
clipboard()->replaceWithCopyOf( selectionVectorLayer->dataProvider()->fields(), features );
clipboard()->replaceWithCopyOf( selectionVectorLayer->pendingFields(), features );
clipboard()->setCRS( selectionVectorLayer->srs() );
}
}
}


void QgisApp::editPaste( QgsMapLayer * destinationLayer )
void QgisApp::editPaste( QgsMapLayer *destinationLayer )
{
if ( mMapCanvas && mMapCanvas->isDrawing() )
{
Expand Down
6 changes: 3 additions & 3 deletions src/app/qgisapp.h
Expand Up @@ -690,10 +690,10 @@ class QgisApp : public QMainWindow
*/
bool addRasterLayer( QgsRasterLayer * theRasterLayer );
//@todo We should move these next two into vector layer class
/** This helper checks to see whether the file name appears to be a valid vector file name */
bool isValidVectorFileName( QString theFileNameQString );
/** This helper checks to see whether the file name appears to be a valid shape file name */
bool isValidShapeFileName( QString theFileNameQString );
/** Overloaded version of the above function provided for convenience that takes a qstring pointer */
bool isValidVectorFileName( QString * theFileNameQString );
bool isValidShapeFileName( QString * theFileNameQString );
/** add this file to the recently opened/saved projects list
* pass settings by reference since creating more than one
* instance simultaneously results in data loss.
Expand Down
71 changes: 33 additions & 38 deletions src/app/qgsnewconnection.cpp
Expand Up @@ -79,7 +79,39 @@ QgsNewConnection::QgsNewConnection( QWidget *parent, const QString& connName, Qt
/** Autoconnected SLOTS **/
void QgsNewConnection::accept()
{
saveConnection();
QSettings settings;
QString baseKey = "/PostgreSQL/connections/";
settings.setValue( baseKey + "selected", txtName->text() );

// warn if entry was renamed to an existing connection
if (( mOriginalConnName.isNull() || mOriginalConnName != txtName->text() ) &&
settings.contains( baseKey + txtName->text() + "/host" ) &&
QMessageBox::question( this,
tr( "Save connection" ),
tr( "Should the existing connection %1 be overwritten?" ).arg( txtName->text() ),
QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Cancel )
{
return;
}

// on rename delete the original entry first
if ( !mOriginalConnName.isNull() && mOriginalConnName != txtName->text() )
{

settings.remove( baseKey + mOriginalConnName );
}

baseKey += txtName->text();
settings.setValue( baseKey + "/host", txtHost->text() );
settings.setValue( baseKey + "/database", txtDatabase->text() );
settings.setValue( baseKey + "/port", txtPort->text() );
settings.setValue( baseKey + "/username", txtUsername->text() );
settings.setValue( baseKey + "/password", chkStorePassword->isChecked() ? txtPassword->text() : "" );
settings.setValue( baseKey + "/publicOnly", cb_publicSchemaOnly->isChecked() );
settings.setValue( baseKey + "/geometryColumnsOnly", cb_geometryColumnsOnly->isChecked() );
settings.setValue( baseKey + "/save", chkStorePassword->isChecked() ? "true" : "false" );
settings.setValue( baseKey + "/sslmode", cbxSSLmode->currentIndex() );

QDialog::accept();
}

Expand Down Expand Up @@ -122,40 +154,3 @@ void QgsNewConnection::testConnection()
// free pg connection resources
PQfinish( pd );
}

void QgsNewConnection::saveConnection()
{
QSettings settings;
QString baseKey = "/PostgreSQL/connections/";
settings.setValue( baseKey + "selected", txtName->text() );
//delete original entry first
if ( !mOriginalConnName.isNull() && mOriginalConnName != txtName->text() )
{
settings.remove( baseKey + mOriginalConnName );
}
baseKey += txtName->text();
settings.setValue( baseKey + "/host", txtHost->text() );
settings.setValue( baseKey + "/database", txtDatabase->text() );
settings.setValue( baseKey + "/port", txtPort->text() );
settings.setValue( baseKey + "/username", txtUsername->text() );
settings.setValue( baseKey + "/password", chkStorePassword->isChecked() ? txtPassword->text() : "" );
settings.setValue( baseKey + "/publicOnly", cb_publicSchemaOnly->isChecked() );
settings.setValue( baseKey + "/geometryColumnsOnly", cb_geometryColumnsOnly->isChecked() );
settings.setValue( baseKey + "/save", chkStorePassword->isChecked() ? "true" : "false" );
settings.setValue( baseKey + "/sslmode", cbxSSLmode->currentIndex() );
}

#if 0
void QgsNewConnection::saveConnection()
{
QSettings settings;
QString baseKey = "/PostgreSQL/connections/";
baseKey += txtName->text();
settings.setValue( baseKey + "/host", txtHost->text() );
settings.setValue( baseKey + "/database", txtDatabase->text() );

settings.setValue( baseKey + "/username", txtUsername->text() );
settings.setValue( baseKey + "/password", chkStorePassword->isChecked() ? txtPassword->text() : "" );
accept();
}
#endif
2 changes: 0 additions & 2 deletions src/app/qgsnewconnection.h
Expand Up @@ -34,8 +34,6 @@ class QgsNewConnection : public QDialog, private Ui::QgsNewConnectionBase
~QgsNewConnection();
//! Tests the connection using the parameters supplied
void testConnection();
//! Saves the connection to ~/.qt/qgisrc
void saveConnection();
public slots:
void accept();
void on_btnConnect_clicked();
Expand Down
15 changes: 14 additions & 1 deletion src/app/qgsnewhttpconnection.cpp
Expand Up @@ -18,6 +18,7 @@
#include "qgsnewhttpconnection.h"
#include "qgscontexthelp.h"
#include <QSettings>
#include <QMessageBox>

QgsNewHttpConnection::QgsNewHttpConnection(
QWidget *parent, const QString& baseKey, const QString& connName, Qt::WFlags fl ):
Expand Down Expand Up @@ -54,12 +55,24 @@ void QgsNewHttpConnection::accept()
QString key = mBaseKey + txtName->text();
QString credentialsKey = "/Qgis/WMS/" + txtName->text();

//delete original entry first
// warn if entry was renamed to an existing connection
if (( mOriginalConnName.isNull() || mOriginalConnName != txtName->text() ) &&
settings.contains( key + "/url" ) &&
QMessageBox::question( this,
tr( "Save connection" ),
tr( "Should the existing connection %1 be overwritten?" ).arg( txtName->text() ),
QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Cancel )
{
return;
}

// on rename delete original entry first
if ( !mOriginalConnName.isNull() && mOriginalConnName != key )
{
settings.remove( mBaseKey + mOriginalConnName );
settings.remove( "/Qgis/WMS/" + mOriginalConnName );
}

settings.setValue( key + "/url", txtUrl->text().trimmed() );
settings.setValue( credentialsKey + "/username", txtUserName->text() );
settings.setValue( credentialsKey + "/password", txtPassword->text() );
Expand Down

0 comments on commit 7d573ce

Please sign in to comment.