<commit_before>/*===================================================================

The Medical Imaging Interaction Toolkit (MITK)

Copyright (c) German Cancer Research Center,
Division of Medical and Biological Informatics.
All rights reserved.

This software is distributed WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE.

See LICENSE.txt or http://www.mitk.org for details.

===================================================================*/

#include "QmitkDataStorageComboBox.h"

#include <itkCommand.h>

QmitkDataStorageComboBox::QmitkDataStorageComboBox(QWidget *parent, bool autoSelectNewNodes)
  : QComboBox(parent),
    m_DataStorage(nullptr),
    m_Predicate(nullptr),
    m_BlockEvents(false),
    m_AutoSelectNewNodes(autoSelectNewNodes)
{
  this->Init();
}

QmitkDataStorageComboBox::QmitkDataStorageComboBox(mitk::DataStorage *dataStorage,
                                                   const mitk::NodePredicateBase *predicate,
                                                   QWidget *parent,
                                                   bool autoSelectNewNodes)
  : QComboBox(parent),
    m_DataStorage(nullptr),
    m_Predicate(predicate),
    m_BlockEvents(false),
    m_AutoSelectNewNodes(autoSelectNewNodes)
{
  // make connections, fill combobox
  this->Init();
  this->SetDataStorage(dataStorage);
}

QmitkDataStorageComboBox::~QmitkDataStorageComboBox()
{
  // if there was an old storage, remove listeners
  if (!m_DataStorage.IsExpired())
  {
    auto dataStorage = m_DataStorage.Lock();

    dataStorage->AddNodeEvent.RemoveListener(
      mitk::MessageDelegate1<QmitkDataStorageComboBox, const mitk::DataNode *>(this,
                                                                               &QmitkDataStorageComboBox::AddNode));

    dataStorage->RemoveNodeEvent.RemoveListener(
      mitk::MessageDelegate1<QmitkDataStorageComboBox, const mitk::DataNode *>(this,
                                                                               &QmitkDataStorageComboBox::RemoveNode));
  }
  // we have lots of observers to nodes and their name properties, this gets ugly if nodes live longer than the box
  while (m_Nodes.size() > 0)
    RemoveNode(0);
}

int QmitkDataStorageComboBox::Find(const mitk::DataNode *dataNode) const
{
  int index = -1;

  auto nodeIt = std::find(m_Nodes.begin(), m_Nodes.end(), dataNode);

  if (nodeIt != m_Nodes.end())
    index = std::distance(m_Nodes.begin(), nodeIt);

  return index;
}

mitk::DataStorage::Pointer QmitkDataStorageComboBox::GetDataStorage() const
{
  return m_DataStorage.Lock();
}

const mitk::NodePredicateBase::ConstPointer QmitkDataStorageComboBox::GetPredicate() const
{
  return m_Predicate.GetPointer();
}
<commit_msg>Use correct data type for distance-function<commit_after>/*===================================================================

The Medical Imaging Interaction Toolkit (MITK)

Copyright (c) German Cancer Research Center,
Division of Medical and Biological Informatics.
All rights reserved.

This software is distributed WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or FITNESS FOR
A PARTICULAR PURPOSE.

See LICENSE.txt or http://www.mitk.org for details.

===================================================================*/

#include "QmitkDataStorageComboBox.h"

#include <itkCommand.h>

QmitkDataStorageComboBox::QmitkDataStorageComboBox(QWidget *parent, bool autoSelectNewNodes)
  : QComboBox(parent),
    m_DataStorage(nullptr),
    m_Predicate(nullptr),
    m_BlockEvents(false),
    m_AutoSelectNewNodes(autoSelectNewNodes)
{
  this->Init();
}

QmitkDataStorageComboBox::QmitkDataStorageComboBox(mitk::DataStorage *dataStorage,
                                                   const mitk::NodePredicateBase *predicate,
                                                   QWidget *parent,
                                                   bool autoSelectNewNodes)
  : QComboBox(parent),
    m_DataStorage(nullptr),
    m_Predicate(predicate),
    m_BlockEvents(false),
    m_AutoSelectNewNodes(autoSelectNewNodes)
{
  // make connections, fill combobox
  this->Init();
  this->SetDataStorage(dataStorage);
}

QmitkDataStorageComboBox::~QmitkDataStorageComboBox()
{
  // if there was an old storage, remove listeners
  if (!m_DataStorage.IsExpired())
  {
    auto dataStorage = m_DataStorage.Lock();

    dataStorage->AddNodeEvent.RemoveListener(
      mitk::MessageDelegate1<QmitkDataStorageComboBox, const mitk::DataNode *>(this,
                                                                               &QmitkDataStorageComboBox::AddNode));

    dataStorage->RemoveNodeEvent.RemoveListener(
      mitk::MessageDelegate1<QmitkDataStorageComboBox, const mitk::DataNode *>(this,
                                                                               &QmitkDataStorageComboBox::RemoveNode));
  }
  // we have lots of observers to nodes and their name properties, this gets ugly if nodes live longer than the box
  while (m_Nodes.size() > 0)
    RemoveNode(0);
}

int QmitkDataStorageComboBox::Find(const mitk::DataNode *dataNode) const
{
  std::iterator_traits<mitk::DataNode*>::difference_type index = -1;

  auto nodeIt = std::find(m_Nodes.begin(), m_Nodes.end(), dataNode);

  if (nodeIt != m_Nodes.end())
    index = std::distance(m_Nodes.begin(), nodeIt);

  return static_cast<int>(index);
}

mitk::DataStorage::Pointer QmitkDataStorageComboBox::GetDataStorage() const
{
  return m_DataStorage.Lock();
}

const mitk::NodePredicateBase::ConstPointer QmitkDataStorageComboBox::GetPredicate() const
{
  return m_Predicate.GetPointer();
}

{'fraction_non_alphanumeric': 0.13760337849727255, 'fraction_numerical': 0.0017596339961288051, 'mean_word_length': 3.309325246398787, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '669771', 'n_tokens_mistral': 1539, 'n_tokens_neox': 1508, 'n_words': 335}