Skip to content

Commit ef2d532

Browse files
author
Emilio Loi
committedApr 16, 2013
Added controls on style table consistency and named style validity
1 parent fcb27d2 commit ef2d532

File tree

4 files changed

+53
-29
lines changed

4 files changed

+53
-29
lines changed
 

‎src/app/qgsloadstylefromdbdialog.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,17 @@ QgsLoadStyleFromDBDialog::QgsLoadStyleFromDBDialog( QWidget *parent )
3131

3232
connect(mRelatedTable, SIGNAL( cellClicked( int,int ) ), this, SLOT( cellSelectedRelatedTable( int ) ) );
3333
connect(mOthersTable, SIGNAL( cellClicked( int,int ) ), this, SLOT( cellSelectedOthersTable( int ) ) );
34-
connect(mCancelButton, SIGNAL( clicked() ), this, SLOT( cancelButtonClicked() ) );
35-
connect(mLoadButton, SIGNAL( clicked() ), this, SLOT( loadButtonClicked() ) );
34+
connect(mRelatedTable, SIGNAL( doubleClicked( QModelIndex ) ),
35+
this, SLOT( accept() ) );
36+
connect(mOthersTable, SIGNAL( doubleClicked( QModelIndex ) ),
37+
this, SLOT( accept() ) );
38+
connect(mCancelButton, SIGNAL( clicked() ), this, SLOT( reject() ) );
39+
connect(mLoadButton, SIGNAL( clicked() ), this, SLOT( accept() ) );
3640

3741
setTabOrder( mRelatedTable, mOthersTable );
3842
setTabOrder( mOthersTable, mCancelButton );
3943
setTabOrder( mCancelButton, mLoadButton );
44+
4045
}
4146

