ContextMenu.cc 15.2 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
/** \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"));  
  connect(action, SIGNAL(triggered()), this, SLOT(slotSetLocalBackgroundColor()) );
  
  //====================================================================================================
  
}
Jan Möbius's avatar
Jan Möbius committed
110

Jan Möbius's avatar
Jan Möbius committed
111 112 113 114 115 116 117 118 119 120 121
/** \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
122 123 124



Jan Möbius's avatar
Jan Möbius committed
125 126 127 128 129 130 131
/** \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
132

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

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

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

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

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

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

193 194
    emit updateContextMenu(objectId);

Jan Möbius's avatar
 
Jan Möbius committed
195
    // Add an empty Menu defining the current Type
196 197 198
    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
199 200


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

Jan Möbius's avatar
 
Jan Möbius committed
204 205 206 207 208 209 210 211
      // 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
212 213

        // Get all Actions in the menu and its submenus.
Jan Möbius's avatar
 
Jan Möbius committed
214 215 216 217
        // 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
218

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

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

    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
246
  }
Jan Möbius's avatar
Jan Möbius committed
247 248

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

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

Jan Möbius's avatar
 
Jan Möbius committed
254 255 256
  // 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
257 258

    // Get all Actions in the menu and its submenus.
Jan Möbius's avatar
 
Jan Möbius committed
259 260 261 262
    // 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
263

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

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

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

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

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

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

294
    //====================================================================================================
295

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

300
    //====================================================================================================
301

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

306
    //====================================================================================================
307

308
    functionMenu_->addSeparator();
309

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

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

316
    //====================================================================================================
317

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

322
    //====================================================================================================
323

324
    functionMenu_->addSeparator();
325

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

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

334
    //====================================================================================================
335

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

342
    //====================================================================================================
343

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

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

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

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

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

372 373
}

Dirk Wilden's avatar
Dirk Wilden committed
374 375 376 377 378 379 380
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);
}
381

Dirk Wilden's avatar
Dirk Wilden committed
382 383 384 385 386 387 388
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);
}
389

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

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

402 403 404 405 406 407 408 409
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
410 411 412
void CoreWidget::slotAddContextMenu(QMenu* _menu) {
  MenuInfo info;
  info.menu = _menu;
Jan Möbius's avatar
Jan Möbius committed
413

Jan Möbius's avatar
 
Jan Möbius committed
414 415 416 417 418 419 420 421
  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
422

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

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