main_window_ecmr.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_ecmr.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 //   //load ECMR Program image into Program tab
00047 //   QGraphicsScene *scene = new QGraphicsScene();
00048 //   QPixmap m(":/images/ecmr/program.png");
00049 //   scene->addPixmap(m.scaled(902,754,Qt::KeepAspectRatio,Qt::SmoothTransformation));
00050 //   ui.graphicsView_program->setScene(scene);
00051 
00052   this->loadTexts();
00053 
00054   /*********************
00055   ** Auto Start
00056   **********************/
00057   qnode.initialize();
00058   this->setLanguage(1); //English
00059 
00060   //TODO: switch blank/arrow cursor with param?
00061   //qApp->setOverrideCursor(QCursor(Qt::BlankCursor));
00062   qApp->setOverrideCursor(QCursor(Qt::ArrowCursor));
00063 
00064   QTimer *timer = new QTimer(this);
00065   connect(timer, SIGNAL(timeout()), this, SLOT(timerDone()));
00066   timer->start(100);
00067 }
00068 
00069 MainWindow::~MainWindow() {}
00070 
00071 /*****************************************************************************
00072 ** Implementation [Slots]
00073 *****************************************************************************/
00074 
00075 /*
00076  * These triggers whenever the button is clicked, regardless of whether it
00077  * is already checked or not.
00078  */
00079 
00080 // void MainWindow::on_button_home_clicked(bool check)
00081 // {
00082 //   qnode.buttonPressed(wiimote::State::MSG_BTN_HOME);
00083 // }
00084 
00085 
00086 void MainWindow::on_button_question_0_clicked(bool check)
00087 {
00088   std::string answer;
00089   answer = qnode.processAnswer(0);
00090   QString q = answer.c_str();
00091   ui.label_answer_text->setText(q);
00092 }
00093 
00094 void MainWindow::on_button_question_1_clicked(bool check)
00095 {
00096   std::string answer;
00097   answer = qnode.processAnswer(1);
00098   QString q = answer.c_str();
00099   ui.label_answer_text->setText(q);
00100 }
00101 
00102 void MainWindow::on_button_question_2_clicked(bool check)
00103 {
00104   std::string answer;
00105   answer = qnode.processAnswer(2);
00106   QString q = answer.c_str();
00107   ui.label_answer_text->setText(q);
00108 }
00109 
00110 void MainWindow::on_button_question_3_clicked(bool check)
00111 {
00112   std::string answer;
00113   answer = qnode.processAnswer(3);
00114   QString q = answer.c_str();
00115   ui.label_answer_text->setText(q);
00116 }
00117 
00118 void MainWindow::on_button_question_4_clicked(bool check)
00119 {
00120   std::string answer;
00121   answer = qnode.processAnswer(4);
00122   QString q = answer.c_str();
00123   ui.label_answer_text->setText(q);
00124 }
00125 
00126 void MainWindow::on_button_question_5_clicked(bool check)
00127 {
00128   std::string answer;
00129   answer = qnode.processAnswer(5);
00130   QString q = answer.c_str();
00131   ui.label_answer_text->setText(q);
00132 }
00133 
00134 void MainWindow::on_button_cat_clicked(bool check)
00135 {
00136   this->setLanguage(0);
00137 }
00138 
00139 void MainWindow::on_button_eng_clicked(bool check)
00140 {
00141   this->setLanguage(1);
00142 }
00143 
00144 void MainWindow::on_button_esp_clicked(bool check)
00145 {
00146   this->setLanguage(2);
00147 }
00148 
00149 void MainWindow::on_button_pause_clicked(bool check)
00150 {
00151   if(this->playImage)
00152   {
00153     //ui.button_pause->setText("PLAY");
00154     ui.button_pause->setIcon(QIcon(":/images/icons/play.png"));
00155     this->playImage = false;
00156   }
00157   else
00158   {
00159     //ui.button_pause->setText("PAUSE");
00160     ui.button_pause->setIcon(QIcon(":/images/icons/pause.png"));
00161     this->playImage = true;
00162   }
00163 }
00164 
00165 void MainWindow::on_button_yes_clicked(bool check)
00166 {
00167   qnode.haodButtonPressed(wiimote::State::MSG_BTN_1);
00168 }
00169 
00170 void MainWindow::on_button_no_clicked(bool check)
00171 {
00172   qnode.haodButtonPressed(wiimote::State::MSG_BTN_2);
00173 }
00174 
00175 void MainWindow::on_button_help_clicked(bool check)
00176 {
00177   qnode.haodButtonPressed(wiimote::State::MSG_BTN_HOME);
00178 }
00179 
00180 void MainWindow::on_button_stop_clicked(bool check)
00181 {
00182   qnode.haodButtonPressed(wiimote::State::MSG_BTN_B);
00183   ui.button_yes->setEnabled(false);
00184   ui.button_no->setEnabled(false);
00185   ui.button_stop->setEnabled(false);
00186   ui.button_help->setEnabled(true);
00187 }
00188 
00189 void MainWindow::mousePressEvent(QMouseEvent *event)
00190 {
00191   if(ui.tab_manager->currentIndex()==0 && !qnode.haodRunning())
00192   {
00193     //from global (main_window) to local (label_image widget)
00194     QPoint mappedPos = ui.label_image->mapFrom(this, event->pos());
00195     //if left click and coordinates inside image
00196     //label can have different size as image
00197     int x = mappedPos.x() - (ui.label_image->width()  - this->image_width)/2;
00198     int y = mappedPos.y() - (ui.label_image->height() - this->image_height)/2;
00199     if((Qt::LeftButton == event->button()) && 
00200         x>0 && x<this->image_width && 
00201         y>0 && y<this->image_height)
00202     {
00203       std::vector<int> point(2);
00204       point[0] = (x*this->image_width_original)/this->image_width;
00205       point[1] = (y*this->image_height_original)/this->image_height;
00206       qnode.setImagePoint(point);
00207       ui.button_stop->setEnabled(true);
00208       ui.button_help->setEnabled(false);
00209     }
00210   }
00211 }
00212 
00213 void MainWindow::changedTab()
00214 {
00215   if(ui.tab_manager->currentIndex()!=0)
00216   {
00217     qnode.haodButtonPressed(wiimote::State::MSG_BTN_MINUS);
00218     ui.button_yes->setEnabled(false);
00219     ui.button_no->setEnabled(false);
00220     ui.button_stop->setEnabled(false);
00221     ui.button_help->setEnabled(true);
00222   }
00223   else if(ui.tab_manager->currentIndex()!=1)
00224   {
00225     qnode.cancelHri();
00226   }
00227 }
00228 
00229 // void MainWindow::imageLabelClicked(QMouseEvent *event)
00230 // {
00231 //   std::cout << "imageLabelClicked SLOT" << std::endl;
00232 // }
00233 
00234 /*****************************************************************************
00235 ** Implemenation [Slots][manually connected]
00236 *****************************************************************************/
00237 void MainWindow::timerDone()
00238 {
00239   this->loadImage();
00240   this->checkAssistance();
00241 }
00242 
00243 void MainWindow::checkAssistance()
00244 {
00245   if(qnode.needAssistance() && qnode.haodRunning())
00246   {
00247     //activate yes/no buttons when assistance is needed
00248     std::cout << "MainWindow::checkAssistance:: enabling buttons yes/no" << std::endl;
00249     ui.button_yes->setEnabled(true);
00250     ui.button_no->setEnabled(true);
00251   }
00252   else
00253   {
00254     ui.button_yes->setEnabled(false);
00255     ui.button_no->setEnabled(false);
00256   }
00257 }
00258 
00259 void MainWindow::loadImage()
00260 {
00261   if(ui.tab_manager->currentIndex()==0 && this->playImage) //Image tab
00262   {
00263     uchar* data;
00264     int width;
00265     int height;
00266     std::string encoding;
00267     bool ok;
00268     if(!qnode.haodRunning())
00269       ok=qnode.getImage(data, width, height, encoding);
00270     else
00271       ok=qnode.getImageDetect(data,width,height,encoding);
00272 
00273     if(ok)
00274     {
00275       QImage::Format format;
00276       bool format_ok=false;
00277       if(encoding=="rgb8")
00278       {
00279         format=QImage::Format_RGB888;
00280         format_ok=true;
00281       }
00282       else if(encoding=="mono8")
00283       {
00284         format=QImage::Format_Mono;
00285         format_ok=true;
00286       }
00287       else if(encoding=="bgr8")
00288       {
00289         format=QImage::Format_RGB888;
00290         format_ok=true;
00291       }
00292       else
00293       {
00294 //        std::cout << "format not expexted:" << encoding << std::endl;
00295       }
00296       if(format_ok)
00297       {
00298         QImage myImage(data, width, height, format);
00299         if(encoding=="bgr8")
00300         {
00301           QImage swapImg = myImage.rgbSwapped();
00302           myImage=swapImg;
00303         }
00304         //QPixmap p(QPixmap::fromImage(myImage));
00305 
00306         if(!qnode.haodRunning())
00307         {
00308           //draw face rectangles on qimage
00309           QPainter qPainter(&myImage);
00310           QPen pen(Qt::blue);
00311           pen.setWidth(3);
00312           qPainter.setBrush(Qt::NoBrush);
00313           qPainter.setPen(pen);
00314           std::vector<std::vector<int> > rects = qnode.getRects();
00315           for(unsigned int i=0; i<rects.size(); i++)
00316           {
00317             int w=rects[i][2];
00318             int h=rects[i][3];
00319             int x=rects[i][0]-w/2;
00320             int y=rects[i][1]-h/2;
00321             qPainter.drawRect(x,y,w,h);
00322           }
00323         }
00324         
00325         QPixmap p(QPixmap::fromImage(myImage));
00326         
00327         //original image size
00328         this->image_width_original = p.width();
00329         this->image_height_original = p.height();
00330         QPixmap p2= p.scaled(ui.label_image->width(),ui.label_image->height(),Qt::KeepAspectRatio);
00331         //scaled image size
00332         this->image_width  = p2.width();
00333         this->image_height = p2.height();
00334         ui.label_image->setPixmap(p2);
00335         ui.label_image->setScaledContents(false);
00336         //ui.label_image->adjustSize();
00337       }
00338     }
00339   }
00340 }
00341 
00342 void MainWindow::setLanguage(int index)
00343 {
00344   if(index!=this->lastLanguage)
00345   {
00346     for(unsigned int i=0; i<this->tabs.size(); i++)
00347     {
00348       ui.tab_manager->setTabText(i, this->tabs[i][index].c_str());
00349     }
00350     ui.button_question_0->setText(qnode.qas[0].question[index].c_str());
00351     ui.button_question_1->setText(qnode.qas[1].question[index].c_str());
00352     ui.button_question_2->setText(qnode.qas[2].question[index].c_str());
00353     ui.button_question_3->setText(qnode.qas[3].question[index].c_str());
00354     ui.button_question_4->setText(qnode.qas[4].question[index].c_str());
00355     ui.button_question_5->setText(qnode.qas[5].question[index].c_str());
00356     ui.label_answer_text->setText("");
00357     ui.label_info_title->setText(this->info[0][index].c_str());
00358     ui.label_info_1->setText(this->info[1][index].c_str());
00359     ui.label_info_2->setText(this->info[2][index].c_str());
00360     ui.label_info_3->setText(this->info[3][index].c_str());
00361     ui.label_info_4->setText(this->info[4][index].c_str());
00362     ui.label_info_5->setText(this->info[5][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("Programa");
00405   tab.push_back("Program");
00406   tab.push_back("Programa");
00407   this->tabs.push_back(tab);
00408   tab.clear();
00409   tab.push_back("Info");
00410   tab.push_back("Info");
00411   tab.push_back("Info");
00412   this->tabs.push_back(tab);
00413   tab.clear();
00414   tab.push_back("Sobre");
00415   tab.push_back("About");
00416   tab.push_back("Acerca de");
00417   this->tabs.push_back(tab);
00418   tab.clear();
00419   
00420   std::vector<std::string> text;
00421   text.push_back("<b>Tibi i Dabo</b>");
00422   text.push_back("<b>Tibi and Dabo<b>");
00423   text.push_back("<b>Tibi y Dabo<b>");
00424   this->info.push_back(text);
00425   text.clear();
00426   text.push_back("- Robots socials i urbans.");
00427   text.push_back("- Social and urban robots.");
00428   text.push_back("- Robots sociales y urbanos");
00429   this->info.push_back(text);
00430   text.clear();
00431   text.push_back("- Plataforma <i><b>Segway rmp200</i></b> per navegació en entorns urbans.");
00432   text.push_back("- <i><b>Segway rmp200</i></b> platform for navigation in urban environments.");
00433   text.push_back("- Plataforma <i><b>Segway rmp200</i></b> para navegación en entornos urbanos.");
00434   this->info.push_back(text);
00435   text.clear();
00436   text.push_back("- Sensors de rang làser <i><b>Hokuyo</i></b> per localització i detecció d'obstacles.");
00437   text.push_back("- <i><b>Hokuyo</i></b> range laser sensors for localization and obstacle detection.");
00438   text.push_back("- Sensores de rango láser <i><b>Hokuyo</i></b> para localización y detección de obstáculos.");
00439   this->info.push_back(text);
00440   text.clear();
00441   text.push_back("- Càmara estèreo <i><b>Bumblebee2</i></b> per visió per computador.");
00442   text.push_back("- <i><b>Bumblebee2</i></b> Stereo camera for computer visión.");
00443   text.push_back("- Cámara estéreo <i><b>Bumblebee2</i></b> para visión por computador.");
00444   this->info.push_back(text);
00445   text.clear();
00446   text.push_back("- Cap, braços, pantalla tàctil i parla per la interacció humà robot");
00447   text.push_back("- Head, arms, touch screen and speech for human robot interaction.");
00448   text.push_back("- Cabeza, brazos, pantalla táctil y habla para la interacción humano robot.");
00449   this->info.push_back(text);
00450   text.clear();
00451 }
00452 
00453 /*****************************************************************************
00454 ** Implementation [Menu]
00455 *****************************************************************************/
00456 
00457 void MainWindow::on_actionAbout_triggered() {
00458     QMessageBox::about(this, tr("About ..."),tr("<h2>Tibi Dabo Gui</h2><p>Copyright IRI</p><p>Graphic User Interface for Tibi Dabo robots.</p>"));
00459 }
00460 
00461 /*****************************************************************************
00462 ** Implementation [Configuration]
00463 *****************************************************************************/
00464 
00465 void MainWindow::ReadSettings() {
00466     QSettings settings("Qt-Ros Package", "tibi_dabo_gui");
00467     restoreGeometry(settings.value("geometry").toByteArray());
00468     restoreState(settings.value("windowState").toByteArray());
00469 }
00470 
00471 void MainWindow::WriteSettings() {
00472     QSettings settings("Qt-Ros Package", "tibi_dabo_gui");
00473     settings.setValue("geometry", saveGeometry());
00474     settings.setValue("windowState", saveState());
00475 }
00476 
00477 void MainWindow::closeEvent(QCloseEvent *event)
00478 {
00479         WriteSettings();
00480         QMainWindow::closeEvent(event);
00481 }
00482 
00483 }  // namespace tibi_dabo_gui
00484 


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