]> git.tdb.fi Git - r2c2.git/blob - source/engineer/timetabledialog.cpp
Add framework for generating simple meshes for vehicles
[r2c2.git] / source / engineer / timetabledialog.cpp
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 #include <msp/gltk/button.h>
9 #include <msp/io/print.h>
10 #include <msp/strings/utils.h>
11 #include "timetabledialog.h"
12
13 using namespace std;
14 using namespace Msp;
15 using namespace Marklin;
16
17 TimetableDialog::TimetableDialog(const GLtk::Resources &r, Timetable &tt):
18         GLtk::Widget(r),
19         GLtk::Dialog(r),
20         timetable(tt)
21 {
22         set_size(250, 200);
23
24         GLtk::Button *btn;
25
26         add_button(*(btn = new GLtk::Button(res, "Cncl")), 0);
27         btn->set_geometry(GLtk::Geometry(geom.w-80, 10, 30, 25));
28         btn->set_style("red");
29
30         add_button(*(btn = new GLtk::Button(res, "OK")), 1);
31         btn->set_geometry(GLtk::Geometry(geom.w-40, 10, 30, 25));
32         btn->set_style("green");
33
34         add(*(tgl_enabled = new GLtk::Toggle(res, "On")));
35         tgl_enabled->set_geometry(GLtk::Geometry(10, 10, 40, 27));
36         tgl_enabled->set_value(timetable.is_enabled());
37         tgl_enabled->signal_toggled.connect(sigc::mem_fun(this, &TimetableDialog::enabled_toggled));
38
39         add(*(ent_timetable = new GLtk::Entry(res)));
40         ent_timetable->set_geometry(GLtk::Geometry(10, 45, geom.w-20, geom.h-55));
41         ent_timetable->set_style("multiline");
42         ent_timetable->set_multiline(true);
43
44         string text;
45         for(unsigned i=0; i<timetable.get_n_rows(); ++i)
46         {
47                 if(!text.empty())
48                         text += '\n';
49                 text += timetable.get_row(i).str();
50         }
51         ent_timetable->set_text(text);
52 }
53
54 void TimetableDialog::enabled_toggled(bool value)
55 {
56         timetable.set_enabled(value);
57         timetable.reset();
58 }
59
60 void TimetableDialog::on_response(int code)
61 {
62         if(code)
63         {
64                 vector<string> lines = split(ent_timetable->get_text(), '\n');
65                 vector<Timetable::Row> rows;
66                 rows.reserve(lines.size());
67                 try
68                 {
69                         for(vector<string>::const_iterator i=lines.begin(); i!=lines.end(); ++i)
70                                 rows.push_back(Timetable::Row::parse(*i));
71                         timetable.clear();
72                         for(vector<Timetable::Row>::const_iterator i=rows.begin(); i!=rows.end(); ++i)
73                                 timetable.append(*i);
74                 }
75                 catch(const Exception &e)
76                 {
77                         // XXX Need a better way to report errors.  Also, should not let the dialog close.
78                         IO::print("%s\n", e.what());
79                 }
80         }
81 }