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"
34 using boost::shared_ptr;
36 /** Must be called in the GUI thread */
37 JobManagerView::JobManagerView (wxWindow* parent, Buttons buttons)
38 : wxScrolledWindow (parent)
41 _panel = new wxPanel (this);
42 wxSizer* sizer = new wxBoxSizer (wxVERTICAL);
43 sizer->Add (_panel, 1, wxEXPAND);
47 if (buttons & PAUSE) {
51 _table = new wxFlexGridSizer (N, 6, 6);
52 _table->AddGrowableCol (1, 1);
53 _panel->SetSizer (_table);
55 SetScrollRate (0, 32);
57 Connect (wxID_ANY, wxEVT_TIMER, wxTimerEventHandler (JobManagerView::periodic), 0, this);
58 _timer.reset (new wxTimer (this));
65 JobManagerView::periodic (wxTimerEvent &)
70 /** Update the view by examining the state of each job.
71 * Must be called in the GUI thread.
74 JobManagerView::update ()
76 list<shared_ptr<Job> > jobs = JobManager::instance()->get ();
80 for (list<shared_ptr<Job> >::iterator i = jobs.begin(); i != jobs.end(); ++i) {
82 if (_job_records.find (*i) == _job_records.end ()) {
83 wxStaticText* m = new wxStaticText (_panel, wxID_ANY, std_to_wx ((*i)->name ()));
84 _table->Insert (index, m, 0, wxALIGN_CENTER_VERTICAL | wxALL, 6);
89 r.gauge = new wxGauge (_panel, wxID_ANY, 100);
90 _table->Insert (index + n, r.gauge, 1, wxEXPAND | wxLEFT | wxRIGHT);
93 r.message = new wxStaticText (_panel, wxID_ANY, std_to_wx (""));
94 _table->Insert (index + n, r.message, 1, wxALIGN_CENTER_VERTICAL | wxALL, 6);
97 r.cancel = new wxButton (_panel, wxID_ANY, _("Cancel"));
98 r.cancel->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (JobManagerView::cancel_clicked), 0, this);
99 _table->Insert (index + n, r.cancel, 1, wxALIGN_CENTER_VERTICAL | wxALL, 6);
102 if (_buttons & PAUSE) {
103 r.pause = new wxButton (_panel, wxID_ANY, _("Pause"));
104 r.pause->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (JobManagerView::pause_clicked), 0, this);
105 _table->Insert (index + n, r.pause, 1, wxALIGN_CENTER_VERTICAL | wxALL, 6);
109 r.details = new wxButton (_panel, wxID_ANY, _("Details..."));
110 r.details->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (JobManagerView::details_clicked), 0, this);
111 r.details->Enable (false);
112 _table->Insert (index + n, r.details, 1, wxALIGN_CENTER_VERTICAL | wxALL, 6);
115 _job_records[*i] = r;
118 string const st = (*i)->status ();
120 if (!(*i)->finished ()) {
121 float const p = (*i)->overall_progress ();
123 checked_set (_job_records[*i].message, st);
124 _job_records[*i].gauge->SetValue (p * 100);
126 checked_set (_job_records[*i].message, wx_to_std (_("Running")));
127 _job_records[*i].gauge->Pulse ();
131 if ((*i)->finished() && !_job_records[*i].finalised) {
132 checked_set (_job_records[*i].message, st);
133 if (!(*i)->finished_cancelled()) {
134 _job_records[*i].gauge->SetValue (100);
137 _job_records[*i].finalised = true;
138 _job_records[*i].cancel->Enable (false);
139 if (!(*i)->error_details().empty ()) {
140 _job_records[*i].details->Enable (true);
145 if (_buttons & PAUSE) {
155 JobManagerView::details_clicked (wxCommandEvent& ev)
157 wxObject* o = ev.GetEventObject ();
159 for (map<boost::shared_ptr<Job>, JobRecord>::iterator i = _job_records.begin(); i != _job_records.end(); ++i) {
160 if (i->second.details == o) {
161 string s = i->first->error_summary();
162 s[0] = toupper (s[0]);
163 error_dialog (this, std_to_wx (String::compose ("%1.\n\n%2", s, i->first->error_details())));
169 JobManagerView::cancel_clicked (wxCommandEvent& ev)
171 wxObject* o = ev.GetEventObject ();
173 for (map<boost::shared_ptr<Job>, JobRecord>::iterator i = _job_records.begin(); i != _job_records.end(); ++i) {
174 if (i->second.cancel == o) {
181 JobManagerView::pause_clicked (wxCommandEvent& ev)
183 wxObject* o = ev.GetEventObject ();
184 for (map<boost::shared_ptr<Job>, JobRecord>::iterator i = _job_records.begin(); i != _job_records.end(); ++i) {
185 if (i->second.pause == o) {
186 if (i->first->paused()) {
188 i->second.pause->SetLabel (_("Pause"));
191 i->second.pause->SetLabel (_("Resume"));