]> git.tdb.fi Git - r2c2.git/commitdiff
Add a UI for editing train timetables
authorMikko Rasa <tdb@tdb.fi>
Sat, 24 Apr 2010 21:01:15 +0000 (21:01 +0000)
committerMikko Rasa <tdb@tdb.fi>
Sat, 24 Apr 2010 21:01:15 +0000 (21:01 +0000)
Add an enabled flag to Timetable
Display turnout and sensor ids when clicked

source/engineer/engineer.cpp
source/engineer/timetabledialog.cpp [new file with mode: 0644]
source/engineer/timetabledialog.h [new file with mode: 0644]
source/engineer/trainpanel.cpp
source/engineer/trainpanel.h
source/libmarklin/timetable.cpp
source/libmarklin/timetable.h
source/libmarklin/train.cpp
source/libmarklin/train.h

index c8092872c4dc0e0b3314115329fabd8562d6b017..ff13b6e2d654c042dde30bb6bf46db39acf6d823 100644 (file)
@@ -270,12 +270,14 @@ void Engineer::button_press(int x, int y, unsigned btn, unsigned)
                                                        ++i;
                                        }
                                        track.set_active_path(i);
+                                       set_status(format("Turnout %d", track.get_turnout_id()));
                                }
                        }
-                       else if(options.simulate)
+                       if(unsigned sid = track.get_sensor_id())
                        {
-                               if(unsigned sid=track.get_sensor_id())
+                               if(options.simulate)
                                        layout.get_driver().set_sensor(sid, !layout.get_driver().get_sensor(sid));
+                               set_status(format("Sensor %d", sid));
                        }
                }
        }
diff --git a/source/engineer/timetabledialog.cpp b/source/engineer/timetabledialog.cpp
new file mode 100644 (file)
index 0000000..978b14b
--- /dev/null
@@ -0,0 +1,79 @@
+/* $Id$
+
+This file is part of the MSP Märklin suite
+Copyright © 2010  Mikkosoft Productions, Mikko Rasa
+Distributed under the GPL
+*/
+
+#include <msp/gltk/button.h>
+#include <msp/io/print.h>
+#include <msp/strings/utils.h>
+#include "timetabledialog.h"
+
+using namespace std;
+using namespace Msp;
+using namespace Marklin;
+
+TimetableDialog::TimetableDialog(const GLtk::Resources &r, Timetable &tt):
+       GLtk::Widget(r),
+       GLtk::Dialog(r),
+       timetable(tt)
+{
+       set_size(200, 200);
+
+       GLtk::Button *btn;
+
+       add_button(*(btn = new GLtk::Button(res, "Cncl")), 0);
+       btn->set_geometry(GLtk::Geometry(geom.w-80, 10, 30, 25));
+       btn->set_style("red");
+
+       add_button(*(btn = new GLtk::Button(res, "OK")), 1);
+       btn->set_geometry(GLtk::Geometry(geom.w-40, 10, 30, 25));
+       btn->set_style("green");
+
+       add(*(tgl_enabled = new GLtk::Toggle(res, "On")));
+       tgl_enabled->set_geometry(GLtk::Geometry(10, 10, 40, 27));
+       tgl_enabled->set_value(timetable.is_enabled());
+       tgl_enabled->signal_toggled.connect(sigc::mem_fun(this, &TimetableDialog::enabled_toggled));
+
+       add(*(ent_timetable = new GLtk::Entry(res)));
+       ent_timetable->set_geometry(GLtk::Geometry(10, 45, geom.w-20, geom.h-55));
+       ent_timetable->set_multiline(true);
+
+       string text;
+       for(unsigned i=0; i<timetable.get_n_rows(); ++i)
+       {
+               if(!text.empty())
+                       text += '\n';
+               text += timetable.get_row(i).str();
+       }
+       ent_timetable->set_text(text);
+}
+
+void TimetableDialog::enabled_toggled(bool value)
+{
+       timetable.set_enabled(value);
+}
+
+void TimetableDialog::on_response(int code)
+{
+       if(code)
+       {
+               timetable.clear();
+               vector<string> lines = split(ent_timetable->get_text(), '\n');
+               vector<Timetable::Row> rows;
+               rows.reserve(lines.size());
+               try
+               {
+                       for(vector<string>::const_iterator i=lines.begin(); i!=lines.end(); ++i)
+                               rows.push_back(Timetable::Row::parse(*i));
+                       for(vector<Timetable::Row>::const_iterator i=rows.begin(); i!=rows.end(); ++i)
+                               timetable.append(*i);
+               }
+               catch(const Exception &e)
+               {
+                       // XXX Need a better way to report errors.  Also, should not let the dialog close.
+                       IO::print("%s\n", e.what());
+               }
+       }
+}
diff --git a/source/engineer/timetabledialog.h b/source/engineer/timetabledialog.h
new file mode 100644 (file)
index 0000000..a75a091
--- /dev/null
@@ -0,0 +1,32 @@
+/* $Id$
+
+This file is part of the MSP Märklin suite
+Copyright © 2010  Mikkosoft Productions, Mikko Rasa
+Distributed under the GPL
+*/
+
+#ifndef TIMETABLEDIALOG_H_
+#define TIMETABLEDIALOG_H_
+
+#include <msp/gltk/dialog.h>
+#include <msp/gltk/entry.h>
+#include <msp/gltk/toggle.h>
+#include "libmarklin/timetable.h"
+
+class TimetableDialog: public Msp::GLtk::Dialog
+{
+private:
+       Marklin::Timetable &timetable;
+       Msp::GLtk::Entry *ent_timetable;
+       Msp::GLtk::Toggle *tgl_enabled;
+
+public:
+       TimetableDialog(const Msp::GLtk::Resources &, Marklin::Timetable &);
+
+private:
+       void enabled_toggled(bool);
+
+       virtual void on_response(int);
+};
+
+#endif
index 6685727663276642de5a1c0c3f56b920ca431906..590f1cd49e5537568b659c893e782a9370d46132 100644 (file)
@@ -9,8 +9,10 @@ Distributed under the GPL
 #include <msp/gltk/button.h>
 #include <msp/strings/formatter.h>
 #include "libmarklin/locotype.h"
