Skip to content

Commit

Permalink
[qgsquick] add option to forcly reload feature forms from qml
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterPetrik authored and nyalldawson committed Nov 27, 2020
1 parent 437df14 commit 37ab5e8
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/quickgui/attributes/qgsquickattributeformmodel.cpp
Expand Up @@ -72,6 +72,11 @@ QVariant QgsQuickAttributeFormModel::attribute( const QString &name ) const
return mSourceModel->attribute( name );
}

void QgsQuickAttributeFormModel::forceClean()
{
mSourceModel->forceClean();
}

bool QgsQuickAttributeFormModel::filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const
{
return mSourceModel->data( mSourceModel->index( source_row, 0, source_parent ), CurrentlyVisible ).toBool();
Expand Down
3 changes: 3 additions & 0 deletions src/quickgui/attributes/qgsquickattributeformmodel.h
Expand Up @@ -108,6 +108,9 @@ class QUICK_EXPORT QgsQuickAttributeFormModel : public QSortFilterProxyModel
//! Returns attribute value with name
Q_INVOKABLE QVariant attribute( const QString &name ) const;

//! Resets the model
Q_INVOKABLE void forceClean();

signals:
//! \copydoc QgsQuickAttributeFormModel::attributeModel
void attributeModelChanged();
Expand Down
12 changes: 12 additions & 0 deletions src/quickgui/attributes/qgsquickattributeformmodelbase.cpp
Expand Up @@ -414,6 +414,18 @@ void QgsQuickAttributeFormModelBase::setConstraintsSoftValid( bool constraintsSo
emit constraintsSoftValidChanged();
}

void QgsQuickAttributeFormModelBase::forceClean()
{
mLayer = nullptr;
mTemporaryContainer = nullptr;
mHasTabs = false;
mVisibilityExpressions.clear();
mConstraints.clear();
mExpressionContext = QgsExpressionContext();
mConstraintsHardValid = false;
mConstraintsSoftValid = false;
}

bool QgsQuickAttributeFormModelBase::hasTabs() const
{
return mHasTabs;
Expand Down
3 changes: 3 additions & 0 deletions src/quickgui/attributes/qgsquickattributeformmodelbase.h
Expand Up @@ -104,6 +104,9 @@ class QgsQuickAttributeFormModelBase : public QStandardItemModel
//! \copydoc QgsQuickAttributeFormModelBase::constraintsSoftValid
void setConstraintsSoftValid( bool constraintsSoftValid );

//! Resets the model
Q_INVOKABLE void forceClean();

signals:
//! \copydoc QgsQuickAttributeFormModelBase::attributeModel
void attributeModelChanged();
Expand Down
8 changes: 7 additions & 1 deletion src/quickgui/attributes/qgsquickattributemodel.cpp
Expand Up @@ -38,6 +38,12 @@ void QgsQuickAttributeModel::setFeatureLayerPair( const QgsQuickFeatureLayerPair
setFeature( pair.feature() );
}

void QgsQuickAttributeModel::forceClean()
{
mRememberedAttributes.clear();
mFeatureLayerPair = QgsQuickFeatureLayerPair();
}


void QgsQuickAttributeModel::setVectorLayer( QgsVectorLayer *layer )
{
Expand All @@ -48,7 +54,7 @@ void QgsQuickAttributeModel::setVectorLayer( QgsVectorLayer *layer )
mFeatureLayerPair = QgsQuickFeatureLayerPair( mFeatureLayerPair.feature(), layer );


if ( auto *lLayer = mFeatureLayerPair.layer() )
if ( const QgsVectorLayer *lLayer = mFeatureLayerPair.layer() )
{
mRememberedAttributes.resize( lLayer->fields().size() );
mRememberedAttributes.fill( false );
Expand Down
7 changes: 5 additions & 2 deletions src/quickgui/attributes/qgsquickattributemodel.h
Expand Up @@ -118,12 +118,15 @@ class QUICK_EXPORT QgsQuickAttributeModel : public QAbstractListModel
//! Gets remembered attributes
QVector<bool> rememberedAttributes() const;

//!\copydoc QgsQuickAttributeModel::featureLayerPair
//! Gets current featureLayerPair
QgsQuickFeatureLayerPair featureLayerPair() const;

//!\copydoc QgsQuickAttributeModel::featureLayerPair
//! Sets current featureLayerPair
void setFeatureLayerPair( const QgsQuickFeatureLayerPair &pair );

//! Resets the model
Q_INVOKABLE void forceClean();

public slots:

signals:
Expand Down
19 changes: 17 additions & 2 deletions src/quickgui/attributes/qgsquicksubmodel.cpp
Expand Up @@ -14,6 +14,7 @@
***************************************************************************/

#include "qgsquicksubmodel.h"
#include <QDebug>

QgsQuickSubModel::QgsQuickSubModel( QObject *parent )
: QAbstractItemModel( parent )
Expand Down Expand Up @@ -62,15 +63,29 @@ QVariant QgsQuickSubModel::data( const QModelIndex &index, int role ) const
if ( !mModel )
return QVariant();

return mModel->data( mapToSource( index ), role );
QModelIndex sourceIndex = mapToSource( index );
if ( sourceIndex.isValid() )
return mModel->data( sourceIndex, role );
else
{
qDebug() << "Warning: QgsQuickSubModel::data invalid index" << index << "role: " << role << "root: " << mRootIndex;
return QVariant();
}
}

bool QgsQuickSubModel::setData( const QModelIndex &index, const QVariant &value, int role )
{
if ( !mModel )
return false;

return mModel->setData( mapToSource( index ), value, role );
QModelIndex sourceIndex = mapToSource( index );
if ( sourceIndex.isValid() )
return mModel->setData( sourceIndex, value, role );
else
{
qDebug() << "Warning: QgsQuickSubModel::setData invalid index" << index << "value: " << value << "role: " << role << "root: " << mRootIndex;
return false;
}
}

QHash<int, QByteArray> QgsQuickSubModel::roleNames() const
Expand Down

0 comments on commit 37ab5e8

Please sign in to comment.