Hand-apply 111f02f4fc8ace359a16aea1c88c2821bf3dde31 from master; improve progress...
[dcpomatic.git] / src / wx / job_manager_view.cc
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 /** @file src/job_manager_view.cc
21  *  @brief Class generating a GTK widget to show the progress of jobs.
22  */
23
24 #include "lib/job_manager.h"
25 #include "lib/job.h"
26 #include "lib/util.h"
27 #include "lib/exceptions.h"
28 #include "job_manager_view.h"
29 #include "wx_util.h"
30
31 using std::string;
32 using std::list;
33 using std::map;
34 using std::min;
35 using std::cout;
36 using boost::shared_ptr;
37 using boost::weak_ptr;
38
39 class JobRecord
40 {
41 public:
42         JobRecord (shared_ptr<Job> job, wxScrolledWindow* window, wxPanel* panel, wxFlexGridSizer* table)
43                 : _job (job)
44                 , _window (window)
45                 , _panel (panel)
46                 , _table (table)
47         {
48                 int n = 0;
49                 
50                 _name = new wxStaticText (panel, wxID_ANY, "");
51                 string const jn = "<b>" + _job->name () + "</b>";
52                 _name->SetLabelMarkup (std_to_wx (jn));
53                 table->Insert (n, _name, 0, wxALIGN_CENTER_VERTICAL | wxALL, 6);
54                 ++n;
55         
56                 _gauge = new wxGauge (panel, wxID_ANY, 100);
57                 /* This seems to be required to allow the gauge to shrink under OS X */
58                 _gauge->SetMinSize (wxSize (0, -1));
59                 table->Insert (n, _gauge, 1, wxEXPAND | wxLEFT | wxRIGHT);
60                 ++n;
61                 
62                 _message = new wxStaticText (panel, wxID_ANY, std_to_wx (""));
63                 table->Insert (n, _message, 1, wxALIGN_CENTER_VERTICAL | wxALL, 6);
64                 ++n;
65         
66                 _cancel = new wxButton (panel, wxID_ANY, _("Cancel"));
67                 _cancel->Bind (wxEVT_COMMAND_BUTTON_CLICKED, &JobRecord::cancel_clicked, this);
68                 table->Insert (n, _cancel, 1, wxALIGN_CENTER_VERTICAL | wxALL, 6);
69                 ++n;
70         
71                 _pause = new wxButton (_panel, wxID_ANY, _("Pause"));
72                 _pause->Bind (wxEVT_COMMAND_BUTTON_CLICKED, &JobRecord::pause_clicked, this);
73                 table->Insert (n, _pause, 1, wxALIGN_CENTER_VERTICAL | wxALL, 6);
74                 ++n;
75         
76                 _details = new wxButton (_panel, wxID_ANY, _("Details..."));
77                 _details->Bind (wxEVT_COMMAND_BUTTON_CLICKED, &JobRecord::details_clicked, this);
78                 _details->Enable (false);
79                 table->Insert (n, _details, 1, wxALIGN_CENTER_VERTICAL | wxALL, 6);
80                 ++n;
81         
82                 job->Progress.connect (boost::bind (&JobRecord::progress, this));
83                 job->Finished.connect (boost::bind (&JobRecord::finished, this));
84         
85                 table->Layout ();
86                 panel->FitInside ();
87         }
88
89         void maybe_pulse ()
90         {
91                 if (_job->running() && !_job->progress ()) {
92                         _gauge->Pulse ();
93                 }
94         }
95
96 private:
97
98         void update_job_name ()
99         {
100                 string n = "<b>" + _job->name () + "</b>";
101                 if (!_job->sub_name().empty ()) {
102                         n += "\n" + _job->sub_name ();
103                 }
104                 
105                 if (n != _last_name) {
106                         _name->SetLabelMarkup (std_to_wx (n));
107                         _last_name = n;
108                 }
109         }
110
111         void progress ()
112         {
113                 checked_set (_message, _job->status ());
114                 update_job_name ();
115                 if (_job->progress ()) {
116                         _gauge->SetValue (min (100.0f, _job->progress().get() * 100));
117                 }
118                 _table->Layout ();
119                 _window->FitInside ();
120         }
121
122         void finished ()
123         {
124                 checked_set (_message, _job->status ());
125                 update_job_name ();
126                 
127                 if (!_job->finished_cancelled ()) {
128                         _gauge->SetValue (100);
129                 }
130                 
131                 _cancel->Enable (false);
132                 _pause->Enable (false);
133                 if (!_job->error_details().empty ()) {
134                         _details->Enable (true);
135                 }
136                 
137                 _table->Layout ();
138                 _window->FitInside ();
139         }
140
141         void details_clicked (wxCommandEvent &)
142         {
143                 string s = _job->error_summary();
144                 s[0] = toupper (s[0]);
145                 error_dialog (_window, std_to_wx (String::compose ("%1.\n\n%2", s, _job->error_details())));
146         }
147         
148         void cancel_clicked (wxCommandEvent &)
149         {
150                 _job->cancel ();
151         }
152
153         void pause_clicked (wxCommandEvent &)
154         {
155                 if (_job->paused()) {
156                         _job->resume ();
157                         _pause->SetLabel (_("Pause"));
158                 } else {
159                         _job->pause ();
160                         _pause->SetLabel (_("Resume"));
161                 }
162         }
163         
164         boost::shared_ptr<Job> _job;
165         wxScrolledWindow* _window;
166         wxPanel* _panel;
167         wxFlexGridSizer* _table;
168         wxStaticText* _name;
169         wxGauge* _gauge;
170         wxStaticText* _message;
171         wxButton* _cancel;
172         wxButton* _pause;
173         wxButton* _details;
174         std::string _last_name;
175 };
176
177 /** Must be called in the GUI thread */
178 JobManagerView::JobManagerView (wxWindow* parent)
179         : wxScrolledWindow (parent)
180 {
181         _panel = new wxPanel (this);
182         wxSizer* sizer = new wxBoxSizer (wxVERTICAL);
183         sizer->Add (_panel, 1, wxEXPAND);
184         SetSizer (sizer);
185
186         _table = new wxFlexGridSizer (6, 6, 6);
187         _table->AddGrowableCol (1, 1);
188         _panel->SetSizer (_table);
189
190         SetScrollRate (0, 32);
191         EnableScrolling (false, true);
192
193         Bind (wxEVT_TIMER, boost::bind (&JobManagerView::periodic, this));
194         _timer.reset (new wxTimer (this));
195         _timer->Start (1000);
196
197         Bind (wxEVT_SIZE, boost::bind (&JobManagerView::sized, this, _1));
198         JobManager::instance()->JobAdded.connect (bind (&JobManagerView::job_added, this, _1));
199 }
200
201 void
202 JobManagerView::sized (wxSizeEvent& ev)
203 {
204         _table->FitInside (_panel);
205         _table->Layout ();
206         ev.Skip ();
207 }
208
209 void
210 JobManagerView::job_added (weak_ptr<Job> j)
211 {
212         shared_ptr<Job> job = j.lock ();
213         if (job) {
214                 _job_records.push_back (shared_ptr<JobRecord> (new JobRecord (job, this, _panel, _table)));
215         }
216 }
217
218 void
219 JobManagerView::periodic ()
220 {
221         for (list<shared_ptr<JobRecord> >::iterator i = _job_records.begin(); i != _job_records.end(); ++i) {
222                 (*i)->maybe_pulse ();
223         }
224 }