Skip to content

Commit

Permalink
Extracted GOSettingsPorts from SettingsAudioOutput GrandOrgue#703
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleg Samarin committed Dec 4, 2021
1 parent 4ba136a commit 06bf535
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 89 deletions.
1 change: 1 addition & 0 deletions src/grandorgue/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ OrganDialog.cpp
GODocument.cpp
GOPanelView.cpp
settings/GOPortFactory.cpp
settings/GOSettingsPorts.cpp
settings/SettingsArchives.cpp
settings/SettingsAudioGroup.cpp
settings/SettingsAudioOutput.cpp
Expand Down
104 changes: 104 additions & 0 deletions src/grandorgue/settings/GOSettingsPorts.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright 2006 Milan Digital Audio LLC
* Copyright 2009-2021 GrandOrgue contributors (see AUTHORS)
* License GPL-2.0 or later (https://www.gnu.org/licenses/old-licenses/gpl-2.0.html).
*/

#include <thread>

#include "GOSettingsPorts.h"

wxString GOSettingsPorts::GetPortItemName(
const wxString &portName, const wxString &apiName
) const
{
wxString itemName;

if (m_PortFactory.IsToUsePortName())
itemName = portName;

if (! apiName.IsEmpty())
{
if (! itemName.IsEmpty())
itemName += ": ";
itemName += apiName;
}
return itemName;
}

void GOSettingsPorts::SetPortItemChecked(wxTreeListItem item, bool isChecked)
{
if (isChecked)
m_Ports->CheckItem(item);
else
m_Ports->UncheckItem(item);
}

bool GOSettingsPorts::GetPortItemChecked(
const wxString &portName, const wxString& apiName
) const
{
bool isChecked = true;
const wxString itemText = GetPortItemName(portName, apiName);

for (
wxTreeListItem item = m_Ports->GetFirstItem();
item.IsOk();
item = m_Ports->GetNextItem(item)
) if (m_Ports->GetItemText(item, 0) == itemText)
{
isChecked = m_Ports->GetCheckedState(item);
break;
}
return isChecked;
}

GOSettingsPorts::GOSettingsPorts(
wxWindow* parent, const GOPortFactory &portFactory, const wxString& name
):
m_PortFactory(portFactory)
{
m_PortsSizer = new wxStaticBoxSizer(wxVERTICAL, parent, name);
m_Ports = new wxTreeListCtrl(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTL_SINGLE | wxTL_CHECKBOX | wxTL_NO_HEADER);
m_Ports->AppendColumn(wxEmptyString);
m_PortsSizer->Add(m_Ports, 1, wxEXPAND | wxALIGN_LEFT);
}

void GOSettingsPorts::FillPortsWith(const GOPortsConfig& config)
{
m_PortsConfig = config;
m_Ports->DeleteAllItems();

for (const wxString &portName: m_PortFactory.GetPortNames())
{
const wxTreeListItem& rootItem = m_Ports->GetRootItem();
const wxTreeListItem& portItem
= m_PortFactory.IsToUsePortName()
? m_Ports->AppendItem(rootItem, GetPortItemName(portName))
: rootItem;

SetPortItemChecked(portItem, m_PortsConfig.IsConfigEnabled(portName));
for (const wxString &apiName: m_PortFactory.GetPortApiNames(portName))
{
const wxTreeListItem portApiItem
= m_Ports->AppendItem(portItem, GetPortItemName(portName, apiName));

SetPortItemChecked(
portApiItem, m_PortsConfig.IsConfigEnabled(portName, apiName)
);
}
m_Ports->Expand(portItem);
}
}

GOPortsConfig& GOSettingsPorts::RenewPortsConfig()
{
for (const wxString &portName: m_PortFactory.GetPortNames())
{
m_PortsConfig.SetConfigEnabled(portName, GetPortItemChecked(portName));
for (const wxString &apiName: m_PortFactory.GetPortApiNames(portName)) {
m_PortsConfig.SetConfigEnabled(portName, apiName, GetPortItemChecked(portName, apiName));
}
}
return m_PortsConfig;
}
43 changes: 43 additions & 0 deletions src/grandorgue/settings/GOSettingsPorts.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2006 Milan Digital Audio LLC
* Copyright 2009-2021 GrandOrgue contributors (see AUTHORS)
* License GPL-2.0 or later (https://www.gnu.org/licenses/old-licenses/gpl-2.0.html).
*/

#ifndef GOSETTINGSPORTS_H
#define GOSETTINGSPORTS_H

#include <vector>

#include <wx/sizer.h>
#include <wx/string.h>
#include <wx/treelist.h>

#include "GOPortFactory.h"
#include "GOPortsConfig.h"

class GOSettingsPorts
{
private:
const GOPortFactory& m_PortFactory;
GOPortsConfig m_PortsConfig;
wxSizer* m_PortsSizer;
wxTreeListCtrl* m_Ports;

wxString GetPortItemName(
const wxString &portName, const wxString &apiName = wxEmptyString
) const;
bool GetPortItemChecked(
const wxString &portName, const wxString& apiName = wxEmptyString
) const;
void SetPortItemChecked(wxTreeListItem item, bool isChecked);

public:
GOSettingsPorts(wxWindow* parent, const GOPortFactory& portFactory, const wxString& name);
wxSizer* GetPortsBox() { return m_PortsSizer; }

void FillPortsWith(const GOPortsConfig& config);
GOPortsConfig& RenewPortsConfig();
};

#endif /* GOSETTINGSPORTS_H */
85 changes: 7 additions & 78 deletions src/grandorgue/settings/SettingsAudioOutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,49 +83,12 @@ BEGIN_EVENT_TABLE(SettingsAudioOutput, wxPanel)
EVT_BUTTON(ID_OUTPUT_DEFAULT, SettingsAudioOutput::OnOutputDefault)
END_EVENT_TABLE()

wxString getPortItemName(const wxString &portName, const wxString &apiName = wxEmptyString)
{
wxString itemName = portName;

if (! apiName.IsEmpty())
itemName += ": " + apiName;
return itemName;
}

void SettingsAudioOutput::SetPortItemChecked(wxTreeListItem item, bool isChecked)
{
if (isChecked)
m_SoundPorts->CheckItem(item);
else
m_SoundPorts->UncheckItem(item);
}

bool SettingsAudioOutput::GetPortItemChecked(
const wxString &portName, const wxString& apiName
) const
{
bool isChecked = true;
const wxString itemText = getPortItemName(portName, apiName);

for (
wxTreeListItem item = m_SoundPorts->GetFirstItem();
item.IsOk();
item = m_SoundPorts->GetNextItem(item)
) if (m_SoundPorts->GetItemText(item, 0) == itemText)
{
isChecked = m_SoundPorts->GetCheckedState(item);
break;
}
return isChecked;
}


SettingsAudioOutput::SettingsAudioOutput(GOSound& sound, GOAudioGroupCallback& callback, wxWindow* parent) :
wxPanel(parent, wxID_ANY),
GOSettingsPorts(this, GOSoundPortFactory::getInstance(), _("Sound &ports")),
m_Sound(sound),
m_Settings(sound.GetSettings()),
m_GroupCallback(callback),
m_SoundPortsConfig(m_Settings.GetSoundPortsConfig())
m_GroupCallback(callback)
{
wxBoxSizer* const item0 = new wxBoxSizer(wxVERTICAL);
wxBoxSizer* item1 = new wxBoxSizer(wxHORIZONTAL);
Expand All @@ -152,28 +115,7 @@ SettingsAudioOutput::SettingsAudioOutput(GOSound& sound, GOAudioGroupCallback& c
item2->Add(grid, 0, wxEXPAND | wxALL, 5);
item1->Add(item2, 0, wxALL | wxALIGN_TOP, 5);

item2 = new wxStaticBoxSizer(wxVERTICAL, this, _("Sound &ports"));
m_SoundPorts = new wxTreeListCtrl(this, ID_SOND_PORTS, wxDefaultPosition, wxDefaultSize, wxTR_SINGLE | wxTL_CHECKBOX | wxTL_NO_HEADER);
m_SoundPorts->AppendColumn(wxEmptyString);
item2->Add(m_SoundPorts, 1, wxALIGN_LEFT | wxEXPAND);

for (const wxString &portName: GOSoundPortFactory::getInstance().GetPortNames())
{
const wxTreeListItem portItem = m_SoundPorts->AppendItem(
m_SoundPorts->GetRootItem(), getPortItemName(portName)
);
SetPortItemChecked(portItem, m_SoundPortsConfig.IsConfigEnabled(portName));
for (const wxString &apiName: GOSoundPortFactory::getInstance().GetPortApiNames(portName)) {
const wxTreeListItem portApiItem
= m_SoundPorts->AppendItem(portItem, getPortItemName(portName, apiName));

SetPortItemChecked(
portApiItem, m_SoundPortsConfig.IsConfigEnabled(portName, apiName)
);
}
m_SoundPorts->Expand(portItem);
}
item1->Add(item2, 1, wxEXPAND | wxALL, 5);
item1->Add(GetPortsBox(), 1, wxEXPAND | wxALL, 5);
item0->Add(item1, 1, wxEXPAND | wxALL, 5);

item2 = new wxStaticBoxSizer(wxVERTICAL, this, _("&Mapping output"));
Expand Down Expand Up @@ -232,23 +174,10 @@ SettingsAudioOutput::SettingsAudioOutput(GOSound& sound, GOAudioGroupCallback& c
this->SetSizer(item0);
item0->Fit(this);

FillPortsWith(sound.GetSettings().GetSoundPortsConfig());
UpdateButtons();
}

GOPortsConfig & SettingsAudioOutput::RenewSoundPortsConfig()

{
for (const wxString &portName: GOSoundPortFactory::getInstance().GetPortNames())
{
m_SoundPortsConfig.SetConfigEnabled(portName, GetPortItemChecked(portName));
for (const wxString &apiName: GOSoundPortFactory::getInstance().GetPortApiNames(portName)) {
m_SoundPortsConfig.SetConfigEnabled(portName, apiName, GetPortItemChecked(portName, apiName));
}
}
return m_SoundPortsConfig;
}


AudioItemData* SettingsAudioOutput::GetObject(const wxTreeItemId& id)
{
if (!id.IsOk())
Expand Down Expand Up @@ -312,7 +241,7 @@ wxTreeItemId SettingsAudioOutput::AddDeviceNode(wxString name, unsigned desired_
wxTreeItemId current;
if (name == wxEmptyString)
{
name = m_Sound.GetDefaultAudioDevice(RenewSoundPortsConfig());
name = m_Sound.GetDefaultAudioDevice(RenewPortsConfig());
}
current = GetDeviceNode(name);
if (current.IsOk())
Expand Down Expand Up @@ -369,7 +298,7 @@ void SettingsAudioOutput::UpdateVolume(const wxTreeItemId& group, float volume)
}

void SettingsAudioOutput::AssureDeviceList() {
const GOPortsConfig portsConfig(RenewSoundPortsConfig());
const GOPortsConfig portsConfig(RenewPortsConfig());

if (m_PortsConfigPopulatedWith != portsConfig) {
m_DeviceList = m_Sound.GetAudioDevices(portsConfig);
Expand Down Expand Up @@ -686,7 +615,7 @@ void SettingsAudioOutput::Save()
wxLogError(_("Invalid sample rate"));
m_Settings.SamplesPerBuffer(m_SamplesPerBuffer->GetValue());

m_Sound.GetSettings().SetSoundPortsConfig(RenewSoundPortsConfig());
m_Sound.GetSettings().SetSoundPortsConfig(RenewPortsConfig());

std::vector<GOAudioDeviceConfig> audio_config;
wxTreeItemId root = m_AudioOutput->GetRootItem();
Expand Down
15 changes: 4 additions & 11 deletions src/grandorgue/settings/SettingsAudioOutput.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@
#include <wx/spinctrl.h>
#include <wx/string.h>
#include <wx/treectrl.h>
#include <wx/treelist.h>

#include "sound/GOSoundDevInfo.h"
#include "GOPortsConfig.h"
#include "sound/ports/GOSoundPort.h"
#include "GOSettings.h"
#include "GOSettingsPorts.h"
#include "SettingsAudioGroup.h"

class AudioItemData;
class GOSound;

class SettingsAudioOutput : public wxPanel
class SettingsAudioOutput : public wxPanel, GOSettingsPorts
{
enum {
ID_OUTPUT_LIST = 200,
Expand All @@ -41,12 +42,10 @@ class SettingsAudioOutput : public wxPanel
GOSound& m_Sound;
GOSettings& m_Settings;
GOAudioGroupCallback& m_GroupCallback;
GOPortsConfig m_SoundPortsConfig;

wxChoice* m_SampleRate;
wxSpinCtrl* m_SamplesPerBuffer;

wxTreeListCtrl* m_SoundPorts;
wxTreeCtrl* m_AudioOutput;
wxButton* m_Add;
wxButton* m_Del;
Expand All @@ -57,12 +56,6 @@ class SettingsAudioOutput : public wxPanel
GOPortsConfig m_PortsConfigPopulatedWith;
std::vector<GOSoundDevInfo> m_DeviceList;

bool GetPortItemChecked(
const wxString &portName, const wxString& apiName = wxEmptyString
) const;
void SetPortItemChecked(wxTreeListItem item, bool isChecked);
GOPortsConfig & RenewSoundPortsConfig();

AudioItemData* GetObject(const wxTreeItemId& id);
wxTreeItemId GetDeviceNode(const wxString& name);
wxTreeItemId GetChannelNode(const wxTreeItemId& audio, unsigned channel);
Expand Down

0 comments on commit 06bf535

Please sign in to comment.