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