Merge branch 'master' of ssh://git.carlh.net/home/carl/git/dcpomatic
[dcpomatic.git] / src / wx / job_view.cc
1 /*
2     Copyright (C) 2012-2015 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 #include "job_view.h"
22 #include "wx_util.h"
23 #include "lib/job.h"
24 #include "lib/compose.hpp"
25 #include <wx/wx.h>
26
27 using std::string;
28 using std::min;
29 using boost::shared_ptr;
30
31 JobView::JobView (shared_ptr<Job> job, wxWindow* parent, wxWindow* container, wxFlexGridSizer* table)
32         : _job (job)
33         , _table (table)
34         , _parent (parent)
35         , _container (container)
36 {
37
38 }
39
40 void
41 JobView::setup ()
42 {
43         int n = insert_position ();
44
45         _gauge_message = new wxBoxSizer (wxVERTICAL);
46         _gauge = new wxGauge (_container, wxID_ANY, 100);
47         /* This seems to be required to allow the gauge to shrink under OS X */
48         _gauge->SetMinSize (wxSize (0, -1));
49         _gauge_message->Add (_gauge, 0, wxEXPAND | wxLEFT | wxRIGHT);
50         _message = new wxStaticText (_container, wxID_ANY, wxT (" \n "), wxDefaultPosition, wxDefaultSize, wxST_ELLIPSIZE_MIDDLE);
51         _gauge_message->Add (_message, 1, wxEXPAND | wxALIGN_CENTER_VERTICAL | wxALL, 6);
52         _table->Insert (n, _gauge_message, 1, wxEXPAND | wxLEFT | wxRIGHT);
53         ++n;
54
55         _buttons = new wxBoxSizer (wxHORIZONTAL);
56
57         _cancel = new wxButton (_container, wxID_ANY, _("Cancel"));
58         _cancel->Bind (wxEVT_BUTTON, &JobView::cancel_clicked, this);
59         _buttons->Add (_cancel, 1, wxALIGN_CENTER_VERTICAL);
60
61         _details = new wxButton (_container, wxID_ANY, _("Details..."));
62         _details->Bind (wxEVT_BUTTON, &JobView::details_clicked, this);
63         _details->Enable (false);
64         _buttons->Add (_details, 1, wxALIGN_CENTER_VERTICAL);
65
66         finish_setup (_container, _buttons);
67
68         _table->Insert (n, _buttons, 1, wxALIGN_CENTER_VERTICAL | wxALL, 3);
69
70         _progress_connection = _job->Progress.connect (boost::bind (&JobView::progress, this));
71         _finished_connection = _job->Finished.connect (boost::bind (&JobView::finished, this));
72
73         progress ();
74
75         _table->Layout ();
76 }
77
78 void
79 JobView::maybe_pulse ()
80 {
81         if (_job->running() && !_job->progress ()) {
82                 _gauge->Pulse ();
83         }
84 }
85
86 void
87 JobView::progress ()
88 {
89         string whole = "<b>" + _job->name () + "</b>\n";
90         if (!_job->sub_name().empty ()) {
91                 whole += _job->sub_name() + " ";
92         }
93         whole += _job->status ();
94         if (whole != _last_message) {
95                 _message->SetLabelMarkup (std_to_wx (whole));
96                 /* This hack fixes the size of _message on OS X */
97                 _message->InvalidateBestSize ();
98                 _message->SetSize (_message->GetBestSize ());
99                 _gauge_message->Layout ();
100                 _last_message = whole;
101         }
102         if (_job->progress ()) {
103                 _gauge->SetValue (min (100.0f, _job->progress().get() * 100));
104         }
105 }
106
107 void
108 JobView::finished ()
109 {
110         progress ();
111
112         if (!_job->finished_cancelled ()) {
113                 _gauge->SetValue (100);
114         }
115
116         _cancel->Enable (false);
117         if (!_job->error_details().empty ()) {
118                 _details->Enable (true);
119         }
120 }
121
122 void
123 JobView::details_clicked (wxCommandEvent &)
124 {
125         string s = _job->error_summary();
126         s[0] = toupper (s[0]);
127         error_dialog (_parent, std_to_wx (String::compose ("%1.\n\n%2", s, _job->error_details())));
128 }
129
130 void
131 JobView::cancel_clicked (wxCommandEvent &)
132 {
133         if (confirm_dialog (_parent, _("Are you sure you want to cancel this job?"))) {
134                 _job->cancel ();
135         }
136 }
137
138 void
139 JobView::insert (int pos)
140 {
141         _table->Insert (pos, _gauge_message, 1, wxEXPAND | wxLEFT | wxRIGHT);
142         _table->Insert (pos + 1, _buttons, 1, wxALIGN_CENTER_VERTICAL | wxALL, 3);
143         _table->Layout ();
144 }
145
146 void
147 JobView::detach ()
148 {
149         _table->Detach (_gauge_message);
150         _table->Detach (_buttons);
151 }