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
Example #2
0
    def bindValueToControl(self, control, value, index=0):
        """
        Binds a control to the supplied value.

        control - QWidget based control that takes the new value
        value - A QVariant holding the value
        """
        if isinstance(control, QDateTimeEdit):
            # Can be removed after http://hub.qgis.org/issues/7013 is fixed.
            control.setDateTime(QDateTime.fromString(value.toString(), Qt.ISODate))
            button = self.getControl(control.objectName() + "_pick", QPushButton)
            if not button:
                return

            button.setIcon(QIcon(":/icons/calender"))
            button.setText("Pick")
            button.setIconSize(QSize(24, 24))
            button.pressed.connect(partial(self.pickDateTime, control, "DateTime"))

        elif isinstance(control, QPushButton):
            if control.text() == "Drawing":
                control.setIcon(QIcon(":/icons/draw"))
                control.setIconSize(QSize(24, 24))
                control.pressed.connect(partial(self.loadDrawingTool, control))
        else:
            if self.layer.editType(index) == QgsVectorLayer.UniqueValues:
                editable = control.isEditable()

            widget = QgsAttributeEditor.createAttributeEditor(self.forminstance, control, self.layer, index, value)
            wasset = QgsAttributeEditor.setValue(control, self.layer, index, value)
            log(widget)

            try:
                control.setValidator(None)
            except AttributeError:
                pass

            if self.layer.editType(index) == QgsVectorLayer.UniqueValues:
                # Set the control back to the editable state the form says it should be.
                # This is to work around http://hub.qgis.org/issues/7012
                control.setEditable(editable)
Example #3
0
    def bindValueToControl(self, control, value, index=0):
        """
        Binds a control to the supplied value.

        control - QWidget based control that takes the new value
        value - A QVariant holding the value
        """
        if isinstance(control, QDateTimeEdit):
            # Can be removed after http://hub.qgis.org/issues/7013 is fixed.
            if isinstance(value, QDateTime):
                control.setDateTime(value)
            else:
                control.setDateTime(QDateTime.fromString(value, Qt.ISODate))

            try:
                button = self.getControl(control.objectName() + "_pick",
                                         QPushButton)
                button.setIcon(QIcon(":/icons/calender"))
                button.setText("Pick")
                button.setIconSize(QSize(24, 24))
                log("{} : {}".format(control.objectName(),
                                     type(control) is QDateEdit))
                if type(control) is QDateEdit:
                    log("Type is QDateEdit")
                    button.pressed.connect(
                        partial(self.pickDateTime, control, "Date"))
                else:
                    button.pressed.connect(
                        partial(self.pickDateTime, control, "DateTime"))

            except ControlNotFound:
                pass

            self.boundControls.append(control)

        elif isinstance(control, QPushButton):
            if control.text() == "Drawing":
                control.setIcon(QIcon(":/icons/draw"))
                control.setIconSize(QSize(24, 24))
                control.pressed.connect(partial(self.loadDrawingTool, control))
                self.boundControls.append(control)

        elif hasattr(control, 'loadImage'):
            image = value
            control.loadImage(image)
            self.boundControls.append(control)

        else:
            if (isinstance(control, QComboBox) and self.layer.editType(index)
                    == QgsVectorLayer.UniqueValuesEditable):

                for v in self.layer.dataProvider().uniqueValues(index):
                    control.addItem(v, v)

                control.setEditText(value)
                self.boundControls.append(control)

            try:
                # Remove the validator because there seems to be a bug with the
                # MS SQL layers and validators.
                control.setValidator(None)
            except AttributeError:
                pass

            QgsAttributeEditor.setValue(control, self.layer, index, value)
Example #4
0
    def bindValueToControl(self, control, value, index=0):
        """
        Binds a control to the supplied value.

        control - QWidget based control that takes the new value
        value - A QVariant holding the value
        """
        if isinstance(control, QDateTimeEdit):
            # Can be removed after http://hub.qgis.org/issues/7013 is fixed.
            if isinstance(value, QDateTime):
                control.setDateTime(value)
            else:
                control.setDateTime(QDateTime.fromString(value, Qt.ISODate))
                
            try:
                button = self.getControl(control.objectName() + "_pick", QPushButton)
                button.setIcon(QIcon(":/icons/calender"))
                button.setText("Pick")
                button.setIconSize(QSize(24, 24))
                log("{} : {}".format(control.objectName(), type(control) is QDateEdit))
                if type(control) is QDateEdit:
                    log("Type is QDateEdit")
                    button.pressed.connect(partial(self.pickDateTime, control, "Date"))
                else:
                    button.pressed.connect(partial(self.pickDateTime, control, "DateTime"))
                    
            except ControlNotFound:
                pass
            
            self.boundControls.append(control)

        elif isinstance(control, QPushButton):
            if control.text() == "Drawing":
                control.setIcon(QIcon(":/icons/draw"))
                control.setIconSize(QSize(24, 24))
                control.pressed.connect(partial(self.loadDrawingTool, control))
                self.boundControls.append(control)
                
        elif hasattr(control, 'loadImage'):
            image = value
            control.loadImage(image)
            self.boundControls.append(control)
            
        else:
            if (isinstance(control, QComboBox) and
                self.layer.editType(index) == QgsVectorLayer.UniqueValuesEditable):
                
                for v in self.layer.dataProvider().uniqueValues(index):
                    control.addItem(v, v)
                                    
                control.setEditText(value)
                self.boundControls.append(control)
                
            try:
                # Remove the validator because there seems to be a bug with the 
                # MS SQL layers and validators.
                control.setValidator(None)
            except AttributeError:
                pass
            
            QgsAttributeEditor.setValue(control, self.layer, index, value)