Use boost::signals2; fix bugs with x-thread signalling.
[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 boost::shared_ptr;
34
35 /** Must be called in the GUI thread */
36 JobManagerView::JobManagerView (wxWindow* parent)
37         : wxScrolledWindow (parent)
38 {
39         _panel = new wxPanel (this);
40         wxSizer* sizer = new wxBoxSizer (wxVERTICAL);
41         sizer->Add (_panel, 1, wxEXPAND);
42         SetSizer (sizer);
43         
44         _table = new wxFlexGridSizer (3, 6, 6);
45         _table->AddGrowableCol (1, 1);
46         _panel->SetSizer (_table);
47
48         SetScrollRate (0, 32);
49
50         Connect (wxID_ANY, wxEVT_TIMER, wxTimerEventHandler (JobManagerView::periodic), 0, this);
51         _timer.reset (new wxTimer (this));
52         _timer->Start (1000);
53
54         update ();
55 }
56
57 void
58 JobManagerView::periodic (wxTimerEvent &)
59 {
60         update ();
61 }
62
63 /** Update the view by examining the state of each job.
64  *  Must be called in the GUI thread.
65  */
66 void
67 JobManagerView::update ()
68 {
69         list<shared_ptr<Job> > jobs = JobManager::instance()->get ();
70
71         int index = 0;
72
73         for (list<shared_ptr<Job> >::iterator i = jobs.begin(); i != jobs.end(); ++i) {
74                 
75                 if (_job_records.find (*i) == _job_records.end ()) {
76                         wxStaticText* m = new wxStaticText (_panel, wxID_ANY, std_to_wx ((*i)->name ()));
77                         _table->Insert (index, m, 0, wxALIGN_CENTER_VERTICAL | wxALL, 6);
78                         
79                         JobRecord r;
80                         r.gauge = new wxGauge (_panel, wxID_ANY, 100);
81                         _table->Insert (index + 1, r.gauge, 1, wxEXPAND | wxLEFT | wxRIGHT);
82                         
83                         r.message = new wxStaticText (_panel, wxID_ANY, std_to_wx (""));
84                         _table->Insert (index + 2, r.message, 1, wxALIGN_CENTER_VERTICAL | wxALL, 6);
85                         
86                         _job_records[*i] = r;
87                 }
88
89                 string const st = (*i)->status ();
90
91                 if (!(*i)->finished ()) {
92                         float const p = (*i)->overall_progress ();
93                         if (p >= 0) {
94                                 _job_records[*i].message->SetLabel (std_to_wx (st));
95                                 _job_records[*i].gauge->SetValue (p * 100);
96                         } else {
97                                 _job_records[*i].message->SetLabel (wxT ("Running"));
98                                 _job_records[*i].gauge->Pulse ();
99                         }
100                 }
101                 
102                 if ((*i)->finished()) {
103                         _job_records[*i].gauge->SetValue (100);
104                         _job_records[*i].message->SetLabel (std_to_wx (st));
105                 }
106
107                 index += 3;
108         }
109
110         _table->Layout ();
111         FitInside ();
112 }