Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement support for map theme default active layer #5484

Merged
merged 2 commits into from
Jul 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/core/layertreemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ FlatLayerTreeModel::FlatLayerTreeModel( QgsLayerTree *layerTree, QgsProject *pro
setSourceModel( mSourceModel );
connect( mSourceModel, &FlatLayerTreeModelBase::mapThemeChanged, this, &FlatLayerTreeModel::mapThemeChanged );
connect( mSourceModel, &FlatLayerTreeModelBase::isTemporalChanged, this, &FlatLayerTreeModel::isTemporalChanged );
connect( mSourceModel, &FlatLayerTreeModelBase::isFrozenChanged, this, &FlatLayerTreeModel::isFrozenChanged );
}

QVariant FlatLayerTreeModel::data( const QModelIndex &index, int role ) const
Expand Down Expand Up @@ -68,6 +69,11 @@ void FlatLayerTreeModel::updateCurrentMapTheme()
mSourceModel->updateCurrentMapTheme();
}

bool FlatLayerTreeModel::isFrozen() const
{
return mSourceModel->isFrozen();
}

void FlatLayerTreeModel::freeze()
{
mSourceModel->freeze();
Expand Down Expand Up @@ -124,14 +130,22 @@ FlatLayerTreeModelBase::FlatLayerTreeModelBase( QgsLayerTree *layerTree, QgsProj
connect( mLayerTreeModel, &QAbstractItemModel::rowsInserted, this, &FlatLayerTreeModelBase::insertInMap );
}

bool FlatLayerTreeModelBase::isFrozen() const
{
return mFrozen > 0;
}

void FlatLayerTreeModelBase::freeze()
{
mFrozen++;
emit isFrozenChanged();
}

void FlatLayerTreeModelBase::unfreeze( bool resetModel )
{
mFrozen = 0;
emit isFrozenChanged();

if ( resetModel )
buildMap( mLayerTreeModel );
}
Expand Down
7 changes: 7 additions & 0 deletions src/core/layertreemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ class FlatLayerTreeModelBase : public QAbstractProxyModel
//! This should be triggered after a project has been loaded
Q_INVOKABLE void updateCurrentMapTheme();

//! Returns TRUE if the model is frozen
bool isFrozen() const;
//! Freezes the model as is, with any source model signals ignored
Q_INVOKABLE void freeze();
//! Unfreezes the model and resume listening to source model signals
Expand All @@ -84,6 +86,7 @@ class FlatLayerTreeModelBase : public QAbstractProxyModel
signals:
void mapThemeChanged();
void isTemporalChanged();
void isFrozenChanged();

private:
void featureCountChanged();
Expand Down Expand Up @@ -111,6 +114,7 @@ class FlatLayerTreeModel : public QSortFilterProxyModel

Q_PROPERTY( QString mapTheme READ mapTheme WRITE setMapTheme NOTIFY mapThemeChanged )
Q_PROPERTY( bool isTemporal READ isTemporal NOTIFY isTemporalChanged )
Q_PROPERTY( bool isFrozen READ isFrozen NOTIFY isFrozenChanged )

public:
enum Roles
Expand Down Expand Up @@ -157,6 +161,8 @@ class FlatLayerTreeModel : public QSortFilterProxyModel
//! This should be triggered after a project has been loaded
Q_INVOKABLE void updateCurrentMapTheme();

//! Returns TRUE if the model is frozen
bool isFrozen() const;
//! Freezes the model as is, with any source model signals ignored
Q_INVOKABLE void freeze();
//! Unfreezes the model and resume listening to source model signals
Expand All @@ -177,6 +183,7 @@ class FlatLayerTreeModel : public QSortFilterProxyModel
signals:
void mapThemeChanged();
void isTemporalChanged();
void isFrozenChanged();

protected:
virtual bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const override;
Expand Down
20 changes: 20 additions & 0 deletions src/core/projectinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -780,3 +780,23 @@ QVariantMap ProjectInfo::getImageDecorationConfiguration()

return configuration;
}

QgsMapLayer *ProjectInfo::getDefaultActiveLayerForMapTheme( const QString &mapTheme )
{
if ( mapTheme.isEmpty() )
{
return nullptr;
}

const QString json = QgsProject::instance()->readEntry( QStringLiteral( "qfieldsync" ), QStringLiteral( "/mapThemesActiveLayers" ) );
const QJsonDocument document = QJsonDocument::fromJson( json.toUtf8() );

QJsonObject entries = document.object();
if ( entries.contains( mapTheme ) )
{
const QString mapLayerId = entries.value( mapTheme ).toString();
return QgsProject::instance()->mapLayer( mapLayerId );
}

return nullptr;
}
3 changes: 3 additions & 0 deletions src/core/projectinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ class ProjectInfo : public QObject
//! Retrieves configuration of the image decoration
Q_INVOKABLE QVariantMap getImageDecorationConfiguration();

//! Retrieves the default active layer for a given map theme
Q_INVOKABLE QgsMapLayer *getDefaultActiveLayerForMapTheme( const QString &mapTheme );

signals:

void filePathChanged();
Expand Down
2 changes: 1 addition & 1 deletion src/qml/Legend.qml
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ ListView {
enabled: (allowActiveLayerChange || (projectInfo.activeLayer != VectorLayerPointer))
onClicked: {
layerTree.setData(legend.model.index(index, 0), !Visible, FlatLayerTreeModel.Visible);
flatLayerTree.mapTheme = '';
projectInfo.saveLayerTreeState();
flatLayerTree.mapTheme = '';
}
}
}
Expand Down
22 changes: 21 additions & 1 deletion src/qml/qgismobileapp.qml
Original file line number Diff line number Diff line change
Expand Up @@ -3303,7 +3303,14 @@ ApplicationWindow {
projectInfo.filePath = path;
stateMachine.state = projectInfo.stateMode;
platformUtilities.setHandleVolumeKeys(qfieldSettings.digitizingVolumeKeys && stateMachine.state != 'browse');
dashBoard.activeLayer = projectInfo.activeLayer;
let activeLayer = projectInfo.activeLayer;
if (flatLayerTree.mapTheme != '') {
let defaultActiveLayer = projectInfo.getDefaultActiveLayerForMapTheme(flatLayerTree.mapTheme);
if (defaultActiveLayer !== null) {
activeLayer = defaultActiveLayer;
}
}
dashBoard.activeLayer = activeLayer;
drawingTemplateModel.projectFilePath = path;
mapCanvasBackground.color = mapCanvas.mapSettings.backgroundColor;
var titleDecorationConfiguration = projectInfo.getTitleDecorationConfiguration();
Expand Down Expand Up @@ -3382,6 +3389,19 @@ ApplicationWindow {
}
}

Connections {
target: flatLayerTree

function onMapThemeChanged() {
if (!flatLayerTree.isFrozen && flatLayerTree.mapTheme != '') {
let defaultActiveLayer = projectInfo.getDefaultActiveLayerForMapTheme(flatLayerTree.mapTheme);
nirvn marked this conversation as resolved.
Show resolved Hide resolved
if (defaultActiveLayer !== null) {
dashBoard.activeLayer = defaultActiveLayer;
}
}
}
}

ProjectInfo {
id: projectInfo

Expand Down
Loading