Skip to content

Commit

Permalink
Now "Save as Default" button ask where to save the style
Browse files Browse the repository at this point in the history
  • Loading branch information
Emilio Loi committed Apr 17, 2013
1 parent 8645094 commit 37e3035
Show file tree
Hide file tree
Showing 10 changed files with 518 additions and 146 deletions.
7 changes: 5 additions & 2 deletions src/app/qgsloadstylefromdbdialog.cpp
Expand Up @@ -24,10 +24,12 @@ QgsLoadStyleFromDBDialog::QgsLoadStyleFromDBDialog( QWidget *parent )
mRelatedTable->setEditTriggers( QTableWidget::NoEditTriggers );
mRelatedTable->horizontalHeader()->setStretchLastSection( true );
mRelatedTable->setSelectionBehavior( QTableWidget::SelectRows );
mRelatedTable->verticalHeader()->setVisible( false );

mOthersTable->setEditTriggers( QTableWidget::NoEditTriggers );
mOthersTable->horizontalHeader()->setStretchLastSection( true );
mOthersTable->setSelectionBehavior( QTableWidget::SelectRows );
mOthersTable->verticalHeader()->setVisible( false );

connect(mRelatedTable, SIGNAL( cellClicked( int,int ) ), this, SLOT( cellSelectedRelatedTable( int ) ) );
connect(mOthersTable, SIGNAL( cellClicked( int,int ) ), this, SLOT( cellSelectedOthersTable( int ) ) );
Expand Down Expand Up @@ -74,8 +76,9 @@ void QgsLoadStyleFromDBDialog::initializeLists( QVector<QString> ids, QVector<QS
}
for( int i=sectionLimit; i<ids.count(); i++ )
{
mOthersTable->setItem(i, 0, new QTableWidgetItem( names.value( i, "" ) ) );
mOthersTable->setItem(i, 1, new QTableWidgetItem( descriptions.value( i, "" ) ) );
int j = i-sectionLimit;
mOthersTable->setItem(j, 0, new QTableWidgetItem( names.value( i, "" ) ) );
mOthersTable->setItem(j, 1, new QTableWidgetItem( descriptions.value( i, "" ) ) );
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/app/qgssavestyletodbdialog.cpp
Expand Up @@ -13,6 +13,7 @@
#include <QSettings>
#include <QDomDocument>
#include <QMessageBox>
#include <QDateTime>

QgsSaveStyleToDbDialog::QgsSaveStyleToDbDialog( QWidget *parent ) :
QDialog( parent )
Expand Down Expand Up @@ -42,6 +43,15 @@ QString QgsSaveStyleToDbDialog::getUIFileContent()
return mUIFileContent;
}

void QgsSaveStyleToDbDialog::accept()
{
if( getName().isEmpty() ){
QMessageBox::information( this, tr( "Save style in database" ), tr( "A name is mandatory" ) );
return;
}
QDialog::accept();
}

void QgsSaveStyleToDbDialog::on_mFilePickButton_clicked()
{
QSettings myQSettings; // where we keep last used filter in persistent state
Expand Down
1 change: 1 addition & 0 deletions src/app/qgssavestyletodbdialog.h
Expand Up @@ -29,6 +29,7 @@ public slots:
QString getDescription();
bool isDefault();
void on_mFilePickButton_clicked();
void accept();
};

