]> git.tdb.fi Git - r2c2.git/blob - source/engineer/mainwindow.cpp
Clear destination when setting a null route
[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 "engineer.h"
6 #include "mainwindow.h"
7 #include "trainlistdialog.h"
8
9 using namespace std;
10 using namespace Msp;
11
12 MainWindow::MainWindow(Engineer &e):
13         engineer(e)
14 {
15         Loader::WidgetMap widgets;
16         DataFile::load(*this, "data/mainwindow.ui", widgets);
17
18         ind_on = dynamic_cast<GLtk::Indicator *>(get_item(widgets, "ind_on"));
19         ind_off = dynamic_cast<GLtk::Indicator *>(get_item(widgets, "ind_off"));
20         ind_halt = dynamic_cast<GLtk::Indicator *>(get_item(widgets, "ind_halt"));
21         lbl_status = dynamic_cast<GLtk::Label *>(get_item(widgets, "lbl_status"));
22         lbl_clock = dynamic_cast<GLtk::Label *>(get_item(widgets, "lbl_clock"));
23
24         dynamic_cast<GLtk::Button *>(get_item(widgets, "btn_on"))->signal_clicked.connect(sigc::mem_fun(this, &MainWindow::on_clicked));
25         dynamic_cast<GLtk::Button *>(get_item(widgets, "btn_off"))->signal_clicked.connect(sigc::mem_fun(this, &MainWindow::off_clicked));
26         dynamic_cast<GLtk::Button *>(get_item(widgets, "btn_halt"))->signal_clicked.connect(sigc::mem_fun(this, &MainWindow::halt_clicked));
27         dynamic_cast<GLtk::Button *>(get_item(widgets, "btn_trains"))->signal_clicked.connect(sigc::mem_fun(this, &MainWindow::trains_clicked));
28         dynamic_cast<GLtk::Button *>(get_item(widgets, "btn_quit"))->signal_clicked.connect(sigc::mem_fun(this, &MainWindow::quit_clicked));
29
30         R2C2::Driver &driver = engineer.get_layout().get_driver();
31         if(driver.get_power())
32                 ind_on->set_active(true);
33         else
34                 ind_off->set_active(true);
35
36         driver.signal_power.connect(sigc::mem_fun(this, &MainWindow::power_event));
37         driver.signal_halt.connect(sigc::mem_fun(this, &MainWindow::halt_event));
38
39         engineer.get_layout().get_clock().signal_minute.connect(sigc::mem_fun(this, &MainWindow::clock_minute));
40 }
41
42 void MainWindow::set_status_text(const string &txt)
43 {
44         lbl_status->set_text(txt);
45 }
46
47 void MainWindow::on_clicked()
48 {
49         engineer.get_layout().get_driver().set_power(true);
50 }
51
52 void MainWindow::off_clicked()
53 {
54         engineer.get_layout().get_driver().set_power(false);
55 }
56
57 void MainWindow::halt_clicked()
58 {
59         R2C2::Driver &driver = engineer.get_layout().get_driver();
60         driver.halt(!driver.is_halted());
61 }
62
63 void MainWindow::trains_clicked()
64 {
65         TrainListDialog *dlg = new TrainListDialog(engineer);
66         find_ancestor<GLtk::Root>()->add(*dlg);
67 }
68
69 void MainWindow::quit_clicked()
70 {
71         engineer.quit();
72 }
73
74 void MainWindow::power_event(bool p)
75 {
76         ind_on->set_active(p);
77         ind_off->set_active(!p);
78 }
79
80 void MainWindow::halt_event(bool h)
81 {
82         ind_halt->set_active(h);
83 }
84
85 void MainWindow::clock_minute()
86 {
87         const Time::TimeDelta &time = engineer.get_layout().get_clock().get_current_time();
88         unsigned minute = time/Time::min;
89         unsigned hour = minute/60;
90         minute %= 60;
91         lbl_clock->set_text(format("%02d:%02d", hour, minute));
92 }