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