Skip to content
Permalink
Browse files
[layertree] Implement more flags for LayerTreeModel (reorder nodes, r…
…ename nodes)
  • Loading branch information
wonder-sk committed May 22, 2014
1 parent 35f55e8 commit 6f5c775f98281e949d54ef07d968d0347646de8f
Showing with 31 additions and 13 deletions.
  1. +3 −1 src/app/qgisapp.cpp
  2. +21 −9 src/gui/layertree/qgslayertreemodel.cpp
  3. +7 −3 src/gui/layertree/qgslayertreemodel.h
@@ -2261,7 +2261,9 @@ void QgisApp::initLayerTreeView()
mLayerTreeDock->setAllowedAreas( Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea );

QgsLayerTreeModel* model = new QgsLayerTreeModel( QgsProject::instance()->layerTreeRoot(), this );
model->setFlag( QgsLayerTreeModel::AllowVisibilityManagement );
model->setFlag( QgsLayerTreeModel::AllowNodeReorder );
model->setFlag( QgsLayerTreeModel::AllowNodeRename );
model->setFlag( QgsLayerTreeModel::AllowNodeChangeVisibility );

mLayerTreeView->setModel( model );
mLayerTreeView->setMenuProvider( new QgsAppLayerTreeViewMenuProvider( mLayerTreeView, mMapCanvas ) );
@@ -216,7 +216,7 @@ QVariant QgsLayerTreeModel::data( const QModelIndex &index, int role ) const
}
else if ( role == Qt::CheckStateRole )
{
if ( !testFlag( AllowVisibilityManagement ) )
if ( !testFlag( AllowNodeChangeVisibility ) )
return QVariant();

if ( QgsLayerTree::isLayer( node ) )
@@ -256,17 +256,26 @@ QVariant QgsLayerTreeModel::data( const QModelIndex &index, int role ) const
Qt::ItemFlags QgsLayerTreeModel::flags( const QModelIndex& index ) const
{
if ( !index.isValid() )
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDropEnabled;
{
Qt::ItemFlags rootFlags = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
if ( testFlag( AllowNodeReorder ) )
rootFlags |= Qt::ItemIsDropEnabled;
return rootFlags;
}

if ( index2symnode( index ) )
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
return Qt::ItemIsEnabled; // | Qt::ItemIsSelectable;

Qt::ItemFlags f = Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsDragEnabled;
Qt::ItemFlags f = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
if ( testFlag( AllowNodeRename ) )
f |= Qt::ItemIsEditable;
if ( testFlag( AllowNodeReorder ) )
f |= Qt::ItemIsDragEnabled;
QgsLayerTreeNode* node = index2node( index );
if ( testFlag( AllowVisibilityManagement ) && QgsLayerTree::isLayer( node ) )
if ( testFlag( AllowNodeChangeVisibility ) && ( QgsLayerTree::isLayer( node ) || QgsLayerTree::isGroup( node ) ) )
f |= Qt::ItemIsUserCheckable;
else if ( QgsLayerTree::isGroup( node ) )
f |= Qt::ItemIsDropEnabled | Qt::ItemIsUserCheckable;
if ( testFlag( AllowNodeReorder ) && QgsLayerTree::isGroup( node ) )
f |= Qt::ItemIsDropEnabled;
return f;
}

@@ -278,7 +287,7 @@ bool QgsLayerTreeModel::setData( const QModelIndex& index, const QVariant& value

if ( role == Qt::CheckStateRole )
{
if ( !testFlag( AllowVisibilityManagement ) )
if ( !testFlag( AllowNodeChangeVisibility ) )
return false;

if ( QgsLayerTree::isLayer( node ) )
@@ -295,6 +304,9 @@ bool QgsLayerTreeModel::setData( const QModelIndex& index, const QVariant& value
}
else if ( role == Qt::EditRole )
{
if ( !testFlag( AllowNodeRename ) )
return false;

if ( QgsLayerTree::isLayer( node ) )
{
QgsLayerTreeLayer* layer = QgsLayerTree::toLayer( node );
@@ -697,7 +709,7 @@ void QgsLayerTreeModel::disconnectFromLayer( QgsLayerTreeLayer* nodeLayer )

Qt::DropActions QgsLayerTreeModel::supportedDropActions() const
{
return /*Qt::CopyAction |*/ Qt::MoveAction;
return Qt::MoveAction;
}

QStringList QgsLayerTreeModel::mimeTypes() const
@@ -69,9 +69,13 @@ class GUI_EXPORT QgsLayerTreeModel : public QAbstractItemModel

enum Flag
{
AllowTreeManagement,
ShowSymbology,
AllowVisibilityManagement,
// display flags
ShowSymbology = 0x0001, //!< Add symbology items for layer nodes

// behavioral flags
AllowNodeReorder = 0x1000, //!< Allow reordering with drag'n'drop
AllowNodeRename = 0x2000, //!< Allow renaming of groups and layers
AllowNodeChangeVisibility = 0x4000, //!< Allow user to set node visibility with a check box
};
Q_DECLARE_FLAGS( Flags, Flag )

0 comments on commit 6f5c775

Please sign in to comment.