Add FilmViewer::time_until_next_frame.
[dcpomatic.git] / src / wx / job_manager_view.cc
1 /*
2     Copyright (C) 2012-2017 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 /** @file src/job_manager_view.cc
22  *  @brief Class generating a GTK widget to show the progress of jobs.
23  */
24
25 #include "job_manager_view.h"
26 #include "batch_job_view.h"
27 #include "normal_job_view.h"
28 #include "wx_util.h"
29 #include "lib/job_manager.h"
30 #include "lib/job.h"
31 #include "lib/util.h"
32 #include "lib/exceptions.h"
33 #include "lib/compose.hpp"
34 #include <boost/foreach.hpp>
35 #include <iostream>
36
37 using std::string;
38 using std::list;
39 using std::map;
40 using std::min;
41 using std::cout;
42 using boost::shared_ptr;
43 using boost::weak_ptr;
44 using boost::bind;
45
46 /** @param parent Parent window.
47  *  @param batch true to use BatchJobView, false to use NormalJobView.
48  *
49  *  Must be called in the GUI thread.
50  */
51 JobManagerView::JobManagerView (wxWindow* parent, bool batch)
52         : wxScrolledWindow (parent)
53         , _batch (batch)
54 {
55         _panel = new wxPanel (this);
56         wxSizer* sizer = new wxBoxSizer (wxVERTICAL);
57         sizer->Add (_panel, 1, wxEXPAND);
58         SetSizer (sizer);
59
60         _table = new wxFlexGridSizer (2, 6, 6);
61         _table->AddGrowableCol (0, 1);
62         _panel->SetSizer (_table);
63
64         SetScrollRate (0, 32);
65         EnableScrolling (false, true);
66
67         Bind (wxEVT_TIMER, boost::bind (&JobManagerView::periodic, this));
68         _timer.reset (new wxTimer (this));
69         _timer->Start (1000);
70
71         JobManager::instance()->JobAdded.connect (bind (&JobManagerView::job_added, this, _1));
72         JobManager::instance()->JobsReordered.connect (bind (&JobManagerView::replace, this));
73 }
74
75 void
76 JobManagerView::job_added (weak_ptr<Job> j)
77 {
78         shared_ptr<Job> job = j.lock ();
79         if (job) {
80                 shared_ptr<JobView> v;
81                 if (_batch) {
82                         v.reset (new BatchJobView (job, this, _panel, _table));
83                 } else {
84                         v.reset (new NormalJobView (job, this, _panel, _table));
85                 }
86                 v->setup ();
87                 _job_records.push_back (v);
88         }
89
90         FitInside();
91         job_list_changed ();
92 }
93
94 void
95 JobManagerView::periodic ()
96 {
97         for (list<shared_ptr<JobView> >::iterator i = _job_records.begin(); i != _job_records.end(); ++i) {
98                 (*i)->maybe_pulse ();
99         }
100 }
101
102 void
103 JobManagerView::replace ()
104 {
105         /* Make a new version of _job_records which reflects the order in JobManager's job list */
106
107         list<shared_ptr<JobView> > new_job_records;
108
109         BOOST_FOREACH (shared_ptr<Job> i, JobManager::instance()->get()) {
110                 /* Find this job's JobView */
111                 BOOST_FOREACH (shared_ptr<JobView> j, _job_records) {
112                         if (j->job() == i) {
113                                 new_job_records.push_back (j);
114                                 break;
115                         }
116                 }
117         }
118
119         BOOST_FOREACH (shared_ptr<JobView> i, _job_records) {
120                 i->detach ();
121         }
122
123         _job_records = new_job_records;
124
125         BOOST_FOREACH (shared_ptr<JobView> i, _job_records) {
126                 i->insert (i->insert_position ());
127         }
128
129         job_list_changed ();
130 }
131
132 void
133 JobManagerView::job_list_changed ()
134 {
135         BOOST_FOREACH (shared_ptr<JobView> i, _job_records) {
136                 i->job_list_changed ();
137         }
138 }