def generate_form_preview(self): """ Create the form preview to show to the user. """ form = self.form.copy() form.settings['widgets'] = list(self.widgetmodel.widgets()) item = self.frame_2.layout().itemAt(0) if item and item.widget(): item.widget().setParent(None) featureform = FeatureForm.from_form(form, form.settings, None, {}) from roam import defaults defaultwidgets = form.widgetswithdefaults() layer = form.QGISLayer try: values = {} feature = next(layer.getFeatures()) defaultvalues = defaults.default_values(defaultwidgets, feature, layer) values.update(defaultvalues) featureform.bindvalues(values) except StopIteration: pass self.frame_2.layout().addWidget(featureform)
def load_feature_form(self, feature, form, editmode, clear=True, callback=None): """ Opens the form for the given feature. """ def _sink(_): pass if not callback: callback = _sink if clear: # Clear all the other open widgets that might be open. self.clear(dontemit=True) # One capture geometry, even for sub forms? # HACK Remove me and do something smarter roam.qgisfunctions.capturegeometry = feature.geometry() # Hold a reference to the fields because QGIS will let the # go out of scope and we get crashes. Yay! layer = form.QGISLayer self.fields = feature.fields() attributes = feature.attributes() fields = [field.name().lower() for field in self.fields] # Something is strange with default values and spatilite. Just delete them for now. if layer.dataProvider().name() == 'spatialite': pkindexes = layer.dataProvider().pkAttributeIndexes() for index in pkindexes: del fields[index] del attributes[index] values = CaseInsensitiveDict(zip(fields, attributes)) defaultvalues = {} if not editmode: defaultwidgets = form.widgetswithdefaults() defaultvalues = defaults.default_values(defaultwidgets, feature, layer) defaultvalues.update(featureform.loadsavedvalues(layer)) values.update(defaultvalues) initconfig = dict(form=form, canvas=self.canvas, editmode=editmode) config = dict(editmode=editmode, layers=QgsMapLayerRegistry.instance().mapLayers(), feature=feature) self.add_widget(FeatureFormWidgetEditor, values, callback, config, initconfig=initconfig)
def default_values(self, feature): """ Return the default values for the form using the given feature. :param feature: The feature that can be used for default values :return: A dict of default values for the form """ defaultwidgets = self.widgetswithdefaults() defaultvalues = defaults.default_values(defaultwidgets, feature, self.QGISLayer) return defaultvalues
def load_feature_form(self, feature, form, editmode, clear=True, callback=None): """ Opens the form for the given feature. """ def _sink(_): pass if not callback: callback = _sink if clear: # Clear all the other open widgets that might be open. self.clear(dontemit=True) # One capture geometry, even for sub forms? # HACK Remove me and do something smarter roam.qgisfunctions.capturegeometry = feature.geometry() # Hold a reference to the fields because QGIS will let the # go out of scope and we get crashes. Yay! layer = form.QGISLayer self.fields = feature.fields() attributes = feature.attributes() fields = [field.name().lower() for field in self.fields] # Something is strange with default values and spatilite. Just delete them for now. if layer.dataProvider().name() == 'spatialite': pkindexes = layer.dataProvider().pkAttributeIndexes() for index in pkindexes: del fields[index] del attributes[index] values = CaseInsensitiveDict(zip(fields, attributes)) defaultvalues = {} if not editmode: defaultwidgets = form.widgetswithdefaults() defaultvalues = defaults.default_values(defaultwidgets, feature, layer) defaultvalues.update(featureform.loadsavedvalues(layer)) values.update(defaultvalues) initconfig = dict(form=form, canvas=self.canvas, editmode=editmode, defaults=defaultvalues) config = dict(editmode=editmode, layers=QgsMapLayerRegistry.instance().mapLayers(), feature=feature) if self.project: layertools = self.project.layer_tools(layer) config['tools'] = layertools self.add_widget(FeatureFormWidgetEditor, values, callback, config, initconfig=initconfig)
def default_values(self, feature): """ Return the default values for the form using the given feature. :param feature: The feature that can be used for default values :return: A dict of default values for the form """ # One capture geometry, even for sub forms? # HACK Remove me and do something smarter roam.qgisfunctions.capturegeometry = feature.geometry() defaultwidgets = self.widgetswithdefaults() defaultvalues = defaults.default_values(defaultwidgets, feature, self.QGISLayer) return defaultvalues
def setformpreview(form): item = self.frame_2.layout().itemAt(0) if item and item.widget(): item.widget().setParent(None) featureform = FeatureForm.from_form(form, form.settings, None, {}) from roam import defaults defaultwidgets = form.widgetswithdefaults() layer = form.QGISLayer try: values = {} feature = layer.getFeatures().next() defaultvalues = defaults.default_values(defaultwidgets, feature, layer) values.update(defaultvalues) featureform.bindvalues(values) except StopIteration: pass self.frame_2.layout().addWidget(featureform)
def generate_form_preview(self): """ Create the form preview to show to the user. """ form = self.form.copy() form.settings['widgets'] = list(self.widgetmodel.widgets()) item = self.frame_2.layout().itemAt(0) if item and item.widget(): item.widget().setParent(None) featureform = FeatureForm.from_form(form, form.settings, None, {}) from roam import defaults defaultwidgets = form.widgetswithdefaults() layer = form.QGISLayer try: values = {} feature = layer.getFeatures().next() defaultvalues = defaults.default_values(defaultwidgets, feature, layer) values.update(defaultvalues) featureform.bindvalues(values) except StopIteration: pass self.frame_2.layout().addWidget(featureform)
def openform(self, form, feature, project, editmode): """ Opens a form for the given feature. """ roam.qgisfunctions.capturegeometry = feature.geometry() defaultvalues = {} layer = form.QGISLayer if not editmode: defaultwidgets = form.widgetswithdefaults() defaultvalues = defaults.default_values(defaultwidgets, feature, layer) defaultvalues.update(featureform.loadsavedvalues(layer)) self.actionDelete.setVisible(editmode) for field, value in defaultvalues.iteritems(): feature[field] = value self.formvalidation(passed=True) self.feature = feature # Hold a reference to the fields because QGIS will let the # go out of scope and we get crashes. Yay! self.fields = self.feature.fields() self.featureform = form.create_featureform(feature, defaultvalues, canvas=self.canvas) self.featureform.editingmode = editmode self.featureform.rejected.connect(self.formrejected) self.featureform.enablesave.connect(self.actionSave.setEnabled) # Call the pre loading events for the form layers = QgsMapLayerRegistry.instance().mapLayers() self.project = project fields = [field.name().lower() for field in self.fields] attributes = feature.attributes() if layer.dataProvider().name() == 'spatialite': pkindexes = layer.dataProvider().pkAttributeIndexes() for index in pkindexes: del fields[index] del attributes[index] values = CaseInsensitiveDict(zip(fields, attributes)) try: self.featureform.load(feature, layers, values) except featureform.RejectedException as rejected: self.formrejected(rejected.message, rejected.level) return self.featureform.formvalidation.connect(self.formvalidation) self.featureform.helprequest.connect(self.helprequest.emit) self.featureform.bindvalues(values) self.featureform.showlargewidget.connect(self.setlargewidget) self.actionSave.setVisible(True) self.setwidget(self.featureform) self.featureform.loaded() RoamEvents.featureformloaded.emit(form, feature, project, editmode)