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;
60 l->setContentsMargins( 0, 0, 0, 0 );
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 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 )
133 bool CustomLayerOrderModel::setData(
const QModelIndex &index,
const QVariant &value,
int role )
136 if ( role == Qt::CheckStateRole )
138 const QString
id = mOrder.at( index.row() );
149 Qt::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;
156 Qt::DropActions CustomLayerOrderModel::supportedDropActions()
const
158 return Qt::CopyAction | Qt::MoveAction;
161 QStringList CustomLayerOrderModel::mimeTypes()
const
164 types << QStringLiteral(
"application/qgis.layerorderdata" );
168 QMimeData *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() );
180 bool 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] );
205 bool CustomLayerOrderModel::removeRows(
int row,
int count,
const QModelIndex &parent )
211 beginRemoveRows( QModelIndex(), row, row + count - 1 );
212 while ( --count >= 0 )
213 mOrder.removeAt( row );
218 void CustomLayerOrderModel::refreshModel(
const QList<QgsMapLayer *> &order )
220 QStringList orderedIds;
221 const auto constOrder = order;
225 orderedIds.append( layer->
id() );
228 if ( orderedIds != mOrder )
236 void CustomLayerOrderModel::updateLayerVisibility(
const QString &layerId )
238 const int row = mOrder.indexOf( layerId );
240 emit dataChanged( index( row ), index( row ) );