]> git.tdb.fi Git - r2c2.git/blob - source/engineer/mainwindow.cpp
Reflect the current state of functions in a newly opened train dialog
[r2c2.git] / source / engineer / mainwindow.cpp
1 #include <msp/gltk/button.h>
2 #include <msp/strings/format.h>
3 #include "libr2c2/clock.h"
4 #include "libr2c2/driver.h"
5 #include "clockdialog.h"
6 #include "engineer.h"
7 #include "mainwindow.h"
8 #include "telemetrydialog.h"
9 #include "trainlistdialog.h"
10
11 using namespace std;
12 using namespace Msp;
13
14 MainWindow::MainWindow(Engineer &e):
15         engineer(e)
16 {
17         Loader::WidgetMap widgets;
18         DataFile::load(*this, "data/mainwindow.ui", widgets);
19
20         ind_on = dynamic_cast<GLtk::Indicator *>(get_item(widgets, "ind_on"));
21         ind_off = dynamic_cast<GLtk::Indicator *>(get_item(widgets, "ind_off"));
22         ind_halt = dynamic_cast<GLtk::Indicator *>(get_item(widgets, "ind_halt"));
23         lbl_status = dynamic_cast<GLtk::Label *>(get_item(widgets, "lbl_status"));
24         lbl_clock = dynamic_cast<GLtk::Label *>(get_item(widgets, "lbl_clock"));
25
26         dynamic_cast<GLtk::Button *>(get_item(widgets, "btn_on"))->signal_clicked.connect(sigc::mem_fun(this, &MainWindow::on_clicked));
27         dynamic_cast<GLtk::Button *>(get_item(widgets, "btn_off"))->signal_clicked.connect(sigc::mem_fun(this, &MainWindow::off_clicked));
28         dynamic_cast<GLtk::Button *>(get_item(widgets, "btn_halt"))->signal_clicked.connect(sigc::mem_fun(this, &MainWindow::halt_clicked));
29         dynamic_cast<GLtk::Button *>(get_item(widgets, "btn_trains"))->signal_clicked.connect(sigc::mem_fun(this, &MainWindow::trains_clicked));
30         dynamic_cast<GLtk::Button *>(get_item(widgets, "btn_telemetry"))->signal_clicked.connect(sigc::mem_fun(this, &MainWindow::telemetry_clicked));
31         dynamic_cast<GLtk::Button *>(get_item(widgets, "btn_quit"))->signal_clicked.connect(sigc::mem_fun(this, &MainWindow::quit_clicked));
32         dynamic_cast<GLtk::Button *>(get_item(widgets, "btn_clock"))->signal_clicked.connect(sigc::mem_fun(this, &MainWindow::clock_clicked));
33
34         R2C2::Driver &driver = engineer.get_layout().get_driver();
35         if(driver.get_power())
36                 ind_on->set_active(true);
37         else
38                 ind_off->set_active(true);
39
40         driver.signal_power.connect(sigc::mem_fun(this, &MainWindow::power_event));
41         driver.signal_halt.connect(sigc::mem_fun(this, &MainWindow::halt_event));
42
43         engineer.get_layout().get_clock().signal_minute.connect(sigc::mem_fun(this, &MainWindow::update_clock));
44         engineer.get_layout().get_clock().signal_discontinuity.connect(sigc::mem_fun(this, &MainWindow::update_clock));
45 }
46
47 void MainWindow::set_status_text(const string &txt)
48 {
49         lbl_status->set_text(txt);
50 }
51
52 void MainWindow::on_clicked()
53 {
54         engineer.get_layout().get_driver().set_power(true);
55 }
56
57 void MainWindow::off_clicked()
58 {
59         engineer.get_layout().get_driver().set_power(false);
60 }
61
62 void MainWindow::halt_clicked()
63 {
64         R2C2::Driver &driver = engineer.get_layout().get_driver();
65         driver.halt(!driver.is_halted());
66 }
67
68 void MainWindow::trains_clicked()
69 {
70         TrainListDialog *dlg = new TrainListDialog(engineer);
71         find_ancestor<GLtk::Root>()->add(*dlg);
72 }
73
74 void MainWindow::telemetry_clicked()
75 {
76         TelemetryDialog *dlg = new TelemetryDialog(engineer);
77         find_ancestor<GLtk::Root>()->add(*dlg);
78 }
79
80 void MainWindow::quit_clicked()
81 {
82         engineer.quit();
83 }
84
85 void MainWindow::clock_clicked()
86 {
87         ClockDialog *dlg = new ClockDialog(engineer);
88         find_ancestor<GLtk::Root>()->add(*dlg);
89 }
90
91 void MainWindow::power_event(bool p)
92 {
93         ind_on->set_active(p);
94         ind_off->set_active(!p);
95 }
96
97 void MainWindow::halt_event(bool h)
98 {
99         ind_halt->set_active(h);
100 }
101
102 void MainWindow::update_clock()
103 {
104         const Time::TimeDelta &time = engineer.get_layout().get_clock().get_current_time();
105         unsigned minute = time/Time::min;
106         unsigned hour = minute/60;
107         minute %= 60;
108         lbl_clock->set_text(format("%02d:%02d", hour, minute));
109 }