1 #include <msp/core/maputils.h>
2 #include <msp/gltk/button.h>
3 #include <msp/gltk/style.h>
5 #include "libr2c2/train.h"
6 #include "newtraindialog.h"
7 #include "traindialog.h"
8 #include "trainlistdialog.h"
14 class TrainItem: public GLtk::List::MultiColumnItem
17 typedef R2C2::Train *ValueType;
23 class DetectedLocoItem: public GLtk::List::MultiColumnItem
26 typedef const Driver::DetectedLocomotive *ValueType;
28 DetectedLocoItem(ValueType);
32 TrainListDialog::TrainListDialog(Engineer &e):
33 DynamicDialog(e.get_user_interface()),
35 layout(engineer.get_layout())
37 Loader::WidgetMap widgets;
38 DataFile::load(*this, "data/trainlistdialog.ui", widgets);
40 lst_trains = dynamic_cast<GLtk::List *>(get_item(widgets, "lst_trains"));
41 lst_trains->set_data(trains);
42 lst_trains->set_item_type<TrainItem>();
44 lst_imported = dynamic_cast<GLtk::List *>(get_item(widgets, "lst_imported"));
45 lst_imported->set_data(detected_locos);
46 lst_imported->set_item_type<DetectedLocoItem>();
48 dynamic_cast<GLtk::Toggle *>(get_item(widgets, "tgl_trains"))->signal_toggled.connect(sigc::bind(sigc::mem_fun(this, &TrainListDialog::tab_toggled), lst_trains));
49 dynamic_cast<GLtk::Toggle *>(get_item(widgets, "tgl_import"))->signal_toggled.connect(sigc::bind(sigc::mem_fun(this, &TrainListDialog::tab_toggled), lst_imported));
51 dynamic_cast<GLtk::Button *>(get_item(widgets, "btn_new"))->signal_clicked.connect(sigc::mem_fun(this, &TrainListDialog::new_clicked));
52 dynamic_cast<GLtk::Button *>(get_item(widgets, "btn_show"))->signal_clicked.connect(sigc::mem_fun(this, &TrainListDialog::show_clicked));
54 const map<unsigned, Train *> <rains = layout.get_trains();
55 for(map<unsigned, Train *>::const_iterator i=ltrains.begin(); i!=ltrains.end(); ++i)
57 trains.append(i->second);
58 i->second->signal_name_changed.connect(sigc::hide(sigc::bind(sigc::mem_fun(this, &TrainListDialog::train_name_changed), sigc::ref(*i->second))));
61 if(layout.has_driver())
63 Driver &driver = layout.get_driver();
64 driver.signal_locomotive_detected.connect(sigc::mem_fun(this, &TrainListDialog::locomotive_detected));
65 driver.signal_locomotive_gone.connect(sigc::mem_fun(this, &TrainListDialog::locomotive_gone));
68 while(const Driver::DetectedLocomotive *loco = driver.enumerate_detected_locos(i++))
69 detected_locos.append(loco);
71 layout.signal_train_added.connect(sigc::mem_fun(this, &TrainListDialog::train_added));
72 layout.signal_train_removed.connect(sigc::mem_fun(this, &TrainListDialog::train_removed));
75 void TrainListDialog::new_clicked()
77 NewTrainDialog *dlg = new NewTrainDialog(engineer);
78 if(lst_imported->is_visible())
80 int selected = lst_imported->get_selected_index();
82 dlg->prefill(*detected_locos.get(selected));
84 find_ancestor<GLtk::Root>()->add(*dlg);
88 void TrainListDialog::show_clicked()
90 int index = lst_trains->get_selected_index();
93 TrainDialog *dlg = new TrainDialog(engineer, *trains.get(index));
94 find_ancestor<GLtk::Root>()->add(*dlg);
98 void TrainListDialog::tab_toggled(bool value, GLtk::Widget *wdg)
100 wdg->set_visible(value);
103 void TrainListDialog::locomotive_detected(const Driver::DetectedLocomotive &loco)
105 detected_locos.append(&loco);
108 void TrainListDialog::locomotive_gone(const Driver::DetectedLocomotive &loco)
110 int index = detected_locos.find(&loco);
112 detected_locos.remove(index);
115 void TrainListDialog::train_added(Train &train)
117 unsigned n_items = trains.size();
119 for(i=0; i<n_items; ++i)
120 if(trains.get(i)->get_address()>train.get_address())
122 trains.insert(i, &train);
124 train.signal_name_changed.connect(sigc::hide(sigc::bind(sigc::mem_fun(this, &TrainListDialog::train_name_changed), sigc::ref(train))));
127 void TrainListDialog::train_removed(Train &train)
129 int i = trains.find(&train);
134 void TrainListDialog::train_name_changed(Train &train)
136 trains.refresh(&train);
139 bool TrainListDialog::save_state(DataFile::Statement &st) const
141 st.keyword = "trainlistdialog";
142 save_position(st.sub);
148 TrainItem::TrainItem(ValueType train)
150 add(*new GLtk::Label(train->get_protocol()));
151 add(*new GLtk::Label(lexical_cast<string>(train->get_address())));
152 add(*new GLtk::Label(train->get_name()));
156 DetectedLocoItem::DetectedLocoItem(ValueType loco)
158 add(*new GLtk::Label(loco->protocol));
159 add(*new GLtk::Label(lexical_cast<string>(loco->address)));
160 add(*new GLtk::Label(loco->name));