Tweak group label formatting in AudioMappingView.
[dcpomatic.git] / src / wx / audio_mapping_view.cc
1 /*
2     Copyright (C) 2013-2015 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 /** @file  src/wx/audio_mapping_view.cc
22  *  @brief AudioMappingView class and helpers.
23  */
24
25 #include "lib/audio_mapping.h"
26 #include "lib/util.h"
27 #include "lib/raw_convert.h"
28 #include "audio_mapping_view.h"
29 #include "wx_util.h"
30 #include "audio_gain_dialog.h"
31 #include <dcp/types.h>
32 #include <wx/wx.h>
33 #include <wx/renderer.h>
34 #include <wx/grid.h>
35 #include <wx/graphics.h>
36 #include <boost/foreach.hpp>
37 #include <iostream>
38
39 using std::cout;
40 using std::list;
41 using std::string;
42 using std::min;
43 using std::max;
44 using std::vector;
45 using boost::shared_ptr;
46
47 #define INDICATOR_SIZE 16
48 #define LEFT_WIDTH 48
49
50 enum {
51         ID_off = 1,
52         ID_full = 2,
53         ID_minus6dB = 3,
54         ID_edit = 4
55 };
56
57 class NoSelectionStringRenderer : public wxGridCellStringRenderer
58 {
59 public:
60         void Draw (wxGrid& grid, wxGridCellAttr& attr, wxDC& dc, const wxRect& rect, int row, int col, bool)
61         {
62                 wxGridCellStringRenderer::Draw (grid, attr, dc, rect, row, col, false);
63         }
64 };
65
66 /** @class ValueRenderer
67  *  @brief wxGridCellRenderer for a gain value.
68  */
69 class ValueRenderer : public wxGridCellRenderer
70 {
71 public:
72
73         void Draw (wxGrid& grid, wxGridCellAttr &, wxDC& dc, const wxRect& rect, int row, int col, bool)
74         {
75                 dc.SetPen (*wxThePenList->FindOrCreatePen (wxColour (255, 255, 255), 1, wxPENSTYLE_SOLID));
76                 dc.SetBrush (*wxTheBrushList->FindOrCreateBrush (wxColour (255, 255, 255), wxBRUSHSTYLE_SOLID));
77                 dc.DrawRectangle (rect);
78
79                 int const xo = (rect.GetWidth() - INDICATOR_SIZE) / 2;
80                 int const yo = (rect.GetHeight() - INDICATOR_SIZE) / 2;
81
82                 dc.SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID));
83                 dc.SetBrush (*wxTheBrushList->FindOrCreateBrush (wxColour (255, 255, 255), wxBRUSHSTYLE_SOLID));
84                 dc.DrawRectangle (wxRect (rect.GetLeft() + xo, rect.GetTop() + yo, INDICATOR_SIZE, INDICATOR_SIZE));
85
86                 float const value = raw_convert<float> (wx_to_std (grid.GetCellValue (row, col)));
87                 float const value_dB = 20 * log10 (value);
88                 int const range = 18;
89                 int height = 0;
90                 if (value_dB > -range) {
91                         height = INDICATOR_SIZE * (1 + value_dB / range);
92                 }
93
94                 height = max (0, height);
95
96                 if (value > 0) {
97                         /* Make sure we get a little bit of the marker if there is any gain */
98                         height = max (3, height);
99                 }
100
101                 dc.SetBrush (*wxTheBrushList->FindOrCreateBrush (wxColour (0, 255, 0), wxBRUSHSTYLE_SOLID));
102                 dc.DrawRectangle (wxRect (rect.GetLeft() + xo, rect.GetTop() + yo + INDICATOR_SIZE - height, INDICATOR_SIZE, height));
103         }
104
105         wxSize GetBestSize (wxGrid &, wxGridCellAttr &, wxDC &, int, int)
106         {
107                 return wxSize (INDICATOR_SIZE + 4, INDICATOR_SIZE + 4);
108         }
109
110         wxGridCellRenderer* Clone () const
111         {
112                 return new ValueRenderer;
113         }
114 };
115
116
117 AudioMappingView::AudioMappingView (wxWindow* parent)
118         : wxPanel (parent, wxID_ANY)
119         , _menu_row (0)
120         , _menu_column (1)
121         , _last_tooltip_row (0)
122         , _last_tooltip_column (0)
123 {
124         _left_labels = new wxPanel (this, wxID_ANY);
125         _left_labels->Bind (wxEVT_PAINT, boost::bind (&AudioMappingView::paint_left_labels, this));
126         _top_labels = new wxPanel (this, wxID_ANY);
127         _top_labels->Bind (wxEVT_PAINT, boost::bind (&AudioMappingView::paint_top_labels, this));
128
129         _grid = new wxGrid (this, wxID_ANY);
130
131         _grid->CreateGrid (0, MAX_DCP_AUDIO_CHANNELS + 1);
132         _grid->HideRowLabels ();
133         _grid->DisableDragRowSize ();
134         _grid->DisableDragColSize ();
135         _grid->EnableEditing (false);
136         _grid->SetCellHighlightPenWidth (0);
137         _grid->SetDefaultRenderer (new NoSelectionStringRenderer);
138         _grid->AutoSize ();
139
140         wxSizer* vertical_sizer = new wxBoxSizer (wxVERTICAL);
141         vertical_sizer->Add (_top_labels);
142         wxSizer* horizontal_sizer = new wxBoxSizer (wxHORIZONTAL);
143         horizontal_sizer->Add (_left_labels);
144         horizontal_sizer->Add (_grid, 1, wxEXPAND | wxALL);
145         vertical_sizer->Add (horizontal_sizer);
146         SetSizerAndFit (vertical_sizer);
147
148         Bind (wxEVT_GRID_CELL_LEFT_CLICK, boost::bind (&AudioMappingView::left_click, this, _1));
149         Bind (wxEVT_GRID_CELL_RIGHT_CLICK, boost::bind (&AudioMappingView::right_click, this, _1));
150         _grid->GetGridWindow()->Bind (wxEVT_MOTION, boost::bind (&AudioMappingView::mouse_moved, this, _1));
151         Bind (wxEVT_SIZE, boost::bind (&AudioMappingView::sized, this, _1));
152
153         _menu = new wxMenu;
154         _menu->Append (ID_off, _("Off"));
155         _menu->Append (ID_full, _("Full"));
156         _menu->Append (ID_minus6dB, _("-6dB"));
157         _menu->Append (ID_edit, _("Edit..."));
158
159         Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&AudioMappingView::off, this), ID_off);
160         Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&AudioMappingView::full, this), ID_full);
161         Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&AudioMappingView::minus6dB, this), ID_minus6dB);
162         Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&AudioMappingView::edit, this), ID_edit);
163 }
164
165 /** Called when any gain value has changed */
166 void
167 AudioMappingView::map_values_changed ()
168 {
169         update_cells ();
170         Changed (_map);
171         _last_tooltip_column = -1;
172 }
173
174 void
175 AudioMappingView::left_click (wxGridEvent& ev)
176 {
177         if (ev.GetCol() == 0) {
178                 return;
179         }
180
181         int const d = ev.GetCol() - 1;
182
183         if (_map.get (ev.GetRow(), d) > 0) {
184                 _map.set (ev.GetRow(), d, 0);
185         } else {
186                 _map.set (ev.GetRow(), d, 1);
187         }
188
189         map_values_changed ();
190 }
191
192 void
193 AudioMappingView::right_click (wxGridEvent& ev)
194 {
195         if (ev.GetCol() == 0) {
196                 return;
197         }
198
199         _menu_row = ev.GetRow ();
200         _menu_column = ev.GetCol ();
201         PopupMenu (_menu, ev.GetPosition ());
202 }
203
204 void
205 AudioMappingView::off ()
206 {
207         _map.set (_menu_row, _menu_column - 1, 0);
208         map_values_changed ();
209 }
210
211 void
212 AudioMappingView::full ()
213 {
214         _map.set (_menu_row, _menu_column - 1, 1);
215         map_values_changed ();
216 }
217
218 void
219 AudioMappingView::minus6dB ()
220 {
221         _map.set (_menu_row, _menu_column - 1, pow (10, -6.0 / 20));
222         map_values_changed ();
223 }
224
225 void
226 AudioMappingView::edit ()
227 {
228         int const d = _menu_column - 1;
229
230         AudioGainDialog* dialog = new AudioGainDialog (this, _menu_row, _menu_column - 1, _map.get (_menu_row, d));
231         if (dialog->ShowModal () == wxID_OK) {
232                 _map.set (_menu_row, d, dialog->value ());
233                 map_values_changed ();
234         }
235
236         dialog->Destroy ();
237 }
238
239 void
240 AudioMappingView::set (AudioMapping map)
241 {
242         _map = map;
243         update_cells ();
244 }
245
246 void
247 AudioMappingView::set_input_channels (vector<string> const & names)
248 {
249         for (int i = 0; i < _grid->GetNumberRows(); ++i) {
250                 _grid->SetCellValue (i, 0, std_to_wx (names[i]));
251         }
252 }
253
254 void
255 AudioMappingView::set_output_channels (vector<string> const & names)
256 {
257         int const o = names.size() + 1;
258         if (o < _grid->GetNumberCols ()) {
259                 _grid->DeleteCols (o, _grid->GetNumberCols() - o);
260         } else if (o > _grid->GetNumberCols ()) {
261                 _grid->InsertCols (_grid->GetNumberCols(), o - _grid->GetNumberCols());
262         }
263
264         _grid->SetColLabelValue (0, wxT (""));
265
266         for (size_t i = 0; i < names.size(); ++i) {
267                 _grid->SetColLabelValue (i + 1, std_to_wx (names[i]));
268         }
269
270         update_cells ();
271 }
272
273 void
274 AudioMappingView::update_cells ()
275 {
276         vector<string> row_names;
277         for (int i = 0; i < _grid->GetNumberRows (); ++i) {
278                 row_names.push_back (wx_to_std (_grid->GetCellValue (i, 0)));
279         }
280
281         if (_grid->GetNumberRows ()) {
282                 _grid->DeleteRows (0, _grid->GetNumberRows ());
283         }
284
285         _grid->InsertRows (0, _map.input_channels ());
286
287         for (int i = 0; i < _map.input_channels(); ++i) {
288                 for (int j = 0; j < _map.output_channels(); ++j) {
289                         _grid->SetCellRenderer (i, j + 1, new ValueRenderer);
290                 }
291         }
292
293         for (int i = 0; i < _map.input_channels(); ++i) {
294                 if (i < int (row_names.size ())) {
295                         _grid->SetCellValue (i, 0, std_to_wx (row_names[i]));
296                 }
297                 for (int j = 1; j < _grid->GetNumberCols(); ++j) {
298                         _grid->SetCellValue (i, j, std_to_wx (raw_convert<string> (_map.get (i, j - 1))));
299                 }
300         }
301
302         _grid->AutoSize ();
303 }
304
305 void
306 AudioMappingView::mouse_moved (wxMouseEvent& ev)
307 {
308         int xx;
309         int yy;
310         _grid->CalcUnscrolledPosition (ev.GetX(), ev.GetY(), &xx, &yy);
311
312         int const row = _grid->YToRow (yy);
313         int const column = _grid->XToCol (xx);
314
315         if (row < 0 || column < 1) {
316                 _grid->GetGridWindow()->SetToolTip ("");
317                 _last_tooltip_row = row;
318                 _last_tooltip_column = column;
319         }
320
321         if (row != _last_tooltip_row || column != _last_tooltip_column) {
322
323                 wxString s;
324                 float const gain = _map.get (row, column - 1);
325                 if (gain == 0) {
326                         s = wxString::Format (_("No audio will be passed from content channel %d to DCP channel %d."), row + 1, column);
327                 } else if (gain == 1) {
328                         s = wxString::Format (_("Audio will be passed from content channel %d to DCP channel %d unaltered."), row + 1, column);
329                 } else {
330                         float const dB = 20 * log10 (gain);
331                         s = wxString::Format (_("Audio will be passed from content channel %d to DCP channel %d with gain %.1fdB."), row + 1, column, dB);
332                 }
333
334                 _grid->GetGridWindow()->SetToolTip (s + " " + _("Right click to change gain."));
335                 _last_tooltip_row = row;
336                 _last_tooltip_column = column;
337         }
338
339         ev.Skip ();
340 }
341
342 void
343 AudioMappingView::sized (wxSizeEvent& ev)
344 {
345         int const top_height = 24;
346
347         _grid->AutoSize ();
348         _left_labels->SetMinSize (wxSize (LEFT_WIDTH, _grid->GetSize().GetHeight()));
349         _top_labels->SetMinSize (wxSize (_grid->GetSize().GetWidth() + LEFT_WIDTH, top_height));
350         ev.Skip ();
351 }
352
353 void
354 AudioMappingView::paint_left_labels ()
355 {
356         wxPaintDC dc (_left_labels);
357
358         wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
359         if (!gc) {
360                 return;
361         }
362
363         wxSize const size = dc.GetSize();
364         int const half = size.GetWidth() / 2;
365
366         gc->SetPen (wxPen (wxColour (0, 0, 0)));
367         gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
368
369         if (_grid->GetNumberRows() > 0) {
370
371                 /* Draw a line at the top of the first group */
372                 int ypos = _grid->GetColLabelSize() - 1;
373                 wxGraphicsPath lines = gc->CreatePath();
374                 lines.MoveToPoint (half, ypos);
375                 lines.AddLineToPoint (size.GetWidth(), ypos);
376
377                 /* And the names of the groups and a line under each */
378                 BOOST_FOREACH (Group const & i, _input_groups) {
379                         int const old_ypos = ypos;
380                         ypos += (i.to - i.from + 1) * _grid->GetRowSize(0);
381
382                         dc.SetClippingRegion (0, old_ypos + 2, size.GetWidth(), ypos - 4);
383
384                         dc.SetFont (*wxSWISS_FONT);
385                         wxCoord label_width;
386                         wxCoord label_height;
387                         dc.GetTextExtent (std_to_wx (i.name), &label_width, &label_height);
388
389                         dc.DrawRotatedText (
390                                 i.name,
391                                 half + (half - label_height) / 2,
392                                 min (ypos, (ypos + old_ypos + label_width) / 2),
393                                 90
394                                 );
395
396                         dc.DestroyClippingRegion ();
397
398                         lines.MoveToPoint (half, ypos);
399                         lines.AddLineToPoint (size.GetWidth(), ypos);
400                 }
401
402                 gc->StrokePath (lines);
403         }
404
405         /* Overall label */
406         dc.SetFont (wxSWISS_FONT->Bold());
407         wxCoord overall_label_width;
408         wxCoord overall_label_height;
409         dc.GetTextExtent (_("Content"), &overall_label_width, &overall_label_height);
410         dc.DrawRotatedText (
411                 _("Content"),
412                 (half - overall_label_height) / 2,
413                 min (size.GetHeight(), (size.GetHeight() + _grid->GetColLabelSize() + overall_label_width) / 2),
414                 90
415                 );
416
417         delete gc;
418 }
419
420 void
421 AudioMappingView::paint_top_labels ()
422 {
423         wxPaintDC dc (_top_labels);
424         if (_grid->GetNumberCols() == 0) {
425                 return;
426         }
427
428         wxGraphicsContext* gc = wxGraphicsContext::Create (dc);
429         if (!gc) {
430                 return;
431         }
432
433         wxSize const size = dc.GetSize();
434
435         gc->SetAntialiasMode (wxANTIALIAS_DEFAULT);
436
437         dc.SetFont (wxSWISS_FONT->Bold());
438         wxCoord label_width;
439         wxCoord label_height;
440         dc.GetTextExtent (_("DCP"), &label_width, &label_height);
441
442         dc.DrawText (_("DCP"), (size.GetWidth() + _grid->GetColSize(0) + LEFT_WIDTH - label_width) / 2, (size.GetHeight() - label_height) / 2);
443
444         gc->SetPen (wxPen (wxColour (0, 0, 0)));
445         wxGraphicsPath lines = gc->CreatePath();
446         lines.MoveToPoint (LEFT_WIDTH + _grid->GetColSize(0) - 1, 0);
447         lines.AddLineToPoint (LEFT_WIDTH + _grid->GetColSize(0) - 1, size.GetHeight());
448         lines.MoveToPoint (size.GetWidth() - 1, 0);
449         lines.AddLineToPoint (size.GetWidth() - 1, size.GetHeight());
450         gc->StrokePath (lines);
451
452         delete gc;
453 }
454
455 void
456 AudioMappingView::set_input_groups (vector<Group> const & groups)
457 {
458         _input_groups = groups;
459 }