picking.cc

00001 /*===========================================================================*\
00002  *                                                                           *
00003  *                              OpenFlipper                                  *
00004  *      Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen      *
00005  *                           www.openflipper.org                             *
00006  *                                                                           *
00007  *---------------------------------------------------------------------------*
00008  *  This file is part of OpenFlipper.                                        *
00009  *                                                                           *
00010  *  OpenFlipper is free software: you can redistribute it and/or modify      *
00011  *  it under the terms of the GNU Lesser General Public License as           *
00012  *  published by the Free Software Foundation, either version 3 of           *
00013  *  the License, or (at your option) any later version with the              *
00014  *  following exceptions:                                                    *
00015  *                                                                           *
00016  *  If other files instantiate templates or use macros                       *
00017  *  or inline functions from this file, or you compile this file and         *
00018  *  link it with other files to produce an executable, this file does        *
00019  *  not by itself cause the resulting executable to be covered by the        *
00020  *  GNU Lesser General Public License. This exception does not however       *
00021  *  invalidate any other reasons why the executable file might be            *
00022  *  covered by the GNU Lesser General Public License.                        *
00023  *                                                                           *
00024  *  OpenFlipper is distributed in the hope that it will be useful,           *
00025  *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00026  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
00027  *  GNU Lesser General Public License for more details.                      *
00028  *                                                                           *
00029  *  You should have received a copy of the GNU LesserGeneral Public          *
00030  *  License along with OpenFlipper. If not,                                  *
00031  *  see <http://www.gnu.org/licenses/>.                                      *
00032  *                                                                           *
00033 \*===========================================================================*/
00034 
00035 /*===========================================================================*\
00036  *                                                                           *
00037  *   $Revision: 8520 $                                                         *
00038  *   $Author: moebius $                                                      *
00039  *   $Date: 2010-02-10 15:56:59 +0100 (Mi, 10. Feb 2010) $                   *
00040  *                                                                           *
00041 \*===========================================================================*/
00042 
00043 
00044 
00045 
00046 //=============================================================================
00047 //
00048 //  CLASS CoreWidget - IMPLEMENTATION
00049 //
00050 //=============================================================================
00051 
00052 
00053 //== INCLUDES =================================================================
00054 
00055 #include "CoreWidget.hh"
00056 
00057 #include <OpenFlipper/BasePlugin/PluginFunctions.hh>
00058 #include <OpenFlipper/common/GlobalOptions.hh>
00059 #include <OpenFlipper/widgets/glWidget/CursorPainter.hh>
00060 
00061 //-----------------------------------------------------------------------------
00062 
00063 void CoreWidget::setActionMode(const Viewer::ActionMode _am){
00064   if (_am != actionMode_) {
00065     lastActionMode_ = actionMode_;
00066     actionMode_ = _am;
00067 
00068 
00069     // update Buttons
00070 
00071     moveButton_->setDown(false);
00072     lightButton_->setDown(false);
00073     pickButton_->setDown(false);
00074     questionButton_->setDown(false);
00075 
00076     switch (_am)
00077     {
00078       case Viewer::ExamineMode:
00079         moveButton_->setDown(true);
00080         break;
00081       case Viewer::LightMode:
00082         lightButton_->setDown(true);
00083         break;
00084       case Viewer::PickingMode:
00085         pickButton_->setDown(true);
00086         break;
00087       case Viewer::QuestionMode:
00088         questionButton_->setDown(true);
00089         break;
00090     }
00091 
00092     // update cursor
00093     switch ( _am )
00094     {
00095       case Viewer::ExamineMode:
00096         cursorPainter_->setCursor(QCursor( QPixmap( OpenFlipper::Options::iconDirStr() + QDir::separator() + "cursor_move.png"  ) ,0,0 ));
00097         break;
00098       case Viewer::LightMode:
00099         cursorPainter_->setCursor(QCursor( QPixmap( OpenFlipper::Options::iconDirStr() + QDir::separator() + "cursor_light.png"  ) ,0,0 ));
00100         break;
00101       case Viewer::PickingMode:
00102         cursorPainter_->setCursor(QCursor( QPixmap( OpenFlipper::Options::iconDirStr() + QDir::separator() + "cursor_arrow.png"  ) ,0,0 ));
00103         if (pick_mode_idx_ != -1) {
00104           cursorPainter_->setCursor(pick_modes_[pick_mode_idx_].cursor);
00105         }
00106         break;
00107       case Viewer::QuestionMode:
00108         cursorPainter_->setCursor(QCursor( QPixmap( OpenFlipper::Options::iconDirStr() + QDir::separator() + "cursor_whatsthis.png"  ) ,0,0 ));
00109         break;
00110     }
00111 
00112     //update Viewers
00113 
00114     for ( unsigned int i = 0 ; i < OpenFlipper::Options::examinerWidgets() ; ++i ) {
00115 
00116       examiner_widgets_[i]->sceneGraph( PluginFunctions::getSceneGraphRootNode() );
00117       examiner_widgets_[i]->trackMouse(false);
00118 
00119 
00120       switch ( _am )
00121       {
00122         case Viewer::ExamineMode:
00123           pickToolbar_->detachToolbar ();
00124           break;
00125         case Viewer::PickingMode:
00126           if (pick_mode_idx_ != -1) {
00127             examiner_widgets_[i]->trackMouse(pick_modes_[pick_mode_idx_].tracking);
00128             
00129             // Show the pickMode Toolbar for this picking mode if it is set
00130             if (pick_modes_[pick_mode_idx_].toolbar)
00131               pickToolbar_->attachToolbar (pick_modes_[pick_mode_idx_].toolbar);
00132             else
00133               pickToolbar_->detachToolbar ();
00134           }
00135           break;
00136         default:
00137           break;
00138       }
00139     }
00140 
00141     //emit pickmodeChanged with either the name of the current pickmode or an empty string
00142     if( pickingMode() )
00143       emit(signalPickModeChanged(pick_mode_name_));
00144     else
00145       emit(signalPickModeChanged(""));
00146   }
00147 }
00148 
00149 //-----------------------------------------------------------------------------
00150 
00151 void CoreWidget::getActionMode(Viewer::ActionMode& _am){
00152   _am = actionMode_;
00153 }
00154 
00155 //-----------------------------------------------------------------------------
00156 
00157 void CoreWidget::setPickMode(const std::string _mode){
00158 
00159   for (unsigned int i=0; i<pick_modes_.size(); ++i)
00160   {
00161     if (pick_modes_[i].name == _mode)
00162     {
00163       pickMode( i );
00164       updatePickMenu();
00165       return;
00166     }
00167   }
00168 }
00169 
00170 //-----------------------------------------------------------------------------
00171 
00172 void CoreWidget::getPickMode(std::string& _mode){
00173   _mode = pick_mode_name_;
00174 }
00175 
00176 //-----------------------------------------------------------------------------
00177 
00178 void CoreWidget::updatePickMenu()
00179 {
00180   if (pickMenu_ != 0)
00181     delete pickMenu_;
00182 
00183   pickMenu_ = new QMenu( 0 );
00184   connect( pickMenu_, SIGNAL( aboutToHide() ),
00185      this, SLOT( hidePopupMenus() ) );
00186 
00187   QActionGroup * ag = new QActionGroup( pickMenu_ );
00188   ag->setExclusive( true );
00189 
00190   for (unsigned int i=0; i<pick_modes_.size(); ++i) {
00191     if ( !pick_modes_[i].visible )
00192       continue;
00193 
00194     if (pick_modes_[i].name == "Separator")
00195     {
00196       if ((i > 0) && (i<pick_modes_.size()-1)) // not first, not last
00197         pickMenu_->addSeparator();
00198     }
00199     else
00200     {
00201       QAction* ac = new QAction( pick_modes_[i].name.c_str(), ag );
00202       ac->setData( QVariant( i ) );
00203       ac->setCheckable( true );
00204 
00205       if ((int)i == pick_mode_idx_)
00206         ac->setChecked( true );
00207 
00208       pickMenu_->addAction( ac );
00209     }
00210   }
00211 
00212   connect( ag, SIGNAL( triggered( QAction * ) ),
00213      this, SLOT( actionPickMenu( QAction * ) ));
00214 }
00215 
00216 //-----------------------------------------------------------------------------
00217 
00218 
00219 void CoreWidget::actionPickMenu( QAction * _action )
00220 {
00221   int _id = _action->data().toInt();
00222   if (_id < (int) pick_modes_.size() )
00223   {
00224     pickMode( _id );
00225   }
00226 
00227   setPickingMode();
00228 
00229   hidePopupMenus();
00230 }
00231 
00232 //-----------------------------------------------------------------------------
00233 
00234 
00235 void CoreWidget::hidePopupMenus()
00236 {
00237 
00238   if ( pickMenu_ )
00239   {
00240     pickMenu_->blockSignals(true);
00241     pickMenu_->hide();
00242     pickMenu_->blockSignals(false);
00243   }
00244 }
00245 
00246 //-----------------------------------------------------------------------------
00247 
00248 
00249 void CoreWidget::pickMode( int _id )
00250 {
00251   if (_id < (int) pick_modes_.size() )
00252   {
00253     pick_mode_idx_  = _id;
00254     pick_mode_name_ = pick_modes_[pick_mode_idx_].name;
00255 
00256     if (pick_modes_[pick_mode_idx_].toolbar)
00257       pickToolbar_->attachToolbar (pick_modes_[pick_mode_idx_].toolbar);
00258     else
00259       pickToolbar_->detachToolbar ();
00260 
00261     // adjust mouse tracking
00262     if ( pickingMode() )
00263       for ( unsigned int i = 0 ; i < OpenFlipper::Options::examinerWidgets() ; ++i )
00264         examiner_widgets_[i]->trackMouse(pick_modes_[pick_mode_idx_].tracking);
00265 
00266     // adjust Cursor
00267     if ( pickingMode() )
00268         cursorPainter_->setCursor( pick_modes_[pick_mode_idx_].cursor);
00269 
00270     // emit signal
00271     emit(signalPickModeChanged(pick_mode_name_));
00272   }
00273 }
00274 
00275 
00276 //-----------------------------------------------------------------------------
00277 
00278 
00279 void CoreWidget::addPickMode(const std::string& _name,
00280                                bool _tracking,
00281                                int  _pos,
00282                                bool _visible,
00283                                QCursor _cursor)
00284 {
00285 
00286   if ((unsigned int)_pos < pick_modes_.size())
00287   {
00288     std::vector<PickMode>::iterator it = pick_modes_.begin();
00289     it += _pos+1;
00290     pick_modes_.insert(it, PickMode(_name, _tracking, _visible, _cursor));
00291   }
00292   else
00293     pick_modes_.push_back(PickMode(_name, _tracking, _visible, _cursor));
00294 
00295   updatePickMenu();
00296 }
00297 
00298 //-----------------------------------------------------------------------------
00299 
00300 void CoreWidget::setPickModeCursor(const std::string& _name, QCursor _cursor)
00301 {
00302   for (uint i=0; i < pick_modes_.size(); i++)
00303     if ( pick_modes_[i].name == _name ){
00304       pick_modes_[i].cursor = _cursor;
00305 
00306       //switch cursor if pickMode is active
00307       if (pick_mode_name_ == _name && pickingMode() )
00308           cursorPainter_->setCursor(_cursor);
00309       break;
00310     }
00311 }
00312 
00313 //-----------------------------------------------------------------------------
00314 
00315 void CoreWidget::setPickModeMouseTracking(const std::string& _name, bool _mouseTracking)
00316 {
00317   for (uint i=0; i < pick_modes_.size(); i++)
00318     if ( pick_modes_[i].name == _name ){
00319       pick_modes_[i].tracking = _mouseTracking;
00320 
00321       //switch cursor if pickMode is active
00322       if (pick_mode_name_ == _name && pickingMode() )
00323         for ( unsigned int i = 0 ; i < OpenFlipper::Options::examinerWidgets() ; ++i )
00324           examiner_widgets_[i]->trackMouse(_mouseTracking);
00325       break;
00326     }
00327 }
00328 
00329 //-----------------------------------------------------------------------------
00330 
00331 void CoreWidget::setPickModeToolbar( const std::string _mode , QToolBar * _toolbar )
00332 {
00333   for (uint i=0; i < pick_modes_.size(); i++)
00334     if ( pick_modes_[i].name == _mode ){
00335       pick_modes_[i].toolbar = _toolbar;
00336 
00337       if (pick_mode_name_ == _mode && pickingMode() )
00338         pickToolbar_->attachToolbar (_toolbar);
00339       break;
00340     }
00341 }
00342 
00343 //-----------------------------------------------------------------------------
00344 
00345 void CoreWidget::removePickModeToolbar( const std::string _mode )
00346 {
00347   for (uint i=0; i < pick_modes_.size(); i++)
00348     if ( pick_modes_[i].name == _mode ){
00349       pick_modes_[i].toolbar = NULL;
00350 
00351       if (pick_mode_name_ == _mode && pickingMode() )
00352         pickToolbar_->detachToolbar ();
00353       break;
00354     }
00355 }
00356 
00357 //-----------------------------------------------------------------------------
00358 
00359 void CoreWidget::clearPickModes()
00360 {
00361   pick_modes_.clear();
00362   pick_mode_idx_  = -1;
00363   pick_mode_name_ = "";
00364   updatePickMenu();
00365 }
00366 
00367 
00368 //-----------------------------------------------------------------------------
00369 
00370 
00371 const std::string& CoreWidget::pickMode() const
00372 {
00373   return pick_mode_name_;
00374 }
00375 
00376 
00377 

acg pic Project OpenFlipper, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .