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