<commit_before>// [WriteFile Name=ListRelatedFeatures, Category=Features]
// [Legal]
// Copyright 2017 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 "ListRelatedFeatures.h"

#include "Map.h"
#include "MapQuickView.h"
#include "Basemap.h"
#include "FeatureLayer.h"
#include "ViewInsets.h"
#include "Envelope.h"
#include "QueryParameters.h"
#include "ArcGISFeature.h"
#include "ArcGISFeatureTable.h"
#include "RelatedFeatureQueryResult.h"
#include "FeatureQueryResult.h"

#include "RelatedFeature.h"
#include "RelatedFeatureListModel.h"

#include <QList>
#include <QUrl>

using namespace Esri::ArcGISRuntime;

namespace
{
  // Convenience RAII struct that deletes all pointers in given container.
  struct FeatureQueryListResultLock
  {
    FeatureQueryListResultLock(const QList<RelatedFeatureQueryResult*>& list) : results(list) { }
    ~FeatureQueryListResultLock() { qDeleteAll(results); }
    const QList<RelatedFeatureQueryResult*>& results;
  };
}

ListRelatedFeatures::ListRelatedFeatures(QQuickItem* parent /* = nullptr */):
  QQuickItem(parent)
{
}

void ListRelatedFeatures::init()
{
  // Register the map view for QML
  qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView");
  qmlRegisterType<ListRelatedFeatures>("Esri.Samples", 1, 0, "ListRelatedFeaturesSample");
  qmlRegisterType<ViewInsets>();
  qmlRegisterUncreatableType<RelatedFeatureListModel>("Esri.ArcGISRuntimeSamples", 1, 0,
                                                      "RelatedFeatureListModel", "RelatedFeatureListModel is an uncreatable type");
}

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

  // Create a list model to store the related features
  m_relatedFeaturesModel = new RelatedFeatureListModel(this);
  emit relatedFeaturesModelChanged();

  // find QML MapView component
  m_mapView = findChild<MapQuickView*>("mapView");
  m_mapView->setSelectionProperties(SelectionProperties(QColor(Qt::yellow)));

  // Create a map using the URL of a web map
  m_map = new Map(QUrl("https://arcgis.com/home/item.html?id=dcc7466a91294c0ab8f7a094430ab437"), this);

  // Set map to map view
  m_mapView->setMap(m_map);

  // connect signals
  connectSignals();
}

