QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
26#include "moc_qgsstylegroupselectiondialog.cpp"
27
29 : QDialog( parent )
30 , mStyle( style )
31{
32 setupUi( this );
33
35
36 QStandardItemModel *model = new QStandardItemModel( groupTree );
37 groupTree->setModel( model );
38
39 QStandardItem *favSymbols = new QStandardItem( tr( "Favorites" ) );
40 favSymbols->setData( "favorites", Qt::UserRole + 2 );
41 favSymbols->setEditable( false );
42 setBold( favSymbols );
43 model->appendRow( favSymbols );
44
45 QStandardItem *allSymbols = new QStandardItem( tr( "All" ) );
46 allSymbols->setData( "all", Qt::UserRole + 2 );
47 allSymbols->setEditable( false );
48 setBold( allSymbols );
49 model->appendRow( allSymbols );
50
51 QStandardItem *tags = new QStandardItem( QString() ); //require empty name to get first order groups
52 tags->setData( "tagsheader", Qt::UserRole + 2 );
53 tags->setEditable( false );
54 tags->setFlags( tags->flags() & ~Qt::ItemIsSelectable );
55 buildTagTree( tags );
56 tags->setText( tr( "Tags" ) ); //set title later
57 setBold( tags );
58 model->appendRow( tags );
59
60 QStandardItem *tag = new QStandardItem( tr( "Smart Groups" ) );
61 tag->setData( "smartgroupsheader", Qt::UserRole + 2 );
62 tag->setEditable( false );
63 tag->setFlags( tag->flags() & ~Qt::ItemIsSelectable );
64 setBold( tag );
65 const QgsSymbolGroupMap sgMap = mStyle->smartgroupsListMap();
66 QgsSymbolGroupMap::const_iterator i = sgMap.constBegin();
67 while ( i != sgMap.constEnd() )
68 {
69 QStandardItem *item = new QStandardItem( i.value() );
70 item->setEditable( false );
71 item->setData( i.key() );
72 item->setData( "smartgroup", Qt::UserRole + 2 );
73 tag->appendRow( item );
74 ++i;
75 }
76 model->appendRow( tag );
77
78 // expand things in the group tree
79 const int rows = model->rowCount( model->indexFromItem( model->invisibleRootItem() ) );
80 for ( int i = 0; i < rows; i++ )
81 {
82 groupTree->setExpanded( model->indexFromItem( model->item( i ) ), true );
83 }
84 connect( groupTree->selectionModel(), &QItemSelectionModel::selectionChanged, this, &QgsStyleGroupSelectionDialog::groupTreeSelectionChanged );
85}
86
87void QgsStyleGroupSelectionDialog::setBold( QStandardItem *item )
88{
89 QFont font = item->font();
90 font.setBold( true );
91 item->setFont( font );
92}
93
94void QgsStyleGroupSelectionDialog::groupTreeSelectionChanged( const QItemSelection &selected, const QItemSelection &deselected )
95{
96 const QModelIndexList selectedItems = selected.indexes();
97 const QModelIndexList deselectedItems = deselected.indexes();
98
99 for ( const QModelIndex &index : deselectedItems )
100 {
101 if ( index.data( Qt::UserRole + 2 ).toString() == QLatin1String( "tagssheader" ) )
102 {
103 // Ignore: it's the group header
104 }
105 else if ( index.data( Qt::UserRole + 2 ).toString() == QLatin1String( "favorites" ) )
106 {
107 emit favoritesDeselected();
108 }
109 else if ( index.data( Qt::UserRole + 2 ).toString() == QLatin1String( "all" ) )
110 {
111 emit allDeselected();
112 }
113 else if ( index.data( Qt::UserRole + 2 ).toString() == QLatin1String( "smartgroupsheader" ) )
114 {
115 // Ignore: it's the smartgroups header
116 }
117 else if ( index.data( Qt::UserRole + 2 ).toString() == QLatin1String( "smartgroup" ) )
118 {
119 emit smartgroupDeselected( index.data().toString() );
120 }
121 else if ( index.data( Qt::UserRole + 2 ).toString() == QLatin1String( "tag" ) )
122 {
123 // It's a tag
124 emit tagDeselected( index.data().toString() );
125 }
126 }
127 const auto constSelectedItems = selectedItems;
128 for ( const QModelIndex &index : constSelectedItems )
129 {
130 if ( index.data( Qt::UserRole + 2 ).toString() == QLatin1String( "tagssheader" ) )
131 {
132 // Ignore: it's the group header
133 }
134 else if ( index.data( Qt::UserRole + 2 ).toString() == QLatin1String( "favorites" ) )
135 {
136 emit favoritesSelected();
137 }
138 else if ( index.data( Qt::UserRole + 2 ).toString() == QLatin1String( "all" ) )
139 {
140 emit allSelected();
141 }
142 else if ( index.data( Qt::UserRole + 2 ).toString() == QLatin1String( "smartgroupsheader" ) )
143 {
144 // Ignore: it's the smartgroups header
145 }
146 else if ( index.data( Qt::UserRole + 2 ).toString() == QLatin1String( "smartgroup" ) )
147 {
148 emit smartgroupSelected( index.data().toString() );
149 }
150 else if ( index.data( Qt::UserRole + 2 ).toString() == QLatin1String( "tag" ) )
151 {
152 // It's a tag
153 emit tagSelected( index.data().toString() );
154 }
155 }
156}
157
158void QgsStyleGroupSelectionDialog::buildTagTree( QStandardItem *&parent )
159{
160 QStringList tags = mStyle->tags();
161 tags.sort();
162 const auto constTags = tags;
163 for ( const QString &tag : constTags )
164 {
165 QStandardItem *item = new QStandardItem( tag );
166 item->setData( mStyle->tagId( tag ) );
167 item->setData( "tag", Qt::UserRole + 2 );
168 item->setEditable( false );
169 parent->appendRow( item );
170 }
171}
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:221
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:88
QMap< int, QString > QgsSymbolGroupMap
Definition qgsstyle.h:42