Skip to content

Commit 8494c67

Browse files
committed
apply map transform directly in the feature highligh c++ code
1 parent 093d53a commit 8494c67

File tree

5 files changed

+27
-20
lines changed

5 files changed

+27
-20
lines changed

src/quickgui/qgsquickfeaturehighlight.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ QgsQuickFeatureHighlight::QgsQuickFeatureHighlight( QQuickItem *parent )
2828
setFlags( QQuickItem::ItemHasContents );
2929
setAntialiasing( true );
3030

31-
connect( this, &QgsQuickFeatureHighlight::mapSettingsChanged, this, &QgsQuickFeatureHighlight::markDirty );
31+
// transform to device coords
32+
mTransform.appendToItem(this);
33+
34+
connect( this, &QgsQuickFeatureHighlight::mapSettingsChanged, this, &QgsQuickFeatureHighlight::onMapSettingsChanged );
3235
connect( this, &QgsQuickFeatureHighlight::featureLayerPairChanged, this, &QgsQuickFeatureHighlight::markDirty );
3336
connect( this, &QgsQuickFeatureHighlight::colorChanged, this, &QgsQuickFeatureHighlight::markDirty );
3437
connect( this, &QgsQuickFeatureHighlight::widthChanged, this, &QgsQuickFeatureHighlight::markDirty );
@@ -40,6 +43,12 @@ void QgsQuickFeatureHighlight::markDirty()
4043
update();
4144
}
4245

46+
void QgsQuickFeatureHighlight::onMapSettingsChanged()
47+
{
48+
mTransform.setMapSettings(mMapSettings);
49+
markDirty();
50+
}
51+
4352
QSGNode *QgsQuickFeatureHighlight::updatePaintNode( QSGNode *n, QQuickItem::UpdatePaintNodeData * )
4453
{
4554
if ( !mDirty || !mMapSettings || !mFeatureLayerPair.isValid() )

src/quickgui/qgsquickfeaturehighlight.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "qgsquickfeaturelayerpair.h"
2222
#include "qgis_quick.h"
23+
#include "qgsquickmaptransform.h"
2324

2425
class QgsQuickMapSettings;
2526

@@ -28,9 +29,10 @@ class QgsQuickMapSettings;
2829
*
2930
* Creates map highlights for a geometry provided by a FeatureModel.
3031
*
31-
* The highlights are compatible with the QtQuick scene graph.
32+
* The highlights are compatible with the QtQuick scene graph and
33+
* can be direcly shown on map canvas
3234
*
33-
* \note QML Type: FeatureModelHighlight
35+
* \note QML Type: FeatureHighlight
3436
*
3537
* \since QGIS 3.4
3638
*/
@@ -81,6 +83,7 @@ class QUICK_EXPORT QgsQuickFeatureHighlight : public QQuickItem
8183

8284
private slots:
8385
void markDirty();
86+
void onMapSettingsChanged();
8487

8588
private:
8689
QSGNode *updatePaintNode( QSGNode *n, UpdatePaintNodeData * ) override;
@@ -90,6 +93,7 @@ class QUICK_EXPORT QgsQuickFeatureHighlight : public QQuickItem
9093
float mWidth = 20;
9194
QgsQuickFeatureLayerPair mFeatureLayerPair;
9295
QgsQuickMapSettings *mMapSettings = nullptr; // not owned
96+
QgsQuickMapTransform mTransform;
9397
};
9498

9599
#endif // QGSQUICKFEATUREHIGHLIGHT_H

src/quickgui/qgsquickmaptransform.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,11 @@ void QgsQuickMapTransform::setMapSettings( QgsQuickMapSettings *mapSettings )
4646
void QgsQuickMapTransform::updateMatrix()
4747
{
4848
QMatrix4x4 matrix;
49-
float scaleFactor = 1 / mMapSettings->mapUnitsPerPixel();
49+
float scaleFactor = static_cast<float>(1.0 / mMapSettings->mapUnitsPerPixel());
5050

5151
matrix.scale( scaleFactor, -scaleFactor );
52-
matrix.translate( -mMapSettings->visibleExtent().xMinimum(), -mMapSettings->visibleExtent().yMaximum() );
52+
matrix.translate( static_cast<float>(-mMapSettings->visibleExtent().xMinimum( )),
53+
static_cast<float>(-mMapSettings->visibleExtent().yMaximum() ));
5354

5455
mMatrix = matrix;
5556
update();

src/quickgui/qgsquickmaptransform.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ class QgsQuickMapSettings;
2525

2626
/**
2727
* \ingroup quick
28-
* The QgsQuickMapTransform is transformation that can be attached to any QQuickItem. Transformation scales and translates
29-
* Item based on the current QgsQuickMapSettings settings.
28+
* The QgsQuickMapTransform is transformation that can be attached to any QQuickItem.
3029
*
31-
* For example it can be used on QgsQuickFeatureHighlight to place it correctly on the map canvas.
30+
* If the item is based on the map coordinates, QgsQuickMapTransform will
31+
* transform it to the device coordintes based on the attached map settings.
3232
*
3333
* \note QML Type: MapTransform
3434
*

tests/src/quickgui/app/main.qml

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,12 @@ ApplicationWindow {
4444
}
4545
}
4646

47-
Item {
47+
QgsQuick.FeatureHighlight {
4848
anchors.fill: mapCanvas
49-
transform: QgsQuick.MapTransform {
50-
mapSettings: mapCanvas.mapSettings
51-
}
52-
53-
QgsQuick.FeatureHighlight {
54-
id: highlight
55-
color: "red"
56-
mapSettings: mapCanvas.mapSettings
57-
}
58-
59-
z: 1 // make sure items from here are on top of the Z-order
49+
id: highlight
50+
color: "red"
51+
mapSettings: mapCanvas.mapSettings
52+
z: 1
6053
}
6154

6255
Drawer {

0 commit comments

Comments
 (0)