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