| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| /*************************************************************************** | ||
| qgsfieldcombobox.cpp | ||
| -------------------------------------- | ||
| Date : 01.04.2014 | ||
| Copyright : (C) 2014 Denis Rouzaud | ||
| Email : denis.rouzaud@gmail.com | ||
| *************************************************************************** | ||
| * * | ||
| * This program is free software; you can redistribute it and/or modify * | ||
| * it under the terms of the GNU General Public License as published by * | ||
| * the Free Software Foundation; either version 2 of the License, or * | ||
| * (at your option) any later version. * | ||
| * * | ||
| ***************************************************************************/ | ||
|
|
||
| #include "qgsfieldcombobox.h" | ||
| #include "qgsfieldmodel.h" | ||
| #include "qgsmaplayer.h" | ||
|
|
||
| QgsFieldComboBox::QgsFieldComboBox( QWidget *parent ) : | ||
| QComboBox( parent ) | ||
| { | ||
| mFieldModel = new QgsFieldModel( this ); | ||
| setModel( mFieldModel ); | ||
|
|
||
| connect( this, SIGNAL( currentIndexChanged( int ) ), this, SLOT( indexChanged( int ) ) ); | ||
| } | ||
|
|
||
| void QgsFieldComboBox::setLayer( QgsMapLayer *layer ) | ||
| { | ||
| mFieldModel->setLayer( layer ); | ||
| } | ||
|
|
||
| void QgsFieldComboBox::setField( QString fieldName ) | ||
| { | ||
| QModelIndex idx = mFieldModel->indexFromName( fieldName ); | ||
| if ( idx.isValid() ) | ||
| { | ||
| setCurrentIndex( idx.row() ); | ||
| } | ||
| else | ||
| { | ||
| setCurrentIndex( -1 ); | ||
| } | ||
| } | ||
|
|
||
| QString QgsFieldComboBox::currentField() | ||
| { | ||
| int i = currentIndex(); | ||
|
|
||
| const QModelIndex index = mFieldModel->index( i, 0 ); | ||
| if ( !index.isValid() ) | ||
| { | ||
| return ""; | ||
| } | ||
|
|
||
| QString name = mFieldModel->data( index, QgsFieldModel::FieldNameRole ).toString(); | ||
| return name; | ||
| } | ||
|
|
||
| void QgsFieldComboBox::indexChanged( int i ) | ||
| { | ||
| Q_UNUSED( i ); | ||
| QString name = currentField(); | ||
| emit fieldChanged( name ); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /*************************************************************************** | ||
| qgsfieldcombobox.h | ||
| -------------------------------------- | ||
| Date : 01.04.2014 | ||
| Copyright : (C) 2014 Denis Rouzaud | ||
| Email : denis.rouzaud@gmail.com | ||
| *************************************************************************** | ||
| * * | ||
| * This program is free software; you can redistribute it and/or modify * | ||
| * it under the terms of the GNU General Public License as published by * | ||
| * the Free Software Foundation; either version 2 of the License, or * | ||
| * (at your option) any later version. * | ||
| * * | ||
| ***************************************************************************/ | ||
|
|
||
| #ifndef QGSFIELDCOMBOBOX_H | ||
| #define QGSFIELDCOMBOBOX_H | ||
|
|
||
| #include <QComboBox> | ||
|
|
||
| class QgsFieldModel; | ||
| class QgsMapLayer; | ||
|
|
||
| /** | ||
| * @brief The QgsFieldComboBox is a combo box which displays the list of fields of a given layer. | ||
| * @note added in 2.3 | ||
| */ | ||
| class GUI_EXPORT QgsFieldComboBox : public QComboBox | ||
| { | ||
| Q_OBJECT | ||
| public: | ||
| /** | ||
| * @brief QgsFieldComboBox creates a combo box to display the fields of a layer. | ||
| * The layer can be either manually given or dynamically set by connecting the signal QgsMapLayerComboBox::layerChanged to the slot setLayer. | ||
| */ | ||
| explicit QgsFieldComboBox( QWidget *parent = 0 ); | ||
|
|
||
| /** | ||
| * @brief currentField returns the currently selected field | ||
| */ | ||
| QString currentField(); | ||
|
|
||
| signals: | ||
| /** | ||
| * @brief fieldChanged the signal is emitted when the currently selected field changes | ||
| */ | ||
| void fieldChanged( QString fieldName ); | ||
|
|
||
| public slots: | ||
| /** | ||
| * @brief setLayer sets the layer of which the fields are listed | ||
| */ | ||
| void setLayer( QgsMapLayer* layer ); | ||
| /** | ||
| * @brief setField sets the currently selected field | ||
| */ | ||
| void setField( QString fieldName ); | ||
|
|
||
| protected slots: | ||
| void indexChanged( int i ); | ||
|
|
||
| private: | ||
| QgsFieldModel* mFieldModel; | ||
|
|
||
| }; | ||
|
|
||
| #endif // QGSFIELDCOMBOBOX_H |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| /*************************************************************************** | ||
| qgsfieldmodel.cpp | ||
| -------------------------------------- | ||
| Date : 01.04.2014 | ||
| Copyright : (C) 2014 Denis Rouzaud | ||
| Email : denis.rouzaud@gmail.com | ||
| *************************************************************************** | ||
| * * | ||
| * This program is free software; you can redistribute it and/or modify * | ||
| * it under the terms of the GNU General Public License as published by * | ||
| * the Free Software Foundation; either version 2 of the License, or * | ||
| * (at your option) any later version. * | ||
| * * | ||
| ***************************************************************************/ | ||
|
|
||
| #include "qgsfieldmodel.h" | ||
| #include "qgsmaplayermodel.h" | ||
| #include "qgsmaplayerproxymodel.h" | ||
| #include "qgslogger.h" | ||
|
|
||
|
|
||
| const int QgsFieldModel::FieldIndexRole = Qt::UserRole + 1; | ||
| const int QgsFieldModel::FieldNameRole = Qt::UserRole + 2; | ||
|
|
||
| QgsFieldModel::QgsFieldModel( QObject *parent ) | ||
| : QAbstractItemModel( parent ) | ||
| , mLayer( NULL ) | ||
| { | ||
| } | ||
|
|
||
| QModelIndex QgsFieldModel::indexFromName( QString fieldName ) | ||
| { | ||
| int r = mFields.indexFromName( fieldName ); | ||
| return index( r, 0 ); | ||
| } | ||
|
|
||
|
|
||
| void QgsFieldModel::setLayer( QgsMapLayer *layer ) | ||
| { | ||
| if ( mLayer ) | ||
| { | ||
| disconnect( mLayer, SIGNAL( updatedFields() ), this, SLOT( updateFields() ) ); | ||
| disconnect( mLayer, SIGNAL( layerDeleted() ), this, SLOT( layerDeleted() ) ); | ||
| } | ||
| QgsVectorLayer* vl = dynamic_cast<QgsVectorLayer*>( layer ); | ||
| if ( vl ) | ||
| { | ||
| mLayer = vl; | ||
| connect( mLayer, SIGNAL( updatedFields() ), this, SLOT( updateFields() ) ); | ||
| connect( mLayer, SIGNAL( layerDeleted() ), this, SLOT( layerDeleted() ) ); | ||
| } | ||
| else | ||
| { | ||
| mLayer = 0; | ||
| } | ||
| updateFields(); | ||
| } | ||
|
|
||
| void QgsFieldModel::layerDeleted() | ||
| { | ||
| mLayer = 0; | ||
| updateFields(); | ||
| } | ||
|
|
||
| void QgsFieldModel::updateFields() | ||
| { | ||
| beginResetModel(); | ||
| if ( mLayer ) | ||
| mFields = mLayer->pendingFields(); | ||
| else | ||
| mFields = QgsFields(); | ||
| endResetModel(); | ||
| } | ||
|
|
||
| QModelIndex QgsFieldModel::index( int row, int column, const QModelIndex &parent ) const | ||
| { | ||
| Q_UNUSED( parent ); | ||
| if ( row < 0 || row >= mFields.count() ) | ||
| return QModelIndex(); | ||
|
|
||
| return createIndex( row, column, row ); | ||
| } | ||
|
|
||
| QModelIndex QgsFieldModel::parent( const QModelIndex &child ) const | ||
| { | ||
| Q_UNUSED( child ); | ||
| return QModelIndex(); | ||
| } | ||
|
|
||
| int QgsFieldModel::rowCount( const QModelIndex &parent ) const | ||
| { | ||
| Q_UNUSED( parent ); | ||
| return mFields.count(); | ||
| } | ||
|
|
||
| int QgsFieldModel::columnCount( const QModelIndex &parent ) const | ||
| { | ||
| Q_UNUSED( parent ); | ||
| return 1; | ||
| } | ||
|
|
||
| QVariant QgsFieldModel::data( const QModelIndex &index, int role ) const | ||
| { | ||
| if ( !index.isValid() ) | ||
| return QVariant(); | ||
|
|
||
| if ( !mLayer ) | ||
| return QVariant(); | ||
|
|
||
| if ( role == Qt::DisplayRole ) | ||
| { | ||
| QgsField field = mFields[index.internalId()]; | ||
| const QMap< QString, QString > aliases = mLayer->attributeAliases(); | ||
| QString alias = aliases.value( field.name(), field.name() ); | ||
| return alias; | ||
| } | ||
|
|
||
| if ( role == Qt::UserRole ) | ||
| { | ||
| QgsField field = mFields[index.internalId()]; | ||
| return field.name(); | ||
| } | ||
|
|
||
| return QVariant(); | ||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /*************************************************************************** | ||
| qgsfieldmodel.h | ||
| -------------------------------------- | ||
| Date : 01.04.2014 | ||
| Copyright : (C) 2014 Denis Rouzaud | ||
| Email : denis.rouzaud@gmail.com | ||
| *************************************************************************** | ||
| * * | ||
| * This program is free software; you can redistribute it and/or modify * | ||
| * it under the terms of the GNU General Public License as published by * | ||
| * the Free Software Foundation; either version 2 of the License, or * | ||
| * (at your option) any later version. * | ||
| * * | ||
| ***************************************************************************/ | ||
|
|
||
| #ifndef QGSFIELDMODEL_H | ||
| #define QGSFIELDMODEL_H | ||
|
|
||
| #include <QAbstractItemModel> | ||
| #include <QItemSelectionModel> | ||
| #include <QComboBox> | ||
|
|
||
| #include "qgsvectorlayer.h" | ||
|
|
||
| /** | ||
| * @brief The QgsFieldModel class is a model to display the list of fields of a layer in widgets. | ||
| * It can be associated with a QgsMapLayerModel to dynamically display a layer and its fields. | ||
| * @note added in 2.3 | ||
| */ | ||
|
|
||
| class GUI_EXPORT QgsFieldModel : public QAbstractItemModel | ||
| { | ||
| Q_OBJECT | ||
| public: | ||
| static const int FieldNameRole, FieldIndexRole; | ||
|
|
||
| /** | ||
| * @brief QgsFieldModel creates a model to display the fields of a given layer | ||
| */ | ||
| explicit QgsFieldModel( QObject *parent = 0 ); | ||
|
|
||
| /** | ||
| * @brief indexFromName returns the index corresponding to a given fieldName | ||
| */ | ||
| QModelIndex indexFromName( QString fieldName ); | ||
|
|
||
| public slots: | ||
| /** | ||
| * @brief setLayer sets the layer of whch fields are displayed | ||
| */ | ||
| void setLayer( QgsMapLayer *layer ); | ||
|
|
||
| protected slots: | ||
| void updateFields(); | ||
| void layerDeleted(); | ||
|
|
||
| protected: | ||
| QgsFields mFields; | ||
| QgsVectorLayer* mLayer; | ||
|
|
||
| // QAbstractItemModel interface | ||
| public: | ||
| QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const; | ||
| QModelIndex parent( const QModelIndex &child ) const; | ||
| int rowCount( const QModelIndex &parent ) const; | ||
| int columnCount( const QModelIndex &parent ) const; | ||
| QVariant data( const QModelIndex &index, int role ) const; | ||
|
|
||
|
|
||
| }; | ||
|
|
||
| #endif // QGSFIELDMODEL_H |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| /*************************************************************************** | ||
| qgsmaplayercombobox.cpp | ||
| -------------------------------------- | ||
| Date : 01.04.2014 | ||
| Copyright : (C) 2014 Denis Rouzaud | ||
| Email : denis.rouzaud@gmail.com | ||
| *************************************************************************** | ||
| * * | ||
| * This program is free software; you can redistribute it and/or modify * | ||
| * it under the terms of the GNU General Public License as published by * | ||
| * the Free Software Foundation; either version 2 of the License, or * | ||
| * (at your option) any later version. * | ||
| * * | ||
| ***************************************************************************/ | ||
|
|
||
| #include "qgsmaplayercombobox.h" | ||
| #include "qgsmaplayermodel.h" | ||
|
|
||
| QgsMapLayerComboBox::QgsMapLayerComboBox( QWidget *parent ) : | ||
| QComboBox( parent ) | ||
| { | ||
| mProxyModel = new QgsMapLayerProxyModel( this ); | ||
| setModel( mProxyModel ); | ||
|
|
||
| connect( this, SIGNAL( currentIndexChanged( int ) ), this, SLOT( indexChanged( int ) ) ); | ||
| } | ||
|
|
||
| void QgsMapLayerComboBox::setFilters( QgsMapLayerProxyModel::Filters filters ) | ||
| { | ||
| mProxyModel->setFilters( filters ); | ||
| } | ||
|
|
||
| void QgsMapLayerComboBox::setLayer( QgsMapLayer *layer ) | ||
| { | ||
| QModelIndex idx = mProxyModel->sourceLayerModel()->indexFromLayer( layer ); | ||
| if ( idx.isValid() ) | ||
| { | ||
| QModelIndex proxyIdx = mProxyModel->mapFromSource( idx ); | ||
| if ( proxyIdx.isValid() ) | ||
| { | ||
| setCurrentIndex( proxyIdx.row() ); | ||
| return; | ||
| } | ||
| } | ||
| setCurrentIndex( -1 ); | ||
| } | ||
|
|
||
| QgsMapLayer* QgsMapLayerComboBox::currentLayer() | ||
| { | ||
| int i = currentIndex(); | ||
|
|
||
| const QModelIndex proxyIndex = mProxyModel->index( i, 0 ); | ||
| if ( !proxyIndex.isValid() ) | ||
| { | ||
| return 0; | ||
| } | ||
|
|
||
| const QModelIndex index = mProxyModel->mapToSource( proxyIndex ); | ||
| if ( !index.isValid() ) | ||
| { | ||
| return 0; | ||
| } | ||
|
|
||
| QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() ); | ||
| if ( layer ) | ||
| { | ||
| return layer; | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| void QgsMapLayerComboBox::indexChanged( int i ) | ||
| { | ||
| Q_UNUSED( i ); | ||
| QgsMapLayer* layer = currentLayer(); | ||
| emit layerChanged( layer ); | ||
| } | ||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| /*************************************************************************** | ||
| qgsmaplayercombobox.h | ||
| -------------------------------------- | ||
| Date : 01.04.2014 | ||
| Copyright : (C) 2014 Denis Rouzaud | ||
| Email : denis.rouzaud@gmail.com | ||
| *************************************************************************** | ||
| * * | ||
| * This program is free software; you can redistribute it and/or modify * | ||
| * it under the terms of the GNU General Public License as published by * | ||
| * the Free Software Foundation; either version 2 of the License, or * | ||
| * (at your option) any later version. * | ||
| * * | ||
| ***************************************************************************/ | ||
|
|
||
| #ifndef QGSMAPLAYERCOMBOBOX_H | ||
| #define QGSMAPLAYERCOMBOBOX_H | ||
|
|
||
| #include <QComboBox> | ||
|
|
||
| #include "qgsmaplayerproxymodel.h" | ||
|
|
||
| class QgsMapLayer; | ||
| class QgsVectorLayer; | ||
|
|
||
| /** | ||
| * @brief The QgsMapLayerComboBox class is a combo box which displays the list of layers | ||
| * @note added in 2.3 | ||
| */ | ||
| class GUI_EXPORT QgsMapLayerComboBox : public QComboBox | ||
| { | ||
| Q_OBJECT | ||
| public: | ||
| /** | ||
| * @brief QgsMapLayerComboBox creates a combo box to dislpay the list of layers (currently in the registry). | ||
| * The layers can be filtered and/or ordered. | ||
| */ | ||
| explicit QgsMapLayerComboBox( QWidget *parent = 0 ); | ||
|
|
||
| /** | ||
| * @brief setFilters allows fitering according to layer type and/or geometry type. | ||
| */ | ||
| void setFilters( QgsMapLayerProxyModel::Filters filters ); | ||
|
|
||
| /** | ||
| * @brief currentLayer returns the current layer selected in the combo box | ||
| */ | ||
| QgsMapLayer* currentLayer(); | ||
|
|
||
| public slots: | ||
| /** | ||
| * @brief setLayer set the current layer selected in the combo | ||
| */ | ||
| void setLayer( QgsMapLayer* layer ); | ||
|
|
||
| signals: | ||
| /** | ||
| * @brief layerChanged this signal is emitted whenever the currently selected layer changes | ||
| */ | ||
| void layerChanged( QgsMapLayer* layer ); | ||
|
|
||
| protected slots: | ||
| void indexChanged( int i ); | ||
|
|
||
| private: | ||
| QgsMapLayerProxyModel* mProxyModel; | ||
|
|
||
| }; | ||
|
|
||
| #endif // QGSMAPLAYERCOMBOBOX_H |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,238 @@ | ||
| /*************************************************************************** | ||
| qgsmaplayermodel.cpp | ||
| -------------------------------------- | ||
| Date : 01.04.2014 | ||
| Copyright : (C) 2014 Denis Rouzaud | ||
| Email : denis.rouzaud@gmail.com | ||
| *************************************************************************** | ||
| * * | ||
| * This program is free software; you can redistribute it and/or modify * | ||
| * it under the terms of the GNU General Public License as published by * | ||
| * the Free Software Foundation; either version 2 of the License, or * | ||
| * (at your option) any later version. * | ||
| * * | ||
| ***************************************************************************/ | ||
|
|
||
| #include <QIcon> | ||
|
|
||
| #include "qgsmaplayermodel.h" | ||
| #include "qgsmaplayerregistry.h" | ||
| #include "qgsapplication.h" | ||
| #include "qgsvectorlayer.h" | ||
|
|
||
|
|
||
| const int QgsMapLayerModel::LayerIdRole = Qt::UserRole + 1; | ||
|
|
||
| QgsMapLayerModel::QgsMapLayerModel( QList<QgsMapLayer *> layers, QObject *parent ) | ||
| : QAbstractItemModel( parent ) | ||
| , mLayersChecked( QMap<QString, Qt::CheckState>() ) | ||
| , mItemCheckable( false ) | ||
| { | ||
| connect( QgsMapLayerRegistry::instance(), SIGNAL( removeLayers( QStringList ) ), this, SLOT( removeLayers( QStringList ) ) ); | ||
| addLayers( layers ); | ||
| } | ||
|
|
||
| QgsMapLayerModel::QgsMapLayerModel( QObject *parent ) | ||
| : QAbstractItemModel( parent ) | ||
| , mLayersChecked( QMap<QString, Qt::CheckState>() ) | ||
| , mItemCheckable( false ) | ||
| { | ||
| connect( QgsMapLayerRegistry::instance(), SIGNAL( layersAdded( QList<QgsMapLayer*> ) ), this, SLOT( addLayers( QList<QgsMapLayer*> ) ) ); | ||
| connect( QgsMapLayerRegistry::instance(), SIGNAL( layersRemoved( QStringList ) ), this, SLOT( removeLayers( QStringList ) ) ); | ||
| addLayers( QgsMapLayerRegistry::instance()->mapLayers().values() ); | ||
| } | ||
|
|
||
| void QgsMapLayerModel::setItemsCheckable( bool checkable ) | ||
| { | ||
| mItemCheckable = checkable; | ||
| } | ||
|
|
||
| void QgsMapLayerModel::checkAll( Qt::CheckState checkState ) | ||
| { | ||
| foreach ( const QString key, mLayersChecked.keys() ) | ||
| { | ||
| mLayersChecked[key] = checkState; | ||
| } | ||
| emit dataChanged( index( 0, 0 ), index( mLayers.length() - 1, 0 ) ); | ||
| } | ||
|
|
||
| QList<QgsMapLayer *> QgsMapLayerModel::layersChecked( Qt::CheckState checkState ) | ||
| { | ||
| QList<QgsMapLayer *> layers; | ||
| foreach ( QgsMapLayer* layer, mLayers ) | ||
| { | ||
| if ( mLayersChecked[layer->id()] == checkState ) | ||
| { | ||
| layers.append( layer ); | ||
| } | ||
| } | ||
| return layers; | ||
| } | ||
|
|
||
| QModelIndex QgsMapLayerModel::indexFromLayer( QgsMapLayer *layer ) | ||
| { | ||
| int r = mLayers.indexOf( layer ); | ||
| return index( r, 0 ); | ||
| } | ||
|
|
||
| void QgsMapLayerModel::removeLayers( const QStringList layerIds ) | ||
| { | ||
| foreach ( const QString layerId, layerIds ) | ||
| { | ||
| QModelIndex startIndex = index( 0, 0 ); | ||
| QModelIndexList list = match( startIndex, LayerIdRole, layerId, 1 ); | ||
| if ( list.count() ) | ||
| { | ||
| QModelIndex index = list[0]; | ||
| beginRemoveRows( QModelIndex(), index.row(), index.row() ); | ||
| mLayersChecked.remove( layerId ); | ||
| mLayers.removeAt( index.row() ); | ||
| endRemoveRows(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void QgsMapLayerModel::addLayers( QList<QgsMapLayer *> layers ) | ||
| { | ||
| beginInsertRows( QModelIndex(), mLayers.count(), mLayers.count() + layers.count() - 1 ); | ||
| foreach ( QgsMapLayer* layer, layers ) | ||
| { | ||
| mLayers.append( layer ); | ||
| mLayersChecked.insert( layer->id(), Qt::Unchecked ); | ||
| } | ||
| endInsertRows(); | ||
| } | ||
|
|
||
| QModelIndex QgsMapLayerModel::index( int row, int column, const QModelIndex &parent ) const | ||
| { | ||
| Q_UNUSED( parent ); | ||
| if ( row < 0 || row >= mLayers.length() ) | ||
| return QModelIndex(); | ||
|
|
||
| return createIndex( row, column, mLayers[row] ); | ||
| } | ||
|
|
||
| QModelIndex QgsMapLayerModel::parent( const QModelIndex &child ) const | ||
| { | ||
| Q_UNUSED( child ); | ||
| return QModelIndex(); | ||
| } | ||
|
|
||
| int QgsMapLayerModel::rowCount( const QModelIndex &parent ) const | ||
| { | ||
| Q_UNUSED( parent ); | ||
|
|
||
| return mLayers.length(); | ||
| } | ||
|
|
||
| int QgsMapLayerModel::columnCount( const QModelIndex &parent ) const | ||
| { | ||
| Q_UNUSED( parent ); | ||
| return 1; | ||
| } | ||
|
|
||
| QVariant QgsMapLayerModel::data( const QModelIndex &index, int role ) const | ||
| { | ||
| if ( !index.isValid() || !index.internalPointer() ) | ||
| return QVariant(); | ||
|
|
||
| if ( role == Qt::DisplayRole ) | ||
| { | ||
| QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() ); | ||
| return layer->name(); | ||
| } | ||
|
|
||
| if ( role == LayerIdRole ) | ||
| { | ||
| QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() ); | ||
| return layer->id(); | ||
| } | ||
|
|
||
| if ( role == Qt::CheckStateRole ) | ||
| { | ||
| QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() ); | ||
| return mLayersChecked[layer->id()]; | ||
| } | ||
|
|
||
| if ( role == Qt::DecorationRole ) | ||
| { | ||
| QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() ); | ||
| QgsMapLayer::LayerType type = layer->type(); | ||
| if ( role == Qt::DecorationRole ) | ||
| { | ||
| switch ( type ) | ||
| { | ||
| case QgsMapLayer::RasterLayer: | ||
| { | ||
| return QgsApplication::getThemeIcon( "/mIconRasterLayer.svg" ); | ||
| } | ||
|
|
||
| case QgsMapLayer::VectorLayer: | ||
| { | ||
| QgsVectorLayer* vl = dynamic_cast<QgsVectorLayer*>( layer ); | ||
| if ( !vl ) | ||
| { | ||
| return QIcon(); | ||
| } | ||
| QGis::GeometryType geomType = vl->geometryType(); | ||
| switch ( geomType ) | ||
| { | ||
| case QGis::Point: | ||
| { | ||
| return QgsApplication::getThemeIcon( "/mIconPointLayer.svg" ); | ||
| } | ||
| case QGis::Polygon : | ||
| { | ||
| return QgsApplication::getThemeIcon( "/mIconPolygonLayer.svg" ); | ||
| } | ||
| case QGis::Line : | ||
| { | ||
| return QgsApplication::getThemeIcon( "/mIconLineLayer.svg" ); | ||
| } | ||
| case QGis::NoGeometry : | ||
| { | ||
| return QgsApplication::getThemeIcon( "/mIconTableLayer.png" ); | ||
| } | ||
| default: | ||
| { | ||
| return QIcon(); | ||
| } | ||
| } | ||
| } | ||
| default: | ||
| { | ||
| return QIcon(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return QVariant(); | ||
| } | ||
|
|
||
|
|
||
| Qt::ItemFlags QgsMapLayerModel::flags( const QModelIndex &index ) const | ||
| { | ||
| Q_UNUSED( index ); | ||
|
|
||
| Qt::ItemFlags flags = Qt::ItemIsEnabled | Qt::ItemIsSelectable; | ||
| if ( mItemCheckable ) | ||
| { | ||
| flags |= Qt::ItemIsUserCheckable; | ||
| } | ||
| return flags; | ||
| } | ||
|
|
||
|
|
||
| bool QgsMapLayerModel::setData( const QModelIndex &index, const QVariant &value, int role ) | ||
| { | ||
| if ( role == Qt::CheckStateRole ) | ||
| { | ||
| QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() ); | ||
| mLayersChecked[layer->id()] = ( Qt::CheckState )value.toInt(); | ||
| emit dataChanged( index, index ); | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| /*************************************************************************** | ||
| qgsmaplayermodel.h | ||
| -------------------------------------- | ||
| Date : 01.04.2014 | ||
| Copyright : (C) 2014 Denis Rouzaud | ||
| Email : denis.rouzaud@gmail.com | ||
| *************************************************************************** | ||
| * * | ||
| * This program is free software; you can redistribute it and/or modify * | ||
| * it under the terms of the GNU General Public License as published by * | ||
| * the Free Software Foundation; either version 2 of the License, or * | ||
| * (at your option) any later version. * | ||
| * * | ||
| ***************************************************************************/ | ||
|
|
||
| #ifndef QGSMAPLAYERMODEL_H | ||
| #define QGSMAPLAYERMODEL_H | ||
|
|
||
| #include <QAbstractItemModel> | ||
| #include <QSortFilterProxyModel> | ||
| #include <QStringList> | ||
|
|
||
| class QgsMapLayer; | ||
|
|
||
|
|
||
| /** | ||
| * @brief The QgsMapLayerModel class is a model to display layers in widgets. | ||
| * @see QgsMapLayerProxyModel to sort and/filter the layers | ||
| * @see QgsFieldModel to combine in with a field selector. | ||
| * @note added in 2.3 | ||
| */ | ||
| class GUI_EXPORT QgsMapLayerModel : public QAbstractItemModel | ||
| { | ||
| Q_OBJECT | ||
| public: | ||
| static const int LayerIdRole; | ||
|
|
||
| /** | ||
| * @brief QgsMapLayerModel creates a model to display layers in widgets. | ||
| */ | ||
| explicit QgsMapLayerModel( QObject *parent = 0 ); | ||
| /** | ||
| * @brief QgsMapLayerModel creates a model to display a specific list of layers in a widget. | ||
| */ | ||
| explicit QgsMapLayerModel( QList<QgsMapLayer*> layers, QObject *parent = 0 ); | ||
|
|
||
| /** | ||
| * @brief setItemsCheckable defines if layers should be selectable in the widget | ||
| */ | ||
| void setItemsCheckable( bool checkable ); | ||
| /** | ||
| * @brief checkAll changes the checkstate for all the layers | ||
| */ | ||
| void checkAll( Qt::CheckState checkState ); | ||
| /** | ||
| * @brief layersChecked returns the list of layers which are checked (or unchecked) | ||
| */ | ||
| QList<QgsMapLayer*> layersChecked( Qt::CheckState checkState = Qt::Checked ); | ||
| //! returns if the items can be checked or not | ||
| bool itemsCheckable() {return mItemCheckable;} | ||
|
|
||
| /** | ||
| * @brief indexFromLayer returns the model index for a given layer | ||
| */ | ||
| QModelIndex indexFromLayer( QgsMapLayer* layer ); | ||
|
|
||
|
|
||
| protected slots: | ||
| void removeLayers( const QStringList layerIds ); | ||
| void addLayers( QList<QgsMapLayer*> layers ); | ||
|
|
||
| protected: | ||
| QList<QgsMapLayer*> mLayers; | ||
| QMap<QString, Qt::CheckState> mLayersChecked; | ||
| bool mItemCheckable; | ||
|
|
||
| // QAbstractItemModel interface | ||
| public: | ||
| QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const; | ||
| QModelIndex parent( const QModelIndex &child ) const; | ||
| int rowCount( const QModelIndex &parent ) const; | ||
| int columnCount( const QModelIndex &parent ) const; | ||
| QVariant data( const QModelIndex &index, int role ) const; | ||
| bool setData( const QModelIndex &index, const QVariant &value, int role ); | ||
| Qt::ItemFlags flags( const QModelIndex &index ) const; | ||
| }; | ||
|
|
||
| #endif // QGSMAPLAYERMODEL_H |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| /*************************************************************************** | ||
| qgsmaplayerproxymodel.cpp | ||
| -------------------------------------- | ||
| Date : 01.04.2014 | ||
| Copyright : (C) 2014 Denis Rouzaud | ||
| Email : denis.rouzaud@gmail.com | ||
| *************************************************************************** | ||
| * * | ||
| * This program is free software; you can redistribute it and/or modify * | ||
| * it under the terms of the GNU General Public License as published by * | ||
| * the Free Software Foundation; either version 2 of the License, or * | ||
| * (at your option) any later version. * | ||
| * * | ||
| ***************************************************************************/ | ||
|
|
||
| #include "qgsmaplayerproxymodel.h" | ||
| #include "qgsmaplayermodel.h" | ||
| #include "qgsmaplayer.h" | ||
| #include "qgsvectorlayer.h" | ||
|
|
||
| QgsMapLayerProxyModel::QgsMapLayerProxyModel( QObject *parent ) | ||
| : QSortFilterProxyModel( parent ) | ||
| , mFilters( NoFilter ) | ||
| , mModel( new QgsMapLayerModel( this ) ) | ||
| { | ||
| setSourceModel( mModel ); | ||
| } | ||
|
|
||
| QgsMapLayerProxyModel *QgsMapLayerProxyModel::setFilters( Filters filters ) | ||
| { | ||
| mFilters = filters; | ||
| return this; | ||
| } | ||
|
|
||
| bool QgsMapLayerProxyModel::filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const | ||
| { | ||
| if ( mFilters.testFlag( NoFilter ) ) | ||
| return true; | ||
|
|
||
| QModelIndex index = sourceModel()->index( source_row, 0, source_parent ); | ||
| QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() ); | ||
| if ( !layer ) | ||
| return false; | ||
|
|
||
| // layer type | ||
| if (( mFilters.testFlag( RasterLayer ) && layer->type() == QgsMapLayer::RasterLayer ) || | ||
| ( mFilters.testFlag( VectorLayer ) && layer->type() == QgsMapLayer::VectorLayer ) ) | ||
| return true; | ||
|
|
||
| // geometry type | ||
| bool detectGeometry = mFilters.testFlag( NoGeometry ) || | ||
| mFilters.testFlag( PointLayer ) || | ||
| mFilters.testFlag( LineLayer ) || | ||
| mFilters.testFlag( PolygonLayer ) || | ||
| mFilters.testFlag( HasGeometry ); | ||
| if ( detectGeometry && layer->type() == QgsMapLayer::VectorLayer ) | ||
| { | ||
| QgsVectorLayer* vl = dynamic_cast<QgsVectorLayer*>( layer ); | ||
| if ( vl ) | ||
| { | ||
| if ( mFilters.testFlag( HasGeometry ) && vl->hasGeometryType() ) | ||
| return true; | ||
| if ( mFilters.testFlag( NoGeometry ) && vl->geometryType() == QGis::NoGeometry ) | ||
| return true; | ||
| if ( mFilters.testFlag( PointLayer ) && vl->geometryType() == QGis::Point ) | ||
| return true; | ||
| if ( mFilters.testFlag( LineLayer ) && vl->geometryType() == QGis::Line ) | ||
| return true; | ||
| if ( mFilters.testFlag( PolygonLayer ) && vl->geometryType() == QGis::Polygon ) | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| bool QgsMapLayerProxyModel::lessThan( const QModelIndex &left, const QModelIndex &right ) const | ||
| { | ||
| // default mode is alphabetical order | ||
| QString leftId = sourceModel()->data( left ).toString(); | ||
| QString rightId = sourceModel()->data( right ).toString(); | ||
| return QString::localeAwareCompare( leftId, rightId ) < 0; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /*************************************************************************** | ||
| qgsmaplayerproxymodel.h | ||
| -------------------------------------- | ||
| Date : 01.04.2014 | ||
| Copyright : (C) 2014 Denis Rouzaud | ||
| Email : denis.rouzaud@gmail.com | ||
| *************************************************************************** | ||
| * * | ||
| * This program is free software; you can redistribute it and/or modify * | ||
| * it under the terms of the GNU General Public License as published by * | ||
| * the Free Software Foundation; either version 2 of the License, or * | ||
| * (at your option) any later version. * | ||
| * * | ||
| ***************************************************************************/ | ||
|
|
||
| #ifndef QGSMAPLAYERPROXYMODEL_H | ||
| #define QGSMAPLAYERPROXYMODEL_H | ||
|
|
||
| #include <QSortFilterProxyModel> | ||
|
|
||
| class QgsMapLayerModel; | ||
|
|
||
| /** | ||
| * @brief The QgsMapLayerProxModel class provides an easy to use model to display the list of layers in widgets. | ||
| * @note added in 2.3 | ||
| */ | ||
| class GUI_EXPORT QgsMapLayerProxyModel : public QSortFilterProxyModel | ||
| { | ||
| Q_OBJECT | ||
| public: | ||
| enum Filter | ||
| { | ||
| NoFilter = 1, | ||
| RasterLayer = 2, | ||
| NoGeometry = 4, | ||
| PointLayer = 8, | ||
| LineLayer = 16, | ||
| PolygonLayer = 32, | ||
| HasGeometry = PointLayer | LineLayer | PolygonLayer, | ||
| VectorLayer = NoGeometry | HasGeometry | ||
| }; | ||
| Q_DECLARE_FLAGS( Filters, Filter ) | ||
|
|
||
| /** | ||
| * @brief QgsMapLayerProxModel creates a proxy model with a QgsMapLayerModel as source model. | ||
| * It can be used to filter the layers list in a widget. | ||
| */ | ||
| explicit QgsMapLayerProxyModel( QObject *parent = 0 ); | ||
|
|
||
| /** | ||
| * @brief layerModel returns the QgsMapLayerModel used in this QSortFilterProxyModel | ||
| */ | ||
| QgsMapLayerModel* sourceLayerModel() {return mModel;} | ||
|
|
||
| /** | ||
| * @brief setFilters set flags that affect how layers are filtered | ||
| * @param filters are Filter flags | ||
| * @note added in 2.3 | ||
| */ | ||
| QgsMapLayerProxyModel* setFilters( Filters filters ); | ||
| const Filters& filters() const { return mFilters; } | ||
|
|
||
| private: | ||
| Filters mFilters; | ||
| QgsMapLayerModel* mModel; | ||
|
|
||
| // QSortFilterProxyModel interface | ||
| public: | ||
| bool filterAcceptsRow( int source_row, const QModelIndex &source_parent ) const; | ||
| bool lessThan( const QModelIndex &left, const QModelIndex &right ) const; | ||
| }; | ||
|
|
||
| Q_DECLARE_OPERATORS_FOR_FLAGS( QgsMapLayerProxyModel::Filters ) | ||
|
|
||
| #endif // QGSMAPLAYERPROXYMODEL_H |