+#include "libmarklin/timetable.h"
 #include "engineer.h"
 #include "routeselect.h"
+#include "timetabledialog.h"
 #include "trainpanel.h"
 #include "trainproperties.h"
 
@@ -84,20 +86,24 @@ TrainPanel::TrainPanel(Engineer &e, const GLtk::Resources &r, Train &t):
        GLtk::Button *btn;
 
        add(*(btn=new GLtk::Button(res, "Edit")));
-       btn->set_geometry(GLtk::Geometry(geom.w-50, 10, 40, 24));
+       btn->set_geometry(GLtk::Geometry(geom.w-46, 10, 36, 24));
        btn->signal_clicked.connect(sigc::mem_fun(this, &TrainPanel::edit_clicked));
 
        add(*(btn=new GLtk::Button(res, "Place")));
-       btn->set_geometry(GLtk::Geometry(geom.w-90, 10, 40, 24));
+       btn->set_geometry(GLtk::Geometry(geom.w-82, 10, 36, 24));
        btn->signal_clicked.connect(sigc::mem_fun(this, &TrainPanel::place_clicked));
 
        add(*(btn=new GLtk::Button(res, "GoTo")));
-       btn->set_geometry(GLtk::Geometry(geom.w-130, 10, 40, 24));
+       btn->set_geometry(GLtk::Geometry(geom.w-118, 10, 36, 24));
        btn->signal_clicked.connect(sigc::mem_fun(this, &TrainPanel::goto_clicked));
 
        add(*(btn=new GLtk::Button(res, "Route")));
-       btn->set_geometry(GLtk::Geometry(geom.w-170, 10, 40, 24));
+       btn->set_geometry(GLtk::Geometry(geom.w-154, 10, 36, 24));
        btn->signal_clicked.connect(sigc::mem_fun(this, &TrainPanel::route_clicked));
+
+       add(*(btn=new GLtk::Button(res, "TTbl")));
+       btn->set_geometry(GLtk::Geometry(geom.w-190, 10, 36, 24));
+       btn->signal_clicked.connect(sigc::mem_fun(this, &TrainPanel::timetable_clicked));
 }
 
 void TrainPanel::speed_slider_changed(double value)
@@ -168,6 +174,20 @@ void TrainPanel::goto_clicked()
        pick_conn = engineer.signal_pick_done.connect(sigc::mem_fun(this, &TrainPanel::go_to));
 }
 
