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