void ListRelatedFeatures::connectSignals()
{
  // connect to doneLoading signal
  connect(m_map, &Map::doneLoading, this, [this](Error loadError)
  {
    if (!loadError.isEmpty())
      return;
    for (int i = 0; i < m_map->operationalLayers()->size(); i++)
    {
      // get the Alaska National Parks layer
      if (m_map->operationalLayers()->at(i)->name().contains(QString("- Alaska National Parks")))
      {
        m_alaskaNationalParks = static_cast<FeatureLayer*>(m_map->operationalLayers()->at(i));

        // connect to selectFeaturesCompleted signal
        connect(m_alaskaNationalParks, &FeatureLayer::selectFeaturesCompleted, this, [this](QUuid, FeatureQueryResult* result)
        {
          // iterate over features returned
          while (result->iterator().hasNext())
          {
            ArcGISFeature* arcGISFeature = static_cast<ArcGISFeature*>(result->iterator().next(this));
            ArcGISFeatureTable* selectedTable = static_cast<ArcGISFeatureTable*>(arcGISFeature->featureTable());

            // connect to queryRelatedFeaturesCompleted signal
            connect(selectedTable, &ArcGISFeatureTable::queryRelatedFeaturesCompleted, this, [this](QUuid, QList<RelatedFeatureQueryResult*> relatedResults)
            {
              // Lock is a convenience wrapper that deletes the contents of featureEditResults when we leave scope.
              FeatureQueryListResultLock lock(relatedResults);

              for (const RelatedFeatureQueryResult* relatedResult : lock.results)
              {
                while (relatedResult->iterator().hasNext())
                {
                  // get the related feature
                  ArcGISFeature* feature = static_cast<ArcGISFeature*>(relatedResult->iterator().next());
                  ArcGISFeatureTable* relatedTable = static_cast<ArcGISFeatureTable*>(feature->featureTable());
                  QString displayFieldName = relatedTable->layerInfo().displayFieldName();
                  QString serviceLayerName = relatedTable->layerInfo().serviceLayerName();
                  QString displayFieldValue = feature->attributes()->attributeValue(displayFieldName).toString();

                  // add the related feature to the list model
                  RelatedFeature relatedFeature = RelatedFeature(displayFieldName,
                                                                 displayFieldValue,
                                                                 serviceLayerName);
                  m_relatedFeaturesModel->addRelatedFeature(relatedFeature);
                  emit relatedFeaturesModelChanged();
                }
              }<commit_msg>Fixed selection crash in ListRelatedFeatures.<commit_after>// [WriteFile Name=ListRelatedFeatures, Category=Features]
// [Legal]
// Copyright 2017 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 "ListRelatedFeatures.h"

#include "Map.h"
#include "MapQuickView.h"
#include "Basemap.h"
#include "FeatureLayer.h"
#include "ViewInsets.h"
#include "Envelope.h"
#include "QueryParameters.h"
#include "ArcGISFeature.h"
#include "ArcGISFeatureTable.h"
#include "RelatedFeatureQueryResult.h"
#include "FeatureQueryResult.h"

#include "RelatedFeature.h"
#include "RelatedFeatureListModel.h"

#include <QList>
#include <QUrl>

using namespace Esri::ArcGISRuntime;

ListRelatedFeatures::ListRelatedFeatures(QQuickItem* parent /* = nullptr */):
  QQuickItem(parent)
{
}

void ListRelatedFeatures::init()
{
  // Register the map view for QML
  qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView");
  qmlRegisterType<ListRelatedFeatures>("Esri.Samples", 1, 0, "ListRelatedFeaturesSample");
  qmlRegisterType<ViewInsets>();
  qmlRegisterUncreatableType<RelatedFeatureListModel>("Esri.ArcGISRuntimeSamples", 1, 0,
                                                      "RelatedFeatureListModel", "RelatedFeatureListModel is an uncreatable type");
}

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

  // Create a list model to store the related features
  m_relatedFeaturesModel = new RelatedFeatureListModel(this);
  emit relatedFeaturesModelChanged();

  // find QML MapView component
  m_mapView = findChild<MapQuickView*>("mapView");
  m_mapView->setSelectionProperties(SelectionProperties(QColor(Qt::yellow)));

  // Create a map using the URL of a web map
  m_map = new Map(QUrl("https://arcgis.com/home/item.html?id=dcc7466a91294c0ab8f7a094430ab437"), this);

  // Set map to map view
  m_mapView->setMap(m_map);

  // connect signals
  connectSignals();
}

void ListRelatedFeatures::connectSignals()
{
  // connect to doneLoading signal
  connect(m_map, &Map::doneLoading, this, [this](Error loadError)
  {
    if (!loadError.isEmpty())
      return;
    for (int i = 0; i < m_map->operationalLayers()->size(); i++)
    {
      // get the Alaska National Parks layer
      if (m_map->operationalLayers()->at(i)->name().contains(QString("- Alaska National Parks")))
      {
        m_alaskaNationalParks = static_cast<FeatureLayer*>(m_map->operationalLayers()->at(i));

        // connect to selectFeaturesCompleted signal
        connect(m_alaskaNationalParks, &FeatureLayer::selectFeaturesCompleted, this, [this](QUuid, FeatureQueryResult* rawResult)
        {
          QPointer<FeatureQueryResult> result(rawResult);
          // iterate over features returned
          while (result->iterator().hasNext())
          {
            ArcGISFeature* arcGISFeature = static_cast<ArcGISFeature*>(result->iterator().next(this));
            ArcGISFeatureTable* selectedTable = static_cast<ArcGISFeatureTable*>(arcGISFeature->featureTable());

            // connect to queryRelatedFeaturesCompleted signal
            connect(selectedTable, &ArcGISFeatureTable::queryRelatedFeaturesCompleted, this, [this, arcGISFeature](QUuid, QList<RelatedFeatureQueryResult*> relatedResults)
            {
              // Delete feature on completion. We need to do this as we've
              // re-parented it from the initial FeatureQueryResult.
              QScopedPointer<ArcGISFeature> arcGISFeatureLock(arcGISFeature);

              for (const RelatedFeatureQueryResult* relatedResult : relatedResults)
              {
                while (relatedResult->iterator().hasNext())
                {
                  // get the related feature
                  ArcGISFeature* feature = static_cast<ArcGISFeature*>(relatedResult->iterator().next());
                  ArcGISFeatureTable* relatedTable = static_cast<ArcGISFeatureTable*>(feature->featureTable());
                  QString displayFieldName = relatedTable->layerInfo().displayFieldName();
                  QString serviceLayerName = relatedTable->layerInfo().serviceLayerName();
                  QString displayFieldValue = feature->attributes()->attributeValue(displayFieldName).toString();

                  // add the related feature to the list model
                  RelatedFeature relatedFeature = RelatedFeature(displayFieldName,
                                                                 displayFieldValue,
                                                                 serviceLayerName);
                  m_relatedFeaturesModel->addRelatedFeature(relatedFeature);
                  emit relatedFeaturesModelChanged();
                }
              }
{'fraction_non_alphanumeric': 0.10415287106234036, 'fraction_numerical': 0.006811086935956863, 'mean_word_length': 3.196903533148075, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 73, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5835182', 'n_tokens_mistral': 2857, 'n_tokens_neox': 2679, 'n_words': 679}