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