]> git.tdb.fi Git - r2c2.git/blob - source/libmarklin/timetable.h
Add a UI for editing train timetables
[r2c2.git] / source / libmarklin / timetable.h
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2010  Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #ifndef LIBMARKLIN_TIMETABLE_H_
9 #define LIBMARKLIN_TIMETABLE_H_
10
11 #include <string>
12 #include <vector>
13 #include <sigc++/trackable.h>
14 #include <msp/datafile/objectloader.h>
15 #include <msp/time/timestamp.h>
16
17 namespace Marklin {
18
19 class Block;
20 class Train;
21
22 class Timetable: public sigc::trackable
23 {
24 public:
25         class Loader: public Msp::DataFile::ObjectLoader<Timetable>
26         {
27         public:
28                 Loader(Timetable &);
29         private:
30                 void go_to(const std::string &);
31                 void route(const std::string &);
32                 void speed(int);
33                 void travel(const std::string &);
34                 void wait(unsigned);
35         };
36
37         enum RowType
38         {
39                 GOTO,
40                 TRAVEL,
41                 WAIT,
42                 SPEED,
43                 ROUTE
44         };
45
46         struct Row
47         {
48                 RowType type;
49                 int intparam;
50                 std::string strparam;
51
52                 Row(RowType, int);
53                 Row(RowType, const std::string &);
54
55                 std::string str() const;
56
57                 static Row parse(const std::string &);
58         };
59
60 private:
61         Train &train;
62         bool enabled;
63         std::vector<Row> rows;
64         unsigned current_row;
65         bool executing;
66         Block *pending_block;
67         Msp::Time::TimeStamp wait_timeout;
68
69 public:
70         Timetable(Train &);
71
72         void set_enabled(bool);
73         bool is_enabled() const { return enabled; }
74
75         void clear();
76         void append(const Row &);
77         void insert(unsigned, const Row &);
78         unsigned get_n_rows() const { return rows.size(); }
79         const Row &get_row(unsigned) const;
80
81         void tick(const Msp::Time::TimeStamp &);
82         void save(std::list<Msp::DataFile::Statement> &) const;
83 private:
84         Block &parse_location(const std::string &);
85         void sensor_event(unsigned, bool);
86         void train_arrived();
87 };
88
89 } // namespace Marklin
90
91 #endif