Skip to content

Commit 0548f42

Browse files
mj10777nyalldawson
authored andcommitted
Added QgsLayerMetadata logic to QgsDataProvider. Added setMetadata in QgsVector/RasterLayer::setDataProvider. Corrections in QgsMetadataWidget with added get/setMetadata and hasChanged functions plus checking for set mLayer pointer before use.
1 parent 2571de3 commit 0548f42

10 files changed

+188
-24
lines changed

python/core/metadata/qgslayermetadata.sip.in

+1
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,7 @@ Stores state in Dom node
618618
:return: true if successful
619619
%End
620620

621+
621622
};
622623

623624

python/core/qgsdataprovider.sip.in

+21
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,27 @@ The default implementation does nothing.
362362
%End
363363

364364

365+
virtual QgsLayerMetadata layerMetadata() const;
366+
%Docstring
367+
Retrieve collected Metadata from the Provider source
368+
A structured metadata store for a map layer.
369+
\note
370+
371+
.. seealso:: :py:func:`setLayerMetadata`
372+
373+
.. versionadded:: 3.0
374+
%End
375+
virtual bool setLayerMetadata( const QgsLayerMetadata &layerMetadata );
376+
%Docstring
377+
Set collected Metadata from the Provider source
378+
A structured metadata store for a map layer.
379+
\note
380+
381+
.. seealso:: :py:func:`layerMetadata`
382+
383+
.. versionadded:: 3.0
384+
%End
385+
365386
signals:
366387

367388
void fullExtentCalculated();

python/gui/qgsmetadatawidget.sip.in

+36
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,52 @@ class QgsMetadataWidget : QWidget
2626
QgsMetadataWidget( QWidget *parent, QgsMapLayer *layer = 0 );
2727
%Docstring
2828
Constructor for the wizard.
29+
\note
30+
For use with a source \layer. This constructor automatically sets the widget's metadata() if the ``layer`` pointer is valid.
31+
calls setMetadata, using mMetadata
32+
33+
:param layer: to set the main QgsLayerMetadata with mLayer->metadata() when not None
34+
35+
.. seealso:: :py:func:`setMetadata`
36+
%End
37+
38+
void setMetadata( const QgsLayerMetadata &layerMetadata );
39+
%Docstring
40+
Sets the ``metadata`` to display in the widget
41+
\note
42+
Called from constructor and initializes child widget on first use
43+
Can be called from outside to change the QgsLayerMetadata object.
44+
45+
.. seealso:: :py:func:`metadata`
46+
%End
47+
48+
QgsLayerMetadata metadata();
49+
%Docstring
50+
Retrieves a QgsLayerMetadata object representing the current state of the widget.
51+
\note
52+
saveMetdata is called before returning :py:class:`QgsLayerMetadata`
53+
54+
.. seealso:: :py:func:`saveMetadata`
2955
%End
3056

3157
void saveMetadata( QgsLayerMetadata &layerMetadata );
3258
%Docstring
3359
Save all fields in a QgsLayerMetadata object.
60+
61+
.. seealso:: :py:func:`getMetadata`
62+
63+
.. seealso:: :py:func:`acceptMetadata`
64+
65+
.. seealso:: :py:func:`checkMetadata`
3466
%End
3567

3668
bool checkMetadata();
3769
%Docstring
3870
Check if values in the wizard are correct.
71+
72+
.. seealso:: :py:func:`updatePanel`
73+
74+
.. seealso:: :py:func:`saveMetadata`
3975
%End
4076

4177
void crsChanged();

