X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=src%2Fwx%2Faudio_mapping_view.cc;h=ad2722f81f3e84b90a053ddb7dd94b33f2410513;hb=eed0487aa9cd13faa45d8f79c777306fd85956b3;hp=0b28b12fb641201ca438c71fff61330951666f15;hpb=a79d78d8bb6d51f6662f1f63b9f8fd19e1a0c5f1;p=dcpomatic.git diff --git a/src/wx/audio_mapping_view.cc b/src/wx/audio_mapping_view.cc index 0b28b12fb..ad2722f81 100644 --- a/src/wx/audio_mapping_view.cc +++ b/src/wx/audio_mapping_view.cc @@ -1,349 +1,382 @@ /* - Copyright (C) 2013 Carl Hetherington + Copyright (C) 2013-2019 Carl Hetherington - This program is free software; you can redistribute it and/or modify + This file is part of DCP-o-matic. + + DCP-o-matic is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. - This program is distributed in the hope that it will be useful, + DCP-o-matic is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + along with DCP-o-matic. If not, see . */ -#include -#include -#include -#include -#include "lib/audio_mapping.h" -#include "lib/util.h" +/** @file src/wx/audio_mapping_view.cc + * @brief AudioMappingView class and helpers. + */ + #include "audio_mapping_view.h" #include "wx_util.h" #include "audio_gain_dialog.h" +#include "lib/audio_mapping.h" +#include "lib/util.h" +#include +#include +#include +#include +#include +#include +#include +#include using std::cout; using std::list; using std::string; +using std::min; using std::max; +using std::vector; +using std::pair; +using std::make_pair; using boost::shared_ptr; -using boost::lexical_cast; +using boost::optional; +using dcp::locale_convert; -#define INDICATOR_SIZE 20 +#define INDICATOR_SIZE 16 +#define GRID_SPACING 24 +#define LEFT_WIDTH (GRID_SPACING * 3) +#define TOP_HEIGHT (GRID_SPACING * 2) enum { ID_off = 1, ID_full = 2, - ID_minus3dB = 3, + ID_minus6dB = 3, ID_edit = 4 }; -class NoSelectionStringRenderer : public wxGridCellStringRenderer +AudioMappingView::AudioMappingView (wxWindow* parent) + : wxScrolledWindow (parent, wxID_ANY) + , _menu_input (0) + , _menu_output (1) { -public: - void Draw (wxGrid& grid, wxGridCellAttr& attr, wxDC& dc, const wxRect& rect, int row, int col, bool) - { - wxGridCellStringRenderer::Draw (grid, attr, dc, rect, row, col, false); - } -}; + _menu = new wxMenu; + _menu->Append (ID_off, _("Off")); + _menu->Append (ID_full, _("Full")); + _menu->Append (ID_minus6dB, _("-6dB")); + _menu->Append (ID_edit, _("Edit...")); -class ValueRenderer : public wxGridCellRenderer -{ -public: - - void Draw (wxGrid& grid, wxGridCellAttr &, wxDC& dc, const wxRect& rect, int row, int col, bool) - { - LocaleGuard lg; - - dc.SetPen (*wxThePenList->FindOrCreatePen (wxColour (255, 255, 255), 1, wxPENSTYLE_SOLID)); - dc.SetBrush (*wxTheBrushList->FindOrCreateBrush (wxColour (255, 255, 255), wxBRUSHSTYLE_SOLID)); - dc.DrawRectangle (rect); - - int const xo = (rect.GetWidth() - INDICATOR_SIZE) / 2; - int const yo = (rect.GetHeight() - INDICATOR_SIZE) / 2; - - dc.SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID)); - dc.SetBrush (*wxTheBrushList->FindOrCreateBrush (wxColour (255, 255, 255), wxBRUSHSTYLE_SOLID)); - dc.DrawRectangle (wxRect (rect.GetLeft() + xo, rect.GetTop() + yo, INDICATOR_SIZE, INDICATOR_SIZE)); - - float const value = lexical_cast (wx_to_std (grid.GetCellValue (row, col))); - float const value_dB = 20 * log10 (value); - int const range = 18; - int height = 0; - if (value_dB > -range) { - height = INDICATOR_SIZE * (1 + value_dB / range); - } + Bind (wxEVT_PAINT, boost::bind(&AudioMappingView::paint, this)); + Bind (wxEVT_MENU, boost::bind(&AudioMappingView::off, this), ID_off); + Bind (wxEVT_LEFT_DOWN, boost::bind(&AudioMappingView::left_down, this, _1)); + Bind (wxEVT_RIGHT_DOWN, boost::bind(&AudioMappingView::right_down, this, _1)); + Bind (wxEVT_MOTION, boost::bind(&AudioMappingView::motion, this, _1)); + Bind (wxEVT_MENU, boost::bind(&AudioMappingView::full, this), ID_full); + Bind (wxEVT_MENU, boost::bind(&AudioMappingView::minus6dB, this), ID_minus6dB); + Bind (wxEVT_MENU, boost::bind(&AudioMappingView::edit, this), ID_edit); - height = max (0, height); - - if (value > 0) { - /* Make sure we get a little bit of the marker if there is any gain */ - height = max (3, height); - } + SetScrollRate (GRID_SPACING, GRID_SPACING); +} - dc.SetBrush (*wxTheBrushList->FindOrCreateBrush (wxColour (0, 255, 0), wxBRUSHSTYLE_SOLID)); - dc.DrawRectangle (wxRect (rect.GetLeft() + xo, rect.GetTop() + yo + INDICATOR_SIZE - height, INDICATOR_SIZE, height)); - } +void +AudioMappingView::paint () +{ + wxPaintDC dc (this); - wxSize GetBestSize (wxGrid &, wxGridCellAttr &, wxDC &, int, int) - { - return wxSize (INDICATOR_SIZE + 4, INDICATOR_SIZE + 4); + wxGraphicsContext* gc = wxGraphicsContext::Create (dc); + if (!gc) { + return; } - - wxGridCellRenderer* Clone () const - { - return new ValueRenderer; + + int sx, sy; + GetViewStart (&sx, &sy); + gc->Translate (-sx * GRID_SPACING, -sy * GRID_SPACING); + dc.SetLogicalOrigin (sx * GRID_SPACING, sy * GRID_SPACING); + + int const output_channels_width = _output_channels.size() * GRID_SPACING; + int const input_channels_height = _input_channels.size() * GRID_SPACING; + + gc->SetAntialiasMode (wxANTIALIAS_DEFAULT); + wxGraphicsPath lines = gc->CreatePath (); + dc.SetFont (wxSWISS_FONT->Bold()); + wxCoord label_width; + wxCoord label_height; + + /* DCP label at the top */ + + dc.GetTextExtent (_("DCP"), &label_width, &label_height); + dc.DrawText (_("DCP"), LEFT_WIDTH + (output_channels_width - label_width) / 2, (GRID_SPACING - label_height) / 2); + + /* Content label on the left */ + + dc.GetTextExtent (_("Content"), &label_width, &label_height); + dc.DrawRotatedText ( + _("Content"), + (GRID_SPACING - label_height) / 2, + TOP_HEIGHT + (input_channels_height + label_width) / 2, + 90 + ); + + dc.SetFont (*wxSWISS_FONT); + gc->SetPen (*wxBLACK_PEN); + + /* Column labels and some lines */ + + int N = 0; + BOOST_FOREACH (string i, _output_channels) { + dc.GetTextExtent (std_to_wx(i), &label_width, &label_height); + dc.DrawText (std_to_wx(i), LEFT_WIDTH + GRID_SPACING * N + (GRID_SPACING - label_width) / 2, GRID_SPACING + (GRID_SPACING - label_height) / 2); + lines.MoveToPoint (LEFT_WIDTH + GRID_SPACING * N, GRID_SPACING); + lines.AddLineToPoint (LEFT_WIDTH + GRID_SPACING * N, TOP_HEIGHT + _input_channels.size() * GRID_SPACING); + ++N; } -}; + lines.MoveToPoint (LEFT_WIDTH + GRID_SPACING * N, GRID_SPACING); + lines.AddLineToPoint (LEFT_WIDTH + GRID_SPACING * N, TOP_HEIGHT + _input_channels.size() * GRID_SPACING); + /* Horizontal lines at the top */ -AudioMappingView::AudioMappingView (wxWindow* parent) - : wxPanel (parent, wxID_ANY) - , _menu_row (0) - , _menu_column (1) - , _last_tooltip_row (0) - , _last_tooltip_column (0) -{ - _grid = new wxGrid (this, wxID_ANY); + lines.MoveToPoint (LEFT_WIDTH, GRID_SPACING); + lines.AddLineToPoint (LEFT_WIDTH + output_channels_width, GRID_SPACING); + lines.MoveToPoint (LEFT_WIDTH, GRID_SPACING * 2); + lines.AddLineToPoint (LEFT_WIDTH + output_channels_width, GRID_SPACING * 2); - _grid->CreateGrid (0, MAX_AUDIO_CHANNELS + 1); - _grid->HideRowLabels (); - _grid->DisableDragRowSize (); - _grid->DisableDragColSize (); - _grid->EnableEditing (false); - _grid->SetCellHighlightPenWidth (0); - _grid->SetDefaultRenderer (new NoSelectionStringRenderer); + /* Row channel labels */ - set_column_labels (); + N = 0; + BOOST_FOREACH (string i, _input_channels) { + dc.GetTextExtent (std_to_wx(i), &label_width, &label_height); + dc.DrawText (std_to_wx(i), GRID_SPACING * 2 + (GRID_SPACING - label_width) / 2, TOP_HEIGHT + GRID_SPACING * N + (GRID_SPACING - label_height) / 2); + lines.MoveToPoint (GRID_SPACING * 2, TOP_HEIGHT + GRID_SPACING * N); + lines.AddLineToPoint (LEFT_WIDTH + output_channels_width, TOP_HEIGHT + GRID_SPACING * N); + ++N; + } + lines.MoveToPoint (GRID_SPACING * 2, TOP_HEIGHT + GRID_SPACING * N); + lines.AddLineToPoint (LEFT_WIDTH + output_channels_width, TOP_HEIGHT + GRID_SPACING * N); - _sizer = new wxBoxSizer (wxVERTICAL); - _sizer->Add (_grid, 1, wxEXPAND | wxALL); - SetSizerAndFit (_sizer); + /* Vertical lines on the left */ - Bind (wxEVT_GRID_CELL_LEFT_CLICK, boost::bind (&AudioMappingView::left_click, this, _1)); - Bind (wxEVT_GRID_CELL_RIGHT_CLICK, boost::bind (&AudioMappingView::right_click, this, _1)); - _grid->GetGridWindow()->Bind (wxEVT_MOTION, boost::bind (&AudioMappingView::mouse_moved, this, _1)); + for (int i = 1; i < 3; ++i) { + lines.MoveToPoint (GRID_SPACING * i, TOP_HEIGHT); + lines.AddLineToPoint (GRID_SPACING * i, TOP_HEIGHT + _input_channels.size() * GRID_SPACING); + } - _menu = new wxMenu; - _menu->Append (ID_off, _("Off")); - _menu->Append (ID_full, _("Full")); - _menu->Append (ID_minus3dB, _("-3dB")); - _menu->Append (ID_edit, _("Edit...")); + /* Group labels and lines */ + + int y = TOP_HEIGHT; + BOOST_FOREACH (Group i, _input_groups) { + dc.GetTextExtent (std_to_wx(i.name), &label_width, &label_height); + int const height = (i.to - i.from + 1) * GRID_SPACING; + dc.DrawRotatedText ( + std_to_wx(i.name), + GRID_SPACING + (GRID_SPACING - label_height) / 2, + y + (height + label_width) / 2, + 90 + ); + lines.MoveToPoint (GRID_SPACING, y); + lines.AddLineToPoint (GRID_SPACING * 2, y); + y += height; + } + + lines.MoveToPoint (GRID_SPACING, y); + lines.AddLineToPoint (GRID_SPACING * 2, y); + + gc->StrokePath (lines); + + /* Indicators */ + + for (size_t x = 0; x < _output_channels.size(); ++x) { + for (size_t y = 0; y < _input_channels.size(); ++y) { + dc.SetBrush (*wxWHITE_BRUSH); + dc.DrawRectangle ( + wxRect( + LEFT_WIDTH + x * GRID_SPACING + (GRID_SPACING - INDICATOR_SIZE) / 2, + TOP_HEIGHT + y * GRID_SPACING + (GRID_SPACING - INDICATOR_SIZE) / 2, + INDICATOR_SIZE, INDICATOR_SIZE + ) + ); + + float const value_dB = 20 * log10 (_map.get(y, x)); + int const range = 18; + int height = 0; + if (value_dB > -range) { + height = INDICATOR_SIZE * (1 + value_dB / range); + } + + dc.SetBrush (*wxTheBrushList->FindOrCreateBrush(wxColour (0, 255, 0), wxBRUSHSTYLE_SOLID)); + dc.DrawRectangle ( + wxRect( + LEFT_WIDTH + x * GRID_SPACING + (GRID_SPACING - INDICATOR_SIZE) / 2, + TOP_HEIGHT + y * GRID_SPACING + (GRID_SPACING - INDICATOR_SIZE) / 2 + INDICATOR_SIZE - height, + INDICATOR_SIZE, height + ) + ); + } + } - Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&AudioMappingView::off, this), ID_off); - Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&AudioMappingView::full, this), ID_full); - Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&AudioMappingView::minus3dB, this), ID_minus3dB); - Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&AudioMappingView::edit, this), ID_edit); + delete gc; } -void -AudioMappingView::map_changed () +optional > +AudioMappingView::mouse_event_to_channels (wxMouseEvent& ev) const { - update_cells (); - Changed (_map); - _last_tooltip_column = -1; -} + int sx, sy; + GetViewStart (&sx, &sy); + int const x = ev.GetX() + sx * GRID_SPACING; + int const y = ev.GetY() + sy * GRID_SPACING; + + if (x <= LEFT_WIDTH || y < TOP_HEIGHT) { + return optional >(); + } + + int const input = (y - TOP_HEIGHT) / GRID_SPACING; + int const output = (x - LEFT_WIDTH) / GRID_SPACING; + + if (input >= int(_input_channels.size()) || output >= int(_output_channels.size())) { + return optional >(); + } + + return make_pair (input, output); +} void -AudioMappingView::left_click (wxGridEvent& ev) +AudioMappingView::left_down (wxMouseEvent& ev) { - if (ev.GetCol() == 0) { + optional > channels = mouse_event_to_channels (ev); + if (!channels) { return; } - dcp::Channel d = static_cast (ev.GetCol() - 1); - - if (_map.get (ev.GetRow(), d) > 0) { - _map.set (ev.GetRow(), d, 0); + if (_map.get(channels->first, channels->second) > 0) { + _map.set (channels->first, channels->second, 0); } else { - _map.set (ev.GetRow(), d, 1); + _map.set (channels->first, channels->second, 1); } - map_changed (); + map_values_changed (); } void -AudioMappingView::right_click (wxGridEvent& ev) +AudioMappingView::right_down (wxMouseEvent& ev) { - if (ev.GetCol() == 0) { + optional > channels = mouse_event_to_channels (ev); + if (!channels) { return; } - _menu_row = ev.GetRow (); - _menu_column = ev.GetCol (); - PopupMenu (_menu, ev.GetPosition ()); + _menu_input = channels->first; + _menu_output = channels->second; + PopupMenu (_menu, ev.GetPosition()); +} + +/** Called when any gain value has changed */ +void +AudioMappingView::map_values_changed () +{ + Changed (_map); + _last_tooltip_channels = optional >(); + Refresh (); } void AudioMappingView::off () { - _map.set (_menu_row, static_cast (_menu_column - 1), 0); - map_changed (); + _map.set (_menu_input, _menu_output, 0); + map_values_changed (); } void AudioMappingView::full () { - _map.set (_menu_row, static_cast (_menu_column - 1), 1); - map_changed (); + _map.set (_menu_input, _menu_output, 1); + map_values_changed (); } void -AudioMappingView::minus3dB () +AudioMappingView::minus6dB () { - _map.set (_menu_row, static_cast (_menu_column - 1), 1 / sqrt (2)); - map_changed (); + _map.set (_menu_input, _menu_output, pow (10, -6.0 / 20)); + map_values_changed (); } void AudioMappingView::edit () { - dcp::Channel d = static_cast (_menu_column - 1); - - AudioGainDialog* dialog = new AudioGainDialog (this, _menu_row, _menu_column - 1, _map.get (_menu_row, d)); - if (dialog->ShowModal () == wxID_OK) { - _map.set (_menu_row, d, dialog->value ()); - map_changed (); + int const d = _menu_output - 1; + + AudioGainDialog* dialog = new AudioGainDialog (this, _menu_input, _menu_output - 1, _map.get(_menu_input, d)); + if (dialog->ShowModal() == wxID_OK) { + _map.set (_menu_input, d, dialog->value ()); + map_values_changed (); } - + dialog->Destroy (); } void -AudioMappingView::set (AudioMapping map) +AudioMappingView::set_virtual_size () { - _map = map; - update_cells (); + SetVirtualSize (LEFT_WIDTH + _output_channels.size() * GRID_SPACING, TOP_HEIGHT + _input_channels.size() * GRID_SPACING); } void -AudioMappingView::update_cells () +AudioMappingView::set (AudioMapping map) { - LocaleGuard lg; - - if (_grid->GetNumberRows ()) { - _grid->DeleteRows (0, _grid->GetNumberRows ()); - } - - _grid->InsertRows (0, _map.content_channels ()); - - for (int i = 0; i < _map.content_channels(); ++i) { - for (int j = 0; j < MAX_AUDIO_CHANNELS; ++j) { - _grid->SetCellRenderer (i, j + 1, new ValueRenderer); - } - } - - for (int i = 0; i < _map.content_channels(); ++i) { - _grid->SetCellValue (i, 0, wxString::Format (wxT("%d"), i + 1)); - - for (int j = 1; j < _grid->GetNumberCols(); ++j) { - _grid->SetCellValue (i, j, std_to_wx (lexical_cast (_map.get (i, static_cast (j - 1))))); - } - } - - _grid->AutoSize (); + _map = map; + Refresh (); } -/** @param c Number of DCP channels */ void -AudioMappingView::set_channels (int c) +AudioMappingView::set_input_channels (vector const & names) { - c++; - - if (c < _grid->GetNumberCols ()) { - _grid->DeleteCols (c, _grid->GetNumberCols() - c); - } else if (c > _grid->GetNumberCols ()) { - _grid->InsertCols (_grid->GetNumberCols(), c - _grid->GetNumberCols()); - set_column_labels (); - } - - update_cells (); + _input_channels = names; + set_virtual_size (); + Refresh (); } void -AudioMappingView::set_column_labels () +AudioMappingView::set_output_channels (vector const & names) { - int const c = _grid->GetNumberCols (); - - _grid->SetColLabelValue (0, _("Content channel")); - -#if MAX_AUDIO_CHANNELS != 8 -#warning AudioMappingView::set_column_labels() is expecting the wrong MAX_AUDIO_CHANNELS -#endif - - if (c > 0) { - _grid->SetColLabelValue (1, _("L")); - } - - if (c > 1) { - _grid->SetColLabelValue (2, _("R")); - } - - if (c > 2) { - _grid->SetColLabelValue (3, _("C")); - } - - if (c > 3) { - _grid->SetColLabelValue (4, _("Lfe")); - } - - if (c > 4) { - _grid->SetColLabelValue (5, _("Ls")); - } - - if (c > 5) { - _grid->SetColLabelValue (6, _("Rs")); - } - - if (c > 6) { - _grid->SetColLabelValue (7, _("HI")); - } - - if (c > 7) { - _grid->SetColLabelValue (8, _("VI")); - } - - _grid->AutoSize (); + _output_channels = names; + set_virtual_size (); + Refresh (); } void -AudioMappingView::mouse_moved (wxMouseEvent& ev) +AudioMappingView::motion (wxMouseEvent& ev) { - int xx; - int yy; - _grid->CalcUnscrolledPosition (ev.GetX(), ev.GetY(), &xx, &yy); - - int const row = _grid->YToRow (yy); - int const column = _grid->XToCol (xx); - - if (row < 0 || column < 1) { - _grid->GetGridWindow()->SetToolTip (""); - _last_tooltip_row = row; - _last_tooltip_column = column; + optional > channels = mouse_event_to_channels (ev); + if (!channels) { + SetToolTip (""); + _last_tooltip_channels = channels; + return; } - if (row != _last_tooltip_row || column != _last_tooltip_column) { - + if (channels != _last_tooltip_channels) { wxString s; - float const gain = _map.get (row, static_cast (column - 1)); + float const gain = _map.get(channels->first, channels->second); if (gain == 0) { - s = wxString::Format (_("No audio will be passed from content channel %d to DCP channel %d."), row + 1, column); + s = wxString::Format (_("No audio will be passed from content channel %d to DCP channel %d."), channels->first, channels->second); } else if (gain == 1) { - s = wxString::Format (_("Audio will be passed from content channel %d to DCP channel %d unaltered."), row + 1, column); + s = wxString::Format (_("Audio will be passed from content channel %d to DCP channel %d unaltered."), channels->first, channels->second); } else { float const dB = 20 * log10 (gain); - s = wxString::Format (_("Audio will be passed from content channel %d to DCP channel %d with gain %.1fdB."), row + 1, column, dB); + s = wxString::Format (_("Audio will be passed from content channel %d to DCP channel %d with gain %.1fdB."), channels->first, channels->second, dB); } - - _grid->GetGridWindow()->SetToolTip (s + " " + _("Right click to change gain.")); - _last_tooltip_row = row; - _last_tooltip_column = column; + + SetToolTip (s + " " + _("Right click to change gain.")); + _last_tooltip_channels = channels; } ev.Skip (); } + +void +AudioMappingView::set_input_groups (vector const & groups) +{ + _input_groups = groups; +}