def updateVars(self): """Set the value of the variables from the user-entered values in the Edit Metadata tab""" # Initialise progressbar i = 0 QApplication.setOverrideCursor(Qt.BusyCursor) currProject = QgsProject.instance() self.dlg.updateBar.setValue(0) self.dlg.updateBar.setMaximum(11) # self.dlg.layoutSelect.currentIndex(): title = self.dlg.layoutSelect.currentText() self.dlg.printinglabel.setText( self.tr(u'Updating {}...').format(title)) # process input events in order to allow canceling QCoreApplication.processEvents() # Project: update variables from dialog QgsExpressionContextUtils.setProjectVariable(currProject, 'ma_country', self.dlg.maCountry.text()) self.dlg.updateBar.setValue(self.dlg.updateBar.value() + 1) QgsExpressionContextUtils.setProjectVariable(currProject, 'ma_glide_number', self.dlg.maGlide.text()) self.dlg.updateBar.setValue(self.dlg.updateBar.value() + 1) QgsExpressionContextUtils.setProjectVariable(currProject, 'ma_crs', self.dlg.maCrs.text()) self.dlg.updateBar.setValue(self.dlg.updateBar.value() + 1) QgsExpressionContextUtils.setProjectVariable( currProject, 'ma_organisation', self.dlg.maOrganisation.text()) self.dlg.updateBar.setValue(self.dlg.updateBar.value() + 1) QgsExpressionContextUtils.setProjectVariable(currProject, 'ma_country', self.dlg.maCountry.text()) self.dlg.updateBar.setValue(self.dlg.updateBar.value() + 1) QgsExpressionContextUtils.setProjectVariable( currProject, 'ma_opid', self.dlg.maOperationID.text()) self.dlg.updateBar.setValue(self.dlg.updateBar.value() + 1) QgsExpressionContextUtils.setProjectVariable( currProject, 'ma_acknowledgements', self.dlg.maAcknowledgements.toPlainText()) self.dlg.updateBar.setValue(self.dlg.updateBar.value() + 1) QgsExpressionContextUtils.setProjectVariable( currProject, 'ma_disclaimer', self.dlg.maDisclaimer.toPlainText()) self.dlg.updateBar.setValue(self.dlg.updateBar.value() + 1) # Layout: update variables from dialog for layout in QgsProject.instance().layoutManager().printLayouts(): # Select the current layout if layout.name() == self.dlg.layoutSelect.currentText(): QgsMessageLog.logMessage( 'Info: value for ' + self.dlg.maSummary.toPlainText(), 'MapExport') # Map Number QgsExpressionContextUtils.setLayoutVariable( layout, 'ma_map_number', self.dlg.maMapNumber.text()) self.dlg.updateBar.setValue(self.dlg.updateBar.value() + 1) # Map Title QgsExpressionContextUtils.setLayoutVariable( layout, 'ma_title', self.dlg.maTitle.text()) self.dlg.updateBar.setValue(self.dlg.updateBar.value() + 1) # Date Created QgsExpressionContextUtils.setLayoutVariable( layout, 'ma_created', self.dlg.maCreated.date()) self.dlg.updateBar.setValue(self.dlg.updateBar.value() + 1) # Map Summary QgsExpressionContextUtils.setLayoutVariable( layout, 'ma_summary', self.dlg.maSummary.toPlainText()) self.dlg.updateBar.setValue(self.dlg.updateBar.value() + 1) # Data sources QgsExpressionContextUtils.setLayoutVariable( layout, 'ma_datasource', self.dlg.maDatasource.toPlainText()) self.dlg.updateBar.setValue(self.dlg.updateBar.value() + 1) # Version QgsExpressionContextUtils.setLayoutVariable( layout, 'ma_version', self.dlg.maVersion.value()) self.dlg.updateBar.setValue(self.dlg.updateBar.value() + 1) # Status QgsExpressionContextUtils.setLayoutVariable( layout, 'ma_status', self.dlg.maStatus.currentText()) self.dlg.updateBar.setValue(self.dlg.updateBar.value() + 1) # Access QgsExpressionContextUtils.setLayoutVariable( layout, 'ma_access', self.dlg.maAccess.currentText()) self.dlg.updateBar.setValue(self.dlg.updateBar.value() + 1) # Language QgsExpressionContextUtils.setLayoutVariable( layout, 'ma_language', self.dlg.maLanguage.currentText()) self.dlg.updateBar.setValue(self.dlg.updateBar.value() + 1) i = i + 1 QApplication.restoreOverrideCursor() # show an ending message # in case of abortion if self.arret: self.iface.messageBar().pushMessage( self.tr(u'Operation interrupted : '), self.tr(u'Maps on {} have been '\ 'exported to "{}" before cancelling. '\ 'Some files may be incomplete.' ).format(i,folder), level = Qgis.Info, duration = 10 ) # or when export ended completely else: self.iface.messageBar().pushMessage( self.tr(u'Operation finished : '), self.tr(u'The metadata has been '\ 'updated.' ).format(title), level = Qgis.Info, duration = 50 )
def populateMetadataItems(self, m, layout): """Get the current value of metadata items from the project or layout variable, and populate the Edit Metadata dialog See *updateVars* to populate variable values from th Edit Metadata UI """ currProject = QgsProject.instance() # Check that all the variables exist, and if they don't, create them and give them a default value where appropriate with open(os.path.join(self.plugin_dir, "input/metadata_items.csv"), 'r') as metadata_file: reader = csv.reader(metadata_file, delimiter=',') metadata_list = list(reader) for x in metadata_list: ma_variable = str(x[0]) elem_name = str(x[1]) elem_name = elem_name.strip() ma_level = str(x[2]) default_value = str(x[3]) ma_level = ma_level.strip() # If project level variables don't exist, create them and populate with the default value if (ma_level == 'project'): if str( QgsExpressionContextUtils.projectScope( currProject).variable(ma_variable)) == 'None': QgsExpressionContextUtils.setProjectVariable( currProject, ma_variable, default_value) # Populate comboboxes # Populate values of Status combobox self.dlg.maStatus.addItem('New') self.dlg.maStatus.addItem('Correction') self.dlg.maStatus.addItem('Update') # Populate values of Access combobox self.dlg.maAccess.addItem('Public') self.dlg.maAccess.addItem('MapAction') self.dlg.maAccess.addItem('Selected partners') # Populate values of Language combobox self.dlg.maLanguage.addItem('English') self.dlg.maLanguage.addItem('French') self.dlg.maLanguage.addItem('Spanish') # Populate the Edit Metadata dialog with current values from variables for x in m: ma_variable = str(x[0]) elem_name = str(x[1]) elem_name = elem_name.strip() ma_level = str(x[2]) ma_level = ma_level.strip() default_value = str(x[3]) proj_crs = QgsProject.instance().crs() if (ma_level == 'project'): # Project: get current value for each variable from project and populate field if ma_variable == 'ma_country': self.dlg.maCountry.setText( str( QgsExpressionContextUtils.projectScope( currProject).variable(ma_variable))) elif ma_variable == 'ma_crs': self.dlg.maCrs.setText( str( QgsCoordinateReferenceSystem( proj_crs).description())) elif ma_variable == 'ma_glide_number': self.dlg.maGlide.setText( str( QgsExpressionContextUtils.projectScope( currProject).variable(ma_variable))) elif ma_variable == 'ma_organisation': self.dlg.maOrganisation.setText( str( QgsExpressionContextUtils.projectScope( currProject).variable(ma_variable))) elif ma_variable == 'ma_opid': self.dlg.maOperationID.setText( str( QgsExpressionContextUtils.projectScope( currProject).variable(ma_variable))) elif ma_variable == 'ma_sourceorg': self.dlg.maSourceOrg.setText( str( QgsExpressionContextUtils.projectScope( currProject).variable(ma_variable))) elif ma_variable == 'ma_acknowledgements': self.dlg.maAcknowledgements.setText( str( QgsExpressionContextUtils.projectScope( currProject).variable(ma_variable))) elif ma_variable == 'ma_disclaimer': self.dlg.maDisclaimer.setText( str( QgsExpressionContextUtils.projectScope( currProject).variable(ma_variable))) # Layout: Get the current value of the variable if it exists and populate the field elif (ma_level == 'layout'): for layout in QgsProject.instance().layoutManager( ).printLayouts(): # If the variable doesn't exist, create it if layout.name() == self.dlg.layoutSelect.currentText(): if str( QgsExpressionContextUtils.layoutScope( layout).variable(ma_variable)) == 'None': QgsExpressionContextUtils.setLayoutVariable( layout, ma_variable, default_value) elif ma_variable == 'ma_map_number': self.dlg.maMapNumber.setText( str( QgsExpressionContextUtils.layoutScope( layout).variable(ma_variable))) elif ma_variable == 'ma_summary': self.dlg.maSummary.setText( str( QgsExpressionContextUtils.layoutScope( layout).variable(ma_variable))) elif ma_variable == 'ma_datasource': self.dlg.maDatasource.setText( str( QgsExpressionContextUtils.layoutScope( layout).variable(ma_variable))) elif ma_variable == 'ma_title': self.dlg.maTitle.setText( str( QgsExpressionContextUtils.layoutScope( layout).variable(ma_variable))) elif ma_variable == 'ma_version': self.dlg.maVersion.setValue( int( QgsExpressionContextUtils.layoutScope( layout).variable(ma_variable))) elif ma_variable == 'ma_status': self.dlg.maStatus.setCurrentText( str( QgsExpressionContextUtils.layoutScope( layout).variable(ma_variable))) elif ma_variable == 'ma_access': self.dlg.maAccess.setCurrentText( str( QgsExpressionContextUtils.layoutScope( layout).variable(ma_variable))) elif ma_variable == 'ma_language': self.dlg.maLanguage.setCurrentText( str( QgsExpressionContextUtils.layoutScope( layout).variable(ma_variable))) else: # ACTION: update or remove this message QgsMessageLog.logMessage('Warning: level ' + str(ma_level), 'MapExport')