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