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