Skip to content

Commit 14f84d1

Browse files
committedSep 11, 2015
Improvements to SQL Server connection dialog
- List databases found - Remove message boxes replace with message bar - Split into two columns for nicer layout. Funded by TechnoglogOne, Aus
1 parent e2684a4 commit 14f84d1

File tree

3 files changed

+383
-228
lines changed

3 files changed

+383
-228
lines changed
 

‎src/providers/mssql/qgsmssqlnewconnection.cpp

Lines changed: 83 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
***************************************************************************/
1717

1818
#include <QSettings>
19-
#include <QMessageBox>
2019
#include <QInputDialog>
20+
#include <QMessageBox>
2121

2222
#include <QtSql/QSqlDatabase>
2323
#include <QtSql/QSqlError>
@@ -31,6 +31,8 @@ QgsMssqlNewConnection::QgsMssqlNewConnection( QWidget *parent, const QString& co
3131
{
3232
setupUi( this );
3333

34+
lblWarning->hide();
35+
3436
if ( !connName.isEmpty() )
3537
{
3638
// populate the dialog with the information stored for the connection
@@ -40,7 +42,8 @@ QgsMssqlNewConnection::QgsMssqlNewConnection( QWidget *parent, const QString& co
4042
QString key = "/MSSQL/connections/" + connName;
4143
txtService->setText( settings.value( key + "/service" ).toString() );
4244
txtHost->setText( settings.value( key + "/host" ).toString() );
43-
txtDatabase->setText( settings.value( key + "/database" ).toString() );
45+
listDatabase->addItem( settings.value( key + "/database" ).toString() );
46+
listDatabase->setCurrentRow( 0 );
4447
cb_geometryColumns->setChecked( settings.value( key + "/geometryColumns", true ).toBool() );
4548
cb_allowGeometrylessTables->setChecked( settings.value( key + "/allowGeometrylessTables", true ).toBool() );
4649
cb_useEstimatedMetadata->setChecked( settings.value( key + "/estimatedMetadata", false ).toBool() );
@@ -69,15 +72,6 @@ void QgsMssqlNewConnection::accept()
6972
QString baseKey = "/MSSQL/connections/";
7073
settings.setValue( baseKey + "selected", txtName->text() );
7174

72-
if ( chkStorePassword->isChecked() &&
73-
QMessageBox::question( this,
74-
tr( "Saving passwords" ),
75-
tr( "WARNING: You have opted to save your password. It will be stored in plain text in your project files and in your home directory on Unix-like systems, or in your user profile on Windows. If you do not want this to happen, please press the Cancel button.\n" ),
76-
QMessageBox::Ok | QMessageBox::Cancel ) == QMessageBox::Cancel )
77-
{
78-
return;
79-
}
80-
8175
// warn if entry was renamed to an existing connection
8276
if (( mOriginalConnName.isNull() || mOriginalConnName.compare( txtName->text(), Qt::CaseInsensitive ) != 0 ) &&
8377
( settings.contains( baseKey + txtName->text() + "/service" ) ||
@@ -98,9 +92,16 @@ void QgsMssqlNewConnection::accept()
9892
}
9993

10094
baseKey += txtName->text();
95+
QString database;
96+
QListWidgetItem* item = listDatabase->currentItem();
97+
if ( item && item->text() != "(from service)" )
98+
{
99+
database = item->text();
100+
}
101+
101102
settings.setValue( baseKey + "/service", txtService->text() );
102103
settings.setValue( baseKey + "/host", txtHost->text() );
103-
settings.setValue( baseKey + "/database", txtDatabase->text() );
104+
settings.setValue( baseKey + "/database", database );
104105
settings.setValue( baseKey + "/username", chkStoreUsername->isChecked() ? txtUsername->text() : "" );
105106
settings.setValue( baseKey + "/password", chkStorePassword->isChecked() ? txtPassword->text() : "" );
106107
settings.setValue( baseKey + "/saveUsername", chkStoreUsername->isChecked() ? "true" : "false" );
@@ -117,6 +118,11 @@ void QgsMssqlNewConnection::on_btnConnect_clicked()
117118
testConnection();
118119
}
119120

121+
void QgsMssqlNewConnection::on_btnListDatabase_clicked()
122+
{
123+
listDatabases();
124+
}
125+
120126
void QgsMssqlNewConnection::on_cb_trustedConnection_clicked()
121127
{
122128
if ( cb_trustedConnection->checkState() == Qt::Checked )
@@ -137,32 +143,36 @@ void QgsMssqlNewConnection::on_cb_trustedConnection_clicked()
137143

138144
QgsMssqlNewConnection::~QgsMssqlNewConnection()
139145
{
146+
delete bar;
140147
}
141148

142-
void QgsMssqlNewConnection::testConnection()
149+
bool QgsMssqlNewConnection::testConnection( QString testDatabase )
143150
{
144-
if ( txtService->text().isEmpty() )
151+
bar->pushMessage( "Testing connection", "....." );
152+
// Gross but needed to show the last message.
153+
qApp->processEvents();
154+
155+
if ( txtService->text().isEmpty() && txtHost->text().isEmpty() )
145156
{
146-
if ( txtHost->text().isEmpty() )
147-
{
148-
QMessageBox::information( this,
149-
tr( "Test connection" ),
150-
tr( "Connection failed - Host name hasn't been specified.\n\n" ) );
151-
return;
152-
}
157+
bar->clearWidgets();
158+
bar->pushWarning( tr( "Connection Failed" ), tr( "Host name hasn't been specified' " ) );
159+
return false;
160+
}
153161

154-
if ( txtDatabase->text().isEmpty() )
155-
{
156-
QMessageBox::information( this,
157-
tr( "Test connection" ),
158-
tr( "Connection failed - Database name hasn't been specified.\n\n" ) );
159-
return;
160-
}
162+
QString database;
163+
QListWidgetItem* item = listDatabase->currentItem();
164+
if ( !testDatabase.isEmpty() )
165+
{
166+
database = testDatabase;
167+
}
168+
else if ( item && item->text() != "(from service)" )
169+
{
170+
database = item->text();
161171
}
162172

163173
QSqlDatabase db = QgsMssqlProvider::GetDatabase( txtService->text().trimmed(),
164174
txtHost->text().trimmed(),
165-
txtDatabase->text().trimmed(),
175+
database,
166176
txtUsername->text().trimmed(),
167177
txtPassword->text().trimmed() );
168178

@@ -171,19 +181,54 @@ void QgsMssqlNewConnection::testConnection()
171181

172182
if ( !db.open() )
173183
{
174-
QMessageBox::information( this,
175-
tr( "Test connection" ),
176-
db.lastError().text() );
184+
bar->clearWidgets();
185+
bar->pushWarning( tr( "Error opening connection" ), db.lastError().text() );
186+
return false;
177187
}
178188
else
179189
{
180-
QString dbName = txtDatabase->text();
181-
if ( dbName.isEmpty() )
190+
if ( database.isEmpty() )
191+
{
192+
database = txtService->text();
193+
}
194+
bar->clearWidgets();
195+
}
196+
197+
return true;
198+
}
199+
200+
void QgsMssqlNewConnection::listDatabases()
201+
{
202+
testConnection( "master" );
203+
listDatabase->clear();
204+
QString queryStr = "SELECT name FROM master..sysdatabases WHERE name NOT IN ('master', 'tempdb', 'model', 'msdb')";
205+
206+
QSqlDatabase db = QgsMssqlProvider::GetDatabase( txtService->text().trimmed(),
207+
txtHost->text().trimmed(),
208+
"master",
209+
txtUsername->text().trimmed(),
210+
txtPassword->text().trimmed() );
211+
if ( db.open() )
212+
{
213+
QSqlQuery query = QSqlQuery( db );
214+
query.setForwardOnly( true );
215+
query.exec( queryStr );
216+
217+
if ( !txtService->text().isEmpty() )
182218
{
183-
dbName = txtService->text();
219+
listDatabase->addItem( "(from service)" );
184220
}
185-
QMessageBox::information( this,
186-
tr( "Test connection" ),
187-
tr( "Connection to %1 was successful" ).arg( dbName ) );
221+
222+
if ( query.isActive() )
223+
{
224+
while ( query.next() )
225+
{
226+
QString name = query.value( 0 ).toString();
227+
listDatabase->addItem( name );
228+
}
229+
listDatabase->setCurrentRow( 0 );
230+
}
231+
db.close();
188232
}
189233
}
234+

‎src/providers/mssql/qgsmssqlnewconnection.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "qgisgui.h"
2121
#include "qgscontexthelp.h"
2222

23+
2324
/** \class QgsMssqlNewConnection
2425
* \brief Dialog to allow the user to configure and save connection
2526
* information for an MSSQL database
@@ -30,12 +31,20 @@ class QgsMssqlNewConnection : public QDialog, private Ui::QgsMssqlNewConnectionB
3031
public:
3132
//! Constructor
3233
QgsMssqlNewConnection( QWidget *parent = 0, const QString& connName = QString::null, Qt::WindowFlags fl = QgisGui::ModalDialogFlags );
34+
3335
//! Destructor
3436
~QgsMssqlNewConnection();
37+
3538
//! Tests the connection using the parameters supplied
36-
void testConnection();
39+
bool testConnection( QString testDatabase = QString() );
40+
41+
/**
42+
* @brief List all databases found for the given server.
43+
*/
44+
void listDatabases();
3745
public slots:
3846
void accept() override;
47+
void on_btnListDatabase_clicked();
3948
void on_btnConnect_clicked();
4049
void on_cb_trustedConnection_clicked();
4150
void on_buttonBox_helpRequested() { QgsContextHelp::run( metaObject()->className() ); }

0 commit comments

Comments
 (0)
Please sign in to comment.