2 Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 /** @file src/job_manager_view.cc
21 * @brief Class generating a GTK widget to show the progress of jobs.
24 #include "lib/job_manager.h"
27 #include "lib/exceptions.h"
28 #include "job_manager_view.h"
36 using boost::shared_ptr;
37 using boost::weak_ptr;
42 JobRecord (shared_ptr<Job> job, wxScrolledWindow* window, wxPanel* panel, wxFlexGridSizer* table)
50 _name = new wxStaticText (panel, wxID_ANY, "");
51 string const jn = "<b>" + _job->name () + "</b>";
52 _name->SetLabelMarkup (std_to_wx (jn));
53 table->Insert (n, _name, 0, wxALIGN_CENTER_VERTICAL | wxALL, 6);
56 _gauge = new wxGauge (panel, wxID_ANY, 100);
57 /* This seems to be required to allow the gauge to shrink under OS X */
58 _gauge->SetMinSize (wxSize (0, -1));
59 table->Insert (n, _gauge, 1, wxEXPAND | wxLEFT | wxRIGHT);
62 _message = new wxStaticText (panel, wxID_ANY, std_to_wx (""));
63 table->Insert (n, _message, 1, wxALIGN_CENTER_VERTICAL | wxALL, 6);
66 _cancel = new wxButton (panel, wxID_ANY, _("Cancel"));
67 _cancel->Bind (wxEVT_COMMAND_BUTTON_CLICKED, &JobRecord::cancel_clicked, this);
68 table->Insert (n, _cancel, 1, wxALIGN_CENTER_VERTICAL | wxALL, 6);
71 _pause = new wxButton (_panel, wxID_ANY, _("Pause"));
72 _pause->Bind (wxEVT_COMMAND_BUTTON_CLICKED, &JobRecord::pause_clicked, this);
73 table->Insert (n, _pause, 1, wxALIGN_CENTER_VERTICAL | wxALL, 6);
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);
82 job->Progress.connect (boost::bind (&JobRecord::progress, this));
83 job->Finished.connect (boost::bind (&JobRecord::finished, this));
91 if (_job->running() && _job->progress_unknown ()) {
98 void update_job_name ()
100 string n = "<b>" + _job->name () + "</b>";
101 if (!_job->sub_name().empty ()) {
102 n += "\n" + _job->sub_name ();
105 if (n != _last_name) {
106 _name->SetLabelMarkup (std_to_wx (n));
113 float const p = _job->progress ();
115 checked_set (_message, _job->status ());
117 int const pp = min (100.0f, p * 100);
118 _gauge->SetValue (pp);
122 _window->FitInside ();
127 checked_set (_message, _job->status ());
130 if (!_job->finished_cancelled ()) {
131 _gauge->SetValue (100);
134 _cancel->Enable (false);
135 _pause->Enable (false);
136 if (!_job->error_details().empty ()) {
137 _details->Enable (true);
141 _window->FitInside ();
144 void details_clicked (wxCommandEvent &)
146 string s = _job->error_summary();
147 s[0] = toupper (s[0]);
148 error_dialog (_window, std_to_wx (String::compose ("%1.\n\n%2", s, _job->error_details())));
151 void cancel_clicked (wxCommandEvent &)
156 void pause_clicked (wxCommandEvent &)
158 if (_job->paused()) {
160 _pause->SetLabel (_("Pause"));
163 _pause->SetLabel (_("Resume"));
167 boost::shared_ptr<Job> _job;
168 wxScrolledWindow* _window;
170 wxFlexGridSizer* _table;
173 wxStaticText* _message;
177 std::string _last_name;
180 /** Must be called in the GUI thread */
181 JobManagerView::JobManagerView (wxWindow* parent)
182 : wxScrolledWindow (parent)
184 _panel = new wxPanel (this);
185 wxSizer* sizer = new wxBoxSizer (wxVERTICAL);
186 sizer->Add (_panel, 1, wxEXPAND);
189 _table = new wxFlexGridSizer (6, 6, 6);
190 _table->AddGrowableCol (1, 1);
191 _panel->SetSizer (_table);
193 SetScrollRate (0, 32);
194 EnableScrolling (false, true);
196 Bind (wxEVT_TIMER, boost::bind (&JobManagerView::periodic, this));
197 _timer.reset (new wxTimer (this));
198 _timer->Start (1000);
200 Bind (wxEVT_SIZE, boost::bind (&JobManagerView::sized, this, _1));
201 JobManager::instance()->JobAdded.connect (bind (&JobManagerView::job_added, this, _1));
205 JobManagerView::sized (wxSizeEvent& ev)
207 _table->FitInside (_panel);
213 JobManagerView::job_added (weak_ptr<Job> j)
215 shared_ptr<Job> job = j.lock ();
217 _job_records.push_back (shared_ptr<JobRecord> (new JobRecord (job, this, _panel, _table)));
222 JobManagerView::periodic ()
224 for (list<shared_ptr<JobRecord> >::iterator i = _job_records.begin(); i != _job_records.end(); ++i) {
225 (*i)->maybe_pulse ();