]> git.tdb.fi Git - r2c2.git/blobdiff - source/engineer/trainlistdialog.cpp
Complete rewrite of the engineer UI
[r2c2.git] / source / engineer / trainlistdialog.cpp
diff --git a/source/engineer/trainlistdialog.cpp b/source/engineer/trainlistdialog.cpp
new file mode 100644 (file)
index 0000000..0a31d67
--- /dev/null
@@ -0,0 +1,98 @@
+#include <msp/core/maputils.h>
+#include <msp/gltk/button.h>
+#include <msp/gltk/style.h>
+#include "engineer.h"
+#include "libr2c2/train.h"
+#include "newtraindialog.h"
+#include "traindialog.h"
+#include "trainlistdialog.h"
+
+using namespace std;
+using namespace Msp;
+using namespace R2C2;
+
+class TrainItem: public GLtk::List::Item
+{
+private:
+       GLtk::Label address;
+       GLtk::Label name;
+
+public:        
+       typedef R2C2::Train *ValueType;
+
+       TrainItem(ValueType);
+
+private:
+       virtual void on_style_change();
+};
+
+
+TrainListDialog::TrainListDialog(Engineer &e):
+       engineer(e),
+       layout(engineer.get_layout())
+{
+       Loader::WidgetMap widgets;
+       DataFile::load(*this, "data/trainlistdialog.ui", widgets);
+
+       lst_trains = dynamic_cast<GLtk::List *>(get_item(widgets, "lst_trains"));
+       lst_trains->set_data(trains);
+       lst_trains->set_item_type<TrainItem>();
+
+       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));
+
+       const map<unsigned, Train *> &ltrains = layout.get_trains();
+       for(map<unsigned, Train *>::const_iterator i=ltrains.begin(); i!=ltrains.end(); ++i)
+               trains.append(i->second);
+}
+
+void TrainListDialog::new_clicked()
+{
+       GLtk::Container *root = parent;
+       while(root->get_parent())
+               root = root->get_parent();
+
+       NewTrainDialog *dlg = new NewTrainDialog(engineer);
+       root->add(*dlg);
+       dlg->autosize();
+}
+
+void TrainListDialog::show_clicked()
+{
+       int index = lst_trains->get_selected_index();
+       if(index>=0)
+       {
+               GLtk::Container *root = parent;
+               while(root->get_parent())
+                       root = root->get_parent();
+
+               TrainDialog *dlg = new TrainDialog(engineer, *trains.get(index));
+               root->add(*dlg);
+               dlg->autosize();
+       }
+}
+
+
+TrainItem::TrainItem(ValueType train):
+       address(lexical_cast<string>(train->get_address())),
+       name(train->get_name())
+{
+       add(address);
+       add(name);
+}
+
+void TrainItem::on_style_change()
+{
+       if(!style)
+               return;
+
+       address.autosize();
+       name.autosize();
+
+       if(const GLtk::Part *part = style->get_part("children"))
+       {
+               const GLtk::Sides &margin = part->get_margin();
+               address.set_position(margin.left, margin.bottom);
+               name.set_position(margin.left+30, margin.bottom);
+       }
+}