41
41
#include " qgstexteditwidgetfactory.h"
42
42
#include " qgsexpressioncontextutils.h"
43
43
#include " qgsvectorlayerutils.h"
44
- #include " qgsvectorlayercache.h"
45
44
46
45
#include < QVariant>
47
46
#include < QUuid>
50
49
51
50
QgsAttributeTableModel::QgsAttributeTableModel ( QgsVectorLayerCache *layerCache, QObject *parent )
52
51
: QAbstractTableModel( parent )
53
- , mLayer( layerCache->layer () )
54
52
, mLayerCache( layerCache )
55
53
{
56
54
mExpressionContext .appendScopes ( QgsExpressionContextUtils::globalProjectLayerScopes ( layerCache->layer () ) );
57
55
58
- if ( mLayer ->geometryType () == QgsWkbTypes::NullGeometry )
56
+ if ( layerCache-> layer () ->geometryType () == QgsWkbTypes::NullGeometry )
59
57
{
60
58
mFeatureRequest .setFlags ( QgsFeatureRequest::NoGeometry );
61
59
}
62
60
63
61
mFeat .setId ( std::numeric_limits<int >::min () );
64
62
65
- if ( !mLayer ->isSpatial () )
63
+ if ( !layer () ->isSpatial () )
66
64
mFeatureRequest .setFlags ( QgsFeatureRequest::NoGeometry );
67
65
68
66
loadAttributes ();
69
67
70
- connect ( mLayer , &QgsVectorLayer::featuresDeleted, this , &QgsAttributeTableModel::featuresDeleted );
71
- connect ( mLayer , &QgsVectorLayer::attributeDeleted, this , &QgsAttributeTableModel::attributeDeleted );
72
- connect ( mLayer , &QgsVectorLayer::updatedFields, this , &QgsAttributeTableModel::updatedFields );
68
+ connect ( layer () , &QgsVectorLayer::featuresDeleted, this , &QgsAttributeTableModel::featuresDeleted );
69
+ connect ( layer () , &QgsVectorLayer::attributeDeleted, this , &QgsAttributeTableModel::attributeDeleted );
70
+ connect ( layer () , &QgsVectorLayer::updatedFields, this , &QgsAttributeTableModel::updatedFields );
73
71
74
- connect ( mLayer , &QgsVectorLayer::editCommandStarted, this , &QgsAttributeTableModel::bulkEditCommandStarted );
75
- connect ( mLayer , &QgsVectorLayer::beforeRollBack, this , &QgsAttributeTableModel::bulkEditCommandStarted );
76
- connect ( mLayer , &QgsVectorLayer::afterRollBack, this , &QgsAttributeTableModel::bulkEditCommandEnded );
72
+ connect ( layer () , &QgsVectorLayer::editCommandStarted, this , &QgsAttributeTableModel::bulkEditCommandStarted );
73
+ connect ( layer () , &QgsVectorLayer::beforeRollBack, this , &QgsAttributeTableModel::bulkEditCommandStarted );
74
+ connect ( layer () , &QgsVectorLayer::afterRollBack, this , &QgsAttributeTableModel::bulkEditCommandEnded );
77
75
78
- connect ( mLayer , &QgsVectorLayer::editCommandEnded, this , &QgsAttributeTableModel::editCommandEnded );
76
+ connect ( layer () , &QgsVectorLayer::editCommandEnded, this , &QgsAttributeTableModel::editCommandEnded );
79
77
connect ( mLayerCache , &QgsVectorLayerCache::attributeValueChanged, this , &QgsAttributeTableModel::attributeValueChanged );
80
78
connect ( mLayerCache , &QgsVectorLayerCache::featureAdded, this , [ = ]( QgsFeatureId id ) { featureAdded ( id ); } );
81
79
connect ( mLayerCache , &QgsVectorLayerCache::cachedLayerDeleted, this , &QgsAttributeTableModel::layerDeleted );
80
+
82
81
}
83
82
84
83
bool QgsAttributeTableModel::loadFeatureAtId ( QgsFeatureId fid ) const
@@ -234,7 +233,7 @@ void QgsAttributeTableModel::featureAdded( QgsFeatureId fid )
234
233
QgsFieldFormatter *fieldFormatter = mFieldFormatters .at ( cache.sortFieldIndex );
235
234
const QVariant &widgetCache = mAttributeWidgetCaches .at ( cache.sortFieldIndex );
236
235
const QVariantMap &widgetConfig = mWidgetConfigs .at ( cache.sortFieldIndex );
237
- QVariant sortValue = fieldFormatter->representValue ( mLayer , cache.sortFieldIndex , widgetConfig, widgetCache, mFeat .attribute ( cache.sortFieldIndex ) );
236
+ QVariant sortValue = fieldFormatter->representValue ( layer () , cache.sortFieldIndex , widgetConfig, widgetCache, mFeat .attribute ( cache.sortFieldIndex ) );
238
237
cache.sortCache .insert ( mFeat .id (), sortValue );
239
238
}
240
239
else if ( cache.sortCacheExpression .isValid () )
@@ -288,7 +287,6 @@ void QgsAttributeTableModel::attributeDeleted( int idx )
288
287
void QgsAttributeTableModel::layerDeleted ()
289
288
{
290
289
mLayerCache = nullptr ;
291
- mLayer = nullptr ;
292
290
removeRows ( 0 , rowCount () );
293
291
294
292
mAttributeWidgetCaches .clear ();
@@ -334,7 +332,7 @@ void QgsAttributeTableModel::attributeValueChanged( QgsFeatureId fid, int idx, c
334
332
QgsFieldFormatter *fieldFormatter = mFieldFormatters .at ( cache.sortFieldIndex );
335
333
const QVariant &widgetCache = mAttributeWidgetCaches .at ( cache.sortFieldIndex );
336
334
const QVariantMap &widgetConfig = mWidgetConfigs .at ( cache.sortFieldIndex );
337
- QVariant sortValue = fieldFormatter->representValue ( mLayer , cache.sortFieldIndex , widgetConfig, widgetCache, value );
335
+ QVariant sortValue = fieldFormatter->representValue ( layer () , cache.sortFieldIndex , widgetConfig, widgetCache, value );
338
336
cache.sortCache .insert ( fid, sortValue );
339
337
}
340
338
}
@@ -377,29 +375,29 @@ void QgsAttributeTableModel::attributeValueChanged( QgsFeatureId fid, int idx, c
377
375
378
376
void QgsAttributeTableModel::loadAttributes ()
379
377
{
380
- if ( !mLayer )
378
+ if ( !layer () )
381
379
{
382
380
return ;
383
381
}
384
382
385
383
bool ins = false , rm = false ;
386
384
387
385
QgsAttributeList attributes;
388
- const QgsFields &fields = mLayer ->fields ();
386
+ const QgsFields &fields = layer () ->fields ();
389
387
390
388
mWidgetFactories .clear ();
391
389
mAttributeWidgetCaches .clear ();
392
390
mWidgetConfigs .clear ();
393
391
394
392
for ( int idx = 0 ; idx < fields.count (); ++idx )
395
393
{
396
- const QgsEditorWidgetSetup setup = QgsGui::editorWidgetRegistry ()->findBest ( mLayer , fields[idx].name () );
394
+ const QgsEditorWidgetSetup setup = QgsGui::editorWidgetRegistry ()->findBest ( layer () , fields[idx].name () );
397
395
QgsEditorWidgetFactory *widgetFactory = QgsGui::editorWidgetRegistry ()->factory ( setup.type () );
398
396
QgsFieldFormatter *fieldFormatter = QgsApplication::fieldFormatterRegistry ()->fieldFormatter ( setup.type () );
399
397
400
398
mWidgetFactories .append ( widgetFactory );
401
399
mWidgetConfigs .append ( setup.config () );
402
- mAttributeWidgetCaches .append ( fieldFormatter->createCache ( mLayer , idx, setup.config () ) );
400
+ mAttributeWidgetCaches .append ( fieldFormatter->createCache ( layer () , idx, setup.config () ) );
403
401
mFieldFormatters .append ( fieldFormatter );
404
402
405
403
attributes << idx;
@@ -497,7 +495,7 @@ void QgsAttributeTableModel::fieldConditionalStyleChanged( const QString &fieldN
497
495
return ;
498
496
}
499
497
500
- int fieldIndex = mLayer ->fields ().lookupField ( fieldName );
498
+ int fieldIndex = mLayerCache -> layer () ->fields ().lookupField ( fieldName );
501
499
if ( fieldIndex == -1 )
502
500
return ;
503
501
@@ -598,7 +596,7 @@ int QgsAttributeTableModel::columnCount( const QModelIndex &parent ) const
598
596
599
597
QVariant QgsAttributeTableModel::headerData ( int section, Qt::Orientation orientation, int role ) const
600
598
{
601
- if ( !mLayer )
599
+ if ( !layer () )
602
600
return QVariant ();
603
601
604
602
if ( role == Qt::DisplayRole )
@@ -609,7 +607,7 @@ QVariant QgsAttributeTableModel::headerData( int section, Qt::Orientation orient
609
607
}
610
608
else if ( section >= 0 && section < mFieldCount )
611
609
{
612
- QString attributeName = mLayer ->fields ().at ( mAttributes .at ( section ) ).displayName ();
610
+ QString attributeName = layer () ->fields ().at ( mAttributes .at ( section ) ).displayName ();
613
611
return QVariant ( attributeName );
614
612
}
615
613
else
@@ -626,8 +624,8 @@ QVariant QgsAttributeTableModel::headerData( int section, Qt::Orientation orient
626
624
}
627
625
else
628
626
{
629
- const QgsField field = mLayer ->fields ().at ( mAttributes .at ( section ) );
630
- return QgsFieldModel::fieldToolTipExtended ( field, mLayer );
627
+ const QgsField field = layer () ->fields ().at ( mAttributes .at ( section ) );
628
+ return QgsFieldModel::fieldToolTipExtended ( field, layer () );
631
629
}
632
630
}
633
631
else
@@ -638,7 +636,7 @@ QVariant QgsAttributeTableModel::headerData( int section, Qt::Orientation orient
638
636
639
637
QVariant QgsAttributeTableModel::data ( const QModelIndex &index, int role ) const
640
638
{
641
- if ( !index.isValid () || !mLayer ||
639
+ if ( !index.isValid () || !layer () ||
642
640
( role != Qt::TextAlignmentRole
643
641
&& role != Qt::DisplayRole
644
642
&& role != Qt::ToolTipRole
@@ -681,11 +679,11 @@ QVariant QgsAttributeTableModel::data( const QModelIndex &index, int role ) cons
681
679
return QVariant ();
682
680
}
683
681
684
- QgsField field = mLayer ->fields ().at ( fieldId );
682
+ QgsField field = layer () ->fields ().at ( fieldId );
685
683
686
684
if ( role == Qt::TextAlignmentRole )
687
685
{
688
- return QVariant ( mFieldFormatters .at ( index.column () )->alignmentFlag ( mLayer , fieldId, mWidgetConfigs .at ( index.column () ) ) | Qt::AlignVCenter );
686
+ return QVariant ( mFieldFormatters .at ( index.column () )->alignmentFlag ( layer () , fieldId, mWidgetConfigs .at ( index.column () ) ) | Qt::AlignVCenter );
689
687
}
690
688
691
689
if ( mFeat .id () != rowId || !mFeat .isValid () )
@@ -703,7 +701,7 @@ QVariant QgsAttributeTableModel::data( const QModelIndex &index, int role ) cons
703
701
{
704
702
case Qt::DisplayRole:
705
703
case Qt::ToolTipRole:
706
- return mFieldFormatters .at ( index.column () )->representValue ( mLayer ,
704
+ return mFieldFormatters .at ( index.column () )->representValue ( layer () ,
707
705
fieldId,
708
706
mWidgetConfigs .at ( index.column () ),
709
707
mAttributeWidgetCaches .at ( index.column () ),
@@ -729,12 +727,12 @@ QVariant QgsAttributeTableModel::data( const QModelIndex &index, int role ) cons
729
727
}
730
728
else
731
729
{
732
- styles = QgsConditionalStyle::matchingConditionalStyles ( mLayer ->conditionalStyles ()->rowStyles (), QVariant (), mExpressionContext );
730
+ styles = QgsConditionalStyle::matchingConditionalStyles ( layer () ->conditionalStyles ()->rowStyles (), QVariant (), mExpressionContext );
733
731
mRowStylesMap .insert ( mFeat .id (), styles );
734
732
}
735
733
736
734
QgsConditionalStyle rowstyle = QgsConditionalStyle::compressStyles ( styles );
737
- styles = mLayer ->conditionalStyles ()->fieldStyles ( field.name () );
735
+ styles = layer () ->conditionalStyles ()->fieldStyles ( field.name () );
738
736
styles = QgsConditionalStyle::matchingConditionalStyles ( styles, val, mExpressionContext );
739
737
styles.insert ( 0 , rowstyle );
740
738
QgsConditionalStyle style = QgsConditionalStyle::compressStyles ( styles );
@@ -766,12 +764,12 @@ bool QgsAttributeTableModel::setData( const QModelIndex &index, const QVariant &
766
764
{
767
765
Q_UNUSED ( value )
768
766
769
- if ( !index.isValid () || index.column () >= mFieldCount || role != Qt::EditRole || !mLayer ->isEditable () )
767
+ if ( !index.isValid () || index.column () >= mFieldCount || role != Qt::EditRole || !layer () ->isEditable () )
770
768
return false ;
771
769
772
770
mRowStylesMap .remove ( mFeat .id () );
773
771
774
- if ( !mLayer ->isModified () )
772
+ if ( !layer () ->isModified () )
775
773
return false ;
776
774
777
775
return true ;
@@ -782,15 +780,15 @@ Qt::ItemFlags QgsAttributeTableModel::flags( const QModelIndex &index ) const
782
780
if ( !index.isValid () )
783
781
return Qt::ItemIsEnabled;
784
782
785
- if ( index.column () >= mFieldCount || !mLayer )
783
+ if ( index.column () >= mFieldCount || !layer () )
786
784
return Qt::NoItemFlags;
787
785
788
786
Qt::ItemFlags flags = QAbstractTableModel::flags ( index );
789
787
790
788
const int fieldIndex = mAttributes [index.column ()];
791
789
const QgsFeatureId fid = rowToId ( index.row () );
792
790
793
- if ( QgsVectorLayerUtils::fieldIsEditable ( mLayer , fieldIndex, fid ) )
791
+ if ( QgsVectorLayerUtils::fieldIsEditable ( layer () , fieldIndex, fid ) )
794
792
flags |= Qt::ItemIsEditable;
795
793
796
794
return flags;
@@ -821,7 +819,7 @@ void QgsAttributeTableModel::bulkEditCommandEnded()
821
819
if ( fullModelUpdate )
822
820
{
823
821
// Invalidates the cache (there is no API for doing this directly)
824
- emit mLayer ->dataChanged ();
822
+ emit mLayerCache -> layer () ->dataChanged ();
825
823
emit dataChanged ( createIndex ( 0 , 0 ), createIndex ( rowCount () - 1 , columnCount () - 1 ) );
826
824
}
827
825
else
@@ -856,18 +854,18 @@ void QgsAttributeTableModel::reload( const QModelIndex &index1, const QModelInde
856
854
void QgsAttributeTableModel::executeAction ( QUuid action, const QModelIndex &idx ) const
857
855
{
858
856
QgsFeature f = feature ( idx );
859
- mLayer ->actions ()->doAction ( action, f, fieldIdx ( idx.column () ) );
857
+ layer () ->actions ()->doAction ( action, f, fieldIdx ( idx.column () ) );
860
858
}
861
859
862
860
void QgsAttributeTableModel::executeMapLayerAction ( QgsMapLayerAction *action, const QModelIndex &idx ) const
863
861
{
864
862
QgsFeature f = feature ( idx );
865
- action->triggerForFeature ( mLayer , f );
863
+ action->triggerForFeature ( layer () , f );
866
864
}
867
865
868
866
QgsFeature QgsAttributeTableModel::feature ( const QModelIndex &idx ) const
869
867
{
870
- QgsFeature f ( mLayer ->fields () );
868
+ QgsFeature f ( mLayerCache -> layer () ->fields () );
871
869
f.initAttributes ( mAttributes .size () );
872
870
f.setId ( rowToId ( idx.row () ) );
873
871
for ( int i = 0 ; i < mAttributes .size (); i++ )
@@ -886,7 +884,7 @@ void QgsAttributeTableModel::prefetchColumnData( int column )
886
884
}
887
885
else
888
886
{
889
- prefetchSortData ( QgsExpression::quotedColumnRef ( mLayer ->fields ().at ( mAttributes .at ( column ) ).name () ) );
887
+ prefetchSortData ( QgsExpression::quotedColumnRef ( mLayerCache -> layer () ->fields ().at ( mAttributes .at ( column ) ).name () ) );
890
888
}
891
889
}
892
890
@@ -916,7 +914,7 @@ void QgsAttributeTableModel::prefetchSortData( const QString &expressionString,
916
914
if ( cache.sortCacheExpression .isField () )
917
915
{
918
916
QString fieldName = static_cast <const QgsExpressionNodeColumnRef *>( cache.sortCacheExpression .rootNode () )->name ();
919
- cache.sortFieldIndex = mLayer ->fields ().lookupField ( fieldName );
917
+ cache.sortFieldIndex = mLayerCache -> layer () ->fields ().lookupField ( fieldName );
920
918
}
921
919
922
920
if ( cache.sortFieldIndex == -1 )
@@ -927,7 +925,7 @@ void QgsAttributeTableModel::prefetchSortData( const QString &expressionString,
927
925
928
926
for ( const QString &col : referencedColumns )
929
927
{
930
- cache.sortCacheAttributes .append ( mLayer ->fields ().lookupField ( col ) );
928
+ cache.sortCacheAttributes .append ( mLayerCache -> layer () ->fields ().lookupField ( col ) );
931
929
}
932
930
}
933
931
else
@@ -955,7 +953,7 @@ void QgsAttributeTableModel::prefetchSortData( const QString &expressionString,
955
953
}
956
954
else
957
955
{
958
- QVariant sortValue = fieldFormatter->sortValue ( mLayer , cache.sortFieldIndex , widgetConfig, widgetCache, f.attribute ( cache.sortFieldIndex ) );
956
+ QVariant sortValue = fieldFormatter->sortValue ( layer () , cache.sortFieldIndex , widgetConfig, widgetCache, f.attribute ( cache.sortFieldIndex ) );
959
957
cache.sortCache .insert ( f.id (), sortValue );
960
958
}
961
959
}
@@ -981,7 +979,7 @@ QString QgsAttributeTableModel::sortCacheExpression( unsigned long cacheIndex )
981
979
void QgsAttributeTableModel::setRequest ( const QgsFeatureRequest &request )
982
980
{
983
981
mFeatureRequest = request;
984
- if ( mLayer && !mLayer ->isSpatial () )
982
+ if ( layer () && !layer () ->isSpatial () )
985
983
mFeatureRequest .setFlags ( mFeatureRequest .flags () | QgsFeatureRequest::NoGeometry );
986
984
}
987
985
0 commit comments