Skip to content

Commit

Permalink
Forward declare
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 8, 2021
1 parent 5fdb88b commit 2abf1ca
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 45 deletions.
1 change: 1 addition & 0 deletions src/gui/attributetable/qgsattributetablefiltermodel.cpp
Expand Up @@ -28,6 +28,7 @@
#include "qgsvectorlayereditbuffer.h"
#include "qgsexpressioncontextutils.h"
#include "qgsapplication.h"
#include "qgsvectorlayercache.h"

//////////////////
// Filter Model //
Expand Down
82 changes: 42 additions & 40 deletions src/gui/attributetable/qgsattributetablemodel.cpp
Expand Up @@ -41,6 +41,7 @@
#include "qgstexteditwidgetfactory.h"
#include "qgsexpressioncontextutils.h"
#include "qgsvectorlayerutils.h"
#include "qgsvectorlayercache.h"

#include <QVariant>
#include <QUuid>
Expand All @@ -49,35 +50,35 @@

QgsAttributeTableModel::QgsAttributeTableModel( QgsVectorLayerCache *layerCache, QObject *parent )
: QAbstractTableModel( parent )
, mLayer( layerCache->layer() )
, mLayerCache( layerCache )
{
mExpressionContext.appendScopes( QgsExpressionContextUtils::globalProjectLayerScopes( layerCache->layer() ) );

if ( layerCache->layer()->geometryType() == QgsWkbTypes::NullGeometry )
if ( mLayer->geometryType() == QgsWkbTypes::NullGeometry )
{
mFeatureRequest.setFlags( QgsFeatureRequest::NoGeometry );
}

mFeat.setId( std::numeric_limits<int>::min() );

if ( !layer()->isSpatial() )
if ( !mLayer->isSpatial() )
mFeatureRequest.setFlags( QgsFeatureRequest::NoGeometry );

loadAttributes();

connect( layer(), &QgsVectorLayer::featuresDeleted, this, &QgsAttributeTableModel::featuresDeleted );
connect( layer(), &QgsVectorLayer::attributeDeleted, this, &QgsAttributeTableModel::attributeDeleted );
connect( layer(), &QgsVectorLayer::updatedFields, this, &QgsAttributeTableModel::updatedFields );
connect( mLayer, &QgsVectorLayer::featuresDeleted, this, &QgsAttributeTableModel::featuresDeleted );
connect( mLayer, &QgsVectorLayer::attributeDeleted, this, &QgsAttributeTableModel::attributeDeleted );
connect( mLayer, &QgsVectorLayer::updatedFields, this, &QgsAttributeTableModel::updatedFields );

connect( layer(), &QgsVectorLayer::editCommandStarted, this, &QgsAttributeTableModel::bulkEditCommandStarted );
connect( layer(), &QgsVectorLayer::beforeRollBack, this, &QgsAttributeTableModel::bulkEditCommandStarted );
connect( layer(), &QgsVectorLayer::afterRollBack, this, &QgsAttributeTableModel::bulkEditCommandEnded );
connect( mLayer, &QgsVectorLayer::editCommandStarted, this, &QgsAttributeTableModel::bulkEditCommandStarted );
connect( mLayer, &QgsVectorLayer::beforeRollBack, this, &QgsAttributeTableModel::bulkEditCommandStarted );
connect( mLayer, &QgsVectorLayer::afterRollBack, this, &QgsAttributeTableModel::bulkEditCommandEnded );

connect( layer(), &QgsVectorLayer::editCommandEnded, this, &QgsAttributeTableModel::editCommandEnded );
connect( mLayer, &QgsVectorLayer::editCommandEnded, this, &QgsAttributeTableModel::editCommandEnded );
connect( mLayerCache, &QgsVectorLayerCache::attributeValueChanged, this, &QgsAttributeTableModel::attributeValueChanged );
connect( mLayerCache, &QgsVectorLayerCache::featureAdded, this, [ = ]( QgsFeatureId id ) { featureAdded( id ); } );
connect( mLayerCache, &QgsVectorLayerCache::cachedLayerDeleted, this, &QgsAttributeTableModel::layerDeleted );

}

