QGIS API Documentation  3.12.1-BucureČ™ti (121cc00ff0)
qgsgeopdflayertreemodel.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsgeopdflayertreemodel.cpp
3  ---------------------
4  begin : August 2019
5  copyright : (C) 2019 by Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include <QComboBox>
17 #include <QDoubleSpinBox>
18 
20 #include "qgslayertree.h"
21 #include "qgsproject.h"
22 #include "qgsvectorlayer.h"
23 #include "qgsapplication.h"
24 
26  : QgsLayerTreeModel( rootNode, parent )
27 {
28  setFlags( nullptr ); // ideally we'd just show embedded legend nodes - but the api doesn't exist for this
29 }
30 
31 int QgsGeoPdfLayerTreeModel::columnCount( const QModelIndex &parent ) const
32 {
33  Q_UNUSED( parent )
34  return 2;
35 }
36 
37 Qt::ItemFlags QgsGeoPdfLayerTreeModel::flags( const QModelIndex &idx ) const
38 {
39  if ( idx.column() == LayerColumn )
40  {
41  return QgsLayerTreeModel::flags( idx ) | Qt::ItemIsUserCheckable;
42  }
43 
44  QgsVectorLayer *vl = vectorLayer( idx );
45  if ( !vl )
46  {
47  return Qt::NoItemFlags;
48  }
49  else
50  {
51  const QModelIndex layerIndex = sibling( idx.row(), LayerColumn, idx );
52  if ( data( layerIndex, Qt::CheckStateRole ) == Qt::Checked )
53  {
54  return Qt::ItemIsEnabled | Qt::ItemIsEditable;
55  }
56  }
57  return Qt::NoItemFlags;
58 }
59 
60 QgsVectorLayer *QgsGeoPdfLayerTreeModel::vectorLayer( const QModelIndex &idx ) const
61 {
62  QgsLayerTreeNode *node = index2node( index( idx.row(), LayerColumn, idx.parent() ) );
63  if ( !node || !QgsLayerTree::isLayer( node ) )
64  return nullptr;
65 
66  return qobject_cast<QgsVectorLayer *>( QgsLayerTree::toLayer( node )->layer() );
67 }
68 
69 QVariant QgsGeoPdfLayerTreeModel::headerData( int section, Qt::Orientation orientation, int role ) const
70 {
71  if ( orientation == Qt::Horizontal )
72  {
73  if ( role == Qt::DisplayRole )
74  {
75  switch ( section )
76  {
77  case 0:
78  return tr( "Layer" );
79  case 1:
80  return tr( "PDF Group" );
81  default:
82  return QVariant();
83  }
84  }
85  }
86  return QgsLayerTreeModel::headerData( section, orientation, role );
87 }
88 
89 QVariant QgsGeoPdfLayerTreeModel::data( const QModelIndex &idx, int role ) const
90 {
91  switch ( idx.column() )
92  {
93  case LayerColumn:
94  {
95  if ( role == Qt::CheckStateRole )
96  {
97  QgsLayerTreeNode *node = index2node( index( idx.row(), LayerColumn, idx.parent() ) );
98  QgsVectorLayer *vl = vectorLayer( idx );
99  if ( vl )
100  {
101  const QVariant v = vl->customProperty( QStringLiteral( "geopdf/includeFeatures" ) );
102  if ( v.isValid() )
103  {
104  return v.toBool() ? Qt::Checked : Qt::Unchecked;
105  }
106  else
107  {
108  // otherwise, we default to the layer's visibility
109  return node->itemVisibilityChecked() ? Qt::Checked : Qt::Unchecked;
110  }
111  }
112  return QVariant();
113  }
114  return QgsLayerTreeModel::data( idx, role );
115  }
116  case GroupColumn:
117  {
118  switch ( role )
119  {
120  case Qt::DisplayRole:
121  case Qt::EditRole:
122  {
123  if ( QgsVectorLayer *vl = vectorLayer( idx ) )
124  {
125  return vl->customProperty( QStringLiteral( "geopdf/groupName" ) ).toString();
126  }
127  break;
128  }
129  }
130 
131  return QVariant();
132  }
133  }
134 
135  return QVariant();
136 }
137 
138 bool QgsGeoPdfLayerTreeModel::setData( const QModelIndex &index, const QVariant &value, int role )
139 {
140  switch ( index.column() )
141  {
142  case LayerColumn:
143  {
144  if ( role == Qt::CheckStateRole )
145  {
146  if ( QgsVectorLayer *vl = vectorLayer( index ) )
147  {
148  vl->setCustomProperty( QStringLiteral( "geopdf/includeFeatures" ), value.toInt() == Qt::Checked );
149  emit dataChanged( index, index );
150  return true;
151  }
152  }
153  break;
154  }
155 
156  case GroupColumn:
157  {
158  if ( role == Qt::EditRole )
159  {
160  if ( QgsVectorLayer *vl = vectorLayer( index ) )
161  {
162  vl->setCustomProperty( QStringLiteral( "geopdf/groupName" ), value.toString() );
163  emit dataChanged( index, index );
164  return true;
165  }
166  }
167  break;
168  }
169  }
170  return false;
171 }
QModelIndex parent(const QModelIndex &child) const override
static QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer.
Definition: qgslayertree.h:75
bool itemVisibilityChecked() const
Returns whether a node is checked (independently of its ancestors or children)
QVariant data(const QModelIndex &index, int role) const override
bool setData(const QModelIndex &index, const QVariant &value, int role) override
void setFlags(QgsLayerTreeModel::Flags f)
Sets OR-ed combination of model flags.
The QgsLayerTreeModel class is model implementation for Qt item views framework.
Namespace with helper functions for layer tree operations.
Definition: qgslayertree.h:32
QgsGeoPdfLayerTreeModel(QgsLayerTree *rootNode, QObject *parent=nullptr)
constructor
static bool isLayer(const QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
Definition: qgslayertree.h:53
This class is a base class for nodes in a layer tree.
QgsMapLayer * layer() const
Returns the map layer associated with this node.
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
QgsLayerTreeNode * index2node(const QModelIndex &index) const
Returns layer tree node for given index.
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
int columnCount(const QModelIndex &parent) const override
Flags flags() const
Returns OR-ed combination of model flags.
Q_INVOKABLE QVariant customProperty(const QString &value, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer.
Represents a vector layer which manages a vector based data sets.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override