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