bool QgsAttributeTableModel::loadFeatureAtId( QgsFeatureId fid ) const
Expand Down Expand Up @@ -233,7 +234,7 @@ void QgsAttributeTableModel::featureAdded( QgsFeatureId fid )
QgsFieldFormatter *fieldFormatter = mFieldFormatters.at( cache.sortFieldIndex );
const QVariant &widgetCache = mAttributeWidgetCaches.at( cache.sortFieldIndex );
const QVariantMap &widgetConfig = mWidgetConfigs.at( cache.sortFieldIndex );
QVariant sortValue = fieldFormatter->representValue( layer(), cache.sortFieldIndex, widgetConfig, widgetCache, mFeat.attribute( cache.sortFieldIndex ) );
QVariant sortValue = fieldFormatter->representValue( mLayer, cache.sortFieldIndex, widgetConfig, widgetCache, mFeat.attribute( cache.sortFieldIndex ) );
cache.sortCache.insert( mFeat.id(), sortValue );
}
else if ( cache.sortCacheExpression.isValid() )
Expand Down Expand Up @@ -287,6 +288,7 @@ void QgsAttributeTableModel::attributeDeleted( int idx )
void QgsAttributeTableModel::layerDeleted()
{
mLayerCache = nullptr;
mLayer = nullptr;
removeRows( 0, rowCount() );

mAttributeWidgetCaches.clear();
Expand Down Expand Up @@ -332,7 +334,7 @@ void QgsAttributeTableModel::attributeValueChanged( QgsFeatureId fid, int idx, c
QgsFieldFormatter *fieldFormatter = mFieldFormatters.at( cache.sortFieldIndex );
const QVariant &widgetCache = mAttributeWidgetCaches.at( cache.sortFieldIndex );
const QVariantMap &widgetConfig = mWidgetConfigs.at( cache.sortFieldIndex );
QVariant sortValue = fieldFormatter->representValue( layer(), cache.sortFieldIndex, widgetConfig, widgetCache, value );
QVariant sortValue = fieldFormatter->representValue( mLayer, cache.sortFieldIndex, widgetConfig, widgetCache, value );
cache.sortCache.insert( fid, sortValue );
}
}
Expand Down Expand Up @@ -375,29 +377,29 @@ void QgsAttributeTableModel::attributeValueChanged( QgsFeatureId fid, int idx, c

void QgsAttributeTableModel::loadAttributes()
{
if ( !layer() )
if ( !mLayer )
{
return;
}

bool ins = false, rm = false;

QgsAttributeList attributes;
const QgsFields &fields = layer()->fields();
const QgsFields &fields = mLayer->fields();

mWidgetFactories.clear();
mAttributeWidgetCaches.clear();
mWidgetConfigs.clear();

for ( int idx = 0; idx < fields.count(); ++idx )
{
const QgsEditorWidgetSetup setup = QgsGui::editorWidgetRegistry()->findBest( layer(), fields[idx].name() );
const QgsEditorWidgetSetup setup = QgsGui::editorWidgetRegistry()->findBest( mLayer, fields[idx].name() );
QgsEditorWidgetFactory *widgetFactory = QgsGui::editorWidgetRegistry()->factory( setup.type() );
QgsFieldFormatter *fieldFormatter = QgsApplication::fieldFormatterRegistry()->fieldFormatter( setup.type() );

mWidgetFactories.append( widgetFactory );
mWidgetConfigs.append( setup.config() );
mAttributeWidgetCaches.append( fieldFormatter->createCache( layer(), idx, setup.config() ) );
mAttributeWidgetCaches.append( fieldFormatter->createCache( mLayer, idx, setup.config() ) );
mFieldFormatters.append( fieldFormatter );

attributes << idx;
Expand Down Expand Up @@ -495,7 +497,7 @@ void QgsAttributeTableModel::fieldConditionalStyleChanged( const QString &fieldN
return;
}

int fieldIndex = mLayerCache->layer()->fields().lookupField( fieldName );
int fieldIndex = mLayer->fields().lookupField( fieldName );
if ( fieldIndex == -1 )
return;

Expand Down Expand Up @@ -596,7 +598,7 @@ int QgsAttributeTableModel::columnCount( const QModelIndex &parent ) const

QVariant QgsAttributeTableModel::headerData( int section, Qt::Orientation orientation, int role ) const
{
if ( !layer() )
if ( !mLayer )
return QVariant();

if ( role == Qt::DisplayRole )
Expand All @@ -607,7 +609,7 @@ QVariant QgsAttributeTableModel::headerData( int section, Qt::Orientation orient
}
else if ( section >= 0 && section < mFieldCount )
{
QString attributeName = layer()->fields().at( mAttributes.at( section ) ).displayName();
QString attributeName = mLayer->fields().at( mAttributes.at( section ) ).displayName();
return QVariant( attributeName );
}
else
Expand All @@ -624,8 +626,8 @@ QVariant QgsAttributeTableModel::headerData( int section, Qt::Orientation orient
}
else
{
const QgsField field = layer()->fields().at( mAttributes.at( section ) );
return QgsFieldModel::fieldToolTipExtended( field, layer() );
const QgsField field = mLayer->fields().at( mAttributes.at( section ) );
return QgsFieldModel::fieldToolTipExtended( field, mLayer );
}
}
else
Expand All @@ -636,7 +638,7 @@ QVariant QgsAttributeTableModel::headerData( int section, Qt::Orientation orient

QVariant QgsAttributeTableModel::data( const QModelIndex &index, int role ) const
{
if ( !index.isValid() || !layer() ||
if ( !index.isValid() || !mLayer ||
( role != Qt::TextAlignmentRole
&& role != Qt::DisplayRole
&& role != Qt::ToolTipRole
Expand Down Expand Up @@ -679,11 +681,11 @@ QVariant QgsAttributeTableModel::data( const QModelIndex &index, int role ) cons
return QVariant();
}

QgsField field = layer()->fields().at( fieldId );
QgsField field = mLayer->fields().at( fieldId );

if ( role == Qt::TextAlignmentRole )
{
return QVariant( mFieldFormatters.at( index.column() )->alignmentFlag( layer(), fieldId, mWidgetConfigs.at( index.column() ) ) | Qt::AlignVCenter );
return QVariant( mFieldFormatters.at( index.column() )->alignmentFlag( mLayer, fieldId, mWidgetConfigs.at( index.column() ) ) | Qt::AlignVCenter );
}

if ( mFeat.id() != rowId || !mFeat.isValid() )
Expand All @@ -701,7 +703,7 @@ QVariant QgsAttributeTableModel::data( const QModelIndex &index, int role ) cons
{
case Qt::DisplayRole:
case Qt::ToolTipRole:
return mFieldFormatters.at( index.column() )->representValue( layer(),
return mFieldFormatters.at( index.column() )->representValue( mLayer,
fieldId,
mWidgetConfigs.at( index.column() ),
mAttributeWidgetCaches.at( index.column() ),
Expand All @@ -727,12 +729,12 @@ QVariant QgsAttributeTableModel::data( const QModelIndex &index, int role ) cons
}
else
{
styles = QgsConditionalStyle::matchingConditionalStyles( layer()->conditionalStyles()->rowStyles(), QVariant(), mExpressionContext );
styles = QgsConditionalStyle::matchingConditionalStyles( mLayer->conditionalStyles()->rowStyles(), QVariant(), mExpressionContext );
mRowStylesMap.insert( mFeat.id(), styles );
}

QgsConditionalStyle rowstyle = QgsConditionalStyle::compressStyles( styles );
styles = layer()->conditionalStyles()->fieldStyles( field.name() );
styles = mLayer->conditionalStyles()->fieldStyles( field.name() );
styles = QgsConditionalStyle::matchingConditionalStyles( styles, val, mExpressionContext );
styles.insert( 0, rowstyle );
QgsConditionalStyle style = QgsConditionalStyle::compressStyles( styles );
Expand Down Expand Up @@ -764,12 +766,12 @@ bool QgsAttributeTableModel::setData( const QModelIndex &index, const QVariant &
{
Q_UNUSED( value )

if ( !index.isValid() || index.column() >= mFieldCount || role != Qt::EditRole || !layer()->isEditable() )
if ( !index.isValid() || index.column() >= mFieldCount || role != Qt::EditRole || !mLayer->isEditable() )
return false;

mRowStylesMap.remove( mFeat.id() );

if ( !layer()->isModified() )
if ( !mLayer->isModified() )
return false;

return true;
Expand All @@ -780,15 +782,15 @@ Qt::ItemFlags QgsAttributeTableModel::flags( const QModelIndex &index ) const
if ( !index.isValid() )
return Qt::ItemIsEnabled;

if ( index.column() >= mFieldCount || !layer() )
if ( index.column() >= mFieldCount || !mLayer )
return Qt::NoItemFlags;

Qt::ItemFlags flags = QAbstractTableModel::flags( index );

const int fieldIndex = mAttributes[index.column()];
const QgsFeatureId fid = rowToId( index.row() );

if ( QgsVectorLayerUtils::fieldIsEditable( layer(), fieldIndex, fid ) )
if ( QgsVectorLayerUtils::fieldIsEditable( mLayer, fieldIndex, fid ) )
flags |= Qt::ItemIsEditable;

return flags;
Expand Down Expand Up @@ -819,7 +821,7 @@ void QgsAttributeTableModel::bulkEditCommandEnded()
if ( fullModelUpdate )
{
// Invalidates the cache (there is no API for doing this directly)
emit mLayerCache->layer()->dataChanged();
emit mLayer->dataChanged();
emit dataChanged( createIndex( 0, 0 ), createIndex( rowCount() - 1, columnCount() - 1 ) );
}
else
Expand Down Expand Up @@ -854,18 +856,18 @@ void QgsAttributeTableModel::reload( const QModelIndex &index1, const QModelInde
void QgsAttributeTableModel::executeAction( QUuid action, const QModelIndex &idx ) const
{
QgsFeature f = feature( idx );
layer()->actions()->doAction( action, f, fieldIdx( idx.column() ) );
mLayer->actions()->doAction( action, f, fieldIdx( idx.column() ) );
}

void QgsAttributeTableModel::executeMapLayerAction( QgsMapLayerAction *action, const QModelIndex &idx ) const
{
QgsFeature f = feature( idx );
action->triggerForFeature( layer(), f );
action->triggerForFeature( mLayer, f );
}

QgsFeature QgsAttributeTableModel::feature( const QModelIndex &idx ) const
{
QgsFeature f( mLayerCache->layer()->fields() );
QgsFeature f( mLayer->fields() );
f.initAttributes( mAttributes.size() );
f.setId( rowToId( idx.row() ) );
for ( int i = 0; i < mAttributes.size(); i++ )
Expand All @@ -884,7 +886,7 @@ void QgsAttributeTableModel::prefetchColumnData( int column )
}
else
{
prefetchSortData( QgsExpression::quotedColumnRef( mLayerCache->layer()->fields().at( mAttributes.at( column ) ).name() ) );
prefetchSortData( QgsExpression::quotedColumnRef( mLayer->fields().at( mAttributes.at( column ) ).name() ) );
}
}

Expand Down Expand Up @@ -914,7 +916,7 @@ void QgsAttributeTableModel::prefetchSortData( const QString &expressionString,
if ( cache.sortCacheExpression.isField() )
{
QString fieldName = static_cast<const QgsExpressionNodeColumnRef *>( cache.sortCacheExpression.rootNode() )->name();
cache.sortFieldIndex = mLayerCache->layer()->fields().lookupField( fieldName );
cache.sortFieldIndex = mLayer->fields().lookupField( fieldName );
}

if ( cache.sortFieldIndex == -1 )
Expand All @@ -925,7 +927,7 @@ void QgsAttributeTableModel::prefetchSortData( const QString &expressionString,

for ( const QString &col : referencedColumns )
{
cache.sortCacheAttributes.append( mLayerCache->layer()->fields().lookupField( col ) );
cache.sortCacheAttributes.append( mLayer->fields().lookupField( col ) );
}
}
else
Expand Down Expand Up @@ -953,7 +955,7 @@ void QgsAttributeTableModel::prefetchSortData( const QString &expressionString,
}
else
{
QVariant sortValue = fieldFormatter->sortValue( layer(), cache.sortFieldIndex, widgetConfig, widgetCache, f.attribute( cache.sortFieldIndex ) );
QVariant sortValue = fieldFormatter->sortValue( mLayer, cache.sortFieldIndex, widgetConfig, widgetCache, f.attribute( cache.sortFieldIndex ) );
cache.sortCache.insert( f.id(), sortValue );
}
}
Expand All @@ -979,7 +981,7 @@ QString QgsAttributeTableModel::sortCacheExpression( unsigned long cacheIndex )
void QgsAttributeTableModel::setRequest( const QgsFeatureRequest &request )
{
mFeatureRequest = request;
if ( layer() && !layer()->isSpatial() )
if ( mLayer && !mLayer->isSpatial() )
mFeatureRequest.setFlags( mFeatureRequest.flags() | QgsFeatureRequest::NoGeometry );
}

Expand Down
5 changes: 3 additions & 2 deletions src/gui/attributetable/qgsattributetablemodel.h
Expand Up @@ -27,13 +27,13 @@

#include "qgsconditionalstyle.h"
#include "qgsattributeeditorcontext.h"
#include "qgsvectorlayercache.h"
#include "qgis_gui.h"

class QgsMapCanvas;
class QgsMapLayerAction;
class QgsEditorWidgetFactory;
class QgsFieldFormatter;
class QgsVectorLayerCache;

/**
* \ingroup gui
Expand Down Expand Up @@ -165,7 +165,7 @@ class GUI_EXPORT QgsAttributeTableModel: public QAbstractTableModel
/**
* Returns the layer this model uses as backend. Retrieved from the layer cache.
*/
inline QgsVectorLayer *layer() const { return mLayerCache ? mLayerCache->layer() : nullptr; }
inline QgsVectorLayer *layer() const { return mLayer; }

/**
* Returns the layer cache this model uses as backend.
Expand Down Expand Up @@ -329,6 +329,7 @@ class GUI_EXPORT QgsAttributeTableModel: public QAbstractTableModel
virtual void fieldFormatterRemoved( QgsFieldFormatter *fieldFormatter );

private:
QgsVectorLayer *mLayer = nullptr;
QgsVectorLayerCache *mLayerCache = nullptr;
int mFieldCount = 0;

Expand Down
1 change: 1 addition & 0 deletions src/gui/attributetable/qgsfeaturelistmodel.cpp
Expand Up @@ -19,6 +19,7 @@
#include "qgsvectorlayereditbuffer.h"
#include "qgsattributetablefiltermodel.h"
#include "qgsapplication.h"
#include "qgsvectorlayercache.h"

#include <QItemSelection>
#include <QSettings>
Expand Down
1 change: 1 addition & 0 deletions src/gui/attributetable/qgsfeaturelistview.cpp
Expand Up @@ -31,6 +31,7 @@
#include "qgsvectordataprovider.h"
#include "qgsvectorlayer.h"
#include "qgsvectorlayerselectionmanager.h"
#include "qgsvectorlayercache.h"

QgsFeatureListView::QgsFeatureListView( QWidget *parent )
: QListView( parent )
Expand Down
3 changes: 1 addition & 2 deletions tests/src/app/testqgsattributetable.cpp
Expand Up @@ -29,8 +29,7 @@
#include "qgsvectorfilewriter.h"
#include "qgsfeaturelistmodel.h"
#include "qgsclipboard.h"

#include "qgstest.h"
#include "qgsvectorlayercache.h"

/**
* \ingroup UnitTests
Expand Down
2 changes: 1 addition & 1 deletion tests/src/gui/testqgsdualview.cpp
Expand Up @@ -27,7 +27,7 @@
#include <qgsmapcanvas.h>
#include <qgsfeature.h>
#include "qgsgui.h"

#include "qgsvectorlayercache.h"
#include "qgstest.h"

class TestQgsDualView : public QObject
Expand Down

0 comments on commit 2abf1ca

Please sign in to comment.