ContextMenu.cc 16 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 105 106 107 108 109 110 111 112 113
/** \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* typeEntry = new QAction("Background",_menu);
  _menu->addAction( typeEntry );
  
  //====================================================================================================
  
  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"));  
  connect(action, SIGNAL(triggered()), this, SLOT(slotSetLocalBackgroundColor()) );
  
  //====================================================================================================
  
}
Jan Möbius's avatar
Jan Möbius committed
114

Jan Möbius's avatar
Jan Möbius committed
115 116 117 118 119 120 121 122 123 124 125
/** \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
126 127 128



Jan Möbius's avatar
Jan Möbius committed
129 130 131 132 133 134 135
/** \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
136

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

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

Jan Möbius's avatar
 
Jan Möbius committed
152 153 154 155
  // 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
156
  
157
  if (examiner_widgets_[PluginFunctions::activeExaminer()]->pick( ACG::SceneGraph::PICK_ANYTHING,_point,node_idx, target_idx, &hit_point ) ) {
158

Jan Möbius's avatar
Jan Möbius committed
159
    if ( PluginFunctions::getPickedObject(node_idx, object) ) {
Jan Möbius's avatar
 
Jan Möbius committed
160
      objectId = object->id();
Jan Möbius's avatar
Jan Möbius committed
161 162
      context  = OBJECTCONTEXT;
    } else {
163
      ACG::SceneGraph::BaseNode* node = ACG::SceneGraph::find_node( PluginFunctions::getSceneGraphRootNode() , node_idx );
Jan Möbius's avatar
Jan Möbius committed
164 165
      if ( node != 0 && ( node->name() == "Core Coordsys Node") ) 
        context = COORDSYSCONTEXT;
166
    }
Jan Möbius's avatar
Jan Möbius committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
  } 
  
  // =============================================================================
  // 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
186
  }
Jan Möbius's avatar
Jan Möbius committed
187 188 189 190 191
  
  // 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
192

Jan Möbius's avatar
 
Jan Möbius committed
193
  int topLevelAdded  = 0;
Jan Möbius's avatar
Jan Möbius committed
194

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

197 198
    emit updateContextMenu(objectId);

Jan Möbius's avatar
 
Jan Möbius committed
199
    // Add an empty Menu defining the current Type
200 201 202
    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
203 204


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

Jan Möbius's avatar
 
Jan Möbius committed
208 209 210 211 212 213 214 215
      // 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
216 217

        // Get all Actions in the menu and its submenus.
Jan Möbius's avatar
 
Jan Möbius committed
218 219 220 221
        // 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
222

Jan Möbius's avatar
 
Jan Möbius committed
223 224
          // 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
225 226
            allActions[j]->setData( QVariant( objectId ) );
            if ( allActions[j]->menu() != 0 )
Jan Möbius's avatar
 
Jan Möbius committed
227 228
              tmpList << allActions[j]->menu()->actions();
          }
Jan Möbius's avatar
Jan Möbius committed
229

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

    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
250
  }
Jan Möbius's avatar
Jan Möbius committed
251 252

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

Jan Möbius's avatar
 
Jan Möbius committed
255 256
  if ( topLevelAdded > 0 )
    contextMenu_->addSeparator();
Jan Möbius's avatar
Jan Möbius committed
257

Jan Möbius's avatar
 
Jan Möbius committed
258 259 260
  // 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
261 262

    // Get all Actions in the menu and its submenus.
Jan Möbius's avatar
 
Jan Möbius committed
263 264 265 266
    // 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
267

Jan Möbius's avatar
 
Jan Möbius committed
268 269
      // 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
270 271
        allActions[j]->setData( QVariant( objectId ) );
        if ( allActions[j]->menu() != 0 )
Jan Möbius's avatar
 
Jan Möbius committed
272 273
          tmpList << allActions[j]->menu()->actions();
      }
Jan Möbius's avatar
Jan Möbius committed
274

Jan Möbius's avatar
 
Jan Möbius committed
275 276
      allActions = tmpList;
    }
Jan Möbius's avatar
Jan Möbius committed
277

Jan Möbius's avatar
 
Jan Möbius committed
278
  }
Jan Möbius's avatar
Jan Möbius committed
279

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

284
  if (examiner_widgets_[0]->getPickMenu() != NULL) {
285 286 287 288 289
    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
290
  }
Jan Möbius's avatar
Jan Möbius committed
291

Dirk Wilden's avatar
 
Dirk Wilden committed
292 293
  // Add a functions menu
  QAction* action;
Dirk Wilden's avatar
Dirk Wilden committed
294
  if ( functionMenu_ == 0 ){
295

296
    functionMenu_ = new QMenu("&Functions",contextMenu_);
297

298
    //====================================================================================================
299

300 301 302
    action = functionMenu_->addAction("Snapshot");
    action->setToolTip("Make a snapshot");
    connect(action, SIGNAL(triggered()), this, SLOT( slotSnapshot() ) );
303

304
    //====================================================================================================
305

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

310
    //====================================================================================================
311

312
    functionMenu_->addSeparator();
313

314
    //====================================================================================================
315

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

320
    //====================================================================================================
321

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

326
    //====================================================================================================
327

328
    functionMenu_->addSeparator();
329

330
    //====================================================================================================
331

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

338
    //====================================================================================================
339

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

346
    //====================================================================================================
347

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

354 355 356 357
    functionMenu_->setTearOffEnabled(true);
  }

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

359
  if ( ( examiner_widgets_[PluginFunctions::activeExaminer()]->getDrawMenu() != NULL ) && OpenFlipper::Options::drawModesInContextMenu() ) {
Jan Möbius's avatar
Jan Möbius committed
360

361 362
    examiner_widgets_[PluginFunctions::activeExaminer()]->getDrawMenu()->setTitle("&DrawModes");
    QAction* drawMenuAction = contextMenu_->addMenu(examiner_widgets_[PluginFunctions::activeExaminer()]->getDrawMenu() );
Jan Möbius's avatar
Jan Möbius committed
363

Jan Möbius's avatar
 
Jan Möbius committed
364 365 366
    QIcon icon;
    icon.addFile(OpenFlipper::Options::iconDirStr()+OpenFlipper::Options::dirSeparator()+"drawModes.png");
    drawMenuAction->setIcon(icon);
Jan Möbius's avatar
Jan Möbius committed
367

368
    examiner_widgets_[PluginFunctions::activeExaminer()]->getDrawMenu()->setTearOffEnabled(true);
Jan Möbius's avatar
 
Jan Möbius committed
369 370 371
  }
}

372 373
void CoreWidget::slotSnapshotName() {
  std::cerr << "Todo : slotSnapShotName only sets name for current viewer" << std::endl;
374 375 376 377 378 379 380 381 382 383 384 385 386 387

  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);
  }

388 389
}

Dirk Wilden's avatar
Dirk Wilden committed
390 391 392 393 394 395 396
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);
}
397

Dirk Wilden's avatar
Dirk Wilden committed
398 399 400 401 402 403 404
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);
}
405

Dirk Wilden's avatar
Dirk Wilden committed
406 407 408 409 410 411
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
412 413
}

414
void CoreWidget::slotSnapshot() {
415
  examiner_widgets_[PluginFunctions::activeExaminer()]->snapshot();
416 417
}

418 419 420 421 422 423 424 425
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
426 427 428
void CoreWidget::slotAddContextMenu(QMenu* _menu) {
  MenuInfo info;
  info.menu = _menu;
Jan Möbius's avatar
Jan Möbius committed
429

Jan Möbius's avatar
 
Jan Möbius committed
430 431 432 433 434 435 436 437
  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
438

Jan Möbius's avatar
 
Jan Möbius committed
439 440 441 442
  contextMenus_.push_back(info);
}

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