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