QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
qgsstylegroupselectiondialog.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsstylegroupselectiondialog.h
3 ---------------------
4 begin : Oct 2015
5 copyright : (C) 2015 by Alessandro Pasotti
6 email : elpaso at itopen dot it
7
8 ***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
17
19
20#include "qgsgui.h"
21#include "qgsstyle.h"
22
23#include <QStandardItem>
24#include <QStandardItemModel>
25#include <QString>
26
27#include "moc_qgsstylegroupselectiondialog.cpp"
28
29using namespace Qt::StringLiterals;
30
32 : QDialog( parent )
33 , mStyle( style )
34{
35 setupUi( this );
36
38
39 QStandardItemModel *model = new QStandardItemModel( groupTree );
40 groupTree->setModel( model );
41
42 QStandardItem *favSymbols = new QStandardItem( tr( "Favorites" ) );
43 favSymbols->setData( "favorites", Qt::UserRole + 2 );
44 favSymbols->setEditable( false );
45 setBold( favSymbols );
46 model->appendRow( favSymbols );
47
48 QStandardItem *allSymbols = new QStandardItem( tr( "All" ) );
49 allSymbols->setData( "all", Qt::UserRole + 2 );
50 allSymbols->setEditable( false );
51 setBold( allSymbols );
52 model->appendRow( allSymbols );
53
54 QStandardItem *tags = new QStandardItem( QString() ); //require empty name to get first order groups
55 tags->setData( "tagsheader", Qt::UserRole + 2 );
56 tags->setEditable( false );
57 tags->setFlags( tags->flags() & ~Qt::ItemIsSelectable );
58 buildTagTree( tags );
59 tags->setText( tr( "Tags" ) ); //set title later
60 setBold( tags );
61 model->appendRow( tags );
62
63 QStandardItem *tag = new QStandardItem( tr( "Smart Groups" ) );
64 tag->setData( "smartgroupsheader", Qt::UserRole + 2 );
65 tag->setEditable( false );
66 tag->setFlags( tag->flags() & ~Qt::ItemIsSelectable );
67 setBold( tag );
68 const QgsSymbolGroupMap sgMap = mStyle->smartgroupsListMap();
69 QgsSymbolGroupMap::const_iterator i = sgMap.constBegin();
70 while ( i != sgMap.constEnd() )
71 {
72 QStandardItem *item = new QStandardItem( i.value() );
73 item->setEditable( false );
74 item->setData( i.key() );
75 item->setData( "smartgroup", Qt::UserRole + 2 );
76 tag->appendRow( item );
77 ++i;
78 }
79 model->appendRow( tag );
80
81 // expand things in the group tree
82 const int rows = model->rowCount( model->indexFromItem( model->invisibleRootItem() ) );
83 for ( int i = 0; i < rows; i++ )
84 {
85 groupTree->setExpanded( model->indexFromItem( model->item( i ) ), true );
86 }
87 connect( groupTree->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsStyleGroupSelectionDialog::groupTreeSelectionChanged );
88}
89
90void QgsStyleGroupSelectionDialog::setBold( QStandardItem *item )
91{
92 QFont font = item->font();
93 font.setBold( true );
94 item->setFont( font );
95}
96
97void QgsStyleGroupSelectionDialog::groupTreeSelectionChanged( const QItemSelection &selected, const QItemSelection &deselected )
98{
99 const QModelIndexList selectedItems = selected.indexes();
100 const QModelIndexList deselectedItems = deselected.indexes();
101
102 for ( const QModelIndex &index : deselectedItems )
103 {
104 if ( index.data( Qt::UserRole + 2 ).toString() == "tagssheader"_L1 )
105 {
106 // Ignore: it's the group header
107 }
108 else if ( index.data( Qt::UserRole + 2 ).toString() == "favorites"_L1 )
109 {
110 emit favoritesDeselected();
111 }
112 else if ( index.data( Qt::UserRole + 2 ).toString() == "all"_L1 )
113 {
114 emit allDeselected();
115 }
116 else if ( index.data( Qt::UserRole + 2 ).toString() == "smartgroupsheader"_L1 )
117 {
118 // Ignore: it's the smartgroups header
119 }
120 else if ( index.data( Qt::UserRole + 2 ).toString() == "smartgroup"_L1 )
121 {
122 emit smartgroupDeselected( index.data().toString() );
123 }
124 else if ( index.data( Qt::UserRole + 2 ).toString() == "tag"_L1 )
125 {
126 // It's a tag
127 emit tagDeselected( index.data().toString() );
128 }
129 }
130 const auto constSelectedItems = selectedItems;
131 for ( const QModelIndex &index : constSelectedItems )
132 {
133 if ( index.data( Qt::UserRole + 2 ).toString() == "tagssheader"_L1 )
134 {
135 // Ignore: it's the group header
136 }
137 else if ( index.data( Qt::UserRole + 2 ).toString() == "favorites"_L1 )
138 {
139 emit favoritesSelected();
140 }
141 else if ( index.data( Qt::UserRole + 2 ).toString() == "all"_L1 )
142 {
143 emit allSelected();
144 }
145 else if ( index.data( Qt::UserRole + 2 ).toString() == "smartgroupsheader"_L1 )
146 {
147 // Ignore: it's the smartgroups header
148 }
149 else if ( index.data( Qt::UserRole + 2 ).toString() == "smartgroup"_L1 )
150 {
151 emit smartgroupSelected( index.data().toString() );
152 }
153 else if ( index.data( Qt::UserRole + 2 ).toString() == "tag"_L1 )
154 {
155 // It's a tag
156 emit tagSelected( index.data().toString() );
157 }
158 }
159}
160
161void QgsStyleGroupSelectionDialog::buildTagTree( QStandardItem *&parent )
162{
163 QStringList tags = mStyle->tags();
164 tags.sort();
165 const auto constTags = tags;
166 for ( const QString &tag : constTags )
167 {
168 QStandardItem *item = new QStandardItem( tag );
169 item->setData( mStyle->tagId( tag ) );
170 item->setData( "tag", Qt::UserRole + 2 );
171 item->setEditable( false );
172 parent->appendRow( item );
173 }
174}
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
Definition qgsgui.cpp:224
void favoritesDeselected()
Favorites has been deselected.
void allDeselected()
all deselected
void tagSelected(const QString &tagName)
tag with tagName has been selected
QgsStyleGroupSelectionDialog(QgsStyle *style, QWidget *parent=nullptr)
void setBold(QStandardItem *item)
Sets bold font for item.
void tagDeselected(const QString &tagName)
tag with tagName has been deselected
void smartgroupDeselected(const QString &groupName)
smart group with groupName has been deselected
void favoritesSelected()
Favorites has need selected.
void smartgroupSelected(const QString &groupName)
smartgroup with groupName has been selected
void allSelected()
all selected
A database of saved style entities, including symbols, color ramps, text formats and others.
Definition qgsstyle.h:89
QMap< int, QString > QgsSymbolGroupMap
Definition qgsstyle.h:42