Skip to content
Permalink
Browse files

Added controls on style table consistency and named style validity

  • Loading branch information
Emilio Loi
Emilio Loi committed Apr 16, 2013
1 parent fcb27d2 commit ef2d5320c820a98d1724e579eb1be89aef36f9c7
@@ -31,12 +31,17 @@ QgsLoadStyleFromDBDialog::QgsLoadStyleFromDBDialog( QWidget *parent )

connect(mRelatedTable, SIGNAL( cellClicked( int,int ) ), this, SLOT( cellSelectedRelatedTable( int ) ) );
connect(mOthersTable, SIGNAL( cellClicked( int,int ) ), this, SLOT( cellSelectedOthersTable( int ) ) );
connect(mCancelButton, SIGNAL( clicked() ), this, SLOT( cancelButtonClicked() ) );
connect(mLoadButton, SIGNAL( clicked() ), this, SLOT( loadButtonClicked() ) );
connect(mRelatedTable, SIGNAL( doubleClicked( QModelIndex ) ),
this, SLOT( accept() ) );
connect(mOthersTable, SIGNAL( doubleClicked( QModelIndex ) ),
this, SLOT( accept() ) );
connect(mCancelButton, SIGNAL( clicked() ), this, SLOT( reject() ) );
connect(mLoadButton, SIGNAL( clicked() ), this, SLOT( accept() ) );

setTabOrder( mRelatedTable, mOthersTable );
setTabOrder( mOthersTable, mCancelButton );
setTabOrder( mCancelButton, mLoadButton );

}

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

@@ -90,14 +95,3 @@ void QgsLoadStyleFromDBDialog::cellSelectedOthersTable( int r )
mLoadButton->setEnabled( true );
mSelectedStyleId = mIds.value( r + mSectionLimit );
}

void QgsLoadStyleFromDBDialog::cancelButtonClicked()
{
mSelectedStyleId = tr( "" ) ;
this->close();
}

void QgsLoadStyleFromDBDialog::loadButtonClicked()
{
this->close();
}
@@ -32,8 +32,6 @@ class QgsLoadStyleFromDBDialog: public QDialog, private Ui::QgsLoadStyleFromDBDi
public slots:
void cellSelectedRelatedTable( int r );
void cellSelectedOthersTable( int r );
void cancelButtonClicked();
void loadButtonClicked();

private:

@@ -752,27 +752,36 @@ void QgsVectorLayerProperties::showListOfStylesFromDatabase()
QString errorMsg;
QVector<QString> ids, names, descriptions;



int sectionLimit = layer->listStylesInDatabase(ids, names, descriptions, errorMsg);

if( !errorMsg.isNull() )
{
QMessageBox::warning(this, tr( "Error!" ), tr( "Retrieving" ) );
QMessageBox::warning( this, tr( "Error occured retrievning styles from database "), errorMsg );
return;
}

QgsLoadStyleFromDBDialog dialog;
dialog.initializeLists(ids, names, descriptions, sectionLimit);
dialog.exec();

QString selectedStyleId = dialog.getSelectedStyleId();
if( selectedStyleId.compare( tr( "" ) ) )
if( dialog.exec() == QDialog::Accepted )
{
QString selectedStyleId = dialog.getSelectedStyleId();

QString qmlStyle = layer->getStyleFromDatabase( selectedStyleId, errorMsg );
if( !qmlStyle.isNull() ){
layer->applyNamedStyle( qmlStyle, errorMsg);
if( !errorMsg.isNull() )
{
QMessageBox::warning( this, tr( "Error occured retrievning styles from database "), errorMsg );
return;
}
if( layer->applyNamedStyle( qmlStyle, errorMsg) )
{
reset();
}
else
{
QMessageBox::warning( this, tr( "Error occured retrievning styles from database "),
tr( "The style retriev is not a valid named style. Error message: %1" )
.arg( errorMsg ) );
}

}

@@ -3360,7 +3360,7 @@ QGISEXTERN int listStyles( const QString& uri, QVector<QString> &ids, QVector<Q
QgsPostgresConn* conn = QgsPostgresConn::connectDb( dsUri.connectionInfo(), false );
if ( !conn )
{
errCause = QObject::tr( "Connection to database failed" );
errCause = QObject::tr( "Connection to database failed using username: %1" ).arg( dsUri.username() );
return -1;
}

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

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

result = conn->PQexec( selectOthersQuery );
if ( PQresultStatus( result ) != PGRES_TUPLES_OK )
{
QgsMessageLog::logMessage( QObject::tr( "Error executing query: %1" ).arg( selectOthersQuery ) );
errCause = QObject::tr( "Error executing the select query for unrelated styles. The query was logged" );
return -1;
}
for( int i=0; i<PQntuples( result ); i++ )
{
ids.push_front( QObject::tr( PQgetvalue( result, i, 0 ) ) );
@@ -3411,17 +3423,28 @@ QGISEXTERN QString getStyleById(const QString& uri, QString styleId, QString& er
QgsPostgresConn* conn = QgsPostgresConn::connectDb( dsUri.connectionInfo(), false );
if ( !conn )
{
errCause = QObject::tr( "Connection to database failed" );
return "";
errCause = QObject::tr( "Connection to database failed using username: %1" ).arg( dsUri.username() );
return QObject::tr( "" );
}

QString selectQmlQuery = QObject::tr( "SELECT styleQml FROM %1 WHERE id=%2")
.arg( styleTableName )
.arg( styleId );
PGresult* result = conn->PQexec( selectQmlQuery );
if ( PQresultStatus( result ) != PGRES_TUPLES_OK )
{
QgsMessageLog::logMessage( QObject::tr( "Error executing query: %1" ).arg( selectQmlQuery ) );
errCause = QObject::tr( "Error executing the select query. The query was logged" );
return QObject::tr( "" );
}
if( PQntuples( result ) == 1)
{
return PQgetvalue( result, 0, 0 );
}
return "";
else
{
errCause = QObject::tr( "Consistence error in table '%1'. Style id should be unique" ).arg( styleTableName );
return QObject::tr( "" );
}

}

0 comments on commit ef2d532

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