Skip to content

Commit

Permalink
Add label offset to Show Grid
Browse files Browse the repository at this point in the history
* Add label offset option

* Update screenshot

* update color names

* remove unused property
  • Loading branch information
tanneryould authored Jan 23, 2025
1 parent 0c54eca commit 80cf595
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 6 deletions.
7 changes: 3 additions & 4 deletions CppSamples/DisplayInformation/ShowGrid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Grids are often used on printed maps, but can also be helpful on digital 2D maps

## How to use the sample

Tap on the `Change Grid` button in the toolbar to open a settings view. You can change the view from 2D or 3D, select the type of grid from `Grid Type` (LatLong, MGRS, UTM, and USNG) and modify its properties like label visibility, grid line color, grid label color, and label formatting.
Tap on the `Change Grid` button in the toolbar to open a settings view. You can change the view from 2D or 3D, select the type of grid from `Grid Type` (LatLong, MGRS, UTM, and USNG) and modify its properties like label visibility, grid line color, grid label color, label formatting, and label offset.

## How it works

Expand All @@ -19,14 +19,13 @@ Tap on the `Change Grid` button in the toolbar to open a settings view. You can
3. The label position can be set with `setLabelPosition(labelPosition)` method on the grid.
* Note that as of 200.6, MGRS, UTM, and USNG grids in a SceneView only support the `Geographic` label position.
4. For the `LatitudeLongitudeGrid` type, you can specify a label format of `DECIMAL_DEGREES` or `DEGREES_MINUTES_SECONDS`.
5. To set the grid, use the `setGrid(grid)` method on the map view or scene view.
5. For all screen-aligned label placement strategies, you can set the labels' offset in device-independent pixels (DIPs) from the screen edge with `setLabelOffset(offset)`.
6. To set the grid, use the `setGrid(grid)` method on the map view or scene view.

## Relevant API

* ArcGISGrid
* LatitudeLongitudeGrid
* LatitudeLongitudeGridLabelFormat::DecimalDegrees
* LatitudeLongitudeGridLabelFormat::DegreesMinutesSeconds
* MapView
* MGRSGrid
* SceneView
Expand Down
15 changes: 13 additions & 2 deletions CppSamples/DisplayInformation/ShowGrid/ShowGrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,11 @@ ShowGrid::ShowGrid(QObject* parent /* = nullptr */):
constexpr double targetScale = 6450785;
m_map->setInitialViewpoint(Viewpoint(Point(-10336141.70018318, 5418213.05332071, SpatialReference::webMercator()), targetScale));

// Set the intial grid to LatitudeLongitudeGrid
// Set the initial grid to LatitudeLongitudeGrid
m_grid = new LatitudeLongitudeGrid(this);

// Most values are populated from the UI, but we need to set some initial values
m_currentLabelOffset = m_grid->labelOffset();
}

ShowGrid::~ShowGrid() = default;
Expand Down Expand Up @@ -173,6 +176,7 @@ void ShowGrid::setGridType(const QString& gridType)
setLabelColor(m_currentLabelColor);
setLabelPosition(m_currentLabelPosition);
setLabelFormat(m_currentLabelFormat);
setLabelOffset(m_currentLabelOffset);

emit gridTypeChanged();
}
Expand Down Expand Up @@ -200,7 +204,7 @@ void ShowGrid::setLabelsVisible(bool visible)
}

void ShowGrid::setLineColor(const QString& lineColor)
{
{
m_currentLineColor = lineColor;

SimpleLineSymbol* lineSymbol = static_cast<SimpleLineSymbol*>(m_grid->lineSymbol(0));
Expand Down Expand Up @@ -264,3 +268,10 @@ void ShowGrid::setLabelFormat(const QString& labelFormat)

emit labelFormatChanged();
}

void ShowGrid::setLabelOffset(double offset)
{
m_currentLabelOffset = offset;
m_grid->setLabelOffset(offset);
emit labelOffsetChanged();
}
4 changes: 4 additions & 0 deletions CppSamples/DisplayInformation/ShowGrid/ShowGrid.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class ShowGrid : public QObject
Q_PROPERTY(QStringList labelFormats MEMBER m_labelFormats CONSTANT)
Q_PROPERTY(QString currentLabelFormat MEMBER m_currentLabelFormat NOTIFY labelFormatChanged)
Q_PROPERTY(QStringList labelPositions MEMBER m_labelPositions CONSTANT)
Q_PROPERTY(double currentLabelOffset MEMBER m_currentLabelOffset NOTIFY labelOffsetChanged)

public:
explicit ShowGrid(QObject* parent = nullptr);
Expand All @@ -68,6 +69,7 @@ class ShowGrid : public QObject
Q_INVOKABLE void setLabelColor(const QString& labelColor);
Q_INVOKABLE void setLabelPosition(const QString& labelPosition);
Q_INVOKABLE void setLabelFormat(const QString& labelFormat);
Q_INVOKABLE void setLabelOffset(double offset);

signals:
void mapViewChanged();
Expand All @@ -80,6 +82,7 @@ class ShowGrid : public QObject
void labelColorChanged();
void labelPositionChanged();
void labelFormatChanged();
void labelOffsetChanged();

private:
// Declare private methods
Expand All @@ -104,6 +107,7 @@ class ShowGrid : public QObject
QString m_currentLabelColor;
QString m_currentLabelPosition;
QString m_currentLabelFormat;
double m_currentLabelOffset = 0.0;

// Constants
const QString s_mapView = QStringLiteral("MapView");
Expand Down
41 changes: 41 additions & 0 deletions CppSamples/DisplayInformation/ShowGrid/ShowGrid.qml
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,47 @@ Item {
}
}

Text {
Layout.leftMargin: 10
text: "Label offset"
enabled: offsetSlider.enabled
color: enabled ? "black" : "gray"
}

Slider {
id: offsetSlider
Layout.minimumWidth: leftPadding + rightPadding
Layout.rightMargin: 10
Layout.fillWidth: true
from: 0
to: 150
value: gridSample.currentLabelOffset
enabled: (gridSample.currentLabelPosition !== "Geographic" && gridSample.currentLabelPosition !== "Center")
onValueChanged: {
if (value !== gridSample.currentLabelOffset)
gridSample.setLabelOffset(value);
}

handle: Rectangle {
x: offsetSlider.leftPadding + offsetSlider.visualPosition * (offsetSlider.availableWidth - width)
y: offsetSlider.topPadding + offsetSlider.availableHeight / 2 - height / 2
implicitWidth: 26
implicitHeight: 26

color: offsetSlider.enabled ? "white" : "lightgrey"
border.color: "gray"
Text {
anchors.centerIn: parent
text: offsetSlider.value.toFixed(0)
font.pixelSize: 14
color: offsetSlider.enabled ? "black" : "gray"
onWidthChanged: {
parent.width = Math.max(width + 10, 26)
}
}
}
}

// Button to hide the styling window
Rectangle {
id: hideButton
Expand Down
Binary file modified CppSamples/DisplayInformation/ShowGrid/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 80cf595

Please sign in to comment.