Skip to content

Commit

Permalink
Merge pull request #3630 from opengisch/backport-3629-to-release-2_5
Browse files Browse the repository at this point in the history
[Backport release-2_5] Feature form fixes
  • Loading branch information
nirvn authored Nov 13, 2022
2 parents 67a7e80 + 7867fe5 commit e58dc26
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 47 deletions.
77 changes: 34 additions & 43 deletions src/core/submodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* (at your option) any later version. *
* *
***************************************************************************/

#include "attributeformmodel.h"
#include "submodel.h"

SubModel::SubModel( QObject *parent )
Expand All @@ -22,55 +24,63 @@ SubModel::SubModel( QObject *parent )

QModelIndex SubModel::index( int row, int column, const QModelIndex &parent ) const
{
if ( !mEnabled || !mModel )
if ( !mEnabled || !mModel || parent.isValid() )
return QModelIndex();

QModelIndex sourceIndex = mModel->index( row, column, parent.isValid() ? mapToSource( parent ) : static_cast<QModelIndex>( mRootIndex ) );
QModelIndex sourceIndex = mModel->index( row, column, parent.isValid() ? mapToSource( parent ) : QModelIndex( mRootIndex ) );
return mapFromSource( sourceIndex );
}

QModelIndex SubModel::parent( const QModelIndex &child ) const
{
if ( !mEnabled || !mModel )
return QModelIndex();

QModelIndex idx = mModel->parent( child );
if ( idx == mRootIndex )
return QModelIndex();
else
return mapFromSource( idx );
return QModelIndex();
}

int SubModel::rowCount( const QModelIndex &parent ) const
{
return mEnabled && mModel ? mModel->rowCount( parent.isValid() ? mapToSource( parent ) : static_cast<QModelIndex>( mRootIndex ) ) : 0;
if ( !mEnabled || !mModel || parent.isValid() )
return 0;

return mModel->rowCount( QModelIndex( mRootIndex ) );
}

int SubModel::columnCount( const QModelIndex &parent ) const
{
return mEnabled && mModel ? mModel->columnCount( parent.isValid() ? mapToSource( parent ) : static_cast<QModelIndex>( mRootIndex ) ) : 0;
if ( !mEnabled || !mModel || parent.isValid() )
return 0;

return mModel->columnCount( QModelIndex( mRootIndex ) );
}

QVariant SubModel::data( const QModelIndex &index, int role ) const
{
return mEnabled && mModel ? mModel->data( mapToSource( index ), role ) : QVariant();
if ( !mEnabled || !mModel )
return QVariant();

return mModel->data( mapToSource( index ), role );
}

bool SubModel::setData( const QModelIndex &index, const QVariant &value, int role )
{
return mEnabled && mModel ? mModel->setData( mapToSource( index ), value, role ) : false;
if ( !mEnabled || !mModel )
return false;

return mModel->setData( mapToSource( index ), value, role );
}

QHash<int, QByteArray> SubModel::roleNames() const
{
return mEnabled && mModel ? mModel->roleNames() : QHash<int, QByteArray>();
if ( !mEnabled || !mModel )
return QHash<int, QByteArray>();

return mModel->roleNames();
}

QModelIndex SubModel::rootIndex() const
{
return mRootIndex;
}
#include <QDebug>

void SubModel::setRootIndex( const QModelIndex &rootIndex )
{
if ( rootIndex == mRootIndex )
Expand Down Expand Up @@ -98,16 +108,14 @@ void SubModel::handleModelConnection( bool disconnecting ) const
{
disconnect( mModel, &QAbstractItemModel::rowsInserted, this, &SubModel::onRowsInserted );
disconnect( mModel, &QAbstractItemModel::rowsAboutToBeRemoved, this, &SubModel::onRowsAboutToBeRemoved );
disconnect( mModel, &QAbstractItemModel::modelAboutToBeReset, this, &SubModel::onModelAboutToBeReset );
disconnect( mModel, &QAbstractItemModel::modelReset, this, &QAbstractItemModel::modelReset );
disconnect( mModel, &QAbstractItemModel::modelReset, this, &SubModel::onModelReset );
disconnect( mModel, &QAbstractItemModel::dataChanged, this, &SubModel::onDataChanged );
}
else if ( mEnabled )
{
connect( mModel, &QAbstractItemModel::rowsInserted, this, &SubModel::onRowsInserted );
connect( mModel, &QAbstractItemModel::rowsAboutToBeRemoved, this, &SubModel::onRowsAboutToBeRemoved );
connect( mModel, &QAbstractItemModel::modelAboutToBeReset, this, &SubModel::onModelAboutToBeReset );
connect( mModel, &QAbstractItemModel::modelReset, this, &QAbstractItemModel::modelReset );
connect( mModel, &QAbstractItemModel::modelReset, this, &SubModel::onModelReset );
connect( mModel, &QAbstractItemModel::dataChanged, this, &SubModel::onDataChanged );
}
}
Expand Down Expand Up @@ -163,9 +171,11 @@ void SubModel::onRowsAboutToBeRemoved( const QModelIndex &parent, int first, int
}
}

void SubModel::onModelAboutToBeReset()
void SubModel::onModelReset()
{
beginResetModel();
mMappings.clear();
endResetModel();
}

void SubModel::onDataChanged( const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles )
Expand All @@ -176,36 +186,17 @@ void SubModel::onDataChanged( const QModelIndex &topLeft, const QModelIndex &bot

bool SubModel::isInSubModel( const QModelIndex &sourceIndex ) const
{
if ( !mRootIndex.isValid() )
return true;

if ( sourceIndex == mRootIndex || !sourceIndex.isValid() )
if ( !mRootIndex.isValid() || !sourceIndex.isValid() || sourceIndex == mRootIndex )
return false;

QModelIndex idx = sourceIndex;
bool foundRootIndex = false;
while ( idx.isValid() )
{
if ( mModel->parent( idx ) == mRootIndex )
{
foundRootIndex = true;
break;
}
idx = mModel->parent( idx );
}
return foundRootIndex;
return sourceIndex.parent() == mRootIndex;
}

QModelIndex SubModel::mapFromSource( const QModelIndex &sourceIndex ) const
{
if ( !mEnabled || !isInSubModel( sourceIndex ) )
return QModelIndex();

if ( !mMappings.contains( sourceIndex.internalId() ) )
{
mMappings.insert( sourceIndex.internalId(), sourceIndex.parent() );
}

return createIndex( sourceIndex.row(), sourceIndex.column(), sourceIndex.internalId() );
}

Expand All @@ -217,5 +208,5 @@ QModelIndex SubModel::mapToSource( const QModelIndex &index ) const
if ( !index.isValid() )
return QModelIndex();

return mModel->index( index.row(), index.column(), mMappings.find( index.internalId() ).value() );
return mModel->index( index.row(), index.column(), QModelIndex( mRootIndex ) );
}
3 changes: 2 additions & 1 deletion src/core/submodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include <QAbstractItemModel>
#include <QPointer>
#include <QStandardItem>

class SubModel : public QAbstractItemModel
{
Expand Down Expand Up @@ -56,7 +57,7 @@ class SubModel : public QAbstractItemModel
private slots:
void onRowsInserted( const QModelIndex &parent, int first, int last );
void onRowsAboutToBeRemoved( const QModelIndex &parent, int first, int last );
void onModelAboutToBeReset();
void onModelReset();
void onDataChanged( const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles = QVector<int>() );

private:
Expand Down
22 changes: 19 additions & 3 deletions src/qml/FeatureForm.qml
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ Page {
SubModel {
id: contentModel
model: form.model
rootIndex: form.model.index(currentIndex, 0)
rootIndex: form.model.index(form.model.hasTabs ? currentIndex : -1, 0)
}

Repeater {
Expand Down Expand Up @@ -304,14 +304,17 @@ Page {
id: innerContainer

property bool isVisible: GroupIndex != undefined && Type === 'container' && GroupIndex.valid

visible: isVisible
height: isVisible ? innerContainerContent.childrenRect.height : 0
height: childrenRect.height
anchors {
left: parent.left
right: parent.right
}

Flow {
onIsVisibleChanged: {
if (isVisible) {
Qt.createQmlObject('import QtQuick 2.14; import org.qfield 1.0; Flow {
id: innerContainerContent
height: childrenRect.height
anchors {
Expand All @@ -321,12 +324,25 @@ Page {
Repeater {
model: SubModel {
id: innerSubModel
enabled: innerContainer.isVisible
model: form.model
rootIndex: innerContainer.isVisible ? form.model.mapFromSource(GroupIndex) : form.model.index(-1, 0)
}
delegate: fieldItem
}
}', innerContainer)
}
}

Connections {
target: form.model

function onModelReset() {
if (innerContainer.innerContainerContent !== undefined && GroupIndex !== undefined && innerContainer.isVisible) {
innerContainer.innerContainerContent.innerSubModel.rootIndex = form.model.mapFromSource(GroupIndex)
}
}
}
}

Expand Down

1 comment on commit e58dc26

@qfield-fairy
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.