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 } // end multi-selection
368 // Check folder management commands state if only features are selected
369 if ((!hasResult) && hasFeature && (!hasParameter) && (!hasCompositeOwner) &&
370 (!hasResultInHistory) && (!hasFolder)) {
371 std::list<FeaturePtr> aFeatures = aSelMgr->getSelectedFeatures();
372 if (aFeatures.size() > 0) { // Check that features do not include Parts
373 QModelIndexList aIndexes = aSelMgr->selection()->selectedIndexes();
374 QModelIndex aFirstIdx = aIndexes.first();
375 QModelIndex aLastIdx = aIndexes.last();
376 QModelIndex aParentIdx = aFirstIdx.parent();
378 // if all selected are from the same level
379 bool isSameParent = true;
380 foreach(QModelIndex aIdx, aIndexes) {
381 if (aIdx.parent() != aParentIdx) {
382 isSameParent = false;
387 // Check is selection continuous
388 XGUI_DataModel* aModel = myWorkshop->objectBrowser()->dataModel();
389 DocumentPtr aDoc = aMgr->activeDocument();
390 std::list<FeaturePtr> aFeatures = aSelMgr->getSelectedFeatures();
392 bool isContinuos = true;
395 foreach(FeaturePtr aF, aFeatures) {
397 aId = aDoc->index(aF);
400 if (aId != aDoc->index(aF)) {
408 ObjectPtr aDataObj = aModel->object(aParentIdx);
411 if (aFirstIdx.row() > 0) {
412 QModelIndex aPrevIdx = aFirstIdx.sibling(aFirstIdx.row() - 1, 0);
413 aPrevObj = aModel->object(aPrevIdx);
417 if (aLastIdx.row() < (aModel->rowCount(aParentIdx) - 1)) {
418 QModelIndex aNextIdx = aFirstIdx.sibling(aLastIdx.row() + 1, 0);
419 aNextObj = aModel->object(aNextIdx);
422 bool isPrevFolder = (aPrevObj.get() &&
423 (aPrevObj->groupName() == ModelAPI_Folder::group()));
424 bool isNextFolder = (aNextObj.get() &&
425 (aNextObj->groupName() == ModelAPI_Folder::group()));
426 bool isInFolder = (aDataObj.get() &&
427 (aDataObj->groupName() == ModelAPI_Folder::group()));
428 bool isOutsideFolder = !isInFolder;
430 bool hasFirst = false;
431 bool hasLast = false;
433 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(aDataObj);
434 FeaturePtr aFirstFeatureInFolder;
435 AttributeReferencePtr aFirstFeatAttr =
436 aFolder->data()->reference(ModelAPI_Folder::FIRST_FEATURE_ID());
438 aFirstFeatureInFolder = ModelAPI_Feature::feature(aFirstFeatAttr->value());
439 hasFirst = (aFirstFeatureInFolder == aFeatures.front());
441 FeaturePtr aLastFeatureInFolder;
442 AttributeReferencePtr aLastFeatAttr =
443 aFolder->data()->reference(ModelAPI_Folder::LAST_FEATURE_ID());
445 aLastFeatureInFolder = ModelAPI_Feature::feature(aLastFeatAttr->value());
446 hasLast = (aLastFeatureInFolder == aFeatures.back());
448 action("INSERT_FOLDER_CMD")->setEnabled(isOutsideFolder);
449 action("ADD_TO_FOLDER_BEFORE_CMD")->setEnabled(isOutsideFolder && isPrevFolder);
450 action("ADD_TO_FOLDER_AFTER_CMD")->setEnabled(isOutsideFolder && isNextFolder);
451 action("ADD_OUT_FOLDER_BEFORE_CMD")->setEnabled(isInFolder && hasFirst);
452 action("ADD_OUT_FOLDER_AFTER_CMD")->setEnabled(isInFolder && hasLast);
456 } // end folder management commands
458 bool allActive = true;
459 foreach( ObjectPtr aObject, aObjects )
460 if( aMgr->activeDocument() != aObject->document() ) {
464 if (!hasCompositeOwner && allActive ) {
465 if (hasResult || hasFeature || hasParameter) // #2924 results can be erased
466 action("DELETE_CMD")->setEnabled(canBeDeleted);
468 if (!hasCompositeOwner && allActive && (hasFeature|| hasParameter))
469 action("CLEAN_HISTORY_CMD")->setEnabled(true);
471 action("SHOW_RESULTS_CMD")->setEnabled(hasFeature);
472 action("SHOW_FEATURE_CMD")->setEnabled(hasResult && hasResultInHistory);
473 } // end selection processing
475 // Show/Hide command has to be disabled for objects from non active document
476 bool aDeactivate = false;
477 foreach (ObjectPtr aObj, aObjects) {
478 if (!aObj->document()->isActive()) {
479 if ((aObj->document() != ModelAPI_Session::get()->moduleDocument()) ||
480 aObj->groupName() == ModelAPI_ResultPart::group()) {
487 // If at leas a one object can not be edited then Show/Hide has to be disabled
488 action("SHOW_CMD")->setEnabled(false);
489 action("HIDE_CMD")->setEnabled(false);
490 action("SHOW_ONLY_CMD")->setEnabled(false);
493 action("COLOR_CMD")->setEnabled(myWorkshop->canChangeProperty("COLOR_CMD"));
494 action("DEFLECTION_CMD")->setEnabled(myWorkshop->canChangeProperty("DEFLECTION_CMD"));
495 action("TRANSPARENCY_CMD")->setEnabled(myWorkshop->canChangeProperty("TRANSPARENCY_CMD"));
498 action("TINSPECTOR_VIEW")->setEnabled(true);
503 ModuleBase_IModule* aModule = myWorkshop->module();
505 aModule->updateObjectBrowserMenu(myActions);
508 void XGUI_ContextMenuMgr::updateViewerMenu()
510 foreach(QAction* aAction, myActions)
511 aAction->setEnabled(false);
513 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
514 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
515 QList<ModuleBase_ViewerPrsPtr> aPrsList =
516 aSelMgr->selection()->getSelected(ModuleBase_ISelection::Viewer);
517 if (aPrsList.size() > 0) {
518 bool isVisible = false;
519 bool isShading = false;
520 bool canBeShaded = false;
521 bool hasPlanar = false;
523 foreach(ModuleBase_ViewerPrsPtr aPrs, aPrsList) {
524 aObject = aPrs->object();
527 GeomShapePtr aShape = aPrs->shape();
528 if (aObject->isDisplayed()) {
530 canBeShaded = myWorkshop->displayer()->canBeShaded(aObject);
532 (myWorkshop->displayer()->displayMode(aObject) == XGUI_Displayer::Shading);
535 if (aShape->isPlanar()) {
542 XGUI_Displayer::DisplayMode aMode = aDisplayer->displayMode(aObject);
543 if (aMode != XGUI_Displayer::NoMode) {
544 action("WIREFRAME_CMD")->setEnabled(aMode == XGUI_Displayer::Shading);
545 action("SHADING_CMD")->setEnabled(aMode == XGUI_Displayer::Wireframe);
547 action("WIREFRAME_CMD")->setEnabled(true);
548 action("SHADING_CMD")->setEnabled(true);
551 action("SHOW_ONLY_CMD")->setEnabled(true);
552 action("HIDE_CMD")->setEnabled(true);
554 action("SHOW_CMD")->setEnabled(true);
556 action("SET_VIEW_NORMAL_CMD")->setEnabled(hasPlanar);
557 action("SET_VIEW_INVERTEDNORMAL_CMD")->setEnabled(hasPlanar);
559 //issue #2159 Hide all incomplete behavior
561 action("HIDEALL_CMD")->setEnabled(true);
563 if (myWorkshop->displayer()->objectsCount() > 0)
564 action("HIDEALL_CMD")->setEnabled(true);
567 // Update selection menu
568 QIntList aModes = myWorkshop->selectionActivate()->activeSelectionModes();
569 action("SELECT_VERTEX_CMD")->setEnabled(true);
570 action("SELECT_EDGE_CMD")->setEnabled(true);
571 action("SELECT_FACE_CMD")->setEnabled(true);
572 action("SELECT_RESULT_CMD")->setEnabled(true);
574 action("SELECT_RESULT_CMD")->setChecked(false);
575 action("SELECT_VERTEX_CMD")->setChecked(false);
576 action("SELECT_EDGE_CMD")->setChecked(false);
577 action("SELECT_FACE_CMD")->setChecked(false);
578 action("SELECT_RESULT_CMD")->setChecked(false);
579 if (aModes.count() == 0) {
580 action("SELECT_RESULT_CMD")->setChecked(true);
582 foreach(int aMode, aModes) {
585 action("SELECT_VERTEX_CMD")->setChecked(true);
588 action("SELECT_EDGE_CMD")->setChecked(true);
591 action("SELECT_FACE_CMD")->setChecked(true);
594 action("SELECT_RESULT_CMD")->setChecked(true);
599 ModuleBase_IModule* aModule = myWorkshop->module();
601 aModule->updateViewerMenu(myActions);
603 if (myWorkshop->canChangeProperty("COLOR_CMD"))
604 action("COLOR_CMD")->setEnabled(true);
606 if (myWorkshop->canChangeProperty("DEFLECTION_CMD"))
607 action("DEFLECTION_CMD")->setEnabled(true);
609 if (myWorkshop->canChangeProperty("TRANSPARENCY_CMD"))
610 action("TRANSPARENCY_CMD")->setEnabled(true);
612 // Delete command is not used in viewer pop-up menu
613 action("DELETE_CMD")->setEnabled(false);
616 void XGUI_ContextMenuMgr::connectObjectBrowser()
618 connect(myWorkshop->objectBrowser(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
619 SLOT(onContextMenuRequest(QContextMenuEvent*)));
622 void XGUI_ContextMenuMgr::connectViewer()
624 connect(myWorkshop->viewer(), SIGNAL(contextMenuRequested(QContextMenuEvent*)), this,
625 SLOT(onContextMenuRequest(QContextMenuEvent*)));
629 void XGUI_ContextMenuMgr::buildObjBrowserMenu()
631 QAction* aSeparator = ModuleBase_Tools::createAction(QIcon(), "", myWorkshop->desktop());
632 aSeparator->setSeparator(true);
636 // Result construction menu
637 aList.append(action("SHOW_CMD"));
638 aList.append(action("HIDE_CMD"));
639 aList.append(action("SHOW_ONLY_CMD"));
640 aList.append(mySeparator1);
641 aList.append(action("RENAME_CMD"));
642 aList.append(action("COLOR_CMD"));
643 aList.append(action("DEFLECTION_CMD"));
644 aList.append(action("TRANSPARENCY_CMD"));
645 aList.append(action("SHOW_FEATURE_CMD"));
646 aList.append(mySeparator2);
647 aList.append(action("DELETE_CMD"));
648 myObjBrowserMenus[ModelAPI_ResultConstruction::group()] = aList;
650 //-------------------------------------
653 aList.append(action("WIREFRAME_CMD"));
654 aList.append(action("SHADING_CMD"));
655 aList.append(mySeparator1); // this separator is not shown as this action is added after show only
656 // qt list container contains only one instance of the same action
657 aList.append(action("SHOW_CMD"));
658 aList.append(action("HIDE_CMD"));
659 aList.append(action("SHOW_ONLY_CMD"));
660 aList.append(mySeparator2);
661 aList.append(action("RENAME_CMD"));
662 aList.append(action("COLOR_CMD"));
663 aList.append(action("DEFLECTION_CMD"));
664 aList.append(action("TRANSPARENCY_CMD"));
665 aList.append(action("SHOW_FEATURE_CMD"));
666 aList.append(mySeparator3);
667 aList.append(action("DELETE_CMD"));
668 myObjBrowserMenus[ModelAPI_ResultBody::group()] = aList;
670 myObjBrowserMenus[ModelAPI_ResultGroup::group()] = aList;
671 myObjBrowserMenus[ModelAPI_ResultField::group()] = aList;
673 myObjBrowserMenus[ModelAPI_ResultPart::group()] = aList;
674 //-------------------------------------
677 aList.append(action("RENAME_CMD"));
678 aList.append(action("SHOW_RESULTS_CMD"));
679 aList.append(action("MOVE_CMD"));
680 aList.append(mySeparator1);
681 aList.append(action("INSERT_FOLDER_CMD"));
682 aList.append(action("ADD_TO_FOLDER_BEFORE_CMD"));
683 aList.append(action("ADD_TO_FOLDER_AFTER_CMD"));
684 aList.append(mySeparator2);
685 aList.append(action("ADD_OUT_FOLDER_BEFORE_CMD"));
686 aList.append(action("ADD_OUT_FOLDER_AFTER_CMD"));
687 aList.append(mySeparator3);
688 aList.append(action("CLEAN_HISTORY_CMD"));
689 aList.append(action("DELETE_CMD"));
690 myObjBrowserMenus[ModelAPI_Feature::group()] = aList;
693 aList.append(action("RENAME_CMD"));
694 aList.append(mySeparator1);
695 aList.append(action("CLEAN_HISTORY_CMD"));
696 aList.append(action("DELETE_CMD"));
697 myObjBrowserMenus[ModelAPI_ResultParameter::group()] = aList;
698 //-------------------------------------
701 aList.append(action("RENAME_CMD"));
702 aList.append(action("DELETE_CMD"));
703 myObjBrowserMenus[ModelAPI_Folder::group()] = aList;
705 //---------------------------------------
708 aList.append(action("SHOW_CMD"));
709 aList.append(action("HIDE_CMD"));
710 aList.append(action("SHOW_ONLY_CMD"));
711 myObjBrowserMenus[ModelAPI_ResultField::ModelAPI_FieldStep::group()] = aList;
714 void XGUI_ContextMenuMgr::buildViewerMenu()
717 // Result construction menu
718 aList.append(action("COLOR_CMD"));
719 aList.append(action("DEFLECTION_CMD"));
720 aList.append(action("TRANSPARENCY_CMD"));
721 aList.append(mySeparator3);
722 aList.append(action("SET_VIEW_NORMAL_CMD"));
723 aList.append(action("SET_VIEW_INVERTEDNORMAL_CMD"));
724 aList.append(mySeparator1);
725 aList.append(action("SHOW_ONLY_CMD"));
726 aList.append(action("HIDE_CMD"));
727 myViewerMenu[ModelAPI_ResultConstruction::group()] = aList;
729 myViewerMenu[ModelAPI_ResultPart::group()] = aList;
730 //-------------------------------------
733 aList.append(action("WIREFRAME_CMD"));
734 aList.append(action("SHADING_CMD"));
735 aList.append(mySeparator2);
736 aList.append(action("COLOR_CMD"));
737 aList.append(action("DEFLECTION_CMD"));
738 aList.append(action("TRANSPARENCY_CMD"));
739 aList.append(mySeparator3);
740 aList.append(action("SET_VIEW_NORMAL_CMD"));
741 aList.append(action("SET_VIEW_INVERTEDNORMAL_CMD"));
742 aList.append(mySeparator1);
743 aList.append(action("SHOW_ONLY_CMD"));
744 aList.append(action("HIDE_CMD"));
745 myViewerMenu[ModelAPI_ResultBody::group()] = aList;
747 myViewerMenu[ModelAPI_ResultGroup::group()] = aList;
748 myViewerMenu[ModelAPI_ResultField::group()] = aList;
749 //-------------------------------------
752 aList.append(action("HIDE_CMD"));
753 myViewerMenu[ModelAPI_ResultField::ModelAPI_FieldStep::group()] = aList;
757 void XGUI_ContextMenuMgr::addObjBrowserMenu(QMenu* theMenu) const
759 ModuleBase_IModule* aModule = myWorkshop->module();
761 theMenu->addSeparator();
762 aModule->addObjectBrowserMenu(theMenu);
765 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
766 QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
767 int aSelected = aObjects.size();
768 QActionsList aActions;
769 if (aSelected == 1) {
770 ObjectPtr aObject = aObjects.first();
771 std::string aName = aObject->groupName();
772 if (myObjBrowserMenus.contains(aName))
773 aActions = myObjBrowserMenus[aName];
774 } else if (aSelected > 1) {
775 aActions.append(action("WIREFRAME_CMD"));
776 aActions.append(action("SHADING_CMD"));
777 aActions.append(mySeparator1);
778 aActions.append(action("SHOW_CMD"));
779 aActions.append(action("HIDE_CMD"));
780 aActions.append(action("SHOW_ONLY_CMD"));
781 aActions.append(mySeparator2);
782 aActions.append(action("ADD_TO_FOLDER_BEFORE_CMD"));
783 aActions.append(action("ADD_TO_FOLDER_AFTER_CMD"));
784 aActions.append(action("ADD_OUT_FOLDER_BEFORE_CMD"));
785 aActions.append(action("ADD_OUT_FOLDER_AFTER_CMD"));
786 aActions.append(mySeparator3);
787 //aActions.append(action("MOVE_CMD"));
788 aActions.append(action("COLOR_CMD"));
789 aActions.append(action("DEFLECTION_CMD"));
790 aActions.append(action("TRANSPARENCY_CMD"));
791 aActions.append(action("CLEAN_HISTORY_CMD"));
792 aActions.append(action("DELETE_CMD"));
795 if (aSelected == 0) {
797 aActions.append(action("TINSPECTOR_VIEW"));
801 theMenu->addActions(aActions);
802 addFeatures(theMenu);
804 // It is commented out because Object Browser does not have actions
805 //theMenu->addSeparator();
806 //theMenu->addActions(myWorkshop->objectBrowser()->actions());
809 void XGUI_ContextMenuMgr::addViewerMenu(QMenu* theMenu) const
811 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
812 QList<ModuleBase_ViewerPrsPtr> aPrsList =
813 aSelMgr->selection()->getSelected(ModuleBase_ISelection::Viewer);
814 int aSelected = aPrsList.size();
815 QActionsList aActions;
817 // Create selection menu
818 XGUI_OperationMgr* aOpMgr = myWorkshop->operationMgr();
819 if (!aOpMgr->hasOperation() &&
820 myWorkshop->selectionActivate()->activeSelectionPlace() == XGUI_SelectionActivate::Workshop) {
821 QMenu* aSelMenu = new QMenu(tr("Selection mode"), theMenu);
822 aSelMenu->addAction(action("SELECT_VERTEX_CMD"));
823 aSelMenu->addAction(action("SELECT_EDGE_CMD"));
824 aSelMenu->addAction(action("SELECT_FACE_CMD"));
825 //IMP: an attempt to use result selection with other selection modes
826 //aSelMenu->addSeparator();
827 aSelMenu->addAction(action("SELECT_RESULT_CMD"));
828 theMenu->addMenu(aSelMenu);
829 theMenu->addSeparator();
831 if (aSelected == 1) {
832 ObjectPtr aObject = aPrsList.first()->object();
833 if (aObject.get() != NULL) {
834 std::string aName = aObject->groupName();
835 if (myViewerMenu.contains(aName))
836 aActions = myViewerMenu[aName];
838 } else if (aSelected > 1) {
839 aActions.append(action("COLOR_CMD"));
840 aActions.append(action("DEFLECTION_CMD"));
841 aActions.append(action("TRANSPARENCY_CMD"));
842 aActions.append(mySeparator1);
843 aActions.append(action("SHOW_ONLY_CMD"));
844 aActions.append(action("HIDE_CMD"));
846 // hide all is shown always even if selection in the viewer is empty
847 aActions.append(action("HIDEALL_CMD"));
848 theMenu->addActions(aActions);
850 QMap<int, QAction*> aMenuActions;
851 ModuleBase_IModule* aModule = myWorkshop->module();
853 if (aModule->addViewerMenu(myActions, theMenu, aMenuActions))
854 theMenu->addSeparator();
857 // insert the module menu items on specific positions in the popup menu: some actions should be
858 // in the begin of the list, Delete action should be the last by #1343 issue
859 QList<QAction*> anActions = theMenu->actions();
860 int anActionsSize = anActions.size();
861 QAction* aFirstAction = anActions[0];
862 QMap<int, QAction*>::const_iterator anIt = aMenuActions.begin(), aLast = aMenuActions.end();
863 for (; anIt != aLast; anIt++) {
864 if (anIt.key() > anActionsSize)
865 theMenu->addAction(anIt.value());
867 theMenu->insertAction(aFirstAction, *anIt);
871 theMenu->addSeparator();
872 QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
873 if (aMDI->actions().size() > 0) {
874 QMenu* aSubMenu = theMenu->addMenu(tr("Windows"));
875 aSubMenu->addActions(aMDI->actions());
880 QStringList XGUI_ContextMenuMgr::actionObjectGroups(const QString& theName)
884 QMap<std::string, QActionsList>::const_iterator anIt = myObjBrowserMenus.begin(),
885 aLast = myObjBrowserMenus.end();
886 for (; anIt != aLast; anIt++) {
887 QString aGroupName(anIt.key().c_str());
888 if (aGroups.contains(aGroupName))
890 QActionsList anActions = anIt.value();
891 QActionsList::const_iterator anAIt = anActions.begin(), anALast = anActions.end();
893 for (; anAIt != anALast && !aFound; anAIt++)
894 aFound = (*anAIt)->data().toString() == theName;
896 aGroups.append(aGroupName);
901 void XGUI_ContextMenuMgr::onRename()
903 QObjectPtrList anObjects = myWorkshop->selector()->selection()->selectedObjects();
904 if (!myWorkshop->abortAllOperations())
906 // restore selection in case if dialog box was shown
907 myWorkshop->objectBrowser()->setObjectsSelected(anObjects);
908 myWorkshop->objectBrowser()->onEditItem();
911 void XGUI_ContextMenuMgr::addFeatures(QMenu* theMenu) const
913 SessionPtr aMgr = ModelAPI_Session::get();
914 DocumentPtr aActiveDoc = aMgr->activeDocument();
916 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
917 XGUI_ActionsMgr* aActionMgr = myWorkshop->actionsMgr();
918 const Config_DataModelReader* aDataModelXML = myWorkshop->dataModelXMLReader();
919 QModelIndexList aSelectedIndexes = aSelMgr->selection()->selectedIndexes();
923 bool aIsRoot = false;
924 foreach(QModelIndex aIdx, aSelectedIndexes) {
925 // Process only first column
926 if (aIdx.column() == 1) {
927 aIsRoot = !aIdx.parent().isValid();
928 // Exit if the selected index belongs to non active document
929 if (aIsRoot && (aActiveDoc != aMgr->moduleDocument()))
932 // Get name of the selected index
933 aName = aIdx.data().toString();
934 aLen = aName.indexOf('(');
936 aName = aName.left(--aLen);
938 std::string aFeaturesStr = aIsRoot?
939 aDataModelXML->rootFolderFeatures(aName.toStdString()) :
940 aDataModelXML->subFolderFeatures(aName.toStdString());
941 if (aFeaturesStr.length() > 0) {
942 QStringList aFeturesList =
943 QString(aFeaturesStr.c_str()).split(",", QString::SkipEmptyParts);
944 foreach(QString aFea, aFeturesList) {
945 QAction* aAction = aActionMgr->action(aFea);
947 theMenu->addAction(aAction);
954 #define UNCHECK_ACTION(NAME) \
955 { QAction* aAction = action(NAME); \
956 bool isBlock = aAction->signalsBlocked(); \
957 aAction->blockSignals(true); \
958 aAction->setChecked(false); \
959 aAction->blockSignals(isBlock); }
962 void XGUI_ContextMenuMgr::onResultSelection(bool theChecked)
964 UNCHECK_ACTION("SELECT_VERTEX_CMD");
965 UNCHECK_ACTION("SELECT_EDGE_CMD");
966 UNCHECK_ACTION("SELECT_FACE_CMD");
969 void XGUI_ContextMenuMgr::onShapeSelection(bool theChecked)
971 UNCHECK_ACTION("SHOW_RESULTS_CMD");