Supporters update.
[dcpomatic.git] / src / wx / hints_dialog.cc
1 /*
2     Copyright (C) 2012-2021 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
22 #include "hints_dialog.h"
23 #include "wx_util.h"
24 #include "static_text.h"
25 #include "check_box.h"
26 #include "lib/film.h"
27 #include "lib/hints.h"
28 #include "lib/config.h"
29 #include <dcp/warnings.h>
30 LIBDCP_DISABLE_WARNINGS
31 #include <wx/richtext/richtextctrl.h>
32 LIBDCP_ENABLE_WARNINGS
33
34
35 using std::max;
36 using std::vector;
37 using std::string;
38 using std::cout;
39 using std::shared_ptr;
40 using boost::optional;
41 using boost::bind;
42 using std::dynamic_pointer_cast;
43 #if BOOST_VERSION >= 106100
44 using namespace boost::placeholders;
45 #endif
46
47
48 HintsDialog::HintsDialog (wxWindow* parent, std::weak_ptr<Film> film, bool ok)
49         : wxDialog (parent, wxID_ANY, _("Hints"))
50         , _film (film)
51         , _hints (0)
52         , _finished (false)
53 {
54         auto sizer = new wxBoxSizer (wxVERTICAL);
55
56         _gauge = new wxGauge (this, wxID_ANY, 100);
57         sizer->Add (_gauge, 0, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
58         _gauge_message = new StaticText (this, wxT(""));
59         sizer->Add (_gauge_message, 0, wxALL | wxEXPAND, DCPOMATIC_SIZER_GAP);
60
61         _text = new wxRichTextCtrl (this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize (400, 300), wxRE_READONLY);
62         sizer->Add (_text, 1, wxEXPAND | wxALL, 6);
63
64         if (!ok) {
65                 auto b = new CheckBox(this, _("Don't show hints again"));
66                 sizer->Add (b, 0, wxALL, 6);
67                 b->bind(&HintsDialog::shut_up, this, _1);
68         }
69
70         auto buttons = CreateStdDialogButtonSizer (0);
71         sizer->Add (CreateSeparatedSizer(buttons), wxSizerFlags().Expand().DoubleBorder());
72         wxButton* default_button = nullptr;
73         if (ok) {
74                 default_button = new wxButton(this, wxID_OK);
75                 buttons->SetAffirmativeButton(default_button);
76         } else {
77                 default_button = new wxButton(this, wxID_OK, _("Make DCP"));
78                 buttons->SetAffirmativeButton(default_button);
79                 buttons->SetNegativeButton (new wxButton (this, wxID_CANCEL, _("Go back")));
80         }
81
82         buttons->Realize ();
83
84         default_button->SetFocus();
85
86         SetSizer (sizer);
87         sizer->Layout ();
88         sizer->SetSizeHints (this);
89
90         _text->GetCaret()->Hide ();
91
92         auto locked_film = _film.lock ();
93         if (locked_film) {
94                 _film_change_connection = locked_film->Change.connect (boost::bind (&HintsDialog::film_change, this, _1));
95                 _film_content_change_connection = locked_film->ContentChange.connect (boost::bind (&HintsDialog::film_content_change, this, _1));
96         }
97
98         film_change (ChangeType::DONE);
99 }
100
101
102 void
103 HintsDialog::film_change (ChangeType type)
104 {
105         if (type != ChangeType::DONE) {
106                 return;
107         }
108
109         _text->Clear ();
110         _current.clear ();
111
112         auto film = _film.lock ();
113         if (!film) {
114                 return;
115         }
116
117         _gauge->Show ();
118         _gauge_message->Show ();
119         Layout ();
120         _gauge->SetValue (0);
121         update ();
122         _finished = false;
123
124         _hints.reset (new Hints (_film));
125         _hints_hint_connection = _hints->Hint.connect(bind(&HintsDialog::hint, this, _1));
126         _hints_progress_connection = _hints->Progress.connect(bind(&HintsDialog::progress, this, _1));
127         _hints_pulse_connection = _hints->Pulse.connect(bind(&HintsDialog::pulse, this));
128         _hints_finished_connection = _hints->Finished.connect(bind(&HintsDialog::finished, this));
129         _hints->start ();
130 }
131
132 void
133 HintsDialog::film_content_change (ChangeType type)
134 {
135         film_change (type);
136 }
137
138 void
139 HintsDialog::update ()
140 {
141         _text->Clear ();
142         if (_current.empty ()) {
143                 if (_finished) {
144                         _text->WriteText (_("There are no hints: everything looks good!"));
145                 } else {
146                         _text->WriteText (_("There are no hints yet: project check in progress."));
147                 }
148         } else {
149                 _text->BeginStandardBullet (N_("standard/circle"), 1, 50);
150                 for (auto i: _current) {
151                         _text->WriteText (std_to_wx (i));
152                         _text->Newline ();
153                 }
154                 _text->EndSymbolBullet ();
155         }
156 }
157
158 void
159 HintsDialog::hint (string text)
160 {
161         _current.push_back (text);
162         update ();
163 }
164
165 void
166 HintsDialog::shut_up (wxCommandEvent& ev)
167 {
168         Config::instance()->set_show_hints_before_make_dcp (!ev.IsChecked());
169 }
170
171 void
172 HintsDialog::pulse ()
173 {
174         _gauge->Pulse ();
175 }
176
177 void
178 HintsDialog::finished ()
179 {
180         try {
181                 _hints->rethrow ();
182         } catch (std::exception& e) {
183                 error_dialog (this, wxString::Format(_("A problem occurred when looking for hints (%s)"), std_to_wx(e.what())));
184         }
185
186         _finished = true;
187         update ();
188         _gauge->Hide ();
189         _gauge_message->Hide ();
190         Layout ();
191 }
192
193 void
194 HintsDialog::progress (string m)
195 {
196         _gauge_message->SetLabel (std_to_wx(m));
197 }