Skip to content
Permalink
Browse files

QgsMapLayerModel: allow direct retrieval of map layer

  • Loading branch information
m-kuhn committed Mar 9, 2016
1 parent de06cd7 commit ee46ece2da28011202727b067404a215cec477c2
@@ -1371,7 +1371,7 @@ INCLUDE_FILE_PATTERNS =
# undefined via #undef or recursively expanded use the := operator
# instead of the = operator.

PREDEFINED =
PREDEFINED = "QT_VERSION=0x040800"

# If the MACRO_EXPANSION and EXPAND_ONLY_PREDEF tags are set to YES then
# this tag can be used to specify a list of macro names that should be expanded.
@@ -13,7 +13,11 @@ class QgsMapLayerModel : QAbstractItemModel
%End

public:
static const int LayerIdRole;
enum
{
LayerIdRole,
LayerRole
};

/**
* @brief QgsMapLayerModel creates a model to display layers in widgets.
@@ -56,6 +60,14 @@ class QgsMapLayerModel : QAbstractItemModel
int rowCount( const QModelIndex &parent ) const;
int columnCount( const QModelIndex &parent ) const;
QVariant data( const QModelIndex &index, int role ) const;
%If (QT5_SUPPORT)
/**
* Returns strings for all roles supported by this model.
*
* @note Available only with Qt5 (python and c++)
*/
QHash<int, QByteArray> roleNames() const;
%End
bool setData( const QModelIndex &index, const QVariant &value, int role );
Qt::ItemFlags flags( const QModelIndex &index ) const;
};
@@ -759,4 +759,6 @@ class CORE_EXPORT QgsMapLayer : public QObject
QgsMapLayerStyleManager* mStyleManager;
};

Q_DECLARE_METATYPE( QgsMapLayer* )

#endif
@@ -22,8 +22,6 @@
#include "qgsvectorlayer.h"


const int QgsMapLayerModel::LayerIdRole = Qt::UserRole + 1;

QgsMapLayerModel::QgsMapLayerModel( const QList<QgsMapLayer *>& layers, QObject *parent )
: QAbstractItemModel( parent )
, mLayersChecked( QMap<QString, Qt::CheckState>() )
@@ -151,6 +149,11 @@ QVariant QgsMapLayerModel::data( const QModelIndex &index, int role ) const
return layer->id();
}

if ( role == LayerRole )
{
return QVariant::fromValue<QgsMapLayer*>( static_cast<QgsMapLayer*>( index.internalPointer() ) );
}

if ( role == Qt::CheckStateRole && mItemCheckable )
{
QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() );
@@ -213,6 +216,16 @@ QVariant QgsMapLayerModel::data( const QModelIndex &index, int role ) const
return QVariant();
}

#if QT_VERSION >= 0x050000
QHash<int, QByteArray> QgsMapLayerModel::roleNames() const
{
QHash<int, QByteArray> roles = QAbstractItemModel::roleNames();
roles[LayerIdRole] = "layerId";
roles[LayerRole] = "layer";

return roles;
}
#endif

Qt::ItemFlags QgsMapLayerModel::flags( const QModelIndex &index ) const
{
@@ -29,11 +29,16 @@ class QgsMapLayer;
* @see QgsFieldModel to combine in with a field selector.
* @note added in 2.3
*/
// TODO QGIS3: move to core
class GUI_EXPORT QgsMapLayerModel : public QAbstractItemModel
{
Q_OBJECT
public:
static const int LayerIdRole;
enum
{
LayerIdRole = Qt::UserRole + 1,
LayerRole
};

/**
* @brief QgsMapLayerModel creates a model to display layers in widgets.
@@ -81,6 +86,16 @@ class GUI_EXPORT QgsMapLayerModel : public QAbstractItemModel
int rowCount( const QModelIndex &parent ) const override;
int columnCount( const QModelIndex &parent ) const override;
QVariant data( const QModelIndex &index, int role ) const override;
///@cond PRIVATE
#if QT_VERSION >= 0x050000
/**
* Returns strings for all roles supported by this model.
*
* @note Available only with Qt5 (python and c++)
*/
QHash<int, QByteArray> roleNames() const override;
#endif
///@endcond
bool setData( const QModelIndex &index, const QVariant &value, int role ) override;
Qt::ItemFlags flags( const QModelIndex &index ) const override;
};

0 comments on commit ee46ece

Please sign in to comment.
You can’t perform that action at this time.