<commit_before>// [WriteFile Name=ExportTiles, Category=Layers]
// [Legal]
// Copyright 2016 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]

#include "ExportTiles.h"

#include <QUrl>

#include "Map.h"
#include "MapQuickView.h"
#include "Basemap.h"
#include "ExportTiles.h"
#include "ArcGISTiledLayer.h"
#include "ExportTileCacheTask.h"
#include "Envelope.h"
#include "GeometryEngine.h"
#include "SpatialReference.h"
#include "TileCache.h"

using namespace Esri::ArcGISRuntime;

ExportTiles::ExportTiles(QQuickItem* parent) :
    QQuickItem(parent),
    m_map(nullptr),
    m_mapView(nullptr),
    m_serviceUrl("http://sampleserver6.arcgisonline.com/arcgis/rest/services/World_Street_Map/MapServer"),
    m_exportTileCacheTask(nullptr)
{
}

ExportTiles::~ExportTiles()
{
}

void ExportTiles::componentComplete()
{
    QQuickItem::componentComplete();

    // find QML MapView component
    m_mapView = findChild<MapQuickView*>("mapView");

    // create a tiled basemap
    ArcGISTiledLayer* tiledLayer = new ArcGISTiledLayer(m_serviceUrl);
    Basemap* basemap = new Basemap(tiledLayer, this);

    // create a new map instance
    m_map = new Map(basemap, this);

    // set an initial viewpoint
    Envelope env(12362601, 936021, 10187678, 2567213, SpatialReference(3857));
    Viewpoint viewpoint(env);
    m_map->setInitialViewpoint(viewpoint);

    // set map on the map view
    m_mapView->setMap(m_map);

    // create the task from the tiled layer's map service info once it is loaded
    connect(tiledLayer, &ArcGISTiledLayer::doneLoading, [this, tiledLayer](Error)
    {
        m_exportTileCacheTask = new ExportTileCacheTask(tiledLayer->mapServiceInfo(), this);
    });
}

void ExportTiles::exportTileCacheFromCorners(double xCorner1, double yCorner1, double xCorner2, double yCorner2, QString dataPath)
{
    // create an envelope from the QML rectangle corners
    auto corner1 = m_mapView->screenToLocation(xCorner1, yCorner1);
    auto corner2 = m_mapView->screenToLocation(xCorner2, yCorner2);
    auto extent = Envelope(corner1, corner2);
    auto tileCacheExtent = GeometryEngine::project(extent, SpatialReference::webMercator());

    // connect to sync task doneLoading signal
    connect(m_exportTileCacheTask, &ExportTileCacheTask::defaultExportTileCacheParametersCompleted, [this, &dataPath](QUuid, ExportTileCacheParameters parameters)
    {
        m_parameters = parameters;

        // execute the task and obtain the job
        auto exportJob = m_exportTileCacheTask->exportTileCache(m_parameters, dataPath);

        // check if there is a valid job
        if (exportJob)
        {
            // connect to the job's status changed signal
            connect(exportJob, &ExportTileCacheJob::jobStatusChanged, [this, exportJob]()
            {
                // connect to the job's status changed signal to know once it is done
                switch (exportJob->jobStatus()) {
                case JobStatus::Failed:<commit_msg>Fix ExportTiles connection to take by val<commit_after>// [WriteFile Name=ExportTiles, Category=Layers]
// [Legal]
// Copyright 2016 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]

#include "ExportTiles.h"

#include <QUrl>

#include "Map.h"
#include "MapQuickView.h"
#include "Basemap.h"
#include "ExportTiles.h"
#include "ArcGISTiledLayer.h"
#include "ExportTileCacheTask.h"
#include "Envelope.h"
#include "GeometryEngine.h"
#include "SpatialReference.h"
#include "TileCache.h"

using namespace Esri::ArcGISRuntime;

ExportTiles::ExportTiles(QQuickItem* parent) :
    QQuickItem(parent),
    m_map(nullptr),
    m_mapView(nullptr),
    m_serviceUrl("http://sampleserver6.arcgisonline.com/arcgis/rest/services/World_Street_Map/MapServer"),
    m_exportTileCacheTask(nullptr)
{
}

ExportTiles::~ExportTiles()
{
}

void ExportTiles::componentComplete()
{
    QQuickItem::componentComplete();

    // find QML MapView component
    m_mapView = findChild<MapQuickView*>("mapView");

    // create a tiled basemap
    ArcGISTiledLayer* tiledLayer = new ArcGISTiledLayer(m_serviceUrl);
    Basemap* basemap = new Basemap(tiledLayer, this);

    // create a new map instance
    m_map = new Map(basemap, this);

    // set an initial viewpoint
    Envelope env(12362601, 936021, 10187678, 2567213, SpatialReference(3857));
    Viewpoint viewpoint(env);
    m_map->setInitialViewpoint(viewpoint);

    // set map on the map view
    m_mapView->setMap(m_map);

    // create the task from the tiled layer's map service info once it is loaded
    connect(tiledLayer, &ArcGISTiledLayer::doneLoading, [this, tiledLayer](Error)
    {
        m_exportTileCacheTask = new ExportTileCacheTask(tiledLayer->mapServiceInfo(), this);
    });
}

void ExportTiles::exportTileCacheFromCorners(double xCorner1, double yCorner1, double xCorner2, double yCorner2, QString dataPath)
{
    // create an envelope from the QML rectangle corners
    auto corner1 = m_mapView->screenToLocation(xCorner1, yCorner1);
    auto corner2 = m_mapView->screenToLocation(xCorner2, yCorner2);
    auto extent = Envelope(corner1, corner2);
    auto tileCacheExtent = GeometryEngine::project(extent, SpatialReference::webMercator());

    // connect to sync task doneLoading signal
    connect(m_exportTileCacheTask, &ExportTileCacheTask::defaultExportTileCacheParametersCompleted, [this, dataPath](QUuid, ExportTileCacheParameters parameters)
    {
        m_parameters = parameters;

        // execute the task and obtain the job
        auto exportJob = m_exportTileCacheTask->exportTileCache(m_parameters, dataPath);

        // check if there is a valid job
        if (exportJob)
        {
            // connect to the job's status changed signal
            connect(exportJob, &ExportTileCacheJob::jobStatusChanged, [this, exportJob]()
            {
                // connect to the job's status changed signal to know once it is done
                switch (exportJob->jobStatus()) {
                case JobStatus::Failed:
{'fraction_non_alphanumeric': 0.1000429368827823, 'fraction_numerical': 0.015457277801631602, 'mean_word_length': 3.876482903000698, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '936947', 'n_tokens_mistral': 2177, 'n_tokens_neox': 2065, 'n_words': 589}