00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00030 #include "../inc/Inf.h"
00031
00032 Inf::Inf( QWidget *parent ) : QDialog( parent )
00033 {
00034 InitInterface();
00035
00036 InitSlots();
00037
00038 TranslateInterface();
00039
00040
00041 this->setWindowModality( Qt::ApplicationModal );
00042
00043 this->setMinimumWidth( 380 );
00044 }
00045
00046 Inf::~Inf( void )
00047 {
00048
00049 }
00050
00051 void Inf::setDialogStyle( int s )
00052 {
00053 if( s )
00054 {
00055 lblTables->show();
00056 lstTables->show();
00057
00058 radAllTables->show();
00059 radNonTables->show();
00060 }
00061 else
00062 {
00063 lblTables->hide();
00064 lstTables->hide();
00065
00066 radAllTables->hide();
00067 radNonTables->hide();
00068 }
00069 }
00070
00071 void Inf::InitInterface( void )
00072 {
00073 theLayout = new QVBoxLayout( this );
00074 {
00075 lytInfo = new QGridLayout();
00076 {
00077 lblHost = new QLabel();
00078 lblHost->setAlignment( Qt::AlignRight | Qt::AlignVCenter );
00079 lblHost->setMinimumSize( 70, 20 );
00080 lblHost->setMaximumSize( 70, 20 );
00081
00082 lytHost = new QHBoxLayout();
00083 {
00084 txtHost = new QLineEdit();
00085
00086 lytHost->addWidget( txtHost );
00087
00088 lblPort = new QLabel();
00089 lblPort->setAlignment( Qt::AlignRight | Qt::AlignVCenter );
00090 lblPort->setMinimumSize( 30, 20 );
00091 lblPort->setMaximumSize( 30, 20 );
00092
00093 txtPort = new QLineEdit();
00094 txtPort->setMaxLength( 5 );
00095 txtPort->setMinimumSize( 50, 20 );
00096 txtPort->setMaximumSize( 50, 20 );
00097
00098
00099 lytHost->addWidget( lblPort );
00100 lytHost->addWidget( txtPort );
00101 }
00102
00103 lytInfo->addWidget( lblHost, 0, 0 );
00104 lytInfo->addLayout( lytHost, 0, 1 );
00105
00106 lblName = new QLabel();
00107 lblName->setAlignment( Qt::AlignRight | Qt::AlignVCenter );
00108 lblName->setMinimumSize( 70, 20 );
00109 lblName->setMaximumSize( 70, 20 );
00110
00111 txtName = new QLineEdit();
00112
00113 lytInfo->addWidget( lblName, 1, 0 );
00114 lytInfo->addWidget( txtName, 1, 1 );
00115
00116 lblUser = new QLabel();
00117 lblUser->setAlignment( Qt::AlignRight | Qt::AlignVCenter );
00118 lblUser->setMinimumSize( 70, 20 );
00119 lblUser->setMaximumSize( 70, 20 );
00120
00121 txtUser = new QLineEdit();
00122
00123 lytInfo->addWidget( lblUser, 2, 0 );
00124 lytInfo->addWidget( txtUser, 2, 1 );
00125
00126 lblPass = new QLabel();
00127 lblPass->setAlignment( Qt::AlignRight | Qt::AlignVCenter );
00128 lblPass->setMinimumSize( 70, 20 );
00129 lblPass->setMaximumSize( 70, 20 );
00130
00131 txtPass = new QLineEdit();
00132 txtPass->setEchoMode( QLineEdit::Password );
00133
00134 lytInfo->addWidget( lblPass, 3, 0 );
00135 lytInfo->addWidget( txtPass, 3, 1 );
00136
00137 btnConnect = new QPushButton();
00138
00139 lytInfo->addWidget( btnConnect, 4, 1 );
00140
00141 lytTables = new QVBoxLayout();
00142 {
00143 lblTables = new QLabel();
00144 lblTables->setAlignment( Qt::AlignRight );
00145 lblTables->setMinimumSize( 70, 20 );
00146 lblTables->setMaximumSize( 70, 20 );
00147
00148 radAllTables = new QRadioButton();
00149 radAllTables->setLayoutDirection( Qt::RightToLeft );
00150
00151 radNonTables = new QRadioButton();
00152 radNonTables->setLayoutDirection( Qt::RightToLeft );
00153 radNonTables->setChecked( true );
00154
00155 lytTables->addWidget( lblTables );
00156 lytTables->addWidget( radAllTables );
00157 lytTables->addWidget( radNonTables );
00158 }
00159
00160 lstTables = new QListWidget();
00161
00162 lytInfo->addLayout( lytTables, 6, 0 );
00163 lytInfo->addWidget( lstTables, 6, 1 );
00164 }
00165
00166 theLayout->addLayout( lytInfo );
00167
00168
00169 lytDialog = new QHBoxLayout();
00170 {
00171 btnAccept = new QPushButton();
00172 btnAccept->setEnabled( false );
00173
00174 btnCancel = new QPushButton();
00175
00176 lytDialog->addWidget( btnAccept );
00177 lytDialog->addWidget( btnCancel );
00178 }
00179
00180 theLayout->addLayout( lytDialog );
00181 }
00182
00183 this->setLayout( theLayout );
00184 }
00185
00186 void Inf::InitSlots( void )
00187 {
00188 QObject::connect( btnConnect, SIGNAL( clicked() ), this, SLOT( evtBtnConnect( void ) ) );
00189
00190 QObject::connect( radAllTables, SIGNAL( clicked() ), this, SLOT( evtRadAllTables( void ) ) );
00191
00192 QObject::connect( radNonTables, SIGNAL( clicked() ), this, SLOT( evtRadNonTables( void ) ) );
00193
00194 QObject::connect( btnCancel, SIGNAL( clicked() ), this, SLOT( evtBtnCancel( void ) ) );
00195
00196 QObject::connect( btnAccept, SIGNAL( clicked() ), this, SLOT( evtBtnAccept( void ) ) );
00197 }
00198
00199 void Inf::TranslateInterface( void )
00200 {
00201 this->setWindowTitle( tr( "Database" ) );
00202
00203 lblHost->setText( tr( "Hostname" ) );
00204 lblPort->setText( tr( "Port" ) );
00205 lblName->setText( tr( "Database" ) );
00206 lblUser->setText( tr( "Username" ) );
00207 lblPass->setText( tr( "Password" ) );
00208
00209 btnConnect->setText( tr( "&Connect" ) );
00210
00211 lblTables->setText( tr( "Tables" ) );
00212
00213 radAllTables->setText( tr( "all" ) );
00214 radNonTables->setText( tr( "none" ) );
00215
00216 btnAccept->setText( tr( "&Accept" ) );
00217 btnCancel->setText( tr( "Ca&ncel" ) );
00218 }
00219
00220 void Inf::evtBtnConnect( void )
00221 {
00222 QMessageBox msg;
00223
00224 host = txtHost->text();
00225 port = txtPort->text();
00226 name = txtName->text();
00227 user = txtUser->text();
00228 pass = txtPass->text();
00229
00230 QSqlDatabase base = QSqlDatabase::addDatabase( connectionType );
00231 {
00232 base.setHostName( host );
00233 base.setPort( port.toInt() );
00234 base.setDatabaseName( name );
00235 base.setUserName( user );
00236 base.setPassword( pass );
00237 }
00238
00239 char *table;
00240
00241 if( base.open() )
00242 {
00243 QStringList list = base.tables();
00244
00245 QStringList::Iterator it = list.begin();
00246
00247 while( it != list.end() )
00248 {
00249 table = ( char * ) ( *it ).toStdString().c_str();
00250
00251 if( ( strcmp( table, "geometry_columns" ) != 0 ) && ( strcmp( table, "spatial_ref_sys" ) != 0 ) )
00252 {
00253 QListWidgetItem *item = new QListWidgetItem( *it );
00254
00255 item->setCheckState( Qt::Unchecked );
00256
00257 lstTables->addItem( item );
00258 }
00259
00260 it ++;
00261 }
00262
00263 btnAccept->setEnabled( true );
00264 }
00265 else
00266 {
00267 msg.setText( "* Can't connect to database !" );
00268 msg.exec();
00269 }
00270
00271 base.close();
00272 }
00273
00274 void Inf::evtRadAllTables( void )
00275 {
00276 for( int i = 0; i < lstTables->count(); i ++ )
00277 {
00278 lstTables->item( i )->setCheckState( Qt::Checked );
00279 }
00280 }
00281
00282 void Inf::evtRadNonTables( void )
00283 {
00284 for( int i = 0; i < lstTables->count(); i ++ )
00285 {
00286 lstTables->item( i )->setCheckState( Qt::Unchecked );
00287 }
00288 }
00289
00290 void Inf::evtBtnAccept( void )
00291 {
00292 int nb = 0;
00293
00294 QString tables;
00295 QString separator;
00296
00297 host = txtHost->text();
00298 port = txtPort->text();
00299 name = txtName->text();
00300 user = txtUser->text();
00301 pass = txtPass->text();
00302
00303 if( connectionType.compare( "QPSQL" ) == 0 )
00304 {
00305 separator = " ";
00306
00307 connectionString = tr( "PG:" );
00308
00309 if( name.size() > 0 ) connectionString += tr( "dbname=" ) + name;
00310 if( host.size() > 0 ) connectionString += tr( " host=" ) + host;
00311 if( port.size() > 0 ) connectionString += tr( " port=" ) + port;
00312 if( user.size() > 0 ) connectionString += tr( " user=" ) + user;
00313 if( pass.size() > 0 ) connectionString += tr( " password=" ) + pass;
00314 }
00315 else if( connectionType.compare( "QMYSQL" ) == 0 )
00316 {
00317 separator = ",";
00318
00319 if( name.size() > 0 ) connectionString = tr( "MySQL:" ) + name;
00320 if( host.size() > 0 ) connectionString += ",host=" + host;
00321 if( port.size() > 0 ) connectionString += ",port=" + port;
00322 if( user.size() > 0 ) connectionString += ",user=" + user;
00323 if( pass.size() > 0 ) connectionString += ",password=" + pass;
00324 }
00325 else if( connectionType.compare( "QOCI" ) == 0 )
00326 {
00327 separator = "";
00328
00329 if( user.size() > 0 ) connectionString = "OCI:" + user;
00330 if( pass.size() > 0 ) connectionString += "/" + pass;
00331 if( host.size() > 0 ) connectionString += "@" + host;
00332 }
00333 else if( connectionType.compare( "QODBC" ) == 0 )
00334 {
00335 separator = "";
00336
00337 if( user.size() > 0 ) connectionString = "ODBC:" + user;
00338 if( pass.size() > 0 ) connectionString += "/" + pass;
00339 if( host.size() > 0 ) connectionString += "@" + host;
00340 }
00341 else if( connectionType.compare( "QSQLITE" ) == 0 )
00342 {
00343 separator = "";
00344 }
00345
00346 selectedTables.clear();
00347
00348 for( int i = 0; i < lstTables->count(); i ++ )
00349 {
00350 if( lstTables->item( i )->checkState() == Qt::Checked )
00351 {
00352 if( nb > 0 )
00353 {
00354 tables += ",";
00355 }
00356
00357 tables += lstTables->item( i )->text();
00358
00359 selectedTables.append( lstTables->item( i )->text() );
00360
00361 nb ++;
00362 }
00363 }
00364
00365 if( nb > 0 )
00366 {
00367 connectionString += separator;
00368 connectionString += "tables=";
00369 connectionString += tables;
00370 }
00371
00372 lstTables->clear();
00373
00374 btnAccept->setEnabled( false );
00375
00376 this->accept();
00377 }
00378
00379 void Inf::evtBtnCancel( void )
00380 {
00381 txtHost->clear();
00382 txtPort->clear();
00383 txtName->clear();
00384 txtUser->clear();
00385 txtPass->clear();
00386
00387 lstTables->clear();
00388
00389 btnAccept->setEnabled( false );
00390
00391 this->reject();
00392 }
00393
00394 void Inf::setConnectionType( QString type )
00395 {
00396 connectionType = type;
00397
00398 if( connectionType.compare( tr( "QPSQL" ) ) == 0 )
00399 {
00400 txtPort->setText( tr( "5432" ) );
00401 }
00402 else if( connectionType.compare( tr( "QMYSQL" ) ) == 0 )
00403 {
00404 txtPort->setText( tr( "3306" ) );
00405 }
00406 else if( connectionType.compare( tr( "QOCI" ) ) == 0 )
00407 {
00408 txtPort->setText( tr( "1521" ) );
00409 }
00410 else if( connectionType.compare( tr( "QODBC" ) ) == 0 )
00411 {
00412 txtPort->setText( tr( "1521" ) );
00413 }
00414 else if( connectionType.compare( tr( "QSQLITE" ) ) == 0 )
00415 {
00416 txtPort->setText( tr( "1521" ) );
00417 }
00418 }
00419
00420 QString Inf::getConnectionString( void )
00421 {
00422 return connectionString;
00423 }
00424
00425 QStringList Inf::getSelectedTables( void )
00426 {
00427 return selectedTables;
00428 }