QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
25 QgsGeoPdfLayerTreeModel::QgsGeoPdfLayerTreeModel( const QList<QgsMapLayer *> &layers, QObject *parent )
26  : QgsMapLayerModel( layers, parent )
27 {
28  setItemsCanBeReordered( true );
29 }
30 
31 int QgsGeoPdfLayerTreeModel::columnCount( const QModelIndex &parent ) const
32 {
33  Q_UNUSED( parent )
34  return 4;
35 }
36 
37 Qt::ItemFlags QgsGeoPdfLayerTreeModel::flags( const QModelIndex &idx ) const
38 {
39  if ( !idx.isValid() )
40  return Qt::ItemIsDropEnabled;
41 
42  if ( idx.column() == IncludeVectorAttributes )
43  {
44  if ( vectorLayer( idx ) )
45  return QgsMapLayerModel::flags( idx ) | Qt::ItemIsUserCheckable;
46  else
47  return QgsMapLayerModel::flags( idx );
48  }
49 
50  if ( idx.column() == InitiallyVisible )
51  {
52  return QgsMapLayerModel::flags( idx ) | Qt::ItemIsUserCheckable;
53  }
54 
55  if ( !mapLayer( idx ) )
56  {
57  return nullptr;
58  }
59  else
60  {
61  return Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsDragEnabled;
62  }
63  return Qt::NoItemFlags;
64 }
65 
66 QgsMapLayer *QgsGeoPdfLayerTreeModel::mapLayer( const QModelIndex &idx ) const
67 {
68  return layerFromIndex( index( idx.row(), LayerColumn, idx.parent() ) );
69 }
70 
71 QgsVectorLayer *QgsGeoPdfLayerTreeModel::vectorLayer( const QModelIndex &idx ) const
72 {
73  return qobject_cast<QgsVectorLayer *>( mapLayer( idx ) );
74 }
75 
76 QVariant QgsGeoPdfLayerTreeModel::headerData( int section, Qt::Orientation orientation, int role ) const
77 {
78  if ( orientation == Qt::Horizontal )
79  {
80  if ( role == Qt::DisplayRole )
81  {
82  switch ( section )
83  {
84  case LayerColumn:
85  return tr( "Layer" );
86  case GroupColumn:
87  return tr( "PDF Group" );
88  case InitiallyVisible:
89  return tr( "Initially Visible" );
91  return tr( "Include Attributes" );
92  default:
93  return QVariant();
94  }
95  }
96  }
97  return QgsMapLayerModel::headerData( section, orientation, role );
98 }
99 
100 QVariant QgsGeoPdfLayerTreeModel::data( const QModelIndex &idx, int role ) const
101 {
102  switch ( idx.column() )
103  {
104  case LayerColumn:
105  if ( role == Qt::CheckStateRole )
106  return QVariant();
107 
108  return QgsMapLayerModel::data( idx, role );
109 
110  case GroupColumn:
111  {
112  switch ( role )
113  {
114  case Qt::DisplayRole:
115  case Qt::EditRole:
116  {
117  if ( QgsMapLayer *ml = mapLayer( idx ) )
118  {
119  return ml->customProperty( QStringLiteral( "geopdf/groupName" ) ).toString();
120  }
121  break;
122  }
123  }
124 
125  return QVariant();
126  }
127 
128  case InitiallyVisible:
129  {
130  if ( role == Qt::CheckStateRole )
131  {
132  if ( QgsMapLayer *ml = mapLayer( idx ) )
133  {
134  const QVariant v = ml->customProperty( QStringLiteral( "geopdf/initiallyVisible" ) );
135  if ( v.isValid() )
136  {
137  return v.toBool() ? Qt::Checked : Qt::Unchecked;
138  }
139  else
140  {
141  // otherwise, we default to showing by default
142  return Qt::Checked;
143  }
144  }
145  return QVariant();
146  }
147  return QVariant();
148  }
149 
151  {
152  if ( role == Qt::CheckStateRole )
153  {
154  if ( QgsVectorLayer *vl = vectorLayer( idx ) )
155  {
156  const QVariant v = vl->customProperty( QStringLiteral( "geopdf/includeFeatures" ) );
157  if ( v.isValid() )
158  {
159  return v.toBool() ? Qt::Checked : Qt::Unchecked;
160  }
161  else
162  {
163  // otherwise, we default to true
164  return Qt::Checked;
165  }
166  }
167  return QVariant();
168  }
169  }
170  }
171 
172  return QVariant();
173 }
174 
175 bool QgsGeoPdfLayerTreeModel::setData( const QModelIndex &index, const QVariant &value, int role )
176 {
177  switch ( index.column() )
178  {
180  {
181  if ( role == Qt::CheckStateRole )
182  {
183  if ( QgsVectorLayer *vl = vectorLayer( index ) )
184  {
185  vl->setCustomProperty( QStringLiteral( "geopdf/includeFeatures" ), value.toInt() == Qt::Checked );
186  emit dataChanged( index, index );
187  return true;
188  }
189  }
190  break;
191  }
192 
193  case GroupColumn:
194  {
195  if ( role == Qt::EditRole )
196  {
197  if ( QgsMapLayer *ml = mapLayer( index ) )
198  {
199  ml->setCustomProperty( QStringLiteral( "geopdf/groupName" ), value.toString() );
200  emit dataChanged( index, index );
201  return true;
202  }
203  }
204  break;
205  }
206 
207  case InitiallyVisible:
208  {
209  if ( role == Qt::CheckStateRole )
210  {
211  if ( QgsMapLayer *ml = mapLayer( index ) )
212  {
213  ml->setCustomProperty( QStringLiteral( "geopdf/initiallyVisible" ), value.toInt() == Qt::Checked );
214  emit dataChanged( index, index );
215  return true;
216  }
217  }
218  break;
219  }
220 
221  case LayerColumn:
222  return QgsMapLayerModel::setData( index, value, role );
223  }
224  return false;
225 }
226 
227 void QgsGeoPdfLayerTreeModel::checkAll( bool checked, const QModelIndex &parent, int column )
228 {
229  for ( int row = 0; row < rowCount( parent ); ++row )
230  {
231  const QModelIndex childIndex = index( row, column, parent );
232  setData( childIndex, checked ? Qt::Checked : Qt::Unchecked, Qt::CheckStateRole );
233  checkAll( checked, childIndex );
234  }
235 }
236 
237 
239 QgsGeoPdfLayerFilteredTreeModel::QgsGeoPdfLayerFilteredTreeModel( QgsGeoPdfLayerTreeModel *sourceModel, QObject *parent )
240  : QSortFilterProxyModel( parent )
241  , mLayerTreeModel( sourceModel )
242 {
243  setSourceModel( sourceModel );
244 }
245 
246 bool QgsGeoPdfLayerFilteredTreeModel::filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const
247 {
248  if ( QgsMapLayer *layer = mLayerTreeModel->layerFromIndex( sourceModel()->index( source_row, 0, source_parent ) ) )
249  {
250  // filter out non-spatial layers
251  if ( !layer->isSpatial() )
252  return false;
253 
254  }
255  return true;
256 }
257 
QgsMapLayerModel::flags
Qt::ItemFlags flags(const QModelIndex &index) const override
Definition: qgsmaplayermodel.cpp:349
QgsGeoPdfLayerTreeModel::LayerColumn
@ LayerColumn
Layer name.
Definition: qgsgeopdflayertreemodel.h:48
QgsGeoPdfLayerTreeModel::data
QVariant data(const QModelIndex &index, int role) const override
Definition: qgsgeopdflayertreemodel.cpp:100
QgsGeoPdfLayerTreeModel
Definition: qgsgeopdflayertreemodel.h:39
QgsMapLayerModel::data
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Definition: qgsmaplayermodel.cpp:227
QgsGeoPdfLayerTreeModel::QgsGeoPdfLayerTreeModel
QgsGeoPdfLayerTreeModel(const QList< QgsMapLayer * > &layers, QObject *parent=nullptr)
constructor
Definition: qgsgeopdflayertreemodel.cpp:25
QgsGeoPdfLayerTreeModel::IncludeVectorAttributes
@ IncludeVectorAttributes
Vector attribute.
Definition: qgsgeopdflayertreemodel.h:51
QgsGeoPdfLayerTreeModel::GroupColumn
@ GroupColumn
PDF group.
Definition: qgsgeopdflayertreemodel.h:49
QgsMapLayerModel::setData
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole) override
Definition: qgsmaplayermodel.cpp:556
qgsapplication.h
QgsGeoPdfLayerTreeModel::setData
bool setData(const QModelIndex &index, const QVariant &value, int role) override
Definition: qgsgeopdflayertreemodel.cpp:175
QgsMapLayerModel::rowCount
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Definition: qgsmaplayermodel.cpp:212
qgsgeopdflayertreemodel.h
QgsGeoPdfLayerTreeModel::checkAll
void checkAll(bool checked, const QModelIndex &parent=QModelIndex(), int column=IncludeVectorAttributes)
Checks (or unchecks) all rows and children from the specified parent index.
Definition: qgsgeopdflayertreemodel.cpp:227
QgsGeoPdfLayerTreeModel::flags
Qt::ItemFlags flags(const QModelIndex &idx) const override
Definition: qgsgeopdflayertreemodel.cpp:37
QgsGeoPdfLayerTreeModel::headerData
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
Definition: qgsgeopdflayertreemodel.cpp:76
QgsMapLayerModel::setItemsCanBeReordered
void setItemsCanBeReordered(bool allow)
Sets whether items in the model can be reordered via drag and drop.
Definition: qgsmaplayermodel.cpp:46
qgslayertree.h
qgsvectorlayer.h
QgsMapLayerModel::layerFromIndex
QgsMapLayer * layerFromIndex(const QModelIndex &index) const
Returns the map layer corresponding to the specified index.
Definition: qgsmaplayermodel.cpp:116
QgsGeoPdfLayerTreeModel::InitiallyVisible
@ InitiallyVisible
Initial visiblity state.
Definition: qgsgeopdflayertreemodel.h:50
QgsVectorLayer
Definition: qgsvectorlayer.h:385
QgsMapLayer
Definition: qgsmaplayer.h:81
QgsMapLayerModel::index
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Definition: qgsmaplayermodel.cpp:186
QgsGeoPdfLayerTreeModel::columnCount
int columnCount(const QModelIndex &parent) const override
Definition: qgsgeopdflayertreemodel.cpp:31
QgsMapLayerModel::parent
QModelIndex parent(const QModelIndex &child) const override
Definition: qgsmaplayermodel.cpp:205
qgsproject.h
QgsMapLayerModel
The QgsMapLayerModel class is a model to display layers in widgets.
Definition: qgsmaplayermodel.h:36