ContextMenu.cc 15.3 KB
Newer Older
Jan Möbius's avatar
 
Jan Möbius committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//=============================================================================
//
//                               OpenFlipper
//        Copyright (C) 2008 by Computer Graphics Group, RWTH Aachen
//                           www.openflipper.org
//
//-----------------------------------------------------------------------------
//
//                                License
//
//  OpenFlipper is free software: you can redistribute it and/or modify
//  it under the terms of the GNU Lesser General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
Jan Möbius's avatar
Jan Möbius committed
15
//
Jan Möbius's avatar
 
Jan Möbius committed
16 17 18 19
//  OpenFlipper is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU Lesser General Public License for more details.
Jan Möbius's avatar
Jan Möbius committed
20
//
Jan Möbius's avatar
 
Jan Möbius committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
//  You should have received a copy of the GNU Lesser General Public License
//  along with OpenFlipper.  If not, see <http://www.gnu.org/licenses/>.
//
//-----------------------------------------------------------------------------
//
//   $Revision$
//   $Author$
//   $Date$
//
//=============================================================================




//=============================================================================
//
//  CLASS CoreWidget - IMPLEMENTATION
//
//=============================================================================


//== INCLUDES =================================================================

// -------------------- mview
#include "CoreWidget.hh"

#include <OpenFlipper/BasePlugin/PluginFunctions.hh>
#include <OpenFlipper/common/GlobalOptions.hh>

Jan Möbius's avatar
Jan Möbius committed
50
//== IMPLEMENTATION ==========================================================
Jan Möbius's avatar
 
Jan Möbius committed
51 52

void CoreWidget::slotCustomContextMenu( const QPoint& _point ) {
Jan Möbius's avatar
Jan Möbius committed
53

Jan Möbius's avatar
Jan Möbius committed
54
  QPoint   popupPosition;
Jan Möbius's avatar
Jan Möbius committed
55
  QPoint   scenePos;
Jan Möbius's avatar
Jan Möbius committed
56

Jan Möbius's avatar
Jan Möbius committed
57 58 59 60 61 62
  // Calculate popup position. Use the position from the viewer which was clicked on.
  popupPosition =  examiner_widgets_[PluginFunctions::activeExaminer()]->glMapToGlobal(_point);
  QPointF f = examiner_widgets_[PluginFunctions::activeExaminer()]->mapToScene(QPointF(_point.x(), _point.y()));
  scenePos = QPoint (f.x(), f.y());
  
  // Call function to adapt the menu to the currently used contex.
63
  updatePopupMenu(scenePos);
Jan Möbius's avatar
Jan Möbius committed
64

Jan Möbius's avatar
Jan Möbius committed
65
  // If the menu is not correctly initialized, dont try to show it.
Jan Möbius's avatar
 
Jan Möbius committed
66
  if ( !contextMenu_->isEmpty () )
Jan Möbius's avatar
Jan Möbius committed
67 68
    contextMenu_->exec( popupPosition );

Jan Möbius's avatar
 
Jan Möbius committed
69 70
}

Jan Möbius's avatar
Jan Möbius committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
/** \brief Update context Menu when Coordsys node has been clicked on.
 * 
 * This function is called when the coordinate system in a viewer has been clicked on.
 * This context menu will show all function relevant for the current view like the
 * projection/viewing direction...
 * 
 * @param _menu Pointer to the context Menu
 * @param _part id of the coordsys part which has been clicked on.
 */
void CoreWidget::updatePopupMenuCoordsysNode(QMenu* _menu  , const int _part) {
  std::cerr << "Coordsys part was : " << _part << std::endl;
  QAction* typeEntry = new QAction("Viewer Settings",_menu);
  _menu->addAction( typeEntry );
  
  //====================================================================================================
  
  
88 89
}

Jan Möbius's avatar
Jan Möbius committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
/** \brief Update context Menu when background has been clicked on.
 * 
 * This function is called when the background in a viewer has been clicked on.
 * This context menu will show functions which are related to the background of the 
 * viewer.
 * 
 * @param _menu Pointer to the context Menu
 * @param _point position in the viewer where the user clicked.
 */
