QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgsstylev2groupselectiondialog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsstylev2groupselectiondialog.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 "qgsstylev2.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 *group = new QStandardItem( "" ); //require empty name to get first order groups
41  group->setData( "groupsheader", Qt::UserRole + 2 );
42  group->setEditable( false );
43  group->setFlags( group->flags() & ~Qt::ItemIsSelectable );
44  buildGroupTree( group );
45  group->setText( tr( "Groups" ) );//set title later
46  QStandardItem *ungrouped = new QStandardItem( tr( "Ungrouped" ) );
47  ungrouped->setData( 0 );
48  ungrouped->setData( "group", Qt::UserRole + 2 );
49  setBold( ungrouped );
50  setBold( group );
51  group->appendRow( ungrouped );
52  model->appendRow( group );
53 
54  QStandardItem *tag = new QStandardItem( tr( "Smart Groups" ) );
55  tag->setData( "smartgroupsheader" , Qt::UserRole + 2 );
56  tag->setEditable( false );
57  tag->setFlags( group->flags() & ~Qt::ItemIsSelectable );
58  setBold( tag );
59  QgsSymbolGroupMap sgMap = mStyle->smartgroupsListMap();
61  while ( i != sgMap.constEnd() )
62  {
63  QStandardItem *item = new QStandardItem( i.value() );
64  item->setEditable( false );
65  item->setData( i.key() );
66  item->setData( "smartgroup" , Qt::UserRole + 2 );
67  tag->appendRow( item );
68  ++i;
69  }
70  model->appendRow( tag );
71 
72  // expand things in the group tree
73  int rows = model->rowCount( model->indexFromItem( model->invisibleRootItem() ) );
74  for ( int i = 0; i < rows; i++ )
75  {
76  groupTree->setExpanded( model->indexFromItem( model->item( i ) ), true );
77  }
78  connect( groupTree->selectionModel(), SIGNAL( selectionChanged( const QItemSelection&, const QItemSelection& ) ), this, SLOT( groupTreeSelectionChanged( const QItemSelection&, const QItemSelection& ) ) );
79 }
80 
81 
83 {
84 }
85 
86 
88 {
89  QFont font = item->font();
90  font.setBold( true );
91  item->setFont( font );
92 }
93 
94 
95 void QgsStyleV2GroupSelectionDialog::groupTreeSelectionChanged( const QItemSelection &selected, const QItemSelection &deselected )
96 {
98  QModelIndexList selectedItems = selected.indexes();
99  QModelIndexList deselectedItems = deselected.indexes();
100 
101  Q_FOREACH ( index, deselectedItems )
102  {
103  if ( index.data( Qt::UserRole + 2 ).toString() == "groupsheader" )
104  {
105  // Ignore: it's the group header
106  }
107  else if ( index.data( Qt::UserRole + 2 ).toString() == "all" )
108  {
109  emit allDeselected();
110  }
111  else if ( index.data( Qt::UserRole + 2 ).toString() == "smartgroupsheader" )
112  {
113  // Ignore: it's the smartgroups header
114  }
115  else if ( index.data( Qt::UserRole + 2 ).toString() == "smartgroup" )
116  {
117  emit smartgroupDeselected( index.data().toString() );
118  }
119  else if ( index.data( Qt::UserRole + 2 ).toString() == "group" )
120  { // It's a group
121  emit groupDeselected( index.data().toString() );
122  }
123  }
124  Q_FOREACH ( index, selectedItems )
125  {
126  if ( index.data( Qt::UserRole + 2 ).toString() == "groupsheader" )
127  {
128  // Ignore: it's the group header
129  }
130  else if ( index.data( Qt::UserRole + 2 ).toString() == "all" )
131  {
132  emit allSelected();
133  }
134  else if ( index.data( Qt::UserRole + 2 ).toString() == "smartgroupsheader" )
135  {
136  // Ignore: it's the smartgroups header
137  }
138  else if ( index.data( Qt::UserRole + 2 ).toString() == "smartgroup" )
139  {
140  emit smartgroupSelected( index.data().toString() );
141  }
142  else if ( index.data( Qt::UserRole + 2 ).toString() == "group" )
143  { // It's a group
144  emit groupSelected( index.data().toString() );
145  }
146  }
147 }
148 
149 
150 void QgsStyleV2GroupSelectionDialog::buildGroupTree( QStandardItem* &parent )
151 {
152  QgsSymbolGroupMap groups = mStyle->childGroupNames( parent->text() );
154  while ( i != groups.constEnd() )
155  {
156  QStandardItem *item = new QStandardItem( i.value() );
157  item->setData( i.key() );
158  item->setData( "group" , Qt::UserRole + 2 );
159  item->setEditable( false );
160  parent->appendRow( item );
161  buildGroupTree( item );
162  ++i;
163  }
164 }
165 
QgsStyleV2GroupSelectionDialog(QgsStyleV2 *style, QWidget *parent=nullptr)
QModelIndexList indexes() const
static unsigned index
void setupUi(QWidget *widget)
QStandardItem * invisibleRootItem() const
void groupSelected(const QString &groupName)
group with groupName has been selected
Qt::ItemFlags flags() const
void groupDeselected(const QString &groupName)
group with groupName has been deselected
const_iterator constBegin() const
void smartgroupDeselected(const QString &groupName)
smart group with groupName has been deselected
QString tr(const char *sourceText, const char *disambiguation, int n)
QString text() const
virtual void setData(const QVariant &value, int role)
void setBold(bool enable)
QgsSymbolGroupMap childGroupNames(const QString &parent="")
return a map of groupid and names for the given parent group
Definition: qgsstylev2.cpp:491
QModelIndex indexFromItem(const QStandardItem *item) const
const Key & key() const
void appendRow(const QList< QStandardItem * > &items)
void setFlags(QFlags< Qt::ItemFlag > flags)
const_iterator constEnd() const
const T & value() const
QgsSymbolGroupMap smartgroupsListMap()
returns the smart groups map with id as key and name as value
void setFont(const QFont &font)
QStandardItem * item(int row, int column) const
void setBold(QStandardItem *item)
Set bold font for item.
const QFont & font() const
void setText(const QString &text)
QFont font() const
QVariant data(int role) const
void allDeselected()
all deselected
void allSelected()
all selected
virtual int rowCount(const QModelIndex &parent) const
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const
QString toString() const
void appendRow(const QList< QStandardItem * > &items)
void setEditable(bool editable)
void smartgroupSelected(const QString &groupName)
smartgroup with groupName has been selected