Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New class for camera selector button & use in Profile Wizard #1286

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,8 @@ set(phd2_SRC
${phd_src_dir}/calstep_dialog.h
${phd_src_dir}/camcal_import_dialog.cpp
${phd_src_dir}/camcal_import_dialog.h
${phd_src_dir}/camera_selector_button.cpp
${phd_src_dir}/camera_selector_button.h
${phd_src_dir}/circbuf.h

${phd_src_dir}/comet_tool.cpp
Expand Down
140 changes: 140 additions & 0 deletions src/camera_selector_button.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* camera_selector_button.cpp
* PHD Guiding
*
* Copyright (c) 2025 PHD2 Developers
* All rights reserved.
*
* This source code is distributed under the following "BSD" license
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of Bret McKee, Dad Dog Development, Ltd. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/

#include "camera_selector_button.h"
#include "icons/select.png.h"

wxDEFINE_EVENT(SELECT_CAMERA_EVENT, wxCommandEvent);

// clang-format off
wxBEGIN_EVENT_TABLE(CameraSelectorButton, wxBitmapButton)
EVT_BUTTON(GEAR_BUTTON_SELECT_CAMERA, CameraSelectorButton::OnButtonSelectCamera)
EVT_MENU_RANGE(MENU_SELECT_CAMERA_BEGIN, MENU_SELECT_CAMERA_END, CameraSelectorButton::OnMenuSelectCamera)
wxEND_EVENT_TABLE();
// clang-format on

CameraSelectorButton::CameraSelectorButton(wxWindow *pParent, wxWindowID id)
: wxBitmapButton(pParent, id, wxBitmap(wxBITMAP_PNG_FROM_DATA(select)))
{
SetToolTip(_("Select which camera to connect to when there are multiple cameras of the same type."));

m_pCamera = nullptr;
}

CameraSelectorButton::~CameraSelectorButton()
{
delete m_pCamera;
m_pCamera = nullptr;
}

void CameraSelectorButton::SetCamera(wxString cam)
{
delete m_pCamera;
m_pCamera = nullptr;

m_pCamera = GuideCamera::Factory(cam);
m_lastCamera = cam;
}

void CameraSelectorButton::OnButtonSelectCamera(wxCommandEvent& event)
{
if (!m_pCamera || !m_pCamera->CanSelectCamera())
return;

if (m_pCamera->HandleSelectCameraButtonClick(event))
return;

wxArrayString names;
m_cameraIds.clear(); // otherwise camera selection only works randomly as EnumCameras tends to append to the camera Ids
bool error = m_pCamera->EnumCameras(names, m_cameraIds);
if (error || names.size() == 0)
{
names.clear();
names.Add(_("No cameras found"));
m_cameraIds.clear();
}

wxString selectedId = SelectedCameraId(m_lastCamera);

wxMenu *menu = new wxMenu();
int id = MENU_SELECT_CAMERA_BEGIN;
for (unsigned int idx = 0; idx < names.size(); idx++)
{
wxMenuItem *item = menu->AppendRadioItem(id, names.Item(idx));
if (idx < m_cameraIds.size())
{
const wxString& camId = m_cameraIds[idx];
if (camId == selectedId || (idx == 0 && selectedId == GuideCamera::DEFAULT_CAMERA_ID))
item->Check(true);
}
if (++id > MENU_SELECT_CAMERA_END)
{
Debug.AddLine("Truncating camera list!");
break;
}
}

PopupMenu(menu, 0, this->GetSize().GetHeight());

delete menu;
}

void CameraSelectorButton::OnMenuSelectCamera(wxCommandEvent& event)
{
unsigned int idx = event.GetId() - MENU_SELECT_CAMERA_BEGIN;
if (idx < m_cameraIds.size())
{
wxString key = CameraSelectionKey(m_lastCamera);
const wxString& id = m_cameraIds[idx];
if (pConfig->Profile.GetString(key, wxEmptyString) != id)
{
pConfig->Profile.SetString(key, id);

wxCommandEvent event(SELECT_CAMERA_EVENT, idx + MENU_SELECT_CAMERA_BEGIN);
wxPostEvent(GetParent(), event);
}
}
}

wxString CameraSelectorButton::CameraSelectionKey(const wxString& camName)
{
std::hash<std::string> hash_fn;
std::string name(camName.c_str());
return wxString::Format("/cam_hash/%lx/whichCamera", (unsigned long) hash_fn(name));
}

wxString CameraSelectorButton::SelectedCameraId(const wxString& camName)
{
wxString key = CameraSelectionKey(camName);
return pConfig->Profile.GetString(key, GuideCamera::DEFAULT_CAMERA_ID);
}
60 changes: 60 additions & 0 deletions src/camera_selector_button.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* camera_selector_button.h
* PHD Guiding
*
* Copyright (c) 2025 PHD2 Developers
* All rights reserved.
*
* This source code is distributed under the following "BSD" license
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of Bret McKee, Dad Dog Development, Ltd. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/

#ifndef CAMERA_SELECTOR_BUTTON_H_INCLUDED
#define CAMERA_SELECTOR_BUTTON_H_INCLUDED

#include "phd.h"

wxDECLARE_EVENT(SELECT_CAMERA_EVENT, wxCommandEvent);

class CameraSelectorButton : public wxBitmapButton
{
public:
CameraSelectorButton(wxWindow *pParent, wxWindowID id);
~CameraSelectorButton();
void SetCamera(wxString cam);

static wxString CameraSelectionKey(const wxString& camName);
static wxString SelectedCameraId(const wxString& camName);

private:
GuideCamera *m_pCamera;
wxString m_lastCamera;
wxArrayString m_cameraIds;
void OnButtonSelectCamera(wxCommandEvent& event);
void OnMenuSelectCamera(wxCommandEvent& event);
wxDECLARE_EVENT_TABLE();
};

#endif // CAMERA_SELECTOR_BUTTON_H_INCLUDED
86 changes: 8 additions & 78 deletions src/gear_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ wxBEGIN_EVENT_TABLE(GearDialog, wxDialog)
EVT_BUTTON(GEAR_BUTTON_DISCONNECT_ALL, GearDialog::OnButtonDisconnectAll)

EVT_CHOICE(GEAR_CHOICE_CAMERA, GearDialog::OnChoiceCamera)
EVT_BUTTON(GEAR_BUTTON_SELECT_CAMERA, GearDialog::OnButtonSelectCamera)
EVT_MENU_RANGE(MENU_SELECT_CAMERA_BEGIN, MENU_SELECT_CAMERA_END, GearDialog::OnMenuSelectCamera)
EVT_COMMAND_RANGE(MENU_SELECT_CAMERA_BEGIN, MENU_SELECT_CAMERA_END, SELECT_CAMERA_EVENT, GearDialog::OnCommandSelectCamera)
EVT_BUTTON(GEAR_BUTTON_SETUP_CAMERA, GearDialog::OnButtonSetupCamera)
EVT_TOGGLEBUTTON(GEAR_BUTTON_CONNECT_CAMERA, GearDialog::OnButtonConnectCamera)
EVT_TOGGLEBUTTON(GEAR_BUTTON_DISCONNECT_CAMERA, GearDialog::OnButtonDisconnectCamera)
Expand Down Expand Up @@ -256,14 +255,10 @@ void GearDialog::Initialize()
_("Camera"));
m_gearSizer->Add(m_pCameras, wxGBPosition(0, 1), wxGBSpan(1, 1), wxALL | wxEXPAND | wxALIGN_CENTER_VERTICAL, 5);

