Assorted C++11/formatting cleanups.
[dcpomatic.git] / src / wx / job_manager_view.cc
1 /*
2     Copyright (C) 2012-2021 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
22
23 /** @file src/job_manager_view.cc
24  *  @brief Class generating a GTK widget to show the progress of jobs.
25  */
26
27
28 #include "job_manager_view.h"
29 #include "batch_job_view.h"
30 #include "normal_job_view.h"
31 #include "wx_util.h"
32 #include "lib/job_manager.h"
33 #include "lib/job.h"
34 #include "lib/util.h"
35 #include "lib/exceptions.h"
36 #include "lib/compose.hpp"
37 #include <iostream>
38
39
40 using std::string;
41 using std::list;
42 using std::map;
43 using std::min;
44 using std::cout;
45 using std::shared_ptr;
46 using std::weak_ptr;
47 using boost::bind;
48 #if BOOST_VERSION >= 106100
49 using namespace boost::placeholders;
50 #endif
51
52
53 /** @param parent Parent window.
54  *  @param batch true to use BatchJobView, false to use NormalJobView.
55  *
56  *  Must be called in the GUI thread.
57  */
58 JobManagerView::JobManagerView (wxWindow* parent, bool batch)
59         : wxScrolledWindow (parent)
60         , _batch (batch)
61 {
62         _panel = new wxPanel (this);
63         auto sizer = new wxBoxSizer (wxVERTICAL);
64         sizer->Add (_panel, 1, wxEXPAND);
65         SetSizer (sizer);
66
67         _table = new wxFlexGridSizer (2, 6, 6);
68         _table->AddGrowableCol (0, 1);
69         _panel->SetSizer (_table);
70
71         SetScrollRate (0, 32);
72         EnableScrolling (false, true);
73
74         Bind (wxEVT_TIMER, boost::bind(&JobManagerView::periodic, this));
75         _timer.reset (new wxTimer (this));
76         _timer->Start (1000);
77
78         JobManager::instance()->JobAdded.connect (bind(&JobManagerView::job_added, this, _1));
79         JobManager::instance()->JobsReordered.connect (bind(&JobManagerView::replace, this));
80 }
81
82
83 void
84 JobManagerView::job_added (weak_ptr<Job> j)
85 {
86         auto job = j.lock ();
87         if (job) {
88                 shared_ptr<JobView> v;
89                 if (_batch) {
90                         v.reset (new BatchJobView(job, this, _panel, _table));
91                 } else {
92                         v.reset (new NormalJobView(job, this, _panel, _table));
93                 }
94                 v->setup ();
95                 _job_records.push_back (v);
96         }
97
98         FitInside();
99         job_list_changed ();
100 }
101
102
103 void
104 JobManagerView::periodic ()
105 {
106         for (auto i: _job_records) {
107                 i->maybe_pulse ();
108         }
109 }
110
111
112 void
113 JobManagerView::replace ()
114 {
115         /* Make a new version of _job_records which reflects the order in JobManager's job list */
116
117         list<shared_ptr<JobView>> new_job_records;
118
119         for (auto i: JobManager::instance()->get()) {
120                 /* Find this job's JobView */
121                 for (auto j: _job_records) {
122                         if (j->job() == i) {
123                                 new_job_records.push_back (j);
124                                 break;
125                         }
126                 }
127         }
128
129         for (auto i: _job_records) {
130                 i->detach ();
131         }
132
133         _job_records = new_job_records;
134
135         for (auto i: _job_records) {
136                 i->insert (i->insert_position ());
137         }
138
139         job_list_changed ();
140 }
141
142
143 void
144 JobManagerView::job_list_changed ()
145 {
146         for (auto i: _job_records) {
147                 i->job_list_changed ();
148         }
149 }