21 #include <QVBoxLayout> 34 : QAbstractListModel( parent ),
mBridge( bridge )
38 int rowCount(
const QModelIndex & )
const override 43 QVariant
data(
const QModelIndex &
index,
int role )
const override 45 QString
id =
mOrder.at( index.row() );
47 if ( role == Qt::DisplayRole )
54 if ( role == Qt::UserRole + 1 )
61 if ( role == Qt::CheckStateRole )
71 bool setData(
const QModelIndex &
index,
const QVariant &value,
int role )
override 73 if ( role == Qt::CheckStateRole )
75 QString
id =
mOrder.at( index.row() );
79 nodeLayer->
setVisible(( Qt::CheckState )value.toInt() );
86 Qt::ItemFlags
flags(
const QModelIndex &
index )
const override 88 if ( !index.isValid() )
89 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDropEnabled;
90 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsUserCheckable;
95 return Qt::MoveAction;
101 types <<
"application/qgis.layerorderdata";
105 QMimeData*
mimeData(
const QModelIndexList& indexes )
const override 108 foreach ( QModelIndex
index, indexes )
109 lst <<
data( index, Qt::UserRole + 1 ).toString();
111 QMimeData*
mimeData =
new QMimeData();
112 mimeData->setData(
"application/qgis.layerorderdata", lst.join(
"\n" ).toUtf8() );
116 bool dropMimeData(
const QMimeData *
data, Qt::DropAction action,
int row,
int column,
const QModelIndex &parent )
override 121 if ( action == Qt::IgnoreAction )
124 if ( !data->hasFormat(
"application/qgis.layerorderdata" ) )
127 QByteArray encodedData = data->data(
"application/qgis.layerorderdata" );
128 QStringList lst = QString::fromUtf8( encodedData ).split(
"\n" );
133 beginInsertRows( QModelIndex(), row, row + lst.count() - 1 );
134 for (
int i = 0; i < lst.count(); ++i )
135 mOrder.insert( row + i, lst[i] );
141 bool removeRows(
int row,
int count,
const QModelIndex& parent )
override 147 beginRemoveRows( QModelIndex(), row, row + count - 1 );
148 while ( --count >= 0 )
165 int row =
mOrder.indexOf( layerId );
167 emit dataChanged(
index( row ),
index( row ) );
182 mView =
new QListView(
this );
183 mView->setDragEnabled(
true );
184 mView->setAcceptDrops(
true );
185 mView->setDropIndicatorShown(
true );
186 mView->setDragDropMode( QAbstractItemView::InternalMove );
187 mView->setSelectionMode( QAbstractItemView::ExtendedSelection );
193 connect(
mChkOverride, SIGNAL( toggled(
bool ) ), bridge, SLOT( setHasCustomLayerOrder(
bool ) ) );
198 connect(
mModel, SIGNAL( rowsInserted( QModelIndex,
int,
int ) ),
this, SLOT(
modelUpdated() ) );
199 connect(
mModel, SIGNAL( rowsRemoved( QModelIndex,
int,
int ) ),
this, SLOT(
modelUpdated() ) );
203 QVBoxLayout* l =
new QVBoxLayout;
205 l->addWidget(
mView );
214 mView->setEnabled( state );
QMimeData * mimeData(const QModelIndexList &indexes) const override
Base class for all map layer types.
void setCustomLayerOrder(const QStringList &order)
bool setData(const QModelIndex &index, const QVariant &value, int role) override
The QgsLayerTreeMapCanvasBridge class takes care of updates of layer set for QgsMapCanvas from a laye...
static QgsMapLayerRegistry * instance()
CustomLayerOrderModel(QgsLayerTreeMapCanvasBridge *bridge, QObject *parent=0)
bool removeRows(int row, int count, const QModelIndex &parent) override
QVariant data(const QModelIndex &index, int role) const override
QStringList defaultLayerOrder() const
const QString & name() const
Get the display name of the layer.
QgsLayerTreeGroup * rootGroup() const
Qt::CheckState isVisible() const
Qt::DropActions supportedDropActions() const override
This class is a base class for nodes in a layer tree.
QString id() const
Get this layer's unique ID, this ID is used to access this layer from map layer registry.
void setVisible(Qt::CheckState visible)
bool isLayer(QgsLayerTreeNode *node)
Check whether the node is a valid layer node.
Qt::ItemFlags flags(const QModelIndex &index) const override
QStringList customLayerOrder() const
int rowCount(const QModelIndex &) const override
QStringList order() const
QgsLayerTreeLayer * toLayer(QgsLayerTreeNode *node)
Cast node to a layer. No type checking is done - use isLayer() to find out whether this operation is ...
QgsLayerTreeLayer * findLayer(const QString &layerId) const
Find layer node representing the map layer specified by its ID. Searches recursively the whole sub-tr...
QgsLayerTreeMapCanvasBridge * mBridge
QgsMapLayer * mapLayer(QString theLayerId)
Retrieve a pointer to a loaded layer by id.
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
bool hasCustomLayerOrder() const
QStringList mimeTypes() const override
void updateLayerVisibility(const QString &layerId)
void refreshModel(const QStringList &order)
Layer tree node points to a map layer.