Skip to content
Permalink
Browse files
[QgsQuick] Support of importing data for a text field
  • Loading branch information
vsklencar authored and nyalldawson committed Apr 9, 2021
1 parent 2fd51b2 commit 21c38799715ca9880a6ece3930e1ecd4e82da850
@@ -99,6 +99,7 @@ void QgsQuickAttributeFormModelBase::setAttributeModel( QgsQuickAttributeModel *
disconnect( mAttributeModel, &QgsQuickAttributeModel::layerChanged, this, &QgsQuickAttributeFormModelBase::onLayerChanged );
disconnect( mAttributeModel, &QgsQuickAttributeModel::featureChanged, this, &QgsQuickAttributeFormModelBase::onFeatureChanged );
disconnect( mAttributeModel, &QgsQuickAttributeModel::modelReset, this, &QgsQuickAttributeFormModelBase::onFeatureChanged );
disconnect( mAttributeModel, &QgsQuickAttributeModel::dataChangedFailed, this, &QgsQuickAttributeFormModelBase::dataChangedFailed );
}

mAttributeModel = attributeModel;
@@ -108,6 +109,7 @@ void QgsQuickAttributeFormModelBase::setAttributeModel( QgsQuickAttributeModel *
connect( mAttributeModel, &QgsQuickAttributeModel::layerChanged, this, &QgsQuickAttributeFormModelBase::onLayerChanged );
connect( mAttributeModel, &QgsQuickAttributeModel::featureChanged, this, &QgsQuickAttributeFormModelBase::onFeatureChanged );
connect( mAttributeModel, &QgsQuickAttributeModel::modelReset, this, &QgsQuickAttributeFormModelBase::onFeatureChanged );
connect( mAttributeModel, &QgsQuickAttributeModel::dataChangedFailed, this, &QgsQuickAttributeFormModelBase::dataChangedFailed );
}

emit attributeModelChanged();
@@ -123,6 +123,8 @@ class QgsQuickAttributeFormModelBase : public QStandardItemModel
void constraintsHardValidChanged();
//! \copydoc QgsQuickAttributeFormModelBase::constraintsSoftValid
void constraintsSoftValidChanged();
//! Emitted when setData failed
void dataChangedFailed( const QString &message );

