Skip to content

Commit 5d63c35

Browse files
authored
Merge pull request #7764 from opengisch/backport-7760-to-release-4_2
[Backport release-4_2] A couple of extra invokable functions for the MapLayerModel and GeometryUtils classes
2 parents d1fdeca + 55ce54c commit 5d63c35

5 files changed

Lines changed: 105 additions & 14 deletions

File tree

src/core/featurechecklistmodel.cpp

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ QVariant FeatureCheckListModelBase::data( const QModelIndex &index, int role ) c
3131
{
3232
return mCheckedEntries.isEmpty();
3333
}
34+
35+
if ( keyField().isEmpty() )
36+
{
37+
return mCheckedEntries.contains( FeatureListModel::data( index, FeatureListModel::FeatureIdRole ).toString() );
38+
}
39+
3440
return mCheckedEntries.contains( FeatureListModel::data( index, FeatureListModel::KeyFieldRole ).toString() );
3541
}
3642
else
@@ -49,9 +55,13 @@ bool FeatureCheckListModelBase::setData( const QModelIndex &index, const QVarian
4955
case CheckedRole:
5056
{
5157
if ( value.toBool() )
58+
{
5259
setChecked( index );
60+
}
5361
else
62+
{
5463
setUnchecked( index );
64+
}
5565
break;
5666
}
5767
}
@@ -94,14 +104,21 @@ QVariant FeatureCheckListModelBase::attributeValue() const
94104
{
95105
if ( mAllowMulti )
96106
{
97-
if ( mAttributeField.type() == QMetaType::QVariantMap || mAttributeField.type() == QMetaType::QVariantList || mAttributeField.type() == QMetaType::QStringList )
107+
if ( !mAttributeField.name().isEmpty() && ( mAttributeField.type() == QMetaType::QVariantMap || mAttributeField.type() == QMetaType::QVariantList || mAttributeField.type() == QMetaType::QStringList ) )
98108
{
99109
value = vl;
100110
}
101111
else
102112
{
103-
//make string
104-
value = QgsPostgresStringUtils::buildArray( vl );
113+
if ( mAttributeField.name().isEmpty() )
114+
{
115+
value = mCheckedEntries;
116+
}
117+
else
118+
{
119+
//make string
120+
value = QgsPostgresStringUtils::buildArray( vl );
121+
}
105122
}
106123
}
107124
else
@@ -119,7 +136,7 @@ void FeatureCheckListModelBase::setAttributeValue( const QVariant &attributeValu
119136

120137
if ( mAllowMulti )
121138
{
122-
if ( mAttributeField.type() == QMetaType::QVariantMap || mAttributeField.type() == QMetaType::QVariantList || mAttributeField.type() == QMetaType::QStringList )
139+
if ( mAttributeField.name().isEmpty() || ( mAttributeField.type() == QMetaType::QVariantMap || mAttributeField.type() == QMetaType::QVariantList || mAttributeField.type() == QMetaType::QStringList ) )
123140
{
124141
if ( attributeValue.canConvert<QString>() )
125142
{
@@ -152,11 +169,12 @@ void FeatureCheckListModelBase::setAttributeValue( const QVariant &attributeValu
152169
}
153170

154171
if ( mCheckedEntries == checkedEntries )
172+
{
155173
return;
174+
}
156175

157-
beginResetModel();
158176
mCheckedEntries = checkedEntries;
159-
endResetModel();
177+
emit dataChanged( index( 0, 0, QModelIndex() ), index( rowCount() - 1, 0, QModelIndex() ), QList<int>() << CheckedRole );
160178

161179
emit attributeValueChanged();
162180
}
@@ -194,24 +212,30 @@ void FeatureCheckListModelBase::toggleCheckAll( const bool toggleChecked )
194212
if ( toggleChecked )
195213
{
196214
QStringList checkedEntries;
197-
198215
for ( int i = 0; i < rowCount(); i++ )
199-
checkedEntries.append( FeatureListModel::data( createIndex( i, 0 ), FeatureListModel::KeyFieldRole ).toString() );
216+
{
217+
if ( keyField().isEmpty() )
218+
{
219+
checkedEntries.append( FeatureListModel::data( createIndex( i, 0 ), FeatureListModel::FeatureIdRole ).toString() );
220+
}
221+
else
222+
{
223+
checkedEntries.append( FeatureListModel::data( createIndex( i, 0 ), FeatureListModel::KeyFieldRole ).toString() );
224+
}
225+
}
200226

201227
if ( checkedEntries != mCheckedEntries )
202228
{
203-
beginResetModel();
204229
mCheckedEntries = checkedEntries;
205-
endResetModel();
230+
emit dataChanged( index( 0, 0, QModelIndex() ), index( rowCount() - 1, 0, QModelIndex() ), QList<int>() << CheckedRole );
206231
}
207232
}
208233
else
209234
{
210235
if ( !mCheckedEntries.isEmpty() )
211236
{
212-
beginResetModel();
213237
mCheckedEntries = QStringList();
214-
endResetModel();
238+
emit dataChanged( index( 0, 0, QModelIndex() ), index( rowCount() - 1, 0, QModelIndex() ), QList<int>() << CheckedRole );
215239
}
216240
}
217241
}
@@ -225,8 +249,16 @@ void FeatureCheckListModelBase::setChecked( const QModelIndex &idx )
225249
emit dataChanged( index( 0, 0, QModelIndex() ), index( rowCount() - 1, 0, QModelIndex() ), QList<int>() << CheckedRole );
226250
}
227251

228-
mCheckedEntries.append( FeatureListModel::data( idx, FeatureListModel::KeyFieldRole ).toString() );
252+
if ( keyField().isEmpty() )
253+
{
254+
mCheckedEntries.append( FeatureListModel::data( idx, FeatureListModel::FeatureIdRole ).toString() );
255+
}
256+
else
257+
{
258+
mCheckedEntries.append( FeatureListModel::data( idx, FeatureListModel::KeyFieldRole ).toString() );
259+
}
229260
emit dataChanged( idx, idx, QList<int>() << CheckedRole );
261+
230262
if ( addNull() && wasEmpty )
231263
{
232264
QModelIndex nullIdx = index( 0, 0, QModelIndex() );
@@ -238,8 +270,16 @@ void FeatureCheckListModelBase::setChecked( const QModelIndex &idx )
238270
void FeatureCheckListModelBase::setUnchecked( const QModelIndex &idx )
239271
{
240272
const bool wasEmpty = mCheckedEntries.isEmpty();
241-
mCheckedEntries.removeAll( FeatureListModel::data( idx, FeatureListModel::KeyFieldRole ).toString() );
273+
if ( keyField().isEmpty() )
274+
{
275+
mCheckedEntries.removeAll( FeatureListModel::data( idx, FeatureListModel::FeatureIdRole ).toString() );
276+
}
277+
else
278+
{
279+
mCheckedEntries.removeAll( FeatureListModel::data( idx, FeatureListModel::KeyFieldRole ).toString() );
280+
}
242281
emit dataChanged( idx, idx, QList<int>() << CheckedRole );
282+
243283
if ( addNull() && !wasEmpty && mCheckedEntries.isEmpty() )
244284
{
245285
QModelIndex nullIdx = index( 0, 0, QModelIndex() );

src/core/maplayermodel.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,22 @@ int MapLayerModel::findLayer( QgsMapLayer *layer ) const
298298
return -1;
299299
}
300300

301+
int MapLayerModel::findLayerName( const QString &name ) const
302+
{
303+
if ( !name.isEmpty() )
304+
{
305+
QModelIndex startIndex = index( 0, 0 );
306+
QModelIndexList list = match( startIndex, MapLayerModel::NameRole, name );
307+
if ( !list.isEmpty() )
308+
{
309+
QModelIndex index = list[0];
310+
return index.row();
311+
}
312+
}
313+
314+
return -1;
315+
}
316+
301317
QVariantMap MapLayerModel::get( int row ) const
302318
{
303319
QVariantMap data;

src/core/maplayermodel.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@ class MapLayerModel : public QSortFilterProxyModel
142142
*/
143143
Q_INVOKABLE int findLayer( QgsMapLayer *layer ) const;
144144

145+
/**
146+
* Returns the model row for given map layer \a name.
147+
* \note If the map layer name is not found, -1 will be returned
148+
*/
149+
Q_INVOKABLE int findLayerName( const QString &name ) const;
150+
145151
/**
146152
* Returns a model data map for a given \a row.
147153
*/

src/core/utils/geometryutils.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,9 @@ QgsPoint GeometryUtils::reprojectPoint( const QgsPoint &point, const QgsCoordina
433433
QgsRectangle GeometryUtils::reprojectRectangle( const QgsRectangle &rectangle, const QgsCoordinateReferenceSystem &sourceCrs, const QgsCoordinateReferenceSystem &destinationCrs )
434434
{
435435
if ( sourceCrs == destinationCrs )
436+
{
436437
return rectangle;
438+
}
437439

438440
const QgsCoordinateTransform ct( sourceCrs, destinationCrs, QgsProject::instance() );
439441
QgsRectangle reprojectedRectangle;
@@ -449,6 +451,30 @@ QgsRectangle GeometryUtils::reprojectRectangle( const QgsRectangle &rectangle, c
449451
return reprojectedRectangle;
450452
}
451453

454+
QgsGeometry GeometryUtils::reprojectGeometry( const QgsGeometry &geometry, const QgsCoordinateReferenceSystem &sourceCrs, const QgsCoordinateReferenceSystem &destinationCrs )
455+
{
456+
if ( sourceCrs == destinationCrs )
457+
{
458+
return geometry;
459+
}
460+
461+
const QgsCoordinateTransform ct( sourceCrs, destinationCrs, QgsProject::instance() );
462+
QgsGeometry reprojectedGeometry = geometry;
463+
try
464+
{
465+
if ( reprojectedGeometry.transform( ct ) == Qgis::GeometryOperationResult::Success )
466+
{
467+
return reprojectedGeometry;
468+
}
469+
}
470+
catch ( QgsCsException & )
471+
{
472+
return QgsGeometry();
473+
}
474+
475+
return QgsGeometry();
476+
}
477+
452478
QgsGeometry GeometryUtils::createGeometryFromWkt( const QString &wkt )
453479
{
454480
return QgsGeometry::fromWkt( wkt );

src/core/utils/geometryutils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ class QFIELD_CORE_EXPORT GeometryUtils : public QObject
109109
//! Creates a geometry from a WKT string.
110110
static Q_INVOKABLE QgsGeometry createGeometryFromWkt( const QString &wkt );
111111

112+
//! Returns a reprojected \a geometry from the stated \a sourceCrs to a \a destinationCrs.
113+
static Q_INVOKABLE QgsGeometry reprojectGeometry( const QgsGeometry &geometry, const QgsCoordinateReferenceSystem &sourceCrs, const QgsCoordinateReferenceSystem &destinationCrs );
114+
112115
//! Returns the bounding box of a given \a geometry.
113116
static Q_INVOKABLE QgsRectangle boundingBox( const QgsGeometry &geometry ) { return geometry.boundingBox(); }
114117

0 commit comments

Comments
 (0)