#include "icons/select.png.h"
wxBitmap select_bmp(wxBITMAP_PNG_FROM_DATA(select));
#include "icons/setup.png.h"
wxBitmap setup_bmp(wxBITMAP_PNG_FROM_DATA(setup));

m_selectCameraButton = new wxBitmapButton(this, GEAR_BUTTON_SELECT_CAMERA, select_bmp);
m_selectCameraButton->SetToolTip(_("Select which camera to connect to when there are multiple cameras of the same type."));
m_selectCameraButton->Enable(false);
m_selectCameraButton = new CameraSelectorButton(this, GEAR_BUTTON_SELECT_CAMERA);
m_gearSizer->Add(m_selectCameraButton, wxGBPosition(0, 2), wxGBSpan(1, 1), wxALL | wxEXPAND | wxALIGN_CENTER_VERTICAL, 5);
m_pSetupCameraButton = new wxBitmapButton(this, GEAR_BUTTON_SETUP_CAMERA, setup_bmp);
m_pSetupCameraButton->SetToolTip(_("Camera Setup"));
Expand Down Expand Up @@ -952,7 +947,9 @@ void GearDialog::OnChoiceCamera(wxCommandEvent& event)
m_flushConfig = true;
}

m_selectCameraButton->SetCamera(choice);
m_selectCameraButton->Enable(m_pCamera && m_pCamera->CanSelectCamera());
m_lastCamera = choice;