+void TrainPanel::timetable_clicked()
+{
+       Timetable *timetable = train.get_timetable();
+       if(!timetable)
+       {
+               timetable = new Timetable(train);
+               train.set_timetable(timetable);
+       }
+
+       TimetableDialog *dialog = new TimetableDialog(res, *timetable);
+       engineer.get_root().add(*dialog);
+       dialog->set_position(geom.x+geom.w, geom.y+geom.h-dialog->get_geometry().h);
+}
+
 void TrainPanel::forward_toggled(bool /*value*/)
 {
        train.set_control("speed", 0);
index 12aa8fc835a8e1c5d9d355459e23be5aea0ae8ee..2f1453fcbaf00746e5aa4f8aa90a52cde109d025 100644 (file)
@@ -45,6 +45,7 @@ private:
        void edit_clicked();
        void route_clicked();
        void goto_clicked();
+       void timetable_clicked();
        void forward_toggled(bool);
        void func_toggled(bool, unsigned);
        void place(Marklin::Track *, unsigned);
index bd9d4b37313123c3b87f323f56e95e76a6f749f7..cfe2ef6c0d6226eaec15ee2896c096f4372557a2 100644 (file)
@@ -5,6 +5,7 @@ Copyright © 2010  Mikkosoft Productions, Mikko Rasa
 Distributed under the GPL
 */
 
+#include <msp/strings/formatter.h>
 #include <msp/time/units.h>
 #include "block.h"
 #include "catalogue.h"
@@ -20,6 +21,7 @@ namespace Marklin {
 
 Timetable::Timetable(Train &t):
        train(t),
+       enabled(false),
        current_row(0),
        executing(true),
        pending_block(0)
@@ -28,8 +30,44 @@ Timetable::Timetable(Train &t):
        train.get_layout().get_driver().signal_sensor.connect(sigc::mem_fun(this, &Timetable::sensor_event));
 }
 
+void Timetable::set_enabled(bool e)
+{
+       enabled = e;
+}
+
+void Timetable::clear()
+{
+       rows.clear();
+       current_row = 0;
+}
+
+void Timetable::append(const Row &row)
+{
+       rows.push_back(row);
+}
+
+void Timetable::insert(unsigned i, const Row &row)
+{
+       if(i>rows.size())
+               throw InvalidParameterValue("Insert position out of range");
+
+       rows.insert(rows.begin()+i, row);
+       if(i<=current_row)
+               ++current_row;
+}
+
+const Timetable::Row &Timetable::get_row(unsigned i) const
+{
+       if(i>=rows.size())
+               throw InvalidParameterValue("Row index out of range");
+       return rows[i];
+}
+
 void Timetable::tick(const Time::TimeStamp &t)
 {
+       if(rows.empty() || !enabled)
+               return;
+
        if(wait_timeout && t>=wait_timeout)
        {
                wait_timeout = Time::TimeStamp();
@@ -131,6 +169,51 @@ Timetable::Row::Row(RowType t, const string &p):
        strparam(p)
 { }
 
+string Timetable::Row::str() const
+{
+       switch(type)
+       {
+       case GOTO:
+               return "go to "+strparam;
+       case TRAVEL:
+               return "travel to "+strparam;
+       case WAIT:
+               return format("wait for %d seconds", intparam);
+       case SPEED:
+               return format("set speed %d km/h", intparam);
+       case ROUTE:
+               return "set route "+strparam;
+       default:
+               return "invalid row";
+       }
+}
+
+Timetable::Row Timetable::Row::parse(const string &s)
+{
+       if(!s.compare(0, 6, "go to "))
+               return Row(GOTO, s.substr(6));
+       else if(!s.compare(0, 10, "travel to "))
+               return Row(TRAVEL, s.substr(10));
+       else if(!s.compare(0, 9, "wait for ") && isdigit(s[9]))
+       {
+               unsigned nondigit = 10;
+               while(nondigit<s.size() && isdigit(s[nondigit]))
+                       ++nondigit;
+               return Row(WAIT, lexical_cast<unsigned>(s.substr(9, nondigit-9)));
+       }
+       else if(!s.compare(0, 10, "set speed "))
+       {
+               unsigned nondigit = 11;
+               while(nondigit<s.size() && isdigit(s[nondigit]))
+                       ++nondigit;
+               return Row(SPEED, lexical_cast<unsigned>(s.substr(10, nondigit-10)));
+       }
+       else if(!s.compare(0, 10, "set route "))
+               return Row(ROUTE, s.substr(10));
+
+       throw InvalidParameterValue("Invalid row");
+}
+
 
 Timetable::Loader::Loader(Timetable &tt):
        DataFile::ObjectLoader<Timetable>(tt)
index 3c76055e712e6d54d82d29bff77b3511da4e0248..6631f3822c734f2ff99c8a9b28b8e70fd01bc4b5 100644 (file)
@@ -51,10 +51,15 @@ public:
 
                Row(RowType, int);
                Row(RowType, const std::string &);
+
+               std::string str() const;
+
+               static Row parse(const std::string &);
        };
 
 private:
        Train &train;
+       bool enabled;
        std::vector<Row> rows;
        unsigned current_row;
        bool executing;
@@ -64,6 +69,15 @@ private:
 public:
        Timetable(Train &);
 
+       void set_enabled(bool);
+       bool is_enabled() const { return enabled; }
+
+       void clear();
+       void append(const Row &);
+       void insert(unsigned, const Row &);
+       unsigned get_n_rows() const { return rows.size(); }
+       const Row &get_row(unsigned) const;
+
        void tick(const Msp::Time::TimeStamp &);
        void save(std::list<Msp::DataFile::Statement> &) const;
 private:
index efe1a142cb6daefc29162f1a99bd578f55de7f9b..a6fd46d1e40519381c9723058b7252736a5f3b98 100644 (file)
@@ -141,6 +141,12 @@ bool Train::get_function(unsigned func) const
        return (functions>>func)&1;
 }
 
+void Train::set_timetable(Timetable *tt)
+{
+       delete timetable;
+       timetable = tt;
+}
+
 void Train::set_route(const Route *r)
 {
        if(!rsv_blocks.empty())
index 8087745f3dd29f03b9d3b3bfb4eec75859c246e8..ccfcbec1058f0dd6c12ab5409ad66415e4c27298 100644 (file)
@@ -120,6 +120,9 @@ public:
        bool get_function(unsigned) const;
        unsigned get_functions() const { return functions; }
 
+       void set_timetable(Timetable *);
+       Timetable *get_timetable() { return timetable; }
+
        void set_route(const Route *);
        void go_to(const Track &);
        const Route *get_route() const { return route; }