Merge branch '1.0' into 1.0-multiple-selection
[dcpomatic.git] / src / wx / job_manager_view.cc
1 /*
2     Copyright (C) 2012 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, bool pause)
43                 : _job (job)
44                 , _window (window)
45                 , _panel (panel)
46                 , _table (table)
47                 , _pause (0)
48         {
49                 int n = 0;
50                 
51                 _name = new wxStaticText (panel, wxID_ANY, "");
52                 _name->SetLabelMarkup ("<b>" + _job->name () + "</b>");
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                 if (pause) {
72                         _pause = new wxButton (_panel, wxID_ANY, _("Pause"));
73                         _pause->Bind (wxEVT_COMMAND_BUTTON_CLICKED, &JobRecord::pause_clicked, this);
74                         table->Insert (n, _pause, 1, wxALIGN_CENTER_VERTICAL | wxALL, 6);
75                         ++n;
76                 }
77         
78                 _details = new wxButton (_panel, wxID_ANY, _("Details..."));
79                 _details->Bind (wxEVT_COMMAND_BUTTON_CLICKED, &JobRecord::details_clicked, this);
80                 _details->Enable (false);
81                 table->Insert (n, _details, 1, wxALIGN_CENTER_VERTICAL | wxALL, 6);
82                 ++n;
83         
84                 job->Progress.connect (boost::bind (&JobRecord::progress, this));
85                 job->Finished.connect (boost::bind (&JobRecord::finished, this));
86         
87                 table->Layout ();
88                 panel->FitInside ();
89         }
90
91         void maybe_pulse ()
92         {
93                 if (_job->running() && _job->progress_unknown ()) {
94                         _gauge->Pulse ();
95                 }
96         }
97
98 private:
99
100         void update_job_name ()
101         {
102                 string n = "<b>" + _job->name () + "</b>";
103                 if (!_job->sub_name().empty ()) {
104                         n += "\n" + _job->sub_name ();
105                 }
106                 
107                 if (n != _last_name) {
108                         _name->SetLabelMarkup (std_to_wx (n));
109                         _last_name = n;
110                 }
111         }
112
113         void progress ()
114         {
115                 float const p = _job->progress ();
116                 if (p >= 0) {
117                         checked_set (_message, _job->status ());
118                         update_job_name ();
119                         int const pp = min (100.0f, p * 100);
120                         _gauge->SetValue (pp);
121                 }
122
123                 _table->Layout ();
124                 _window->FitInside ();
125         }
126
127         void finished ()
128         {
129                 checked_set (_message, _job->status ());
130                 update_job_name ();
131                 
132                 if (!_job->finished_cancelled ()) {
133                         _gauge->SetValue (100);
134                 }
135                 
136                 _cancel->Enable (false);
137                 if (_pause) {
138                         _pause->Enable (false);
139                 }
140                 if (!_job->error_details().empty ()) {
141                         _details->Enable (true);
142                 }
143                 
144                 _table->Layout ();
145                 _window->FitInside ();
146         }
147
148         void details_clicked (wxCommandEvent &)
149         {
150                 string s = _job->error_summary();
151                 s[0] = toupper (s[0]);
152                 error_dialog (_window, std_to_wx (String::compose ("%1.\n\n%2", s, _job->error_details())));
153         }
154         
155         void cancel_clicked (wxCommandEvent &)
156         {
157                 _job->cancel ();
158         }
159
160         void pause_clicked (wxCommandEvent &)
161         {
162                 if (_job->paused()) {
163                         _job->resume ();
164                         _pause->SetLabel (_("Pause"));
165                 } else {
166                         _job->pause ();
167                         _pause->SetLabel (_("Resume"));
168                 }
169         }
170         
171         boost::shared_ptr<Job> _job;
172         wxScrolledWindow* _window;
173         wxPanel* _panel;
174         wxFlexGridSizer* _table;
175         wxStaticText* _name;
176         wxGauge* _gauge;
177         wxStaticText* _message;
178         wxButton* _cancel;
179         wxButton* _pause;
180         wxButton* _details;
181         std::string _last_name;
182 };
183
184 /** Must be called in the GUI thread */
185 JobManagerView::JobManagerView (wxWindow* parent, Buttons buttons)
186         : wxScrolledWindow (parent)
187         , _buttons (buttons)
188 {
189         _panel = new wxPanel (this);
190         wxSizer* sizer = new wxBoxSizer (wxVERTICAL);
191         sizer->Add (_panel, 1, wxEXPAND);
192         SetSizer (sizer);
193
194         int N = 5;
195         if (buttons & PAUSE) {
196                 ++N;
197         }
198         
199         _table = new wxFlexGridSizer (N, 6, 6);
200         _table->AddGrowableCol (1, 1);
201         _panel->SetSizer (_table);
202
203         SetScrollRate (0, 32);
204
205         Bind (wxEVT_TIMER, boost::bind (&JobManagerView::periodic, this));
206         _timer.reset (new wxTimer (this));
207         _timer->Start (1000);
208         
209         JobManager::instance()->JobAdded.connect (bind (&JobManagerView::job_added, this, _1));
210 }
211
212 void
213 JobManagerView::job_added (weak_ptr<Job> j)
214 {
215         shared_ptr<Job> job = j.lock ();
216         if (job) {
217                 _job_records.push_back (shared_ptr<JobRecord> (new JobRecord (job, this, _panel, _table, _buttons & PAUSE)));
218         }
219 }
220
221 void
222 JobManagerView::periodic ()
223 {
224         for (list<shared_ptr<JobRecord> >::iterator i = _job_records.begin(); i != _job_records.end(); ++i) {
225                 (*i)->maybe_pulse ();
226         }
227 }