Skip to content
Permalink
Browse files

[qgsquick] [feature] Added identify and highlight feature

  • Loading branch information
viktor.sklencar@lutraconsulting.co.uk authored and PeterPetrik committed May 22, 2018
1 parent 6002ce0 commit 264788ac21342d04ce2ca73cc5092dc0af1f0e18
@@ -1,19 +1,31 @@
############################################################
# sources
SET(QGIS_QUICK_GUI_MOC_HDRS
qgsquickfeature.h
qgsquickfeaturemodel.h
qgsquickfeaturehighlight.h
qgsquickidentifykit.h
qgsquickmapcanvasmap.h
qgsquickmapsettings.h
qgsquickmaptransform.h
qgsquickmessagelogmodel.h
qgsquickscalebarkit.h
qgsquickutils.h
)

SET(QGIS_QUICK_GUI_HDRS
qgsquickhighlightsgnode.h
)

SET(QGIS_QUICK_GUI_SRC
qgsquickfeature.cpp
qgsquickfeaturemodel.cpp
qgsquickfeaturehighlight.cpp
qgsquickhighlightsgnode.cpp
qgsquickidentifykit.cpp
qgsquickmapcanvasmap.cpp
qgsquickmapsettings.cpp
qgsquickmaptransform.cpp
qgsquickmessagelogmodel.cpp
qgsquickscalebarkit.cpp
qgsquickutils.cpp
@@ -29,8 +29,13 @@
#include "qgscoordinatetransformcontext.h"
#include "qgsvectorlayer.h"

#include "qgsquickfeaturemodel.h"
#include "qgsquickfeaturehighlight.h"
#include "qgsquickidentifykit.h"
#include "qgsquickfeature.h"
#include "qgsquickmapcanvasmap.h"
#include "qgsquickmapsettings.h"
#include "qgsquickmaptransform.h"
#include "qgsquickmessagelogmodel.h"
#include "qgsquickplugin.h"
#include "qgsquickscalebarkit.h"
@@ -53,10 +58,15 @@ void QgsQuickPlugin::registerTypes( const char *uri )
qRegisterMetaType< QgsFeatureId > ( "QgsFeatureId" );
qRegisterMetaType< QgsPoint >( "QgsPoint" );
qRegisterMetaType< QgsPointXY >( "QgsPointXY" );
qRegisterMetaType< QgsQuickFeature >( "QgsQuickFeature" );

qmlRegisterType< QgsProject >( uri, 0, 1, "Project" );
qmlRegisterType< QgsQuickFeatureModel >( uri, 0, 1, "FeatureModel" );
qmlRegisterType< QgsQuickFeatureHighlight >( uri, 0, 1, "FeatureHighlight" );
qmlRegisterType< QgsQuickIdentifyKit >( uri, 0, 1, "IdentifyKit" );
qmlRegisterType< QgsQuickMapCanvasMap >( uri, 0, 1, "MapCanvasMap" );
qmlRegisterType< QgsQuickMapSettings >( uri, 0, 1, "MapSettings" );
qmlRegisterType< QgsQuickMapTransform >( uri, 0, 1, "MapTransform" );
qmlRegisterType< QgsQuickMessageLogModel >( uri, 0, 1, "MessageLogModel" );
qmlRegisterType< QgsQuickScaleBarKit >( uri, 0, 1, "ScaleBarKit" );
qmlRegisterType< QgsVectorLayer >( uri, 0, 1, "VectorLayer" );
@@ -0,0 +1,55 @@
/***************************************************************************
qgsquickfeature.cpp
---------------------
Date : Nov 2017
Copyright : (C) 2017 by Peter Petrik
Email : zilolv at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsvectorlayer.h"
#include "qgsfeature.h"

#include "qgsquickfeature.h"

QgsQuickFeature::QgsQuickFeature( const QgsFeature &feature, QgsVectorLayer *layer )
: mLayer( layer )
, mFeature( feature )
{
}

QgsQuickFeature::QgsQuickFeature()
{
mFeature.setValid( false );
}

QgsVectorLayer *QgsQuickFeature::layer() const
{
return mLayer;
}

QgsFeature QgsQuickFeature::feature() const
{
return mFeature;
}

bool QgsQuickFeature::valid() const
{
return ( mLayer && mFeature.isValid() );
}

void QgsQuickFeature::setFeature( const QgsFeature &feature )
{
mFeature = feature;
}

void QgsQuickFeature::setLayer( QgsVectorLayer *layer )
{
mLayer = layer;
}
@@ -0,0 +1,102 @@
/***************************************************************************
qgsquickfeature.h
---------------------
Date : Nov 2017
Copyright : (C) 2017 by Peter Petrik
Email : zilolv at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSQUICKFEATURE_H
#define QGSQUICKFEATURE_H

#include <QObject>

#include "qgsfeature.h"

#include "qgis_quick.h"

class QgsVectorLayer;

/**
* \ingroup quick
* Pair of QgsFeature and QgsVectorLayer
*
* Vector layer is commonly used to gather geometry type or CRS
* for the feature.
*
* Note that the feature may or may not be part of the vector layer's
* associated features
*
* \note QML Type: QgsQuickFeature
*
* \since QGIS 3.2
*/
class QUICK_EXPORT QgsQuickFeature
{
Q_GADGET

/**
* Vector layer to which the feature belongs.
*
* This is a readonly property.
*/
Q_PROPERTY( QgsVectorLayer *layer READ layer )

/**
* Feature instance itself.
*
* This is a readonly property.
*/
Q_PROPERTY( QgsFeature feature READ feature )

/**
* Whether the feature is valid and vector layer assigned.
*
* This is a readonly property.
*/
Q_PROPERTY( bool valid READ valid )

public:
//! Constructor of a new feature.
QgsQuickFeature();

/**
* Constructor of a new feature.
* \param feature QgsFeature associated.
* \param layer Vector layer which the feature belongs to, if not defined, the feature is not valid.
*/
QgsQuickFeature( const QgsFeature &feature,
QgsVectorLayer *layer );

//! \copydoc QgsQuickFeature::layer
QgsVectorLayer *layer() const;

//! \copydoc QgsQuickFeature::feature
QgsFeature feature() const;

//! \copydoc QgsQuickFeature::valid
bool valid() const;

//! \copydoc QgsQuickFeature::feature
void setFeature( const QgsFeature &feature );

//! \copydoc QgsQuickFeature::layer
void setLayer( QgsVectorLayer *layer );

private:
QgsVectorLayer *mLayer = nullptr; // not owned
QgsFeature mFeature;
};

typedef QList<QgsQuickFeature> QgsQuickFeatureList;

Q_DECLARE_METATYPE( QgsQuickFeature )

#endif // QGSQUICKFEATURE_H
@@ -0,0 +1,82 @@
/***************************************************************************
qgsqguickfeaturehighlight.cpp
--------------------------------------
Date : 9.12.2014
Copyright : (C) 2014 by Matthias Kuhn
Email : matthias@opengis.ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsvectorlayer.h"

#include "qgsquickfeaturemodel.h"
#include "qgsquickfeaturehighlight.h"
#include "qgsquickmapsettings.h"
#include "qgsquickhighlightsgnode.h"


QgsQuickFeatureHighlight::QgsQuickFeatureHighlight( QQuickItem *parent )
: QQuickItem( parent )
{
setFlags( QQuickItem::ItemHasContents );
setAntialiasing( true );

connect( this, &QgsQuickFeatureHighlight::modelChanged, this, &QgsQuickFeatureHighlight::onDataChanged );
}

void QgsQuickFeatureHighlight::onDataChanged()
{
if ( mModel )
{
connect( mModel, &QgsQuickFeatureModel::modelReset, this, &QgsQuickFeatureHighlight::onModelDataChanged );
connect( mModel, &QgsQuickFeatureModel::rowsRemoved, this, &QgsQuickFeatureHighlight::onModelDataChanged );
}

onModelDataChanged();
}

void QgsQuickFeatureHighlight::onModelDataChanged()
{
mDirty = true;
update();
}

QSGNode *QgsQuickFeatureHighlight::updatePaintNode( QSGNode *n, QQuickItem::UpdatePaintNodeData * )
{
if ( !mDirty || !mMapSettings )
return n;

delete n;
n = new QSGNode;

if ( !mModel )
return n;

QgsVectorLayer *layer = mModel->feature().layer();
if ( layer )
{
QgsCoordinateTransform transf( layer->crs(), mMapSettings->destinationCrs(), mMapSettings->transformContext() );

QgsFeature feature = mModel->feature().feature();
QgsGeometry geom( feature.geometry() );
geom.transform( transf );

// TODO: this is very crude conversion! QgsQuickHighlightsNode should accept any type of geometry
QVector<QgsPoint> points;
for ( auto it = geom.vertices_begin(); it != geom.vertices_end(); ++it )
points.append( *it );

QgsQuickHighlightSGNode *rb = new QgsQuickHighlightSGNode( points, geom.type(), mColor, mWidth );
rb->setFlag( QSGNode::OwnedByParent );
n->appendChildNode( rb );
}
mDirty = false;

return n;
}
@@ -0,0 +1,92 @@
/***************************************************************************
qgsqguickfeaturehighlight.h
--------------------------------------
Date : 9.12.2014
Copyright : (C) 2014 by Matthias Kuhn
Email : matthias@opengis.ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSQUICKFEATUREHIGHLIGHT_H
#define QGSQUICKFEATUREHIGHLIGHT_H

#include <QQuickItem>

#include "qgis_quick.h"

class QgsQuickMapSettings;
class QgsQuickFeatureModel;

/**
* \ingroup quick
*
* Creates map highlights for a geometry provided by a FeatureModel.
*
* The highlights are compatible with the QtQuick scene graph.
*
* \note QML Type: FeatureModelHighlight
*
* \since QGIS 3.2
*/
class QUICK_EXPORT QgsQuickFeatureHighlight : public QQuickItem
{
Q_OBJECT

/**
* Associated map settings. Should be initialized from QML component before the first use.
*/
Q_PROPERTY( QgsQuickMapSettings *mapSettings MEMBER mMapSettings NOTIFY mapSettingsChanged )

/**
* Feature model for geometry.
*/
Q_PROPERTY( QgsQuickFeatureModel *model MEMBER mModel NOTIFY modelChanged )

/**
* Color of the highlighted geometry (feature).
*/
Q_PROPERTY( QColor color MEMBER mColor NOTIFY colorChanged )

/**
* Pen width of the highlighted geometry (feature). Default is 20.
*/
Q_PROPERTY( unsigned int width MEMBER mWidth NOTIFY widthChanged )

public:
//! Creates a new feature highlight
explicit QgsQuickFeatureHighlight( QQuickItem *parent = nullptr );

signals:
//! \copydoc QgsQuickFeatureHighlight::model
void modelChanged();

//! \copydoc QgsQuickFeatureHighlight::color
void colorChanged();

//! \copydoc QgsQuickFeatureHighlight::width
void widthChanged();

//! \copydoc QgsQuickFeatureHighlight::mapSettings
void mapSettingsChanged();

private slots:
void onDataChanged();
void onModelDataChanged();

private:
QSGNode *updatePaintNode( QSGNode *n, UpdatePaintNodeData * ) override;

QColor mColor = Qt::yellow;
bool mDirty = false;
unsigned int mWidth = 20;
QgsQuickFeatureModel *mModel = nullptr; // not owned
QgsQuickMapSettings *mMapSettings = nullptr; // not owned
};

#endif // QGSQUICKFEATUREHIGHLIGHT_H

0 comments on commit 264788a

Please sign in to comment.
You can’t perform that action at this time.