Skip to content

Commit 1e1d53f

Browse files
committed
add source categories to disable unvailable ones for pasting style
1 parent 819aaee commit 1e1d53f

File tree

1 file changed

+33
-17
lines changed

1 file changed

+33
-17
lines changed

src/app/qgsapplayertreeviewmenuprovider.cpp

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
#include "qgssymbolselectordialog.h"
4040
#include "qgssinglesymbolrenderer.h"
4141
#include "qgsmaplayerstylecategoriesmodel.h"
42+
#include "qgsxmlutils.h"
43+
4244

4345

4446
QgsAppLayerTreeViewMenuProvider::QgsAppLayerTreeViewMenuProvider( QgsLayerTreeView *view, QgsMapCanvas *canvas )
@@ -349,24 +351,38 @@ QMenu *QgsAppLayerTreeViewMenuProvider::createContextMenu()
349351
{
350352
if ( layer->type() == QgsMapLayer::VectorLayer )
351353
{
352-
QMenu *pasteStyleMenu = menuStyleManager->addMenu( tr( "Paste Style" ) );
353-
pasteStyleMenu->setToolTipsVisible( true );
354-
355-
QgsMapLayerStyleCategoriesModel *model = new QgsMapLayerStyleCategoriesModel( pasteStyleMenu );
356-
model->setShowAllCategories( true );
357-
for ( int row = 0; row < model->rowCount(); ++row )
354+
QDomDocument doc( QStringLiteral( "qgis" ) );
355+
QString errorMsg;
356+
int errorLine, errorColumn;
357+
if ( doc.setContent( app->clipboard()->data( QGSCLIPBOARD_STYLE_MIME ), false, &errorMsg, &errorLine, &errorColumn ) )
358358
{
359-
QModelIndex index = model->index( row, 0 );
360-
QgsMapLayer::StyleCategory category = model->data( index, Qt::UserRole ).value<QgsMapLayer::StyleCategory>();
361-
QString name = model->data( index, Qt::DisplayRole ).toString();
362-
QString tooltip = model->data( index, Qt::ToolTipRole ).toString();
363-
QIcon icon = model->data( index, Qt::DecorationRole ).value<QIcon>();
364-
QAction *copyAction = new QAction( icon, name, pasteStyleMenu );
365-
copyAction->setToolTip( tooltip );
366-
connect( copyAction, &QAction::triggered, this, [ = ]() {app->pasteStyle( layer, category );} );
367-
pasteStyleMenu->addAction( copyAction );
368-
if ( category == QgsMapLayer::AllStyleCategories )
369-
pasteStyleMenu->addSeparator();
359+
QDomElement myRoot = doc.firstChildElement( QStringLiteral( "qgis" ) );
360+
if ( !myRoot.isNull() )
361+
{
362+
QMenu *pasteStyleMenu = menuStyleManager->addMenu( tr( "Paste Style" ) );
363+
pasteStyleMenu->setToolTipsVisible( true );
364+
365+
QgsMapLayer::StyleCategories sourceCategories = QgsXmlUtils::readFlagAttribute( myRoot, QStringLiteral( "styleCategories" ), QgsMapLayer::AllStyleCategories );
366+
367+
QgsMapLayerStyleCategoriesModel *model = new QgsMapLayerStyleCategoriesModel( pasteStyleMenu );
368+
model->setShowAllCategories( true );
369+
for ( int row = 0; row < model->rowCount(); ++row )
370+
{
371+
QModelIndex index = model->index( row, 0 );
372+
QgsMapLayer::StyleCategory category = model->data( index, Qt::UserRole ).value<QgsMapLayer::StyleCategory>();
373+
QString name = model->data( index, Qt::DisplayRole ).toString();
374+
QString tooltip = model->data( index, Qt::ToolTipRole ).toString();
375+
QIcon icon = model->data( index, Qt::DecorationRole ).value<QIcon>();
376+
QAction *pasteAction = new QAction( icon, name, pasteStyleMenu );
377+
pasteAction->setToolTip( tooltip );
378+
connect( pasteAction, &QAction::triggered, this, [ = ]() {app->pasteStyle( layer, category );} );
379+
pasteStyleMenu->addAction( pasteAction );
380+
if ( category == QgsMapLayer::AllStyleCategories )
381+
pasteStyleMenu->addSeparator();
382+
else
383+
pasteAction->setEnabled( sourceCategories.testFlag( category ) );
384+
}
385+
}
370386
}
371387
}
372388
else

0 commit comments

Comments
 (0)