4247
void QgsLoadStyleFromDBDialog::initializeLists( QVector<QString> ids, QVector<QString> names,
@@ -49,7 +54,7 @@ void QgsLoadStyleFromDBDialog::initializeLists( QVector<QString> ids, QVector<QS
4954
int relatedTableNOfCols = ( sectionLimit > 0 ) ? 2 : 1;
5055
int othersTableNOfCols = ( ids.count() - sectionLimit > 0 ) ? 2 : 1;
5156
QString twoColsHeader( "Name;Description" );
52-
QString oneColsHeader( "No style found in the database" );
57+
QString oneColsHeader( "No styles found in the database" );
5358
QString relatedTableHeader = ( relatedTableNOfCols == 1 ) ? oneColsHeader : twoColsHeader;
5459
QString othersTableHeader = ( othersTableNOfCols == 1 ) ? oneColsHeader : twoColsHeader;
5560

@@ -90,14 +95,3 @@ void QgsLoadStyleFromDBDialog::cellSelectedOthersTable( int r )
9095
mLoadButton->setEnabled( true );
9196
mSelectedStyleId = mIds.value( r + mSectionLimit );
9297
}
93-
94-
void QgsLoadStyleFromDBDialog::cancelButtonClicked()
95-
{
96-
mSelectedStyleId = tr( "" ) ;
97-
this->close();
98-
}
99-
100-
void QgsLoadStyleFromDBDialog::loadButtonClicked()
101-
{
102-
this->close();
103-
}

‎src/app/qgsloadstylefromdbdialog.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ class QgsLoadStyleFromDBDialog: public QDialog, private Ui::QgsLoadStyleFromDBDi
3232
public slots:
3333
void cellSelectedRelatedTable( int r );
3434
void cellSelectedOthersTable( int r );
35-
void cancelButtonClicked();
36-
void loadButtonClicked();
3735

3836
private:
3937

‎src/app/qgsvectorlayerproperties.cpp

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -752,27 +752,36 @@ void QgsVectorLayerProperties::showListOfStylesFromDatabase()
752752
QString errorMsg;
753753
QVector<QString> ids, names, descriptions;
754754

755-
756-
757755
int sectionLimit = layer->listStylesInDatabase(ids, names, descriptions, errorMsg);
758-
759756
if( !errorMsg.isNull() )
760757
{
761-
QMessageBox::warning(this, tr( "Error!" ), tr( "Retrieving" ) );
758+
QMessageBox::warning( this, tr( "Error occured retrievning styles from database "), errorMsg );
762759
return;
763760
}
761+
764762
QgsLoadStyleFromDBDialog dialog;
765763
dialog.initializeLists(ids, names, descriptions, sectionLimit);
766-
dialog.exec();
767764

768-
QString selectedStyleId = dialog.getSelectedStyleId();
769-
if( selectedStyleId.compare( tr( "" ) ) )
765+
if( dialog.exec() == QDialog::Accepted )
770766
{
767+
QString selectedStyleId = dialog.getSelectedStyleId();
768+
771769
QString qmlStyle = layer->getStyleFromDatabase( selectedStyleId, errorMsg );
772-
if( !qmlStyle.isNull() ){
773-
layer->applyNamedStyle( qmlStyle, errorMsg);
770+
if( !errorMsg.isNull() )
771+
{
772+
QMessageBox::warning( this, tr( "Error occured retrievning styles from database "), errorMsg );
773+
return;
774+
}
775+
if( layer->applyNamedStyle( qmlStyle, errorMsg) )
776+
{
774777
reset();
775778
}
779+
else
780+
{
781+
QMessageBox::warning( this, tr( "Error occured retrievning styles from database "),
782+
tr( "The style retriev is not a valid named style. Error message: %1" )
783+
.arg( errorMsg ) );
784+
}
776785

777786
}
778787

‎src/providers/postgres/qgspostgresprovider.cpp

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3360,7 +3360,7 @@ QGISEXTERN int listStyles( const QString& uri, QVector<QString> &ids, QVector<Q
33603360
QgsPostgresConn* conn = QgsPostgresConn::connectDb( dsUri.connectionInfo(), false );
33613361
if ( !conn )
33623362
{
3363-
errCause = QObject::tr( "Connection to database failed" );
3363+
errCause = QObject::tr( "Connection to database failed using username: %1" ).arg( dsUri.username() );
33643364
return -1;
33653365
}
33663366

@@ -3377,6 +3377,12 @@ QGISEXTERN int listStyles( const QString& uri, QVector<QString> &ids, QVector<Q
33773377
.arg( QgsPostgresConn::quotedValue( f_geometry_column ) );
33783378

33793379
PGresult* result = conn->PQexec( selectRelatedQuery );
3380+
if ( PQresultStatus( result ) != PGRES_TUPLES_OK )
3381+
{
3382+
QgsMessageLog::logMessage( QObject::tr( "Error executing query: %1" ).arg( selectRelatedQuery ) );
3383+
errCause = QObject::tr( "Error executing the select query for related styles. The query was logged" );
3384+
return -1;
3385+
}
33803386
int numberOfRelatedStyles = PQntuples( result );
33813387
for( int i=0; i<numberOfRelatedStyles; i++ )
33823388
{
@@ -3393,6 +3399,12 @@ QGISEXTERN int listStyles( const QString& uri, QVector<QString> &ids, QVector<Q
33933399
.arg( QgsPostgresConn::quotedValue( f_geometry_column ) );
33943400

33953401
result = conn->PQexec( selectOthersQuery );
3402+
if ( PQresultStatus( result ) != PGRES_TUPLES_OK )
3403+
{
3404+
QgsMessageLog::logMessage( QObject::tr( "Error executing query: %1" ).arg( selectOthersQuery ) );
3405+
errCause = QObject::tr( "Error executing the select query for unrelated styles. The query was logged" );
3406+
return -1;
3407+
}
33963408
for( int i=0; i<PQntuples( result ); i++ )
33973409
{
33983410
ids.push_front( QObject::tr( PQgetvalue( result, i, 0 ) ) );
@@ -3411,17 +3423,28 @@ QGISEXTERN QString getStyleById(const QString& uri, QString styleId, QString& er
34113423
QgsPostgresConn* conn = QgsPostgresConn::connectDb( dsUri.connectionInfo(), false );
34123424
if ( !conn )
34133425
{
3414-
errCause = QObject::tr( "Connection to database failed" );
3415-
return "";
3426+
errCause = QObject::tr( "Connection to database failed using username: %1" ).arg( dsUri.username() );
3427+
return QObject::tr( "" );
34163428
}
34173429

34183430
QString selectQmlQuery = QObject::tr( "SELECT styleQml FROM %1 WHERE id=%2")
34193431
.arg( styleTableName )
34203432
.arg( styleId );
34213433
PGresult* result = conn->PQexec( selectQmlQuery );
3434+
if ( PQresultStatus( result ) != PGRES_TUPLES_OK )
3435+
{
3436+
QgsMessageLog::logMessage( QObject::tr( "Error executing query: %1" ).arg( selectQmlQuery ) );
3437+
errCause = QObject::tr( "Error executing the select query. The query was logged" );
3438+
return QObject::tr( "" );
3439+
}
34223440
if( PQntuples( result ) == 1)
34233441
{
34243442
return PQgetvalue( result, 0, 0 );
34253443
}
3426-
return "";
3444+
else
3445+
{
3446+
errCause = QObject::tr( "Consistence error in table '%1'. Style id should be unique" ).arg( styleTableName );
3447+
return QObject::tr( "" );
3448+
}
3449+
34273450
}

0 commit comments

Comments
 (0)
Please sign in to comment.