mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-14 00:07:35 -04:00
Allow showing CRS in QgsMapLayerComboBox
This commit is contained in:
parent
959f97f682
commit
ec49341f85
@ -54,6 +54,20 @@ class QgsMapLayerModel : QAbstractItemModel
|
||||
*/
|
||||
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)
|
||||
*/
|
||||
@ -75,15 +89,15 @@ class QgsMapLayerModel : QAbstractItemModel
|
||||
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;
|
||||
int rowCount( const QModelIndex &parent = QModelIndex() ) const;
|
||||
int columnCount( const QModelIndex &parent = QModelIndex() ) const;
|
||||
QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const;
|
||||
|
||||
/**
|
||||
* Returns strings for all roles supported by this model.
|
||||
*/
|
||||
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;
|
||||
};
|
||||
|
@ -42,6 +42,20 @@ class QgsMapLayerComboBox : QComboBox
|
||||
*/
|
||||
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.
|
||||
* @see layer
|
||||
*/
|
||||
|
@ -27,6 +27,7 @@ QgsMapLayerModel::QgsMapLayerModel( const QList<QgsMapLayer *>& layers, QObject
|
||||
, mLayersChecked( QMap<QString, Qt::CheckState>() )
|
||||
, mItemCheckable( false )
|
||||
, mAllowEmpty( false )
|
||||
, mShowCrs( false )
|
||||
{
|
||||
connect( QgsMapLayerRegistry::instance(), SIGNAL( layersWillBeRemoved( QStringList ) ), this, SLOT( removeLayers( QStringList ) ) );
|
||||
addLayers( layers );
|
||||
@ -37,6 +38,7 @@ QgsMapLayerModel::QgsMapLayerModel( QObject *parent )
|
||||
, mLayersChecked( QMap<QString, Qt::CheckState>() )
|
||||
, mItemCheckable( false )
|
||||
, mAllowEmpty( false )
|
||||
, mShowCrs( false )
|
||||
{
|
||||
connect( QgsMapLayerRegistry::instance(), SIGNAL( layersAdded( QList<QgsMapLayer*> ) ), this, SLOT( addLayers( QList<QgsMapLayer*> ) ) );
|
||||
connect( QgsMapLayerRegistry::instance(), SIGNAL( layersWillBeRemoved( QStringList ) ), this, SLOT( removeLayers( QStringList ) ) );
|
||||
@ -54,7 +56,7 @@ void QgsMapLayerModel::checkAll( Qt::CheckState 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 )
|
||||
@ -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 *> layers;
|
||||
@ -183,7 +194,17 @@ QVariant QgsMapLayerModel::data( const QModelIndex &index, int role ) const
|
||||
return QVariant();
|
||||
|
||||
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 )
|
||||
|
@ -32,6 +32,11 @@ class QgsMapLayer;
|
||||
class CORE_EXPORT QgsMapLayerModel : public QAbstractItemModel
|
||||
{
|
||||
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:
|
||||
|
||||
//! Item data roles
|
||||
@ -76,6 +81,20 @@ class CORE_EXPORT QgsMapLayerModel : public QAbstractItemModel
|
||||
*/
|
||||
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)
|
||||
*/
|
||||
@ -101,9 +120,9 @@ class CORE_EXPORT QgsMapLayerModel : public QAbstractItemModel
|
||||
public:
|
||||
QModelIndex index( int row, int column, const QModelIndex &parent = QModelIndex() ) const override;
|
||||
QModelIndex parent( const QModelIndex &child ) const override;
|
||||
int rowCount( const QModelIndex &parent ) const override;
|
||||
int columnCount( const QModelIndex &parent ) const override;
|
||||
QVariant data( const QModelIndex &index, int role ) const override;
|
||||
int rowCount( const QModelIndex &parent = QModelIndex() ) const override;
|
||||
int columnCount( const QModelIndex &parent = QModelIndex() ) const override;
|
||||
QVariant data( const QModelIndex &index, int role = Qt::DisplayRole ) const override;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
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;
|
||||
|
||||
private:
|
||||
|
||||
bool mAllowEmpty;
|
||||
bool mShowCrs;
|
||||
};
|
||||
|
||||
#endif // QGSMAPLAYERMODEL_H
|
||||
|
@ -38,6 +38,16 @@ bool QgsMapLayerComboBox::allowEmptyLayer() const
|
||||
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 )
|
||||
{
|
||||
if ( !layer )
|
||||
|
@ -32,6 +32,8 @@ class GUI_EXPORT QgsMapLayerComboBox : public QComboBox
|
||||
Q_OBJECT
|
||||
Q_FLAGS( QgsMapLayerProxyModel::Filters )
|
||||
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:
|
||||
|
||||
@ -67,6 +69,20 @@ class GUI_EXPORT QgsMapLayerComboBox : public QComboBox
|
||||
*/
|
||||
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.
|
||||
* @see layer
|
||||
*/
|
||||
|
@ -23,7 +23,7 @@ start_app()
|
||||
|
||||
|
||||
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")
|
||||
return layer
|
||||
|
||||
@ -44,6 +44,11 @@ class TestQgsMapLayerModel(unittest.TestCase):
|
||||
m.setAllowEmptyLayer(False)
|
||||
self.assertFalse(m.allowEmptyLayer())
|
||||
|
||||
m.setShowCrs(True)
|
||||
self.assertTrue(m.showCrs())
|
||||
m.setShowCrs(False)
|
||||
self.assertFalse(m.showCrs())
|
||||
|
||||
def testAddingRemovingLayers(self):
|
||||
# test model handles layer addition and removal
|
||||
m = QgsMapLayerModel()
|
||||
@ -146,6 +151,21 @@ class TestQgsMapLayerModel(unittest.TestCase):
|
||||
|
||||
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):
|
||||
l1 = create_layer('l1')
|
||||
l2 = create_layer('l2')
|
||||
|
Loading…
x
Reference in New Issue
Block a user