/
qgsstylev2managerdialog.cpp
1219 lines (1069 loc) · 34.7 KB
/
qgsstylev2managerdialog.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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************
qgsstylev2managerdialog.cpp
---------------------
begin : November 2009
copyright : (C) 2009 by Martin Dobias
email : wonder.sk at gmail.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 "qgsstylev2managerdialog.h"
#include "qgsstylev2.h"
#include "qgssymbolv2.h"
#include "qgssymbollayerv2utils.h"
#include "qgsvectorcolorrampv2.h"
#include "qgssymbolv2selectordialog.h"
#include "qgsvectorgradientcolorrampv2dialog.h"
#include "qgsvectorrandomcolorrampv2dialog.h"
#include "qgsvectorcolorbrewercolorrampv2dialog.h"
#include "qgsstylev2exportimportdialog.h"
#include "qgssmartgroupeditordialog.h"
#include <QFile>
#include <QFileDialog>
#include <QInputDialog>
#include <QMessageBox>
#include <QSettings>
#include <QStandardItemModel>
#include <QAction>
#include <QMenu>
#include "qgsapplication.h"
#include "qgslogger.h"
QgsStyleV2ManagerDialog::QgsStyleV2ManagerDialog( QgsStyleV2* style, QWidget* parent )
: QDialog( parent ), mStyle( style ), mModified( false )
{
setupUi( this );
QSettings settings;
restoreGeometry( settings.value( "/Windows/StyleV2Manager/geometry" ).toByteArray() );
#if QT_VERSION >= 0x40500
tabItemType->setDocumentMode( true );
#endif
// setup icons
btnAddItem->setIcon( QIcon( QgsApplication::iconPath( "symbologyAdd.png" ) ) );
btnEditItem->setIcon( QIcon( QgsApplication::iconPath( "symbologyEdit.png" ) ) );
btnRemoveItem->setIcon( QIcon( QgsApplication::iconPath( "symbologyRemove.png" ) ) );
connect( this, SIGNAL( finished( int ) ), this, SLOT( onFinished() ) );
connect( listItems, SIGNAL( doubleClicked( const QModelIndex & ) ), this, SLOT( editItem() ) );
connect( btnAddItem, SIGNAL( clicked() ), this, SLOT( addItem() ) );
connect( btnEditItem, SIGNAL( clicked() ), this, SLOT( editItem() ) );
connect( btnRemoveItem, SIGNAL( clicked() ), this, SLOT( removeItem() ) );
QMenu *shareMenu = new QMenu( "Share Menu", this );
QAction *exportAction = shareMenu->addAction( "Export" );
QAction *importAction = shareMenu->addAction( "Import" );
connect( exportAction, SIGNAL( triggered() ), this, SLOT( exportItems() ) );
connect( importAction, SIGNAL( triggered() ), this, SLOT( importItems() ) );
btnShare->setMenu( shareMenu );
// Set editing mode off by default
mGrouppingMode = false;
QStandardItemModel* model = new QStandardItemModel( listItems );
listItems->setModel( model );
listItems->setSelectionMode( QAbstractItemView::ExtendedSelection );
connect( model, SIGNAL( itemChanged( QStandardItem* ) ), this, SLOT( itemChanged( QStandardItem* ) ) );
connect( listItems->selectionModel(), SIGNAL( currentChanged( const QModelIndex&, const QModelIndex& ) ),
this, SLOT( symbolSelected( const QModelIndex& ) ) );
populateTypes();
QStandardItemModel* groupModel = new QStandardItemModel( groupTree );
groupTree->setModel( groupModel );
groupTree->setHeaderHidden( true );
populateGroups();
int rows = groupModel->rowCount( groupModel->indexFromItem( groupModel->invisibleRootItem() ) );
for ( int i = 0; i < rows; i++ )
{
groupTree->setExpanded( groupModel->indexFromItem( groupModel->item( i )), true );
}
connect( groupTree->selectionModel(), SIGNAL( currentChanged( const QModelIndex&, const QModelIndex& ) ),
this, SLOT( groupChanged( const QModelIndex& ) ) );
connect( groupModel, SIGNAL( itemChanged( QStandardItem* ) ),
this, SLOT( groupRenamed( QStandardItem* ) ) );
QMenu *groupMenu = new QMenu( "Group Actions", this );
QAction *groupSymbols = groupMenu->addAction( "Group Symbols" );
QAction *editSmartgroup = groupMenu->addAction( "Edit Smart Group" );
btnManageGroups->setMenu( groupMenu );
connect( groupSymbols, SIGNAL( triggered() ), this, SLOT( groupSymbolsAction() ) );
connect( editSmartgroup, SIGNAL( triggered() ), this, SLOT( editSmartgroupAction() ) );
connect( btnAddGroup, SIGNAL( clicked() ), this, SLOT( addGroup() ) );
connect( btnRemoveGroup, SIGNAL( clicked() ), this, SLOT( removeGroup() ) );
connect( tabItemType, SIGNAL( currentChanged( int ) ), this, SLOT( populateList() ) );
populateList();
connect( searchBox, SIGNAL( textChanged( QString ) ), this, SLOT( filterSymbols( QString ) ) );
tagsLineEdit->installEventFilter( this );
// Context menu for groupTree
groupTree->setContextMenuPolicy( Qt::CustomContextMenu );
connect( groupTree, SIGNAL( customContextMenuRequested( const QPoint& ) ),
this, SLOT( grouptreeContextMenu( const QPoint& ) ) );
// Context menu for listItems
listItems->setContextMenuPolicy( Qt::CustomContextMenu );
connect( listItems, SIGNAL( customContextMenuRequested( const QPoint& ) ),
this, SLOT( listitemsContextMenu( const QPoint& ) ) );
}
void QgsStyleV2ManagerDialog::onFinished()
{
if ( mModified )
{
mStyle->save();
}
QSettings settings;
settings.setValue( "/Windows/StyleV2Manager/geometry", saveGeometry() );
}
void QgsStyleV2ManagerDialog::populateTypes()
{
#if 0
// save current selection index in types combo
int current = ( tabItemType->count() > 0 ? tabItemType->currentIndex() : 0 );
// no counting of style items
int markerCount = 0, lineCount = 0, fillCount = 0;
QStringList symbolNames = mStyle->symbolNames();
for ( int i = 0; i < symbolNames.count(); ++i )
{
switch ( mStyle->symbolRef( symbolNames[i] )->type() )
{
case QgsSymbolV2::Marker:
markerCount++;
break;
case QgsSymbolV2::Line:
lineCount++;
break;
case QgsSymbolV2::Fill:
fillCount++;
break;
default: Q_ASSERT( 0 && "unknown symbol type" );
break;
}
}
cboItemType->clear();
cboItemType->addItem( tr( "Marker symbol (%1)" ).arg( markerCount ), QVariant( QgsSymbolV2::Marker ) );
cboItemType->addItem( tr( "Line symbol (%1)" ).arg( lineCount ), QVariant( QgsSymbolV2::Line ) );
cboItemType->addItem( tr( "Fill symbol (%1)" ).arg( fillCount ), QVariant( QgsSymbolV2::Fill ) );
cboItemType->addItem( tr( "Color ramp (%1)" ).arg( mStyle->colorRampCount() ), QVariant( 3 ) );
// update current index to previous selection
cboItemType->setCurrentIndex( current );
#endif
}
void QgsStyleV2ManagerDialog::populateList()
{
if ( currentItemType() > 3 )
{
Q_ASSERT( 0 && "not implemented" );
return;
}
groupChanged( groupTree->selectionModel()->currentIndex() );
}
void QgsStyleV2ManagerDialog::populateSymbols( QStringList symbolNames, bool check )
{
QStandardItemModel* model = qobject_cast<QStandardItemModel*>( listItems->model() );
model->clear();
// QStringList symbolNames = mStyle->symbolNames();
int type = currentItemType();
for ( int i = 0; i < symbolNames.count(); ++i )
{
QString name = symbolNames[i];
QgsSymbolV2* symbol = mStyle->symbol( name );
if ( symbol->type() == type )
{
QStandardItem* item = new QStandardItem( name );
QIcon icon = QgsSymbolLayerV2Utils::symbolPreviewIcon( symbol, listItems->iconSize() );
item->setIcon( icon );
item->setData( name ); // used to find out original name when user edited the name
item->setCheckable( check );
// add to model
model->appendRow( item );
}
delete symbol;
}
}
void QgsStyleV2ManagerDialog::populateColorRamps( QStringList colorRamps, bool check )
{
QStandardItemModel* model = qobject_cast<QStandardItemModel*>( listItems->model() );
model->clear();
for ( int i = 0; i < colorRamps.count(); ++i )
{
QString name = colorRamps[i];
QgsVectorColorRampV2* ramp = mStyle->colorRamp( name );
QStandardItem* item = new QStandardItem( name );
QIcon icon = QgsSymbolLayerV2Utils::colorRampPreviewIcon( ramp, listItems->iconSize() );
item->setIcon( icon );
item->setData( name ); // used to find out original name when user edited the name
item->setCheckable( check );
model->appendRow( item );
delete ramp;
}
}
int QgsStyleV2ManagerDialog::currentItemType()
{
switch ( tabItemType->currentIndex() )
{
case 0: return QgsSymbolV2::Marker;
case 1: return QgsSymbolV2::Line;
case 2: return QgsSymbolV2::Fill;
case 3: return 3;
default: return 0;
}
}
QString QgsStyleV2ManagerDialog::currentItemName()
{
QModelIndex index = listItems->selectionModel()->currentIndex();
if ( !index.isValid() )
return QString();
return index.model()->data( index, 0 ).toString();
}
void QgsStyleV2ManagerDialog::addItem()
{
bool changed = false;
if ( currentItemType() < 3 )
{
changed = addSymbol();
}
else if ( currentItemType() == 3 )
{
changed = addColorRamp();
}
else
{
Q_ASSERT( 0 && "not implemented" );
}
if ( changed )
{
populateList();
populateTypes();
}
}
bool QgsStyleV2ManagerDialog::addSymbol()
{
// create new symbol with current type
QgsSymbolV2* symbol;
switch ( currentItemType() )
{
case QgsSymbolV2::Marker:
symbol = new QgsMarkerSymbolV2();
break;
case QgsSymbolV2::Line:
symbol = new QgsLineSymbolV2();
break;
case QgsSymbolV2::Fill:
symbol = new QgsFillSymbolV2();
break;
default:
Q_ASSERT( 0 && "unknown symbol type" );
return false;
}
// get symbol design
QgsSymbolV2SelectorDialog dlg( symbol, mStyle, NULL, this );
if ( dlg.exec() == 0 )
{
delete symbol;
return false;
}
// get name
bool ok;
QString name = QInputDialog::getText( this, tr( "Symbol name" ),
tr( "Please enter name for new symbol:" ), QLineEdit::Normal, tr( "new symbol" ), &ok );
if ( !ok || name.isEmpty() )
{
delete symbol;
return false;
}
// check if there is no symbol with same name
if ( mStyle->symbolNames().contains( name ) )
{
int res = QMessageBox::warning( this, tr( "Save symbol" ),
tr( "Symbol with name '%1' already exists. Overwrite?" )
.arg( name ),
QMessageBox::Yes | QMessageBox::No );
if ( res != QMessageBox::Yes )
{
delete symbol;
return false;
}
}
// add new symbol to style and re-populate the list
mStyle->addSymbol( name, symbol );
// TODO groups and tags
mStyle->saveSymbol( name, symbol, 0, QStringList() );
mModified = true;
return true;
}
QString QgsStyleV2ManagerDialog::addColorRampStatic( QWidget* parent, QgsStyleV2* style )
{
// let the user choose the color ramp type
QStringList rampTypes;
rampTypes << tr( "Gradient" ) << tr( "Random" ) << tr( "ColorBrewer" );
bool ok;
QString rampType = QInputDialog::getItem( parent, tr( "Color ramp type" ),
tr( "Please select color ramp type:" ), rampTypes, 0, false, &ok );
if ( !ok || rampType.isEmpty() )
return QString();
QgsVectorColorRampV2 *ramp = NULL;
if ( rampType == tr( "Gradient" ) )
{
QgsVectorGradientColorRampV2* gradRamp = new QgsVectorGradientColorRampV2();
QgsVectorGradientColorRampV2Dialog dlg( gradRamp, parent );
if ( !dlg.exec() )
{
delete gradRamp;
return QString();
}
ramp = gradRamp;
}
else if ( rampType == tr( "Random" ) )
{
QgsVectorRandomColorRampV2* randRamp = new QgsVectorRandomColorRampV2();
QgsVectorRandomColorRampV2Dialog dlg( randRamp, parent );
if ( !dlg.exec() )
{
delete randRamp;
return QString();
}
ramp = randRamp;
}
else if ( rampType == tr( "ColorBrewer" ) )
{
QgsVectorColorBrewerColorRampV2* brewerRamp = new QgsVectorColorBrewerColorRampV2();
QgsVectorColorBrewerColorRampV2Dialog dlg( brewerRamp, parent );
if ( !dlg.exec() )
{
delete brewerRamp;
return QString();
}
ramp = brewerRamp;
}
else
{
Q_ASSERT( 0 && "invalid ramp type" );
}
// get name
QString name = QInputDialog::getText( parent, tr( "Color ramp name" ),
tr( "Please enter name for new color ramp:" ), QLineEdit::Normal, tr( "new color ramp" ), &ok );
if ( !ok || name.isEmpty() )
{
if ( ramp )
delete ramp;
return QString();
}
// add new symbol to style and re-populate the list
style->addColorRamp( name, ramp );
return name;
}
bool QgsStyleV2ManagerDialog::addColorRamp()
{
QString rampName = addColorRampStatic( this , mStyle );
if ( !rampName.isEmpty() )
{
mModified = true;
return true;
}
return false;
}
void QgsStyleV2ManagerDialog::editItem()
{
bool changed = false;
if ( currentItemType() < 3 )
{
changed = editSymbol();
}
else if ( currentItemType() == 3 )
{
changed = editColorRamp();
}
else
{
Q_ASSERT( 0 && "not implemented" );
}
if ( changed )
populateList();
}
bool QgsStyleV2ManagerDialog::editSymbol()
{
QString symbolName = currentItemName();
if ( symbolName.isEmpty() )
return false;
QgsSymbolV2* symbol = mStyle->symbol( symbolName );
// let the user edit the symbol and update list when done
QgsSymbolV2SelectorDialog dlg( symbol, mStyle, NULL , this );
if ( dlg.exec() == 0 )
{
delete symbol;
return false;
}
// by adding symbol to style with the same name the old effectively gets overwritten
mStyle->addSymbol( symbolName, symbol );
mModified = true;
return true;
}
bool QgsStyleV2ManagerDialog::editColorRamp()
{
QString name = currentItemName();
if ( name.isEmpty() )
return false;
QgsVectorColorRampV2* ramp = mStyle->colorRamp( name );
if ( ramp->type() == "gradient" )
{
QgsVectorGradientColorRampV2* gradRamp = static_cast<QgsVectorGradientColorRampV2*>( ramp );
QgsVectorGradientColorRampV2Dialog dlg( gradRamp, this );
if ( !dlg.exec() )
{
delete ramp;
return false;
}
}
else if ( ramp->type() == "random" )
{
QgsVectorRandomColorRampV2* randRamp = static_cast<QgsVectorRandomColorRampV2*>( ramp );
QgsVectorRandomColorRampV2Dialog dlg( randRamp, this );
if ( !dlg.exec() )
{
delete ramp;
return false;
}
}
else if ( ramp->type() == "colorbrewer" )
{
QgsVectorColorBrewerColorRampV2* brewerRamp = static_cast<QgsVectorColorBrewerColorRampV2*>( ramp );
QgsVectorColorBrewerColorRampV2Dialog dlg( brewerRamp, this );
if ( !dlg.exec() )
{
delete ramp;
return false;
}
}
else
{
Q_ASSERT( 0 && "invalid ramp type" );
}
mStyle->addColorRamp( name, ramp );
mModified = true;
return true;
}
void QgsStyleV2ManagerDialog::removeItem()
{
bool changed = false;
if ( currentItemType() < 3 )
{
changed = removeSymbol();
}
else if ( currentItemType() == 3 )
{
changed = removeColorRamp();
}
else
{
Q_ASSERT( 0 && "not implemented" );
}
if ( changed )
{
populateList();
populateTypes();
}
}
bool QgsStyleV2ManagerDialog::removeSymbol()
{
QString symbolName = currentItemName();
if ( symbolName.isEmpty() )
return false;
// delete from style and update list
mStyle->removeSymbol( symbolName );
mModified = true;
return true;
}
bool QgsStyleV2ManagerDialog::removeColorRamp()
{
QString rampName = currentItemName();
if ( rampName.isEmpty() )
return false;
mStyle->removeColorRamp( rampName );
mModified = true;
return true;
}
void QgsStyleV2ManagerDialog::itemChanged( QStandardItem* item )
{
// an item has been edited
QString oldName = item->data().toString();
bool changed = false;
if ( currentItemType() < 3 )
{
changed = mStyle->renameSymbol( oldName, item->text() );
}
else if ( currentItemType() == 3 )
{
changed = mStyle->renameColorRamp( oldName, item->text() );
}
if ( changed )
{
populateList();
mModified = true;
}
}
void QgsStyleV2ManagerDialog::exportItems()
{
QgsStyleV2ExportImportDialog dlg( mStyle, this, QgsStyleV2ExportImportDialog::Export );
dlg.exec();
}
void QgsStyleV2ManagerDialog::importItems()
{
QgsStyleV2ExportImportDialog dlg( mStyle, this, QgsStyleV2ExportImportDialog::Import );
dlg.exec();
populateList();
}
void QgsStyleV2ManagerDialog::setBold( QStandardItem* item )
{
QFont font = item->font();
font.setBold( true );
item->setFont( font );
}
void QgsStyleV2ManagerDialog::populateGroups()
{
QStandardItemModel *model = qobject_cast<QStandardItemModel*>( groupTree->model() );
model->clear();
QStandardItem *allSymbols = new QStandardItem( "All Symbols" );
allSymbols->setData( "all" );
allSymbols->setEditable( false );
setBold( allSymbols );
model->appendRow( allSymbols );
QStandardItem *projectSymbols = new QStandardItem( "Project Symbols" );
projectSymbols->setData( "project" );
projectSymbols->setEditable( false );
setBold( projectSymbols );
model->appendRow( projectSymbols );
QStandardItem *recent = new QStandardItem( "Recently Used" );
recent->setData( "recent" );
recent->setEditable( false );
setBold( recent );
model->appendRow( recent );
QStandardItem *group = new QStandardItem( "" ); //require empty name to get first order groups
group->setData( "groups" );
group->setEditable( false );
buildGroupTree( group );
group->setText( "Groups" );//set title later
QStandardItem *ungrouped = new QStandardItem( "Ungrouped" );
ungrouped->setData( 0 );
setBold( ungrouped );
setBold( group );
group->appendRow( ungrouped );
model->appendRow( group );
QStandardItem *tag = new QStandardItem( "Smart Groups" );
tag->setData( "smartgroups" );
tag->setEditable( false );
setBold( tag );
QgsSymbolGroupMap sgMap = mStyle->smartgroupsListMap();
QgsSymbolGroupMap::const_iterator i = sgMap.constBegin();
while ( i != sgMap.constEnd() )
{
QStandardItem *item = new QStandardItem( i.value() );
item->setData( i.key() );
tag->appendRow( item );
++i;
}
model->appendRow( tag );
}
void QgsStyleV2ManagerDialog::buildGroupTree( QStandardItem* &parent )
{
QgsSymbolGroupMap groups = mStyle->childGroupNames( parent->text() );
QgsSymbolGroupMap::const_iterator i = groups.constBegin();
while ( i != groups.constEnd() )
{
QStandardItem *item = new QStandardItem( i.value() );
item->setData( i.key() );
parent->appendRow( item );
buildGroupTree( item );
++i;
}
}
void QgsStyleV2ManagerDialog::groupChanged( const QModelIndex& index )
{
QStringList symbolNames;
QStringList groupSymbols;
StyleEntity type = currentItemType() < 3 ? SymbolEntity : ColorrampEntity;
if ( currentItemType() > 3 )
{
QgsDebugMsg( "Entity not implemented" );
return;
}
QString category = index.data( Qt::UserRole + 1 ).toString();
if ( category == "all" || category == "groups" || category == "smartgroups" )
{
enableGroupInputs( false );
if ( category == "groups" || category == "smartgroups" )
{
btnAddGroup->setEnabled( true );
}
symbolNames = currentItemType() < 3 ? mStyle->symbolNames() : mStyle->colorRampNames();
}
else if ( category == "recent" )
{
// TODO add session symbols
enableGroupInputs( false );
symbolNames = QStringList();
}
else if ( category == "project" )
{
// TODO add project symbols
enableGroupInputs( false );
symbolNames = QStringList();
}
else
{
//determine groups and tags
if ( index.parent().data( Qt::UserRole + 1 ) == "smartgroups" )
{
btnAddGroup->setEnabled( false );
btnRemoveGroup->setEnabled( true );
btnManageGroups->setEnabled( true );
int groupId = index.data( Qt::UserRole + 1 ).toInt();
symbolNames = mStyle->symbolsOfSmartgroup( type, groupId );
}
else // then it must be a group
{
if ( ( !index.data( Qt::UserRole + 1 ).toInt() && ( index.data() == "Ungrouped" ) ) || mGrouppingMode )
enableGroupInputs( false );
else
enableGroupInputs( true );
int groupId = index.data( Qt::UserRole + 1 ).toInt();
symbolNames = mStyle->symbolsOfGroup( type, groupId );
if ( mGrouppingMode && groupId )
{
groupSymbols = symbolNames;
symbolNames += mStyle->symbolsOfGroup( type, 0 );
}
}
}
if ( currentItemType() < 3 )
{
populateSymbols( symbolNames, mGrouppingMode );
}
else if ( currentItemType() == 3 )
{
populateColorRamps( symbolNames, mGrouppingMode );
}
if( mGrouppingMode )
setSymbolsChecked( groupSymbols );
}
void QgsStyleV2ManagerDialog::addGroup()
{
QStandardItemModel *model = qobject_cast<QStandardItemModel*>( groupTree->model() );
QModelIndex parentIndex = groupTree->currentIndex();
// Violation 1: Creating sub-groups of system defined groups
QString parentData = parentIndex.data( Qt::UserRole + 1 ).toString();
if ( parentData == "all" || parentData == "recent" || parentData == "project" ||
( parentIndex.data() == "Ungrouped" && parentData == "0" ) )
{
int err = QMessageBox::critical( this, tr( "Invalid Selection" ),
tr( "The parent group you have selected is not user editable.\n"
"Kindly select a user defined Group."));
if ( err )
return;
}
// Violation 2: Creating a nested tag
if ( parentIndex.parent().data( Qt::UserRole + 1 ).toString() == "smartgroups" )
{
int err = QMessageBox::critical( this, tr( "Operation Not Allowed" ),
tr( "Creation of nested Smart Groups are not allowed\n"
"Select the 'Smart Group' to create a new group." ) );
if ( err )
return;
}
QString itemName;
QVariant itemData;
bool isGroup = true;
// create a smart group if that is selected
if ( parentIndex.data( Qt::UserRole + 1 ).toString() == "smartgroups" )
{
QgsSmartGroupEditorDialog dlg( mStyle, this );
if ( dlg.exec() == QDialog::Rejected )
return;
int id = mStyle->addSmartgroup( dlg.smartgroupName(), dlg.conditionOperator(), dlg.conditionMap() );
if ( !id )
return;
itemData = QVariant( id );
itemName = dlg.smartgroupName();
isGroup = false;
}
else
{
itemName = QString( "New Group" );
itemData = QVariant( "newgroup" );
}
// Else create a simple child-group to the selected
QStandardItem *parentItem = model->itemFromIndex( parentIndex );
QStandardItem *childItem = new QStandardItem( itemName );
childItem->setData( itemData );
parentItem->appendRow( childItem );
groupTree->setCurrentIndex( childItem->index() );
if ( isGroup )
{
groupTree->edit( childItem->index() );
}
}
void QgsStyleV2ManagerDialog::removeGroup()
{
QStandardItemModel *model = qobject_cast<QStandardItemModel*>( groupTree->model() );
QModelIndex index = groupTree->currentIndex();
// Violation: removing system groups
QString data = index.data( Qt::UserRole + 1 ).toString();
if ( data == "all" || data == "recent" || data == "project" || data == "groups" || data == "smartgroups" || index.data() == "Ungrouped" )
{
int err = QMessageBox::critical( this, tr( "Invalid slection" ),
tr( "Cannot delete system defined categories.\n"
"Kindly select a group or smart group you might want to delete."));
if ( err )
return;
}
QStandardItem *parentItem = model->itemFromIndex( index.parent() );
if ( parentItem->data( Qt::UserRole + 1 ).toString() == "smartgroups" )
{
mStyle->remove( SmartgroupEntity, index.data( Qt::UserRole + 1 ).toInt() );
}
else
{
mStyle->remove( GroupEntity, index.data( Qt::UserRole + 1 ).toInt() );
QStandardItem *item = model->itemFromIndex( index );
if ( item->hasChildren() )
{
QStandardItem *parent = item->parent();
for( int i = 0; i < item->rowCount(); i++ )
{
parent->appendRow( item->takeChild( i ) );
}
}
}
parentItem->removeRow( index.row() );
}
void QgsStyleV2ManagerDialog::groupRenamed( QStandardItem * item )
{
QString data = item->data( Qt::UserRole + 1 ).toString();
QgsDebugMsg( "Symbol group edited: data=" + data + " text=" + item->text() );
if ( data == "newgroup" )
{
int id;
if ( item->parent()->data( Qt::UserRole + 1 ).toString() == "groups" )
{
id = mStyle->addGroup( item->text() );
}
else
{
int parentid = item->parent()->data( Qt::UserRole + 1 ).toInt();
id = mStyle->addGroup( item->text(), parentid );
}
if ( !id )
{
QMessageBox::critical( this, tr( "Error!" ),
tr( "New group could not be created.\n"
"There was a problem with your symbol database." ) );
item->parent()->removeRow( item->row() );
return;
}
else
{
item->setData( id );
}
}
else
{
int id = item->data( Qt::UserRole + 1 ).toInt();
QString name = item->text();
if ( item->parent()->data( Qt::UserRole + 1 ) == "smartgroups" )
{
mStyle->rename( SmartgroupEntity, id, name );
}
else
{
mStyle->rename( GroupEntity, id, name );
}
}
}
void QgsStyleV2ManagerDialog::groupSymbolsAction()
{
QStandardItemModel *treeModel = qobject_cast<QStandardItemModel*>( groupTree->model() );
QStandardItemModel *model = qobject_cast<QStandardItemModel*>( listItems->model() );
QAction *senderAction = qobject_cast<QAction*>( sender() );
if ( mGrouppingMode )
{
mGrouppingMode = false;
senderAction->setText( "Group Symbols" );
// disconnect slot which handles regrouping
disconnect( model, SIGNAL( itemChanged( QStandardItem* )),
this, SLOT( regrouped( QStandardItem* ) ) );
// disabel all items except groups in groupTree
enableItemsForGroupingMode( true );
groupChanged( groupTree->currentIndex() );
// Finally: Reconnect all Symbol editing functionalities
connect( treeModel, SIGNAL( itemChanged( QStandardItem* ) ),
this, SLOT( groupRenamed( QStandardItem* ) ) );
connect( model, SIGNAL( itemChanged( QStandardItem* ) ),
this, SLOT( itemChanged( QStandardItem* ) ) );
// Reset the selection mode
listItems->setSelectionMode( QAbstractItemView::ExtendedSelection );
}
else
{
bool validGroup = false;
// determine whether it is a valid group
QModelIndex present = groupTree->currentIndex();
while( present.parent().isValid() )
{
if ( present.parent().data() == "Groups" )
{
validGroup = true;
break;
}
else
present = present.parent();
}
if ( !validGroup )
return;
mGrouppingMode = true;
// Change the text menu
senderAction->setText( "Finish Grouping" );
// Remove all Symbol editing functionalities
disconnect( treeModel, SIGNAL( itemChanged( QStandardItem* ) ),
this, SLOT( groupRenamed( QStandardItem* ) ) );
disconnect( model, SIGNAL( itemChanged( QStandardItem* ) ),
this, SLOT( itemChanged( QStandardItem* ) ) );
// disabel all items except groups in groupTree
enableItemsForGroupingMode( false );
groupChanged( groupTree->currentIndex() );
btnManageGroups->setEnabled( true );
// Connect to slot which handles regrouping
connect( model, SIGNAL( itemChanged( QStandardItem* )),
this, SLOT( regrouped( QStandardItem* ) ) );
// No selection should be possible
listItems->setSelectionMode( QAbstractItemView::NoSelection );
}
}
void QgsStyleV2ManagerDialog::regrouped( QStandardItem *item )
{
StyleEntity type = ( currentItemType() < 3 ) ? SymbolEntity : ColorrampEntity;
if ( currentItemType() > 3 )
{
QgsDebugMsg( "Unknown style entity" );
return;
}
int groupid = groupTree->currentIndex().data( Qt::UserRole + 1 ).toInt();
QString symbolName = item->text();
bool regrouped;
if ( item->checkState() == Qt::Checked )
regrouped = mStyle->group( type, symbolName, groupid );
else
regrouped = mStyle->group( type, symbolName, 0 );
if ( !regrouped )
{
int er = QMessageBox::critical( this, tr( "Database Error"),
tr( "There was a problem with the Symbols database while regrouping." ) );
// call the slot again to get back to normal
if ( er )
groupSymbolsAction();
}
}
void QgsStyleV2ManagerDialog::setSymbolsChecked( QStringList symbols )
{
QStandardItemModel *model = qobject_cast<QStandardItemModel*>( listItems->model() );
foreach( const QString symbol, symbols )
{
QList<QStandardItem*> items = model->findItems( symbol );
foreach( QStandardItem* item, items )
item->setCheckState( Qt::Checked );
}
}
void QgsStyleV2ManagerDialog::filterSymbols( QString qword )
{
QStringList symbols = mStyle->findSymbols( qword );
populateSymbols( symbols );
}
void QgsStyleV2ManagerDialog::tagsChanged()
{
QModelIndexList indexes = listItems->selectionModel()->selection().indexes();
QStringList addtags;