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;
35 using boost::weak_ptr;
37 /** Must be called in the GUI thread */
38 JobManagerView::JobManagerView (wxWindow* parent, Buttons buttons)
39 : wxScrolledWindow (parent)
42 _panel = new wxPanel (this);
43 wxSizer* sizer = new wxBoxSizer (wxVERTICAL);
44 sizer->Add (_panel, 1, wxEXPAND);
48 if (buttons & PAUSE) {
52 _table = new wxFlexGridSizer (N, 6, 6);
53 _table->AddGrowableCol (1, 1);
54 _panel->SetSizer (_table);
56 SetScrollRate (0, 32);
58 JobManager::instance()->JobAdded.connect (bind (&JobManagerView::job_added, this, _1));
62 JobManagerView::job_added (weak_ptr<Job> j)
64 shared_ptr<Job> job = j.lock ();
69 wxStaticText* m = new wxStaticText (_panel, wxID_ANY, std_to_wx (job->name ()));
70 _table->Insert (0, m, 0, wxALIGN_CENTER_VERTICAL | wxALL, 6);
74 r.scroll_nudged = false;
75 r.gauge = new wxGauge (_panel, wxID_ANY, 100);
76 _table->Insert (n, r.gauge, 1, wxEXPAND | wxLEFT | wxRIGHT);
79 r.message = new wxStaticText (_panel, wxID_ANY, std_to_wx (""));
80 _table->Insert (n, r.message, 1, wxALIGN_CENTER_VERTICAL | wxALL, 6);
83 r.cancel = new wxButton (_panel, wxID_ANY, _("Cancel"));
84 r.cancel->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (JobManagerView::cancel_clicked), 0, this);
85 _table->Insert (n, r.cancel, 1, wxALIGN_CENTER_VERTICAL | wxALL, 6);
88 if (_buttons & PAUSE) {
89 r.pause = new wxButton (_panel, wxID_ANY, _("Pause"));
90 r.pause->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (JobManagerView::pause_clicked), 0, this);
91 _table->Insert (n, r.pause, 1, wxALIGN_CENTER_VERTICAL | wxALL, 6);
95 r.details = new wxButton (_panel, wxID_ANY, _("Details..."));
96 r.details->Connect (wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler (JobManagerView::details_clicked), 0, this);
97 r.details->Enable (false);
98 _table->Insert (n, r.details, 1, wxALIGN_CENTER_VERTICAL | wxALL, 6);
101 _job_records[job] = r;
103 job->Progress.connect (bind (&JobManagerView::progress, this, j));
104 job->Progress.connect (bind (&JobManagerView::finished, this, j));
111 JobManagerView::progress (weak_ptr<Job> j)
113 shared_ptr<Job> job = j.lock ();
118 float const p = job->overall_progress ();
120 checked_set (_job_records[job].message, job->status ());
121 _job_records[job].gauge->SetValue (p * 100);
123 checked_set (_job_records[job].message, wx_to_std (_("Running")));
124 _job_records[job].gauge->Pulse ();
127 if (!_job_records[job].scroll_nudged && (job->running () || job->finished())) {
129 _job_records[job].gauge->GetPosition (&x, &y);
131 GetScrollPixelsPerUnit (&px, &py);
133 GetViewStart (&vx, &vy);
135 GetClientSize (&sx, &sy);
137 if (y > (vy * py + sy / 2)) {
139 _job_records[job].scroll_nudged = true;
148 JobManagerView::finished (weak_ptr<Job> j)
150 shared_ptr<Job> job = j.lock ();
155 checked_set (_job_records[job].message, job->status ());
156 if (!job->finished_cancelled ()) {
157 _job_records[job].gauge->SetValue (100);
159 _job_records[job].cancel->Enable (false);
160 if (!job->error_details().empty ()) {
161 _job_records[job].details->Enable (true);
169 JobManagerView::details_clicked (wxCommandEvent& ev)
171 wxObject* o = ev.GetEventObject ();
173 for (map<shared_ptr<Job>, JobRecord>::iterator i = _job_records.begin(); i != _job_records.end(); ++i) {
174 if (i->second.details == o) {
175 string s = i->first->error_summary();
176 s[0] = toupper (s[0]);
177 error_dialog (this, std_to_wx (String::compose ("%1.\n\n%2", s, i->first->error_details())));
183 JobManagerView::cancel_clicked (wxCommandEvent& ev)
185 wxObject* o = ev.GetEventObject ();
187 for (map<shared_ptr<Job>, JobRecord>::iterator i = _job_records.begin(); i != _job_records.end(); ++i) {
188 if (i->second.cancel == o) {
195 JobManagerView::pause_clicked (wxCommandEvent& ev)
197 wxObject* o = ev.GetEventObject ();
198 for (map<boost::shared_ptr<Job>, JobRecord>::iterator i = _job_records.begin(); i != _job_records.end(); ++i) {
199 if (i->second.pause == o) {
200 if (i->first->paused()) {
202 i->second.pause->SetLabel (_("Pause"));
205 i->second.pause->SetLabel (_("Resume"));