Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Value Relation Widget #600

Merged
merged 26 commits into from
Sep 5, 2019
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
c2d8f24
add value relation widget
signedav Jun 25, 2019
b201241
add value relation widget to widget handling
signedav Jun 25, 2019
709ae2c
inherit from RelationWidget
signedav Jun 25, 2019
66a6dce
pass FeatureListModel and relation but not that nice
signedav Jun 25, 2019
bbd2cb9
value field of value relation
signedav Jun 25, 2019
3a7ef39
rename and rearrange editorwidget/RelationWidget.qml to RelationCombo…
signedav Jun 27, 2019
8c1489c
use QgsProject->mapLayer to get layer
signedav Jun 27, 2019
ebb9b8f
add mapLayer to qmlRegisterUncreatableType
signedav Jun 27, 2019
a104a38
AllowNull instead of AllowNULL (how it's called in relationreference)
signedav Jun 27, 2019
c83ac1f
add additional role to call displayString manually
signedav Jun 27, 2019
c415622
Basic gui implementation for multi value
signedav Jun 27, 2019
eaab3cd
indents
signedav Jun 27, 2019
cbf571e
Model for multiple selection in ValueRelation
signedav Jul 2, 2019
4388d25
fix the scope
signedav Jul 3, 2019
3415f9f
updating value on model change
signedav Jul 3, 2019
6635af6
beginResetModel / endResetModel and log that has to be removed later
signedav Jul 3, 2019
040c828
- keep index on list
signedav Jul 3, 2019
a5a7161
clear model on adding features
signedav Jul 3, 2019
16cf30e
remove wrong sign
signedav Jul 3, 2019
004d202
handle JSON and HSTORE regarding the field.type
signedav Jul 4, 2019
7a81a1c
Bump SDK
m-kuhn Jul 5, 2019
5c5f6aa
Fix builds with QGIS 3.9
m-kuhn Jul 7, 2019
8ba98ef
Bump SDK
m-kuhn Jul 26, 2019
35bb72b
use of QgsPostgresStringUtils
signedav Aug 11, 2019
e3f6cd0
exclude QgsPostgresStringUtils for android 5
signedav Sep 2, 2019
039d707
clean up
signedav Sep 2, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/core/core.pro
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ HEADERS += \
linepolygonhighlight.h \
qgsgeometrywrapper.h \
valuemapmodel.h \
referencingfeaturelistmodel.h
referencingfeaturelistmodel.h \
featurechecklistmodel.h

SOURCES += \
appinterface.cpp \
Expand Down Expand Up @@ -102,7 +103,8 @@ SOURCES += \
linepolygonhighlight.cpp \
qgsgeometrywrapper.cpp \
valuemapmodel.cpp \
referencingfeaturelistmodel.cpp
referencingfeaturelistmodel.cpp \
featurechecklistmodel.cpp

INCLUDEPATH += ../../3rdparty/tessellate \
../qgsquick \
Expand Down
71 changes: 71 additions & 0 deletions src/core/featurechecklistmodel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include "featurechecklistmodel.h"
signedav marked this conversation as resolved.
Show resolved Hide resolved

FeatureCheckListModel::FeatureCheckListModel()
{

}

QVariant FeatureCheckListModel::data( const QModelIndex &index, int role ) const
{
if ( role == CheckedRole )
return mCheckedEntries.contains( FeatureListModel::data( index, FeatureListModel::KeyFieldRole ).toString() );
else
return FeatureListModel::data( index, role );
}

bool FeatureCheckListModel::setData( const QModelIndex &index, const QVariant &value, int role )
{
if ( data( index, role ) == value )
return true;

switch ( role )
{
case CheckedRole:
{
if ( value.toBool() )
setChecked( index );
else
setUnchecked( index );
break;
}
}
return true;
}

QHash<int, QByteArray> FeatureCheckListModel::roleNames() const
{
QHash<int, QByteArray> roles = FeatureListModel::roleNames();

roles[CheckedRole] = "checked";
signedav marked this conversation as resolved.
Show resolved Hide resolved

return roles;
}

QString FeatureCheckListModel::attributeValue() const
{
return mAttributeValue;
}

void FeatureCheckListModel::setAttributeValue( const QString &attributeValue )
{
//just for test
return;

if ( mAttributeValue == attributeValue )
return;

mAttributeValue = attributeValue;

//set mCheckedEntries
emit attributeValueChanged();
}

void FeatureCheckListModel::setChecked( const QModelIndex &index )
{
mCheckedEntries.append( FeatureListModel::data( index, FeatureListModel::KeyFieldRole ).toString() );
}

void FeatureCheckListModel::setUnchecked( const QModelIndex &index )
{
mCheckedEntries.removeAll( FeatureListModel::data( index, FeatureListModel::KeyFieldRole ).toString() );
}
55 changes: 55 additions & 0 deletions src/core/featurechecklistmodel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#ifndef FEATURECHECKLISTMODEL_H
#define FEATURECHECKLISTMODEL_H

#include <featurelistmodel.h>

class FeatureCheckListModel : public FeatureListModel
{
Q_OBJECT

/**
* The attribute value
*/
Q_PROPERTY( QString attributeValue READ attributeValue WRITE setAttributeValue NOTIFY attributeValueChanged )

public:
FeatureCheckListModel();

enum FeatureListRoles
{
CheckedRole
};

/*
virtual QModelIndex index( int row, int column, const QModelIndex &parent ) const override;
virtual QModelIndex parent( const QModelIndex &child ) const override;
virtual int rowCount( const QModelIndex &parent ) const override;
virtual int columnCount( const QModelIndex &parent ) const override;
*/
virtual QVariant data( const QModelIndex &index, int role ) const override;
virtual bool setData( const QModelIndex &index, const QVariant &value, int role ) override;

QHash<int, QByteArray> roleNames() const override;

/**
* the attribute value (hstore or json list)
signedav marked this conversation as resolved.
Show resolved Hide resolved
*/
QString attributeValue() const;

/**
* the attribute value (hstore or json list)
signedav marked this conversation as resolved.
Show resolved Hide resolved
*/
void setAttributeValue( const QString &attributeValue );

signals:
void attributeValueChanged();

private:
void setChecked( const QModelIndex &index );
void setUnchecked( const QModelIndex &index );

QString mAttributeValue;
QStringList mCheckedEntries;
};

#endif // FEATURECHECKLISTMODEL_H
30 changes: 26 additions & 4 deletions src/core/featurelistmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "featurelistmodel.h"

#include "qgsvectorlayer.h"
#include <qgsproject.h>

FeatureListModel::FeatureListModel()
: mCurrentLayer( nullptr )
Expand Down Expand Up @@ -55,7 +56,7 @@ int FeatureListModel::columnCount( const QModelIndex &parent ) const

QVariant FeatureListModel::data( const QModelIndex &index, int role ) const
{
if ( role == Qt::DisplayRole )
if ( role == Qt::DisplayRole || role == DisplayStringRole )
return mEntries.value( index.row() ).displayString;
else if ( role == KeyFieldRole )
return mEntries.value( index.row() ).key;
Expand All @@ -67,7 +68,8 @@ QHash<int, QByteArray> FeatureListModel::roleNames() const
{
QHash<int, QByteArray> roles = QAbstractItemModel::roleNames();

roles[KeyFieldRole] = "keyField";
roles[KeyFieldRole] = "keyFieldValue";
roles[DisplayStringRole] = "displayString";

return roles;
}
Expand Down Expand Up @@ -118,6 +120,23 @@ void FeatureListModel::setKeyField( const QString &keyField )
emit keyFieldChanged();
}

QString FeatureListModel::displayValueField() const
{
return mDisplayValueField;
}

void FeatureListModel::setDisplayValueField( const QString &displayValueField )
{
if ( mDisplayValueField == displayValueField )
return;

mDisplayValueField = displayValueField;

reloadLayer();

emit displayValueFieldChanged();
}

int FeatureListModel::findKey( const QVariant &key ) const
{
int idx = 0;
Expand All @@ -142,7 +161,6 @@ void FeatureListModel::onFeatureDeleted()
reloadLayer();
}


void FeatureListModel::processReloadLayer()
{
mEntries.clear();
Expand All @@ -165,6 +183,7 @@ void FeatureListModel::processReloadLayer()
request.setSubsetOfAttributes( referencedColumns, fields );

int keyIndex = fields.indexOf( mKeyField );
int displayValueIndex = fields.indexOf( mDisplayValueField );

QgsFeatureIterator iterator = mCurrentLayer->getFeatures( request );

Expand All @@ -178,7 +197,10 @@ void FeatureListModel::processReloadLayer()
while ( iterator.nextFeature( feature ) )
{
context.setFeature( feature );
entries.append( Entry( expression.evaluate( &context ).toString(), feature.attribute( keyIndex ) ) );
if ( mDisplayValueField.isEmpty() )
entries.append( Entry( expression.evaluate( &context ).toString(), feature.attribute( keyIndex ) ) );
else
entries.append( Entry( feature.attribute( displayValueIndex ).toString(), feature.attribute( keyIndex ) ) );
}

if ( mOrderByValue )
Expand Down
13 changes: 12 additions & 1 deletion src/core/featurelistmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class QgsVectorLayer;
* Provides access to a list of features from a layer.
* For each feature, the display expression is exposed as DisplayRole
* and a keyField as KeyFieldRole for a unique identifier.
* If a displayValueField is set it replaces the display expression of the layer.
*/
class FeatureListModel : public QAbstractItemModel
{
Expand All @@ -38,6 +39,10 @@ class FeatureListModel : public QAbstractItemModel
* The primary key field
*/
Q_PROPERTY( QString keyField READ keyField WRITE setKeyField NOTIFY keyFieldChanged )
/**
* The display value field
*/
Q_PROPERTY( QString displayValueField READ displayValueField WRITE setDisplayValueField NOTIFY displayValueFieldChanged )

Q_PROPERTY( bool orderByValue READ orderByValue WRITE setOrderByValue NOTIFY orderByValueChanged )

Expand All @@ -46,7 +51,8 @@ class FeatureListModel : public QAbstractItemModel
public:
enum FeatureListRoles
{
KeyFieldRole = Qt::UserRole + 1
KeyFieldRole = Qt::UserRole + 1,
DisplayStringRole
};

Q_ENUM( FeatureListRoles )
Expand All @@ -67,6 +73,9 @@ class FeatureListModel : public QAbstractItemModel
QString keyField() const;
void setKeyField( const QString &keyField );

QString displayValueField() const;
void setDisplayValueField( const QString &displayValueField );

/**
* Get the row for a given key value.
*/
Expand Down Expand Up @@ -95,6 +104,7 @@ class FeatureListModel : public QAbstractItemModel
signals:
void currentLayerChanged();
void keyFieldChanged();
void displayValueFieldChanged();
void addNullChanged();
void orderByValueChanged();

Expand Down Expand Up @@ -134,6 +144,7 @@ class FeatureListModel : public QAbstractItemModel

QList<Entry> mEntries;
QString mKeyField;
QString mDisplayValueField;
bool mOrderByValue = false;
bool mAddNull = false;

Expand Down
3 changes: 3 additions & 0 deletions src/core/qgismobileapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
#include "linepolygonhighlight.h"
#include "valuemapmodel.h"
#include "referencingfeaturelistmodel.h"
#include "featurechecklistmodel.h"

QgisMobileapp::QgisMobileapp( QgsApplication *app, QObject *parent )
: QQmlApplicationEngine( parent )
Expand Down Expand Up @@ -157,6 +158,7 @@ void QgisMobileapp::initDeclarative()
qmlRegisterUncreatableType<QgsUnitTypes>( "org.qgis", 1, 0, "QgsUnitTypes", "" );
qmlRegisterUncreatableType<QgsRelationManager>( "org.qgis", 1, 0, "RelationManager", "The relation manager is available from the QgsProject. Try `qgisProject.relationManager`" );
qmlRegisterUncreatableType<QgsWkbTypes>( "org.qgis", 1, 0, "QgsWkbTypes", "" );
qmlRegisterUncreatableType<QgsMapLayer>( "org.qgis", 1, 0, "MapLayer", "" );

// Register QgsQuick QML types
qmlRegisterType<QgsQuickMapCanvasMap>( "org.qgis", 1, 0, "MapCanvasMap" );
Expand Down Expand Up @@ -195,6 +197,7 @@ void QgisMobileapp::initDeclarative()
qmlRegisterType<QgsGeometryWrapper>( "org.qfield", 1, 0, "QgsGeometryWrapper" );
qmlRegisterType<ValueMapModel>( "org.qfield", 1, 0, "ValueMapModel" );
qmlRegisterType<ReferencingFeatureListModel>( "org.qgis", 1, 0, "ReferencingFeatureListModel" );
qmlRegisterType<FeatureCheckListModel>( "org.qfield", 1, 0, "FeatureCheckListModel" );

qmlRegisterUncreatableType<AppInterface>( "org.qgis", 1, 0, "QgisInterface", "QgisInterface is only provided by the environment and cannot be created ad-hoc" );
qmlRegisterUncreatableType<Settings>( "org.qgis", 1, 0, "Settings", "" );
Expand Down
Loading