class MetadataDialog(QDialog): def __init__(self, metadata, tab, parent=None): super(MetadataDialog, self).__init__(parent) self.metadata = metadata self.setWindowTitle(self.tr('Metadata')) layout = QVBoxLayout() self.metadataWidget = QgsMetadataWidget() self.metadataWidget.setMetadata(metadata) layout.addWidget(self.metadataWidget) self.metadataWidget.layout().itemAt(0).widget().setCurrentIndex(tab) self.buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel) layout.addWidget(self.buttonBox) self.setLayout(layout) self.buttonBox.accepted.connect(self.okPressed) self.buttonBox.rejected.connect(self.cancelPressed) def okPressed(self): self.metadataWidget.saveMetadata(self.metadata) self.close() def cancelPressed(self): self.metadata = None self.close()
def testLayerMode(self): """ Create a fully populated QgsLayerMetadata object, then set it to the widget and re-read back the generated metadata to ensure that no content is lost. """ w = QgsMetadataWidget() m = QgsLayerMetadata() m.setIdentifier('1234') m.setParentIdentifier('xyz') m.setLanguage('en-CA') m.setType('dataset') m.setTitle('roads') m.setAbstract('my roads') m.setFees('None') m.setConstraints([QgsLayerMetadata.Constraint('None', 'access')]) m.setRights(['Copyright foo 2017']) m.setLicenses(['WTFPL']) m.setHistory(['history a', 'history b']) m.setKeywords({ 'GEMET': ['kw1', 'kw2'], 'gmd:topicCategory': ['natural'], }) #m.setEncoding('utf-8') m.setCrs(QgsCoordinateReferenceSystem.fromOgcWmsCrs('EPSG:4326')) e = QgsLayerMetadata.Extent() se = QgsLayerMetadata.SpatialExtent() se.extentCrs = QgsCoordinateReferenceSystem.fromOgcWmsCrs('EPSG:4326') se.bounds = QgsBox3d(-180, -90, 0, 180, 90, 0) e.setSpatialExtents([se]) dates = [ QgsDateTimeRange( QDateTime(QDate(2001, 12, 17), QTime(9, 30, 47)), QDateTime(QDate(2001, 12, 17), QTime(9, 30, 47))) ] e.setTemporalExtents(dates) m.setExtent(e) c = QgsLayerMetadata.Contact() c.name = 'John Smith' c.organization = 'ACME' c.position = 'staff' c.voice = '1500 515 555' c.fax = 'xx.xxx.xxx.xxxx' c.email = '*****@*****.**' c.role = 'pointOfContact' address = QgsLayerMetadata.Address() address.type = 'postal' address.address = '123 Main Street' address.city = 'anycity' address.administrativeArea = 'anyprovince' address.postalCode = '90210' address.country = 'Canada' c.addresses = [address] m.setContacts([c]) l = QgsLayerMetadata.Link() l.name = 'geonode:roads' l.type = 'OGC:WMS' l.description = 'my GeoNode road layer' l.url = 'http://example.org/wms' l2 = QgsLayerMetadata.Link() l2.name = 'geonode:roads' l2.type = 'OGC:WFS' l2.description = 'my GeoNode road layer' l2.url = 'http://example.org/wfs' l3 = QgsLayerMetadata.Link() l3.name = 'roads' l3.type = 'WWW:LINK' l3.description = 'full dataset download' l3.url = 'http://example.org/roads.tgz' l3.format = 'ESRI Shapefile' l3.mimeType = 'application/gzip' l3.size = '283676' m.setLinks([l, l2, l3]) # set widget metadata w.setMetadata(m) self.assertEqual(w.mode(), QgsMetadataWidget.LayerMetadata) m = w.metadata() self.assertIsInstance(m, QgsLayerMetadata) self.assertEqual(m.identifier(), '1234') self.assertEqual(m.parentIdentifier(), 'xyz') self.assertEqual(m.language(), 'en-CA') self.assertEqual(m.type(), 'dataset') self.assertEqual(m.title(), 'roads') self.assertEqual(m.abstract(), 'my roads') self.assertEqual(m.fees(), 'None') self.assertEqual(m.constraints()[0].constraint, 'None') self.assertEqual(m.constraints()[0].type, 'access') self.assertEqual(m.rights(), ['Copyright foo 2017']) self.assertEqual(m.licenses(), ['WTFPL']) self.assertEqual(m.history(), ['history a', 'history b']) #self.assertEqual(m.encoding(), 'utf-8') self.assertEqual( m.keywords(), {'GEMET': ['kw1', 'kw2'], 'gmd:topicCategory': ['natural']}) self.assertEqual(m.crs().authid(), 'EPSG:4326') extent = m.extent().spatialExtents()[0] self.assertEqual(extent.extentCrs.authid(), 'EPSG:4326') self.assertEqual(extent.bounds.xMinimum(), -180.0) self.assertEqual(extent.bounds.yMinimum(), -90.0) self.assertEqual(extent.bounds.xMaximum(), 180.0) self.assertEqual(extent.bounds.yMaximum(), 90.0) self.assertEqual(m.extent().temporalExtents()[0].begin(), QDateTime(QDate(2001, 12, 17), QTime(9, 30, 47))) self.assertTrue(m.extent().temporalExtents()[0].isInstant()) self.assertEqual(m.contacts()[0].name, 'John Smith') self.assertEqual(m.contacts()[0].organization, 'ACME') self.assertEqual(m.contacts()[0].position, 'staff') self.assertEqual(m.contacts()[0].voice, '1500 515 555') self.assertEqual(m.contacts()[0].fax, 'xx.xxx.xxx.xxxx') self.assertEqual(m.contacts()[0].email, '*****@*****.**') self.assertEqual(m.contacts()[0].role, 'pointOfContact') self.assertEqual(m.contacts()[0].addresses[0].type, 'postal') self.assertEqual(m.contacts()[0].addresses[0].address, '123 Main Street') self.assertEqual(m.contacts()[0].addresses[0].city, 'anycity') self.assertEqual(m.contacts()[0].addresses[0].administrativeArea, 'anyprovince') self.assertEqual(m.contacts()[0].addresses[0].postalCode, '90210') self.assertEqual(m.contacts()[0].addresses[0].country, 'Canada') self.assertEqual(m.links()[0].name, 'geonode:roads') self.assertEqual(m.links()[0].type, 'OGC:WMS') self.assertEqual(m.links()[0].description, 'my GeoNode road layer') self.assertEqual(m.links()[0].url, 'http://example.org/wms') self.assertEqual(m.links()[1].name, 'geonode:roads') self.assertEqual(m.links()[1].type, 'OGC:WFS') self.assertEqual(m.links()[1].description, 'my GeoNode road layer') self.assertEqual(m.links()[1].url, 'http://example.org/wfs') self.assertEqual(m.links()[2].name, 'roads') self.assertEqual(m.links()[2].type, 'WWW:LINK') self.assertEqual(m.links()[2].description, 'full dataset download') self.assertEqual(m.links()[2].url, 'http://example.org/roads.tgz') self.assertEqual(m.links()[2].format, 'ESRI Shapefile') self.assertEqual(m.links()[2].mimeType, 'application/gzip') self.assertEqual(m.links()[2].size, '283676')
def testProjectMode(self): """ Create a fully populated QgsProjectMetadata object, then set it to the widget and re-read back the generated metadata to ensure that no content is lost. """ w = QgsMetadataWidget() m = QgsProjectMetadata() m.setIdentifier('1234') m.setParentIdentifier('xyz') m.setLanguage('en-CA') m.setType('project') m.setTitle('roads') m.setAbstract('my roads') m.setHistory(['history a', 'history b']) m.setKeywords({ 'GEMET': ['kw1', 'kw2'], 'gmd:topicCategory': ['natural'], }) c = QgsAbstractMetadataBase.Contact() c.name = 'John Smith' c.organization = 'ACME' c.position = 'staff' c.voice = '1500 515 555' c.fax = 'xx.xxx.xxx.xxxx' c.email = '*****@*****.**' c.role = 'pointOfContact' address = QgsAbstractMetadataBase.Address() address.type = 'postal' address.address = '123 Main Street' address.city = 'anycity' address.administrativeArea = 'anyprovince' address.postalCode = '90210' address.country = 'Canada' c.addresses = [address] m.setContacts([c]) l = QgsAbstractMetadataBase.Link() l.name = 'geonode:roads' l.type = 'OGC:WMS' l.description = 'my GeoNode road layer' l.url = 'http://example.org/wms' l2 = QgsAbstractMetadataBase.Link() l2.name = 'geonode:roads' l2.type = 'OGC:WFS' l2.description = 'my GeoNode road layer' l2.url = 'http://example.org/wfs' l3 = QgsAbstractMetadataBase.Link() l3.name = 'roads' l3.type = 'WWW:LINK' l3.description = 'full dataset download' l3.url = 'http://example.org/roads.tgz' l3.format = 'ESRI Shapefile' l3.mimeType = 'application/gzip' l3.size = '283676' m.setLinks([l, l2, l3]) m.setAuthor('my author') m.setCreationDateTime(QDateTime(QDate(2001, 12, 17), QTime(9, 30, 47))) # set widget metadata w.setMetadata(m) self.assertEqual(w.mode(), QgsMetadataWidget.ProjectMetadata) m = w.metadata() self.assertIsInstance(m, QgsProjectMetadata) self.assertEqual(m.identifier(), '1234') self.assertEqual(m.parentIdentifier(), 'xyz') self.assertEqual(m.language(), 'en-CA') self.assertEqual(m.type(), 'project') self.assertEqual(m.title(), 'roads') self.assertEqual(m.abstract(), 'my roads') self.assertEqual(m.history(), ['history a', 'history b']) self.assertEqual( m.keywords(), {'GEMET': ['kw1', 'kw2'], 'gmd:topicCategory': ['natural']}) self.assertEqual(m.contacts()[0].name, 'John Smith') self.assertEqual(m.contacts()[0].organization, 'ACME') self.assertEqual(m.contacts()[0].position, 'staff') self.assertEqual(m.contacts()[0].voice, '1500 515 555') self.assertEqual(m.contacts()[0].fax, 'xx.xxx.xxx.xxxx') self.assertEqual(m.contacts()[0].email, '*****@*****.**') self.assertEqual(m.contacts()[0].role, 'pointOfContact') self.assertEqual(m.contacts()[0].addresses[0].type, 'postal') self.assertEqual(m.contacts()[0].addresses[0].address, '123 Main Street') self.assertEqual(m.contacts()[0].addresses[0].city, 'anycity') self.assertEqual(m.contacts()[0].addresses[0].administrativeArea, 'anyprovince') self.assertEqual(m.contacts()[0].addresses[0].postalCode, '90210') self.assertEqual(m.contacts()[0].addresses[0].country, 'Canada') self.assertEqual(m.links()[0].name, 'geonode:roads') self.assertEqual(m.links()[0].type, 'OGC:WMS') self.assertEqual(m.links()[0].description, 'my GeoNode road layer') self.assertEqual(m.links()[0].url, 'http://example.org/wms') self.assertEqual(m.links()[1].name, 'geonode:roads') self.assertEqual(m.links()[1].type, 'OGC:WFS') self.assertEqual(m.links()[1].description, 'my GeoNode road layer') self.assertEqual(m.links()[1].url, 'http://example.org/wfs') self.assertEqual(m.links()[2].name, 'roads') self.assertEqual(m.links()[2].type, 'WWW:LINK') self.assertEqual(m.links()[2].description, 'full dataset download') self.assertEqual(m.links()[2].url, 'http://example.org/roads.tgz') self.assertEqual(m.links()[2].format, 'ESRI Shapefile') self.assertEqual(m.links()[2].mimeType, 'application/gzip') self.assertEqual(m.links()[2].size, '283676') self.assertEqual(m.author(), 'my author') self.assertEqual(m.creationDateTime(), QDateTime(QDate(2001, 12, 17), QTime(9, 30, 47)))