New flag to disable checkboxes for legend nodes in layer tree model

This commit is contained in:
Martin Dobias 2014-08-12 15:53:06 +07:00
parent 8dba8af60d
commit 0fc7fc7b07
3 changed files with 11 additions and 2 deletions

View File

@ -57,6 +57,7 @@ class QgsLayerTreeModel : QAbstractItemModel
AllowNodeReorder, //!< Allow reordering with drag'n'drop
AllowNodeRename, //!< Allow renaming of groups and layers
AllowNodeChangeVisibility, //!< Allow user to set node visibility with a check box
AllowSymbologyChangeState, //!< Allow check boxes for symbology items (if supported by layer's legend)
};
typedef QFlags<QgsLayerTreeModel::Flag> Flags;

View File

@ -33,7 +33,7 @@
QgsLayerTreeModel::QgsLayerTreeModel( QgsLayerTreeGroup* rootNode, QObject *parent )
: QAbstractItemModel( parent )
, mRootNode( rootNode )
, mFlags( ShowSymbology )
, mFlags( ShowSymbology | AllowSymbologyChangeState )
, mAutoCollapseSymNodesCount( -1 )
{
Q_ASSERT( mRootNode );
@ -166,6 +166,8 @@ QVariant QgsLayerTreeModel::data( const QModelIndex &index, int role ) const
if ( QgsLayerTreeModelLegendNode* sym = index2symnode( index ) )
{
if ( role == Qt::CheckStateRole && !testFlag( AllowSymbologyChangeState ) )
return QVariant();
return sym->data( role );
}
@ -289,7 +291,10 @@ Qt::ItemFlags QgsLayerTreeModel::flags( const QModelIndex& index ) const
if ( QgsLayerTreeModelLegendNode* symn = index2symnode( index ) )
{
return symn->flags();
Qt::ItemFlags f = symn->flags();
if ( !testFlag( AllowSymbologyChangeState ) )
f &= ~Qt::ItemIsUserCheckable;
return f;
}
Qt::ItemFlags f = Qt::ItemIsEnabled | Qt::ItemIsSelectable;
@ -321,6 +326,8 @@ bool QgsLayerTreeModel::setData( const QModelIndex& index, const QVariant& value
QgsLayerTreeModelLegendNode *sym = index2symnode( index );
if ( sym )
{
if ( role == Qt::CheckStateRole && !testFlag( AllowSymbologyChangeState ) )
return false;
bool res = sym->setData( value, role );
if ( res )
emit dataChanged( index, index );

View File

@ -77,6 +77,7 @@ class CORE_EXPORT QgsLayerTreeModel : public QAbstractItemModel
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
AllowSymbologyChangeState = 0x8000, //!< Allow check boxes for symbology items (if supported by layer's legend)
};
Q_DECLARE_FLAGS( Flags, Flag )