Fix accidental breakage.
[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 namespace std;
32 using namespace boost;
33
34 /** Must be called in the GUI thread */
35 JobManagerView::JobManagerView (wxWindow* parent)
36         : wxScrolledWindow (parent)
37 {
38         _panel = new wxPanel (this);
39         wxSizer* sizer = new wxBoxSizer (wxVERTICAL);
40         sizer->Add (_panel, 1, wxEXPAND);
41         SetSizer (sizer);
42         
43         _table = new wxFlexGridSizer (3, 6, 6);
44         _table->AddGrowableCol (1, 1);
45         _panel->SetSizer (_table);
46
47         SetScrollRate (0, 32);
48
49         Connect (wxID_ANY, wxEVT_TIMER, wxTimerEventHandler (JobManagerView::periodic), 0, this);
50         _timer.reset (new wxTimer (this));
51         _timer->Start (1000);
52
53         update ();
54 }
55
56 void
57 JobManagerView::periodic (wxTimerEvent &)
58 {
59         update ();
60 }
61
62 /** Update the view by examining the state of each job.
63  *  Must be called in the GUI thread.
64  */
65 void
66 JobManagerView::update ()
67 {
68         list<shared_ptr<Job> > jobs = JobManager::instance()->get ();
69
70         for (list<shared_ptr<Job> >::iterator i = jobs.begin(); i != jobs.end(); ++i) {
71                 
72                 if (_job_records.find (*i) == _job_records.end ()) {
73                         add_label_to_sizer (_table, _panel, (*i)->name ());
74                         JobRecord r;
75                         r.gauge = new wxGauge (_panel, wxID_ANY, 100);
76                         _table->Add (r.gauge, 1, wxEXPAND | wxLEFT | wxRIGHT);
77                         r.informed_of_finish = false;
78                         r.message = add_label_to_sizer (_table, _panel, "", 1);
79                         
80                         _job_records[*i] = r;
81                 }
82
83                 string const st = (*i)->status ();
84
85                 if (!(*i)->finished ()) {
86                         float const p = (*i)->overall_progress ();
87                         if (p >= 0) {
88                                 _job_records[*i].message->SetLabel (std_to_wx (st));
89                                 _job_records[*i].gauge->SetValue (p * 100);
90                         } else {
91                                 _job_records[*i].message->SetLabel (wxT ("Running"));
92                                 _job_records[*i].gauge->Pulse ();
93                         }
94                 }
95                 
96                 /* Hack to work around our lack of cross-thread
97                    signalling; we tell the job to emit_finished()
98                    from here (the GUI thread).
99                 */
100                 
101                 if ((*i)->finished () && !_job_records[*i].informed_of_finish) {
102                         _job_records[*i].gauge->SetValue (100);
103                         _job_records[*i].message->SetLabel (std_to_wx (st));
104
105                         try {
106                                 (*i)->emit_finished ();
107                         } catch (OpenFileError& e) {
108                                 stringstream s;
109                                 s << "Error: " << e.what();
110                                 error_dialog (this, s.str ());
111                         }
112                         
113                         _job_records[*i].informed_of_finish = true;
114                 }
115         }
116
117         _table->Layout ();
118         FitInside ();
119 }