Combine multiple symbols from a mobile style file into a single symbol.

Use case
You may choose to display individual elements of a dataset like a water infrastructure network (such as valves, nodes, or endpoints) with the same basic shape, but wish to modify characteristics of elements according to some technical specifications. Multilayer symbols lets you add or remove components or modify the colors to create advanced symbol styles.
How to use the sample
Select a symbol and a color from each of the category lists to create an emoji. A preview of the symbol is updated as selections are made. The size of the symbol can be set using the slider. Click the map to create a point graphic using the customized emoji symbol, and click Clear to clear all graphics from the display.
How it works
- Create a new
SymbolStylefrom a stylx file, and load it. - Get a list of symbols in the style by calling
SymbolStyle::searchSymbolsAsyncwithinReadSymbolsFromMobileStyle::searchSymbolLayer. - Display the resulting
SymbolStyleSearchResultListModelinside a series of ComboBoxes. - When symbol selections change, create a new multilayer symbol by passing the keys for the selected symbols into
SymbolStyle::fetchSymbolAsync. - Iterate through the symbol layers and color lock all symbol layers except the base layer and update the current symbol preview image by calling
Symbol::createSwatchAsync. - Create graphics symbolized with the current symbol when the user taps the map view.
Relevant API
- MultilayerPointSymbol
- Symbol::createSwatch
- SymbolLayer
- SymbolStyle
- SymbolStyle::fetchSymbolAsync
- SymbolStyle::searchSymbolsAsync
- SymbolStyleSearchParameters
- SymbolStyleSearchResult
- SymbolStyleSearchResultListModel
Offline Data
A mobile style file (created using ArcGIS Pro) provides the symbols used by the sample.
| Link | Local Location |
|---|---|
| Emoji mobile style | <userhome>/ArcGIS/Runtime/Data/style/emoji-mobile.stylx |
About the data
The mobile style file used in this sample was created using ArcGIS Pro, and is hosted on ArcGIS Online. It contains symbol layers that can be combined to create emojis.
Additional information
While each of these symbols can be created from scratch, a more convenient workflow is to author them using ArcGIS Pro and store them in a mobile style file (.stylx). The ArcGIS Maps SDK for Qt can read symbols from a mobile style, and you can modify and combine them as needed in your app.
Tags
advanced symbology, mobile style, multilayer, stylx
Sample Code
// [WriteFile Name=ReadSymbolsFromMobileStyle, Category=DisplayInformation]// [Legal]// Copyright 2019 Esri.//// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at// http://www.apache.org/licenses/LICENSE-2.0//// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.// [Legal]
#ifdef PCH_BUILD#include "pch.hpp"#endif // PCH_BUILD
// sample headers#include "ReadSymbolsFromMobileStyle.h"#include "SymbolImageProvider.h"
// ArcGIS Maps SDK headers#include "Error.h"#include "Graphic.h"#include "GraphicListModel.h"#include "GraphicsOverlay.h"#include "GraphicsOverlayListModel.h"#include "Map.h"#include "MapQuickView.h"#include "MapTypes.h"#include "MultilayerPointSymbol.h"#include "MultilayerSymbol.h"#include "Point.h"#include "SymbolLayer.h"#include "SymbolLayerListModel.h"#include "SymbolStyle.h"#include "SymbolStyleSearchParameters.h"#include "SymbolStyleSearchResult.h"#include "SymbolStyleSearchResultListModel.h"
// Qt headers#include <QFuture>#include <QObject>#include <QQmlContext>#include <QStandardPaths>#include <QTemporaryDir>#include <QtCore/qglobal.h>
using namespace Esri::ArcGISRuntime;
constexpr int hatIndex = 0;constexpr int mouthIndex = 1;constexpr int eyeIndex = 2;constexpr int faceIndex = 3;
// helper method to get cross platform data pathnamespace{QString defaultDataPath(){ QString dataPath;
#ifdef Q_OS_IOS dataPath = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);#else dataPath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);#endif
return dataPath;}} // namespace
ReadSymbolsFromMobileStyle::ReadSymbolsFromMobileStyle(QObject* parent /* = nullptr */) : QObject(parent), m_map(new Map(BasemapStyle::ArcGISTopographic, this)), m_graphicParent(new QObject()){ m_symbolStyle = new SymbolStyle(defaultDataPath() + "/ArcGIS/Runtime/Data/styles/emoji-mobile.stylx", this);
// Load the style connect(m_symbolStyle, &SymbolStyle::doneLoading, this, [this](const Error& e) { if (!e.isEmpty()) return; searchSymbolLayer("Hat", hatIndex); searchSymbolLayer("Mouth", mouthIndex); searchSymbolLayer("Eyes", eyeIndex); searchSymbolLayer("Face", faceIndex); });
m_symbolStyle->load();}
ReadSymbolsFromMobileStyle::~ReadSymbolsFromMobileStyle() = default;
void ReadSymbolsFromMobileStyle::init(){ // Register the map view for QML qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView"); qmlRegisterType<ReadSymbolsFromMobileStyle>("Esri.Samples", 1, 0, "ReadSymbolsFromMobileStyleSample"); qmlRegisterUncreatableType<SymbolStyleSearchResultListModel>("Esri.Samples", 1, 0, "SymbolStyleSearchResultListModel", "SymbolStyleSearchResultListModel is uncreateable");}
MapQuickView* ReadSymbolsFromMobileStyle::mapView() const{ return m_mapView;}
// Set the view (created in QML)void ReadSymbolsFromMobileStyle::setMapView(MapQuickView* mapView){ if (!mapView || mapView == m_mapView) return;
m_mapView = mapView; m_mapView->setMap(m_map);
// Get the image provider from the QML Engine QQmlEngine* engine = QQmlEngine::contextForObject(this)->engine(); engine->addImageProvider(SymbolImageProvider::imageProviderId(), new SymbolImageProvider); m_symbolImageProvider = static_cast<SymbolImageProvider*>(engine->imageProvider(SymbolImageProvider::imageProviderId()));
// add a graphics overlay GraphicsOverlay* overlay = new GraphicsOverlay(this); m_mapView->graphicsOverlays()->append(overlay);
// connect to mouse clicked signal connect(m_mapView, &MapQuickView::mouseClicked, this, [this, overlay](QMouseEvent& mouseEvent) { if (!m_currentSymbol) return;
const Point clickedPoint = m_mapView->screenToLocation(mouseEvent.position().x(), mouseEvent.position().y()); Graphic* graphic = new Graphic(clickedPoint, m_currentSymbol, m_graphicParent.get()); overlay->graphics()->append(graphic); });
emit mapViewChanged();}
// Clear the graphics from the overlay and delete each objectvoid ReadSymbolsFromMobileStyle::clearGraphics(){ if (!m_mapView) return;
GraphicsOverlay* overlay = m_mapView->graphicsOverlays()->first(); if (!overlay) return;
// clear the list model overlay->graphics()->clear();
// reset m_graphicsParent to delete all children m_graphicParent.reset(new QObject());}
void ReadSymbolsFromMobileStyle::updateSymbol(int requestHatIndex, int requestMouthIndex, int requestEyeIndex, QColor color, int size){ if (!m_symbolStyle || !hatResults() || !mouthResults() || !eyeResults() || !faceResults()) return;
// set the color and size members m_currentColor = color; m_symbolSize = size;
// fetch the new symbol based on keys QStringList keys; keys.append(faceResults()->searchResults().at(0).key()); keys.append(eyeResults()->searchResults().at(requestEyeIndex).key()); keys.append(mouthResults()->searchResults().at(requestMouthIndex).key()); keys.append(hatResults()->searchResults().at(requestHatIndex).key()); m_symbolStyle->fetchSymbolAsync(keys).then(this, [this](Symbol* symbol) { if (m_currentSymbol) delete m_currentSymbol;
// store the resulting symbol m_currentSymbol = static_cast<MultilayerPointSymbol*>(symbol);
// ensure cast was successful if (!m_currentSymbol) return;
// set the size m_currentSymbol->setSize(m_symbolSize);
// set the color preferences per layer for (SymbolLayer* lyr : *(m_currentSymbol->symbolLayers())) lyr->setColorLocked(true);
m_currentSymbol->symbolLayers()->at(0)->setColorLocked(false);
// set the color m_currentSymbol->setColor(m_currentColor);
// request symbol swatch m_currentSymbol->createSwatchAsync().then(this, [this](const QImage& img) { if (!m_symbolImageProvider) return;
// convert the QUuid into a QString const QString imageId = QUuid().createUuid().toString(QUuid::StringFormat::WithoutBraces);
// add the image to the provider m_symbolImageProvider->addImage(imageId, img);
// update the URL with the unique id m_symbolImageUrl = QString("image://%1/%2").arg(SymbolImageProvider::imageProviderId(), imageId);
// emit the signal to trigger the QML Image to update emit symbolImageUrlChanged(); });
});}
SymbolStyleSearchResultListModel* ReadSymbolsFromMobileStyle::hatResults() const{ return m_models[hatIndex];}
SymbolStyleSearchResultListModel* ReadSymbolsFromMobileStyle::mouthResults() const{ return m_models[mouthIndex];}
SymbolStyleSearchResultListModel* ReadSymbolsFromMobileStyle::eyeResults() const{ return m_models[eyeIndex];}
SymbolStyleSearchResultListModel* ReadSymbolsFromMobileStyle::faceResults() const{ return m_models[faceIndex];}
void ReadSymbolsFromMobileStyle::searchSymbolLayer(const QString& category, int index){ SymbolStyleSearchParameters params; params.setCategories({category}); m_symbolStyle->searchSymbolsAsync(params).then(this, [this, index](SymbolStyleSearchResultListModel* results) { m_models[index] = results; emit symbolResultsChanged(); });}// [WriteFile Name=ReadSymbolsFromMobileStyle, Category=DisplayInformation]// [Legal]// Copyright 2019 Esri.//// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at// http://www.apache.org/licenses/LICENSE-2.0//// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.// [Legal]
#ifndef READSYMBOLSFROMMOBILESTYLE_H#define READSYMBOLSFROMMOBILESTYLE_H
// Qt headers#include <QColor>#include <QList>#include <QObject>#include <QScopedPointer>#include <QUrl>#include <QUuid>
namespace Esri::ArcGISRuntime{class Map;class MapQuickView;class MultilayerPointSymbol;class SymbolStyle;class SymbolStyleSearchResultListModel;}
class SymbolImageProvider;class QAbstractListModel;
Q_MOC_INCLUDE("MapQuickView.h")Q_MOC_INCLUDE("SymbolStyleSearchResultListModel.h")
class ReadSymbolsFromMobileStyle : public QObject{ Q_OBJECT
Q_PROPERTY(Esri::ArcGISRuntime::MapQuickView* mapView READ mapView WRITE setMapView NOTIFY mapViewChanged) Q_PROPERTY(Esri::ArcGISRuntime::SymbolStyleSearchResultListModel* hatResults READ hatResults NOTIFY symbolResultsChanged) Q_PROPERTY(Esri::ArcGISRuntime::SymbolStyleSearchResultListModel* mouthResults READ mouthResults NOTIFY symbolResultsChanged) Q_PROPERTY(Esri::ArcGISRuntime::SymbolStyleSearchResultListModel* eyesResults READ eyeResults NOTIFY symbolResultsChanged) Q_PROPERTY(QUrl symbolImageUrl MEMBER m_symbolImageUrl NOTIFY symbolImageUrlChanged)
public: explicit ReadSymbolsFromMobileStyle(QObject* parent = nullptr); ~ReadSymbolsFromMobileStyle();
static void init();
Q_INVOKABLE void clearGraphics(); Q_INVOKABLE void updateSymbol(int hatIndex, int mouthIndex, int eyeIndex, QColor color, int size);
signals: void mapViewChanged(); void symbolResultsChanged(); void symbolImageUrlChanged();
private: Esri::ArcGISRuntime::MapQuickView* mapView() const; void setMapView(Esri::ArcGISRuntime::MapQuickView* mapView);
Esri::ArcGISRuntime::SymbolStyleSearchResultListModel* hatResults() const; Esri::ArcGISRuntime::SymbolStyleSearchResultListModel* mouthResults() const; Esri::ArcGISRuntime::SymbolStyleSearchResultListModel* eyeResults() const; Esri::ArcGISRuntime::SymbolStyleSearchResultListModel* faceResults() const; void searchSymbolLayer(const QString& category, int index);
Esri::ArcGISRuntime::Map* m_map = nullptr; Esri::ArcGISRuntime::MapQuickView* m_mapView = nullptr; Esri::ArcGISRuntime::MultilayerPointSymbol* m_currentSymbol = nullptr; Esri::ArcGISRuntime::SymbolStyle* m_symbolStyle = nullptr; SymbolImageProvider* m_symbolImageProvider = nullptr; QList<Esri::ArcGISRuntime::SymbolStyleSearchResultListModel*> m_models = { nullptr, nullptr, nullptr, nullptr }; QList<QUuid> m_taskIds; QColor m_currentColor = QColor(Qt::yellow); int m_symbolSize = 40; QUrl m_symbolImageUrl; QScopedPointer<QObject> m_graphicParent;};
#endif // READSYMBOLSFROMMOBILESTYLE_H// [WriteFile Name=ReadSymbolsFromMobileStyle, Category=DisplayInformation]// [Legal]// Copyright 2019 Esri.//// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at// http://www.apache.org/licenses/LICENSE-2.0//// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.// [Legal]
import QtQuickimport QtQuick.Controlsimport QtQuick.Layoutsimport Esri.Samples
Item {
// add a mapView component MapView { id: view anchors.fill: parent
Component.onCompleted: { // Set the focus on MapView to initially enable keyboard navigation forceActiveFocus(); }
Button { anchors { bottom: view.attributionTop horizontalCenter: parent.horizontalCenter bottomMargin: 10 } text: "Clear Graphics" onClicked: model.clearGraphics(); } }
// Declare the C++ instance which creates the map etc. and supply the view ReadSymbolsFromMobileStyleSample { id: model mapView: view }
Rectangle { anchors { fill: optionGrid margins: -5 } color: "#efefef" }
GridLayout { id: optionGrid anchors { left: parent.left top: parent.top margins: 10 }
columns: 2
Label { Layout.columnSpan: 2 Layout.alignment: Qt.AlignCenter text: "Change Options to Modify Symbol" font.underline: true }
Label { text: "Eyes" }
SymbolComboBox { id: eyeComboBox model: model.eyesResults onCurrentTextChanged: updateSymbol() }
Label { text: "Mouth" }
SymbolComboBox { id: mouthComboBox model: model.mouthResults onCurrentTextChanged: updateSymbol() }
Label { text: "Hat" }
SymbolComboBox { id: hatComboBox model: model.hatResults onCurrentTextChanged: updateSymbol() }
Label { text: "Base Color" }
ComboBox { id: colorComboBox model: ["yellow", "green", "pink"] onCurrentTextChanged: updateSymbol() }
Label { text: "Size" }
Slider { id: sizeSlider Layout.preferredWidth: parent.width / 2 from: 1 to: 60 value: 40 onValueChanged: updateSymbol() }
Label { text: "Preview:" }
Image { id: symbolImage source: model.symbolImageUrl sourceSize.width: 40 sourceSize.height: 40 fillMode: Image.PreserveAspectFit width: 40 height: 40 } }
function updateSymbol() { model.updateSymbol(hatComboBox.currentIndex, mouthComboBox.currentIndex, eyeComboBox.currentIndex, colorComboBox.currentText, sizeSlider.value); }}// [WriteFile Name=ReadSymbolsFromMobileStyle, Category=DisplayInformation]// [Legal]// Copyright 2019 Esri.//// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at// http://www.apache.org/licenses/LICENSE-2.0//// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.// [Legal]
import QtQuickimport QtQuick.Controlsimport QtQuick.Layouts
ComboBox { id: rootComboBox textRole: "name" delegate: Item { id: cboDelegate height: 30 width: parent.width
RowLayout { Image { id: img source: symbolUrl Layout.preferredWidth: 20 Layout.preferredHeight: 20 } Label { text: name Layout.preferredWidth: cboDelegate.width - img.width elide: Text.ElideRight } }
MouseArea { anchors.fill: parent onClicked: { rootComboBox.currentIndex = index; rootComboBox.popup.close(); } } }}// [Legal]// Copyright 2019 Esri.//// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at// http://www.apache.org/licenses/LICENSE-2.0//// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.// [Legal]
// sample headers#include "SymbolImageProvider.h"
// Qt headers#include <QQuickImageProvider>
SymbolImageProvider::SymbolImageProvider() : QQuickImageProvider(QQuickImageProvider::Image){}
// reimplemented function for QML to request Images from the providerQImage SymbolImageProvider::requestImage(const QString& id, QSize* size, const QSize& requestedSize){ Q_UNUSED(size) Q_UNUSED(requestedSize) return m_images[id];}
// helper to add images to the the providervoid SymbolImageProvider::addImage(const QString& id, const QImage& img){ m_images[id] = img;}
// static function to return the image provider idQString SymbolImageProvider::imageProviderId(){ return QStringLiteral("symbolimageprovider");}// [Legal]// Copyright 2019 Esri.//// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at// http://www.apache.org/licenses/LICENSE-2.0//// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.// [Legal]
// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.
#ifndef SYMBOLIMAGEPROVIDER_H#define SYMBOLIMAGEPROVIDER_H
// Qt headers#include <QHash>#include <QImage>#include <QQuickImageProvider>
class SymbolImageProvider : public QQuickImageProvider{
public: SymbolImageProvider(); ~SymbolImageProvider() override = default;
public: QImage requestImage(const QString& id, QSize* size, const QSize& requestedSize) override; void addImage(const QString& id, const QImage& img); static QString imageProviderId();
private: QHash<QString, QImage> m_images;};
#endif // SYMBOLIMAGEPROVIDER_H// [Legal]// Copyright 2019 Esri.//// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at// http://www.apache.org/licenses/LICENSE-2.0//// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.// [Legal]
// sample headers#include "ReadSymbolsFromMobileStyle.h"
// ArcGIS Maps SDK headers#include "ArcGISRuntimeEnvironment.h"
// Qt headers#include <QDir>#include <QGuiApplication>#include <QQmlApplicationEngine>
// Platform specific headers#ifdef Q_OS_WIN#include <Windows.h>#endif
#define STRINGIZE(x) #x#define QUOTE(x) STRINGIZE(x)
int main(int argc, char *argv[]){ Esri::ArcGISRuntime::ArcGISRuntimeEnvironment::setUseLegacyAuthentication(false); QGuiApplication app(argc, argv); app.setApplicationName(QString("ReadSymbolsFromMobileStyle"));
// Use of ArcGIS location services, such as basemap styles, geocoding, and routing services, // requires an access token. For more information see // https://links.esri.com/arcgis-runtime-security-auth.
// The following methods grant an access token:
// 1. User authentication: Grants a temporary access token associated with a user's ArcGIS account. // To generate a token, a user logs in to the app with an ArcGIS account that is part of an // organization in ArcGIS Online or ArcGIS Enterprise.
// 2. API key authentication: Get a long-lived access token that gives your application access to // ArcGIS location services. Go to the tutorial at https://links.esri.com/create-an-api-key. // Copy the API Key access token.
const QString accessToken = QString("");
if (accessToken.isEmpty()) { qWarning() << "Use of ArcGIS location services, such as the basemap styles service, requires" << "you to authenticate with an ArcGIS account or set the API Key property."; } else { Esri::ArcGISRuntime::ArcGISRuntimeEnvironment::setApiKey(accessToken); }
// Initialize the sample ReadSymbolsFromMobileStyle::init();
QString arcGISRuntimeImportPath = QUOTE(ARCGIS_RUNTIME_IMPORT_PATH);
#if defined(LINUX_PLATFORM_REPLACEMENT) // on some linux platforms the string 'linux' is replaced with 1 // fix the replacement paths which were created QString replaceString = QUOTE(LINUX_PLATFORM_REPLACEMENT); arcGISRuntimeImportPath = arcGISRuntimeImportPath.replace(replaceString, "linux", Qt::CaseSensitive);#endif
// Initialize application view QQmlApplicationEngine engine; // Add the import Path engine.addImportPath(QDir(QCoreApplication::applicationDirPath()).filePath("qml")); // Add the Runtime and Extras path engine.addImportPath(arcGISRuntimeImportPath);
// Set the source engine.load(QUrl("qrc:/Samples/DisplayInformation/ReadSymbolsFromMobileStyle/main.qml"));
return app.exec();}// Copyright 2019 ESRI//// All rights reserved under the copyright laws of the United States// and applicable international laws, treaties, and conventions.//// You may freely redistribute and use this sample code, with or// without modification, provided you include the original copyright// notice and use restrictions.//// See the Sample code usage restrictions document for further information.//
import QtQuick.Controlsimport Esri.Samples
ApplicationWindow { visible: true width: 800 height: 600
ReadSymbolsFromMobileStyle { anchors.fill: parent }}