if (!m_pCamera)
{
Expand Down Expand Up @@ -987,81 +984,14 @@ static void AutoLoadDarks()
}
}

static wxString CameraSelectionKey(const wxString& camName)
{
std::hash<std::string> hash_fn;
std::string name(camName.c_str());
return wxString::Format("/cam_hash/%lx/whichCamera", (unsigned long) hash_fn(name));
}

static wxString SelectedCameraId(const wxString& camName)
{
wxString key = CameraSelectionKey(camName);
return pConfig->Profile.GetString(key, GuideCamera::DEFAULT_CAMERA_ID);
}

wxString GearDialog::SelectedCameraId() const
{
return ::SelectedCameraId(m_lastCamera);
}

void GearDialog::OnButtonSelectCamera(wxCommandEvent& event)
{
if (!m_pCamera || !m_pCamera->CanSelectCamera())
return;

if (m_pCamera->HandleSelectCameraButtonClick(event))
return;

wxArrayString names;
m_cameraIds.clear(); // otherwise camera selection only works randomly as EnumCameras tends to append to the camera Ids
bool error = m_pCamera->EnumCameras(names, m_cameraIds);
if (error || names.size() == 0)
{
names.clear();
names.Add(_("No cameras found"));
m_cameraIds.clear();
}

wxString selectedId = ::SelectedCameraId(m_lastCamera);

wxMenu *menu = new wxMenu();
int id = MENU_SELECT_CAMERA_BEGIN;
for (unsigned int idx = 0; idx < names.size(); idx++)
{
wxMenuItem *item = menu->AppendRadioItem(id, names.Item(idx));
if (idx < m_cameraIds.size())
{
const wxString& camId = m_cameraIds[idx];
if (camId == selectedId || (idx == 0 && selectedId == GuideCamera::DEFAULT_CAMERA_ID))
item->Check(true);
}
if (++id > MENU_SELECT_CAMERA_END)
{
Debug.AddLine("Truncating camera list!");
break;
}
}

PopupMenu(menu, m_selectCameraButton->GetPosition().x,
m_selectCameraButton->GetPosition().y + m_selectCameraButton->GetSize().GetHeight());

delete menu;
return CameraSelectorButton::SelectedCameraId(m_lastCamera);
}

void GearDialog::OnMenuSelectCamera(wxCommandEvent& event)
void GearDialog::OnCommandSelectCamera(wxCommandEvent& event)
{
unsigned int idx = event.GetId() - MENU_SELECT_CAMERA_BEGIN;
if (idx < m_cameraIds.size())
{
wxString key = CameraSelectionKey(m_lastCamera);
const wxString& id = m_cameraIds[idx];
if (pConfig->Profile.GetString(key, wxEmptyString) != id)
{
pConfig->Profile.SetString(key, id);
m_flushConfig = true;
}
}
m_flushConfig = true;
}

void GearDialog::OnButtonSetupCamera(wxCommandEvent& event)
Expand Down Expand Up @@ -1096,7 +1026,7 @@ bool GearDialog::DoConnectCamera(bool autoReconnecting)

pFrame->StatusMsgNoTimeout(_("Connecting to Camera ..."));

wxString cameraId = ::SelectedCameraId(m_lastCamera);
wxString cameraId = SelectedCameraId();

Debug.Write(wxString::Format("Connecting to camera [%s] id = [%s]\n", newCam, cameraId));

Expand Down
7 changes: 4 additions & 3 deletions src/gear_dialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
#ifndef GEAR_DIALOG_H_INCLUDED
#define GEAR_DIALOG_H_INCLUDED

#include "camera_selector_button.h"

class wxGridBagSizer;

class GearDialog : public wxDialog
Expand Down Expand Up @@ -64,7 +66,7 @@ class GearDialog : public wxDialog
wxMenu *m_menuProfileManage;

wxChoice *m_pCameras;
wxButton *m_selectCameraButton;
CameraSelectorButton *m_selectCameraButton;
wxButton *m_pSetupCameraButton;
wxToggleButton *m_pConnectCameraButton;

Expand Down Expand Up @@ -139,8 +141,7 @@ class GearDialog : public wxDialog
void OnChar(wxKeyEvent& event);

void OnChoiceCamera(wxCommandEvent& event);
void OnButtonSelectCamera(wxCommandEvent& event);
void OnMenuSelectCamera(wxCommandEvent& evt);
void OnCommandSelectCamera(wxCommandEvent& event);

void OnButtonSetupCamera(wxCommandEvent& event);
bool DoConnectCamera(bool autoReconnecting);
Expand Down
Loading
Loading