Skip to content
Permalink
Browse files
Deactivate old legend. Improve insertion of new layers into layer tree.
  • Loading branch information
wonder-sk committed May 21, 2014
1 parent c9f9ea8 commit 6beb7d77d5cac5b2b885cb76962e9e1160ae26de
@@ -88,6 +88,7 @@ QgsLegend::QgsLegend( QgsMapCanvas *canvas, QWidget * parent, const char *name )
{
setObjectName( name );

#if 0
connect( this, SIGNAL( itemChanged( QTreeWidgetItem*, int ) ),
this, SLOT( handleItemChange( QTreeWidgetItem*, int ) ) );

@@ -117,6 +118,8 @@ QgsLegend::QgsLegend( QgsMapCanvas *canvas, QWidget * parent, const char *name )

connect( mMapCanvas, SIGNAL( layersChanged() ),
this, SLOT( refreshCheckStates() ) );
#endif

#if 0
// too much
connect( mMapCanvas, SIGNAL( extentsChanged() ),
@@ -135,8 +135,10 @@
#include "qgsgpsinformationwidget.h"
#include "qgsguivectorlayertools.h"
#include "qgslabelinggui.h"
#include "qgslayertreemapcanvasbridge.h"
#include "qgslayertreemodel.h"
#include "qgslayertreenode.h"
#include "qgslayertreeregistrybridge.h"
#include "qgslayertreeutils.h"
#include "qgslayertreeview.h"
#include "qgslayertreeviewdefaultactions.h"
@@ -2226,9 +2228,37 @@ void QgisApp::initLayerTreeView()

connect( mLayerTreeView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(layerTreeViewDoubleClicked(QModelIndex)) );
connect( mLayerTreeView, SIGNAL(currentLayerChanged(QgsMapLayer*)), this, SLOT(activeLayerChanged(QgsMapLayer*)) );
connect( mLayerTreeView->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), this, SLOT(layerTreeViewCurrentChanged(QModelIndex,QModelIndex)));

mLayerTreeDock->setWidget( mLayerTreeView );
addDockWidget( Qt::LeftDockWidgetArea, mLayerTreeDock );

QgsLayerTreeMapCanvasBridge* bridge = new QgsLayerTreeMapCanvasBridge( QgsProject::instance()->layerTreeRoot(), mMapCanvas );
bridge->setParent(this);
}


void QgisApp::layerTreeViewCurrentChanged(const QModelIndex& current, const QModelIndex& previous)
{
Q_UNUSED(previous);

// defaults
QgsLayerTreeGroup* parentGroup = mLayerTreeView->layerTreeModel()->rootGroup();
int index = 0;

if (current.isValid())
{
if (QgsLayerTreeNode* currentNode = mLayerTreeView->currentNode())
{
QgsLayerTreeNode* parentNode = currentNode->parent();
if (parentNode && parentNode->nodeType() == QgsLayerTreeNode::NodeGroup)
parentGroup = static_cast<QgsLayerTreeGroup*>(parentNode);
}

index = current.row();
}

QgsProject::instance()->layerTreeRegistryBridge()->setLayerInsertionPoint(parentGroup, index);
}


@@ -469,6 +469,7 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow

public slots:
void layerTreeViewDoubleClicked(const QModelIndex& index);
void layerTreeViewCurrentChanged(const QModelIndex& current, const QModelIndex& previous);
void activeLayerChanged(QgsMapLayer* layer);
//! Zoom to full extent
void zoomFull();
@@ -145,6 +145,13 @@ QgsLayerTreeGroup* QgsLayerTreeGroup::addGroup(const QString &name)
return grp;
}

QgsLayerTreeLayer*QgsLayerTreeGroup::insertLayer(int index, QgsMapLayer* layer)
{
QgsLayerTreeLayer* ll = new QgsLayerTreeLayer(layer);
insertChildNode( index, ll );
return ll;
}

