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