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