Example #1
0
    def unbindFeature(self, qgsfeature, editingmode=False):
        """
        Unbinds the feature from the form saving the values back to the QgsFeature.

        qgsfeature -- A QgsFeature that will store the new values.
        """
        savefields = []
        for index, control in self.fieldtocontrol.items():
            value = QVariant()
            if isinstance(control, QDateTimeEdit):
                value = control.dateTime().toString(Qt.ISODate)
            else:
                if self.layer.editType(index) == QgsVectorLayer.UniqueValues and control.isEditable():
                    # Due to http://hub.qgis.org/issues/7012 we can't have editable
                    # comboxs using QgsAttributeEditor. If the value isn't in the
                    # dataset already it will return null.  Until that bug is fixed
                    # we are just going to handle ourself.
                    value = control.currentText()
                else:
                    modified = QgsAttributeEditor.retrieveValue(control, self.layer, index, value)

            info("Setting value to %s from %s" % (value, control.objectName()))
            qgsfeature.changeAttribute(index, value)

            # Save the value to the database as a default if it is needed.
            if self.shouldSaveValue(control):
                savefields.append(index)

        if not editingmode:
            m = qgsfeature.attributeMap()
            fields_map = self.layer.pendingFields()
            attr = {str(fields_map[k].name()): str(v.toString()) for k, v in m.items() if k in savefields}
            self.form.setSavedValues(attr)

        return qgsfeature