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