00001 #include "MainWindow.h"
00002 #include "WiiuseMotionReceiver.h"
00003 #include "CWiidMotionReceiver.h"
00004 #include "RandomMotionReceiver.h"
00005 #include <iostream>
00006
00007 MainWindow::MainWindow(ConfigManager *cfgManager) :
00008 Gtk::Window(),
00009 m_pageNum(0),
00010 m_motionReceiver(NULL),
00011 m_classificationChain(NULL),
00012 m_receiverThread(NULL),
00013 m_btnQuit("_Quit", true),
00014 m_btnConnect("_Connect", true),
00015 m_lblWiiuse("No config for wiiuse"),
00016 m_lblCwiid("Wiimote MAC-address:"),
00017 m_lblRandomMotion("Receive random data"),
00018 m_boxWiiuse(false, 5),
00019 m_boxCwiid(false, 5),
00020 m_boxRandomMotion(false, 5),
00021 m_boxMain(false, 5),
00022 m_cfgManager(cfgManager)
00023 {
00024 if (!m_cfgManager->hasSection("Main Config")) {
00025 m_cfgManager->addEmptySection("Main Config");
00026 m_cfgManager->getSection("Main Config").setItem("motion receiver", "0");
00027 m_cfgManager->getSection("Main Config").setItem("wiimote bdaddr", "00:00:00:00:00:00");
00028 }
00029
00030 set_title("Modular Motion Processor");
00031 set_resizable(false);
00032
00033 m_btnQuit.signal_clicked().connect(sigc::mem_fun(this, &MainWindow::hide));
00034 m_btnConnect.signal_clicked().connect(sigc::mem_fun(this, &MainWindow::connect));
00035
00036 m_entWiimoteAddr.set_text(m_cfgManager->getSection("Main Config").get<std::string>("wiimote bdaddr"));
00037
00038 m_boxWiiuse.add(m_lblWiiuse);
00039 m_boxCwiid.add(m_lblCwiid);
00040 m_boxCwiid.add(m_entWiimoteAddr);
00041 m_boxRandomMotion.add(m_lblRandomMotion);
00042
00043 m_notebook.set_border_width(5);
00044 m_notebook.append_page(m_boxWiiuse, "Wiiuse interface");
00045 m_notebook.append_page(m_boxCwiid, "Cwiid interface");
00046 m_notebook.append_page(m_boxRandomMotion, "Random Motion interface");
00047 m_notebook.signal_switch_page().connect(sigc::mem_fun(*this,
00048 &MainWindow::onNotebookSwitchPage));
00049
00050 m_boxButtons.add(m_btnConnect);
00051 m_boxButtons.add(m_btnQuit);
00052
00053 m_boxMain.add(m_notebook);
00054 m_boxMain.add(m_boxButtons);
00055
00056 add(m_boxMain);
00057 show_all_children();
00058
00059 m_notebook.set_current_page(m_cfgManager->getSection("Main Config").get<int>("motion receiver"));
00060 }
00061
00062 MainWindow::~MainWindow()
00063 {
00064 if (m_receiverThread != NULL) {
00065 if (m_motionReceiver != NULL) {
00066 m_motionReceiver->stop();
00067 }
00068 m_receiverThread->join();
00069 }
00070 if (m_motionReceiver != NULL) {
00071 delete m_motionReceiver;
00072 m_motionReceiver = NULL;
00073 }
00074 if (m_classificationChain != NULL) {
00075 delete m_classificationChain;
00076 m_classificationChain = NULL;
00077 }
00078 }
00079
00080 void MainWindow::connect()
00081 {
00082 m_cfgManager->getSection("Main Config").setItem<int>("motion receiver", m_pageNum);
00083 m_cfgManager->getSection("Main Config").setItem("wiimote bdaddr", m_entWiimoteAddr.get_text());
00084
00085 try {
00086 if (m_pageNum == 0) {
00087 m_motionReceiver = new WiiuseMotionReceiver;
00088 } else if (m_pageNum == 1) {
00089 m_motionReceiver = new CWiidMotionReceiver;
00090 static_cast<CWiidMotionReceiver*>(m_motionReceiver)->setAddr(m_entWiimoteAddr.get_text().c_str());
00091 } else if (m_pageNum == 2) {
00092 m_motionReceiver = new RandomMotionReceiver;
00093 }
00094 m_motionReceiver->start();
00095 } catch (const Error &e) {
00096 Gtk::MessageDialog dlgError(*this, "Error while connecting:", false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
00097 dlgError.set_secondary_text(e.what());
00098 dlgError.run();
00099 if (m_motionReceiver != NULL) {
00100 delete m_motionReceiver;
00101 m_motionReceiver = NULL;
00102 }
00103 return;
00104 }
00105
00106 m_classificationChain = new ClassificationChain(m_motionReceiver, m_cfgManager);
00107
00108 m_receiverThread = Glib::Thread::create(sigc::mem_fun(*m_motionReceiver, &AbstractMotionReceiver::run), true);
00109 if (m_receiverThread == NULL) {
00110 Gtk::MessageDialog dlgError(*this, "Could not start motion receiving thread",
00111 false, Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK, true);
00112 dlgError.set_secondary_text("(and i dont know why, sorry)");
00113 dlgError.run();
00114 if (m_motionReceiver != NULL) {
00115 delete m_motionReceiver;
00116 m_motionReceiver = NULL;
00117 }
00118 delete m_classificationChain;
00119 m_classificationChain = NULL;
00120 }
00121
00122 m_btnConnect.hide();
00123 }
00124
00125 void MainWindow::onNotebookSwitchPage(GtkNotebookPage* page, guint page_num)
00126 {
00127 m_pageNum = page_num;
00128 }