src/core/metadata/qgslayermetadata.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -767,3 +767,26 @@ void QgsLayerMetadata::Extent::setTemporalExtents( const QList<QgsDateTimeRange>
767767
{
768768
mTemporalExtents = temporalExtents;
769769
}
770+
771+
bool QgsLayerMetadata::operator==( const QgsLayerMetadata &metadataOther ) const
772+
{
773+
return ( ( parentIdentifier() == metadataOther.parentIdentifier() ) &&
774+
( identifier() == metadataOther.identifier() ) &&
775+
( language() == metadataOther.language() ) &&
776+
( type() == metadataOther.type() ) &&
777+
( title() == metadataOther.title() ) &&
778+
( abstract() == metadataOther.abstract() ) &&
779+
( fees() == metadataOther.fees() ) &&
780+
( rights() == metadataOther.rights() ) &&
781+
( licenses() == metadataOther.licenses() ) &&
782+
( history() == metadataOther.history() ) &&
783+
( encoding() == metadataOther.encoding() ) &&
784+
( crs() == metadataOther.crs() ) &&
785+
( keywords() == metadataOther.keywords() ) &&
786+
( categories() == metadataOther.categories() ) &&
787+
// QgsLayerMetadata::ConstraintList, LinkList, Extent, ContactList need to be delt with properly
788+
( constraints().count() == metadataOther.constraints().count() ) &&
789+
( extent().spatialExtents().count() == metadataOther.extent().spatialExtents().count() ) &&
790+
( contacts().count() == metadataOther.contacts().count() ) &&
791+
( links().count() == metadataOther.links().count() ) );
792+
}

src/core/metadata/qgslayermetadata.h

+5
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,11 @@ class CORE_EXPORT QgsLayerMetadata
726726
*/
727727
bool writeMetadataXml( QDomElement &metadataElement, QDomDocument &document ) const;
728728

729+
/**
730+
* Compares two features
731+
*/
732+
bool operator==( const QgsLayerMetadata &metadataOther ) const SIP_SKIP;
733+
729734
private:
730735

731736
/*

src/core/qgsdataprovider.h

+19
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
//#include "qgsdataitem.h"
2626
#include "qgsdatasourceuri.h"
27+
#include "qgslayermetadata.h"
2728
#include "qgserror.h"
2829

2930
typedef int dataCapabilities_t(); // SIP_SKIP
@@ -492,6 +493,24 @@ class CORE_EXPORT QgsDataProvider : public QObject
492493
*/
493494
virtual bool renderInPreview( const QgsDataProvider::PreviewContext &context ); // SIP_SKIP
494495

496+
/**
497+
* Retrieve collected Metadata from the Provider source
498+
* \brief A structured metadata store for a map layer.
499+
* \note
500+
* \see setLayerMetadata
501+
* \since QGIS 3.0
502+
*/
503+
virtual QgsLayerMetadata layerMetadata() const { return QgsLayerMetadata(); };
504+
505+
/**
506+
* Set collected Metadata from the Provider source
507+
* \brief A structured metadata store for a map layer.
508+
* \note
509+
* \see layerMetadata
510+
* \since QGIS 3.0
511+
*/
512+
virtual bool setLayerMetadata( const QgsLayerMetadata &layerMetadata ) { Q_UNUSED( layerMetadata ); return false; }
513+
495514
signals:
496515

497516
/**

src/core/qgsvectorlayer.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -1526,6 +1526,9 @@ bool QgsVectorLayer::setDataProvider( QString const &provider )
15261526
return false;
15271527
}
15281528

1529+
setMetadata( mDataProvider->layerMetadata() );
1530+
QgsDebugMsgLevel( QString( "Set Data provider QgsLayerMetadata identifier[%1]" ).arg( metadata().identifier() ), 4 );
1531+
15291532
// TODO: Check if the provider has the capability to send fullExtentCalculated
15301533
connect( mDataProvider, &QgsVectorDataProvider::fullExtentCalculated, this, [ = ] { updateExtents(); } );
15311534

src/core/raster/qgsrasterlayer.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,9 @@ void QgsRasterLayer::setDataProvider( QString const &provider )
625625
return;
626626
}
627627

628+
setMetadata( mDataProvider->layerMetadata() );
629+
QgsDebugMsgLevel( QString( "Set Data provider QgsLayerMetadata identifier[%1]" ).arg( metadata().identifier() ), 4 );
630+
628631
if ( provider == QLatin1String( "gdal" ) )
629632
{
630633
// make sure that the /vsigzip or /vsizip is added to uri, if applicable

src/gui/qgsmetadatawidget.cpp

+50-24
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ QgsMetadataWidget::QgsMetadataWidget( QWidget *parent, QgsMapLayer *layer )
3535
mLayer( layer )
3636
{
3737
setupUi( this );
38-
mMetadata = layer->metadata();
38+
if ( mLayer )
39+
{
40+
mMetadata = mLayer->metadata();
41+
}
3942
tabWidget->setCurrentIndex( 0 );
4043

4144
// Disable the encoding
@@ -107,15 +110,36 @@ QgsMetadataWidget::QgsMetadataWidget( QWidget *parent, QgsMapLayer *layer )
107110
connect( btnRemoveCategory, &QPushButton::clicked, this, &QgsMetadataWidget::removeSelectedCategories );
108111

109112
fillComboBox();
113+
if ( !mLayer )
114+
{
115+
btnAutoSource->setEnabled( false );
116+
btnAutoEncoding->setEnabled( false );
117+
btnAutoCrs->setEnabled( false );
118+
}
119+
setMetadata( mMetadata );
120+
}
121+
122+
void QgsMetadataWidget::setMetadata( const QgsLayerMetadata &layerMetadata )
123+
{
124+
mMetadata = layerMetadata;
110125
setPropertiesFromLayer();
111126
}
112127

113-
void QgsMetadataWidget::fillSourceFromLayer()
128+
QgsLayerMetadata QgsMetadataWidget::metadata()
114129
{
115-
lineEditIdentifier->setText( mLayer->publicSource() );
130+
saveMetadata( mMetadata );
131+
return mMetadata;
116132
}
117133

118-
void QgsMetadataWidget::addVocabulary()
134+
void QgsMetadataWidget::fillSourceFromLayer() const
135+
{
136+
if ( mLayer )
137+
{
138+
lineEditIdentifier->setText( mLayer->publicSource() );
139+
}
140+
}
141+
142+
void QgsMetadataWidget::addVocabulary() const
119143
{
120144
int row = tabKeywords->rowCount();
121145
tabKeywords->setRowCount( row + 1 );
@@ -130,7 +154,7 @@ void QgsMetadataWidget::addVocabulary()
130154
tabKeywords->setItem( row, 1, pCell );
131155
}
132156

133-
void QgsMetadataWidget::removeSelectedVocabulary()
157+
void QgsMetadataWidget::removeSelectedVocabulary() const
134158
{
135159
QItemSelectionModel *selectionModel = tabKeywords->selectionModel();
136160
const QModelIndexList selectedRows = selectionModel->selectedRows();
@@ -152,7 +176,7 @@ void QgsMetadataWidget::addLicence()
152176
}
153177
}
154178

155-
void QgsMetadataWidget::removeSelectedLicence()
179+
void QgsMetadataWidget::removeSelectedLicence() const
156180
{
157181
QItemSelectionModel *selectionModel = tabLicenses->selectionModel();
158182
const QModelIndexList selectedRows = selectionModel->selectedRows();
@@ -173,7 +197,7 @@ void QgsMetadataWidget::addRight()
173197
}
174198
}
175199

176-
void QgsMetadataWidget::removeSelectedRight()
200+
void QgsMetadataWidget::removeSelectedRight() const
177201
{
178202
QItemSelectionModel *selection = listRights->selectionModel();
179203
if ( selection->hasSelection() )
@@ -187,22 +211,22 @@ void QgsMetadataWidget::removeSelectedRight()
187211
}
188212
}
189213

190-
void QgsMetadataWidget::addConstraint()
214+
void QgsMetadataWidget::addConstraint() const
191215
{
192216
int row = mConstraintsModel->rowCount();
193217
mConstraintsModel->setItem( row, 0, new QStandardItem( QString( tr( "undefined %1" ) ).arg( row + 1 ) ) );
194218
mConstraintsModel->setItem( row, 1, new QStandardItem( QString( tr( "undefined %1" ) ).arg( row + 1 ) ) );
195219
}
196220

197-
void QgsMetadataWidget::removeSelectedConstraint()
221+
void QgsMetadataWidget::removeSelectedConstraint() const
198222
{
199223
const QModelIndexList selectedRows = tabConstraints->selectionModel()->selectedRows();
200224
mConstraintsModel->removeRow( selectedRows[0].row() );
201225
}
202226

203-
void QgsMetadataWidget::crsChanged()
227+
void QgsMetadataWidget::crsChanged() const
204228
{
205-
if ( mCrs.isValid() )
229+
if ( ( mCrs.isValid() ) && ( mLayer ) )
206230
{
207231
lblCurrentCrs->setText( tr( "CRS: %1 - %2" ).arg( mCrs.authid(), mCrs.description() ) );
208232
spatialExtentSelector->setEnabled( true );
@@ -233,7 +257,7 @@ void QgsMetadataWidget::crsChanged()
233257
}
234258
}
235259

236-
void QgsMetadataWidget::addAddress()
260+
void QgsMetadataWidget::addAddress() const
237261
{
238262
int row = tabAddresses->rowCount();
239263
tabAddresses->setRowCount( row + 1 );
@@ -259,7 +283,7 @@ void QgsMetadataWidget::addAddress()
259283
tabAddresses->setItem( row, 5, new QTableWidgetItem() );
260284
}
261285

262-
void QgsMetadataWidget::removeSelectedAddress()
286+
void QgsMetadataWidget::removeSelectedAddress() const
263287
{
264288
QItemSelectionModel *selectionModel = tabAddresses->selectionModel();
265289
const QModelIndexList selectedRows = selectionModel->selectedRows();
@@ -281,7 +305,7 @@ void QgsMetadataWidget::fillCrsFromProvider()
281305
crsChanged();
282306
}
283307

284-
void QgsMetadataWidget::addLink()
308+
void QgsMetadataWidget::addLink() const
285309
{
286310
int row = mLinksModel->rowCount();
287311
mLinksModel->setItem( row, 0, new QStandardItem( QString( tr( "undefined %1" ) ).arg( row + 1 ) ) );
@@ -293,7 +317,7 @@ void QgsMetadataWidget::addLink()
293317
mLinksModel->setItem( row, 6, new QStandardItem() );
294318
}
295319

296-
void QgsMetadataWidget::removeSelectedLink()
320+
void QgsMetadataWidget::removeSelectedLink() const
297321
{
298322
const QModelIndexList selectedRows = tabLinks->selectionModel()->selectedRows();
299323
mLinksModel->removeRow( selectedRows[0].row() );
@@ -310,7 +334,7 @@ void QgsMetadataWidget::addHistory()
310334
}
311335
}
312336

313-
void QgsMetadataWidget::removeSelectedHistory()
337+
void QgsMetadataWidget::removeSelectedHistory() const
314338
{
315339
QItemSelectionModel *selection = listHistory->selectionModel();
316340
if ( selection->hasSelection() )
@@ -324,7 +348,7 @@ void QgsMetadataWidget::removeSelectedHistory()
324348
}
325349
}
326350

327-
void QgsMetadataWidget::fillComboBox()
351+
void QgsMetadataWidget::fillComboBox() const
328352
{
329353
// Set default values in type combobox
330354
// It is advised to use the ISO 19115 MD_ScopeCode values. E.g. 'dataset' or 'series'.
@@ -522,7 +546,7 @@ void QgsMetadataWidget::setPropertiesFromLayer()
522546
mHistoryModel->setStringList( mMetadata.history() );
523547
}
524548

525-
void QgsMetadataWidget::saveMetadata( QgsLayerMetadata &layerMetadata )
549+
void QgsMetadataWidget::saveMetadata( QgsLayerMetadata &layerMetadata ) const
526550
{
527551
layerMetadata.setParentIdentifier( lineEditParentId->text() );
528552
layerMetadata.setIdentifier( lineEditIdentifier->text() );
@@ -635,7 +659,7 @@ void QgsMetadataWidget::saveMetadata( QgsLayerMetadata &layerMetadata )
635659
layerMetadata.setHistory( mHistoryModel->stringList() );
636660
}
637661

638-
bool QgsMetadataWidget::checkMetadata()
662+
bool QgsMetadataWidget::checkMetadata() const
639663
{
640664
QgsLayerMetadata metadata = QgsLayerMetadata();
641665
saveMetadata( metadata );
@@ -815,9 +839,11 @@ void QgsMetadataWidget::setMapCanvas( QgsMapCanvas *canvas )
815839
void QgsMetadataWidget::acceptMetadata()
816840
{
817841
saveMetadata( mMetadata );
818-
819-
// Save layer metadata properties
820-
mLayer->setMetadata( mMetadata );
842+
if ( mLayer )
843+
{
844+
// Save layer metadata properties
845+
mLayer->setMetadata( mMetadata );
846+
}
821847
}
822848

823849
void QgsMetadataWidget::setMetadata( const QgsLayerMetadata &metadata )
@@ -826,7 +852,7 @@ void QgsMetadataWidget::setMetadata( const QgsLayerMetadata &metadata )
826852
setPropertiesFromLayer();
827853
}
828854

829-
void QgsMetadataWidget::syncFromCategoriesTabToKeywordsTab()
855+
void QgsMetadataWidget::syncFromCategoriesTabToKeywordsTab() const
830856
{
831857
if ( mCategoriesModel->rowCount() > 0 )
832858
{
@@ -847,7 +873,7 @@ void QgsMetadataWidget::syncFromCategoriesTabToKeywordsTab()
847873
}
848874
}
849875

850-
void QgsMetadataWidget::updatePanel()
876+
void QgsMetadataWidget::updatePanel() const
851877
{
852878
int index = tabWidget->currentIndex();
853879
QString currentTabText = tabWidget->widget( index )->objectName();

0 commit comments

Comments
 (0)