QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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  QModelIndex index;
90  QModelIndexList selectedItems = selected.indexes();
91  QModelIndexList deselectedItems = deselected.indexes();
92 
93  Q_FOREACH ( index, deselectedItems )
94  {
95  if ( index.data( Qt::UserRole + 2 ).toString() == QLatin1String( "tagssheader" ) )
96  {
97  // Ignore: it's the group header
98  }
99  else if ( index.data( Qt::UserRole + 2 ).toString() == QLatin1String( "all" ) )
100  {
101  emit allDeselected();
102  }
103  else if ( index.data( Qt::UserRole + 2 ).toString() == QLatin1String( "smartgroupsheader" ) )
104  {
105  // Ignore: it's the smartgroups header
106  }
107  else if ( index.data( Qt::UserRole + 2 ).toString() == QLatin1String( "smartgroup" ) )
108  {
109  emit smartgroupDeselected( index.data().toString() );
110  }
111  else if ( index.data( Qt::UserRole + 2 ).toString() == QLatin1String( "tag" ) )
112  {
113  // It's a tag
114  emit tagDeselected( index.data().toString() );
115  }
116  }
117  Q_FOREACH ( index, selectedItems )
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  Q_FOREACH ( const QString &tag, tags )
149  {
150  QStandardItem *item = new QStandardItem( tag );
151  item->setData( mStyle->tagId( tag ) );
152  item->setData( "tag", Qt::UserRole + 2 );
153  item->setEditable( false );
154  parent->appendRow( item );
155  }
156 }
157 
QMap< int, QString > QgsSymbolGroupMap
Definition: qgsstyle.h:38
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:1216
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:98
void smartgroupDeselected(const QString &groupName)
smart group with groupName has been deselected
QgsStyleGroupSelectionDialog(QgsStyle *style, QWidget *parent=nullptr)
QStringList tags() const
Returns a list of all tags in the style database.
Definition: qgsstyle.cpp:647
QgsSymbolGroupMap smartgroupsListMap()
Returns the smart groups map with id as key and name as value.
Definition: qgsstyle.cpp:1278
void tagDeselected(const QString &tagName)
tag with tagName has been deselected