]> git.tdb.fi Git - r2c2.git/blobdiff - source/engineer/trainlistdialog.cpp
Persist most dialogs across runs
[r2c2.git] / source / engineer / trainlistdialog.cpp
index fccfbfb645288c15deb0117efabcadca8a9d9e1f..5e3c50b6873e827c0d3ba8b77ce14104e99e6ede 100644 (file)
@@ -11,24 +11,26 @@ using namespace std;
 using namespace Msp;
 using namespace R2C2;
 
-class TrainItem: public GLtk::List::Item
+class TrainItem: public GLtk::List::MultiColumnItem
 {
-private:
-       GLtk::Label protocol;
-       GLtk::Label address;
-       GLtk::Label name;
-
-public:        
+public:
        typedef R2C2::Train *ValueType;
 
        TrainItem(ValueType);
+};
+
+
+class DetectedLocoItem: public GLtk::List::MultiColumnItem
+{
+public:
+       typedef const Driver::DetectedLocomotive *ValueType;
 
-private:
-       virtual void on_style_change();
+       DetectedLocoItem(ValueType);
 };
 
 
 TrainListDialog::TrainListDialog(Engineer &e):
+       DynamicDialog(e.get_user_interface()),
        engineer(e),
        layout(engineer.get_layout())
 {
@@ -39,6 +41,13 @@ TrainListDialog::TrainListDialog(Engineer &e):
        lst_trains->set_data(trains);
        lst_trains->set_item_type<TrainItem>();
 
+       lst_imported = dynamic_cast<GLtk::List *>(get_item(widgets, "lst_imported"));
+       lst_imported->set_data(detected_locos);
+       lst_imported->set_item_type<DetectedLocoItem>();
+
+       dynamic_cast<GLtk::Toggle *>(get_item(widgets, "tgl_trains"))->signal_toggled.connect(sigc::bind(sigc::mem_fun(this, &TrainListDialog::tab_toggled), lst_trains));
+       dynamic_cast<GLtk::Toggle *>(get_item(widgets, "tgl_import"))->signal_toggled.connect(sigc::bind(sigc::mem_fun(this, &TrainListDialog::tab_toggled), lst_imported));
+
        dynamic_cast<GLtk::Button *>(get_item(widgets, "btn_new"))->signal_clicked.connect(sigc::mem_fun(this, &TrainListDialog::new_clicked));
        dynamic_cast<GLtk::Button *>(get_item(widgets, "btn_show"))->signal_clicked.connect(sigc::mem_fun(this, &TrainListDialog::show_clicked));
 
@@ -49,6 +58,16 @@ TrainListDialog::TrainListDialog(Engineer &e):
                i->second->signal_name_changed.connect(sigc::hide(sigc::bind(sigc::mem_fun(this, &TrainListDialog::train_name_changed), sigc::ref(*i->second))));
        }
 
+       if(layout.has_driver())
+       {
+               Driver &driver = layout.get_driver();
+               driver.signal_locomotive_detected.connect(sigc::mem_fun(this, &TrainListDialog::locomotive_detected));
+               driver.signal_locomotive_gone.connect(sigc::mem_fun(this, &TrainListDialog::locomotive_gone));
+
+               unsigned i = 0;
+               while(const Driver::DetectedLocomotive *loco = driver.enumerate_detected_locos(i++))
+                       detected_locos.append(loco);
+       }
        layout.signal_train_added.connect(sigc::mem_fun(this, &TrainListDialog::train_added));
        layout.signal_train_removed.connect(sigc::mem_fun(this, &TrainListDialog::train_removed));
 }
@@ -56,6 +75,12 @@ TrainListDialog::TrainListDialog(Engineer &e):
 void TrainListDialog::new_clicked()
 {
        NewTrainDialog *dlg = new NewTrainDialog(engineer);
+       if(lst_imported->is_visible())
+       {
+               int selected = lst_imported->get_selected_index();
+               if(selected>=0)
+                       dlg->prefill(*detected_locos.get(selected));
+       }
        find_ancestor<GLtk::Root>()->add(*dlg);
        dlg->autosize();
 }
@@ -70,6 +95,23 @@ void TrainListDialog::show_clicked()
        }
 }
 
+void TrainListDialog::tab_toggled(bool value, GLtk::Widget *wdg)
+{
+       wdg->set_visible(value);
+}
+
+void TrainListDialog::locomotive_detected(const Driver::DetectedLocomotive &loco)
+{
+       detected_locos.append(&loco);
+}
+
+void TrainListDialog::locomotive_gone(const Driver::DetectedLocomotive &loco)
+{
+       int index = detected_locos.find(&loco);
+       if(index>=0)
+               detected_locos.remove(index);
+}
+
 void TrainListDialog::train_added(Train &train)
 {
        unsigned n_items = trains.size();
@@ -94,31 +136,26 @@ void TrainListDialog::train_name_changed(Train &train)
        trains.refresh(&train);
 }
 
-
-TrainItem::TrainItem(ValueType train):
-       protocol(train->get_protocol()),
-       address(lexical_cast<string>(train->get_address())),
-       name(train->get_name())
+bool TrainListDialog::save_state(DataFile::Statement &st) const
 {
-       add(protocol);
-       add(address);
-       add(name);
+       st.keyword = "trainlistdialog";
+       save_position(st.sub);
+
+       return true;
 }
 
-void TrainItem::on_style_change()
+
+TrainItem::TrainItem(ValueType train)
 {
-       if(!style)
-               return;
+       add(*new GLtk::Label(train->get_protocol()));
+       add(*new GLtk::Label(lexical_cast<string>(train->get_address())));
+       add(*new GLtk::Label(train->get_name()));
+}
 
-       protocol.autosize();
-       address.autosize();
-       name.autosize();
 
-       if(const GLtk::Part *part = style->get_part("children"))
-       {
-               const GLtk::Sides &margin = part->get_margin();
-               protocol.set_position(margin.left, margin.bottom);
-               address.set_position(margin.left+30, margin.bottom);
-               name.set_position(margin.left+60, margin.bottom);
-       }
+DetectedLocoItem::DetectedLocoItem(ValueType loco)
+{
+       add(*new GLtk::Label(loco->protocol));
+       add(*new GLtk::Label(lexical_cast<string>(loco->address)));
+       add(*new GLtk::Label(loco->name));
 }