Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
apply map transform directly in the feature highligh c++ code
  • Loading branch information
PeterPetrik committed May 29, 2018
1 parent 093d53a commit 8494c67
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 20 deletions.
11 changes: 10 additions & 1 deletion src/quickgui/qgsquickfeaturehighlight.cpp
Expand Up @@ -28,7 +28,10 @@ QgsQuickFeatureHighlight::QgsQuickFeatureHighlight( QQuickItem *parent )
setFlags( QQuickItem::ItemHasContents );
setAntialiasing( true );

connect( this, &QgsQuickFeatureHighlight::mapSettingsChanged, this, &QgsQuickFeatureHighlight::markDirty );
// transform to device coords
mTransform.appendToItem(this);

connect( this, &QgsQuickFeatureHighlight::mapSettingsChanged, this, &QgsQuickFeatureHighlight::onMapSettingsChanged );
connect( this, &QgsQuickFeatureHighlight::featureLayerPairChanged, this, &QgsQuickFeatureHighlight::markDirty );
connect( this, &QgsQuickFeatureHighlight::colorChanged, this, &QgsQuickFeatureHighlight::markDirty );
connect( this, &QgsQuickFeatureHighlight::widthChanged, this, &QgsQuickFeatureHighlight::markDirty );
Expand All @@ -40,6 +43,12 @@ void QgsQuickFeatureHighlight::markDirty()
update();
}

void QgsQuickFeatureHighlight::onMapSettingsChanged()
{
mTransform.setMapSettings(mMapSettings);
markDirty();
}

QSGNode *QgsQuickFeatureHighlight::updatePaintNode( QSGNode *n, QQuickItem::UpdatePaintNodeData * )
{
if ( !mDirty || !mMapSettings || !mFeatureLayerPair.isValid() )
Expand Down
8 changes: 6 additions & 2 deletions src/quickgui/qgsquickfeaturehighlight.h
Expand Up @@ -20,6 +20,7 @@

#include "qgsquickfeaturelayerpair.h"
#include "qgis_quick.h"
#include "qgsquickmaptransform.h"

class QgsQuickMapSettings;

Expand All @@ -28,9 +29,10 @@ class QgsQuickMapSettings;
*
* Creates map highlights for a geometry provided by a FeatureModel.
*
* The highlights are compatible with the QtQuick scene graph.
* The highlights are compatible with the QtQuick scene graph and
* can be direcly shown on map canvas
*
* \note QML Type: FeatureModelHighlight
* \note QML Type: FeatureHighlight
*
* \since QGIS 3.4
*/
Expand Down Expand Up @@ -81,6 +83,7 @@ class QUICK_EXPORT QgsQuickFeatureHighlight : public QQuickItem

private slots:
void markDirty();
void onMapSettingsChanged();

private:
QSGNode *updatePaintNode( QSGNode *n, UpdatePaintNodeData * ) override;
Expand All @@ -90,6 +93,7 @@ class QUICK_EXPORT QgsQuickFeatureHighlight : public QQuickItem
float mWidth = 20;
QgsQuickFeatureLayerPair mFeatureLayerPair;
QgsQuickMapSettings *mMapSettings = nullptr; // not owned
QgsQuickMapTransform mTransform;
};

#endif // QGSQUICKFEATUREHIGHLIGHT_H
5 changes: 3 additions & 2 deletions src/quickgui/qgsquickmaptransform.cpp
Expand Up @@ -46,10 +46,11 @@ void QgsQuickMapTransform::setMapSettings( QgsQuickMapSettings *mapSettings )
void QgsQuickMapTransform::updateMatrix()
{
QMatrix4x4 matrix;
float scaleFactor = 1 / mMapSettings->mapUnitsPerPixel();
float scaleFactor = static_cast<float>(1.0 / mMapSettings->mapUnitsPerPixel());

matrix.scale( scaleFactor, -scaleFactor );
matrix.translate( -mMapSettings->visibleExtent().xMinimum(), -mMapSettings->visibleExtent().yMaximum() );
matrix.translate( static_cast<float>(-mMapSettings->visibleExtent().xMinimum( )),
static_cast<float>(-mMapSettings->visibleExtent().yMaximum() ));

mMatrix = matrix;
update();
Expand Down
6 changes: 3 additions & 3 deletions src/quickgui/qgsquickmaptransform.h
Expand Up @@ -25,10 +25,10 @@ class QgsQuickMapSettings;

/**
* \ingroup quick
* The QgsQuickMapTransform is transformation that can be attached to any QQuickItem. Transformation scales and translates
* Item based on the current QgsQuickMapSettings settings.
* The QgsQuickMapTransform is transformation that can be attached to any QQuickItem.
*
* For example it can be used on QgsQuickFeatureHighlight to place it correctly on the map canvas.
* If the item is based on the map coordinates, QgsQuickMapTransform will
* transform it to the device coordintes based on the attached map settings.
*
* \note QML Type: MapTransform
*
Expand Down
17 changes: 5 additions & 12 deletions tests/src/quickgui/app/main.qml
Expand Up @@ -44,19 +44,12 @@ ApplicationWindow {
}
}

Item {
QgsQuick.FeatureHighlight {
anchors.fill: mapCanvas
transform: QgsQuick.MapTransform {
mapSettings: mapCanvas.mapSettings
}

QgsQuick.FeatureHighlight {
id: highlight
color: "red"
mapSettings: mapCanvas.mapSettings
}

z: 1 // make sure items from here are on top of the Z-order
id: highlight
color: "red"
mapSettings: mapCanvas.mapSettings
z: 1
}

Drawer {
Expand Down

0 comments on commit 8494c67

Please sign in to comment.