void CoreWidget::updatePopupMenuBackground(QMenu* _menu , const QPoint& _point) {
  
  QAction* action = _menu->addAction("Set Background Color");
  action->setToolTip("Set the background color for the current viewer");
  action->setStatusTip(tr("Set the background color for the current viewer"));
  action->setWhatsThis(tr("Set the background color for the current viewer"));  
105
  action->setIcon(QIcon(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"BackgroundColor.png") );
Jan Möbius's avatar
Jan Möbius committed
106 107 108 109 110
  connect(action, SIGNAL(triggered()), this, SLOT(slotSetLocalBackgroundColor()) );
  
  //====================================================================================================
  
}
Jan Möbius's avatar
Jan Möbius committed
111

Jan Möbius's avatar
Jan Möbius committed
112 113 114 115 116 117 118 119 120 121 122
/** \brief Update context Menu an object has been clicked on.
 * 
 * This function is called when an object has been clicked on.
 * This context menu will show all properties for the given object.
 * 
 * @param _menu Pointer to the context Menu
 * @param _objectId Id of the object that has been clicked on.
 */
void CoreWidget::updatePopupMenuObject(QMenu* _menu , const int _objectId ) {
  std::cerr << "Object Context Menu for id: " << _objectId << std::endl;
}
Jan Möbius's avatar
Jan Möbius committed
123 124 125



Jan Möbius's avatar
Jan Möbius committed
126 127 128 129 130 131 132
/** \brief check current context and initialize context menu according to this context. 
 * 
 * This function is called whenever a context menu for the corewidget is requested. 
 * It will decide about the current context, collect all menus for plugins and
 * construct the final context menu.
 */
