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