6b0ed44104b655366ecc260f3395c5423f2583c1
[dcpomatic.git] / src / wx / video_waveform_dialog.cc
1 /*
2     Copyright (C) 2015-2018 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 #include "video_waveform_dialog.h"
22 #include "video_waveform_plot.h"
23 #include "film_viewer.h"
24 #include "wx_util.h"
25 #include <boost/bind.hpp>
26 #include <iostream>
27
28 using std::cout;
29 using boost::bind;
30 using boost::weak_ptr;
31 using boost::shared_ptr;
32
33 VideoWaveformDialog::VideoWaveformDialog (wxWindow* parent, weak_ptr<const Film> film, weak_ptr<FilmViewer> viewer)
34         : wxDialog (
35                 parent,
36                 wxID_ANY,
37                 _("Video Waveform"),
38                 wxDefaultPosition,
39                 wxSize (640, 512),
40 #ifdef DCPOMATIC_OSX
41                 /* I can't get wxFRAME_FLOAT_ON_PARENT to work on OS X, and although wxSTAY_ON_TOP keeps
42                    the window above all others (and not just our own) it's better than nothing for now.
43                 */
44                 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxSTAY_ON_TOP
45 #else
46                 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE | wxFRAME_FLOAT_ON_PARENT
47 #endif
48                 )
49         , _viewer (viewer)
50 {
51         wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
52
53         wxBoxSizer* controls = new wxBoxSizer (wxHORIZONTAL);
54
55         _component = new wxChoice (this, wxID_ANY);
56         _component->Append (wxT ("X"));
57         _component->Append (wxT ("Y"));
58         _component->Append (wxT ("Z"));
59         add_label_to_sizer (controls, this, _("Component"), true);
60         controls->Add (_component, 1, wxALL, DCPOMATIC_SIZER_X_GAP);
61
62         add_label_to_sizer (controls, this, _("Contrast"), true);
63         _contrast = new wxSlider (this, wxID_ANY, 0, 0, 256);
64         controls->Add (_contrast, 1, wxALL, DCPOMATIC_SIZER_X_GAP);
65
66         overall_sizer->Add (controls, 0, wxALL | wxEXPAND, DCPOMATIC_SIZER_X_GAP);
67
68         wxBoxSizer* position = new wxBoxSizer (wxHORIZONTAL);
69         add_label_to_sizer (position, this, _("Image X position"), true);
70         _x_position = new wxStaticText (this, wxID_ANY, "");
71         _x_position->SetMinSize (wxSize (64, -1));
72         position->Add (_x_position, 0, wxALL, DCPOMATIC_SIZER_X_GAP);
73         add_label_to_sizer (position, this, _("component value"), true);
74         _value = new wxStaticText (this, wxID_ANY, "");
75         _value->SetMinSize (wxSize (64, -1));
76         position->Add (_value, 0, wxALL, DCPOMATIC_SIZER_X_GAP);
77         overall_sizer->Add (position, 0, wxEXPAND | wxALL, DCPOMATIC_SIZER_Y_GAP);
78
79         _plot = new VideoWaveformPlot (this, film, _viewer);
80         overall_sizer->Add (_plot, 1, wxALL | wxEXPAND, 12);
81
82 #ifdef DCPOMATIC_LINUX
83         wxSizer* buttons = CreateSeparatedButtonSizer (wxCLOSE);
84         if (buttons) {
85                 overall_sizer->Add (buttons, wxSizerFlags().Expand().DoubleBorder());
86         }
87 #endif
88
89         SetSizer (overall_sizer);
90         overall_sizer->Layout ();
91         overall_sizer->SetSizeHints (this);
92
93         Bind (wxEVT_SHOW, bind (&VideoWaveformDialog::shown, this, _1));
94         _component->Bind (wxEVT_CHOICE, bind (&VideoWaveformDialog::component_changed, this));
95         _contrast->Bind (wxEVT_SCROLL_THUMBTRACK, bind (&VideoWaveformDialog::contrast_changed, this));
96         _plot->MouseMoved.connect (bind (&VideoWaveformDialog::mouse_moved, this, _1, _2, _3, _4));
97
98         _component->SetSelection (0);
99         _contrast->SetValue (32);
100
101         component_changed ();
102         contrast_changed ();
103 }
104
105 void
106 VideoWaveformDialog::shown (wxShowEvent& ev)
107 {
108         _plot->set_enabled (ev.IsShown ());
109         if (ev.IsShown ()) {
110                 shared_ptr<FilmViewer> fv = _viewer.lock ();
111                 DCPOMATIC_ASSERT (fv);
112                 fv->slow_refresh ();
113         }
114 }
115
116 void
117 VideoWaveformDialog::component_changed ()
118 {
119         _plot->set_component (_component->GetCurrentSelection ());
120 }
121
122 void
123 VideoWaveformDialog::contrast_changed ()
124 {
125         _plot->set_contrast (_contrast->GetValue ());
126 }
127
128 void
129 VideoWaveformDialog::mouse_moved (int x1, int x2, int y1, int y2)
130 {
131         if (x1 != x2) {
132                 _x_position->SetLabel (wxString::Format ("%d-%d", x1, x2));
133         } else {
134                 _x_position->SetLabel (wxString::Format ("%d", x1));
135         }
136
137         if (y1 != y2) {
138                 _value->SetLabel (wxString::Format ("%d-%d", y1, y2));
139         } else {
140                 _value->SetLabel (wxString::Format ("%d", y1));
141         }
142 }