stdr_map_connector.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002    STDR Simulator - Simple Two DImensional Robot Simulator
00003    Copyright (C) 2013 STDR Simulator
00004    This program is free software; you can redistribute it and/or modify
00005    it under the terms of the GNU General Public License as published by
00006    the Free Software Foundation; either version 3 of the License, or
00007    (at your option) any later version.
00008    This program is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011    GNU General Public License for more details.
00012    You should have received a copy of the GNU General Public License
00013    along with this program; if not, write to the Free Software Foundation,
00014    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
00015    
00016    Authors : 
00017    * Manos Tsardoulias, etsardou@gmail.com
00018    * Aris Thallas, aris.thallas@gmail.com
00019    * Chris Zalidis, zalidis@gmail.com 
00020 ******************************************************************************/
00021 
00022 #include "stdr_gui/stdr_map_connector.h"
00023 
00024 namespace stdr_gui{
00025   
00032   CMapConnector::CMapConnector(int argc, char **argv):
00033     QObject(),
00034     loader_(argc,argv),
00035     argc_(argc),
00036     argv_(argv)
00037   {
00038     map_state_ = NORMAL;
00039     
00040     loader_.map->setScaledContents(true);
00041     
00042     loader_.map->installEventFilter(this);
00043     
00044     QObject::connect(
00045       this,SIGNAL(signalUpdateImage(QImage *)),
00046       this, SLOT(serveImage(QImage *)));
00047     
00048     QPixmap p((
00049       stdr_gui_tools::getRosPackagePath("stdr_gui") + 
00050       std::string("/resources/images/zoom_in.png")).c_str());
00051     zoom_in_cursor_ = QCursor(p.scaled(20,20));
00052     
00053     p=QPixmap((
00054       stdr_gui_tools::getRosPackagePath("stdr_gui") + 
00055       std::string("/resources/images/zoom_out.png")).c_str());
00056     zoom_out_cursor_ = QCursor(p.scaled(20,20));
00057     
00058     bool map_initialized_ = false;
00059   }
00060   
00065   CMapConnector::~CMapConnector(void)
00066   {
00067     
00068   }
00069   
00075   void CMapConnector::setInitialImageSize(QSize s)
00076   {
00077     loader_.setInitialImageSize(s);
00078   }
00079   
00086   void CMapConnector::updateZoom(QPoint p,bool z)
00087   {
00088     if ( ! map_initialized_ )
00089     {
00090       return;
00091     }
00092     loader_.updateZoom(p,z);
00093   }
00094   
00100   void CMapConnector::updateCenter(QPoint p){
00101     if ( ! map_initialized_ )
00102     {
00103       return;
00104     }
00105     loader_.updateCenter(p);
00106   }
00107   
00113   QPoint CMapConnector::getGlobalPoint(QPoint p)
00114   {
00115     return loader_.getGlobalPoint(p);
00116   }
00117   
00124   void CMapConnector::drawGrid(QImage *img,float resolution)
00125   {
00126     if ( ! map_initialized_ )
00127     {
00128       return;
00129     }
00130     loader_.drawGrid(img,resolution);
00131   }
00132 
00139   bool CMapConnector::eventFilter( QObject* watched, QEvent* event ) 
00140   {
00141     if ( ! map_initialized_ )
00142     {
00143       return false;
00144     }
00145     if(watched == loader_.map)
00146     {
00147       if(event->type() == QEvent::MouseButtonPress)
00148       {
00149         
00150         loader_.map->setFocus(Qt::MouseFocusReason);
00151         
00152         const QMouseEvent* const me = 
00153           static_cast<const QMouseEvent*>( event );
00154         QPoint p = me->pos();
00155         if(me->button() == Qt::RightButton)
00156         {
00157           map_state_ = NORMAL;
00158           loader_.map->setCursor(QCursor(Qt::CrossCursor));
00159           Q_EMIT itemClicked(p,Qt::RightButton);
00160         }
00161         else if(me->button() == Qt::LeftButton)
00162         {
00163           if(map_state_ == ZOOMIN)
00164           {
00165             Q_EMIT zoomInPressed(p);
00166           }
00167           else if(map_state_ == ZOOMOUT)
00168           {
00169             Q_EMIT zoomOutPressed(p);
00170           }
00171           else if(map_state_ == SETPLACE)
00172           {
00173             map_state_ = NORMAL;
00174             loader_.map->setCursor(QCursor(Qt::CrossCursor));
00175             Q_EMIT robotPlaceSet(p);
00176           }
00177           else if(map_state_ == NORMAL)
00178           {
00179             Q_EMIT itemClicked(p,Qt::LeftButton);
00180           }
00181           else if(map_state_ == SETREPLACE)
00182           {
00183             map_state_ = NORMAL;
00184             loader_.map->setCursor(QCursor(Qt::CrossCursor));
00185             Q_EMIT robotReplaceSet(p,current_robot_frame_id_);
00186           }
00187           else if(map_state_ == SETPLACERFID)
00188           {
00189             map_state_ = NORMAL;
00190             loader_.map->setCursor(QCursor(Qt::CrossCursor));
00191             Q_EMIT rfidPlaceSet(p);
00192           }
00193           else if(map_state_ == SETPLACETHERMAL)
00194           {
00195             map_state_ = NORMAL;
00196             loader_.map->setCursor(QCursor(Qt::CrossCursor));
00197             Q_EMIT thermalPlaceSet(p);
00198           }
00199           else if(map_state_ == SETPLACECO2)
00200           {
00201             map_state_ = NORMAL;
00202             loader_.map->setCursor(QCursor(Qt::CrossCursor));
00203             Q_EMIT co2PlaceSet(p);
00204           }
00205           else if(map_state_ == SETPLACESOUND)
00206           {
00207             map_state_ = NORMAL;
00208             loader_.map->setCursor(QCursor(Qt::CrossCursor));
00209             Q_EMIT soundPlaceSet(p);
00210           }
00211         }
00212       }
00213       else if(event->type() == QEvent::Wheel)
00214       {
00215         const QWheelEvent* const me = 
00216           static_cast<const QWheelEvent*>( event );
00217         QPoint p = me->pos();
00218         if(me->delta() > 0)
00219         {
00220           Q_EMIT zoomInPressed(p);
00221         }
00222         else
00223         {
00224           Q_EMIT zoomOutPressed(p);
00225         }
00226       }
00227       else if(event->type() == QEvent::KeyPress)
00228       {
00229         const QKeyEvent* const me = 
00230           static_cast<const QKeyEvent*>( event );
00231         
00232         if ( ! map_initialized_ )
00233         {
00234           return false;
00235         }
00236         if(me->key() == Qt::Key_Right)
00237         {
00238           loader_.moveDirectionally(Qt::Key_Right);
00239         }
00240         else if(me->key() == Qt::Key_Left)
00241         {
00242           loader_.moveDirectionally(Qt::Key_Left);
00243         } 
00244         else if(me->key() == Qt::Key_Up)
00245         {
00246           loader_.moveDirectionally(Qt::Key_Up);
00247         }
00248         else if(me->key() == Qt::Key_Down)
00249         {
00250           loader_.moveDirectionally(Qt::Key_Down);
00251         }
00252       }
00253     }
00254     return false;
00255   }
00256 
00262   void CMapConnector::updateImage(QImage *img)
00263   {
00264     Q_EMIT signalUpdateImage(img);
00265   }
00266   
00272   void CMapConnector::serveImage(QImage *img)
00273   {
00274     loader_.updateImage(img);
00275   }
00276   
00282   QPoint CMapConnector::mapToGlobal(QPoint p)
00283   {
00284     return loader_.mapToGlobal(p);
00285   }
00286   
00292   void CMapConnector::setCursorZoomIn(bool state)
00293   {
00294     if ( ! map_initialized_ )
00295     {
00296       return;
00297     }
00298     if(state)
00299     {
00300       map_state_ = ZOOMIN;
00301       loader_.map->setCursor(zoom_in_cursor_);
00302     }
00303     else
00304     {
00305       map_state_ = NORMAL;
00306       loader_.map->setCursor(QCursor(Qt::CrossCursor));
00307     }
00308   }
00309   
00315   void CMapConnector::setCursorZoomOut(bool state)
00316   {
00317     if ( ! map_initialized_ )
00318     {
00319       return;
00320     }
00321     if(state)
00322     {
00323       map_state_ = ZOOMOUT;
00324       loader_.map->setCursor(zoom_out_cursor_);
00325     }
00326     else
00327     {
00328       map_state_ = NORMAL;
00329       loader_.map->setCursor(QCursor(Qt::CrossCursor));
00330     }
00331   }
00332   
00338   void CMapConnector::setCursorAdjusted(bool state)
00339   {
00340     if ( ! map_initialized_ )
00341     {
00342       return;
00343     }
00344     loader_.resetZoom();
00345     map_state_ = NORMAL;
00346     loader_.map->setCursor(QCursor(Qt::CrossCursor));
00347   }
00348   
00353   void CMapConnector::waitForPlace(void)
00354   {
00355     if ( ! map_initialized_ )
00356     {
00357       return;
00358     }
00359     map_state_ = SETPLACE;
00360     loader_.map->setCursor(Qt::PointingHandCursor);
00361   }
00362   
00367   void CMapConnector::waitForThermalPlace(void)
00368   {
00369     if ( ! map_initialized_ )
00370     {
00371       return;
00372     }
00373     map_state_ = SETPLACETHERMAL;
00374     loader_.map->setCursor(Qt::PointingHandCursor);
00375   }
00376   
00381   void CMapConnector::waitForRfidPlace(void)
00382   {
00383     if ( ! map_initialized_ )
00384     {
00385       return;
00386     }
00387     map_state_ = SETPLACERFID;
00388     loader_.map->setCursor(Qt::PointingHandCursor);
00389   }
00390   
00395   void CMapConnector::waitForCo2Place(void)
00396   {
00397     if ( ! map_initialized_ )
00398     {
00399       return;
00400     }
00401     map_state_ = SETPLACECO2;
00402     loader_.map->setCursor(Qt::PointingHandCursor);
00403   }
00404   
00409   void CMapConnector::waitForSoundPlace(void)
00410   {
00411     if ( ! map_initialized_ )
00412     {
00413       return;
00414     }
00415     map_state_ = SETPLACESOUND;
00416     loader_.map->setCursor(Qt::PointingHandCursor);
00417   }
00418   
00423   QWidget* CMapConnector::getLoader(void)
00424   {
00425     return static_cast<QWidget *>(&loader_);
00426   }
00427   
00433   void CMapConnector::waitForReplace(std::string robotFrameId){
00434     if ( ! map_initialized_ )
00435     {
00436       return;
00437     }
00438     current_robot_frame_id_ = robotFrameId;
00439     map_state_ = SETREPLACE;
00440     loader_.map->setCursor(Qt::PointingHandCursor);
00441   }
00442   
00448   void CMapConnector::setMapInitialized(bool mi)
00449   {
00450     map_initialized_ = mi;
00451   }
00452 }


stdr_gui
Author(s): Manos Tsardoulias
autogenerated on Wed Sep 2 2015 03:36:44