Skip to content

Commit

Permalink
categories in paste style
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids authored and nyalldawson committed Sep 14, 2018
1 parent 2cabd48 commit e91a42d
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 28 deletions.
7 changes: 5 additions & 2 deletions python/core/auto_generated/qgsmaplayer.sip.in
Expand Up @@ -807,13 +807,15 @@ Retrieve a named style for this layer from a sqlite database.
:return: true if style was successfully loaded
%End

virtual bool importNamedStyle( QDomDocument &doc, QString &errorMsg /Out/ );
virtual bool importNamedStyle( QDomDocument &doc, QString &errorMsg /Out/,
QgsMapLayer::StyleCategories categories = QgsMapLayer::AllCategories );
%Docstring
Import the properties of this layer from a QDomDocument

:param doc: source QDomDocument
:param errorMsg: this QString will be initialized on error
during the execution of readSymbology
:param categories: the style categories to import

:return: true on success

Expand All @@ -827,7 +829,8 @@ Export the properties of this layer as named style in a QDomDocument

:param doc: the target QDomDocument
:param errorMsg: this QString will be initialized on error
during the execution of writeSymbology
:param categories: the style categories to export
during the execution of writeSymbology
%End


Expand Down
4 changes: 2 additions & 2 deletions src/app/qgisapp.cpp
Expand Up @@ -8872,7 +8872,7 @@ void QgisApp::copyStyle( QgsMapLayer *sourceLayer, QgsMapLayer::StyleCategories
}
}

void QgisApp::pasteStyle( QgsMapLayer *destinationLayer )
void QgisApp::pasteStyle( QgsMapLayer *destinationLayer, QgsMapLayer::StyleCategories categories )
{
QgsMapLayer *selectionLayer = destinationLayer ? destinationLayer : activeLayer();
if ( selectionLayer )
Expand All @@ -8898,7 +8898,7 @@ void QgisApp::pasteStyle( QgsMapLayer *destinationLayer )
return;
}

if ( !selectionLayer->importNamedStyle( doc, errorMsg ) )
if ( !selectionLayer->importNamedStyle( doc, errorMsg, categories ) )
{
messageBar()->pushMessage( tr( "Cannot paste style" ),
errorMsg,
Expand Down
10 changes: 5 additions & 5 deletions src/app/qgisapp.h
Expand Up @@ -831,18 +831,18 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow

/**
* Copy the style of a map layer
* \param sourceLayer The layer where the style will be taken from
(defaults to the active layer on the legend)
* \param sourceLayer The layer where the style will be taken from (defaults to the active layer on the legend)
* \param categories The style categories to copy
*/
void copyStyle( QgsMapLayer *sourceLayer = nullptr, QgsMapLayer::StyleCategories categories = QgsMapLayer::AllCategories );
//! pastes style on the clipboard to the active layer

/**
\param destinationLayer The layer that the clipboard will be pasted to
(defaults to the active layer on the legend)
* \param destinationLayer The layer that the clipboard will be pasted to (defaults to the active layer on the legend)
* \param categories The style categories to copy
*/
void pasteStyle( QgsMapLayer *destinationLayer = nullptr );
void pasteStyle( QgsMapLayer *destinationLayer = nullptr,
QgsMapLayer::StyleCategories categories = QgsMapLayer::AllCategories );
//! copies group or layer on the clipboard
void copyLayer();
//! pastes group or layer from the clipboard to layer tree
Expand Down
24 changes: 22 additions & 2 deletions src/app/qgsapplayertreeviewmenuprovider.cpp
Expand Up @@ -318,7 +318,7 @@ QMenu *QgsAppLayerTreeViewMenuProvider::createContextMenu()
QgisApp *app = QgisApp::instance();
if ( layer->type() == QgsMapLayer::VectorLayer )
{
QMenu *copyStyleMenu = menuStyleManager->addMenu( tr( "Copy Style" ) );
QMenu *copyStyleMenu = menuStyleManager->addMenu( tr( "Copy Style" ) );
copyStyleMenu->setToolTipsVisible( true );
QList<QgsMapLayer::StyleCategory> categories = qgsEnumMap<QgsMapLayer::StyleCategory>().keys();
categories.move( categories.indexOf( QgsMapLayer::AllCategories ), 0 ); // move All categories to top
Expand All @@ -340,7 +340,27 @@ QMenu *QgsAppLayerTreeViewMenuProvider::createContextMenu()

if ( app->clipboard()->hasFormat( QGSCLIPBOARD_STYLE_MIME ) )
{
menuStyleManager->addAction( tr( "Paste Style" ), app, SLOT( pasteStyle() ) );
if ( layer->type() == QgsMapLayer::VectorLayer )
{
QMenu *copyStyleMenu = menuStyleManager->addMenu( tr( "Paste Style" ) );
copyStyleMenu->setToolTipsVisible( true );
QList<QgsMapLayer::StyleCategory> categories = qgsEnumMap<QgsMapLayer::StyleCategory>().keys();
categories.move( categories.indexOf( QgsMapLayer::AllCategories ), 0 ); // move All categories to top
for ( QgsMapLayer::StyleCategory category : categories )
{
QgsMapLayer::ReadableStyleCategory readableCategory = QgsMapLayer::readableStyleCategory( category );
QAction *copyAction = new QAction( readableCategory.icon(), readableCategory.name() );
copyAction->setToolTip( readableCategory.toolTip() );
connect( copyAction, &QAction::triggered, this, [ = ]() {app->pasteStyle( layer, category );} );
copyStyleMenu->addAction( copyAction );
if ( category == QgsMapLayer::AllCategories )
copyStyleMenu->addSeparator();
}
}
else
{
menuStyleManager->addAction( tr( "Paste Style" ), app, SLOT( pasteStyle() ) );
}
}

menuStyleManager->addSeparator();
Expand Down
18 changes: 2 additions & 16 deletions src/core/qgsmaplayer.cpp
Expand Up @@ -1080,7 +1080,7 @@ bool QgsMapLayer::importNamedMetadata( QDomDocument &document, QString &errorMes
return mMetadata.readMetadataXml( myRoot );
}

bool QgsMapLayer::importNamedStyle( QDomDocument &myDocument, QString &myErrorMessage )
bool QgsMapLayer::importNamedStyle( QDomDocument &myDocument, QString &myErrorMessage, QgsMapLayer::StyleCategories categories )
{
QDomElement myRoot = myDocument.firstChildElement( QStringLiteral( "qgis" ) );
if ( myRoot.isNull() )
Expand Down Expand Up @@ -1111,22 +1111,8 @@ bool QgsMapLayer::importNamedStyle( QDomDocument &myDocument, QString &myErrorMe
}
}

// use scale dependent visibility flag
setScaleBasedVisibility( myRoot.attribute( QStringLiteral( "hasScaleBasedVisibilityFlag" ) ).toInt() == 1 );
if ( myRoot.hasAttribute( QStringLiteral( "minimumScale" ) ) )
{
//older scale element, when min/max were reversed
setMaximumScale( myRoot.attribute( QStringLiteral( "minimumScale" ) ).toDouble() );
setMinimumScale( myRoot.attribute( QStringLiteral( "maximumScale" ) ).toDouble() );
}
else
{
setMaximumScale( myRoot.attribute( QStringLiteral( "maxScale" ) ).toDouble() );
setMinimumScale( myRoot.attribute( QStringLiteral( "minScale" ) ).toDouble() );
}

QgsReadWriteContext context = QgsReadWriteContext();
return readSymbology( myRoot, myErrorMessage, context ); // TODO: support relative paths in QML?
return readSymbology( myRoot, myErrorMessage, context, categories ); // TODO: support relative paths in QML?
}

void QgsMapLayer::exportNamedMetadata( QDomDocument &doc, QString &errorMsg ) const
Expand Down
5 changes: 4 additions & 1 deletion src/core/qgsmaplayer.h
Expand Up @@ -781,15 +781,18 @@ class CORE_EXPORT QgsMapLayer : public QObject
* \param doc source QDomDocument
* \param errorMsg this QString will be initialized on error
* during the execution of readSymbology
* \param categories the style categories to import
* \returns true on success
* \since QGIS 2.8
*/
virtual bool importNamedStyle( QDomDocument &doc, QString &errorMsg SIP_OUT );
virtual bool importNamedStyle( QDomDocument &doc, QString &errorMsg SIP_OUT,
QgsMapLayer::StyleCategories categories = QgsMapLayer::AllCategories );

/**
* Export the properties of this layer as named style in a QDomDocument
* \param doc the target QDomDocument
* \param errorMsg this QString will be initialized on error
* \param categories the style categories to export
* during the execution of writeSymbology
*/
virtual void exportNamedStyle( QDomDocument &doc, QString &errorMsg SIP_OUT, QgsReadWriteContext &context,
Expand Down

0 comments on commit e91a42d

Please sign in to comment.