Skip to content
Permalink
Browse files

[needs-docs] Use an indicator icon for embedded layers

The previous approach of showing these layers with italic
text is not obvious for users to understand, and the
interface gives little clues as to why a layer cannot
be edited. There's also no way to see what project
a layer has been embedded from!

So use a new indicator icon in the layer tree to
flag embedded layers, with a tooltip showing the path
to the embedded project location.
  • Loading branch information
nyalldawson committed Jun 9, 2018
1 parent 404f07d commit f633bbcbc0f32a73e99a5a648e4d906790407dd2
@@ -52,6 +52,7 @@ SET(QGIS_APP_SRCS
qgslabelengineconfigdialog.cpp
qgslabelinggui.cpp
qgslabelingwidget.cpp
qgslayertreeviewembeddedindicator.cpp
qgslayertreeviewfilterindicator.cpp
qgsloadstylefromdbdialog.cpp
qgsmapcanvasdockwidget.cpp
@@ -263,6 +264,7 @@ SET (QGIS_APP_MOC_HDRS
qgslabelinggui.h
qgslabelingwidget.h
qgslabelpropertydialog.h
qgslayertreeviewembeddedindicator.h
qgslayertreeviewfilterindicator.h
qgsloadstylefromdbdialog.h
qgsmapcanvasdockwidget.h
@@ -202,6 +202,7 @@ Q_GUI_EXPORT extern int qt_defaultDpiX();
#include "qgslayertreeutils.h"
#include "qgslayertreeview.h"
#include "qgslayertreeviewdefaultactions.h"
#include "qgslayertreeviewembeddedindicator.h"
#include "qgslayertreeviewfilterindicator.h"
#include "qgslayout.h"
#include "qgslayoutatlas.h"
@@ -3726,6 +3727,7 @@ void QgisApp::initLayerTreeView()
mLayerTreeView->setModel( model );
mLayerTreeView->setMenuProvider( new QgsAppLayerTreeViewMenuProvider( mLayerTreeView, mMapCanvas ) );
new QgsLayerTreeViewFilterIndicatorProvider( mLayerTreeView ); // gets parented to the layer view
new QgsLayerTreeViewEmbeddedIndicatorProvider( mLayerTreeView ); // gets parented to the layer view

setupLayerTreeViewFromSettings();

@@ -0,0 +1,77 @@
/***************************************************************************
qgslayertreeviewembeddedindicator.h
--------------------------------------
Date : June 2018
Copyright : (C) 2018 by Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgslayertreeviewembeddedindicator.h"
#include "qgslayertree.h"
#include "qgslayertreemodel.h"
#include "qgslayertreeview.h"

QgsLayerTreeViewEmbeddedIndicatorProvider::QgsLayerTreeViewEmbeddedIndicatorProvider( QgsLayerTreeView *view )
: QObject( view )
, mLayerTreeView( view )
{
mIcon = QgsApplication::getThemeIcon( QStringLiteral( "/mIndicatorFilter.svg" ) );

QgsLayerTree *tree = mLayerTreeView->layerTreeModel()->rootGroup();
onAddedChildren( tree, 0, tree->children().count() - 1 );

connect( tree, &QgsLayerTree::addedChildren, this, &QgsLayerTreeViewEmbeddedIndicatorProvider::onAddedChildren );
}

void QgsLayerTreeViewEmbeddedIndicatorProvider::onAddedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo )
{
// recursively populate indicators
QList<QgsLayerTreeNode *> children = node->children();
for ( int i = indexFrom; i <= indexTo; ++i )
{
QgsLayerTreeNode *childNode = children[i];

if ( QgsLayerTree::isGroup( childNode ) )
{
onAddedChildren( childNode, 0, childNode->children().count() - 1 );
}
else if ( QgsLayerTree::isLayer( childNode ) && childNode->customProperty( QStringLiteral( "embedded" ) ).toInt() )
{
QgsLayerTreeLayer *childLayerNode = QgsLayerTree::toLayer( childNode );
addIndicatorForEmbeddedLayer( childLayerNode );
}
}
}

QgsLayerTreeViewIndicator *QgsLayerTreeViewEmbeddedIndicatorProvider::newIndicator( const QString &project )
{
QgsLayerTreeViewIndicator *indicator = new QgsLayerTreeViewIndicator( this );
indicator->setIcon( mIcon );
indicator->setToolTip( tr( "Embedded from <b>%1</b>" ).arg( project ) );
mIndicators.insert( indicator );
return indicator;
}

void QgsLayerTreeViewEmbeddedIndicatorProvider::addIndicatorForEmbeddedLayer( QgsLayerTreeNode *node )
{
const QList<QgsLayerTreeViewIndicator *> nodeIndicators = mLayerTreeView->indicators( node );

// maybe the indicator exists already
for ( QgsLayerTreeViewIndicator *indicator : nodeIndicators )
{
if ( mIndicators.contains( indicator ) )
{
return;
}
}

// it does not exist: need to create a new one
mLayerTreeView->addIndicator( node, newIndicator( node->customProperty( QStringLiteral( "embedded_project" ) ).toString() ) );
}
@@ -0,0 +1,47 @@
/***************************************************************************
qgslayertreeviewembeddedindicator.h
--------------------------------------
Date : June 2018
Copyright : (C) 2018 by Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSLAYERTREEVIEWEMBEDDEDINDICATOR_H
#define QGSLAYERTREEVIEWEMBEDDEDINDICATOR_H

#include "qgslayertreeviewindicator.h"

#include <QSet>

class QgsLayerTreeNode;
class QgsLayerTreeView;

//! Adds indicators showing whether layers are embedded.
class QgsLayerTreeViewEmbeddedIndicatorProvider : public QObject
{
Q_OBJECT
public:
explicit QgsLayerTreeViewEmbeddedIndicatorProvider( QgsLayerTreeView *view );

private slots:
//! Connects to signals of layers newly added to the tree
void onAddedChildren( QgsLayerTreeNode *node, int indexFrom, int indexTo );

private:
QgsLayerTreeViewIndicator *newIndicator( const QString &project );
void addIndicatorForEmbeddedLayer( QgsLayerTreeNode *node );

private:
QgsLayerTreeView *mLayerTreeView = nullptr;
QIcon mIcon;
QSet<QgsLayerTreeViewIndicator *> mIndicators;
};

#endif // QGSLAYERTREEVIEWEMBEDDEDINDICATOR_H

0 comments on commit f633bbc

Please sign in to comment.
You can’t perform that action at this time.