Skip to content
Permalink
Browse files

[styledock] Add saving and loading default styles

  • Loading branch information
NathanW2 committed Jun 3, 2016
1 parent bded2b5 commit 3d941e3ac991a7c5c4f154055eb5b86138760041
Showing with 117 additions and 0 deletions.
  1. +115 −0 src/gui/qgsmaplayerstylemanagerwidget.cpp
  2. +2 −0 src/gui/qgsmaplayerstylemanagerwidget.h
@@ -16,13 +16,18 @@
#include <QVBoxLayout>
#include <QToolBar>
#include <QInputDialog>
#include <QMessageBox>

#include "qgsmaplayerstylemanagerwidget.h"
#include "qgslogger.h"
#include "qgsmaplayer.h"
#include "qgsmapcanvas.h"
#include "qgsmapstylepanel.h"
#include "qgsmaplayerstylemanager.h"
#include "qgsvectordataprovider.h"
#include "qgsrasterdataprovider.h"
#include "qgsvectorlayer.h"
#include "qgsrasterlayer.h"


QgsMapLayerStyleManagerWidget::QgsMapLayerStyleManagerWidget( QgsMapLayer* layer, QgsMapCanvas *canvas, QWidget *parent )
@@ -39,6 +44,10 @@ QgsMapLayerStyleManagerWidget::QgsMapLayerStyleManagerWidget( QgsMapLayer* layer
connect( addAction, SIGNAL( triggered() ), this, SLOT( addStyle() ) );
QAction* removeAction = toolbar->addAction( tr( "Remove Current" ) );
connect( removeAction, SIGNAL( triggered() ), this, SLOT( removeStyle() ) );
QAction* saveAsDefaultAction = toolbar->addAction( tr( "Save as default" ) );
connect( saveAsDefaultAction, SIGNAL( triggered() ), this, SLOT( saveAsDefault() ) );
QAction* loadDefaultAction = toolbar->addAction( tr( "Load default" ) );
connect( loadDefaultAction, SIGNAL( triggered() ), this, SLOT( loadDefault() ) );

connect( canvas, SIGNAL( mapCanvasRefreshed() ), this, SLOT( updateCurrent() ) );

@@ -157,3 +166,109 @@ void QgsMapLayerStyleManagerWidget::removeStyle()
}

}

void QgsMapLayerStyleManagerWidget::saveAsDefault()
{
QString errorMsg;

if ( QgsVectorLayer* layer = qobject_cast<QgsVectorLayer*>( mLayer ) )
{
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;
case 2:
layer->saveStyleToDatabase( "", "", true, "", errorMsg );
if ( errorMsg.isNull() )
{
return;
}
break;
default:
break;
}
}
}

bool defaultSavedFlag = false;
errorMsg = mLayer->saveDefaultStyle( defaultSavedFlag );
if ( !defaultSavedFlag )
{
QMessageBox::warning( this, tr( "Default Style" ), errorMsg );
}

}

void QgsMapLayerStyleManagerWidget::loadDefault()
{
QString msg;
bool defaultLoadedFlag = false;

if ( QgsVectorLayer* layer = qobject_cast<QgsVectorLayer*>( mLayer ) )
{
if ( layer->dataProvider()->isSaveAndLoadStyleToDBSupported() )
{
QMessageBox askToUser;
askToUser.setText( tr( "Load default style from: " ) );
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;
case 2:
msg = layer->loadNamedStyle( mLayer->styleURI(), defaultLoadedFlag );
if ( !defaultLoadedFlag )
{
//something went wrong - let them know why
QMessageBox::information( this, tr( "Default Style" ), msg );
}
if ( msg.compare( tr( "Loaded from Provider" ) ) )
{
QMessageBox::information( this, tr( "Default Style" ),
tr( "No default style was found for this layer" ) );
}
return;
default:
break;
}
}
}

QString myMessage;
if ( QgsVectorLayer* layer = qobject_cast<QgsVectorLayer*>( mLayer ) )
{
myMessage = layer->loadNamedStyle( mLayer->styleURI(), defaultLoadedFlag, true );
}
if ( QgsRasterLayer* layer = qobject_cast<QgsRasterLayer*>( mLayer ) )
{
myMessage = layer->loadNamedStyle( mLayer->styleURI(), defaultLoadedFlag );
}

// QString myMessage = layer->loadDefaultStyle( defaultLoadedFlag );
//reset if the default style was loaded ok only


if ( !defaultLoadedFlag )
{
//something went wrong - let them know why
QMessageBox::information( this, tr( "Default Style" ), myMessage );
}
else
{
emit widgetChanged();
}

}
@@ -53,6 +53,8 @@ class GUI_EXPORT QgsMapLayerStyleManagerWidget : public QgsMapStylePanel
void styleRenamed( QString oldname, QString newname );
void addStyle();
void removeStyle();
void saveAsDefault();
void loadDefault();

private:
QStandardItemModel* mModel;

0 comments on commit 3d941e3

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