Merge master.
[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 boost::shared_ptr;
35
36 /** Must be called in the GUI thread */
37 JobManagerView::JobManagerView (wxWindow* parent, Buttons buttons)
38         : wxScrolledWindow (parent)
39         , _buttons (buttons)
40 {
41         _panel = new wxPanel (this);
42         wxSizer* sizer = new wxBoxSizer (wxVERTICAL);
43         sizer->Add (_panel, 1, wxEXPAND);
44         SetSizer (sizer);
45
46         int N = 5;
47         if (buttons & PAUSE) {
48                 ++N;
49         }
50         
51         _table = new wxFlexGridSizer (N, 6, 6);
52         _table->AddGrowableCol (1, 1);
53         _panel->SetSizer (_table);
54
55         SetScrollRate (0, 32);
56
57         Connect (wxID_ANY, wxEVT_TIMER, wxTimerEventHandler (JobManagerView::periodic), 0, this);
58         _timer.reset (new wxTimer (this));
59         _timer->Start (1000);
60
61         update ();
62 }
63
64 void
65 JobManagerView::periodic (wxTimerEvent &)
66 {
67         update ();
68 }
69
70 /** Update the view by examining the state of each job.
71  *  Must be called in the GUI thread.
72  */
73 void
74 JobManagerView::update ()
75 {
76         list<shared_ptr<Job> > jobs = JobManager::instance()->get ();
77
78         int index = 0;
79
80         for (list<shared_ptr<Job> >::iterator i = jobs.begin(); i != jobs.end(); ++i) {
81                 
82                 if (_job_records.find (*i) == _job_records.end ()) {
83                         wxStaticText* m = new wxStaticText (_panel, wxID_ANY, std_to_wx ((*i)->name ()));
84                         _table->Insert (index, m, 0, wxALIGN_CENTER_VERTICAL | wxALL, 6);
85                         
86                         JobRecord r;
87                         int n = 1;
88                         r.finalised = false;
89                         r.scroll_nudged = false;
90                         r.gauge = new wxGauge (_panel, wxID_ANY, 100);
91                         _table->Insert (index + n, r.gauge, 1, wxEXPAND | wxLEFT | wxRIGHT);
92                         ++n;
93                         
94                         r.message = new wxStaticText (_panel, wxID_ANY, std_to_wx (""));
95                         _table->Insert (index + n, r.message, 1, wxALIGN_CENTER_VERTICAL | wxALL, 6);
96                         ++n;
97
98                         r.cancel = new wxButton (_panel, wxID_ANY, _("Cancel"));
99                         r.cancel->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (JobManagerView::cancel_clicked), 0, this);
100                         _table->Insert (index + n, r.cancel, 1, wxALIGN_CENTER_VERTICAL | wxALL, 6);
101                         ++n;
102
103                         if (_buttons & PAUSE) {
104                                 r.pause = new wxButton (_panel, wxID_ANY, _("Pause"));
105                                 r.pause->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (JobManagerView::pause_clicked), 0, this);
106                                 _table->Insert (index + n, r.pause, 1, wxALIGN_CENTER_VERTICAL | wxALL, 6);
107                                 ++n;
108                         }
109                         
110                         r.details = new wxButton (_panel, wxID_ANY, _("Details..."));
111                         r.details->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (JobManagerView::details_clicked), 0, this);
112                         r.details->Enable (false);
113                         _table->Insert (index + n, r.details, 1, wxALIGN_CENTER_VERTICAL | wxALL, 6);
114                         ++n;
115                         
116                         _job_records[*i] = r;
117
118                 }
119
120                 string const st = (*i)->status ();
121
122                 if (!(*i)->finished ()) {
123                         float const p = (*i)->overall_progress ();
124                         if (p >= 0) {
125                                 checked_set (_job_records[*i].message, st);
126                                 _job_records[*i].gauge->SetValue (p * 100);
127                         } else {
128                                 checked_set (_job_records[*i].message, wx_to_std (_("Running")));
129                                 _job_records[*i].gauge->Pulse ();
130                         }
131
132                 }
133
134                 if (!_job_records[*i].scroll_nudged && ((*i)->running () || (*i)->finished())) {
135                         int x, y;
136                         _job_records[*i].gauge->GetPosition (&x, &y);
137                         int px, py;
138                         GetScrollPixelsPerUnit (&px, &py);
139                         int vx, vy;
140                         GetViewStart (&vx, &vy);
141                         int sx, sy;
142                         GetClientSize (&sx, &sy);
143
144                         if (y > (vy * py + sy / 2)) {
145                                 Scroll (-1, y / py);
146                                 _job_records[*i].scroll_nudged = true;
147                         }
148                 }
149                         
150                 if ((*i)->finished() && !_job_records[*i].finalised) {
151                         checked_set (_job_records[*i].message, st);
152                         if (!(*i)->finished_cancelled()) {
153                                 _job_records[*i].gauge->SetValue (100);
154                         }
155                         _job_records[*i].finalised = true;
156                         _job_records[*i].cancel->Enable (false);
157                         if (!(*i)->error_details().empty ()) {
158                                 _job_records[*i].details->Enable (true);
159                         }
160                 }
161
162                 index += 5;
163                 if (_buttons & PAUSE) {
164                         ++index;
165                 }
166         }
167
168         _table->Layout ();
169         FitInside ();
170 }
171
172 void
173 JobManagerView::details_clicked (wxCommandEvent& ev)
174 {
175         wxObject* o = ev.GetEventObject ();
176
177         for (map<shared_ptr<Job>, JobRecord>::iterator i = _job_records.begin(); i != _job_records.end(); ++i) {
178                 if (i->second.details == o) {
179                         string s = i->first->error_summary();
180                         s[0] = toupper (s[0]);
181                         error_dialog (this, std_to_wx (String::compose ("%1.\n\n%2", s, i->first->error_details())));
182                 }
183         }
184 }
185
186 void
187 JobManagerView::cancel_clicked (wxCommandEvent& ev)
188 {
189         wxObject* o = ev.GetEventObject ();
190
191         for (map<shared_ptr<Job>, JobRecord>::iterator i = _job_records.begin(); i != _job_records.end(); ++i) {
192                 if (i->second.cancel == o) {
193                         i->first->cancel ();
194                 }
195         }
196 }
197
198 void
199 JobManagerView::pause_clicked (wxCommandEvent& ev)
200 {
201         wxObject* o = ev.GetEventObject ();
202         for (map<boost::shared_ptr<Job>, JobRecord>::iterator i = _job_records.begin(); i != _job_records.end(); ++i) {
203                 if (i->second.pause == o) {
204                         if (i->first->paused()) {
205                                 i->first->resume ();
206                                 i->second.pause->SetLabel (_("Pause"));
207                         } else {
208                                 i->first->pause ();
209                                 i->second.pause->SetLabel (_("Resume"));
210                         }
211                 }
212         }
213 }
214