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