17#include "moc_qgscustomlayerorderwidget.cpp"
37 mModel =
new CustomLayerOrderModel( bridge,
this );
39 mView =
new QListView(
this );
40 mView->setDragEnabled(
true );
41 mView->setAcceptDrops(
true );
42 mView->setDropIndicatorShown(
true );
43 mView->setSelectionMode( QAbstractItemView::ExtendedSelection );
44 mView->setDefaultDropAction( Qt::MoveAction );
46 mView->setModel( mModel );
48 mChkOverride =
new QCheckBox( tr(
"Control rendering order" ) );
55 connect( mModel, &QAbstractItemModel::rowsInserted,
this, &QgsCustomLayerOrderWidget::modelUpdated );
56 connect( mModel, &QAbstractItemModel::rowsRemoved,
this, &QgsCustomLayerOrderWidget::modelUpdated );
60 QVBoxLayout *l =
new QVBoxLayout;
61 l->setContentsMargins( 0, 0, 0, 0 );
62 l->addWidget( mView );
63 l->addWidget( mChkOverride );
67void QgsCustomLayerOrderWidget::bridgeHasCustomLayerOrderChanged(
bool state )
69 mChkOverride->setChecked( state );
70 mView->setEnabled( state );
73void QgsCustomLayerOrderWidget::bridgeCustomLayerOrderChanged()
78void QgsCustomLayerOrderWidget::nodeVisibilityChanged(
QgsLayerTreeNode *node )
86void QgsCustomLayerOrderWidget::modelUpdated()
96 : QAbstractListModel( parent )
101int CustomLayerOrderModel::rowCount(
const QModelIndex & )
const
103 return mOrder.count();
106QVariant CustomLayerOrderModel::data(
const QModelIndex &index,
int role )
const
108 const QString
id = mOrder.at( index.row() );
110 if ( role == Qt::DisplayRole )
114 return layer->
name();
117 if ( role == Qt::UserRole + 1 )
124 if ( role == Qt::CheckStateRole )
134bool CustomLayerOrderModel::setData(
const QModelIndex &index,
const QVariant &value,
int role )
137 if ( role == Qt::CheckStateRole )
139 const QString
id = mOrder.at( index.row() );
150Qt::ItemFlags CustomLayerOrderModel::flags(
const QModelIndex &index )
const
152 if ( !index.isValid() )
153 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDropEnabled;
154 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsUserCheckable;
157Qt::DropActions CustomLayerOrderModel::supportedDropActions()
const
159 return Qt::CopyAction | Qt::MoveAction;
162QStringList CustomLayerOrderModel::mimeTypes()
const
165 types << QStringLiteral(
"application/qgis.layerorderdata" );
169QMimeData *CustomLayerOrderModel::mimeData(
const QModelIndexList &indexes )
const
172 const auto constIndexes = indexes;
173 for (
const QModelIndex &index : constIndexes )
174 lst << data( index, Qt::UserRole + 1 ).toString();
176 QMimeData *mimeData =
new QMimeData();
177 mimeData->setData( QStringLiteral(
"application/qgis.layerorderdata" ), lst.join( QLatin1Char(
'\n' ) ).toUtf8() );
181bool CustomLayerOrderModel::dropMimeData(
const QMimeData *data, Qt::DropAction action,
int row,
int column,
const QModelIndex &parent )
186 if ( action == Qt::IgnoreAction )
189 if ( !data->hasFormat( QStringLiteral(
"application/qgis.layerorderdata" ) ) )
192 const QByteArray encodedData = data->data( QStringLiteral(
"application/qgis.layerorderdata" ) );
193 QStringList lst = QString::fromUtf8( encodedData ).split(
'\n' );
196 row = mOrder.count();
198 beginInsertRows( QModelIndex(), row, row + lst.count() - 1 );
199 for (
int i = 0; i < lst.count(); ++i )
200 mOrder.insert( row + i, lst[i] );
206bool CustomLayerOrderModel::removeRows(
int row,
int count,
const QModelIndex &parent )
212 beginRemoveRows( QModelIndex(), row, row + count - 1 );
213 while ( --count >= 0 )
214 mOrder.removeAt( row );
219void CustomLayerOrderModel::refreshModel(
const QList<QgsMapLayer *> &order )
221 QStringList orderedIds;
222 const auto constOrder = order;
226 orderedIds.append( layer->
id() );
229 if ( orderedIds != mOrder )
237void CustomLayerOrderModel::updateLayerVisibility(
const QString &layerId )
239 const int row = mOrder.indexOf( layerId );
241 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.