Skip to content

Commit f856b4c

Browse files
committedNov 5, 2018
Renamed originalXmlProperties methods and variables
1 parent 9cfb436 commit f856b4c

File tree

10 files changed

+98
-25
lines changed

10 files changed

+98
-25
lines changed
 

‎python/core/auto_generated/layertree/qgslayertreeutils.sip.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ Returns true if any of the layers is modified
5959
Removes layer nodes that refer to invalid layers
6060
%End
6161

62-
static void storeInvalidLayersProperties( QgsLayerTreeGroup *group, const QDomDocument *doc );
62+
static void storeOriginalLayersProperties( QgsLayerTreeGroup *group, const QDomDocument *doc );
6363
%Docstring
64-
Stores in a custom layer node property the layer properties XML information for an invalid layer
64+
Stores in a layer's originalXmlProperties the layer properties information
6565

6666
.. versionadded:: 3.6
6767
%End

‎python/core/auto_generated/qgsmaplayer.sip.in

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,6 +1237,24 @@ Returns true if the refresh on provider nofification is enabled
12371237
.. versionadded:: 3.0
12381238
%End
12391239

1240+
QString originalXmlProperties() const;
1241+
%Docstring
1242+
Returns the XML properties of the original layer as they were when the layer
1243+
was first read from the project file. In case of new layers this is normally empty.
1244+
1245+
The storage format for the XML is qlr
1246+
1247+
.. versionadded:: 3.6
1248+
%End
1249+
1250+
void setOriginalXmlProperties( const QString &originalXmlProperties );
1251+
%Docstring
1252+
Sets the original XML properties for the layer to ``originalXmlProperties``
1253+
1254+
The storage format for the XML is qlr
1255+
1256+
.. versionadded:: 3.6
1257+
%End
12401258

12411259
public slots:
12421260

