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;
60 l->setContentsMargins( 0, 0, 0, 0 );
61 l->addWidget( mView );
62 l->addWidget( mChkOverride );
66void QgsCustomLayerOrderWidget::bridgeHasCustomLayerOrderChanged(
bool state )
68 mChkOverride->setChecked( state );
69 mView->setEnabled( state );
72void QgsCustomLayerOrderWidget::bridgeCustomLayerOrderChanged()
77void QgsCustomLayerOrderWidget::nodeVisibilityChanged(
QgsLayerTreeNode *node )
85void QgsCustomLayerOrderWidget::modelUpdated()
95 : QAbstractListModel( parent )
100int CustomLayerOrderModel::rowCount(
const QModelIndex & )
const
102 return mOrder.count();
105QVariant CustomLayerOrderModel::data(
const QModelIndex &index,
int role )
const
107 const 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 )
133bool CustomLayerOrderModel::setData(
const QModelIndex &index,
const QVariant &value,
int role )
136 if ( role == Qt::CheckStateRole )
138 const QString
id = mOrder.at( index.row() );
149Qt::ItemFlags CustomLayerOrderModel::flags(
const QModelIndex &index )
const
151 if ( !index.isValid() )
152 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDropEnabled;
153 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsUserCheckable;
156Qt::DropActions CustomLayerOrderModel::supportedDropActions()
const
158 return Qt::CopyAction | Qt::MoveAction;
161QStringList CustomLayerOrderModel::mimeTypes()
const
164 types << QStringLiteral(
"application/qgis.layerorderdata" );
168QMimeData *CustomLayerOrderModel::mimeData(
const QModelIndexList &indexes )
const
171 const auto constIndexes = indexes;
172 for (
const QModelIndex &index : constIndexes )
173 lst << data( index, Qt::UserRole + 1 ).toString();
175 QMimeData *mimeData =
new QMimeData();
176 mimeData->setData( QStringLiteral(
"application/qgis.layerorderdata" ), lst.join( QLatin1Char(
'\n' ) ).toUtf8() );
180bool CustomLayerOrderModel::dropMimeData(
const QMimeData *data, Qt::DropAction action,
int row,
int column,
const QModelIndex &parent )
185 if ( action == Qt::IgnoreAction )
188 if ( !data->hasFormat( QStringLiteral(
"application/qgis.layerorderdata" ) ) )
191 const QByteArray encodedData = data->data( QStringLiteral(
"application/qgis.layerorderdata" ) );
192 QStringList lst = QString::fromUtf8( encodedData ).split(
'\n' );
195 row = mOrder.count();
197 beginInsertRows( QModelIndex(), row, row + lst.count() - 1 );
198 for (
int i = 0; i < lst.count(); ++i )
199 mOrder.insert( row + i, lst[i] );
205bool CustomLayerOrderModel::removeRows(
int row,
int count,
const QModelIndex &parent )
211 beginRemoveRows( QModelIndex(), row, row + count - 1 );
212 while ( --count >= 0 )
213 mOrder.removeAt( row );
218void CustomLayerOrderModel::refreshModel(
const QList<QgsMapLayer *> &order )
220 QStringList orderedIds;
221 const auto constOrder = order;
225 orderedIds.append( layer->
id() );
228 if ( orderedIds != mOrder )
236void CustomLayerOrderModel::updateLayerVisibility(
const QString &layerId )
238 const int row = mOrder.indexOf( layerId );
240 emit dataChanged( index( row ), index( row ) );
Layer tree node points to a map layer.
The QgsLayerTreeMapCanvasBridge class takes care of updates of layer set for QgsMapCanvas from a laye...
QgsLayerTree * rootGroup() const
This class is a base class for nodes in a layer tree.
bool isVisible() const
Returns whether a node is really visible (ie checked and all its ancestors checked as well)
void visibilityChanged(QgsLayerTreeNode *node)
Emitted when check state of a node within the tree has been changed.
void setItemVisibilityChecked(bool checked)
Check or uncheck a node (independently of its ancestors or children)
bool itemVisibilityChecked() const
Returns whether a node is checked (independently of its ancestors or children)
bool hasCustomLayerOrder() const
Determines if the layer order should be derived from the layer tree or if a custom override order sha...
void customLayerOrderChanged()
Emitted when the custom layer order has changed.
static QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer.
QList< QgsMapLayer * > layerOrder() const
The order in which layers will be rendered on the canvas.
void hasCustomLayerOrderChanged(bool hasCustomLayerOrder)
Emitted when the hasCustomLayerOrder flag changes.
static bool isLayer(const QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
void setHasCustomLayerOrder(bool hasCustomLayerOrder)
Determines if the layer order should be derived from the layer tree or if a custom override order sha...
void setCustomLayerOrder(const QList< QgsMapLayer * > &customLayerOrder)
The order in which layers will be rendered on the canvas.
Base class for all map layer types.
static QgsProject * instance()
Returns the QgsProject singleton instance.
Q_INVOKABLE QgsMapLayer * mapLayer(const QString &layerId) const
Retrieve a pointer to a registered layer by layer ID.