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_Operation.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_to_end.png"),
100 XGUI_Workshop::MOVE_TO_END_COMMAND, this);
101 addAction("MOVE_CMD", aAction);
103 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/move_to_end_split.png"),
104 XGUI_Workshop::MOVE_TO_END_SPLIT_COMMAND, this);
105 addAction("MOVE_SPLIT_CMD", aAction);
107 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/clean_history.png"),
108 tr("Clean history"), aDesktop);
109 addAction("CLEAN_HISTORY_CMD", aAction);
111 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/color.png"), tr("Color..."), aDesktop);
112 addAction("COLOR_CMD", aAction);
114 aAction = ModuleBase_Tools::createAction(QIcon(""), tr("Deflection..."), aDesktop);
115 addAction("DEFLECTION_CMD", aAction);
117 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/transparency.png"),
118 tr("Transparency..."), aDesktop);
119 addAction("TRANSPARENCY_CMD", aAction);
121 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/eye_pencil.png"), tr("Show"), aDesktop);
122 addAction("SHOW_CMD", aAction);
124 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/eye_pencil.png"), tr("Show only"),
126 addAction("SHOW_ONLY_CMD", aAction);
128 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide"),
130 addAction("HIDE_CMD", aAction);
132 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/eye_pencil_closed.png"), tr("Hide all"),
134 addAction("HIDEALL_CMD", aAction);
136 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/shading.png"), tr("Shading"), aDesktop);
137 addAction("SHADING_CMD", aAction);
139 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/wireframe.png"), tr("Wireframe"),
141 addAction("WIREFRAME_CMD", aAction);
143 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/iso_lines.png"), tr("Iso-lines..."),
145 addAction("ISOLINES_CMD", aAction);
147 mySeparator1 = ModuleBase_Tools::createAction(QIcon(), "", aDesktop);
148 mySeparator1->setSeparator(true);
150 mySeparator2 = ModuleBase_Tools::createAction(QIcon(), "", aDesktop);
151 mySeparator2->setSeparator(true);
153 mySeparator3 = ModuleBase_Tools::createAction(QIcon(), "", aDesktop);
154 mySeparator3->setSeparator(true);
156 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/vertex.png"), tr("Vertices"), aDesktop,
157 this, SLOT(onShapeSelection(bool)));
158 aAction->setCheckable(true);
159 addAction("SELECT_VERTEX_CMD", aAction);
161 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/edge.png"), tr("Edges"), aDesktop,
162 this, SLOT(onShapeSelection(bool)));
163 aAction->setCheckable(true);
164 addAction("SELECT_EDGE_CMD", aAction);
166 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/face.png"), tr("Faces"), aDesktop,
167 this, SLOT(onShapeSelection(bool)));
168 aAction->setCheckable(true);
169 addAction("SELECT_FACE_CMD", aAction);
171 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/result.png"), tr("Results"), aDesktop,
172 this, SLOT(onResultSelection(bool)));
173 aAction->setCheckable(true);
174 addAction("SELECT_RESULT_CMD", aAction);
176 aAction->setChecked(true);
178 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/find_result.png"),
179 tr("Select results"), aDesktop);
180 addAction("SHOW_RESULTS_CMD", aAction);
182 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/find_result.png"),
183 tr("Select parent feature"), aDesktop);
184 addAction("SHOW_FEATURE_CMD", aAction);
187 aAction = ModuleBase_Tools::createAction(QIcon(), tr("TInspector"), aDesktop);
188 addAction("TINSPECTOR_VIEW", aAction);
191 // Features folders actions
192 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/create_folder.png"),
193 tr("Insert a folder before"), aDesktop);
194 addAction("INSERT_FOLDER_CMD", aAction);
196 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/insert_folder_before.png"),
197 tr("Move into the previous folder"), aDesktop);
198 addAction("ADD_TO_FOLDER_BEFORE_CMD", aAction);
200 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/insert_folder_after.png"),
201 tr("Move into the next folder"), aDesktop);
202 addAction("ADD_TO_FOLDER_AFTER_CMD", aAction);
204 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/move_out_before.png"),
205 tr("Move out before the folder"), aDesktop);
206 addAction("ADD_OUT_FOLDER_BEFORE_CMD", aAction);
208 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/move_out_after.png"),
209 tr("Move out after the folder"), aDesktop);
210 addAction("ADD_OUT_FOLDER_AFTER_CMD", aAction);
212 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/normal-view-inversed.png"),
213 tr("Set view by inverted normal to face"), aDesktop);
214 addAction("SET_VIEW_INVERTEDNORMAL_CMD", aAction);
216 aAction = ModuleBase_Tools::createAction(QIcon(":pictures/normal-view.png"),
217 tr("Set view by normal to face"), aDesktop);
218 addAction("SET_VIEW_NORMAL_CMD", aAction);
220 buildObjBrowserMenu();
224 void XGUI_ContextMenuMgr::addAction(const QString& theId, QAction* theAction)
226 if (myActions.contains(theId))
227 qCritical("A command with Id = '%s' already defined!", qPrintable(theId));
228 theAction->setData(theId);
229 connect(theAction, SIGNAL(triggered(bool)), this, SLOT(onAction(bool)));
230 myActions[theId] = theAction;
233 QAction* XGUI_ContextMenuMgr::action(const QString& theId) const
235 if (myActions.contains(theId))
236 return myActions[theId];
240 QAction* XGUI_ContextMenuMgr::actionByName(const QString& theName) const
242 foreach(QAction* eachAction, myActions) {
243 if (eachAction->text() == theName) {
250 QStringList XGUI_ContextMenuMgr::actionIds() const
252 return myActions.keys();
255 void XGUI_ContextMenuMgr::onAction(bool isChecked)
257 QAction* aAction = static_cast<QAction*>(sender());
258 emit actionTriggered(aAction->data().toString(), isChecked);
261 void XGUI_ContextMenuMgr::updateCommandsStatus()
265 void XGUI_ContextMenuMgr::onContextMenuRequest(QContextMenuEvent* theEvent)
267 QMenu* aMenu = new QMenu();
268 if (sender() == myWorkshop->objectBrowser()) {
269 updateObjectBrowserMenu();
270 addObjBrowserMenu(aMenu);
271 } else if (sender() == myWorkshop->viewer()) {
273 addViewerMenu(aMenu);
276 if (aMenu && (aMenu->actions().size() > 0)) {
277 // it is possible that some objects should do something before and after the popup menu exec
278 // e.g. a sketch manager changes an internal flag on this signals in order to do not hide
280 emit beforeContextMenu();
281 aMenu->exec(theEvent->globalPos());
282 emit afterContextMenu();
287 void XGUI_ContextMenuMgr::updateObjectBrowserMenu()
289 foreach(QAction* aAction, myActions)
290 aAction->setEnabled(false);
292 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
293 QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
294 int aSelected = aObjects.size();
296 SessionPtr aMgr = ModelAPI_Session::get();
297 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
298 bool hasResult = false;
299 bool hasFeature = false;
300 bool hasParameter = false;
301 bool hasCompositeOwner = false;
302 bool hasResultInHistory = false;
303 bool hasFolder = false;
304 ModuleBase_Tools::checkObjects(aObjects, hasResult, hasFeature, hasParameter,
305 hasCompositeOwner, hasResultInHistory, hasFolder);
307 if (aSelected == 1) { // single selection
308 ObjectPtr aObject = aObjects.first();
310 if (hasResult && myWorkshop->canBeShaded(aObject)) {
311 XGUI_Displayer::DisplayMode aMode = aDisplayer->displayMode(aObject);
312 if (aMode != XGUI_Displayer::NoMode) {
313 action("WIREFRAME_CMD")->setEnabled(aMode == XGUI_Displayer::Shading);
314 action("SHADING_CMD")->setEnabled(aMode == XGUI_Displayer::Wireframe);
315 action("ISOLINES_CMD")->setEnabled(true);
317 action("WIREFRAME_CMD")->setEnabled(true);
318 action("SHADING_CMD")->setEnabled(true);
319 action("ISOLINES_CMD")->setEnabled(true);
323 bool aHasSubResults = ModelAPI_Tools::hasSubResults(
324 std::dynamic_pointer_cast<ModelAPI_Result>(aObject));
325 if (aHasSubResults) {
326 action("HIDE_CMD")->setEnabled(true);
327 action("SHOW_CMD")->setEnabled(true);
330 if (aObject->isDisplayed()) {
331 action("HIDE_CMD")->setEnabled(true);
332 } else if (hasResult && (!hasParameter)) {
333 action("SHOW_CMD")->setEnabled(true);
336 if (!(hasParameter || hasFeature))
337 action("SHOW_ONLY_CMD")->setEnabled(true);
339 else if (hasFeature && myWorkshop->canMoveFeature()) {
340 action("MOVE_CMD")->setEnabled(true);
341 action("MOVE_SPLIT_CMD")->setEnabled(true);
344 if( aMgr->activeDocument() == aObject->document() )
346 action("RENAME_CMD")->setEnabled(true);
347 action("DELETE_CMD")->setEnabled(!hasCompositeOwner);
348 action("CLEAN_HISTORY_CMD")->setEnabled(!hasCompositeOwner &&
349 (hasFeature || hasParameter));
352 // end single selection
353 } else { // multi-selection
354 // parameter is commented because the actions are not in the list of result parameter actions
355 if (hasResult /*&& (!hasParameter)*/) {
356 action("SHOW_CMD")->setEnabled(true);
357 action("HIDE_CMD")->setEnabled(true);
358 action("SHOW_ONLY_CMD")->setEnabled(true);
359 action("SHADING_CMD")->setEnabled(true);
360 action("WIREFRAME_CMD")->setEnabled(true);
361 action("ISOLINES_CMD")->setEnabled(true);
363 if (hasFeature && myWorkshop->canMoveFeature()) {
364 action("MOVE_CMD")->setEnabled(true);
365 action("MOVE_SPLIT_CMD")->setEnabled(true);
367 } // end multi-selection
369 // Check folder management commands state if only features are selected
370 if ((!hasResult) && hasFeature && (!hasParameter) && (!hasCompositeOwner) &&
371 (!hasResultInHistory) && (!hasFolder)) {
372 std::list<FeaturePtr> aFeatures = aSelMgr->getSelectedFeatures();
373 if (aFeatures.size() > 0) { // Check that features do not include Parts
374 QModelIndexList aIndexes = aSelMgr->selection()->selectedIndexes();
375 QModelIndex aFirstIdx = aIndexes.first();
376 QModelIndex aLastIdx = aIndexes.last();
377 QModelIndex aParentIdx = aFirstIdx.parent();
379 // if all selected are from the same level
380 bool isSameParent = true;
381 foreach(QModelIndex aIdx, aIndexes) {
382 if (aIdx.parent() != aParentIdx) {
383 isSameParent = false;
388 // Check is selection continuous
389 XGUI_DataModel* aModel = myWorkshop->objectBrowser()->dataModel();
390 DocumentPtr aDoc = aMgr->activeDocument();
391 std::list<FeaturePtr> aFeatures = aSelMgr->getSelectedFeatures();
393 bool isContinuos = true;
396 foreach(FeaturePtr aF, aFeatures) {
398 aId = aDoc->index(aF);
401 if (aId != aDoc->index(aF)) {
409 ObjectPtr aDataObj = aModel->object(aParentIdx);
412 if (aFirstIdx.row() > 0) {
413 QModelIndex aPrevIdx = aFirstIdx.sibling(aFirstIdx.row() - 1, 0);
414 aPrevObj = aModel->object(aPrevIdx);
418 if (aLastIdx.row() < (aModel->rowCount(aParentIdx) - 1)) {
419 QModelIndex aNextIdx = aFirstIdx.sibling(aLastIdx.row() + 1, 0);
420 aNextObj = aModel->object(aNextIdx);
423 bool isPrevFolder = (aPrevObj.get() &&
424 (aPrevObj->groupName() == ModelAPI_Folder::group()));
425 bool isNextFolder = (aNextObj.get() &&
426 (aNextObj->groupName() == ModelAPI_Folder::group()));
427 bool isInFolder = (aDataObj.get() &&
428 (aDataObj->groupName() == ModelAPI_Folder::group()));
429 bool isOutsideFolder = !isInFolder;
431 bool hasFirst = false;
432 bool hasLast = false;
434 FolderPtr aFolder = std::dynamic_pointer_cast<ModelAPI_Folder>(aDataObj);
435 FeaturePtr aFirstFeatureInFolder;
436 AttributeReferencePtr aFirstFeatAttr =
437 aFolder->data()->reference(ModelAPI_Folder::FIRST_FEATURE_ID());
439 aFirstFeatureInFolder = ModelAPI_Feature::feature(aFirstFeatAttr->value());
440 hasFirst = (aFirstFeatureInFolder == aFeatures.front());
442 FeaturePtr aLastFeatureInFolder;
443 AttributeReferencePtr aLastFeatAttr =
444 aFolder->data()->reference(ModelAPI_Folder::LAST_FEATURE_ID());
446 aLastFeatureInFolder = ModelAPI_Feature::feature(aLastFeatAttr->value());
447 hasLast = (aLastFeatureInFolder == aFeatures.back());
449 action("INSERT_FOLDER_CMD")->setEnabled(isOutsideFolder);
450 action("ADD_TO_FOLDER_BEFORE_CMD")->setEnabled(isOutsideFolder && isPrevFolder);
451 action("ADD_TO_FOLDER_AFTER_CMD")->setEnabled(isOutsideFolder && isNextFolder);
452 action("ADD_OUT_FOLDER_BEFORE_CMD")->setEnabled(isInFolder && hasFirst);
453 action("ADD_OUT_FOLDER_AFTER_CMD")->setEnabled(isInFolder && hasLast);
457 } // end folder management commands
459 bool allActive = true;
460 foreach( ObjectPtr aObject, aObjects )
461 if( aMgr->activeDocument() != aObject->document() ) {
465 if (!hasCompositeOwner && allActive ) {
466 if (hasResult || hasFeature || hasParameter) // #2924 results can be erased
467 action("DELETE_CMD")->setEnabled(true);
469 if (!hasCompositeOwner && allActive && (hasFeature|| hasParameter))
470 action("CLEAN_HISTORY_CMD")->setEnabled(true);
472 action("SHOW_RESULTS_CMD")->setEnabled(hasFeature);
473 action("SHOW_FEATURE_CMD")->setEnabled(hasResult && hasResultInHistory);
474 } // end selection processing
476 // Show/Hide command has to be disabled for objects from non active document
477 bool aDeactivate = false;
478 foreach (ObjectPtr aObj, aObjects) {
479 if (!aObj->document()->isActive()) {
480 if ((aObj->document() != ModelAPI_Session::get()->moduleDocument()) ||
481 aObj->groupName() == ModelAPI_ResultPart::group()) {
488 // If at leas a one object can not be edited then Show/Hide has to be disabled
489 action("SHOW_CMD")->setEnabled(false);
490 action("HIDE_CMD")->setEnabled(false);
491 action("SHOW_ONLY_CMD")->setEnabled(false);
494 action("COLOR_CMD")->setEnabled(myWorkshop->canChangeProperty("COLOR_CMD"));
495 action("DEFLECTION_CMD")->setEnabled(myWorkshop->canChangeProperty("DEFLECTION_CMD"));
496 action("TRANSPARENCY_CMD")->setEnabled(myWorkshop->canChangeProperty("TRANSPARENCY_CMD"));
499 action("TINSPECTOR_VIEW")->setEnabled(true);
504 ModuleBase_IModule* aModule = myWorkshop->module();
506 aModule->updateObjectBrowserMenu(myActions);
509 void XGUI_ContextMenuMgr::updateViewerMenu()
511 foreach(QAction* aAction, myActions)
512 aAction->setEnabled(false);
514 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
515 XGUI_Displayer* aDisplayer = myWorkshop->displayer();
516 QList<ModuleBase_ViewerPrsPtr> aPrsList =
517 aSelMgr->selection()->getSelected(ModuleBase_ISelection::Viewer);
518 if (aPrsList.size() > 0) {
519 bool isVisible = false;
520 bool isShading = false;
521 bool canBeShaded = false;
522 bool hasPlanar = false;
524 foreach(ModuleBase_ViewerPrsPtr aPrs, aPrsList) {
525 aObject = aPrs->object();
528 GeomShapePtr aShape = aPrs->shape();
529 if (aObject->isDisplayed()) {
531 canBeShaded = myWorkshop->displayer()->canBeShaded(aObject);
533 (myWorkshop->displayer()->displayMode(aObject) == XGUI_Displayer::Shading);
536 if (aShape->isPlanar()) {
543 XGUI_Displayer::DisplayMode aMode = aDisplayer->displayMode(aObject);
544 if (aMode != XGUI_Displayer::NoMode) {
545 action("WIREFRAME_CMD")->setEnabled(aMode == XGUI_Displayer::Shading);
546 action("SHADING_CMD")->setEnabled(aMode == XGUI_Displayer::Wireframe);
547 action("ISOLINES_CMD")->setEnabled(true);
549 action("WIREFRAME_CMD")->setEnabled(true);
550 action("SHADING_CMD")->setEnabled(true);
551 action("ISOLINES_CMD")->setEnabled(true);
554 action("SHOW_ONLY_CMD")->setEnabled(true);
555 action("HIDE_CMD")->setEnabled(true);
557 action("SHOW_CMD")->setEnabled(true);
559 action("SET_VIEW_NORMAL_CMD")->setEnabled(hasPlanar);
560 action("SET_VIEW_INVERTEDNORMAL_CMD")->setEnabled(hasPlanar);
562 //issue #2159 Hide all incomplete behavior
564 action("HIDEALL_CMD")->setEnabled(true);
566 if (myWorkshop->displayer()->objectsCount() > 0)
567 action("HIDEALL_CMD")->setEnabled(true);
570 // Update selection menu
571 QIntList aModes = myWorkshop->selectionActivate()->activeSelectionModes();
572 action("SELECT_VERTEX_CMD")->setEnabled(true);
573 action("SELECT_EDGE_CMD")->setEnabled(true);
574 action("SELECT_FACE_CMD")->setEnabled(true);
575 action("SELECT_RESULT_CMD")->setEnabled(true);
577 action("SELECT_RESULT_CMD")->setChecked(false);
578 action("SELECT_VERTEX_CMD")->setChecked(false);
579 action("SELECT_EDGE_CMD")->setChecked(false);
580 action("SELECT_FACE_CMD")->setChecked(false);
581 action("SELECT_RESULT_CMD")->setChecked(false);
582 if (aModes.count() == 0) {
583 action("SELECT_RESULT_CMD")->setChecked(true);
585 foreach(int aMode, aModes) {
588 action("SELECT_VERTEX_CMD")->setChecked(true);
591 action("SELECT_EDGE_CMD")->setChecked(true);
594 action("SELECT_FACE_CMD")->setChecked(true);
597 action("SELECT_RESULT_CMD")->setChecked(true);
602 ModuleBase_IModule* aModule = myWorkshop->module();
604 aModule->updateViewerMenu(myActions);
606 if (myWorkshop->canChangeProperty("COLOR_CMD"))
607 action("COLOR_CMD")->setEnabled(true);
609 if (myWorkshop->canChangeProperty("DEFLECTION_CMD"))
610 action("DEFLECTION_CMD")->setEnabled(true);
612 if (myWorkshop->canChangeProperty("TRANSPARENCY_CMD"))
613 action("TRANSPARENCY_CMD")->setEnabled(true);
615 action("DELETE_CMD")->setEnabled(true);
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("ISOLINES_CMD"));
668 aList.append(action("SHOW_FEATURE_CMD"));
669 aList.append(mySeparator3);
670 aList.append(action("DELETE_CMD"));
671 myObjBrowserMenus[ModelAPI_ResultBody::group()] = aList;
673 myObjBrowserMenus[ModelAPI_ResultGroup::group()] = aList;
674 myObjBrowserMenus[ModelAPI_ResultField::group()] = aList;
676 myObjBrowserMenus[ModelAPI_ResultPart::group()] = aList;
677 //-------------------------------------
680 aList.append(action("RENAME_CMD"));
681 aList.append(action("SHOW_RESULTS_CMD"));
682 aList.append(action("MOVE_CMD"));
683 aList.append(action("MOVE_SPLIT_CMD"));
684 aList.append(mySeparator1);
685 aList.append(action("INSERT_FOLDER_CMD"));
686 aList.append(action("ADD_TO_FOLDER_BEFORE_CMD"));
687 aList.append(action("ADD_TO_FOLDER_AFTER_CMD"));
688 aList.append(mySeparator2);
689 aList.append(action("ADD_OUT_FOLDER_BEFORE_CMD"));
690 aList.append(action("ADD_OUT_FOLDER_AFTER_CMD"));
691 aList.append(mySeparator3);
692 aList.append(action("CLEAN_HISTORY_CMD"));
693 aList.append(action("DELETE_CMD"));
694 myObjBrowserMenus[ModelAPI_Feature::group()] = aList;
697 aList.append(action("RENAME_CMD"));
698 aList.append(mySeparator1);
699 aList.append(action("CLEAN_HISTORY_CMD"));
700 aList.append(action("DELETE_CMD"));
701 myObjBrowserMenus[ModelAPI_ResultParameter::group()] = aList;
702 //-------------------------------------
705 aList.append(action("RENAME_CMD"));
706 aList.append(action("DELETE_CMD"));
707 myObjBrowserMenus[ModelAPI_Folder::group()] = aList;
709 //---------------------------------------
712 aList.append(action("SHOW_CMD"));
713 aList.append(action("HIDE_CMD"));
714 aList.append(action("SHOW_ONLY_CMD"));
715 myObjBrowserMenus[ModelAPI_ResultField::ModelAPI_FieldStep::group()] = aList;
718 void XGUI_ContextMenuMgr::buildViewerMenu()
721 // Result construction menu
722 aList.append(action("COLOR_CMD"));
723 aList.append(action("DEFLECTION_CMD"));
724 aList.append(action("TRANSPARENCY_CMD"));
725 aList.append(mySeparator3);
726 aList.append(action("SET_VIEW_NORMAL_CMD"));
727 aList.append(action("SET_VIEW_INVERTEDNORMAL_CMD"));
728 aList.append(mySeparator1);
729 aList.append(action("SHOW_ONLY_CMD"));
730 aList.append(action("HIDE_CMD"));
731 myViewerMenu[ModelAPI_ResultConstruction::group()] = aList;
733 myViewerMenu[ModelAPI_ResultPart::group()] = aList;
734 //-------------------------------------
737 aList.append(action("WIREFRAME_CMD"));
738 aList.append(action("SHADING_CMD"));
739 aList.append(mySeparator2);
740 aList.append(action("COLOR_CMD"));
741 aList.append(action("DEFLECTION_CMD"));
742 aList.append(action("TRANSPARENCY_CMD"));
743 aList.append(action("ISOLINES_CMD"));
744 aList.append(mySeparator3);
745 aList.append(action("SET_VIEW_NORMAL_CMD"));
746 aList.append(action("SET_VIEW_INVERTEDNORMAL_CMD"));
747 aList.append(mySeparator1);
748 aList.append(action("SHOW_ONLY_CMD"));
749 aList.append(action("HIDE_CMD"));
750 myViewerMenu[ModelAPI_ResultBody::group()] = aList;
752 myViewerMenu[ModelAPI_ResultGroup::group()] = aList;
753 myViewerMenu[ModelAPI_ResultField::group()] = aList;
754 //-------------------------------------
757 aList.append(action("HIDE_CMD"));
758 myViewerMenu[ModelAPI_ResultField::ModelAPI_FieldStep::group()] = aList;
762 void XGUI_ContextMenuMgr::addObjBrowserMenu(QMenu* theMenu) const
764 ModuleBase_IModule* aModule = myWorkshop->module();
766 theMenu->addSeparator();
767 aModule->addObjectBrowserMenu(theMenu);
770 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
771 QObjectPtrList aObjects = aSelMgr->selection()->selectedObjects();
772 int aSelected = aObjects.size();
773 QActionsList aActions;
774 if (aSelected == 1) {
775 ObjectPtr aObject = aObjects.first();
776 std::string aName = aObject->groupName();
777 if (myObjBrowserMenus.contains(aName))
778 aActions = myObjBrowserMenus[aName];
779 } else if (aSelected > 1) {
780 aActions.append(action("WIREFRAME_CMD"));
781 aActions.append(action("SHADING_CMD"));
782 aActions.append(mySeparator1);
783 aActions.append(action("SHOW_CMD"));
784 aActions.append(action("HIDE_CMD"));
785 aActions.append(action("SHOW_ONLY_CMD"));
786 aActions.append(mySeparator2);
787 aActions.append(action("ADD_TO_FOLDER_BEFORE_CMD"));
788 aActions.append(action("ADD_TO_FOLDER_AFTER_CMD"));
789 aActions.append(action("ADD_OUT_FOLDER_BEFORE_CMD"));
790 aActions.append(action("ADD_OUT_FOLDER_AFTER_CMD"));
791 aActions.append(mySeparator3);
792 aActions.append(action("MOVE_CMD"));
793 aActions.append(action("MOVE_SPLIT_CMD"));
794 aActions.append(action("COLOR_CMD"));
795 aActions.append(action("DEFLECTION_CMD"));
796 aActions.append(action("TRANSPARENCY_CMD"));
797 aActions.append(action("ISOLINES_CMD"));
798 aActions.append(action("CLEAN_HISTORY_CMD"));
799 aActions.append(action("DELETE_CMD"));
802 if (aSelected == 0) {
804 aActions.append(action("TINSPECTOR_VIEW"));
808 theMenu->addActions(aActions);
809 addFeatures(theMenu);
811 // It is commented out because Object Browser does not have actions
812 //theMenu->addSeparator();
813 //theMenu->addActions(myWorkshop->objectBrowser()->actions());
816 void XGUI_ContextMenuMgr::addViewerMenu(QMenu* theMenu) const
818 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
819 QList<ModuleBase_ViewerPrsPtr> aPrsList =
820 aSelMgr->selection()->getSelected(ModuleBase_ISelection::Viewer);
821 int aSelected = aPrsList.size();
822 QActionsList aActions;
824 // Create selection menu
825 XGUI_OperationMgr* aOpMgr = myWorkshop->operationMgr();
826 if (!aOpMgr->hasOperation() &&
827 myWorkshop->selectionActivate()->activeSelectionPlace() == XGUI_SelectionActivate::Workshop) {
828 QMenu* aSelMenu = new QMenu(tr("Selection mode"), theMenu);
829 aSelMenu->addAction(action("SELECT_VERTEX_CMD"));
830 aSelMenu->addAction(action("SELECT_EDGE_CMD"));
831 aSelMenu->addAction(action("SELECT_FACE_CMD"));
832 //IMP: an attempt to use result selection with other selection modes
833 //aSelMenu->addSeparator();
834 aSelMenu->addAction(action("SELECT_RESULT_CMD"));
835 theMenu->addMenu(aSelMenu);
836 theMenu->addSeparator();
838 if (aSelected == 1) {
839 ObjectPtr aObject = aPrsList.first()->object();
840 if (aObject.get() != NULL) {
841 std::string aName = aObject->groupName();
842 if (myViewerMenu.contains(aName))
843 aActions = myViewerMenu[aName];
845 } else if (aSelected > 1) {
846 aActions.append(action("COLOR_CMD"));
847 aActions.append(action("DEFLECTION_CMD"));
848 aActions.append(action("TRANSPARENCY_CMD"));
849 aActions.append(mySeparator1);
850 aActions.append(action("SHOW_ONLY_CMD"));
851 aActions.append(action("HIDE_CMD"));
853 // hide all is shown always even if selection in the viewer is empty
854 aActions.append(action("HIDEALL_CMD"));
855 theMenu->addActions(aActions);
857 QMap<int, QAction*> aMenuActions;
858 ModuleBase_IModule* aModule = myWorkshop->module();
860 if (aModule->addViewerMenu(myActions, theMenu, aMenuActions))
861 theMenu->addSeparator();
864 // insert the module menu items on specific positions in the popup menu: some actions should be
865 // in the begin of the list, Delete action should be the last by #1343 issue
866 QList<QAction*> anActions = theMenu->actions();
867 int anActionsSize = anActions.size();
868 QAction* aFirstAction = anActions[0];
869 QMap<int, QAction*>::const_iterator anIt = aMenuActions.begin(), aLast = aMenuActions.end();
870 for (; anIt != aLast; anIt++) {
871 if (anIt.key() > anActionsSize)
872 theMenu->addAction(anIt.value());
874 theMenu->insertAction(aFirstAction, *anIt);
878 theMenu->addSeparator();
879 QMdiArea* aMDI = myWorkshop->mainWindow()->mdiArea();
880 if (aMDI->actions().size() > 0) {
881 QMenu* aSubMenu = theMenu->addMenu(tr("Windows"));
882 aSubMenu->addActions(aMDI->actions());
887 QStringList XGUI_ContextMenuMgr::actionObjectGroups(const QString& theName)
891 QMap<std::string, QActionsList>::const_iterator anIt = myObjBrowserMenus.begin(),
892 aLast = myObjBrowserMenus.end();
893 for (; anIt != aLast; anIt++) {
894 QString aGroupName(anIt.key().c_str());
895 if (aGroups.contains(aGroupName))
897 QActionsList anActions = anIt.value();
898 QActionsList::const_iterator anAIt = anActions.begin(), anALast = anActions.end();
900 for (; anAIt != anALast && !aFound; anAIt++)
901 aFound = (*anAIt)->data().toString() == theName;
903 aGroups.append(aGroupName);
908 void XGUI_ContextMenuMgr::onRename()
910 QObjectPtrList anObjects = myWorkshop->selector()->selection()->selectedObjects();
911 if (!myWorkshop->abortAllOperations())
913 // restore selection in case if dialog box was shown
914 myWorkshop->objectBrowser()->setObjectsSelected(anObjects);
915 myWorkshop->objectBrowser()->onEditItem();
918 void XGUI_ContextMenuMgr::addFeatures(QMenu* theMenu) const
920 SessionPtr aMgr = ModelAPI_Session::get();
921 DocumentPtr aActiveDoc = aMgr->activeDocument();
923 XGUI_SelectionMgr* aSelMgr = myWorkshop->selector();
924 XGUI_ActionsMgr* aActionMgr = myWorkshop->actionsMgr();
925 const Config_DataModelReader* aDataModelXML = myWorkshop->dataModelXMLReader();
926 QModelIndexList aSelectedIndexes = aSelMgr->selection()->selectedIndexes();
930 bool aIsRoot = false;
931 foreach(QModelIndex aIdx, aSelectedIndexes) {
932 // Process only first column
933 if (aIdx.column() == 1) {
934 aIsRoot = !aIdx.parent().isValid();
935 // Exit if the selected index belongs to non active document
936 if (aIsRoot && (aActiveDoc != aMgr->moduleDocument()))
939 // Get name of the selected index
940 aName = aIdx.data().toString();
941 aLen = aName.indexOf('(');
943 aName = aName.left(--aLen);
945 std::string aFeaturesStr = aIsRoot?
946 aDataModelXML->rootFolderFeatures(aName.toStdString()) :
947 aDataModelXML->subFolderFeatures(aName.toStdString());
948 if (aFeaturesStr.length() > 0) {
949 QStringList aFeturesList =
950 QString(aFeaturesStr.c_str()).split(",", QString::SkipEmptyParts);
951 foreach(QString aFea, aFeturesList) {
952 QAction* aAction = aActionMgr->action(aFea);
954 theMenu->addAction(aAction);
961 #define UNCHECK_ACTION(NAME) \
962 { QAction* aAction = action(NAME); \
963 bool isBlock = aAction->signalsBlocked(); \
964 aAction->blockSignals(true); \
965 aAction->setChecked(false); \
966 aAction->blockSignals(isBlock); }
969 void XGUI_ContextMenuMgr::onResultSelection(bool theChecked)
971 UNCHECK_ACTION("SELECT_VERTEX_CMD");
972 UNCHECK_ACTION("SELECT_EDGE_CMD");
973 UNCHECK_ACTION("SELECT_FACE_CMD");
976 void XGUI_ContextMenuMgr::onShapeSelection(bool theChecked)
978 UNCHECK_ACTION("SHOW_RESULTS_CMD");