1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "XGUI_ContextMenuMgr.h"
21 #include "XGUI_Workshop.h"
22 #include "XGUI_ObjectsBrowser.h"
23 #include "XGUI_SelectionMgr.h"
24 #include "XGUI_Displayer.h"
25 #include "XGUI_ViewerProxy.h"
26 #include "XGUI_SalomeConnector.h"
27 #include "XGUI_Selection.h"
28 #include "XGUI_SelectionActivate.h"
29 #include "XGUI_DataModel.h"
30 #include "XGUI_OperationMgr.h"
31 #include "XGUI_Tools.h"
32 #include "XGUI_ActionsMgr.h"
35 #include <AppElements_MainWindow.h>
38 //#include "PartSetPlugin_Part.h"
40 #include <ModelAPI_Data.h>
41 #include <ModelAPI_AttributeDocRef.h>
42 #include <ModelAPI_Object.h>
43 #include <ModelAPI_Session.h>
44 #include <ModelAPI_ResultGroup.h>
45 #include <ModelAPI_ResultParameter.h>
46 #include <ModelAPI_ResultConstruction.h>
47 #include <ModelAPI_ResultBody.h>
48 #include <ModelAPI_Tools.h>
49 #include <ModelAPI_ResultField.h>
50 #include <ModelAPI_Folder.h>
51 #include <ModelAPI_AttributeReference.h>
52 #include <ModelAPI_ResultField.h>
54 #include <Config_DataModelReader.h>
56 #include <ModuleBase_IModule.h>
57 #include <ModuleBase_Tools.h>
58 #include <ModuleBase_OperationAction.h>
59 #include <ModuleBase_ViewerPrs.h>
62 #include <QActionGroup>
63 #include <QContextMenuEvent>
66 #include <QMainWindow>
67 #include <QModelIndex>
69 XGUI_ContextMenuMgr::XGUI_ContextMenuMgr(XGUI_Workshop* theParent)
71 myWorkshop(theParent),
72 mySeparator1(0), mySeparator2(0), mySeparator3(0)
76 XGUI_ContextMenuMgr::~XGUI_ContextMenuMgr()
80 void XGUI_ContextMenuMgr::createActions()
83 QMainWindow* aDesktop = myWorkshop->salomeConnector()->desktop();
85 QMainWindow* aDesktop = myWorkshop->mainWindow();
88 QAction* aAction = ModuleBase_Tools::createAction(QIcon(":pictures/delete.png"), tr("Delete"),
90 aDesktop->addAction(aAction);
92 addAction("DELETE_CMD", aAction);
93 aAction->setShortcutContext(Qt::ApplicationShortcut);
95 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/rename_edit.png"), tr("Rename"),
96 aDesktop, this, SLOT(onRename()));
97 addAction("RENAME_CMD", aAction);
99 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/move.png"),
100 XGUI_Workshop::MOVE_TO_END_COMMAND, this);
101 addAction("MOVE_CMD", aAction);
103 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/clean_history.png"),
104 tr("Clean history"), aDesktop);
105 addAction("CLEAN_HISTORY_CMD", aAction);
107 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/color.png"), tr("Color..."), aDesktop);
108 addAction("COLOR_CMD", aAction);
110 aAction = ModuleBase_Tools::createAction(QIcon(""), tr("Deflection..."), aDesktop);
111 addAction("DEFLECTION_CMD", aAction);
113 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/transparency.png"),
114 tr("Transparency..."), aDesktop);
115 addAction("TRANSPARENCY_CMD", aAction);
117 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/eye_pencil.png"), tr("Show"), aDesktop);
118 addAction("SHOW_CMD", aAction);
120 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/eye_pencil.png"), tr("Show only"),
122 addAction("SHOW_ONLY_CMD", aAction);
124 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide"),
126 addAction("HIDE_CMD", aAction);
128 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide all"),
130 addAction("HIDEALL_CMD", aAction);
132 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/shading.png"), tr("Shading"), aDesktop);
133 addAction("SHADING_CMD", aAction);
135 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/wireframe.png"), tr("Wireframe"),
137 addAction("WIREFRAME_CMD", aAction);
139 mySeparator1 = ModuleBase_Tools::createAction(QIcon(), "", aDesktop);
140 mySeparator1->setSeparator(true);
142 mySeparator2 = ModuleBase_Tools::createAction(QIcon(), "", aDesktop);
143 mySeparator2->setSeparator(true);
145 mySeparator3 = ModuleBase_Tools::createAction(QIcon(), "", aDesktop);
146 mySeparator3->setSeparator(true);
148 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/vertex.png"), tr("Vertices"), aDesktop,
149 this, SLOT(onShapeSelection(bool)));
150 aAction->setCheckable(true);
151 addAction("SELECT_VERTEX_CMD", aAction);
153 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/edge.png"), tr("Edges"), aDesktop,
154 this, SLOT(onShapeSelection(bool)));
155 aAction->setCheckable(true);
156 addAction("SELECT_EDGE_CMD", aAction);
158 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/face.png"), tr("Faces"), aDesktop,
159 this, SLOT(onShapeSelection(bool)));
160 aAction->setCheckable(true);
161 addAction("SELECT_FACE_CMD", aAction);
163 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/result.png"), tr("Results"), aDesktop,
164 this, SLOT(onResultSelection(bool)));
165 aAction->setCheckable(true);
166 addAction("SELECT_RESULT_CMD", aAction);
168 aAction->setChecked(true);
170 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/find_result.png"),
171 tr("Select results"), aDesktop);
172 addAction("SHOW_RESULTS_CMD", aAction);
174 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/find_result.png"),
175 tr("Select parent feature"), aDesktop);
176 addAction("SHOW_FEATURE_CMD", aAction);
179 aAction = ModuleBase_Tools::createAction(QIcon(), tr("TInspector"), aDesktop);
180 addAction("TINSPECTOR_VIEW", aAction);
183 // Features folders actions
184 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/create_folder.png"),
185 tr("Insert a folder before"), aDesktop);
186 addAction("INSERT_FOLDER_CMD", aAction);
188 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/insert_folder_before.png"),
189 tr("Move into the previous folder"), aDesktop);
190 addAction("ADD_TO_FOLDER_BEFORE_CMD", aAction);
192 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/insert_folder_after.png"),
193 tr("Move into the next folder"), aDesktop);
194 addAction("ADD_TO_FOLDER_AFTER_CMD", aAction);
196 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/move_out_before.png"),
197 tr("Move out before the folder"), aDesktop);
198 addAction("ADD_OUT_FOLDER_BEFORE_CMD", aAction);
200 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/move_out_after.png"),
201 tr("Move out after the folder"), aDesktop);
202 addAction("ADD_OUT_FOLDER_AFTER_CMD", aAction);
204 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/normal-view-inversed.png"),
205 tr("Set view by inverted normal to face"), aDesktop);
206 addAction("SET_VIEW_INVERTEDNORMAL_CMD", aAction);
208 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/normal-view.png"),
209 tr("Set view by normal to face"), aDesktop);
210 addAction("SET_VIEW_NORMAL_CMD", aAction);
212 buildObjBrowserMenu();
216 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
218 if (myActions.contains(theId))
219 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
220 theAction->setData(theId);
221 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
222 myActions[theId] = theAction;
225 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
227 if (myActions.contains(theId))
228 return myActions[theId];
232 QAction* XGUI_ContextMenuMgr::actionByName(const QString& theName) const
234 foreach(QAction* eachAction, myActions) {
235 if (eachAction->text() == theName) {
242 QStringList XGUI_ContextMenuMgr::actionIds() const
244 return myActions.keys();
247 void XGUI_ContextMenuMgr::onAction(bool isChecked)
249 QAction* aAction = static_cast<QAction*>(sender());
250 emit actionTriggered(aAction->data().toString(), isChecked);
253 void XGUI_ContextMenuMgr::updateCommandsStatus()
257 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
259 QMenu* aMenu = new QMenu();
260 if (sender() == myWorkshop->objectBrowser()) {
261 updateObjectBrowserMenu();
262 addObjBrowserMenu(aMenu);
263 } else if (sender() == myWorkshop->viewer()) {
265 addViewerMenu(aMenu);
268 if (aMenu && (aMenu->actions().size() > 0)) {
269 // it is possible that some objects should do something before and after the popup menu exec
270 // e.g. a sketch manager changes an internal flag on this signals in order to do not hide
272 emit beforeContextMenu();
273 aMenu->exec(theEvent->globalPos());
274 emit afterContextMenu();
279 void XGUI_ContextMenuMgr::updateObjectBrowserMenu()
281 foreach(QAction* aAction, myActions)
282 aAction->setEnabled(false);
284 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
285 QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
286 int aSelected = aObjects.size();
288 SessionPtr aMgr = ModelAPI_Session::get();
289 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
290 bool hasResult = false;
291 bool hasFeature = false;
292 bool hasParameter = false;
293 bool hasCompositeOwner = false;
294 bool hasResultInHistory = false;
295 bool hasFolder = false;
296 bool canBeDeleted = true;
297 ModuleBase_Tools::checkObjects(aObjects, hasResult, hasFeature, hasParameter,
298 hasCompositeOwner, hasResultInHistory, hasFolder);
300 if (aSelected == 1) { // single selection
301 ObjectPtr aObject = aObjects.first();
303 if (hasResult && myWorkshop->canBeShaded(aObject)) {
304 XGUI_Displayer::DisplayMode aMode = aDisplayer->displayMode(aObject);
305 if (aMode != XGUI_Displayer::NoMode) {
306 action("WIREFRAME_CMD")->setEnabled(aMode == XGUI_Displayer::Shading);
307 action("SHADING_CMD")->setEnabled(aMode == XGUI_Displayer::Wireframe);
309 action("WIREFRAME_CMD")->setEnabled(true);
310 action("SHADING_CMD")->setEnabled(true);
314 bool aHasSubResults = ModelAPI_Tools::hasSubResults(
315 std::dynamic_pointer_cast<ModelAPI_Result>(aObject));
316 if (aHasSubResults) {
317 action("HIDE_CMD")->setEnabled(true);
318 action("SHOW_CMD")->setEnabled(true);
321 if (aObject->isDisplayed()) {
322 action("HIDE_CMD")->setEnabled(true);
323 } else if (hasResult && (!hasParameter)) {
324 action("SHOW_CMD")->setEnabled(true);
327 if (!(hasParameter || hasFeature))
328 action("SHOW_ONLY_CMD")->setEnabled(true);
330 else if (hasFeature && myWorkshop->canMoveFeature())
331 action("MOVE_CMD")->setEnabled(true);
333 if( aMgr->activeDocument() == aObject->document() )
335 action("RENAME_CMD")->setEnabled(true);
336 if (aObject->groupName() == ModelAPI_ResultConstruction::group()) {
337 FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
338 canBeDeleted = aFeature->isInHistory();
339 action("DELETE_CMD")->setEnabled(canBeDeleted);
342 action("DELETE_CMD")->setEnabled(!hasCompositeOwner);
343 action("CLEAN_HISTORY_CMD")->setEnabled(!hasCompositeOwner &&
344 (hasFeature || hasParameter));
347 // end single selection
348 } else { // multi-selection
349 // parameter is commented because the actions are not in the list of result parameter actions
350 if (hasResult /*&& (!hasParameter)*/) {
351 action("SHOW_CMD")->setEnabled(true);
352 action("HIDE_CMD")->setEnabled(true);
353 action("SHOW_ONLY_CMD")->setEnabled(true);
354 action("SHADING_CMD")->setEnabled(true);
355 action("WIREFRAME_CMD")->setEnabled(true);
357 foreach(ObjectPtr aObj, aObjects) {
358 FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
359 if (!aFeature->isInHistory()) {
360 canBeDeleted = false;
364 action("DELETE_CMD")->setEnabled(canBeDeleted);
366 if (hasFeature && myWorkshop->canMoveFeature())
367 action("MOVE_CMD")->setEnabled(true);
368 } // end multi-selection
370 // Check folder management commands state if only features are selected
371 if ((!hasResult) && hasFeature && (!hasParameter) && (!hasCompositeOwner) &&
372 (!hasResultInHistory) && (!hasFolder)) {
373 std::list<FeaturePtr> aFeatures = aSelMgr->getSelectedFeatures();
374 if (aFeatures.size() > 0) { // Check that features do not include Parts
375 QModelIndexList aIndexes = aSelMgr->selection()->selectedIndexes();
376 QModelIndex aFirstIdx = aIndexes.first();
377 QModelIndex aLastIdx = aIndexes.last();
378 QModelIndex aParentIdx = aFirstIdx.parent();
380 // if all selected are from the same level
381 bool isSameParent = true;
382 foreach(QModelIndex aIdx, aIndexes) {
383 if (aIdx.parent() != aParentIdx) {
384 isSameParent = false;
389 // Check is selection continuous
390 XGUI_DataModel* aModel = myWorkshop->objectBrowser()->dataModel();
391 DocumentPtr aDoc = aMgr->activeDocument();
392 std::list<FeaturePtr> aFeatures = aSelMgr->getSelectedFeatures();
394 bool isContinuos = true;
397 foreach(FeaturePtr aF, aFeatures) {
399 aId = aDoc->index(aF);
402 if (aId != aDoc->index(aF)) {
410 ObjectPtr aDataObj = aModel->object(aParentIdx);
413 if (aFirstIdx.row() > 0) {
414 QModelIndex aPrevIdx = aFirstIdx.sibling(aFirstIdx.row() - 1, 0);
415 aPrevObj = aModel->object(aPrevIdx);
419 if (aLastIdx.row() < (aModel->rowCount(aParentIdx) - 1)) {
420 QModelIndex aNextIdx = aFirstIdx.sibling(aLastIdx.row() + 1, 0);
421 aNextObj = aModel->object(aNextIdx);
424 bool isPrevFolder = (aPrevObj.get() &&
425 (aPrevObj->groupName() == ModelAPI_Folder::group()));
426 bool isNextFolder = (aNextObj.get() &&
427 (aNextObj->groupName() == ModelAPI_Folder::group()));
428 bool isInFolder = (aDataObj.get() &&
429 (aDataObj->groupName() == ModelAPI_Folder::group()));
430 bool isOutsideFolder = !isInFolder;
432 bool hasFirst = false;
433 bool hasLast = false;
435 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(aDataObj);
436 FeaturePtr aFirstFeatureInFolder;
437 AttributeReferencePtr aFirstFeatAttr =
438 aFolder->data()->reference(ModelAPI_Folder::FIRST_FEATURE_ID());
440 aFirstFeatureInFolder = ModelAPI_Feature::feature(aFirstFeatAttr->value());
441 hasFirst = (aFirstFeatureInFolder == aFeatures.front());
443 FeaturePtr aLastFeatureInFolder;
444 AttributeReferencePtr aLastFeatAttr =
445 aFolder->data()->reference(ModelAPI_Folder::LAST_FEATURE_ID());
447 aLastFeatureInFolder = ModelAPI_Feature::feature(aLastFeatAttr->value());
448 hasLast = (aLastFeatureInFolder == aFeatures.back());
450 action("INSERT_FOLDER_CMD")->setEnabled(isOutsideFolder);
451 action("ADD_TO_FOLDER_BEFORE_CMD")->setEnabled(isOutsideFolder && isPrevFolder);
452 action("ADD_TO_FOLDER_AFTER_CMD")->setEnabled(isOutsideFolder && isNextFolder);
453 action("ADD_OUT_FOLDER_BEFORE_CMD")->setEnabled(isInFolder && hasFirst);
454 action("ADD_OUT_FOLDER_AFTER_CMD")->setEnabled(isInFolder && hasLast);
458 } // end folder management commands
460 bool allActive = true;
461 foreach( ObjectPtr aObject, aObjects )
462 if( aMgr->activeDocument() != aObject->document() ) {
466 if (!hasCompositeOwner && allActive ) {
467 if (hasResult || hasFeature || hasParameter) // #2924 results can be erased
468 action("DELETE_CMD")->setEnabled(canBeDeleted);
470 if (!hasCompositeOwner && allActive && (hasFeature|| hasParameter))
471 action("CLEAN_HISTORY_CMD")->setEnabled(true);
473 action("SHOW_RESULTS_CMD")->setEnabled(hasFeature);
474 action("SHOW_FEATURE_CMD")->setEnabled(hasResult && hasResultInHistory);
475 } // end selection processing
477 // Show/Hide command has to be disabled for objects from non active document
478 bool aDeactivate = false;
479 foreach (ObjectPtr aObj, aObjects) {
480 if (!aObj->document()->isActive()) {
481 if ((aObj->document() != ModelAPI_Session::get()->moduleDocument()) ||
482 aObj->groupName() == ModelAPI_ResultPart::group()) {
489 // If at leas a one object can not be edited then Show/Hide has to be disabled
490 action("SHOW_CMD")->setEnabled(false);
491 action("HIDE_CMD")->setEnabled(false);
492 action("SHOW_ONLY_CMD")->setEnabled(false);
495 action("COLOR_CMD")->setEnabled(myWorkshop->canChangeProperty("COLOR_CMD"));
496 action("DEFLECTION_CMD")->setEnabled(myWorkshop->canChangeProperty("DEFLECTION_CMD"));
497 action("TRANSPARENCY_CMD")->setEnabled(myWorkshop->canChangeProperty("TRANSPARENCY_CMD"));
500 action("TINSPECTOR_VIEW")->setEnabled(true);
505 ModuleBase_IModule* aModule = myWorkshop->module();
507 aModule->updateObjectBrowserMenu(myActions);
510 void XGUI_ContextMenuMgr::updateViewerMenu()
512 foreach(QAction* aAction, myActions)
513 aAction->setEnabled(false);
515 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
516 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
517 QList<ModuleBase_ViewerPrsPtr> aPrsList =
518 aSelMgr->selection()->getSelected(ModuleBase_ISelection::Viewer);
519 if (aPrsList.size() > 0) {
520 bool isVisible = false;
521 bool isShading = false;
522 bool canBeShaded = false;
523 bool hasPlanar = false;
525 foreach(ModuleBase_ViewerPrsPtr aPrs, aPrsList) {
526 aObject = aPrs->object();
529 GeomShapePtr aShape = aPrs->shape();
530 if (aObject->isDisplayed()) {
532 canBeShaded = myWorkshop->displayer()->canBeShaded(aObject);
534 (myWorkshop->displayer()->displayMode(aObject) == XGUI_Displayer::Shading);
537 if (aShape->isPlanar()) {
544 XGUI_Displayer::DisplayMode aMode = aDisplayer->displayMode(aObject);
545 if (aMode != XGUI_Displayer::NoMode) {
546 action("WIREFRAME_CMD")->setEnabled(aMode == XGUI_Displayer::Shading);
547 action("SHADING_CMD")->setEnabled(aMode == XGUI_Displayer::Wireframe);
549 action("WIREFRAME_CMD")->setEnabled(true);
550 action("SHADING_CMD")->setEnabled(true);
553 action("SHOW_ONLY_CMD")->setEnabled(true);
554 action("HIDE_CMD")->setEnabled(true);
556 action("SHOW_CMD")->setEnabled(true);
558 action("SET_VIEW_NORMAL_CMD")->setEnabled(hasPlanar);
559 action("SET_VIEW_INVERTEDNORMAL_CMD")->setEnabled(hasPlanar);
561 //issue #2159 Hide all incomplete behavior
563 action("HIDEALL_CMD")->setEnabled(true);
565 if (myWorkshop->displayer()->objectsCount() > 0)
566 action("HIDEALL_CMD")->setEnabled(true);
569 // Update selection menu
570 QIntList aModes = myWorkshop->selectionActivate()->activeSelectionModes();
571 action("SELECT_VERTEX_CMD")->setEnabled(true);
572 action("SELECT_EDGE_CMD")->setEnabled(true);
573 action("SELECT_FACE_CMD")->setEnabled(true);
574 action("SELECT_RESULT_CMD")->setEnabled(true);
576 action("SELECT_RESULT_CMD")->setChecked(false);
577 action("SELECT_VERTEX_CMD")->setChecked(false);
578 action("SELECT_EDGE_CMD")->setChecked(false);
579 action("SELECT_FACE_CMD")->setChecked(false);
580 action("SELECT_RESULT_CMD")->setChecked(false);
581 if (aModes.count() == 0) {
582 action("SELECT_RESULT_CMD")->setChecked(true);
584 foreach(int aMode, aModes) {
587 action("SELECT_VERTEX_CMD")->setChecked(true);
590 action("SELECT_EDGE_CMD")->setChecked(true);
593 action("SELECT_FACE_CMD")->setChecked(true);
596 action("SELECT_RESULT_CMD")->setChecked(true);
601 ModuleBase_IModule* aModule = myWorkshop->module();
603 aModule->updateViewerMenu(myActions);
605 if (myWorkshop->canChangeProperty("COLOR_CMD"))
606 action("COLOR_CMD")->setEnabled(true);
608 if (myWorkshop->canChangeProperty("DEFLECTION_CMD"))
609 action("DEFLECTION_CMD")->setEnabled(true);
611 if (myWorkshop->canChangeProperty("TRANSPARENCY_CMD"))
612 action("TRANSPARENCY_CMD")->setEnabled(true);
614 // Delete command is not used in viewer pop-up menu
615 action("DELETE_CMD")->setEnabled(false);
618 void XGUI_ContextMenuMgr::connectObjectBrowser()
620 connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
621 SLOT(onContextMenuRequest(QContextMenuEvent*)));
624 void XGUI_ContextMenuMgr::connectViewer()
626 connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
627 SLOT(onContextMenuRequest(QContextMenuEvent*)));
631 void XGUI_ContextMenuMgr::buildObjBrowserMenu()
633 QAction* aSeparator = ModuleBase_Tools::createAction(QIcon(), "", myWorkshop->desktop());
634 aSeparator->setSeparator(true);
638 // Result construction menu
639 aList.append(action("SHOW_CMD"));
640 aList.append(action("HIDE_CMD"));
641 aList.append(action("SHOW_ONLY_CMD"));
642 aList.append(mySeparator1);
643 aList.append(action("RENAME_CMD"));
644 aList.append(action("COLOR_CMD"));
645 aList.append(action("DEFLECTION_CMD"));
646 aList.append(action("TRANSPARENCY_CMD"));
647 aList.append(action("SHOW_FEATURE_CMD"));
648 aList.append(mySeparator2);
649 aList.append(action("DELETE_CMD"));
650 myObjBrowserMenus[ModelAPI_ResultConstruction::group()] = aList;
652 //-------------------------------------
655 aList.append(action("WIREFRAME_CMD"));
656 aList.append(action("SHADING_CMD"));
657 aList.append(mySeparator1); // this separator is not shown as this action is added after show only
658 // qt list container contains only one instance of the same action
659 aList.append(action("SHOW_CMD"));
660 aList.append(action("HIDE_CMD"));
661 aList.append(action("SHOW_ONLY_CMD"));
662 aList.append(mySeparator2);
663 aList.append(action("RENAME_CMD"));
664 aList.append(action("COLOR_CMD"));
665 aList.append(action("DEFLECTION_CMD"));
666 aList.append(action("TRANSPARENCY_CMD"));
667 aList.append(action("SHOW_FEATURE_CMD"));
668 aList.append(mySeparator3);
669 aList.append(action("DELETE_CMD"));
670 myObjBrowserMenus[ModelAPI_ResultBody::group()] = aList;
672 myObjBrowserMenus[ModelAPI_ResultGroup::group()] = aList;
673 myObjBrowserMenus[ModelAPI_ResultField::group()] = aList;
675 myObjBrowserMenus[ModelAPI_ResultPart::group()] = aList;
676 //-------------------------------------
679 aList.append(action("RENAME_CMD"));
680 aList.append(action("SHOW_RESULTS_CMD"));
681 aList.append(action("MOVE_CMD"));
682 aList.append(mySeparator1);
683 aList.append(action("INSERT_FOLDER_CMD"));
684 aList.append(action("ADD_TO_FOLDER_BEFORE_CMD"));
685 aList.append(action("ADD_TO_FOLDER_AFTER_CMD"));
686 aList.append(mySeparator2);
687 aList.append(action("ADD_OUT_FOLDER_BEFORE_CMD"));
688 aList.append(action("ADD_OUT_FOLDER_AFTER_CMD"));
689 aList.append(mySeparator3);
690 aList.append(action("CLEAN_HISTORY_CMD"));
691 aList.append(action("DELETE_CMD"));
692 myObjBrowserMenus[ModelAPI_Feature::group()] = aList;
695 aList.append(action("RENAME_CMD"));
696 aList.append(mySeparator1);
697 aList.append(action("CLEAN_HISTORY_CMD"));
698 aList.append(action("DELETE_CMD"));
699 myObjBrowserMenus[ModelAPI_ResultParameter::group()] = aList;
700 //-------------------------------------
703 aList.append(action("RENAME_CMD"));
704 aList.append(action("DELETE_CMD"));
705 myObjBrowserMenus[ModelAPI_Folder::group()] = aList;
707 //---------------------------------------
710 aList.append(action("SHOW_CMD"));
711 aList.append(action("HIDE_CMD"));
712 aList.append(action("SHOW_ONLY_CMD"));
713 myObjBrowserMenus[ModelAPI_ResultField::ModelAPI_FieldStep::group()] = aList;
716 void XGUI_ContextMenuMgr::buildViewerMenu()
719 // Result construction menu
720 aList.append(action("COLOR_CMD"));
721 aList.append(action("DEFLECTION_CMD"));
722 aList.append(action("TRANSPARENCY_CMD"));
723 aList.append(mySeparator3);
724 aList.append(action("SET_VIEW_NORMAL_CMD"));
725 aList.append(action("SET_VIEW_INVERTEDNORMAL_CMD"));
726 aList.append(mySeparator1);
727 aList.append(action("SHOW_ONLY_CMD"));
728 aList.append(action("HIDE_CMD"));
729 myViewerMenu[ModelAPI_ResultConstruction::group()] = aList;
731 myViewerMenu[ModelAPI_ResultPart::group()] = aList;
732 //-------------------------------------
735 aList.append(action("WIREFRAME_CMD"));
736 aList.append(action("SHADING_CMD"));
737 aList.append(mySeparator2);
738 aList.append(action("COLOR_CMD"));
739 aList.append(action("DEFLECTION_CMD"));
740 aList.append(action("TRANSPARENCY_CMD"));
741 aList.append(mySeparator3);
742 aList.append(action("SET_VIEW_NORMAL_CMD"));
743 aList.append(action("SET_VIEW_INVERTEDNORMAL_CMD"));
744 aList.append(mySeparator1);
745 aList.append(action("SHOW_ONLY_CMD"));
746 aList.append(action("HIDE_CMD"));
747 myViewerMenu[ModelAPI_ResultBody::group()] = aList;
749 myViewerMenu[ModelAPI_ResultGroup::group()] = aList;
750 myViewerMenu[ModelAPI_ResultField::group()] = aList;
751 //-------------------------------------
754 aList.append(action("HIDE_CMD"));
755 myViewerMenu[ModelAPI_ResultField::ModelAPI_FieldStep::group()] = aList;
759 void XGUI_ContextMenuMgr::addObjBrowserMenu(QMenu* theMenu) const
761 ModuleBase_IModule* aModule = myWorkshop->module();
763 theMenu->addSeparator();
764 aModule->addObjectBrowserMenu(theMenu);
767 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
768 QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
769 int aSelected = aObjects.size();
770 QActionsList aActions;
771 if (aSelected == 1) {
772 ObjectPtr aObject = aObjects.first();
773 std::string aName = aObject->groupName();
774 if (myObjBrowserMenus.contains(aName))
775 aActions = myObjBrowserMenus[aName];
776 } else if (aSelected > 1) {
777 aActions.append(action("WIREFRAME_CMD"));
778 aActions.append(action("SHADING_CMD"));
779 aActions.append(mySeparator1);
780 aActions.append(action("SHOW_CMD"));
781 aActions.append(action("HIDE_CMD"));
782 aActions.append(action("SHOW_ONLY_CMD"));
783 aActions.append(mySeparator2);
784 aActions.append(action("ADD_TO_FOLDER_BEFORE_CMD"));
785 aActions.append(action("ADD_TO_FOLDER_AFTER_CMD"));
786 aActions.append(action("ADD_OUT_FOLDER_BEFORE_CMD"));
787 aActions.append(action("ADD_OUT_FOLDER_AFTER_CMD"));
788 aActions.append(mySeparator3);
789 aActions.append(action("MOVE_CMD"));
790 aActions.append(action("COLOR_CMD"));
791 aActions.append(action("DEFLECTION_CMD"));
792 aActions.append(action("TRANSPARENCY_CMD"));
793 aActions.append(action("CLEAN_HISTORY_CMD"));
794 aActions.append(action("DELETE_CMD"));
797 if (aSelected == 0) {
799 aActions.append(action("TINSPECTOR_VIEW"));
803 theMenu->addActions(aActions);
804 addFeatures(theMenu);
806 // It is commented out because Object Browser does not have actions
807 //theMenu->addSeparator();
808 //theMenu->addActions(myWorkshop->objectBrowser()->actions());
811 void XGUI_ContextMenuMgr::addViewerMenu(QMenu* theMenu) const
813 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
814 QList<ModuleBase_ViewerPrsPtr> aPrsList =
815 aSelMgr->selection()->getSelected(ModuleBase_ISelection::Viewer);
816 int aSelected = aPrsList.size();
817 QActionsList aActions;
819 // Create selection menu
820 XGUI_OperationMgr* aOpMgr = myWorkshop->operationMgr();
821 if (!aOpMgr->hasOperation() &&
822 myWorkshop->selectionActivate()->activeSelectionPlace() == XGUI_SelectionActivate::Workshop) {
823 QMenu* aSelMenu = new QMenu(tr("Selection mode"), theMenu);
824 aSelMenu->addAction(action("SELECT_VERTEX_CMD"));
825 aSelMenu->addAction(action("SELECT_EDGE_CMD"));
826 aSelMenu->addAction(action("SELECT_FACE_CMD"));
827 //IMP: an attempt to use result selection with other selection modes
828 //aSelMenu->addSeparator();
829 aSelMenu->addAction(action("SELECT_RESULT_CMD"));
830 theMenu->addMenu(aSelMenu);
831 theMenu->addSeparator();
833 if (aSelected == 1) {
834 ObjectPtr aObject = aPrsList.first()->object();
835 if (aObject.get() != NULL) {
836 std::string aName = aObject->groupName();
837 if (myViewerMenu.contains(aName))
838 aActions = myViewerMenu[aName];
840 } else if (aSelected > 1) {
841 aActions.append(action("COLOR_CMD"));
842 aActions.append(action("DEFLECTION_CMD"));
843 aActions.append(action("TRANSPARENCY_CMD"));
844 aActions.append(mySeparator1);
845 aActions.append(action("SHOW_ONLY_CMD"));
846 aActions.append(action("HIDE_CMD"));
848 // hide all is shown always even if selection in the viewer is empty
849 aActions.append(action("HIDEALL_CMD"));
850 theMenu->addActions(aActions);
852 QMap<int, QAction*> aMenuActions;
853 ModuleBase_IModule* aModule = myWorkshop->module();
855 if (aModule->addViewerMenu(myActions, theMenu, aMenuActions))
856 theMenu->addSeparator();
859 // insert the module menu items on specific positions in the popup menu: some actions should be
860 // in the begin of the list, Delete action should be the last by #1343 issue
861 QList<QAction*> anActions = theMenu->actions();
862 int anActionsSize = anActions.size();
863 QAction* aFirstAction = anActions[0];
864 QMap<int, QAction*>::const_iterator anIt = aMenuActions.begin(), aLast = aMenuActions.end();
865 for (; anIt != aLast; anIt++) {
866 if (anIt.key() > anActionsSize)
867 theMenu->addAction(anIt.value());
869 theMenu->insertAction(aFirstAction, *anIt);
873 theMenu->addSeparator();
874 QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
875 if (aMDI->actions().size() > 0) {
876 QMenu* aSubMenu = theMenu->addMenu(tr("Windows"));
877 aSubMenu->addActions(aMDI->actions());
882 QStringList XGUI_ContextMenuMgr::actionObjectGroups(const QString& theName)
886 QMap<std::string, QActionsList>::const_iterator anIt = myObjBrowserMenus.begin(),
887 aLast = myObjBrowserMenus.end();
888 for (; anIt != aLast; anIt++) {
889 QString aGroupName(anIt.key().c_str());
890 if (aGroups.contains(aGroupName))
892 QActionsList anActions = anIt.value();
893 QActionsList::const_iterator anAIt = anActions.begin(), anALast = anActions.end();
895 for (; anAIt != anALast && !aFound; anAIt++)
896 aFound = (*anAIt)->data().toString() == theName;
898 aGroups.append(aGroupName);
903 void XGUI_ContextMenuMgr::onRename()
905 QObjectPtrList anObjects = myWorkshop->selector()->selection()->selectedObjects();
906 if (!myWorkshop->abortAllOperations())
908 // restore selection in case if dialog box was shown
909 myWorkshop->objectBrowser()->setObjectsSelected(anObjects);
910 myWorkshop->objectBrowser()->onEditItem();
913 void XGUI_ContextMenuMgr::addFeatures(QMenu* theMenu) const
915 SessionPtr aMgr = ModelAPI_Session::get();
916 DocumentPtr aActiveDoc = aMgr->activeDocument();
918 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
919 XGUI_ActionsMgr* aActionMgr = myWorkshop->actionsMgr();
920 const Config_DataModelReader* aDataModelXML = myWorkshop->dataModelXMLReader();
921 QModelIndexList aSelectedIndexes = aSelMgr->selection()->selectedIndexes();
925 bool aIsRoot = false;
926 foreach(QModelIndex aIdx, aSelectedIndexes) {
927 // Process only first column
928 if (aIdx.column() == 1) {
929 aIsRoot = !aIdx.parent().isValid();
930 // Exit if the selected index belongs to non active document
931 if (aIsRoot && (aActiveDoc != aMgr->moduleDocument()))
934 // Get name of the selected index
935 aName = aIdx.data().toString();
936 aLen = aName.indexOf('(');
938 aName = aName.left(--aLen);
940 std::string aFeaturesStr = aIsRoot?
941 aDataModelXML->rootFolderFeatures(aName.toStdString()) :
942 aDataModelXML->subFolderFeatures(aName.toStdString());
943 if (aFeaturesStr.length() > 0) {
944 QStringList aFeturesList =
945 QString(aFeaturesStr.c_str()).split(",", QString::SkipEmptyParts);
946 foreach(QString aFea, aFeturesList) {
947 QAction* aAction = aActionMgr->action(aFea);
949 theMenu->addAction(aAction);
956 #define UNCHECK_ACTION(NAME) \
957 { QAction* aAction = action(NAME); \
958 bool isBlock = aAction->signalsBlocked(); \
959 aAction->blockSignals(true); \
960 aAction->setChecked(false); \
961 aAction->blockSignals(isBlock); }
964 void XGUI_ContextMenuMgr::onResultSelection(bool theChecked)
966 UNCHECK_ACTION("SELECT_VERTEX_CMD");
967 UNCHECK_ACTION("SELECT_EDGE_CMD");
968 UNCHECK_ACTION("SELECT_FACE_CMD");
971 void XGUI_ContextMenuMgr::onShapeSelection(bool theChecked)
973 UNCHECK_ACTION("SHOW_RESULTS_CMD");