21 #include <QVBoxLayout> 36 mModel =
new CustomLayerOrderModel( bridge,
this );
38 mView =
new QListView(
this );
39 mView->setDragEnabled(
true );
40 mView->setAcceptDrops(
true );
41 mView->setDropIndicatorShown(
true );
42 mView->setSelectionMode( QAbstractItemView::ExtendedSelection );
43 mView->setDefaultDropAction( Qt::MoveAction );
45 mView->setModel( mModel );
47 mChkOverride =
new QCheckBox( tr(
"Control rendering order" ) );
54 connect( mModel, &QAbstractItemModel::rowsInserted,
this, &QgsCustomLayerOrderWidget::modelUpdated );
55 connect( mModel, &QAbstractItemModel::rowsRemoved,
this, &QgsCustomLayerOrderWidget::modelUpdated );
59 QVBoxLayout *l =
new QVBoxLayout;
61 l->addWidget( mView );
62 l->addWidget( mChkOverride );
66 void QgsCustomLayerOrderWidget::bridgeHasCustomLayerOrderChanged(
bool state )
68 mChkOverride->setChecked( state );
69 mView->setEnabled( state );
72 void QgsCustomLayerOrderWidget::bridgeCustomLayerOrderChanged()
77 void QgsCustomLayerOrderWidget::nodeVisibilityChanged(
QgsLayerTreeNode *node )
85 void QgsCustomLayerOrderWidget::modelUpdated()
95 : QAbstractListModel( parent )
100 int CustomLayerOrderModel::rowCount(
const QModelIndex & )
const 102 return mOrder.count();
105 QVariant CustomLayerOrderModel::data(
const QModelIndex &index,
int role )
const 107 QString
id = mOrder.at( index.row() );
109 if ( role == Qt::DisplayRole )
113 return layer->
name();
116 if ( role == Qt::UserRole + 1 )
123 if ( role == Qt::CheckStateRole )
133 bool CustomLayerOrderModel::setData(
const QModelIndex &index,
const QVariant &value,
int role )
135 if ( role == Qt::CheckStateRole )
137 QString
id = mOrder.at( index.row() );
148 Qt::ItemFlags CustomLayerOrderModel::flags(
const QModelIndex &index )
const 150 if ( !index.isValid() )
151 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDropEnabled;
152 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsUserCheckable;
155 Qt::DropActions CustomLayerOrderModel::supportedDropActions()
const 157 return Qt::CopyAction | Qt::MoveAction;
160 QStringList CustomLayerOrderModel::mimeTypes()
const 163 types << QStringLiteral(
"application/qgis.layerorderdata" );
167 QMimeData *CustomLayerOrderModel::mimeData(
const QModelIndexList &indexes )
const 170 Q_FOREACH (
const QModelIndex &index, indexes )
171 lst << data( index, Qt::UserRole + 1 ).toString();
173 QMimeData *mimeData =
new QMimeData();
174 mimeData->setData( QStringLiteral(
"application/qgis.layerorderdata" ), lst.join( QStringLiteral(
"\n" ) ).toUtf8() );
178 bool CustomLayerOrderModel::dropMimeData(
const QMimeData *data, Qt::DropAction action,
int row,
int column,
const QModelIndex &parent )
183 if ( action == Qt::IgnoreAction )
186 if ( !data->hasFormat( QStringLiteral(
"application/qgis.layerorderdata" ) ) )
189 QByteArray encodedData = data->data( QStringLiteral(
"application/qgis.layerorderdata" ) );
190 QStringList lst = QString::fromUtf8( encodedData ).split(
'\n' );
193 row = mOrder.count();
195 beginInsertRows( QModelIndex(), row, row + lst.count() - 1 );
196 for (
int i = 0; i < lst.count(); ++i )
197 mOrder.insert( row + i, lst[i] );
203 bool CustomLayerOrderModel::removeRows(
int row,
int count,
const QModelIndex &parent )
209 beginRemoveRows( QModelIndex(), row, row + count - 1 );
210 while ( --count >= 0 )
211 mOrder.removeAt( row );
216 void CustomLayerOrderModel::refreshModel(
const QList<QgsMapLayer *> &order )
218 QStringList orderedIds;
222 orderedIds.append( layer->
id() );
225 if ( orderedIds != mOrder )
233 void CustomLayerOrderModel::updateLayerVisibility(
const QString &layerId )
235 int row = mOrder.indexOf( layerId );
237 emit dataChanged( index( row ), index( row ) );
static QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer.
Base class for all map layer types.
The QgsLayerTreeMapCanvasBridge class takes care of updates of layer set for QgsMapCanvas from a laye...
void customLayerOrderChanged()
Emitted when the custom layer order has changed.
bool isVisible() const
Returns whether a node is really visible (ie checked and all its ancestors checked as well) ...
QString id() const
Returns the layer's unique ID, which is used to access this layer from QgsProject.
QgsLayerTree * rootGroup() const
void setHasCustomLayerOrder(bool hasCustomLayerOrder)
Determines if the layer order should be derived from the layer tree or if a custom override order sha...
static bool isLayer(const QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
This class is a base class for nodes in a layer tree.
bool hasCustomLayerOrder() const
Determines if the layer order should be derived from the layer tree or if a custom override order sha...
void hasCustomLayerOrderChanged(bool hasCustomLayerOrder)
Emitted when the hasCustomLayerOrder flag changes.
QList< QgsMapLayer * > layerOrder() const
The order in which layers will be rendered on the canvas.
void setCustomLayerOrder(const QList< QgsMapLayer *> &customLayerOrder)
The order in which layers will be rendered on the canvas.
void visibilityChanged(QgsLayerTreeNode *node)
Emitted when check state of a node within the tree has been changed.
static QgsProject * instance()
Returns the QgsProject singleton instance.
QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.
void setItemVisibilityChecked(bool checked)
Check or uncheck a node (independently of its ancestors or children)
QgsLayerTreeLayer * findLayer(QgsMapLayer *layer) const
Find layer node representing the map layer.
Layer tree node points to a map layer.