29#include "moc_qgscustomlayerorderwidget.cpp"
31using namespace Qt::StringLiterals;
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()
75 mModel->refreshModel( mBridge->rootGroup()->layerOrder() );
78void QgsCustomLayerOrderWidget::nodeVisibilityChanged(
QgsLayerTreeNode *node )
86void QgsCustomLayerOrderWidget::modelUpdated()
88 mBridge->rootGroup()->setCustomLayerOrder( mModel->order() );
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 << u
"application/qgis.layerorderdata"_s;
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( u
"application/qgis.layerorderdata"_s, 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( u
"application/qgis.layerorderdata"_s ) )
191 const QByteArray encodedData = data->data( u
"application/qgis.layerorderdata"_s );
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.
Takes care of updates of layer sets for a QgsMapCanvas from a layer tree.
QgsLayerTree * rootGroup() const
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.
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...
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.