]> git.tdb.fi Git - r2c2.git/blob - source/engineer/clockdialog.cpp
Reflect the current state of functions in a newly opened train dialog
[r2c2.git] / source / engineer / clockdialog.cpp
1 #include <msp/gltk/button.h>
2 #include <msp/gltk/stack.h>
3 #include "libr2c2/layout.h"
4 #include "clockdialog.h"
5 #include "clockwidget.h"
6 #include "engineer.h"
7
8 using namespace std;
9 using namespace Msp;
10 using namespace R2C2;
11
12 ClockDialog::ClockDialog(Engineer &engineer):
13         DynamicDialog(engineer.get_user_interface()),
14         clock(engineer.get_layout().get_clock())
15 {
16         Loader::WidgetMap widgets;
17         DataFile::load(*this, "data/clockdialog.ui", widgets);
18
19         GLtk::Button *button = dynamic_cast<GLtk::Button *>(get_item(widgets, "btn_rate"));
20         button->signal_clicked.connect(sigc::mem_fun(this, &ClockDialog::set_rate));
21         ent_rate = dynamic_cast<GLtk::Entry *>(get_item(widgets, "ent_rate"));
22         ent_rate->set_text(lexical_cast<string>(clock.get_rate()));
23
24         button = dynamic_cast<GLtk::Button *>(get_item(widgets, "btn_stop"));
25         button->signal_clicked.connect(sigc::mem_fun(this, &ClockDialog::stop_clicked));
26         ind_stopped = dynamic_cast<GLtk::Indicator *>(get_item(widgets, "ind_stopped"));
27         ind_stopped->set_active(clock.is_stopped());
28
29         button = dynamic_cast<GLtk::Button *>(get_item(widgets, "btn_time"));
30         button->signal_clicked.connect(sigc::mem_fun(this, &ClockDialog::set_time));
31         ent_hour = dynamic_cast<GLtk::Entry *>(get_item(widgets, "ent_hour"));
32         ent_minute = dynamic_cast<GLtk::Entry *>(get_item(widgets, "ent_minute"));
33
34         GLtk::Stack stack(*layout);
35         stack.arrange(*get_item(widgets, "placeholder"));
36         add(*(new ClockWidget(clock)));
37
38         clock.signal_minute.connect(sigc::mem_fun(this, &ClockDialog::update_time));
39         clock.signal_discontinuity.connect(sigc::mem_fun(this, &ClockDialog::update_time));
40         update_time();
41 }
42
43 void ClockDialog::update_time()
44 {
45         Time::TimeDelta time = clock.get_current_time();
46         unsigned hour = time/Time::hour;
47         unsigned minute = static_cast<unsigned>(time/Time::min)%60;
48         ent_hour->set_text(lexical_cast<string>(hour));
49         ent_minute->set_text(lexical_cast<string>(minute));
50 }
51
52 void ClockDialog::set_rate()
53 {
54         try
55         {
56                 float rate = lexical_cast<float>(ent_rate->get_text());
57                 clock.set_rate(rate);
58         }
59         catch(const lexical_error &e)
60         {
61         }
62 }
63
64 void ClockDialog::stop_clicked()
65 {
66         clock.stop(!clock.is_stopped());
67         ind_stopped->set_active(clock.is_stopped());
68 }
69
70 void ClockDialog::set_time()
71 {
72         try
73         {
74                 unsigned hour = lexical_cast<unsigned>(ent_hour->get_text());
75                 unsigned minute = lexical_cast<unsigned>(ent_minute->get_text());
76                 clock.set_current_time(hour*Time::hour+minute*Time::min);
77         }
78         catch(const lexical_error &e)
79         {
80         }
81 }
82
83 bool ClockDialog::save_state(DataFile::Statement &st) const
84 {
85         st.keyword = "clockdialog";
86         save_position(st.sub);
87
88         return true;
89 }