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