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"
7 #include "mainwindow.h"
8 #include "telemetrydialog.h"
9 #include "trainlistdialog.h"
14 MainWindow::MainWindow(Engineer &e):
17 Loader::WidgetMap widgets;
18 DataFile::load(*this, "data/mainwindow.ui", widgets);
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"));
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));
34 R2C2::Driver &driver = engineer.get_layout().get_driver();
35 if(driver.get_power())
36 ind_on->set_active(true);
38 ind_off->set_active(true);
40 driver.signal_power.connect(sigc::mem_fun(this, &MainWindow::power_event));
41 driver.signal_halt.connect(sigc::mem_fun(this, &MainWindow::halt_event));
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));
47 void MainWindow::set_status_text(const string &txt)
49 lbl_status->set_text(txt);
52 void MainWindow::on_clicked()
54 engineer.get_layout().get_driver().set_power(true);
57 void MainWindow::off_clicked()
59 engineer.get_layout().get_driver().set_power(false);
62 void MainWindow::halt_clicked()
64 R2C2::Driver &driver = engineer.get_layout().get_driver();
65 driver.halt(!driver.is_halted());
68 void MainWindow::trains_clicked()
70 TrainListDialog *dlg = new TrainListDialog(engineer);
71 find_ancestor<GLtk::Root>()->add(*dlg);
74 void MainWindow::telemetry_clicked()
76 TelemetryDialog *dlg = new TelemetryDialog(engineer);
77 find_ancestor<GLtk::Root>()->add(*dlg);
80 void MainWindow::quit_clicked()
85 void MainWindow::clock_clicked()
87 ClockDialog *dlg = new ClockDialog(engineer);
88 find_ancestor<GLtk::Root>()->add(*dlg);
91 void MainWindow::power_event(bool p)
93 ind_on->set_active(p);
94 ind_off->set_active(!p);
97 void MainWindow::halt_event(bool h)
99 ind_halt->set_active(h);
102 void MainWindow::update_clock()
104 const Time::TimeDelta &time = engineer.get_layout().get_clock().get_current_time();
105 unsigned minute = time/Time::min;
106 unsigned hour = minute/60;
108 lbl_clock->set_text(format("%02d:%02d", hour, minute));