<commit_before>//
// RemotePhotoTool - remote camera control software
// Copyright (C) 2008-2013 <NAME>
//
//! \file ImagePropertyCombobox.cpp Image property combobox
//

// includes
#include "stdafx.h"
#include "ImagePropertyCombobox.hpp"
#include "CameraException.hpp"
#include "CameraErrorDlg.hpp"

void ImagePropertyCombobox::SetRemoteReleaseControl(std::shared_ptr<RemoteReleaseControl> spRemoteReleaseControl)
{
   ATLASSERT(spRemoteReleaseControl != nullptr);

   m_spRemoteReleaseControl = spRemoteReleaseControl;

   m_uiPropertyId = m_spRemoteReleaseControl->MapImagePropertyTypeToId(m_enImagePropertyType);
}

void ImagePropertyCombobox::UpdateValuesList()
{
   ATLASSERT(m_spRemoteReleaseControl != nullptr);

   SetRedraw(FALSE);
   ResetContent();

   m_mapValueIndex.clear();
   m_vecValues.clear();


   try
   {
      m_spRemoteReleaseControl->EnumImagePropertyValues(m_uiPropertyId, m_vecValues);
   }
   catch(CameraException& ex)
   {
      CameraErrorDlg dlg(_T("Couldn't enumerate values for image property"), ex);
      dlg.DoModal();
   }

   bool bReadOnly = false;
   int iSelected = -1;

   try
   {
      ImageProperty currentVal = m_spRemoteReleaseControl->GetImageProperty(m_uiPropertyId);

      if (m_vecValues.empty())
      {
         // when empty, treat as read-only
         bReadOnly = true;
      }
      else
      {
         // check if the property is currently read only
         bReadOnly = currentVal.IsReadOnly();
      }

      for (size_t i=0, iMax=m_vecValues.size(); i<iMax; i++)
      {
         const ImageProperty& val = m_vecValues[i];
         int iItem = AddString(val.AsString());
         SetItemData(iItem, i);

         if (val == currentVal)
            iSelected = iItem;

         m_mapValueIndex.insert(std::make_pair(i, iItem));
      }
   }
   catch(CameraException& ex)
   {
      CameraErrorDlg dlg(_T("Couldn't get current value for image property"), ex);
      dlg.DoModal();
   }

   // select item, in case control doesn't also get a UpdateValue() call
   if (iSelected != -1)
      SetCurSel(iSelected);

   EnableWindow(bReadOnly ? FALSE : TRUE);

   SetRedraw(TRUE);
}

void ImagePropertyCombobox::UpdateValue()
{
   ATLASSERT(m_spRemoteReleaseControl != nullptr);

   try
   {
      ImageProperty val = m_spRemoteReleaseControl->GetImageProperty(m_uiPropertyId);

      // no descriptions available?
      if (m_vecValues.empty())
      {
         ResetContent();
         AddString(val.AsString());
         SetCurSel(0);

         return;
      }

      // search item in value list
      std::vector<ImageProperty>::iterator iterValues = std::find(m_vecValues.begin(), m_vecValues.end(), val);
      if (iterValues == m_vecValues.end())
         return;

      ptrdiff_t iPos = std::distance(m_vecValues.begin(), iterValues);
      ATLASSERT(iPos >= 0);

      // search cb index
      std::map<size_t, int>::const_iterator iterMap = m_mapValueIndex.find(iPos);
      if (iterMap == m_mapValueIndex.end())
         return;

      SetCurSel(iterMap->second);
   }
   catch(CameraException& ex)
   {
      CameraErrorDlg dlg(_T("Couldn't get value for image property"), ex);
      dlg.DoModal();
   }
}