private slots:
void onFeatureChanged();
@@ -181,7 +181,10 @@ bool QgsQuickAttributeModel::setData( const QModelIndex &index, const QVariant &

if ( !fld.convertCompatible( val ) )
{
QgsMessageLog::logMessage( tr( "Value \"%1\" %4 could not be converted to a compatible value for field %2(%3)." ).arg( value.toString(), fld.name(), fld.typeName(), value.isNull() ? "NULL" : "NOT NULL" ) );
QString msg( tr( "Value \"%1\" %4 could not be converted to a compatible value for field %2(%3)." ).arg( value.toString(), fld.name(), fld.typeName(), value.isNull() ? "NULL" : "NOT NULL" ) );
QString userFriendlyMsg( tr( "Value %1 is not compatible with field type %2." ).arg( value.toString(), fld.typeName() ) );
QgsMessageLog::logMessage( msg );
emit dataChangedFailed( userFriendlyMsg );
return false;
}
bool success = mFeatureLayerPair.featureRef().setAttribute( index.row(), val );
@@ -164,6 +164,9 @@ class QUICK_EXPORT QgsQuickAttributeModel : public QAbstractListModel
//! Emitted when user allows reusing last entered values
void rememberValuesAllowChanged();

//! Emitted when setData failed
void dataChangedFailed( const QString &message );

protected:
//! Commits model changes
bool commit();
@@ -17,6 +17,7 @@ import QtQuick 2.9
import QtQuick.Controls 2.2
import QgsQuick 0.1 as QgsQuick
import QtQuick.Layouts 1.3
import QtGraphicalEffects 1.0

/**
* Text Edit for QGIS Attribute Form
@@ -25,6 +26,9 @@ import QtQuick.Layouts 1.3
*/
Item {
signal valueChanged(var value, bool isNull)
signal importDataRequested()
property var rowHeight: customStyle.fields.height * 0.75
property real iconSize: rowHeight

id: fieldItem
enabled: !readOnly
@@ -40,6 +44,8 @@ Item {
height: textArea.height == 0 ? customStyle.fields.height : 0
topPadding: 10 * QgsQuick.Utils.dp
bottomPadding: 10 * QgsQuick.Utils.dp
leftPadding: customStyle.fields.sideMargin
rightPadding: textField.leftPadding + (importDataBtn.visible ? importDataBtn.width : 0)
visible: height !== 0
anchors.left: parent.left
anchors.right: parent.right
@@ -79,6 +85,8 @@ Item {
height: config['IsMultiline'] === true ? undefined : 0
topPadding: customStyle.fields.height * 0.25
bottomPadding: customStyle.fields.height * 0.25
leftPadding: customStyle.fields.sideMargin
rightPadding: textArea.leftPadding + (importDataBtn.visible ? importDataBtn.width : 0)
visible: height !== 0
anchors.left: parent.left
anchors.right: parent.right
@@ -94,8 +102,49 @@ Item {
}

onEditingFinished: {
valueChanged( text, text == '' )
valueChanged( text, text == '' )
}
}
}

// Icon
Item {
id: importDataBtn
visible: supportDataImport
anchors.right: parent.right
anchors.verticalCenter: parent.verticalCenter
anchors.rightMargin: customStyle.fields.sideMargin

property int borderWidth: 50 * QgsQuick.Utils.dp
width: fieldItem.iconSize
height: width
antialiasing: true

MouseArea {
anchors.fill: parent
onClicked: {
fieldItem.importDataRequested()
}
}

Image {
id: importDataBtnIcon
height: fieldItem.iconSize
sourceSize.height: fieldItem.iconSize
autoTransform: true
fillMode: Image.PreserveAspectFit
source: customStyle.icons.importData
anchors.right: parent.right
anchors.verticalCenter: parent.verticalCenter
visible: fieldItem.enabled
anchors.rightMargin: fieldItem.anchors.rightMargin
}

ColorOverlay {
anchors.fill: importDataBtnIcon
anchors.centerIn: parent
source: importDataBtnIcon
color: customStyle.fields.fontColor
smooth: true
}
}
}
@@ -36,6 +36,11 @@ Item {
*/
signal canceled

/**
* When any notification message has to be shown.
*/
signal notify(var message)

/**
* A handler for extra events in externalSourceWidget.
*/
@@ -108,6 +113,30 @@ Item {
}
}

/**
* A handler for extra events withing a TextEdit widget .
*/
property var importDataHandler: QtObject {

/**
* Suppose to set `supportDataImport` variable of a feature form. If true, enables to set data by this handler.
* \param name "Name" property of field item. Expecting alias if defined, otherwise field name.
*/
property var supportImportData: function supportImportData(name) { return false }

/**
* Suppose to be called to invoke a component to set data automatically (e.g. code scanner, sensor).
* \param itemWidget editorWidget for modified field to send valueChanged signal.
*/
property var importData: function importData(itemWidget) {}

/**
* Suppose to be called after `importData` function as a callback to set the value to the widget.
* \param value Value to be set.
*/
property var setValue: function setValue(value) {}
}

/**
* AttributeFormModel binded on a feature supporting auto-generated editor layouts and "tab" layout.
*/
@@ -445,6 +474,7 @@ Item {
property var featurePair: form.model.attributeModel.featureLayerPair
property var activeProject: form.project
property var customWidget: form.customWidgetCallback
property bool supportDataImport: importDataHandler.supportImportData(Name)

active: widget !== 'Hidden'

@@ -462,6 +492,14 @@ Item {
}
}

Connections {
target: attributeEditorLoader.item
ignoreUnknownSignals: true
onImportDataRequested: {
importDataHandler.importData(attributeEditorLoader.item)
}
}

Connections {
target: form.model
onDataChanged: {
@@ -472,6 +510,11 @@ Item {
}
}

Connections {
target: form.model.attributeModel
onDataChangedFailed: notify(message)
}

Connections {
target: form
ignoreUnknownSignals: true
@@ -662,4 +705,3 @@ Item {
}
}
}

@@ -81,6 +81,7 @@ QtObject {
property var notAvailable: QgsQuick.Utils.getThemeIcon("ic_photo_notavailable_white")
property var today: QgsQuick.Utils.getThemeIcon("ic_today")
property var back: QgsQuick.Utils.getThemeIcon("ic_back")
property var importData: QgsQuick.Utils.getThemeIcon("ic_camera")
}

property QtObject checkboxComponent: QtObject {

0 comments on commit 21c3879

Please sign in to comment.