void CoreWidget::updatePopupMenu(const QPoint& _point) {
Jan Möbius's avatar
Jan Möbius committed
133

Jan Möbius's avatar
Jan Möbius committed
134 135 136 137 138
  // Clear the complete context menu.
  contextMenu_->clear();
  
  // Clear the selection context menu part.
  contextSelectionMenu_->clear();
Jan Möbius's avatar
Jan Möbius committed
139

Jan Möbius's avatar
Jan Möbius committed
140 141 142
  // =============================================================================
  // First do a picking on the current position to check which context we are in.
  // =============================================================================
Jan Möbius's avatar
 
Jan Möbius committed
143
  int objectId = -1;
Jan Möbius's avatar
Jan Möbius committed
144 145 146 147
  
  enum CONTEXTTYPE {
    COORDSYSCONTEXT ,BACKGROUNDCONTEXT ,OBJECTCONTEXT
  } context = BACKGROUNDCONTEXT;
Jan Möbius's avatar
Jan Möbius committed
148

Jan Möbius's avatar
 
Jan Möbius committed
149 150 151 152
  // Do picking in the gl area to find an object
  unsigned int    node_idx, target_idx;
  ACG::Vec3d      hit_point;
  BaseObjectData* object;
Jan Möbius's avatar
Jan Möbius committed
153
  
154
  if (examiner_widgets_[PluginFunctions::activeExaminer()]->pick( ACG::SceneGraph::PICK_ANYTHING,_point,node_idx, target_idx, &hit_point ) ) {
155

Jan Möbius's avatar
Jan Möbius committed
156
    if ( PluginFunctions::getPickedObject(node_idx, object) ) {
Jan Möbius's avatar
 
Jan Möbius committed
157
      objectId = object->id();
Jan Möbius's avatar
Jan Möbius committed
158 159
      context  = OBJECTCONTEXT;
    } else {
160
      ACG::SceneGraph::BaseNode* node = ACG::SceneGraph::find_node( PluginFunctions::getSceneGraphRootNode() , node_idx );
Jan Möbius's avatar
Jan Möbius committed
161 162
      if ( node != 0 && ( node->name() == "Core Coordsys Node") ) 
        context = COORDSYSCONTEXT;
163
    }
Jan Möbius's avatar
Jan Möbius committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
  } 
  
  // =============================================================================
  // Depending on the context create the basic context menu.
  // =============================================================================
  
  QIcon icon;
  QAction* typeEntry = 0;      
  switch (context) {
    case BACKGROUNDCONTEXT:
      updatePopupMenuBackground(contextMenu_,_point);
      break;
    case OBJECTCONTEXT:
      typeEntry = new QAction("Object",contextMenu_);
      contextMenu_->addAction( typeEntry );
      break;
    case COORDSYSCONTEXT:
      updatePopupMenuCoordsysNode(contextMenu_,target_idx);
      break;
Jan Möbius's avatar
 
Jan Möbius committed
183
  }
Jan Möbius's avatar
Jan Möbius committed
184 185 186 187 188
  
  // Add the global entry to the context menu.
  QAction* entrySeparator = contextMenu_->addSeparator( );

  QAction* contextSelectionAction = contextMenu_->addMenu( contextSelectionMenu_ );
Jan Möbius's avatar
Jan Möbius committed
189

Jan Möbius's avatar
 
Jan Möbius committed
190
  int topLevelAdded  = 0;
Jan Möbius's avatar
Jan Möbius committed
191

Jan Möbius's avatar
 
Jan Möbius committed
192
  if ( objectId != -1) {
Jan Möbius's avatar
Jan Möbius committed
193

194 195
    emit updateContextMenu(objectId);

Jan Möbius's avatar
 
Jan Möbius committed
196
    // Add an empty Menu defining the current Type
197 198 199
    typeEntry->setText( typeName(object->dataType()) );
    icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+ typeIcon(object->dataType()) );
    typeEntry->setIcon(icon);
Jan Möbius's avatar
Jan Möbius committed
200 201


Jan Möbius's avatar
 
Jan Möbius committed
202 203
    // Add real context Menus first
    for ( uint i = 0 ; i < contextMenus_.size(); ++i ) {
Jan Möbius's avatar
Jan Möbius committed
204

Jan Möbius's avatar
 
Jan Möbius committed
205 206 207 208 209 210 211 212
      // check if the dataType of the object matches the context type
      if ( object->dataType(  contextMenus_[i].contextType ) ) {
        if ( contextMenus_[i].position == CONTEXTTOPLEVELMENU ) {
          contextMenu_->addMenu( contextMenus_[i].menu );
          topLevelAdded++;
        } else if ( contextMenus_[i].position == CONTEXTSELECTIONMENU ) {
          contextSelectionMenu_->addMenu(contextMenus_[i].menu);
        }
Jan Möbius's avatar
Jan Möbius committed
213 214

        // Get all Actions in the menu and its submenus.
Jan Möbius's avatar
 
Jan Möbius committed
215 216 217 218
        // Set their data to the picked Object id
        QList< QAction *> allActions = contextMenus_[i].menu->actions();
        while ( !allActions.empty() ) {
          QList< QAction *> tmpList;
Jan Möbius's avatar
Jan Möbius committed
219

Jan Möbius's avatar
 
Jan Möbius committed
220 221
          // Set userdata of all actions to the picked Object Id
          for ( int j = 0 ; j < allActions.size(); ++j ) {
Jan Möbius's avatar
Jan Möbius committed
222 223
            allActions[j]->setData( QVariant( objectId ) );
            if ( allActions[j]->menu() != 0 )
Jan Möbius's avatar
 
Jan Möbius committed
224 225
              tmpList << allActions[j]->menu()->actions();
          }
Jan Möbius's avatar
Jan Möbius committed
226

Jan Möbius's avatar
 
Jan Möbius committed
227 228 229 230 231
          allActions = tmpList;
        }
      }
    }
  } else {
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246

    emit updateContextMenu(-1);

    // Add real context Menus first
    for ( uint i = 0 ; i < contextMenus_.size(); ++i ) {
      if ( contextMenus_[i].contextType == DATA_NONE ) {
        contextMenu_->addMenu( contextMenus_[i].menu );
        topLevelAdded++;
      }
    }

    if ( topLevelAdded == 0 ) {
      contextMenu_->removeAction(typeEntry);
      contextMenu_->removeAction(entrySeparator);
    }
Jan Möbius's avatar
 
Jan Möbius committed
247
  }
Jan Möbius's avatar
Jan Möbius committed
248 249

  if ( contextSelectionMenu_->isEmpty()  )
Jan Möbius's avatar
 
Jan Möbius committed
250
    contextMenu_->removeAction( contextSelectionAction );
Jan Möbius's avatar
Jan Möbius committed
251

Jan Möbius's avatar
 
Jan Möbius committed
252 253
  if ( topLevelAdded > 0 )
    contextMenu_->addSeparator();
Jan Möbius's avatar
Jan Möbius committed
254

Jan Möbius's avatar
 
