Skip to content

Commit 264788a

Browse files
viktor.sklencar@lutraconsulting.co.ukPeterPetrik
authored andcommitted
[qgsquick] [feature] Added identify and highlight feature
1 parent 6002ce0 commit 264788a

19 files changed

+1674
-1
lines changed

src/quickgui/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
11
############################################################
22
# sources
33
SET(QGIS_QUICK_GUI_MOC_HDRS
4+
qgsquickfeature.h
5+
qgsquickfeaturemodel.h
6+
qgsquickfeaturehighlight.h
7+
qgsquickidentifykit.h
48
qgsquickmapcanvasmap.h
59
qgsquickmapsettings.h
10+
qgsquickmaptransform.h
611
qgsquickmessagelogmodel.h
712
qgsquickscalebarkit.h
813
qgsquickutils.h
914
)
1015

1116
SET(QGIS_QUICK_GUI_HDRS
17+
qgsquickhighlightsgnode.h
1218
)
1319

1420
SET(QGIS_QUICK_GUI_SRC
21+
qgsquickfeature.cpp
22+
qgsquickfeaturemodel.cpp
23+
qgsquickfeaturehighlight.cpp
24+
qgsquickhighlightsgnode.cpp
25+
qgsquickidentifykit.cpp
1526
qgsquickmapcanvasmap.cpp
1627
qgsquickmapsettings.cpp
28+
qgsquickmaptransform.cpp
1729
qgsquickmessagelogmodel.cpp
1830
qgsquickscalebarkit.cpp
1931
qgsquickutils.cpp

src/quickgui/plugin/qgsquickplugin.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,13 @@
2929
#include "qgscoordinatetransformcontext.h"
3030
#include "qgsvectorlayer.h"
3131

32+
#include "qgsquickfeaturemodel.h"
33+
#include "qgsquickfeaturehighlight.h"
34+
#include "qgsquickidentifykit.h"
35+
#include "qgsquickfeature.h"
3236
#include "qgsquickmapcanvasmap.h"
3337
#include "qgsquickmapsettings.h"
38+
#include "qgsquickmaptransform.h"
3439
#include "qgsquickmessagelogmodel.h"
3540
#include "qgsquickplugin.h"
3641
#include "qgsquickscalebarkit.h"
@@ -53,10 +58,15 @@ void QgsQuickPlugin::registerTypes( const char *uri )
5358
qRegisterMetaType< QgsFeatureId > ( "QgsFeatureId" );
5459
qRegisterMetaType< QgsPoint >( "QgsPoint" );
5560
qRegisterMetaType< QgsPointXY >( "QgsPointXY" );
61+
qRegisterMetaType< QgsQuickFeature >( "QgsQuickFeature" );
5662

5763
qmlRegisterType< QgsProject >( uri, 0, 1, "Project" );
64+
qmlRegisterType< QgsQuickFeatureModel >( uri, 0, 1, "FeatureModel" );
65+
qmlRegisterType< QgsQuickFeatureHighlight >( uri, 0, 1, "FeatureHighlight" );
66+
qmlRegisterType< QgsQuickIdentifyKit >( uri, 0, 1, "IdentifyKit" );
5867
qmlRegisterType< QgsQuickMapCanvasMap >( uri, 0, 1, "MapCanvasMap" );
5968
qmlRegisterType< QgsQuickMapSettings >( uri, 0, 1, "MapSettings" );
69+
qmlRegisterType< QgsQuickMapTransform >( uri, 0, 1, "MapTransform" );
6070
qmlRegisterType< QgsQuickMessageLogModel >( uri, 0, 1, "MessageLogModel" );
6171
qmlRegisterType< QgsQuickScaleBarKit >( uri, 0, 1, "ScaleBarKit" );
6272
qmlRegisterType< QgsVectorLayer >( uri, 0, 1, "VectorLayer" );

src/quickgui/qgsquickfeature.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/***************************************************************************
2+
qgsquickfeature.cpp
3+
---------------------
4+
Date : Nov 2017
5+
Copyright : (C) 2017 by Peter Petrik
6+
Email : zilolv at gmail dot com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
16+
#include "qgsvectorlayer.h"
17+
#include "qgsfeature.h"
18+
19+
#include "qgsquickfeature.h"
20+
21+
QgsQuickFeature::QgsQuickFeature( const QgsFeature &feature, QgsVectorLayer *layer )
22+
: mLayer( layer )
23+
, mFeature( feature )
24+
{
25+
}
26+
27+
QgsQuickFeature::QgsQuickFeature()
28+
{
29+
mFeature.setValid( false );
30+
}
31+
32+
QgsVectorLayer *QgsQuickFeature::layer() const
33+
{
34+
return mLayer;
35+
}
36+
37+
QgsFeature QgsQuickFeature::feature() const
38+
{
39+
return mFeature;
40+
}
41+
42+
bool QgsQuickFeature::valid() const
43+
{
44+
return ( mLayer && mFeature.isValid() );
45+
}
46+
47+
void QgsQuickFeature::setFeature( const QgsFeature &feature )
48+
{
49+
mFeature = feature;
50+
}
51+
52+
void QgsQuickFeature::setLayer( QgsVectorLayer *layer )
53+
{
54+
mLayer = layer;
55+
}

src/quickgui/qgsquickfeature.h

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/***************************************************************************
2+
qgsquickfeature.h
3+
---------------------
4+
Date : Nov 2017
5+
Copyright : (C) 2017 by Peter Petrik
6+
Email : zilolv at gmail dot com
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
16+
#ifndef QGSQUICKFEATURE_H
17+
#define QGSQUICKFEATURE_H
18+
19+
#include <QObject>
20+
21+
#include "qgsfeature.h"
22+
23+
#include "qgis_quick.h"
24+
25+
class QgsVectorLayer;
26+
27+
/**
28+
* \ingroup quick
29+
* Pair of QgsFeature and QgsVectorLayer
30+
*
31+
* Vector layer is commonly used to gather geometry type or CRS
32+
* for the feature.
33+
*
34+
* Note that the feature may or may not be part of the vector layer's
35+
* associated features
36+
*
37+
* \note QML Type: QgsQuickFeature
38+
*
39+
* \since QGIS 3.2
40+
*/
41+
class QUICK_EXPORT QgsQuickFeature
42+
{
43+
Q_GADGET
44+
45+
/**
46+
* Vector layer to which the feature belongs.
47+
*
48+
* This is a readonly property.
49+
*/
50+
Q_PROPERTY( QgsVectorLayer *layer READ layer )
51+
52+
/**
53+
* Feature instance itself.
54+
*
55+
* This is a readonly property.
56+
*/
57+
Q_PROPERTY( QgsFeature feature READ feature )
58+
59+
/**
60+
* Whether the feature is valid and vector layer assigned.
61+
*
62+
* This is a readonly property.
63+
*/
64+
Q_PROPERTY( bool valid READ valid )
65+
66+
public:
67+
//! Constructor of a new feature.
68+
QgsQuickFeature();
69+
70+
/**
71+
* Constructor of a new feature.
72+
* \param feature QgsFeature associated.
73+
* \param layer Vector layer which the feature belongs to, if not defined, the feature is not valid.
74+
*/
75+
QgsQuickFeature( const QgsFeature &feature,
76+
QgsVectorLayer *layer );
77+
78+
//! \copydoc QgsQuickFeature::layer
79+
QgsVectorLayer *layer() const;
80+
81+
//! \copydoc QgsQuickFeature::feature
82+
QgsFeature feature() const;
83+
84+
//! \copydoc QgsQuickFeature::valid
85+
bool valid() const;
86+
87+
//! \copydoc QgsQuickFeature::feature
88+
void setFeature( const QgsFeature &feature );
89+
90+
//! \copydoc QgsQuickFeature::layer
91+
void setLayer( QgsVectorLayer *layer );
92+
93+
private:
94+
QgsVectorLayer *mLayer = nullptr; // not owned
95+
QgsFeature mFeature;
96+
};
97+
98+
typedef QList<QgsQuickFeature> QgsQuickFeatureList;
99+
100+
Q_DECLARE_METATYPE( QgsQuickFeature )
101+
102+
#endif // QGSQUICKFEATURE_H
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/***************************************************************************
2+
qgsqguickfeaturehighlight.cpp
3+
--------------------------------------
4+
Date : 9.12.2014
5+
Copyright : (C) 2014 by Matthias Kuhn
6+
Email : matthias@opengis.ch
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
16+
#include "qgsvectorlayer.h"
17+
18+
#include "qgsquickfeaturemodel.h"
19+
#include "qgsquickfeaturehighlight.h"
20+
#include "qgsquickmapsettings.h"
21+
#include "qgsquickhighlightsgnode.h"
22+
23+
24+
QgsQuickFeatureHighlight::QgsQuickFeatureHighlight( QQuickItem *parent )
25+
: QQuickItem( parent )
26+
{
27+
setFlags( QQuickItem::ItemHasContents );
28+
setAntialiasing( true );
29+
30+
connect( this, &QgsQuickFeatureHighlight::modelChanged, this, &QgsQuickFeatureHighlight::onDataChanged );
31+
}
32+
33+
void QgsQuickFeatureHighlight::onDataChanged()
34+
{
35+
if ( mModel )
36+
{
37+
connect( mModel, &QgsQuickFeatureModel::modelReset, this, &QgsQuickFeatureHighlight::onModelDataChanged );
38+
connect( mModel, &QgsQuickFeatureModel::rowsRemoved, this, &QgsQuickFeatureHighlight::onModelDataChanged );
39+
}
40+
41+
onModelDataChanged();
42+
}
43+
44+
void QgsQuickFeatureHighlight::onModelDataChanged()
45+
{
46+
mDirty = true;
47+
update();
48+
}
49+
50+
QSGNode *QgsQuickFeatureHighlight::updatePaintNode( QSGNode *n, QQuickItem::UpdatePaintNodeData * )
51+
{
52+
if ( !mDirty || !mMapSettings )
53+
return n;
54+
55+
delete n;
56+
n = new QSGNode;
57+
58+
if ( !mModel )
59+
return n;
60+
61+
QgsVectorLayer *layer = mModel->feature().layer();
62+
if ( layer )
63+
{
64+
QgsCoordinateTransform transf( layer->crs(), mMapSettings->destinationCrs(), mMapSettings->transformContext() );
65+
66+
QgsFeature feature = mModel->feature().feature();
67+
QgsGeometry geom( feature.geometry() );
68+
geom.transform( transf );
69+
70+
// TODO: this is very crude conversion! QgsQuickHighlightsNode should accept any type of geometry
71+
QVector<QgsPoint> points;
72+
for ( auto it = geom.vertices_begin(); it != geom.vertices_end(); ++it )
73+
points.append( *it );
74+
75+
QgsQuickHighlightSGNode *rb = new QgsQuickHighlightSGNode( points, geom.type(), mColor, mWidth );
76+
rb->setFlag( QSGNode::OwnedByParent );
77+
n->appendChildNode( rb );
78+
}
79+
mDirty = false;
80+
81+
return n;
82+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/***************************************************************************
2+
qgsqguickfeaturehighlight.h
3+
--------------------------------------
4+
Date : 9.12.2014
5+
Copyright : (C) 2014 by Matthias Kuhn
6+
Email : matthias@opengis.ch
7+
***************************************************************************
8+
* *
9+
* This program is free software; you can redistribute it and/or modify *
10+
* it under the terms of the GNU General Public License as published by *
11+
* the Free Software Foundation; either version 2 of the License, or *
12+
* (at your option) any later version. *
13+
* *
14+
***************************************************************************/
15+
16+
#ifndef QGSQUICKFEATUREHIGHLIGHT_H
17+
#define QGSQUICKFEATUREHIGHLIGHT_H
18+
19+
#include <QQuickItem>
20+
21+
#include "qgis_quick.h"
22+
23+
class QgsQuickMapSettings;
24+
class QgsQuickFeatureModel;
25+
26+
/**
27+
* \ingroup quick
28+
*
29+
* Creates map highlights for a geometry provided by a FeatureModel.
30+
*
31+
* The highlights are compatible with the QtQuick scene graph.
32+
*
33+
* \note QML Type: FeatureModelHighlight
34+
*
35+
* \since QGIS 3.2
36+
*/
37+
class QUICK_EXPORT QgsQuickFeatureHighlight : public QQuickItem
38+
{
39+
Q_OBJECT
40+
41+
/**
42+
* Associated map settings. Should be initialized from QML component before the first use.
43+
*/
44+
Q_PROPERTY( QgsQuickMapSettings *mapSettings MEMBER mMapSettings NOTIFY mapSettingsChanged )
45+
46+
/**
47+
* Feature model for geometry.
48+
*/
49+
Q_PROPERTY( QgsQuickFeatureModel *model MEMBER mModel NOTIFY modelChanged )
50+
51+
/**
52+
* Color of the highlighted geometry (feature).
53+
*/
54+
Q_PROPERTY( QColor color MEMBER mColor NOTIFY colorChanged )
55+
56+
/**
57+
* Pen width of the highlighted geometry (feature). Default is 20.
58+
*/
59+
Q_PROPERTY( unsigned int width MEMBER mWidth NOTIFY widthChanged )
60+
61+
public:
62+
//! Creates a new feature highlight
63+
explicit QgsQuickFeatureHighlight( QQuickItem *parent = nullptr );
64+
65+
signals:
66+
//! \copydoc QgsQuickFeatureHighlight::model
67+
void modelChanged();
68+
69+
//! \copydoc QgsQuickFeatureHighlight::color
70+
void colorChanged();
71+
72+
//! \copydoc QgsQuickFeatureHighlight::width
73+
void widthChanged();
74+
75+
//! \copydoc QgsQuickFeatureHighlight::mapSettings
76+
void mapSettingsChanged();
77+
78+
private slots:
79+
void onDataChanged();
80+
void onModelDataChanged();
81+
82+
private:
83+
QSGNode *updatePaintNode( QSGNode *n, UpdatePaintNodeData * ) override;
84+
85+
QColor mColor = Qt::yellow;
86+
bool mDirty = false;
87+
unsigned int mWidth = 20;
88+
QgsQuickFeatureModel *mModel = nullptr; // not owned
89+
QgsQuickMapSettings *mMapSettings = nullptr; // not owned
90+
};
91+
92+
#endif // QGSQUICKFEATUREHIGHLIGHT_H

0 commit comments

Comments
 (0)