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