Jan Möbius committed
255 256 257
  // Add persistent context Menus as second part
  for ( uint i = 0 ; i < persistentContextMenus_.size(); ++i ) {
    contextMenu_->addMenu( persistentContextMenus_[i].menu );
Jan Möbius's avatar
Jan Möbius committed
258 259

    // Get all Actions in the menu and its submenus.
Jan Möbius's avatar
 
Jan Möbius committed
260 261 262 263
    // Set their data to the picked Object id
    QList< QAction *> allActions = persistentContextMenus_[i].menu->actions();
    while ( !allActions.empty() ) {
      QList< QAction *> tmpList;
Jan Möbius's avatar
Jan Möbius committed
264

Jan Möbius's avatar
 
Jan Möbius committed
265 266
      // Set userdata of all actions to the picked Object Id
      for ( int j = 0 ; j < allActions.size(); ++j ) {
Jan Möbius's avatar
Jan Möbius committed
267 268
        allActions[j]->setData( QVariant( objectId ) );
        if ( allActions[j]->menu() != 0 )
Jan Möbius's avatar
 
Jan Möbius committed
269 270
          tmpList << allActions[j]->menu()->actions();
      }
Jan Möbius's avatar
Jan Möbius committed
271

Jan Möbius's avatar
 
Jan Möbius committed
272 273
      allActions = tmpList;
    }
Jan Möbius's avatar
Jan Möbius committed
274

Jan Möbius's avatar
 
Jan Möbius committed
275
  }
Jan Möbius's avatar
Jan Möbius committed
276

Jan Möbius's avatar
 
Jan Möbius committed
277 278 279
  // Only add Separator if we had plugin context menus
  if ( persistentContextMenus_.size() > 0 )
    contextMenu_->addSeparator();
Jan Möbius's avatar
Jan Möbius committed
280

281
  if (examiner_widgets_[0]->getPickMenu() != NULL) {
282 283 284 285 286
    if ( examiner_widgets_[0]->getPickMenu()->actions().size() > 0 ) {
      examiner_widgets_[0]->getPickMenu()->setTitle("&Picking");
      contextMenu_->addMenu(examiner_widgets_[0]->getPickMenu() );
      examiner_widgets_[0]->getPickMenu()->setTearOffEnabled(true);
    }
Jan Möbius's avatar
 
Jan Möbius committed
287
  }
Jan Möbius's avatar
Jan Möbius committed
288

Dirk Wilden's avatar
 
Dirk Wilden committed
289 290
  // Add a functions menu
  QAction* action;
Dirk Wilden's avatar
Dirk Wilden committed
291
  if ( functionMenu_ == 0 ){
292

293
    functionMenu_ = new QMenu("&Functions",contextMenu_);
294

295
    //====================================================================================================
296

297 298 299
    action = functionMenu_->addAction("Snapshot");
    action->setToolTip("Make a snapshot");
    connect(action, SIGNAL(triggered()), this, SLOT( slotSnapshot() ) );
300

301
    //====================================================================================================
302

303 304 305
    action = functionMenu_->addAction("Set Snapshot Name");
    action->setToolTip("Set a name for snapshots");
    connect(action, SIGNAL(triggered()), this, SLOT(slotSnapshotName()) );
306

307
    //====================================================================================================
308

309
    functionMenu_->addSeparator();
310

311
    //====================================================================================================
312

313 314 315
    action = functionMenu_->addAction("Copy View");
    action->setToolTip("Copy current view to clipboard");
    connect(action, SIGNAL(triggered()), this, SLOT(slotCopyView()) );
316

317
    //====================================================================================================
318

319 320 321
    action = functionMenu_->addAction("Paste View");
    action->setToolTip("Paste current view from clipboard");
    connect(action, SIGNAL(triggered()), this , SLOT( slotPasteView( ) ) );
322

323
    //====================================================================================================
324

325
    functionMenu_->addSeparator();
326

327
    //====================================================================================================
328

329 330 331
    action = functionMenu_->addAction("Animation");
    action->setToolTip("Animate rotation of objects");
    action->setCheckable( true );
Dirk Wilden's avatar
Dirk Wilden committed
332 333
    action->setChecked( PluginFunctions::viewerProperties().animation() );
    connect(action, SIGNAL(triggered(bool)), this , SLOT( slotChangeAnimation(bool) ) );
334

335
    //====================================================================================================
336

337 338 339
    action = functionMenu_->addAction("Backface Culling");
    action->setToolTip("Enable backface culling");
    action->setCheckable( true );
Dirk Wilden's avatar
Dirk Wilden committed
340 341
    action->setChecked( PluginFunctions::viewerProperties().backFaceCulling() );
    connect(action, SIGNAL(triggered(bool)), this , SLOT( slotChangeBackFaceCulling(bool) ) );
342

343
    //====================================================================================================
344

345 346 347
    action = functionMenu_->addAction("Two-sided Lighting");
    action->setToolTip("Enable two-sided lighting");
    action->setCheckable( true );
Dirk Wilden's avatar
Dirk Wilden committed
348 349
    action->setChecked( PluginFunctions::viewerProperties().twoSidedLighting() );
    connect(action, SIGNAL(triggered(bool)), this , SLOT( slotChangeTwoSidedLighting(bool) ) );
350

351 352 353 354
    functionMenu_->setTearOffEnabled(true);
  }

  contextMenu_->addMenu(functionMenu_ );