#endif // QGSSAVESTYLETODBDIALOG_H
46 changes: 34 additions & 12 deletions src/app/qgsvectorlayerproperties.cpp
Expand Up @@ -129,7 +129,7 @@ QgsVectorLayerProperties::QgsVectorLayerProperties(
mSaveAsMenu->addAction( tr( "QGIS Layer Style File" ) );
mSaveAsMenu->addAction( tr( "SLD File" ) );

//Only if the provider support loading & saving style to db add new choices
//Only if the provider support loading & saving styles to db add new choices
if( layer->dataProvider()->isSaveAndLoadStyleToDBSupported() )
{
//for loading
Expand Down Expand Up @@ -557,18 +557,40 @@ void QgsVectorLayerProperties::on_pbnLoadDefaultStyle_clicked()

void QgsVectorLayerProperties::on_pbnSaveDefaultStyle_clicked()
{
apply(); // make sure the qml to save is uptodate
apply();
QString errorMsg;
if( layer->dataProvider()->isSaveAndLoadStyleToDBSupported() )
{
QMessageBox askToUser;
askToUser.setText( tr( "Save default style to: " ) );
askToUser.setIcon( QMessageBox::Question );
askToUser.addButton( tr( "Cancel" ), QMessageBox::RejectRole);
askToUser.addButton( tr( "Local database" ), QMessageBox::NoRole );
askToUser.addButton( tr( "Datasource database" ), QMessageBox::YesRole );

switch ( askToUser.exec() )
{
case (0):
return;
break;
case (2):
layer->saveStyleToDatabase("", "", true, "", errorMsg );
if( errorMsg.isNull() )
{
return;
}
break;
default:
break;
}
}

// a flag passed by reference
bool defaultSavedFlag = false;
// after calling this the above flag will be set true for success
// or false if the save operation failed
QString myMessage = layer->saveDefaultStyle( defaultSavedFlag );
if ( !defaultSavedFlag )
{
//only raise the message if something went wrong
QMessageBox::information( this, tr( "Default Style" ), myMessage );
}
bool defaultSavedFlag = false;
errorMsg = layer->saveDefaultStyle( defaultSavedFlag );
if ( !defaultSavedFlag )
{
QMessageBox::warning( this, tr( "Default Style" ), errorMsg );
}
}


Expand Down
3 changes: 2 additions & 1 deletion src/core/qgsmaplayer.cpp
Expand Up @@ -698,6 +698,7 @@ QString QgsMapLayer::styleURI( )

QString QgsMapLayer::loadDefaultStyle( bool & theResultFlag )
{
QString ciao = styleURI();
return loadNamedStyle( styleURI(), theResultFlag );
}

Expand Down Expand Up @@ -881,7 +882,7 @@ void QgsMapLayer::exportNamedStyle(QDomDocument &doc, QString &errorMsg)

QString QgsMapLayer::saveDefaultStyle( bool & theResultFlag )
{
return saveNamedStyle( styleURI(), theResultFlag );
return saveNamedStyle( styleURI(), theResultFlag );
}

QString QgsMapLayer::saveNamedStyle( const QString theURI, bool & theResultFlag )
Expand Down
9 changes: 3 additions & 6 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -1711,10 +1711,7 @@ bool QgsVectorLayer::writeXml( QDomNode & layer_node,
// renderer specific settings
QString errorMsg;
return writeSymbology( layer_node, document, errorMsg );
} // bool QgsVectorLayer::writeXml



}

bool QgsVectorLayer::readSymbology( const QDomNode& node, QString& errorMessage )
{
Expand Down Expand Up @@ -3774,6 +3771,7 @@ QString QgsVectorLayer::getStyleFromDatabase(QString styleId, QString &msgError)
return getStyleByIdMethod( mDataSource, styleId, msgError );
}


void QgsVectorLayer::saveStyleToDatabase(QString name, QString description,
bool useAsDefault, QString uiFileContent, QString &msgError){

Expand Down Expand Up @@ -3819,7 +3817,6 @@ void QgsVectorLayer::saveStyleToDatabase(QString name, QString description,
QString QgsVectorLayer::loadNamedStyle( const QString theURI, bool &theResultFlag )
{
QgsDataSourceURI dsUri( theURI );

if ( !dsUri.database().isEmpty() )
{
QgsProviderRegistry * pReg = QgsProviderRegistry::instance();
Expand All @@ -3831,7 +3828,7 @@ QString QgsVectorLayer::loadNamedStyle( const QString theURI, bool &theResultFla
{
QString qml, errorMsg;
qml = loadStyleExternalMethod( mDataSource, errorMsg );
if( qml.compare( tr( "" ) ) )
if( !qml.isEmpty() )
{
theResultFlag = this->applyNamedStyle( qml, errorMsg );
}
Expand Down
25 changes: 14 additions & 11 deletions src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -3229,7 +3229,7 @@ QGISEXTERN bool saveStyle( const QString& uri, const QString& qmlStyle, const QS
const QString& uiFileContent, bool useAsDefault, QString& errCause )
{
QgsDataSourceURI dsUri( uri );
QString f_table_catalog, f_table_schema, f_table_name, f_geometry_column;
QString f_table_catalog, f_table_schema, f_table_name, f_geometry_column, owner, isdef, name, desc;
QString styleTableName = QObject::tr( "layer_styles" );
QgsPostgresResult res;

Expand Down Expand Up @@ -3261,8 +3261,11 @@ QGISEXTERN bool saveStyle( const QString& uri, const QString& qmlStyle, const QS
f_table_schema = dsUri.schema();
f_table_name = dsUri.table();
f_geometry_column = dsUri.geometryColumn();
QString owner = dsUri.username();
QString isdef = (useAsDefault) ? QObject::tr( "true" ) : QObject::tr( "false" );
owner = dsUri.username();
isdef = (useAsDefault) ? QObject::tr( "true" ) : QObject::tr( "false" );
name = ( styleName.isEmpty() ) ? dsUri.table() : styleName;
desc = ( styleDescription.isEmpty() ) ? QDateTime::currentDateTime().toString() : styleDescription;

QString uiFileColumn( "" );
QString uiFileValue( "" );
if( !uiFileContent.isEmpty() )
Expand All @@ -3282,11 +3285,11 @@ QGISEXTERN bool saveStyle( const QString& uri, const QString& qmlStyle, const QS
.arg( QgsPostgresConn::quotedValue( f_table_schema ) )
.arg( QgsPostgresConn::quotedValue( f_table_name ) )
.arg( QgsPostgresConn::quotedValue( f_geometry_column ) )
.arg( QgsPostgresConn::quotedValue( styleName ) )
.arg( QgsPostgresConn::quotedValue( name ) )
.arg( QgsPostgresConn::quotedValue( qmlStyle ) )
.arg( QgsPostgresConn::quotedValue( sldStyle ) )
.arg( isdef )
.arg( QgsPostgresConn::quotedValue( styleDescription ) )
.arg( QgsPostgresConn::quotedValue( desc ) )
.arg( QgsPostgresConn::quotedValue( owner ) )
.arg( uiFileColumn )
.arg( uiFileValue );
Expand Down Expand Up @@ -3386,9 +3389,9 @@ QGISEXTERN int listStyles( const QString& uri, QVector<QString> &ids, QVector<Q
int numberOfRelatedStyles = PQntuples( result );
for( int i=0; i<numberOfRelatedStyles; i++ )
{
ids.push_front( QObject::tr( PQgetvalue( result, i, 0 ) ) );
names.push_front( QObject::tr( PQgetvalue( result, i, 1 ) ) );
descriptions.push_front( QObject::tr( PQgetvalue( result, i, 2 ) ) );
ids.append( QObject::tr( PQgetvalue( result, i, 0 ) ) );
names.append( QObject::tr( PQgetvalue( result, i, 1 ) ) );
descriptions.append( QObject::tr( PQgetvalue( result, i, 2 ) ) );
}

QString selectOthersQuery = QObject::tr( "SELECT id, styleName, description FROM %1 WHERE NOT(f_table_catalog=%2 AND f_table_schema=%3 AND f_table_name=%4 AND f_geometry_column=%5) ORDER BY update_time DESC;")
Expand All @@ -3407,9 +3410,9 @@ QGISEXTERN int listStyles( const QString& uri, QVector<QString> &ids, QVector<Q
}
for( int i=0; i<PQntuples( result ); i++ )
{
ids.push_front( QObject::tr( PQgetvalue( result, i, 0 ) ) );
names.push_front( QObject::tr( PQgetvalue( result, i, 1 ) ) );
descriptions.push_front( QObject::tr( PQgetvalue( result, i, 2 ) ) );
ids.append( QObject::tr( PQgetvalue( result, i, 0 ) ) );
names.append( QObject::tr( PQgetvalue( result, i, 1 ) ) );
descriptions.append( QObject::tr( PQgetvalue( result, i, 2 ) ) );
}

return numberOfRelatedStyles;
Expand Down
108 changes: 100 additions & 8 deletions tests/testdata/lines.qml
@@ -1,5 +1,5 @@
<!DOCTYPE qgis PUBLIC 'http://mrcc.com/qgis.dtd' 'SYSTEM'>
<qgis version="1.9.90-Alpha" minimumScale="1" maximumScale="1e+08" minLabelScale="1" maxLabelScale="1e+08" hasScaleBasedVisibilityFlag="0" scaleBasedLabelVisibilityFlag="0">
<qgis version="1.9.0-Master" minimumScale="1" maximumScale="1e+08" minLabelScale="1" maxLabelScale="1e+08" hasScaleBasedVisibilityFlag="0" scaleBasedLabelVisibilityFlag="0">
<transparencyLevelInt>255</transparencyLevelInt>
<classificationattribute>Name</classificationattribute>
<uniquevalue>
Expand All @@ -11,9 +11,9 @@
<pointsymbol>hard:circle</pointsymbol>
<pointsize>11</pointsize>
<pointsizeunits>pixels</pointsizeunits>
<rotationclassificationfieldname></rotationclassificationfieldname>
<scaleclassificationfieldname></scaleclassificationfieldname>
<symbolfieldname></symbolfieldname>
<rotationclassificationfieldname null="1"></rotationclassificationfieldname>
<scaleclassificationfieldname null="1"></scaleclassificationfieldname>
<symbolfieldname null="1"></symbolfieldname>
<outlinecolor red="154" blue="116" green="139"/>
<outlinestyle>DashDotDotLine</outlinestyle>
<outlinewidth>5</outlinewidth>
Expand All @@ -28,9 +28,9 @@
<pointsymbol>hard:circle</pointsymbol>
<pointsize>11</pointsize>
<pointsizeunits>pixels</pointsizeunits>
<rotationclassificationfieldname></rotationclassificationfieldname>
<scaleclassificationfieldname></scaleclassificationfieldname>
<symbolfieldname></symbolfieldname>
<rotationclassificationfieldname null="1"></rotationclassificationfieldname>
<scaleclassificationfieldname null="1"></scaleclassificationfieldname>
<symbolfieldname null="1"></symbolfieldname>
<outlinecolor red="94" blue="55" green="89"/>
<outlinestyle>SolidLine</outlinestyle>
<outlinewidth>7</outlinewidth>
Expand All @@ -39,7 +39,96 @@
<texturepath></texturepath>
</symbol>
</uniquevalue>
<customproperties/>
<customproperties>
<property key="labeling" value="pal"/>
<property key="labeling/addDirectionSymbol" value="false"/>
<property key="labeling/angleOffset" value="0"/>
<property key="labeling/bufferColorB" value="255"/>
<property key="labeling/bufferColorG" value="255"/>
<property key="labeling/bufferColorR" value="255"/>
<property key="labeling/bufferJoinStyle" value="64"/>
<property key="labeling/bufferNoFill" value="false"/>
<property key="labeling/bufferSize" value="1"/>
<property key="labeling/bufferSizeInMapUnits" value="false"/>
<property key="labeling/bufferTransp" value="0"/>
<property key="labeling/centroidWhole" value="false"/>
<property key="labeling/dataDefined/AlwaysShow" value=""/>
<property key="labeling/dataDefined/Bold" value=""/>
<property key="labeling/dataDefined/BufferColor" value=""/>
<property key="labeling/dataDefined/BufferSize" value=""/>
<property key="labeling/dataDefined/BufferTransp" value=""/>
<property key="labeling/dataDefined/Color" value=""/>
<property key="labeling/dataDefined/Family" value=""/>
<property key="labeling/dataDefined/FontTransp" value=""/>
<property key="labeling/dataDefined/Hali" value=""/>
<property key="labeling/dataDefined/Italic" value=""/>
<property key="labeling/dataDefined/LabelDistance" value=""/>
<property key="labeling/dataDefined/MaxScale" value=""/>
<property key="labeling/dataDefined/MinScale" value=""/>
<property key="labeling/dataDefined/PositionX" value=""/>
<property key="labeling/dataDefined/PositionY" value=""/>
<property key="labeling/dataDefined/Rotation" value=""/>
<property key="labeling/dataDefined/Show" value=""/>
<property key="labeling/dataDefined/Size" value=""/>
<property key="labeling/dataDefined/Strikeout" value=""/>
<property key="labeling/dataDefined/Underline" value=""/>
<property key="labeling/dataDefined/Vali" value=""/>
<property key="labeling/decimals" value="0"/>
<property key="labeling/displayAll" value="false"/>
<property key="labeling/dist" value="0"/>
<property key="labeling/distInMapUnits" value="false"/>
<property key="labeling/enabled" value="false"/>
<property key="labeling/fieldName" value=""/>
<property key="labeling/fontCapitals" value="0"/>
<property key="labeling/fontFamily" value="Ubuntu"/>
<property key="labeling/fontItalic" value="false"/>
<property key="labeling/fontLetterSpacing" value="0"/>
<property key="labeling/fontLimitPixelSize" value="false"/>
<property key="labeling/fontMaxPixelSize" value="10000"/>
<property key="labeling/fontMinPixelSize" value="3"/>
<property key="labeling/fontSize" value="11"/>
<property key="labeling/fontSizeInMapUnits" value="false"/>
<property key="labeling/fontStrikeout" value="false"/>
<property key="labeling/fontUnderline" value="false"/>
<property key="labeling/fontWeight" value="50"/>
<property key="labeling/fontWordSpacing" value="0"/>
<property key="labeling/formatNumbers" value="false"/>
<property key="labeling/isExpression" value="false"/>
<property key="labeling/labelOffsetInMapUnits" value="true"/>
<property key="labeling/labelPerPart" value="false"/>
<property key="labeling/leftDirectionSymbol" value="&lt;"/>
<property key="labeling/limitNumLabels" value="false"/>
<property key="labeling/maxCurvedCharAngleIn" value="20"/>
<property key="labeling/maxCurvedCharAngleOut" value="-20"/>
<property key="labeling/maxNumLabels" value="2000"/>
<property key="labeling/mergeLines" value="false"/>
<property key="labeling/minFeatureSize" value="0"/>
<property key="labeling/multilineAlign" value="0"/>
<property key="labeling/multilineHeight" value="1"/>
<property key="labeling/namedStyle" value="Regular"/>
<property key="labeling/obstacle" value="true"/>
<property key="labeling/placeDirectionSymbol" value="0"/>
<property key="labeling/placement" value="2"/>
<property key="labeling/placementFlags" value="10"/>
<property key="labeling/plussign" value="true"/>
<property key="labeling/preserveRotation" value="true"/>
<property key="labeling/previewBkgrdColor" value="#ffffff"/>
<property key="labeling/priority" value="5"/>
<property key="labeling/reverseDirectionSymbol" value="false"/>
<property key="labeling/rightDirectionSymbol" value=">"/>
<property key="labeling/scaleMax" value="0"/>
<property key="labeling/scaleMin" value="0"/>
<property key="labeling/textColorB" value="0"/>
<property key="labeling/textColorG" value="0"/>
<property key="labeling/textColorR" value="0"/>
<property key="labeling/textTransp" value="0"/>
<property key="labeling/upsidedownLabels" value="0"/>
<property key="labeling/wrapChar" value=""/>
<property key="labeling/xOffset" value="0"/>
<property key="labeling/xQuadOffset" value="0"/>
<property key="labeling/yOffset" value="0"/>
<property key="labeling/yQuadOffset" value="0"/>
</customproperties>
<displayfield>Name</displayfield>
<label>0</label>
<labelattributes>
Expand Down Expand Up @@ -69,5 +158,8 @@
<editform></editform>
<editforminit></editforminit>
<annotationform></annotationform>
<editorlayout>generatedlayout</editorlayout>
<excludeAttributesWMS/>
<excludeAttributesWFS/>
<attributeactions/>
</qgis>

0 comments on commit 37e3035

Please sign in to comment.