2dfb4113168259ac9a9181fba79db0f9bf206fd5
[dcpomatic.git] / src / wx / job_manager_view.cc
1 /*
2     Copyright (C) 2012-2015 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 "job_manager_view.h"
25 #include "wx_util.h"
26 #include "lib/job_manager.h"
27 #include "lib/job.h"
28 #include "lib/util.h"
29 #include "lib/exceptions.h"
30 #include "lib/compose.hpp"
31
32 using std::string;
33 using std::list;
34 using std::map;
35 using std::min;
36 using std::cout;
37 using boost::shared_ptr;
38 using boost::weak_ptr;
39
40 class JobRecord : public boost::noncopyable
41 {
42 public:
43         JobRecord (shared_ptr<Job> job, wxScrolledWindow* window, wxPanel* panel, wxFlexGridSizer* table)
44                 : _job (job)
45                 , _window (window)
46                 , _panel (panel)
47         {
48                 int n = 0;
49
50                 wxBoxSizer* gauge_message = new wxBoxSizer (wxVERTICAL);
51                 _gauge = new wxGauge (panel, wxID_ANY, 100);
52                 /* This seems to be required to allow the gauge to shrink under OS X */
53                 _gauge->SetMinSize (wxSize (0, -1));
54                 gauge_message->Add (_gauge, 0, wxEXPAND | wxLEFT | wxRIGHT);
55                 _message = new wxStaticText (panel, wxID_ANY, wxT (" \n "));
56                 gauge_message->Add (_message, 1, wxEXPAND | wxALIGN_CENTER_VERTICAL | wxALL, 6);
57                 table->Insert (n, gauge_message, 1, wxEXPAND | wxLEFT | wxRIGHT);
58                 ++n;
59
60                 _cancel = new wxButton (panel, wxID_ANY, _("Cancel"));
61                 _cancel->Bind (wxEVT_COMMAND_BUTTON_CLICKED, &JobRecord::cancel_clicked, this);
62                 table->Insert (n, _cancel, 1, wxALIGN_CENTER_VERTICAL | wxALL, 3);
63                 ++n;
64
65                 _pause = new wxButton (_panel, wxID_ANY, _("Pause"));
66                 _pause->Bind (wxEVT_COMMAND_BUTTON_CLICKED, &JobRecord::pause_clicked, this);
67                 table->Insert (n, _pause, 1, wxALIGN_CENTER_VERTICAL | wxALL, 3);
68                 ++n;
69
70                 _details = new wxButton (_panel, wxID_ANY, _("Details..."));
71                 _details->Bind (wxEVT_COMMAND_BUTTON_CLICKED, &JobRecord::details_clicked, this);
72                 _details->Enable (false);
73                 table->Insert (n, _details, 1, wxALIGN_CENTER_VERTICAL | wxALL, 3);
74                 ++n;
75
76                 _progress_connection = job->Progress.connect (boost::bind (&JobRecord::progress, this));
77                 _finished_connection = job->Finished.connect (boost::bind (&JobRecord::finished, this));
78
79                 progress ();
80
81                 table->Layout ();
82         }
83
84         void maybe_pulse ()
85         {
86                 if (_job->running() && !_job->progress ()) {
87                         _gauge->Pulse ();
88                 }
89         }
90
91 private:
92
93         void progress ()
94         {
95                 string whole = "<b>" + _job->name () + "</b>\n";
96                 if (!_job->sub_name().empty ()) {
97                         whole += _job->sub_name() + " ";
98                 }
99                 whole += _job->status ();
100                 if (whole != _last_message) {
101                         _message->SetLabelMarkup (std_to_wx (whole));
102                         _last_message = whole;
103                 }
104                 if (_job->progress ()) {
105                         _gauge->SetValue (min (100.0f, _job->progress().get() * 100));
106                 }
107         }
108
109         void finished ()
110         {
111                 progress ();
112
113                 if (!_job->finished_cancelled ()) {
114                         _gauge->SetValue (100);
115                 }
116
117                 _cancel->Enable (false);
118                 _pause->Enable (false);
119                 if (!_job->error_details().empty ()) {
120                         _details->Enable (true);
121                 }
122         }
123
124         void details_clicked (wxCommandEvent &)
125         {
126                 string s = _job->error_summary();
127                 s[0] = toupper (s[0]);
128                 error_dialog (_window, std_to_wx (String::compose ("%1.\n\n%2", s, _job->error_details())));
129         }
130
131         void cancel_clicked (wxCommandEvent &)
132         {
133                 _job->cancel ();
134         }
135
136         void pause_clicked (wxCommandEvent &)
137         {
138                 if (_job->paused()) {
139                         _job->resume ();
140                         _pause->SetLabel (_("Pause"));
141                 } else {
142                         _job->pause ();
143                         _pause->SetLabel (_("Resume"));
144                 }
145         }
146
147         boost::shared_ptr<Job> _job;
148         wxScrolledWindow* _window;
149         wxPanel* _panel;
150         wxGauge* _gauge;
151         wxStaticText* _message;
152         wxButton* _cancel;
153         wxButton* _pause;
154         wxButton* _details;
155         std::string _last_message;
156
157         boost::signals2::scoped_connection _progress_connection;
158         boost::signals2::scoped_connection _finished_connection;
159 };
160
161 /** Must be called in the GUI thread */
162 JobManagerView::JobManagerView (wxWindow* parent)
163         : wxScrolledWindow (parent)
164 {
165         _panel = new wxPanel (this);
166         wxSizer* sizer = new wxBoxSizer (wxVERTICAL);
167         sizer->Add (_panel, 1, wxEXPAND);
168         SetSizer (sizer);
169
170         _table = new wxFlexGridSizer (4, 4, 6);
171         _table->AddGrowableCol (0, 1);
172         _panel->SetSizer (_table);
173
174         SetScrollRate (0, 32);
175         EnableScrolling (false, true);
176
177         Bind (wxEVT_TIMER, boost::bind (&JobManagerView::periodic, this));
178         _timer.reset (new wxTimer (this));
179         _timer->Start (1000);
180
181         JobManager::instance()->JobAdded.connect (bind (&JobManagerView::job_added, this, _1));
182 }
183
184 void
185 JobManagerView::job_added (weak_ptr<Job> j)
186 {
187         shared_ptr<Job> job = j.lock ();
188         if (job) {
189                 _job_records.push_back (shared_ptr<JobRecord> (new JobRecord (job, this, _panel, _table)));
190         }
191 }
192
193 void
194 JobManagerView::periodic ()
195 {
196         for (list<shared_ptr<JobRecord> >::iterator i = _job_records.begin(); i != _job_records.end(); ++i) {
197                 (*i)->maybe_pulse ();
198         }
199 }