6b341307d9e2fee6c8d928c9439569b2ec6f3e55
[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 <iostream>
35
36 using std::string;
37 using std::list;
38 using std::map;
39 using std::min;
40 using std::cout;
41 using std::shared_ptr;
42 using std::weak_ptr;
43 using boost::bind;
44 #if BOOST_VERSION >= 106100
45 using namespace boost::placeholders;
46 #endif
47
48 /** @param parent Parent window.
49  *  @param batch true to use BatchJobView, false to use NormalJobView.
50  *
51  *  Must be called in the GUI thread.
52  */
53 JobManagerView::JobManagerView (wxWindow* parent, bool batch)
54         : wxScrolledWindow (parent)
55         , _batch (batch)
56 {
57         _panel = new wxPanel (this);
58         wxSizer* sizer = new wxBoxSizer (wxVERTICAL);
59         sizer->Add (_panel, 1, wxEXPAND);
60         SetSizer (sizer);
61
62         _table = new wxFlexGridSizer (2, 6, 6);
63         _table->AddGrowableCol (0, 1);
64         _panel->SetSizer (_table);
65
66         SetScrollRate (0, 32);
67         EnableScrolling (false, true);
68
69         Bind (wxEVT_TIMER, boost::bind (&JobManagerView::periodic, this));
70         _timer.reset (new wxTimer (this));
71         _timer->Start (1000);
72
73         JobManager::instance()->JobAdded.connect (bind (&JobManagerView::job_added, this, _1));
74         JobManager::instance()->JobsReordered.connect (bind (&JobManagerView::replace, this));
75 }
76
77 void
78 JobManagerView::job_added (weak_ptr<Job> j)
79 {
80         shared_ptr<Job> job = j.lock ();
81         if (job) {
82                 shared_ptr<JobView> v;
83                 if (_batch) {
84                         v.reset (new BatchJobView (job, this, _panel, _table));
85                 } else {
86                         v.reset (new NormalJobView (job, this, _panel, _table));
87                 }
88                 v->setup ();
89                 _job_records.push_back (v);
90         }
91
92         FitInside();
93         job_list_changed ();
94 }
95
96 void
97 JobManagerView::periodic ()
98 {
99         for (list<shared_ptr<JobView> >::iterator i = _job_records.begin(); i != _job_records.end(); ++i) {
100                 (*i)->maybe_pulse ();
101         }
102 }
103
104 void
105 JobManagerView::replace ()
106 {
107         /* Make a new version of _job_records which reflects the order in JobManager's job list */
108
109         list<shared_ptr<JobView> > new_job_records;
110
111         for (auto i: JobManager::instance()->get()) {
112                 /* Find this job's JobView */
113                 for (auto j: _job_records) {
114                         if (j->job() == i) {
115                                 new_job_records.push_back (j);
116                                 break;
117                         }
118                 }
119         }
120
121         for (auto i: _job_records) {
122                 i->detach ();
123         }
124
125         _job_records = new_job_records;
126
127         for (auto i: _job_records) {
128                 i->insert (i->insert_position ());
129         }
130
131         job_list_changed ();
132 }
133
134 void
135 JobManagerView::job_list_changed ()
136 {
137         for (auto i: _job_records) {
138                 i->job_list_changed ();
139         }
140 }