-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqgsapplayertreeviewmenuprovider.cpp
620 lines (524 loc) · 24 KB
/
qgsapplayertreeviewmenuprovider.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
/***************************************************************************
qgsapplayertreeviewmenuprovider.cpp
---------------------
begin : May 2014
copyright : (C) 2014 by Martin Dobias
email : wonder dot sk at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsapplayertreeviewmenuprovider.h"
#include "qgisapp.h"
#include "qgsapplication.h"
#include "qgsclipboard.h"
#include "qgscolorwidgets.h"
#include "qgscolorschemeregistry.h"
#include "qgscolorswatchgrid.h"
#include "qgslayertree.h"
#include "qgslayertreemodel.h"
#include "qgslayertreemodellegendnode.h"
#include "qgslayertreeviewdefaultactions.h"
#include "qgsmapcanvas.h"
#include "qgsmaplayerstyleguiutils.h"
#include "qgsproject.h"
#include "qgsrasterlayer.h"
#include "qgsrenderer.h"
#include "qgssymbol.h"
#include "qgsstyle.h"
#include "qgsvectordataprovider.h"
#include "qgsvectorlayer.h"
#include "qgslayertreeregistrybridge.h"
#include "qgssymbolselectordialog.h"
#include "qgssinglesymbolrenderer.h"
QgsAppLayerTreeViewMenuProvider::QgsAppLayerTreeViewMenuProvider( QgsLayerTreeView *view, QgsMapCanvas *canvas )
: mView( view )
, mCanvas( canvas )
{
}
QMenu *QgsAppLayerTreeViewMenuProvider::createContextMenu()
{
QMenu *menu = new QMenu;
QgsLayerTreeViewDefaultActions *actions = mView->defaultActions();
QModelIndex idx = mView->currentIndex();
if ( !idx.isValid() )
{
// global menu
menu->addAction( actions->actionAddGroup( menu ) );
menu->addAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionExpandTree.svg" ) ), tr( "&Expand All" ), mView, SLOT( expandAll() ) );
menu->addAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionCollapseTree.svg" ) ), tr( "&Collapse All" ), mView, SLOT( collapseAll() ) );
// TODO: update drawing order
}
else if ( QgsLayerTreeNode *node = mView->layerTreeModel()->index2node( idx ) )
{
// layer or group selected
if ( QgsLayerTree::isGroup( node ) )
{
menu->addAction( actions->actionZoomToGroup( mCanvas, menu ) );
menu->addAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionRemoveLayer.svg" ) ), tr( "&Remove" ), QgisApp::instance(), SLOT( removeLayer() ) );
menu->addAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionSetCRS.png" ) ),
tr( "&Set Group CRS" ), QgisApp::instance(), SLOT( legendGroupSetCrs() ) );
menu->addAction( actions->actionRenameGroupOrLayer( menu ) );
menu->addAction( tr( "&Set Group WMS data" ), QgisApp::instance(), SLOT( legendGroupSetWmsData() ) );
menu->addAction( actions->actionMutuallyExclusiveGroup( menu ) );
menu->addAction( actions->actionCheckAndAllChildren( menu ) );
menu->addAction( actions->actionUncheckAndAllChildren( menu ) );
if ( mView->selectedNodes( true ).count() >= 2 )
menu->addAction( actions->actionGroupSelected( menu ) );
if ( QgisApp::instance()->clipboard()->hasFormat( QGSCLIPBOARD_STYLE_MIME ) )
{
menu->addAction( tr( "Paste Style" ), QgisApp::instance(), SLOT( applyStyleToGroup() ) );
}
menu->addAction( tr( "Save As Layer Definition File..." ), QgisApp::instance(), SLOT( saveAsLayerDefinition() ) );
menu->addAction( actions->actionAddGroup( menu ) );
}
else if ( QgsLayerTree::isLayer( node ) )
{
QgsMapLayer *layer = QgsLayerTree::toLayer( node )->layer();
QgsRasterLayer *rlayer = qobject_cast<QgsRasterLayer *>( layer );
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( layer );
if ( layer && layer->isSpatial() )
{
menu->addAction( actions->actionZoomToLayer( mCanvas, menu ) );
menu->addAction( actions->actionShowInOverview( menu ) );
}
if ( rlayer )
{
menu->addAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionZoomActual.svg" ) ), tr( "&Zoom to Native Resolution (100%)" ), QgisApp::instance(), SLOT( legendLayerZoomNative() ) );
if ( rlayer->rasterType() != QgsRasterLayer::Palette )
menu->addAction( tr( "&Stretch Using Current Extent" ), QgisApp::instance(), SLOT( legendLayerStretchUsingCurrentExtent() ) );
}
menu->addAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionRemoveLayer.svg" ) ), tr( "&Remove" ), QgisApp::instance(), SLOT( removeLayer() ) );
// duplicate layer
QAction *duplicateLayersAction = menu->addAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionDuplicateLayer.svg" ) ), tr( "&Duplicate" ), QgisApp::instance(), SLOT( duplicateLayers() ) );
if ( layer && layer->isSpatial() )
{
// set layer scale visibility
menu->addAction( tr( "&Set Layer Scale Visibility" ), QgisApp::instance(), SLOT( setLayerScaleVisibility() ) );
if ( !layer->isInScaleRange( mCanvas->scale() ) )
menu->addAction( tr( "Zoom to &Visible Scale" ), QgisApp::instance(), SLOT( zoomToLayerScale() ) );
menu->addAction( actions->actionCheckAndAllParents( menu ) );
// set layer crs
menu->addAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionSetCRS.png" ) ), tr( "Set Layer CRS" ), QgisApp::instance(), SLOT( setLayerCrs() ) );
// assign layer crs to project
menu->addAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionSetProjectCRS.png" ) ), tr( "Set &Project CRS from Layer" ), QgisApp::instance(), SLOT( setProjectCrsFromLayer() ) );
}
// style-related actions
if ( layer && mView->selectedLayerNodes().count() == 1 )
{
QMenu *menuStyleManager = new QMenu( tr( "Styles" ), menu );
QgisApp *app = QgisApp::instance();
menuStyleManager->addAction( tr( "Copy Style" ), app, SLOT( copyStyle() ) );
if ( app->clipboard()->hasFormat( QGSCLIPBOARD_STYLE_MIME ) )
{
menuStyleManager->addAction( tr( "Paste Style" ), app, SLOT( pasteStyle() ) );
}
menuStyleManager->addSeparator();
QgsMapLayerStyleGuiUtils::instance()->addStyleManagerActions( menuStyleManager, layer );
if ( vlayer )
{
const QgsSingleSymbolRenderer *singleRenderer = dynamic_cast< const QgsSingleSymbolRenderer * >( vlayer->renderer() );
if ( !singleRenderer && vlayer->renderer() && vlayer->renderer()->embeddedRenderer() )
{
singleRenderer = dynamic_cast< const QgsSingleSymbolRenderer * >( vlayer->renderer()->embeddedRenderer() );
}
if ( singleRenderer && singleRenderer->symbol() )
{
//single symbol renderer, so add set color/edit symbol actions
menuStyleManager->addSeparator();
QgsColorWheel *colorWheel = new QgsColorWheel( menuStyleManager );
colorWheel->setColor( singleRenderer->symbol()->color() );
QgsColorWidgetAction *colorAction = new QgsColorWidgetAction( colorWheel, menuStyleManager, menuStyleManager );
colorAction->setDismissOnColorSelection( false );
connect( colorAction, SIGNAL( colorChanged( const QColor & ) ), this, SLOT( setVectorSymbolColor( const QColor & ) ) );
//store the layer id in action, so we can later retrieve the corresponding layer
colorAction->setProperty( "layerId", vlayer->id() );
menuStyleManager->addAction( colorAction );
//add recent colors action
QList<QgsRecentColorScheme *> recentSchemes;
QgsApplication::colorSchemeRegistry()->schemes( recentSchemes );
if ( !recentSchemes.isEmpty() )
{
QgsColorSwatchGridAction *recentColorAction = new QgsColorSwatchGridAction( recentSchemes.at( 0 ), menuStyleManager, QStringLiteral( "symbology" ), menuStyleManager );
recentColorAction->setProperty( "layerId", vlayer->id() );
recentColorAction->setDismissOnColorSelection( false );
menuStyleManager->addAction( recentColorAction );
connect( recentColorAction, SIGNAL( colorChanged( const QColor & ) ), this, SLOT( setVectorSymbolColor( const QColor & ) ) );
}
menuStyleManager->addSeparator();
QAction *editSymbolAction = new QAction( tr( "Edit Symbol..." ), menuStyleManager );
//store the layer id in action, so we can later retrieve the corresponding layer
editSymbolAction->setProperty( "layerId", vlayer->id() );
connect( editSymbolAction, SIGNAL( triggered() ), this, SLOT( editVectorSymbol() ) );
menuStyleManager->addAction( editSymbolAction );
}
}
menu->addMenu( menuStyleManager );
}
else
{
if ( QgisApp::instance()->clipboard()->hasFormat( QGSCLIPBOARD_STYLE_MIME ) )
{
menu->addAction( tr( "Paste Style" ), QgisApp::instance(), SLOT( applyStyleToGroup() ) );
}
}
menu->addSeparator();
if ( vlayer )
{
QAction *toggleEditingAction = QgisApp::instance()->actionToggleEditing();
QAction *saveLayerEditsAction = QgisApp::instance()->actionSaveActiveLayerEdits();
QAction *allEditsAction = QgisApp::instance()->actionAllEdits();
// attribute table
menu->addAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionOpenTable.svg" ) ), tr( "&Open Attribute Table" ),
QgisApp::instance(), SLOT( attributeTable() ) );
// allow editing
int cap = vlayer->dataProvider()->capabilities();
if ( cap & QgsVectorDataProvider::EditingCapabilities )
{
if ( toggleEditingAction )
{
menu->addAction( toggleEditingAction );
toggleEditingAction->setChecked( vlayer->isEditable() );
}
if ( saveLayerEditsAction && vlayer->isModified() )
{
menu->addAction( saveLayerEditsAction );
}
}
if ( allEditsAction->isEnabled() )
menu->addAction( allEditsAction );
// disable duplication of memory layers
if ( vlayer->storageType() == QLatin1String( "Memory storage" ) && mView->selectedLayerNodes().count() == 1 )
duplicateLayersAction->setEnabled( false );
// save as vector file
menu->addAction( tr( "Save As..." ), QgisApp::instance(), SLOT( saveAsFile() ) );
menu->addAction( tr( "Save As Layer Definition File..." ), QgisApp::instance(), SLOT( saveAsLayerDefinition() ) );
if ( vlayer->dataProvider()->supportsSubsetString() )
{
QAction *action = menu->addAction( tr( "&Filter..." ), QgisApp::instance(), SLOT( layerSubsetString() ) );
action->setEnabled( !vlayer->isEditable() );
}
menu->addAction( actions->actionShowFeatureCount( menu ) );
menu->addSeparator();
}
else if ( rlayer )
{
menu->addAction( tr( "Save As..." ), QgisApp::instance(), SLOT( saveAsRasterFile() ) );
menu->addAction( tr( "Save As Layer Definition File..." ), QgisApp::instance(), SLOT( saveAsLayerDefinition() ) );
}
else if ( layer && layer->type() == QgsMapLayer::PluginLayer && mView->selectedLayerNodes().count() == 1 )
{
// disable duplication of plugin layers
duplicateLayersAction->setEnabled( false );
}
addCustomLayerActions( menu, layer );
if ( layer && QgsProject::instance()->layerIsEmbedded( layer->id() ).isEmpty() )
menu->addAction( tr( "&Properties" ), QgisApp::instance(), SLOT( layerProperties() ) );
if ( node->parent() != mView->layerTreeModel()->rootGroup() )
menu->addAction( actions->actionMakeTopLevel( menu ) );
menu->addAction( actions->actionRenameGroupOrLayer( menu ) );
if ( mView->selectedNodes( true ).count() >= 2 )
menu->addAction( actions->actionGroupSelected( menu ) );
if ( layer && layer->type() == QgsMapLayer::VectorLayer && static_cast<QgsVectorLayer *>( layer )->providerType() == QLatin1String( "virtual" ) )
{
menu->addAction( tr( "Edit virtual layer settings" ), QgisApp::instance(), SLOT( addVirtualLayer() ) );
}
}
}
else if ( QgsLayerTreeModelLegendNode *node = mView->layerTreeModel()->index2legendNode( idx ) )
{
if ( QgsSymbolLegendNode *symbolNode = dynamic_cast< QgsSymbolLegendNode * >( node ) )
{
// symbology item
if ( symbolNode->flags() & Qt::ItemIsUserCheckable )
{
menu->addAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionShowAllLayers.svg" ) ), tr( "&Show All Items" ),
symbolNode, SLOT( checkAllItems() ) );
menu->addAction( QgsApplication::getThemeIcon( QStringLiteral( "/mActionHideAllLayers.svg" ) ), tr( "&Hide All Items" ),
symbolNode, SLOT( uncheckAllItems() ) );
menu->addSeparator();
}
if ( symbolNode->symbol() )
{
QgsColorWheel *colorWheel = new QgsColorWheel( menu );
colorWheel->setColor( symbolNode->symbol()->color() );
QgsColorWidgetAction *colorAction = new QgsColorWidgetAction( colorWheel, menu, menu );
colorAction->setDismissOnColorSelection( false );
connect( colorAction, SIGNAL( colorChanged( const QColor & ) ), this, SLOT( setSymbolLegendNodeColor( const QColor & ) ) );
//store the layer id and rule key in action, so we can later retrieve the corresponding
//legend node, if it still exists
colorAction->setProperty( "layerId", symbolNode->layerNode()->layerId() );
colorAction->setProperty( "ruleKey", symbolNode->data( QgsLayerTreeModelLegendNode::RuleKeyRole ).toString() );
menu->addAction( colorAction );
//add recent colors action
QList<QgsRecentColorScheme *> recentSchemes;
QgsApplication::colorSchemeRegistry()->schemes( recentSchemes );
if ( !recentSchemes.isEmpty() )
{
QgsColorSwatchGridAction *recentColorAction = new QgsColorSwatchGridAction( recentSchemes.at( 0 ), menu, QStringLiteral( "symbology" ), menu );
recentColorAction->setProperty( "layerId", symbolNode->layerNode()->layerId() );
recentColorAction->setProperty( "ruleKey", symbolNode->data( QgsLayerTreeModelLegendNode::RuleKeyRole ).toString() );
recentColorAction->setDismissOnColorSelection( false );
menu->addAction( recentColorAction );
connect( recentColorAction, SIGNAL( colorChanged( const QColor & ) ), this, SLOT( setSymbolLegendNodeColor( const QColor & ) ) );
}
menu->addSeparator();
}
QAction *editSymbolAction = new QAction( tr( "Edit Symbol..." ), menu );
//store the layer id and rule key in action, so we can later retrieve the corresponding
//legend node, if it still exists
editSymbolAction->setProperty( "layerId", symbolNode->layerNode()->layerId() );
editSymbolAction->setProperty( "ruleKey", symbolNode->data( QgsLayerTreeModelLegendNode::RuleKeyRole ).toString() );
connect( editSymbolAction, SIGNAL( triggered() ), this, SLOT( editSymbolLegendNodeSymbol() ) );
menu->addAction( editSymbolAction );
}
}
return menu;
}
void QgsAppLayerTreeViewMenuProvider::addLegendLayerAction( QAction *action, const QString &menu,
QgsMapLayer::LayerType type, bool allLayers )
{
mLegendLayerActionMap[type].append( LegendLayerAction( action, menu, allLayers ) );
}
bool QgsAppLayerTreeViewMenuProvider::removeLegendLayerAction( QAction *action )
{
QMap< QgsMapLayer::LayerType, QList< LegendLayerAction > >::iterator it;
for ( it = mLegendLayerActionMap.begin();
it != mLegendLayerActionMap.end(); ++it )
{
for ( int i = 0; i < it->count(); i++ )
{
if ( ( *it )[i].action == action )
{
( *it ).removeAt( i );
return true;
}
}
}
return false;
}
void QgsAppLayerTreeViewMenuProvider::addLegendLayerActionForLayer( QAction *action, QgsMapLayer *layer )
{
if ( !action || !layer )
return;
legendLayerActions( layer->type() );
if ( !mLegendLayerActionMap.contains( layer->type() ) )
return;
QMap< QgsMapLayer::LayerType, QList< LegendLayerAction > >::iterator it
= mLegendLayerActionMap.find( layer->type() );
for ( int i = 0; i < it->count(); i++ )
{
if ( ( *it )[i].action == action )
{
( *it )[i].layers.append( layer );
return;
}
}
}
void QgsAppLayerTreeViewMenuProvider::removeLegendLayerActionsForLayer( QgsMapLayer *layer )
{
if ( ! layer || ! mLegendLayerActionMap.contains( layer->type() ) )
return;
QMap< QgsMapLayer::LayerType, QList< LegendLayerAction > >::iterator it
= mLegendLayerActionMap.find( layer->type() );
for ( int i = 0; i < it->count(); i++ )
{
( *it )[i].layers.removeAll( layer );
}
}
QList< LegendLayerAction > QgsAppLayerTreeViewMenuProvider::legendLayerActions( QgsMapLayer::LayerType type ) const
{
#ifdef QGISDEBUG
if ( mLegendLayerActionMap.contains( type ) )
{
QgsDebugMsg( QString( "legendLayerActions for layers of type %1:" ).arg( type ) );
Q_FOREACH ( const LegendLayerAction &lyrAction, mLegendLayerActionMap[ type ] )
{
Q_UNUSED( lyrAction );
QgsDebugMsg( QString( "%1/%2 - %3 layers" ).arg( lyrAction.menu, lyrAction.action->text() ).arg( lyrAction.layers.count() ) );
}
}
#endif
return mLegendLayerActionMap.contains( type ) ? mLegendLayerActionMap.value( type ) : QList< LegendLayerAction >();
}
void QgsAppLayerTreeViewMenuProvider::addCustomLayerActions( QMenu *menu, QgsMapLayer *layer )
{
if ( !layer )
return;
// add custom layer actions - should this go at end?
QList< LegendLayerAction > lyrActions = legendLayerActions( layer->type() );
if ( ! lyrActions.isEmpty() )
{
menu->addSeparator();
QList<QMenu *> menus;
for ( int i = 0; i < lyrActions.count(); i++ )
{
if ( lyrActions[i].allLayers || lyrActions[i].layers.contains( layer ) )
{
if ( lyrActions[i].menu.isEmpty() )
{
menu->addAction( lyrActions[i].action );
}
else
{
// find or create menu for given menu name
// adapted from QgisApp::getPluginMenu( QString menuName )
QString menuName = lyrActions[i].menu;
#ifdef Q_OS_MAC
// Mac doesn't have '&' keyboard shortcuts.
menuName.remove( QChar( '&' ) );
#endif
QAction *before = nullptr;
QMenu *newMenu = nullptr;
QString dst = menuName;
dst.remove( QChar( '&' ) );
Q_FOREACH ( QMenu *menu, menus )
{
QString src = menu->title();
src.remove( QChar( '&' ) );
int comp = dst.localeAwareCompare( src );
if ( comp < 0 )
{
// Add item before this one
before = menu->menuAction();
break;
}
else if ( comp == 0 )
{
// Plugin menu item already exists
newMenu = menu;
break;
}
}
if ( ! newMenu )
{
// It doesn't exist, so create
newMenu = new QMenu( menuName );
menus.append( newMenu );
// Where to put it? - we worked that out above...
menu->insertMenu( before, newMenu );
}
// QMenu* menu = getMenu( lyrActions[i].menu, &beforeSep, &afterSep, &menu );
newMenu->addAction( lyrActions[i].action );
}
}
}
menu->addSeparator();
}
}
void QgsAppLayerTreeViewMenuProvider::editVectorSymbol()
{
QAction *action = qobject_cast< QAction *>( sender() );
if ( !action )
return;
QString layerId = action->property( "layerId" ).toString();
QgsVectorLayer *layer = dynamic_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( layerId ) );
if ( !layer )
return;
QgsSingleSymbolRenderer *singleRenderer = dynamic_cast< QgsSingleSymbolRenderer * >( layer->renderer() );
if ( !singleRenderer )
return;
std::unique_ptr< QgsSymbol > symbol( singleRenderer->symbol() ? singleRenderer->symbol()->clone() : nullptr );
QgsSymbolSelectorDialog dlg( symbol.get(), QgsStyle::defaultStyle(), layer, mView->window() );
dlg.setWindowTitle( tr( "Symbol selector" ) );
QgsSymbolWidgetContext context;
context.setMapCanvas( mCanvas );
dlg.setContext( context );
if ( dlg.exec() )
{
singleRenderer->setSymbol( symbol.release() );
layer->triggerRepaint();
mView->refreshLayerSymbology( layer->id() );
}
}
void QgsAppLayerTreeViewMenuProvider::setVectorSymbolColor( const QColor &color )
{
QAction *action = qobject_cast< QAction *>( sender() );
if ( !action )
return;
QString layerId = action->property( "layerId" ).toString();
QgsVectorLayer *layer = qobject_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( layerId ) );
if ( !layer )
return;
QgsSingleSymbolRenderer *singleRenderer = dynamic_cast< QgsSingleSymbolRenderer * >( layer->renderer() );
QgsSymbol *newSymbol = nullptr;
if ( singleRenderer && singleRenderer->symbol() )
newSymbol = singleRenderer->symbol()->clone();
const QgsSingleSymbolRenderer *embeddedRenderer = nullptr;
if ( !newSymbol && layer->renderer()->embeddedRenderer() )
{
embeddedRenderer = dynamic_cast< const QgsSingleSymbolRenderer * >( layer->renderer()->embeddedRenderer() );
if ( embeddedRenderer && embeddedRenderer->symbol() )
newSymbol = embeddedRenderer->symbol()->clone();
}
if ( !newSymbol )
return;
newSymbol->setColor( color );
if ( singleRenderer )
{
singleRenderer->setSymbol( newSymbol );
}
else if ( embeddedRenderer )
{
QgsSingleSymbolRenderer *newRenderer = embeddedRenderer->clone();
newRenderer->setSymbol( newSymbol );
layer->renderer()->setEmbeddedRenderer( newRenderer );
}
layer->triggerRepaint();
layer->emitStyleChanged();
mView->refreshLayerSymbology( layer->id() );
}
void QgsAppLayerTreeViewMenuProvider::editSymbolLegendNodeSymbol()
{
QAction *action = qobject_cast< QAction *>( sender() );
if ( !action )
return;
QString layerId = action->property( "layerId" ).toString();
QString ruleKey = action->property( "ruleKey" ).toString();
QgsSymbolLegendNode *node = dynamic_cast<QgsSymbolLegendNode *>( mView->layerTreeModel()->findLegendNode( layerId, ruleKey ) );
if ( !node )
return;
const QgsSymbol *originalSymbol = node->symbol();
if ( !originalSymbol )
return;
std::unique_ptr< QgsSymbol > symbol( originalSymbol->clone() );
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( node->layerNode()->layer() );
QgsSymbolSelectorDialog dlg( symbol.get(), QgsStyle::defaultStyle(), vlayer, mView->window() );
dlg.setWindowTitle( tr( "Symbol selector" ) );
QgsSymbolWidgetContext context;
context.setMapCanvas( mCanvas );
dlg.setContext( context );
if ( dlg.exec() )
{
node->setSymbol( symbol.release() );
if ( vlayer )
{
vlayer->emitStyleChanged();
}
}
}
void QgsAppLayerTreeViewMenuProvider::setSymbolLegendNodeColor( const QColor &color )
{
QAction *action = qobject_cast< QAction *>( sender() );
if ( !action )
return;
QString layerId = action->property( "layerId" ).toString();
QString ruleKey = action->property( "ruleKey" ).toString();
QgsSymbolLegendNode *node = dynamic_cast<QgsSymbolLegendNode *>( mView->layerTreeModel()->findLegendNode( layerId, ruleKey ) );
if ( !node )
return;
const QgsSymbol *originalSymbol = node->symbol();
if ( !originalSymbol )
return;
QgsSymbol *newSymbol = originalSymbol->clone();
newSymbol->setColor( color );
node->setSymbol( newSymbol );
if ( QgsVectorLayer *layer = qobject_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( layerId ) ) )
{
layer->emitStyleChanged();
}
}