Jan Möbius's avatar
 
Jan Möbius committed
355 356
}

357 358
void CoreWidget::slotSnapshotName() {
  std::cerr << "Todo : slotSnapShotName only sets name for current viewer" << std::endl;
359 360 361 362 363 364 365 366 367 368 369 370 371 372

  QString fname = PluginFunctions::viewerProperties().snapshotName();

  fname.replace('%', '$');
  fname = QFileDialog::getSaveFileName ( 0, "Save snapshot name" );
  if (!fname.isEmpty())
  {
    fname.replace('$', '%');

    PluginFunctions::viewerProperties().snapshotBaseFileName(fname);
    QString msg="next snapshot: ";
    statusBar()->showMessage(msg);
  }

373 374
}

Dirk Wilden's avatar
Dirk Wilden committed
375 376 377 378 379 380 381
void CoreWidget::slotChangeAnimation(bool _animation){
  if ( shiftPressed_ ){
    for ( uint i = 0 ; i < OpenFlipper::Options::examinerWidgets() ; ++i )
      PluginFunctions::viewerProperties(i).animation(_animation);
    }else
      PluginFunctions::viewerProperties().animation(_animation);
}
382

Dirk Wilden's avatar
Dirk Wilden committed
383 384 385 386 387 388 389
void CoreWidget::slotChangeBackFaceCulling(bool _backFaceCulling){
  if ( shiftPressed_ ){
    for ( uint i = 0 ; i < OpenFlipper::Options::examinerWidgets() ; ++i )
      PluginFunctions::viewerProperties(i).backFaceCulling(_backFaceCulling);
    }else
      PluginFunctions::viewerProperties().backFaceCulling(_backFaceCulling);
}
390

Dirk Wilden's avatar
Dirk Wilden committed
391 392 393 394 395 396
void CoreWidget::slotChangeTwoSidedLighting(bool _lighting){
  if ( shiftPressed_ ){
    for ( uint i = 0 ; i < OpenFlipper::Options::examinerWidgets() ; ++i )
      PluginFunctions::viewerProperties(i).twoSidedLighting(_lighting);
    }else
      PluginFunctions::viewerProperties().twoSidedLighting(_lighting);
Dirk Wilden's avatar
 
Dirk Wilden committed
397 398
}

399
void CoreWidget::slotSnapshot() {
400
  examiner_widgets_[PluginFunctions::activeExaminer()]->snapshot();
401 402
}

403 404 405 406 407 408 409 410
void CoreWidget::slotPasteView( ) {
  examiner_widgets_[PluginFunctions::activeExaminer()]->actionPasteView();
}

void CoreWidget::slotCopyView( ) {
  examiner_widgets_[PluginFunctions::activeExaminer()]->actionCopyView();
}

Jan Möbius's avatar
 
Jan Möbius committed
411 412 413
void CoreWidget::slotAddContextMenu(QMenu* _menu) {
  MenuInfo info;
  info.menu = _menu;
Jan Möbius's avatar
Jan Möbius committed
414

Jan Möbius's avatar
 
Jan Möbius committed
415 416 417 418 419 420 421 422
  persistentContextMenus_.push_back(info);
}

void CoreWidget::slotAddContextMenu( QMenu* _menu , DataType _dataType ,ContextMenuType _type ) {
  MenuInfo info;
  info.menu        = _menu;
  info.contextType = _dataType;
  info.position    = _type;
Jan Möbius's avatar
Jan Möbius committed
423

Jan Möbius's avatar
 
Jan Möbius committed
424 425 426 427
  contextMenus_.push_back(info);
}

//=============================================================================