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