main_window.cpp
Go to the documentation of this file.
00001 
00008 /*****************************************************************************
00009 ** Includes
00010 *****************************************************************************/
00011 
00012 #include <QtGui>
00013 #include <QMessageBox>
00014 #include <QTimer>
00015 #include <iostream>
00016 #include "../include/tibi_dabo_gui/main_window.hpp"
00017 #include <wiimote/State.h>
00018 
00019 /*****************************************************************************
00020 ** Namespaces
00021 *****************************************************************************/
00022 
00023 namespace tibi_dabo_gui {
00024 
00025 using namespace Qt;
00026 
00027 /*****************************************************************************
00028 ** Implementation [MainWindow]
00029 *****************************************************************************/
00030 
00031 MainWindow::MainWindow(int argc, char** argv, QWidget *parent)
00032   : QMainWindow(parent)
00033   , qnode(argc,argv)
00034 {
00035   ui.setupUi(this); // Calling this incidentally connects all ui's triggers to on_...() callbacks in this class.
00036   QObject::connect(ui.actionAbout_Qt, SIGNAL(triggered(bool)), qApp, SLOT(aboutQt())); // qApp is a global variable for the application
00037   ReadSettings();
00038   setWindowIcon(QIcon(":/images/logos/iri.png"));
00039   ui.tab_manager->setCurrentIndex(0); // ensure the first tab is showing - qt-designer should have this already hardwired, but often loses it (settings?).
00040   QObject::connect(&qnode, SIGNAL(rosShutdown()), this, SLOT(close()));
00041   QObject::connect(ui.tab_manager, SIGNAL(currentChanged(int)), this, SLOT(changedTab()));
00042 
00043   QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF-8"));
00044   this->playImage = true;
00045 
00046   this->loadTexts();
00047 
00048   /*********************
00049   ** Auto Start
00050   **********************/
00051   qnode.initialize();
00052   this->setLanguage(1); //English
00053 
00054   //TODO: switch blank/arrow cursor with param?
00055   qApp->setOverrideCursor(QCursor(Qt::BlankCursor));
00056   //qApp->setOverrideCursor(QCursor(Qt::ArrowCursor));
00057 
00058   QTimer *timer = new QTimer(this);
00059   connect(timer, SIGNAL(timeout()), this, SLOT(timerDone()));
00060   timer->start(100);
00061 }
00062 
00063 MainWindow::~MainWindow() {}
00064 
00065 /*****************************************************************************
00066 ** Implementation [Slots]
00067 *****************************************************************************/
00068 
00069 /*
00070  * These triggers whenever the button is clicked, regardless of whether it
00071  * is already checked or not.
00072  */
00073 
00074 // void MainWindow::on_button_home_clicked(bool check)
00075 // {
00076 //   qnode.buttonPressed(wiimote::State::MSG_BTN_HOME);
00077 // }
00078 
00079 
00080 void MainWindow::on_button_question_0_clicked(bool check)
00081 {
00082   std::string answer;
00083   answer = qnode.processAnswer(0);
00084   QString q = answer.c_str();
00085   ui.label_answer_text->setText(q);
00086 }
00087 
00088 void MainWindow::on_button_question_1_clicked(bool check)
00089 {
00090   std::string answer;
00091   answer = qnode.processAnswer(1);
00092   QString q = answer.c_str();
00093   ui.label_answer_text->setText(q);
00094 }
00095 
00096 void MainWindow::on_button_question_2_clicked(bool check)
00097 {
00098   std::string answer;
00099   answer = qnode.processAnswer(2);
00100   QString q = answer.c_str();
00101   ui.label_answer_text->setText(q);
00102 }
00103 
00104 void MainWindow::on_button_question_3_clicked(bool check)
00105 {
00106   std::string answer;
00107   answer = qnode.processAnswer(3);
00108   QString q = answer.c_str();
00109   ui.label_answer_text->setText(q);
00110 }
00111 
00112 void MainWindow::on_button_question_4_clicked(bool check)
00113 {
00114   std::string answer;
00115   answer = qnode.processAnswer(4);
00116   QString q = answer.c_str();
00117   ui.label_answer_text->setText(q);
00118 }
00119 
00120 void MainWindow::on_button_question_5_clicked(bool check)
00121 {
00122   std::string answer;
00123   answer = qnode.processAnswer(5);
00124   QString q = answer.c_str();
00125   ui.label_answer_text->setText(q);
00126 }
00127 
00128 void MainWindow::on_button_cat_clicked(bool check)
00129 {
00130   this->setLanguage(0);
00131 }
00132 
00133 void MainWindow::on_button_eng_clicked(bool check)
00134 {
00135   this->setLanguage(1);
00136 }
00137 
00138 void MainWindow::on_button_esp_clicked(bool check)
00139 {
00140   this->setLanguage(2);
00141 }
00142 
00143 void MainWindow::on_button_pause_clicked(bool check)
00144 {
00145   if(this->playImage)
00146   {
00147     //ui.button_pause->setText("PLAY");
00148     ui.button_pause->setIcon(QIcon(":/images/icons/play.png"));
00149     this->playImage = false;
00150   }
00151   else
00152   {
00153     //ui.button_pause->setText("PAUSE");
00154     ui.button_pause->setIcon(QIcon(":/images/icons/pause.png"));
00155     this->playImage = true;
00156   }
00157 }
00158 
00159 void MainWindow::on_button_yes_clicked(bool check)
00160 {
00161   qnode.haodButtonPressed(wiimote::State::MSG_BTN_1);
00162 }
00163 
00164 void MainWindow::on_button_no_clicked(bool check)
00165 {
00166   qnode.haodButtonPressed(wiimote::State::MSG_BTN_2);
00167 }
00168 
00169 void MainWindow::on_button_help_clicked(bool check)
00170 {
00171   qnode.haodButtonPressed(wiimote::State::MSG_BTN_HOME);
00172 }
00173 
00174 void MainWindow::on_button_stop_clicked(bool check)
00175 {
00176   qnode.haodButtonPressed(wiimote::State::MSG_BTN_B);
00177   ui.button_yes->setEnabled(false);
00178   ui.button_no->setEnabled(false);
00179   ui.button_stop->setEnabled(false);
00180   ui.button_help->setEnabled(true);
00181 }
00182 
00183 void MainWindow::mousePressEvent(QMouseEvent *event)
00184 {
00185   if(ui.tab_manager->currentIndex()==0 && !qnode.haodRunning())
00186   {
00187     //from global (main_window) to local (label_image widget)
00188     QPoint mappedPos = ui.label_image->mapFrom(this, event->pos());
00189     //if left click and coordinates inside image
00190     //label can have different size as image
00191     int x = mappedPos.x() - (ui.label_image->width()  - this->image_width)/2;
00192     int y = mappedPos.y() - (ui.label_image->height() - this->image_height)/2;
00193     if((Qt::LeftButton == event->button()) && 
00194         x>0 && x<this->image_width && 
00195         y>0 && y<this->image_height)
00196     {
00197       std::vector<int> point(2);
00198       point[0] = (x*this->image_width_original)/this->image_width;
00199       point[1] = (y*this->image_height_original)/this->image_height;
00200       qnode.setImagePoint(point);
00201       ui.button_stop->setEnabled(true);
00202       ui.button_help->setEnabled(false);
00203     }
00204   }
00205 }
00206 
00207 void MainWindow::changedTab()
00208 {
00209   if(ui.tab_manager->currentIndex()!=0)
00210   {
00211     qnode.haodButtonPressed(wiimote::State::MSG_BTN_MINUS);
00212     ui.button_yes->setEnabled(false);
00213     ui.button_no->setEnabled(false);
00214     ui.button_stop->setEnabled(false);
00215     ui.button_help->setEnabled(true);
00216   }
00217   else if(ui.tab_manager->currentIndex()!=1)
00218   {
00219     qnode.cancelHri();
00220   }
00221 }
00222 
00223 // void MainWindow::imageLabelClicked(QMouseEvent *event)
00224 // {
00225 //   std::cout << "imageLabelClicked SLOT" << std::endl;
00226 // }
00227 
00228 /*****************************************************************************
00229 ** Implemenation [Slots][manually connected]
00230 *****************************************************************************/
00231 void MainWindow::timerDone()
00232 {
00233   this->loadImage();
00234   this->checkAssistance();
00235 }
00236 
00237 void MainWindow::checkAssistance()
00238 {
00239   if(qnode.needAssistance() && qnode.haodRunning())
00240   {
00241     //activate yes/no buttons when assistance is needed
00242     std::cout << "MainWindow::checkAssistance:: enabling buttons yes/no" << std::endl;
00243     ui.button_yes->setEnabled(true);
00244     ui.button_no->setEnabled(true);
00245   }
00246   else
00247   {
00248     ui.button_yes->setEnabled(false);
00249     ui.button_no->setEnabled(false);
00250   }
00251 }
00252 
00253 void MainWindow::loadImage()
00254 {
00255   if(ui.tab_manager->currentIndex()==0 && this->playImage) //Image tab
00256   {
00257     uchar* data;
00258     int width;
00259     int height;
00260     std::string encoding;
00261     bool ok;
00262     if(!qnode.haodRunning())
00263       ok=qnode.getImage(data, width, height, encoding);
00264     else
00265       ok=qnode.getImageDetect(data,width,height,encoding);
00266 
00267     if(ok)
00268     {
00269       QImage::Format format;
00270       bool format_ok=false;
00271       if(encoding=="rgb8")
00272       {
00273         format=QImage::Format_RGB888;
00274         format_ok=true;
00275       }
00276       else if(encoding=="mono8")
00277       {
00278         format=QImage::Format_Mono;
00279         format_ok=true;
00280       }
00281       else if(encoding=="bgr8")
00282       {
00283         format=QImage::Format_RGB888;
00284         format_ok=true;
00285       }
00286       else
00287       {
00288 //        std::cout << "format not expexted:" << encoding << std::endl;
00289       }
00290       if(format_ok)
00291       {
00292         QImage myImage(data, width, height, format);
00293         if(encoding=="bgr8")
00294         {
00295           QImage swapImg = myImage.rgbSwapped();
00296           myImage=swapImg;
00297         }
00298         //QPixmap p(QPixmap::fromImage(myImage));
00299 
00300         if(!qnode.haodRunning())
00301         {
00302           //draw face rectangles on qimage
00303           QPainter qPainter(&myImage);
00304           QPen pen(Qt::blue);
00305           pen.setWidth(3);
00306           qPainter.setBrush(Qt::NoBrush);
00307           qPainter.setPen(pen);
00308           std::vector<std::vector<int> > rects = qnode.getRects();
00309           for(unsigned int i=0; i<rects.size(); i++)
00310           {
00311             int w=rects[i][2];
00312             int h=rects[i][3];
00313             int x=rects[i][0]-w/2;
00314             int y=rects[i][1]-h/2;
00315             qPainter.drawRect(x,y,w,h);
00316           }
00317         }
00318         
00319         QPixmap p(QPixmap::fromImage(myImage));
00320         
00321         //original image size
00322         this->image_width_original = p.width();
00323         this->image_height_original = p.height();
00324         QPixmap p2= p.scaled(ui.label_image->width(),ui.label_image->height(),Qt::KeepAspectRatio);
00325         //scaled image size
00326         this->image_width  = p2.width();
00327         this->image_height = p2.height();
00328         ui.label_image->setPixmap(p2);
00329         ui.label_image->setScaledContents(false);
00330         //ui.label_image->adjustSize();
00331       }
00332     }
00333   }
00334 }
00335 
00336 void MainWindow::setLanguage(int index)
00337 {
00338   if(index!=this->lastLanguage)
00339   {
00340     for(unsigned int i=0; i<this->tabs.size(); i++)
00341     {
00342       ui.tab_manager->setTabText(i, this->tabs[i][index].c_str());
00343     }
00344     ui.button_question_0->setText(qnode.qas[0].question[index].c_str());
00345     ui.button_question_1->setText(qnode.qas[1].question[index].c_str());
00346     ui.button_question_2->setText(qnode.qas[2].question[index].c_str());
00347     ui.button_question_3->setText(qnode.qas[3].question[index].c_str());
00348     ui.button_question_4->setText(qnode.qas[4].question[index].c_str());
00349     ui.button_question_5->setText(qnode.qas[5].question[index].c_str());
00350     ui.label_answer_text->setText("");
00351     ui.label_info_title->setText(this->info[0][index].c_str());
00352     ui.label_info_1->setText(this->info[1][index].c_str());
00353     ui.label_info_2->setText(this->info[2][index].c_str());
00354     ui.label_info_3->setText(this->info[3][index].c_str());
00355     ui.label_info_4->setText(this->info[4][index].c_str());
00356     ui.label_info_5->setText(this->info[5][index].c_str());
00357     ui.label_info_title_2->setText(this->info[6][index].c_str());
00358     ui.label_info_6->setText(this->info[7][index].c_str());
00359     ui.label_info_7->setText(this->info[8][index].c_str());
00360     ui.label_info_8->setText(this->info[9][index].c_str());
00361     ui.label_info_9->setText(this->info[10][index].c_str());
00362     ui.label_info_10->setText(this->info[11][index].c_str());
00363 
00364     if(index==0)
00365     {
00366       std::cout << "Setting language to Catalan" << std::endl;
00367       ui.button_cat->setIcon(QIcon(":/images/lang/cat.png"));
00368       ui.button_eng->setIcon(QIcon(":/images/lang/eng_bw.png"));
00369       ui.button_esp->setIcon(QIcon(":/images/lang/esp_bw.png"));
00370     }
00371     else if(index==1)
00372     {
00373       std::cout << "Setting language to English" << std::endl;
00374       ui.button_cat->setIcon(QIcon(":/images/lang/cat_bw.png"));
00375       ui.button_eng->setIcon(QIcon(":/images/lang/eng.png"));
00376       ui.button_esp->setIcon(QIcon(":/images/lang/esp_bw.png"));
00377     }
00378     else if(index==2)
00379     {
00380       std::cout << "Setting language to Spanish" << std::endl;
00381       ui.button_cat->setIcon(QIcon(":/images/lang/cat_bw.png"));
00382       ui.button_eng->setIcon(QIcon(":/images/lang/eng_bw.png"));
00383       ui.button_esp->setIcon(QIcon(":/images/lang/esp.png"));
00384     }
00385 
00386     qnode.setLanguage(index);
00387     this->lastLanguage=index;
00388   }
00389 }
00390 
00391 void MainWindow::loadTexts()
00392 {
00393   std::vector<std::string> tab;
00394   tab.push_back("Càmara");
00395   tab.push_back("Camera");
00396   tab.push_back("Cámara");
00397   this->tabs.push_back(tab);
00398   tab.clear();
00399   tab.push_back("Preguntes");
00400   tab.push_back("Questions");
00401   tab.push_back("Preguntas");
00402   this->tabs.push_back(tab);
00403   tab.clear();
00404   tab.push_back("Info");
00405   tab.push_back("Info");
00406   tab.push_back("Info");
00407   this->tabs.push_back(tab);
00408   tab.clear();
00409   tab.push_back("Sobre");
00410   tab.push_back("About");
00411   tab.push_back("Acerca de");
00412   this->tabs.push_back(tab);
00413   tab.clear();
00414   
00415   std::vector<std::string> text;
00416   text.push_back("<b>Tibi i Dabo</b>");
00417   text.push_back("<b>Tibi and Dabo<b>");
00418   text.push_back("<b>Tibi y Dabo<b>");
00419   this->info.push_back(text);
00420   text.clear();
00421   text.push_back("- Robots socials i urbans.");
00422   text.push_back("- Social and urban robots.");
00423   text.push_back("- Robots sociales y urbanos");
00424   this->info.push_back(text);
00425   text.clear();
00426   text.push_back("- Plataforma <i><b>Segway rmp200</i></b> per navegació en entorns urbans.");
00427   text.push_back("- <i><b>Segway rmp200</i></b> platform for navigation in urban environments.");
00428   text.push_back("- Plataforma <i><b>Segway rmp200</i></b> para navegación en entornos urbanos.");
00429   this->info.push_back(text);
00430   text.clear();
00431   text.push_back("- Sensors de rang làser <i><b>Hokuyo</i></b> per localització i detecció d'obstacles.");
00432   text.push_back("- <i><b>Hokuyo</i></b> range laser sensors for localization and obstacle detection.");
00433   text.push_back("- Sensores de rango láser <i><b>Hokuyo</i></b> para localización y detección de obstáculos.");
00434   this->info.push_back(text);
00435   text.clear();
00436   text.push_back("- Càmara estèreo <i><b>Bumblebee2</i></b> per visió per computador.");
00437   text.push_back("- <i><b>Bumblebee2</i></b> Stereo camera for computer visión.");
00438   text.push_back("- Cámara estéreo <i><b>Bumblebee2</i></b> para visión por computador.");
00439   this->info.push_back(text);
00440   text.clear();
00441   text.push_back("- Cap, braços, pantalla tàctil i parla per la interacció humà robot");
00442   text.push_back("- Head, arms, touch screen and speech for human robot interaction.");
00443   text.push_back("- Cabeza, brazos, pantalla táctil y habla para la interacción humano robot.");
00444   this->info.push_back(text);
00445   text.clear();
00446   text.push_back("<b>Cotxe</b>");
00447   text.push_back("<b>Car</b>");
00448   text.push_back("<b>Coche</b>");
00449   this->info.push_back(text);
00450   text.clear();
00451   text.push_back("- Vehicle de transport autònom.");
00452   text.push_back("- Autonomous transport vehicle.");
00453   text.push_back("- Vehículo de transporte autónomo.");
00454   this->info.push_back(text);
00455   text.clear();
00456   text.push_back("- Plataforma Ackerman tipus cotxe.");
00457   text.push_back("- Ackerman car-like platform.");
00458   text.push_back("- Plataforma Ackerman tipo coche");
00459   this->info.push_back(text);
00460   text.clear();
00461   text.push_back("- Sensors de rang làser: parada de seguretat, detecció d'obstacles i localització.");
00462   text.push_back("- Range laser sensors: security stop, obstacle detection and localization.");
00463   text.push_back("- Sensores de rango láser para parada de seguridad, detección de obstáculos y localización.");
00464   this->info.push_back(text);
00465   text.clear();
00466   text.push_back("- Làser 3D Velodyne i Càmara 360º Ladybug: percepció de l'entorn.");
00467   text.push_back("- 3D laser Velodyne and 360 camera Ladybug: environment perception.");
00468   text.push_back("- Láser 3D Velodyne i cámara 360º Ladybug: percepción del entorno.");
00469   this->info.push_back(text);
00470   text.clear();
00471   text.push_back("- Pantalla tàctil: interacció amb passatgers.");
00472   text.push_back("- Touchscreen: passengers interaction.");
00473   text.push_back("- Pantalla táctil: interacción con pasajeros.");
00474   this->info.push_back(text);
00475   text.clear();
00476 }
00477 
00478 /*****************************************************************************
00479 ** Implementation [Menu]
00480 *****************************************************************************/
00481 
00482 void MainWindow::on_actionAbout_triggered() {
00483     QMessageBox::about(this, tr("About ..."),tr("<h2>Tibi Dabo Gui</h2><p>Copyright IRI</p><p>Graphic User Interface for Tibi Dabo robots.</p>"));
00484 }
00485 
00486 /*****************************************************************************
00487 ** Implementation [Configuration]
00488 *****************************************************************************/
00489 
00490 void MainWindow::ReadSettings() {
00491     QSettings settings("Qt-Ros Package", "tibi_dabo_gui");
00492     restoreGeometry(settings.value("geometry").toByteArray());
00493     restoreState(settings.value("windowState").toByteArray());
00494 }
00495 
00496 void MainWindow::WriteSettings() {
00497     QSettings settings("Qt-Ros Package", "tibi_dabo_gui");
00498     settings.setValue("geometry", saveGeometry());
00499     settings.setValue("windowState", saveState());
00500 }
00501 
00502 void MainWindow::closeEvent(QCloseEvent *event)
00503 {
00504         WriteSettings();
00505         QMainWindow::closeEvent(event);
00506 }
00507 
00508 }  // namespace tibi_dabo_gui
00509 


tibi_dabo_gui
Author(s): fherrero
autogenerated on Fri Dec 6 2013 22:01:34