17#include "moc_qgscustomlayerorderwidget.cpp"
35 mModel =
new CustomLayerOrderModel( bridge,
this );
37 mView =
new QListView(
this );
38 mView->setDragEnabled(
true );
39 mView->setAcceptDrops(
true );
40 mView->setDropIndicatorShown(
true );
41 mView->setSelectionMode( QAbstractItemView::ExtendedSelection );
42 mView->setDefaultDropAction( Qt::MoveAction );
44 mView->setModel( mModel );
46 mChkOverride =
new QCheckBox( tr(
"Control rendering order" ) );
53 connect( mModel, &QAbstractItemModel::rowsInserted,
this, &QgsCustomLayerOrderWidget::modelUpdated );
54 connect( mModel, &QAbstractItemModel::rowsRemoved,
this, &QgsCustomLayerOrderWidget::modelUpdated );
58 QVBoxLayout *l =
new QVBoxLayout;
59 l->setContentsMargins( 0, 0, 0, 0 );
60 l->addWidget( mView );
61 l->addWidget( mChkOverride );
65void QgsCustomLayerOrderWidget::bridgeHasCustomLayerOrderChanged(
bool state )
67 mChkOverride->setChecked( state );
68 mView->setEnabled( state );
71void QgsCustomLayerOrderWidget::bridgeCustomLayerOrderChanged()
76void QgsCustomLayerOrderWidget::nodeVisibilityChanged(
QgsLayerTreeNode *node )
84void QgsCustomLayerOrderWidget::modelUpdated()
93 : QAbstractListModel( parent )
98int CustomLayerOrderModel::rowCount(
const QModelIndex & )
const
100 return mOrder.count();
103QVariant CustomLayerOrderModel::data(
const QModelIndex &index,
int role )
const
105 const QString
id = mOrder.at( index.row() );
107 if ( role == Qt::DisplayRole )
111 return layer->
name();
114 if ( role == Qt::UserRole + 1 )
121 if ( role == Qt::CheckStateRole )
131bool CustomLayerOrderModel::setData(
const QModelIndex &index,
const QVariant &value,
int role )
134 if ( role == Qt::CheckStateRole )
136 const QString
id = mOrder.at( index.row() );
147Qt::ItemFlags CustomLayerOrderModel::flags(
const QModelIndex &index )
const
149 if ( !index.isValid() )
150 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDropEnabled;
151 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsUserCheckable;
154Qt::DropActions CustomLayerOrderModel::supportedDropActions()
const
156 return Qt::CopyAction | Qt::MoveAction;
159QStringList CustomLayerOrderModel::mimeTypes()
const
162 types << QStringLiteral(
"application/qgis.layerorderdata" );
166QMimeData *CustomLayerOrderModel::mimeData(
const QModelIndexList &indexes )
const
169 const auto constIndexes = indexes;
170 for (
const QModelIndex &index : constIndexes )
171 lst << data( index, Qt::UserRole + 1 ).toString();
173 QMimeData *mimeData =
new QMimeData();
174 mimeData->setData( QStringLiteral(
"application/qgis.layerorderdata" ), lst.join( QLatin1Char(
'\n' ) ).toUtf8() );
178bool 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 const 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] );
203bool CustomLayerOrderModel::removeRows(
int row,
int count,
const QModelIndex &parent )
209 beginRemoveRows( QModelIndex(), row, row + count - 1 );
210 while ( --count >= 0 )
211 mOrder.removeAt( row );
216void CustomLayerOrderModel::refreshModel(
const QList<QgsMapLayer *> &order )
218 QStringList orderedIds;
219 const auto constOrder = order;
223 orderedIds.append( layer->
id() );
226 if ( orderedIds != mOrder )
234void CustomLayerOrderModel::updateLayerVisibility(
const QString &layerId )
236 const int row = mOrder.indexOf( layerId );
238 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.