dfe496e58000de36b361f6c6a9e59d008c1e422a
[dcpomatic.git] / src / wx / verify_dcp_progress_dialog.cc
1 /*
2     Copyright (C) 2020 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 "verify_dcp_progress_dialog.h"
23 #include "wx_util.h"
24 #include "lib/cross.h"
25 #include "lib/job.h"
26 #include "lib/job_manager.h"
27 #include <wx/evtloop.h>
28 #include <boost/shared_ptr.hpp>
29 #include <string>
30
31
32 using std::string;
33 using boost::optional;
34 using boost::shared_ptr;
35
36
37 static int const max_file_name_length = 80;
38
39
40 VerifyDCPProgressDialog::VerifyDCPProgressDialog (wxWindow* parent, wxString title)
41         : wxDialog (parent, wxID_ANY, title)
42         , _cancel (false)
43 {
44         wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
45
46         _job_name = new wxStaticText (this, wxID_ANY, wxT(""));
47         overall_sizer->Add (_job_name, 0, wxEXPAND | wxTOP | wxLEFT | wxRIGHT, DCPOMATIC_SIZER_GAP);
48
49         _file_name = new wxStaticText (this, wxID_ANY, wxT(""));
50         wxFont file_name_font (*wxNORMAL_FONT);
51         file_name_font.SetFamily (wxFONTFAMILY_MODERN);
52         file_name_font.SetPointSize (file_name_font.GetPointSize() - 2);
53         _file_name->SetFont (file_name_font);
54
55         int w;
56         int h;
57         _file_name->GetTextExtent (std_to_wx(string(max_file_name_length, 'X')), &w, &h);
58         _file_name->SetMinSize (wxSize(w, -1));
59
60         overall_sizer->Add (_file_name, 0, wxEXPAND | wxLEFT | wxRIGHT | wxTOP, DCPOMATIC_SIZER_GAP);
61
62         _progress = new wxGauge (this, wxID_ANY, 100);
63         overall_sizer->Add (_progress, 0, wxEXPAND | wxALL, DCPOMATIC_SIZER_GAP);
64
65         wxButton* cancel = new wxButton (this, wxID_ANY, _("Cancel"));
66         wxSizer* buttons = new wxBoxSizer (wxHORIZONTAL);
67         buttons->AddStretchSpacer ();
68         buttons->Add (cancel, 0);
69         overall_sizer->Add (buttons, 0, wxEXPAND | wxALL, DCPOMATIC_SIZER_GAP);
70
71         SetSizerAndFit (overall_sizer);
72
73         cancel->Bind (wxEVT_BUTTON, boost::bind(&VerifyDCPProgressDialog::cancel, this));
74 }
75
76
77 void
78 VerifyDCPProgressDialog::cancel ()
79 {
80         _cancel = true;
81 }
82
83
84 bool
85 VerifyDCPProgressDialog::run (shared_ptr<Job> job)
86 {
87         Show ();
88
89         JobManager* jm = JobManager::instance ();
90         jm->add (job);
91
92         while (jm->work_to_do()) {
93                 wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI | wxEVT_CATEGORY_USER_INPUT);
94                 dcpomatic_sleep_seconds (1);
95                 optional<float> const progress = job->progress ();
96                 if (progress) {
97                         _progress->SetValue (*progress * 100);
98                 } else {
99                         _progress->Pulse ();
100                 }
101                 string const sub = job->sub_name ();
102                 size_t colon = sub.find (":");
103                 if (colon != string::npos) {
104                         _job_name->SetLabel (std_to_wx(sub.substr(0, colon)));
105                         string file_name;
106                         if ((sub.length() - colon - 1) > max_file_name_length) {
107                                 file_name = "..." + sub.substr(sub.length() - max_file_name_length + 3);
108                         } else {
109                                 file_name = sub.substr(colon + 1);
110                         }
111                         _file_name->SetLabel (std_to_wx(file_name));
112                 } else {
113                         _job_name->SetLabel (std_to_wx(sub));
114                 }
115
116                 if (_cancel) {
117                         break;
118                 }
119         }
120
121         return !_cancel;
122 }