QgsLayerTreeLayer* QgsLayerTreeGroup::addLayer(QgsMapLayer* layer)
{
QgsLayerTreeLayer* ll = new QgsLayerTreeLayer(layer);
@@ -148,6 +148,7 @@ class QgsLayerTreeGroup : public QgsLayerTreeNode
void setName(const QString& n) { mName = n; }

QgsLayerTreeGroup* addGroup(const QString& name);
QgsLayerTreeLayer* insertLayer(int index, QgsMapLayer* layer);
QgsLayerTreeLayer* addLayer(QgsMapLayer* layer);

void insertChildNodes(int index, QList<QgsLayerTreeNode*> nodes);
@@ -10,21 +10,30 @@ QgsLayerTreeRegistryBridge::QgsLayerTreeRegistryBridge(QgsLayerTreeGroup *root,
: QObject(parent)
, mRoot(root)
, mEnabled(true)
, mInsertionPointGroup(root)
, mInsertionPointIndex(0)
{
connect(QgsMapLayerRegistry::instance(), SIGNAL(layersAdded(QList<QgsMapLayer*>)), this, SLOT(layersAdded(QList<QgsMapLayer*>)));
connect(QgsMapLayerRegistry::instance(), SIGNAL(layersWillBeRemoved(QStringList)), this, SLOT(layersWillBeRemoved(QStringList)));

connectToGroup(mRoot);
}

void QgsLayerTreeRegistryBridge::setLayerInsertionPoint(QgsLayerTreeGroup* parentGroup, int index)
{
mInsertionPointGroup = parentGroup;
mInsertionPointIndex = index;
}

void QgsLayerTreeRegistryBridge::layersAdded(QList<QgsMapLayer*> layers)
{
if (!mEnabled)
return;

foreach (QgsMapLayer* layer, layers)
{
QgsLayerTreeLayer* nodeLayer = mRoot->addLayer(layer);
// add new layer to the top
QgsLayerTreeLayer* nodeLayer = mInsertionPointGroup->insertLayer(mInsertionPointIndex, layer);

// check whether the layer is marked as embedded
QString projectFile = QgsProject::instance()->layerIsEmbedded(nodeLayer->layerId());
@@ -20,6 +20,10 @@ class CORE_EXPORT QgsLayerTreeRegistryBridge : public QObject
void setEnabled(bool enabled) { mEnabled = enabled; }
bool isEnabled() const { return mEnabled; }

//! Set where the new layers should be inserted - can be used to follow current selection.
//! By default it is root group with zero index.
void setLayerInsertionPoint(QgsLayerTreeGroup* parentGroup, int index);

signals:

protected slots:
@@ -37,6 +41,9 @@ protected slots:
QgsLayerTreeGroup* mRoot;
QStringList mLayerIdsForRemoval;
bool mEnabled;

QgsLayerTreeGroup* mInsertionPointGroup;
int mInsertionPointIndex;
};

#endif // QGSLAYERTREEREGISTRYBRIDGE_H
@@ -336,6 +336,9 @@ class CORE_EXPORT QgsProject : public QObject
* @node added in 2.4 */
QgsLayerTreeGroup* layerTreeRoot() const;

/** Return pointer to the helper class that synchronizes map layer registry with layer tree */
QgsLayerTreeRegistryBridge* layerTreeRegistryBridge() const { return mLayerTreeRegistryBridge; }

protected:

/** Set error message from read/write operation
@@ -164,10 +164,17 @@ QgsLayerTreeNode* QgsLayerTreeView::currentNode() const

QgsLayerTreeGroup* QgsLayerTreeView::currentGroupNode() const
{
// TODO: also handle if a layer / symbology is selected within a group?
QgsLayerTreeNode* node = currentNode();
if (node && node->nodeType() == QgsLayerTreeNode::NodeGroup)
return static_cast<QgsLayerTreeGroup*>(node);
else if (node && node->nodeType() == QgsLayerTreeNode::NodeLayer)
{
QgsLayerTreeNode* parent = node->parent();
if (parent && parent->nodeType() == QgsLayerTreeNode::NodeGroup)
return static_cast<QgsLayerTreeGroup*>(node);
}
// TODO: also handle if symbology is selected?

return 0;
}

0 comments on commit 6beb7d7

Please sign in to comment.