Fix shared_ptr for Film.
[dcpomatic.git] / src / wx / job_manager_view.cc
index 22c18fd93f4624e733d909d481c84844b3ef94a0..bd28862319aa956bb074e2523f74587d94966011 100644 (file)
 #include "lib/util.h"
 #include "lib/exceptions.h"
 #include "job_manager_view.h"
-#include "gtk_util.h"
+#include "wx_util.h"
 
-using namespace std;
-using namespace boost;
+using std::string;
+using std::list;
+using boost::shared_ptr;
 
 /** Must be called in the GUI thread */
-JobManagerView::JobManagerView ()
+JobManagerView::JobManagerView (wxWindow* parent)
+       : wxScrolledWindow (parent)
 {
-       _scroller.set_policy (Gtk::POLICY_NEVER, Gtk::POLICY_ALWAYS);
-       
-       _store = Gtk::TreeStore::create (_columns);
-       _view.set_model (_store);
-       _view.append_column ("Name", _columns.name);
-
-       Gtk::CellRendererProgress* r = Gtk::manage (new Gtk::CellRendererProgress ());
-       int const n = _view.append_column ("Progress", *r);
-       Gtk::TreeViewColumn* c = _view.get_column (n - 1);
-       c->add_attribute (r->property_value(), _columns.progress);
-       c->add_attribute (r->property_pulse(), _columns.progress_unknown);
-       c->add_attribute (r->property_text(), _columns.text);
-
-       _scroller.add (_view);
-       _scroller.set_size_request (-1, 150);
+       _panel = new wxPanel (this);
+       wxSizer* sizer = new wxBoxSizer (wxVERTICAL);
+       sizer->Add (_panel, 1, wxEXPAND);
+       SetSizer (sizer);
        
+       _table = new wxFlexGridSizer (3, 6, 6);
+       _table->AddGrowableCol (1, 1);
+       _panel->SetSizer (_table);
+
+       SetScrollRate (0, 32);
+
+       Connect (wxID_ANY, wxEVT_TIMER, wxTimerEventHandler (JobManagerView::periodic), 0, this);
+       _timer.reset (new wxTimer (this));
+       _timer->Start (1000);
+
+       update ();
+}
+
+void
+JobManagerView::periodic (wxTimerEvent &)
+{
        update ();
 }
 
-/** Update the view by examining the state of each jobs.
+/** Update the view by examining the state of each job.
  *  Must be called in the GUI thread.
  */
 void
@@ -61,75 +68,45 @@ JobManagerView::update ()
 {
        list<shared_ptr<Job> > jobs = JobManager::instance()->get ();
 
-       for (list<shared_ptr<Job> >::iterator i = jobs.begin(); i != jobs.end(); ++i) {
-               Gtk::ListStore::iterator j = _store->children().begin();
-               while (j != _store->children().end()) {
-                       Gtk::TreeRow r = *j;
-                       shared_ptr<Job> job = r[_columns.job];
-                       if (job == *i) {
-                               break;
-                       }
-                       ++j;
-               }
+       int index = 0;
 
-               Gtk::TreeRow r;
-               if (j == _store->children().end ()) {
-                       j = _store->append ();
-                       r = *j;
-                       r[_columns.name] = (*i)->name ();
-                       r[_columns.job] = *i;
-                       r[_columns.progress_unknown] = -1;
-                       r[_columns.informed_of_finish] = false;
-               } else {
-                       r = *j;
+       for (list<shared_ptr<Job> >::iterator i = jobs.begin(); i != jobs.end(); ++i) {
+               
+               if (_job_records.find (*i) == _job_records.end ()) {
+                       wxStaticText* m = new wxStaticText (_panel, wxID_ANY, std_to_wx ((*i)->name ()));
+                       _table->Insert (index, m, 0, wxALIGN_CENTER_VERTICAL | wxALL, 6);
+                       
+                       JobRecord r;
+                       r.gauge = new wxGauge (_panel, wxID_ANY, 100);
+                       _table->Insert (index + 1, r.gauge, 1, wxEXPAND | wxLEFT | wxRIGHT);
+                       
+                       r.message = new wxStaticText (_panel, wxID_ANY, std_to_wx (""));
+                       _table->Insert (index + 2, r.message, 1, wxALIGN_CENTER_VERTICAL | wxALL, 6);
+                       
+                       _job_records[*i] = r;
                }
 
-               bool inform_of_finish = false;
                string const st = (*i)->status ();
 
                if (!(*i)->finished ()) {
                        float const p = (*i)->overall_progress ();
                        if (p >= 0) {
-                               r[_columns.text] = st;
-                               r[_columns.progress] = p * 100;
+                               _job_records[*i].message->SetLabel (std_to_wx (st));
+                               _job_records[*i].gauge->SetValue (p * 100);
                        } else {
-                               r[_columns.text] = "Running";
-                               r[_columns.progress_unknown] = r[_columns.progress_unknown] + 1;
+                               _job_records[*i].message->SetLabel (wxT ("Running"));
+                               _job_records[*i].gauge->Pulse ();
                        }
                }
                
-               /* Hack to work around our lack of cross-thread
-                  signalling; we tell the job to emit_finished()
-                  from here (the GUI thread).
-               */
-               
-               if ((*i)->finished_ok ()) {
-                       bool i = r[_columns.informed_of_finish];
-                       if (!i) {
-                               r[_columns.progress_unknown] = -1;
-                               r[_columns.progress] = 100;
-                               r[_columns.text] = st;
-                               inform_of_finish = true;
-                       }
-               } else if ((*i)->finished_in_error ()) {
-                       bool i = r[_columns.informed_of_finish];
-                       if (!i) {
-                               r[_columns.progress_unknown] = -1;
-                               r[_columns.progress] = 100;
-                               r[_columns.text] = st;
-                               inform_of_finish = true;
-                       }
+               if ((*i)->finished()) {
+                       _job_records[*i].gauge->SetValue (100);
+                       _job_records[*i].message->SetLabel (std_to_wx (st));
                }
 
-               if (inform_of_finish) {
-                       try {
-                               (*i)->emit_finished ();
-                       } catch (OpenFileError& e) {
-                               stringstream s;
-                               s << "Error: " << e.what();
-                               error_dialog (s.str ());
-                       }
-                       r[_columns.informed_of_finish] = true;
-               }
+               index += 3;
        }
+
+       _table->Layout ();
+       FitInside ();
 }