LRESULT ImagePropertyCombobox::OnSelChange(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& bHandled)
{
   bHandled = false;

   ATLASSERT(m_spRemoteReleaseControl != nullptr);

   // value has changed; update value
   int iItem = CComboBox::GetCurSel();
   if (iItem == CB_ERR)
      return 0;

   size_t iIndex = GetItemData(iItem);

   try
   {
      ATLASSERT(iIndex < m_vecValues.size());
      m_spRemoteReleaseControl->SetImageProperty(m_vecValues[iIndex]);
   }
   catch(CameraException& ex)
   {<commit_msg>made image property combobox more robust when selecting values that are not in the list of possible values anymore<commit_after>//
// RemotePhotoTool - remote camera control software
// Copyright (C) 2008-2013 <NAME>
//
//! \file ImagePropertyCombobox.cpp Image property combobox
//

// includes
#include "stdafx.h"
#include "ImagePropertyCombobox.hpp"
#include "CameraException.hpp"
#include "CameraErrorDlg.hpp"

void ImagePropertyCombobox::SetRemoteReleaseControl(std::shared_ptr<RemoteReleaseControl> spRemoteReleaseControl)
{
   ATLASSERT(spRemoteReleaseControl != nullptr);

   m_spRemoteReleaseControl = spRemoteReleaseControl;

   m_uiPropertyId = m_spRemoteReleaseControl->MapImagePropertyTypeToId(m_enImagePropertyType);
}

void ImagePropertyCombobox::UpdateValuesList()
{
   ATLASSERT(m_spRemoteReleaseControl != nullptr);

   SetRedraw(FALSE);
   ResetContent();

   m_mapValueIndex.clear();
   m_vecValues.clear();


   try
   {
      m_spRemoteReleaseControl->EnumImagePropertyValues(m_uiPropertyId, m_vecValues);
   }
   catch(CameraException& ex)
   {
      CameraErrorDlg dlg(_T("Couldn't enumerate values for image property"), ex);
      dlg.DoModal();
   }

   bool bReadOnly = false;
   int iSelected = -1;

   try
   {
      ImageProperty currentVal = m_spRemoteReleaseControl->GetImageProperty(m_uiPropertyId);

      if (m_vecValues.empty())
      {
         // when empty, treat as read-only
         bReadOnly = true;
      }
      else
      {
         // check if the property is currently read only
         bReadOnly = currentVal.IsReadOnly();
      }

      for (size_t i=0, iMax=m_vecValues.size(); i<iMax; i++)
      {
         const ImageProperty& val = m_vecValues[i];
         int iItem = AddString(val.AsString());
         SetItemData(iItem, i);

         if (val == currentVal)
            iSelected = iItem;

         m_mapValueIndex.insert(std::make_pair(i, iItem));
      }
   }
   catch(CameraException& ex)
   {
      CameraErrorDlg dlg(_T("Couldn't get current value for image property"), ex);
      dlg.DoModal();
   }

   // select item, in case control doesn't also get a UpdateValue() call
   if (iSelected != -1)
      SetCurSel(iSelected);

   EnableWindow(bReadOnly ? FALSE : TRUE);

   SetRedraw(TRUE);
}

void ImagePropertyCombobox::UpdateValue()
{
   ATLASSERT(m_spRemoteReleaseControl != nullptr);

   try
   {
      ImageProperty val = m_spRemoteReleaseControl->GetImageProperty(m_uiPropertyId);

      // no descriptions available?
      if (m_vecValues.empty())
      {
         ResetContent();
         AddString(val.AsString());
         SetCurSel(0);

         return;
      }

      // search item in value list
      std::vector<ImageProperty>::iterator iterValues = std::find(m_vecValues.begin(), m_vecValues.end(), val);
      if (iterValues == m_vecValues.end())
         return;

      ptrdiff_t iPos = std::distance(m_vecValues.begin(), iterValues);
      ATLASSERT(iPos >= 0);

      // search cb index
      std::map<size_t, int>::const_iterator iterMap = m_mapValueIndex.find(iPos);
      if (iterMap == m_mapValueIndex.end())
         return;

      SetCurSel(iterMap->second);
   }
   catch(CameraException& ex)
   {
      CameraErrorDlg dlg(_T("Couldn't get value for image property"), ex);
      dlg.DoModal();
   }
}

LRESULT ImagePropertyCombobox::OnSelChange(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& bHandled)
{
   bHandled = false;

   ATLASSERT(m_spRemoteReleaseControl != nullptr);

   // value has changed; update value
   int iItem = CComboBox::GetCurSel();
   if (iItem == CB_ERR)
      return 0;

   size_t iIndex = GetItemData(iItem);

   if (iIndex >= m_vecValues.size())
      return 0; // invalid index

   try
   {
      m_spRemoteReleaseControl->SetImageProperty(m_vecValues[iIndex]);
   }
   catch(CameraException& ex)
   {
{'fraction_non_alphanumeric': 0.11575178997613365, 'fraction_numerical': 0.0038451339167329623, 'mean_word_length': 3.1376851343938563, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7050167', 'n_tokens_mistral': 2433, 'n_tokens_neox': 2337, 'n_words': 489}