‎src/app/qgslayertreeviewbadlayerindicator.cpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "qgslayertreeutils.h"
2121
#include "qgslayertreemodel.h"
2222
#include "qgsvectorlayer.h"
23+
#include "qgsrasterlayer.h"
2324
#include "qgisapp.h"
2425
#include "qgsbrowsermodel.h"
2526
#include "qgsbrowsertreeview.h"
@@ -40,14 +41,21 @@ void QgsLayerTreeViewBadLayerIndicatorProvider::onIndicatorClicked( const QModel
4041
if ( !QgsLayerTree::isLayer( node ) )
4142
return;
4243

43-
QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( QgsLayerTree::toLayer( node )->layer() );
44-
if ( !vlayer )
44+
// Only raster/vector for now are supported
45+
QgsMapLayer *layer = nullptr;
46+
if ( qobject_cast<QgsVectorLayer *>( QgsLayerTree::toLayer( node )->layer() ) ||
47+
qobject_cast<QgsRasterLayer *>( QgsLayerTree::toLayer( node )->layer() ) )
48+
{
49+
layer = qobject_cast<QgsMapLayer *>( QgsLayerTree::toLayer( node )->layer() );
50+
}
51+
if ( !layer )
4552
return;
4653

47-
// TODO: raster layers
48-
4954
// Get provider type
50-
QString providerType( vlayer->providerType() );
55+
QString providerType( layer->providerType() );
56+
QgsMapLayer::LayerType layerType( layer->type() );
57+
58+
// Builds the dialog to select a new data source
5159
QgsBrowserModel browserModel;
5260
browserModel.initialize();
5361
QDialog dlg;
@@ -69,8 +77,8 @@ void QgsLayerTreeViewBadLayerIndicatorProvider::onIndicatorClicked( const QModel
6977
{
7078
if ( index.isValid() )
7179
{
72-
const QgsDataItem *item( browserModel.dataItem( index ) );
73-
if ( item->mimeUri().layerType == QStringLiteral( "vector" ) && item->mimeUri().providerKey == providerType )
80+
const QgsLayerItem *item = qobject_cast<QgsLayerItem *>( browserModel.dataItem( index ) );
81+
if ( item && item->mapLayerType() == layerType && item->providerKey() == providerType )
7482
{
7583
return true;
7684
}
@@ -86,12 +94,11 @@ void QgsLayerTreeViewBadLayerIndicatorProvider::onIndicatorClicked( const QModel
8694
dlg.setLayout( &lay );
8795
if ( dlg.exec() == QDialog::Accepted )
8896
{
89-
// Get selected item(s)
9097
QModelIndex index = browserWidget->currentIndex();
9198
if ( isItemCompatible( index ) )
9299
{
93100
const QgsDataItem *item( browserModel.dataItem( index ) );
94-
vlayer->setDataSource( item->mimeUri().uri, vlayer->name(), item->mimeUri().providerKey, QgsDataProvider::ProviderOptions() );
101+
layer->setDataSource( item->mimeUri().uri, layer->name(), item->mimeUri().providerKey, QgsDataProvider::ProviderOptions() );
95102
}
96103
}
97104
QgsSettings().setValue( QStringLiteral( "/Windows/selectDataSourceDialog/geometry" ), dlg.saveGeometry(), QgsSettings::Section::App );
@@ -106,8 +113,7 @@ QString QgsLayerTreeViewBadLayerIndicatorProvider::iconName( QgsMapLayer *layer
106113
QString QgsLayerTreeViewBadLayerIndicatorProvider::tooltipText( QgsMapLayer *layer )
107114
{
108115
Q_UNUSED( layer );
109-
// TODO, click here to set a new data source.
110-
return tr( "<b>Bad layer!</b><br>Layer data source could not be found." );
116+
return tr( "<b>Bad layer!</b><br>Layer data source could not be found. Click to set a new data source" );
111117
}
112118

113119
bool QgsLayerTreeViewBadLayerIndicatorProvider::acceptLayer( QgsMapLayer *layer )

‎src/core/layertree/qgslayertreemodellegendnode.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,10 +636,13 @@ QImage QgsWmsLegendNode::getLegendGraphic() const
636636

637637
QgsRasterLayer *layer = qobject_cast<QgsRasterLayer *>( mLayerNode->layer() );
638638
const QgsLayerTreeModel *mod = model();
639-
if ( ! mod ) return mImage;
639+
if ( ! mod )
640+
return mImage;
640641
const QgsMapSettings *ms = mod->legendFilterMapSettings();
641642

642643
QgsRasterDataProvider *prov = layer->dataProvider();
644+
if ( ! prov )
645+
return mImage;
643646

644647
Q_ASSERT( ! mFetcher );
645648
mFetcher.reset( prov->getLegendGraphicFetcher( ms ) );

‎src/core/layertree/qgslayertreeutils.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ void QgsLayerTreeUtils::removeInvalidLayers( QgsLayerTreeGroup *group )
306306
group->removeChildNode( node );
307307
}
308308

309-
void QgsLayerTreeUtils::storeInvalidLayersProperties( QgsLayerTreeGroup *group, const QDomDocument *doc )
309+
void QgsLayerTreeUtils::storeOriginalLayersProperties( QgsLayerTreeGroup *group, const QDomDocument *doc )
310310
{
311311
const QDomNodeList mlNodeList( doc->documentElement()
312312
.firstChildElement( QStringLiteral( "projectlayers" ) )
@@ -316,7 +316,7 @@ void QgsLayerTreeUtils::storeInvalidLayersProperties( QgsLayerTreeGroup *group,
316316
if ( QgsLayerTree::isLayer( node ) )
317317
{
318318
QgsMapLayer *l( QgsLayerTree::toLayer( node )->layer() );
319-
if ( l && ! l->isValid( ) )
319+
if ( l )
320320
{
321321
for ( int i = 0; i < mlNodeList.count(); i++ )
322322
{
@@ -332,7 +332,7 @@ void QgsLayerTreeUtils::storeInvalidLayersProperties( QgsLayerTreeGroup *group,
332332
QString str;
333333
QTextStream stream( &str );
334334
document.save( stream, 4 /*indent*/ );
335-
l->setCustomProperty( QStringLiteral( "invalidLayerProperties" ), str );
335+
l->setOriginalXmlProperties( str );
336336
}
337337
}
338338
}

‎src/core/layertree/qgslayertreeutils.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ class CORE_EXPORT QgsLayerTreeUtils
6363
static void removeInvalidLayers( QgsLayerTreeGroup *group );
6464

6565
/**
66-
* Stores in a custom layer node property the layer properties XML information for an invalid layer
66+
* Stores in a layer's originalXmlProperties the layer properties information
6767
* \since 3.6
6868
*/
69-
static void storeInvalidLayersProperties( QgsLayerTreeGroup *group, const QDomDocument *doc );
69+
static void storeOriginalLayersProperties( QgsLayerTreeGroup *group, const QDomDocument *doc );
7070

7171
//! Remove subtree of embedded groups and replaces it with a custom property embedded-visible-layers
7272
static void replaceChildrenOfEmbeddedGroups( QgsLayerTreeGroup *group );

‎src/core/qgsmaplayer.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1833,6 +1833,16 @@ bool QgsMapLayer::isReadOnly() const
18331833
return true;
18341834
}
18351835

1836+
QString QgsMapLayer::originalXmlProperties() const
1837+
{
1838+
return mOriginalXmlProperties;
1839+
}
1840+
1841+
void QgsMapLayer::setOriginalXmlProperties( const QString &originalXmlProperties )
1842+
{
1843+
mOriginalXmlProperties = originalXmlProperties;
1844+
}
1845+
18361846
void QgsMapLayer::setProviderType( const QString &providerType )
18371847
{
18381848
mProviderKey = providerType;

‎src/core/qgsmaplayer.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,24 @@ class CORE_EXPORT QgsMapLayer : public QObject
11011101
*/
11021102
bool isRefreshOnNotifyEnabled() const { return mIsRefreshOnNofifyEnabled; }
11031103

1104+
/**
1105+
* Returns the XML properties of the original layer as they were when the layer
1106+
* was first read from the project file. In case of new layers this is normally empty.
1107+
*
1108+
* The storage format for the XML is qlr
1109+
*
1110+
* \since QGIS 3.6
1111+
*/
1112+
QString originalXmlProperties() const;
1113+
1114+
/**
1115+
* Sets the original XML properties for the layer to \a originalXmlProperties
1116+
*
1117+
* The storage format for the XML is qlr
1118+
*
1119+
* \since QGIS 3.6
1120+
*/
1121+
void setOriginalXmlProperties( const QString &originalXmlProperties );
11041122

11051123
public slots:
11061124

@@ -1506,6 +1524,13 @@ class CORE_EXPORT QgsMapLayer : public QObject
15061524
//! Renderer for 3D views
15071525
QgsAbstract3DRenderer *m3DRenderer = nullptr;
15081526

1527+
/**
1528+
* Stores the original XML properties of the layer when loaded from the project
1529+
*
1530+
* This information can be used to pass through the bad layers or to reset changes on a good layer
1531+
*/
1532+
QString mOriginalXmlProperties;
1533+
15091534
};
15101535

15111536
Q_DECLARE_METATYPE( QgsMapLayer * )

‎src/core/qgsproject.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,8 +1292,8 @@ bool QgsProject::readProjectFile( const QString &filename )
12921292

12931293
// After bad layer handling we might still have invalid layers,
12941294
// store them in case the user wanted to handle them later
1295-
// or she wanted to pass them through when saving
1296-
QgsLayerTreeUtils::storeInvalidLayersProperties( mRootGroup, doc.get() );
1295+
// or wanted to pass them through when saving
1296+
QgsLayerTreeUtils::storeOriginalLayersProperties( mRootGroup, doc.get() );
12971297

12981298
mRootGroup->removeCustomProperty( QStringLiteral( "loading" ) );
12991299

@@ -1760,17 +1760,16 @@ bool QgsProject::writeProjectFile( const QString &filename )
17601760
maplayerElem = doc->createElement( QStringLiteral( "maplayer" ) );
17611761
ml->writeLayerXml( maplayerElem, *doc, context );
17621762
}
1763-
else if ( ml->customPropertyKeys().contains( QStringLiteral( "invalidLayerProperties" ) ) )
1763+
else if ( ! ml->originalXmlProperties().isEmpty() )
17641764
{
17651765
QDomDocument document;
1766-
if ( document.setContent( ml->customProperty( QStringLiteral( "invalidLayerProperties" ) ).toString() ) )
1766+
if ( document.setContent( ml->originalXmlProperties() ) )
17671767
{
17681768
maplayerElem = document.firstChildElement();
1769-
maplayerElem.setAttribute( QStringLiteral( "invalidLayerProperties" ), QStringLiteral( "true" ) );
17701769
}
17711770
else
17721771
{
1773-
QgsDebugMsg( QStringLiteral( "Could not restore bad layer properties %1 from saved invalidLayerProperties" ).arg( ml->id() ) );
1772+
QgsDebugMsg( QStringLiteral( "Could not restore layer properties for layer %1" ).arg( ml->id() ) );
17741773
}
17751774
}
17761775

‎tests/src/python/test_qgsprojectbadlayers.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ def test_project_roundtrip(self):
5959
project_path = os.path.join(temp_dir.path(), 'project.qgs')
6060
self.assertTrue(p.write(project_path))
6161

62+
# Re-load the project, checking for the XML properties
63+
self.assertTrue(p.read(project_path))
64+
vector = list(p.mapLayersByName('lines'))[0]
65+
raster = list(p.mapLayersByName('raster'))[0]
66+
raster_copy = list(p.mapLayersByName('raster_copy'))[0]
67+
self.assertTrue(vector.originalLayerXmlProperties() != '')
68+
self.assertTrue(raster.originalLayerXmlProperties() != '')
69+
self.assertTrue(raster_copy.originalLayerXmlProperties() != '')
70+
# Test setter
71+
raster.setOriginalLayerXmlProperties('pippo')
72+
self.assertEqual(raster.originalLayerXmlProperties(), 'pippo')
73+
6274
# Now create and invalid project:
6375
bad_project_path = os.path.join(temp_dir.path(), 'project_bad.qgs')
6476
with open(project_path, 'r') as infile:

0 commit comments

Comments
 (0)
Please sign in to comment.