mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-28 00:05:04 -04:00
Allow showing CRS in QgsMapLayerComboBox
This commit is contained in:
parent
959f97f682
commit
ec49341f85
python
src
tests/src/python
@ -54,6 +54,20 @@ class QgsMapLayerModel : QAbstractItemModel
|
|||||||
*/
|
*/
|
||||||
bool allowEmptyLayer() const;
|
bool allowEmptyLayer() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets whether the CRS of layers is also included in the model's display role.
|
||||||
|
* @see showCrs()
|
||||||
|
* @note added in QGIS 3.0
|
||||||
|
*/
|
||||||
|
void setShowCrs( bool showCrs );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if the model includes layer's CRS in the display role.
|
||||||
|
* @see setShowCrs()
|
||||||
|
* @note added in QGIS 3.0
|
||||||
|
*/
|
||||||
|
bool showCrs() const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief layersChecked returns the list of layers which are checked (or unchecked)
|
* @brief layersChecked returns the list of layers which are checked (or unchecked)
|
||||||
*/
|
*/
|
||||||
@ -75,15 +89,15 @@ class QgsMapLayerModel : QAbstractItemModel
|
|||||||
public:
|
public:
|
||||||
QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const;
|
QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const;
|
||||||
QModelIndex parent( const QModelIndex &child ) const;
|
QModelIndex parent( const QModelIndex &child ) const;
|
||||||
int rowCount( const QModelIndex &parent ) const;
|
int rowCount( const QModelIndex &parent = QModelIndex() ) const;
|
||||||
int columnCount( const QModelIndex &parent ) const;
|
int columnCount( const QModelIndex &parent = QModelIndex() ) const;
|
||||||
QVariant data( const QModelIndex &index, int role ) const;
|
QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns strings for all roles supported by this model.
|
* Returns strings for all roles supported by this model.
|
||||||
*/
|
*/
|
||||||
QHash<int, QByteArray> roleNames() const;
|
QHash<int, QByteArray> roleNames() const;
|
||||||
|
|
||||||
bool setData( const QModelIndex &index, const QVariant &value, int role );
|
bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::DisplayRole );
|
||||||
Qt::ItemFlags flags( const QModelIndex &index ) const;
|
Qt::ItemFlags flags( const QModelIndex &index ) const;
|
||||||
};
|
};
|
||||||
|
@ -42,6 +42,20 @@ class QgsMapLayerComboBox : QComboBox
|
|||||||
*/
|
*/
|
||||||
bool allowEmptyLayer() const;
|
bool allowEmptyLayer() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets whether the CRS of layers is also included in the combo box text.
|
||||||
|
* @see showCrs()
|
||||||
|
* @note added in QGIS 3.0
|
||||||
|
*/
|
||||||
|
void setShowCrs( bool showCrs );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if the combo box shows the layer's CRS.
|
||||||
|
* @see setShowCrs()
|
||||||
|
* @note added in QGIS 3.0
|
||||||
|
*/
|
||||||
|
bool showCrs() const;
|
||||||
|
|
||||||
/** Returns the current layer selected in the combo box.
|
/** Returns the current layer selected in the combo box.
|
||||||
* @see layer
|
* @see layer
|
||||||
*/
|
*/
|
||||||
|
@ -27,6 +27,7 @@ QgsMapLayerModel::QgsMapLayerModel( const QList<QgsMapLayer *>& layers, QObject
|
|||||||
, mLayersChecked( QMap<QString, Qt::CheckState>() )
|
, mLayersChecked( QMap<QString, Qt::CheckState>() )
|
||||||
, mItemCheckable( false )
|
, mItemCheckable( false )
|
||||||
, mAllowEmpty( false )
|
, mAllowEmpty( false )
|
||||||
|
, mShowCrs( false )
|
||||||
{
|
{
|
||||||
connect( QgsMapLayerRegistry::instance(), SIGNAL( layersWillBeRemoved( QStringList ) ), this, SLOT( removeLayers( QStringList ) ) );
|
connect( QgsMapLayerRegistry::instance(), SIGNAL( layersWillBeRemoved( QStringList ) ), this, SLOT( removeLayers( QStringList ) ) );
|
||||||
addLayers( layers );
|
addLayers( layers );
|
||||||
@ -37,6 +38,7 @@ QgsMapLayerModel::QgsMapLayerModel( QObject *parent )
|
|||||||
, mLayersChecked( QMap<QString, Qt::CheckState>() )
|
, mLayersChecked( QMap<QString, Qt::CheckState>() )
|
||||||
, mItemCheckable( false )
|
, mItemCheckable( false )
|
||||||
, mAllowEmpty( false )
|
, mAllowEmpty( false )
|
||||||
|
, mShowCrs( false )
|
||||||
{
|
{
|
||||||
connect( QgsMapLayerRegistry::instance(), SIGNAL( layersAdded( QList<QgsMapLayer*> ) ), this, SLOT( addLayers( QList<QgsMapLayer*> ) ) );
|
connect( QgsMapLayerRegistry::instance(), SIGNAL( layersAdded( QList<QgsMapLayer*> ) ), this, SLOT( addLayers( QList<QgsMapLayer*> ) ) );
|
||||||
connect( QgsMapLayerRegistry::instance(), SIGNAL( layersWillBeRemoved( QStringList ) ), this, SLOT( removeLayers( QStringList ) ) );
|
connect( QgsMapLayerRegistry::instance(), SIGNAL( layersWillBeRemoved( QStringList ) ), this, SLOT( removeLayers( QStringList ) ) );
|
||||||
@ -54,7 +56,7 @@ void QgsMapLayerModel::checkAll( Qt::CheckState checkState )
|
|||||||
{
|
{
|
||||||
mLayersChecked[key] = checkState;
|
mLayersChecked[key] = checkState;
|
||||||
}
|
}
|
||||||
emit dataChanged( index( 0, 0 ), index( mLayers.length() - 1, 0 ) );
|
emit dataChanged( index( 0, 0 ), index( rowCount() - 1, 0 ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
void QgsMapLayerModel::setAllowEmptyLayer( bool allowEmpty )
|
void QgsMapLayerModel::setAllowEmptyLayer( bool allowEmpty )
|
||||||
@ -76,6 +78,15 @@ void QgsMapLayerModel::setAllowEmptyLayer( bool allowEmpty )
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void QgsMapLayerModel::setShowCrs( bool showCrs )
|
||||||
|
{
|
||||||
|
if ( mShowCrs == showCrs )
|
||||||
|
return;
|
||||||
|
|
||||||
|
mShowCrs = showCrs;
|
||||||
|
emit dataChanged( index( 0, 0 ), index( rowCount() - 1, 0 ), QVector<int>() << Qt::DisplayRole );
|
||||||
|
}
|
||||||
|
|
||||||
QList<QgsMapLayer *> QgsMapLayerModel::layersChecked( Qt::CheckState checkState )
|
QList<QgsMapLayer *> QgsMapLayerModel::layersChecked( Qt::CheckState checkState )
|
||||||
{
|
{
|
||||||
QList<QgsMapLayer *> layers;
|
QList<QgsMapLayer *> layers;
|
||||||
@ -183,7 +194,17 @@ QVariant QgsMapLayerModel::data( const QModelIndex &index, int role ) const
|
|||||||
return QVariant();
|
return QVariant();
|
||||||
|
|
||||||
QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() );
|
QgsMapLayer* layer = static_cast<QgsMapLayer*>( index.internalPointer() );
|
||||||
return layer ? layer->name() : QVariant();
|
if ( !layer )
|
||||||
|
return QVariant();
|
||||||
|
|
||||||
|
if ( !mShowCrs )
|
||||||
|
{
|
||||||
|
return layer->name();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return tr( "%1 [%2]" ).arg( layer->name(), layer->crs().authid() );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( role == LayerIdRole )
|
if ( role == LayerIdRole )
|
||||||
|
@ -32,6 +32,11 @@ class QgsMapLayer;
|
|||||||
class CORE_EXPORT QgsMapLayerModel : public QAbstractItemModel
|
class CORE_EXPORT QgsMapLayerModel : public QAbstractItemModel
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
|
Q_PROPERTY( bool allowEmptyLayer READ allowEmptyLayer WRITE setAllowEmptyLayer )
|
||||||
|
Q_PROPERTY( bool showCrs READ showCrs WRITE setShowCrs )
|
||||||
|
Q_PROPERTY( bool itemsCheckable READ itemsCheckable WRITE setItemsCheckable )
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
//! Item data roles
|
//! Item data roles
|
||||||
@ -76,6 +81,20 @@ class CORE_EXPORT QgsMapLayerModel : public QAbstractItemModel
|
|||||||
*/
|
*/
|
||||||
bool allowEmptyLayer() const { return mAllowEmpty; }
|
bool allowEmptyLayer() const { return mAllowEmpty; }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets whether the CRS of layers is also included in the model's display role.
|
||||||
|
* @see showCrs()
|
||||||
|
* @note added in QGIS 3.0
|
||||||
|
*/
|
||||||
|
void setShowCrs( bool showCrs );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if the model includes layer's CRS in the display role.
|
||||||
|
* @see setShowCrs()
|
||||||
|
* @note added in QGIS 3.0
|
||||||
|
*/
|
||||||
|
bool showCrs() const { return mShowCrs; }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief layersChecked returns the list of layers which are checked (or unchecked)
|
* @brief layersChecked returns the list of layers which are checked (or unchecked)
|
||||||
*/
|
*/
|
||||||
@ -101,9 +120,9 @@ class CORE_EXPORT QgsMapLayerModel : public QAbstractItemModel
|
|||||||
public:
|
public:
|
||||||
QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
|
QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
|
||||||
QModelIndex parent( const QModelIndex &child ) const override;
|
QModelIndex parent( const QModelIndex &child ) const override;
|
||||||
int rowCount( const QModelIndex &parent ) const override;
|
int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
|
||||||
int columnCount( const QModelIndex &parent ) const override;
|
int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
|
||||||
QVariant data( const QModelIndex &index, int role ) const override;
|
QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns strings for all roles supported by this model.
|
* Returns strings for all roles supported by this model.
|
||||||
@ -112,12 +131,13 @@ class CORE_EXPORT QgsMapLayerModel : public QAbstractItemModel
|
|||||||
*/
|
*/
|
||||||
QHash<int, QByteArray> roleNames() const override;
|
QHash<int, QByteArray> roleNames() const override;
|
||||||
|
|
||||||
bool setData( const QModelIndex &index, const QVariant &value, int role ) override;
|
bool setData( const QModelIndex &index, const QVariant &value, int role = Qt::EditRole ) override;
|
||||||
Qt::ItemFlags flags( const QModelIndex &index ) const override;
|
Qt::ItemFlags flags( const QModelIndex &index ) const override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
bool mAllowEmpty;
|
bool mAllowEmpty;
|
||||||
|
bool mShowCrs;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // QGSMAPLAYERMODEL_H
|
#endif // QGSMAPLAYERMODEL_H
|
||||||
|
@ -38,6 +38,16 @@ bool QgsMapLayerComboBox::allowEmptyLayer() const
|
|||||||
return mProxyModel->sourceLayerModel()->allowEmptyLayer();
|
return mProxyModel->sourceLayerModel()->allowEmptyLayer();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void QgsMapLayerComboBox::setShowCrs( bool showCrs )
|
||||||
|
{
|
||||||
|
mProxyModel->sourceLayerModel()->setShowCrs( showCrs );
|
||||||
|
}
|
||||||
|
|
||||||
|
bool QgsMapLayerComboBox::showCrs() const
|
||||||
|
{
|
||||||
|
return mProxyModel->sourceLayerModel()->showCrs();
|
||||||
|
}
|
||||||
|
|
||||||
void QgsMapLayerComboBox::setLayer( QgsMapLayer *layer )
|
void QgsMapLayerComboBox::setLayer( QgsMapLayer *layer )
|
||||||
{
|
{
|
||||||
if ( !layer )
|
if ( !layer )
|
||||||
|
@ -32,6 +32,8 @@ class GUI_EXPORT QgsMapLayerComboBox : public QComboBox
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
Q_FLAGS( QgsMapLayerProxyModel::Filters )
|
Q_FLAGS( QgsMapLayerProxyModel::Filters )
|
||||||
Q_PROPERTY( QgsMapLayerProxyModel::Filters filters READ filters WRITE setFilters )
|
Q_PROPERTY( QgsMapLayerProxyModel::Filters filters READ filters WRITE setFilters )
|
||||||
|
Q_PROPERTY( bool allowEmptyLayer READ allowEmptyLayer WRITE setAllowEmptyLayer )
|
||||||
|
Q_PROPERTY( bool showCrs READ showCrs WRITE setShowCrs )
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
@ -67,6 +69,20 @@ class GUI_EXPORT QgsMapLayerComboBox : public QComboBox
|
|||||||
*/
|
*/
|
||||||
bool allowEmptyLayer() const;
|
bool allowEmptyLayer() const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets whether the CRS of layers is also included in the combo box text.
|
||||||
|
* @see showCrs()
|
||||||
|
* @note added in QGIS 3.0
|
||||||
|
*/
|
||||||
|
void setShowCrs( bool showCrs );
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if the combo box shows the layer's CRS.
|
||||||
|
* @see setShowCrs()
|
||||||
|
* @note added in QGIS 3.0
|
||||||
|
*/
|
||||||
|
bool showCrs() const;
|
||||||
|
|
||||||
/** Returns the current layer selected in the combo box.
|
/** Returns the current layer selected in the combo box.
|
||||||
* @see layer
|
* @see layer
|
||||||
*/
|
*/
|
||||||
|
@ -23,7 +23,7 @@ start_app()
|
|||||||
|
|
||||||
|
|
||||||
def create_layer(name):
|
def create_layer(name):
|
||||||
layer = QgsVectorLayer("Point?field=fldtxt:string&field=fldint:integer",
|
layer = QgsVectorLayer("Point?crs=EPSG:3111&field=fldtxt:string&field=fldint:integer",
|
||||||
name, "memory")
|
name, "memory")
|
||||||
return layer
|
return layer
|
||||||
|
|
||||||
@ -44,6 +44,11 @@ class TestQgsMapLayerModel(unittest.TestCase):
|
|||||||
m.setAllowEmptyLayer(False)
|
m.setAllowEmptyLayer(False)
|
||||||
self.assertFalse(m.allowEmptyLayer())
|
self.assertFalse(m.allowEmptyLayer())
|
||||||
|
|
||||||
|
m.setShowCrs(True)
|
||||||
|
self.assertTrue(m.showCrs())
|
||||||
|
m.setShowCrs(False)
|
||||||
|
self.assertFalse(m.showCrs())
|
||||||
|
|
||||||
def testAddingRemovingLayers(self):
|
def testAddingRemovingLayers(self):
|
||||||
# test model handles layer addition and removal
|
# test model handles layer addition and removal
|
||||||
m = QgsMapLayerModel()
|
m = QgsMapLayerModel()
|
||||||
@ -146,6 +151,21 @@ class TestQgsMapLayerModel(unittest.TestCase):
|
|||||||
|
|
||||||
QgsMapLayerRegistry.instance().removeMapLayers([l1.id(), l2.id()])
|
QgsMapLayerRegistry.instance().removeMapLayers([l1.id(), l2.id()])
|
||||||
|
|
||||||
|
def testDisplayRoleShowCrs(self):
|
||||||
|
l1 = create_layer('l1')
|
||||||
|
l2 = create_layer('l2')
|
||||||
|
QgsMapLayerRegistry.instance().addMapLayers([l1, l2])
|
||||||
|
m = QgsMapLayerModel()
|
||||||
|
m.setShowCrs(True)
|
||||||
|
self.assertEqual(m.data(m.index(0, 0), Qt.DisplayRole), 'l1 [EPSG:3111]')
|
||||||
|
self.assertEqual(m.data(m.index(1, 0), Qt.DisplayRole), 'l2 [EPSG:3111]')
|
||||||
|
m.setAllowEmptyLayer(True)
|
||||||
|
self.assertFalse(m.data(m.index(0, 0), Qt.DisplayRole))
|
||||||
|
self.assertEqual(m.data(m.index(1, 0), Qt.DisplayRole), 'l1 [EPSG:3111]')
|
||||||
|
self.assertEqual(m.data(m.index(2, 0), Qt.DisplayRole), 'l2 [EPSG:3111]')
|
||||||
|
|
||||||
|
QgsMapLayerRegistry.instance().removeMapLayers([l1.id(), l2.id()])
|
||||||
|
|
||||||
def testLayerIdRole(self):
|
def testLayerIdRole(self):
|
||||||
l1 = create_layer('l1')
|
l1 = create_layer('l1')
|
||||||
l2 = create_layer('l2')
|
l2 = create_layer('l2')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user