Skip to content
Permalink
Browse files
Allow changes of visibility from layer order widget
  • Loading branch information
wonder-sk committed May 21, 2014
1 parent 393a415 commit 66cecd72ac9ae9dc11247a58c1f338bc9900a4ee
@@ -5,6 +5,7 @@
#include <QMimeData>
#include <QVBoxLayout>

#include "qgslayertree.h"
#include "qgslayertreemapcanvasbridge.h"

#include "qgsmaplayer.h"
@@ -26,36 +27,52 @@ class CustomLayerOrderModel : public QAbstractListModel

QVariant data(const QModelIndex &index, int role) const
{
QString id = mOrder.at(index.row());

if (role == Qt::DisplayRole)
{
QString id = mOrder.at(index.row());
QgsMapLayer* layer = QgsMapLayerRegistry::instance()->mapLayer(id);
if (layer)
return layer->name();
}

if (role == Qt::UserRole+1)
{
QString id = mOrder.at(index.row());
QgsMapLayer* layer = QgsMapLayerRegistry::instance()->mapLayer(id);
if (layer)
return layer->id();
}

if (role == Qt::CheckStateRole)
{
// TODO: layer visibility
return Qt::Checked;
QgsLayerTreeLayer* nodeLayer = mBridge->rootGroup()->findLayer(id);
if (nodeLayer)
return nodeLayer->isVisible();
}

return QVariant();
}

bool setData(const QModelIndex &index, const QVariant &value, int role)
{
if (role == Qt::CheckStateRole)
{
QString id = mOrder.at(index.row());
QgsLayerTreeLayer* nodeLayer = mBridge->rootGroup()->findLayer(id);
if (nodeLayer)
{
nodeLayer->setVisible((Qt::CheckState)value.toInt());
return true;
}
}
return false;
}

Qt::ItemFlags flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDropEnabled;
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled; // | Qt::ItemIsUserCheckable;
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsUserCheckable;
}

Qt::DropActions supportedDropActions() const
@@ -128,6 +145,13 @@ class CustomLayerOrderModel : public QAbstractListModel

QStringList order() const { return mOrder; }

void updateLayerVisibility(const QString& layerId)
{
int row = mOrder.indexOf(layerId);
if (row != -1)
emit dataChanged(index(row),index(row));
}

protected:
QgsLayerTreeMapCanvasBridge* mBridge;
QStringList mOrder;
@@ -159,6 +183,8 @@ QgsCustomLayerOrderWidget::QgsCustomLayerOrderWidget(QgsLayerTreeMapCanvasBridge
connect(mModel, SIGNAL(rowsInserted(QModelIndex,int,int)), this, SLOT(modelUpdated()));
connect(mModel, SIGNAL(rowsRemoved(QModelIndex,int,int)), this, SLOT(modelUpdated()));

connect(bridge->rootGroup(), SIGNAL(visibilityChanged(QgsLayerTreeNode*,Qt::CheckState)), this, SLOT(nodeVisibilityChanged(QgsLayerTreeNode*,Qt::CheckState)));

QVBoxLayout* l = new QVBoxLayout;
l->setMargin( 0 );
l->addWidget( mView );
@@ -179,6 +205,14 @@ void QgsCustomLayerOrderWidget::bridgeCustomLayerOrderChanged(const QStringList&
mModel->refreshModel(mBridge->hasCustomLayerOrder() ? mBridge->customLayerOrder() : mBridge->defaultLayerOrder());
}

void QgsCustomLayerOrderWidget::nodeVisibilityChanged(QgsLayerTreeNode* node, Qt::CheckState state)
{
if (QgsLayerTree::isLayer(node))
{
mModel->updateLayerVisibility(QgsLayerTree::toLayer(node)->layerId());
}
}

void QgsCustomLayerOrderWidget::modelUpdated()
{
mBridge->setCustomLayerOrder(mModel->order());
@@ -5,6 +5,7 @@

class CustomLayerOrderModel;
class QgsLayerTreeMapCanvasBridge;
class QgsLayerTreeNode;

class QCheckBox;
class QListView;
@@ -20,6 +21,7 @@ class GUI_EXPORT QgsCustomLayerOrderWidget : public QWidget
protected slots:
void bridgeHasCustomLayerOrderChanged(bool override);
void bridgeCustomLayerOrderChanged(const QStringList& order);
void nodeVisibilityChanged(QgsLayerTreeNode* node, Qt::CheckState state);

void modelUpdated();

@@ -19,6 +19,9 @@ class GUI_EXPORT QgsLayerTreeMapCanvasBridge : public QObject

void clear();

QgsLayerTreeGroup* rootGroup() const { return mRoot; }
QgsMapCanvas* mapCanvas() const { return mCanvas; }

bool hasCustomLayerOrder() const { return mHasCustomLayerOrder; }
QStringList customLayerOrder() const { return mCustomLayerOrder; }

0 comments on commit 66cecd7

Please sign in to comment.