1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
4 #include "NewGeom_Module.h"
5 #include "NewGeom_DataModel.h"
6 #include "NewGeom_OCCSelector.h"
7 #include <NewGeom_NestedButton.h>
9 #include <XGUI_Workshop.h>
10 #include <XGUI_PropertyPanel.h>
11 #include <XGUI_ContextMenuMgr.h>
12 #include <XGUI_ObjectsBrowser.h>
13 #include <XGUI_OperationMgr.h>
14 #include <XGUI_Displayer.h>
16 #include <ModuleBase_Operation.h>
17 #include <ModuleBase_Preferences.h>
18 #include <ModuleBase_ActionInfo.h>
19 #include <ModuleBase_IModule.h>
21 #include <LightApp_Application.h>
22 #include <LightApp_SelectionMgr.h>
23 #include <LightApp_OCCSelector.h>
24 #include <LightApp_Study.h>
25 #include <OCCViewer_ViewModel.h>
27 #include <SUIT_Selector.h>
28 #include <SUIT_Desktop.h>
29 #include <SUIT_ViewManager.h>
30 #include <SUIT_ResourceMgr.h>
32 #include <QtxPopupMgr.h>
33 #include <QtxActionMenuMgr.h>
34 #include <QtxActionToolMgr.h>
35 #include <QtxResourceMgr.h>
37 #include <Config_PropManager.h>
38 #include <Config_ModuleReader.h>
40 #include <AIS_ListOfInteractive.hxx>
41 #include <AIS_ListIteratorOfListOfInteractive.hxx>
43 #include <QDockWidget>
50 NewGeom_EXPORT CAM_Module* createModule()
52 return new NewGeom_Module();
55 NewGeom_EXPORT char* getModuleVersion()
62 * Class for preferences management
64 class NewGeom_PrefMgr: public ModuleBase_IPrefMgr
68 /// \param theMgr preferences manager of SALOME
69 /// \param theModName name of the module
70 NewGeom_PrefMgr(LightApp_Preferences* theMgr, const QString& theModName):myMgr(theMgr), myModName(theModName) {}
72 virtual int addPreference(const QString& theLbl, int pId,
73 SUIT_PreferenceMgr::PrefItemType theType,
74 const QString& theSection, const QString& theName )
76 return myMgr->addPreference(myModName, theLbl, pId, theType, theSection, theName);
79 virtual void setItemProperty(const QString& thePropName,
80 const QVariant& theValue,
83 myMgr->setItemProperty(thePropName, theValue, theId);
87 virtual SUIT_PreferenceMgr* prefMgr() const { return myMgr; }
90 LightApp_Preferences* myMgr;
97 //******************************************************
98 NewGeom_Module::NewGeom_Module()
99 : LightApp_Module("NewGeom"),
100 mySelector(0), myIsOpened(0), myPopupMgr(0)
102 myWorkshop = new XGUI_Workshop(this);
103 connect(myWorkshop, SIGNAL(commandStatusUpdated()),
104 this, SLOT(onUpdateCommandStatus()));
106 myProxyViewer = new NewGeom_SalomeViewer(this);
108 ModuleBase_Preferences::setResourceMgr(application()->resourceMgr());
109 ModuleBase_Preferences::loadCustomProps();
112 //******************************************************
113 NewGeom_Module::~NewGeom_Module()
117 //******************************************************
118 void NewGeom_Module::initialize(CAM_Application* theApp)
120 LightApp_Module::initialize(theApp);
121 inspectSalomeModules();
123 myWorkshop->startApplication();
124 LightApp_Application* anApp = dynamic_cast<LightApp_Application*>(theApp);
127 connect(anApp, SIGNAL(preferenceResetToDefaults()), this, SLOT(onDefaultPreferences()));
131 //******************************************************
132 void NewGeom_Module::windows(QMap<int, int>& theWndMap) const
134 theWndMap.insert(LightApp_Application::WT_PyConsole, Qt::BottomDockWidgetArea);
137 //******************************************************
138 void NewGeom_Module::viewManagers(QStringList& theList) const
140 theList.append(OCCViewer_Viewer::Type());
143 //******************************************************
144 bool NewGeom_Module::activateModule(SUIT_Study* theStudy)
146 bool isDone = LightApp_Module::activateModule(theStudy);
151 QObject* aObj = myWorkshop->objectBrowser()->parent();
152 QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
154 QAction* aViewAct = aObjDoc->toggleViewAction();
155 aViewAct->setEnabled(true);
156 myWorkshop->objectBrowser()->setVisible(true);
157 aObjDoc->setVisible(true);
158 desktop()->tabifyDockWidget(aObjDoc, myWorkshop->propertyPanel());
162 ViewManagerList OCCViewManagers;
163 application()->viewManagers(OCCViewer_Viewer::Type(), OCCViewManagers);
164 if (OCCViewManagers.size() > 0) {
165 mySelector = createSelector(OCCViewManagers.first());
168 // it should be pefromed after the selector creation in order to have AISContext
169 myWorkshop->activateModule();
170 //action(myEraseAll)->setEnabled(false);
173 myWorkshop->objectBrowser()->rebuildDataTree();
174 myWorkshop->updateCommandStatus();
178 myWorkshop->updateCommandStatus();
180 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
181 myIsStorePositions = aResMgr->booleanValue("Study", "store_positions", true);
182 myIsEditEnabled = getApp()->isEditEnabled();
183 getApp()->setEditEnabled(false);
185 // this following row is caused by #187 bug.
186 // SALOME saves the dock widget positions before deactivateModule() and
187 // load it after the module activation. So, if the panel is visible before
188 // deactivate, it becomes visible after activate.
189 // In order to avoid the visible property panel, the widget position save is
190 // switch off in this module
191 aResMgr->setValue("Study", "store_positions", false);
193 // Synchronize displayed objects
194 Handle(AIS_InteractiveContext) aContext;
195 if (mySelector && mySelector->viewer())
196 aContext = mySelector->viewer()->getAISContext();
198 if (!aContext.IsNull()) {
199 XGUI_Displayer* aDisp = myWorkshop->displayer();
200 QObjectPtrList aObjList = aDisp->displayedObjects();
202 AIS_ListOfInteractive aList;
203 aContext->DisplayedObjects(aList);
204 AIS_ListIteratorOfListOfInteractive aLIt;
205 Handle(AIS_InteractiveObject) anAISIO;
206 foreach (ObjectPtr aObj, aObjList) {
207 AISObjectPtr aPrs = aDisp->getAISObject(aObj);
208 Handle(AIS_InteractiveObject) aAIS = aPrs->impl<Handle(AIS_InteractiveObject)>();
210 for (aLIt.Initialize(aList); aLIt.More(); aLIt.Next()) {
211 anAISIO = aLIt.Value();
212 if (anAISIO.Access() == aAIS.Access()) {
218 aObj->setDisplayed(false);
219 //aDisp->erase(aObj, false);
222 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
224 myProxyViewer->activateViewer(true);
228 //******************************************************
229 bool NewGeom_Module::deactivateModule(SUIT_Study* theStudy)
231 myProxyViewer->activateViewer(false);
235 myWorkshop->deactivateModule();
237 QObject* aObj = myWorkshop->objectBrowser()->parent();
238 QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
240 aObjDoc->setVisible(false);
241 myWorkshop->objectBrowser()->setVisible(false);
242 QAction* aViewAct = aObjDoc->toggleViewAction();
243 aViewAct->setEnabled(false);
246 // the active operation should be stopped for the next activation.
247 // There should not be active operation and visualized preview.
248 // Abort operation should be performed before the selection's remove
249 // because the displayed objects should be removed from the viewer, but
250 // the AIS context is obtained from the selector.
251 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
252 while (anOperation) {
253 anOperation->abort();
254 anOperation = myWorkshop->operationMgr()->currentOperation();
256 // Delete selector because it has to be redefined on next activation
258 myProxyViewer->setSelector(0);
263 //myWorkshop->contextMenuMgr()->disconnectViewer();
265 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
266 aResMgr->setValue("Study", "store_positions", myIsStorePositions);
267 getApp()->setEditEnabled(myIsEditEnabled);
269 return LightApp_Module::deactivateModule(theStudy);
272 //******************************************************
273 void NewGeom_Module::onViewManagerAdded(SUIT_ViewManager* theMgr)
276 mySelector = createSelector(theMgr);
277 myWorkshop->module()->activateSelectionFilters();
281 //******************************************************
282 void NewGeom_Module::onViewManagerRemoved(SUIT_ViewManager* theMgr)
285 if (theMgr->getType() == OCCViewer_Viewer::Type()) {
286 OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
287 if (mySelector->viewer() == aViewer) {
288 XGUI_Displayer* aDisp = myWorkshop->displayer();
289 QObjectPtrList aObjects = aDisp->displayedObjects();
290 foreach(ObjectPtr aObj, aObjects)
291 aObj->setDisplayed(false);
292 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
293 myProxyViewer->setSelector(0);
297 myWorkshop->module()->clearViewer();
303 //******************************************************
304 QtxPopupMgr* NewGeom_Module::popupMgr()
307 myPopupMgr = new QtxPopupMgr( 0, this );
311 //******************************************************
312 void NewGeom_Module::onDefaultPreferences()
314 ModuleBase_Preferences::resetConfig();
315 ModuleBase_Preferences::updateResourcesByConfig();
317 LightApp_Preferences* pref = preferences();
321 myWorkshop->displayer()->redisplayObjects();
324 //******************************************************
325 void NewGeom_Module::onUpdateCommandStatus()
327 getApp()->updateActions();
330 //******************************************************
331 NewGeom_OCCSelector* NewGeom_Module::createSelector(SUIT_ViewManager* theMgr)
333 if (theMgr->getType() == OCCViewer_Viewer::Type()) {
334 OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
335 NewGeom_OCCSelector* aSelector = new NewGeom_OCCSelector(aViewer, getApp()->selectionMgr());
336 LightApp_SelectionMgr* aMgr = getApp()->selectionMgr();
337 QList<SUIT_Selector*> aList;
338 aMgr->selectors(aList);
339 foreach(SUIT_Selector* aSel, aList)
341 aSel->setEnabled(aSel == aSelector);
343 myProxyViewer->setSelector(aSelector);
349 //******************************************************
350 CAM_DataModel* NewGeom_Module::createDataModel()
352 NewGeom_DataModel* aDataModel = new NewGeom_DataModel(this);
354 // Calling addComponent() for persistent functionality work in the SalomeApp_Study
355 LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>(application()->activeStudy() );
356 aStudy->addComponent(aDataModel);
361 QAction* NewGeom_Module::addFeature(const QString& theWBName, const ActionInfo& theInfo)
363 return addFeature(theWBName,
372 //******************************************************
373 QAction* NewGeom_Module::addFeature(const QString& theWBName, const QString& theId,
374 const QString& theTitle, const QString& theTip,
375 const QIcon& theIcon, const QKeySequence& theKeys,
378 static QString aLastTool = "";
380 if (aLastTool.isEmpty())
381 aLastTool = theWBName;
382 else if (theWBName != aLastTool) {
383 aLastTool = theWBName;
385 desktop()->addToolBarBreak();
391 int aMenu = createMenu(theWBName, -1, -1, 50);
392 int aTool = createTool(theWBName, theWBName);
394 int aId = myActionsList.size();
395 myActionsList.append(theId);
396 SUIT_Desktop* aDesk = application()->desktop();
398 for (unsigned int i = 0; i < theKeys.count(); i++)
400 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
402 aAction->setData(theId);
403 int aItemId = createMenu(aId, aMenu, -1, 10);
404 int aToolId = createTool(aId, aTool);
409 bool NewGeom_Module::isFeatureOfNested(const QAction* theAction)
411 return dynamic_cast<const NewGeom_NestedButton*>(theAction);
414 QAction* NewGeom_Module::addFeatureOfNested(const QString& theWBName,
415 const ActionInfo& theInfo,
416 const QList<QAction*>& theNestedActions)
418 int aMenu = createMenu(theWBName, -1, -1, 50);
419 int aTool = createTool(theWBName, theWBName);
421 int aId = myActionsList.size();
422 myActionsList.append(theInfo.id);
423 SUIT_Desktop* aDesk = application()->desktop();
424 NewGeom_NestedButton* anAction = new NewGeom_NestedButton(aDesk, theNestedActions);
425 anAction->setData(theInfo.id);
426 anAction->setCheckable(theInfo.checkable);
427 anAction->setChecked(theInfo.checked);
428 anAction->setEnabled(theInfo.enabled);
429 anAction->setVisible(theInfo.visible);
430 anAction->setIcon(theInfo.icon);
431 anAction->setText(theInfo.text);
432 anAction->setToolTip(theInfo.toolTip);
433 anAction->setShortcut(theInfo.shortcut);
434 anAction->setFont(theInfo.font);
436 //int aItemId = createMenu(aId, aMenu, -1, 10);
437 int aToolId = createTool(anAction, aTool, aId);
443 //******************************************************
444 QAction* NewGeom_Module::addDesktopCommand(const QString& theId, const QString& theTitle,
445 const QString& theTip, const QIcon& theIcon,
446 const QKeySequence& theKeys, bool isCheckable,
447 const char* theMenuSourceText, const int theMenuPosition)
449 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
451 int aId = myActionsList.size();
452 myActionsList.append(theId);
453 SUIT_Desktop* aDesk = application()->desktop();
455 for (unsigned int i = 0; i < theKeys.count(); i++)
457 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
459 aAction->setData(theId);
460 createMenu(aId, aMenu, theMenuPosition);
464 //******************************************************
465 void NewGeom_Module::addDesktopMenuSeparator(const char* theMenuSourceText, const int theMenuPosition)
467 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
468 createMenu(separator(), aMenu, -1, theMenuPosition);
471 bool NewGeom_Module::addActionInToolbar( QAction* theAction, const QString& theToolBarTitle )
476 SUIT_Desktop* aDesktop = application()->desktop();
480 QtxActionToolMgr* aToolMgr = aDesktop->toolMgr();
484 aToolMgr->append( theAction, theToolBarTitle );
488 //******************************************************
489 QList<QAction*> NewGeom_Module::commandList() const
491 QList<QAction*> aActions;
492 for (int i = 0; i < myActionsList.size(); i++) {
493 QAction* aCmd = action(i);
494 if (aCmd && myActionsList.contains(aCmd->data().toString()))
495 aActions.append(aCmd);
500 //******************************************************
501 QStringList NewGeom_Module::commandIdList() const
503 return myActionsList;
506 //******************************************************
507 QMainWindow* NewGeom_Module::desktop() const
509 return application()->desktop();
512 //******************************************************
513 QString NewGeom_Module::commandId(const QAction* theCmd) const
515 int aId = actionId(theCmd);
516 if (aId < myActionsList.size())
517 return myActionsList[aId];
521 //******************************************************
522 QAction* NewGeom_Module::command(const QString& theId) const
524 int aId = myActionsList.indexOf(theId);
525 if ((aId != -1) && (aId < myActionsList.size())) {
531 //******************************************************
532 void NewGeom_Module::setNestedActions(const QString& theId, const QStringList& theActions)
534 myNestedActions[theId] = theActions;
537 //******************************************************
538 QStringList NewGeom_Module::nestedActions(const QString& theId) const
540 if (myNestedActions.contains(theId))
541 return myNestedActions[theId];
542 return QStringList();
545 //******************************************************
546 void NewGeom_Module::setDocumentKind(const QString& theId, const QString& theKind)
548 myDocumentType[theId] = theKind;
551 //******************************************************
552 QString NewGeom_Module::documentKind(const QString& theId) const
554 if (myDocumentType.contains(theId))
555 return myDocumentType[theId];
560 //******************************************************
561 void NewGeom_Module::selectionChanged()
563 LightApp_Module::selectionChanged();
564 myWorkshop->salomeViewerSelectionChanged();
567 //******************************************************
568 void NewGeom_Module::contextMenuPopup(const QString& theClient, QMenu* theMenu, QString& theTitle)
570 myWorkshop->contextMenuMgr()->updateViewerMenu();
571 myWorkshop->contextMenuMgr()->addViewerMenu(theMenu);
572 LightApp_Module::contextMenuPopup(theClient, theMenu, theTitle);
576 //******************************************************
577 void NewGeom_Module::createPreferences()
579 LightApp_Preferences* pref = preferences();
582 ModuleBase_Preferences::updateConfigByResources();
583 QString aModName = moduleName();
585 QtxPreferenceItem* item = pref->findItem(aModName, true );
586 if ( item && (!item->isEmpty() )) {
587 item->parentItem()->removeItem(item);
591 int catId = pref->addPreference(aModName, -1 );
594 NewGeom_PrefMgr aMgr(pref, aModName);
595 ModuleBase_Preferences::createEditContent(&aMgr, catId);
599 //******************************************************
600 void NewGeom_Module::preferencesChanged(const QString& theSection, const QString& theParam)
602 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
603 QString aVal = aResMgr->stringValue(theSection, theParam);
604 Config_Prop* aProp = Config_PropManager::findProp(theSection.toStdString(), theParam.toStdString());
605 std::string aValue = aVal.toStdString();
606 if (aValue.empty()) {
607 aValue = aProp->defaultValue();
608 aResMgr->setValue(theSection, theParam, QString(aValue.c_str()));
610 LightApp_Preferences* pref = preferences();
614 aProp->setValue(aValue);
616 myWorkshop->displayer()->redisplayObjects();
619 void NewGeom_Module::inspectSalomeModules()
621 QStringList aModuleNames;
622 getApp()->modules(aModuleNames, false);
623 foreach(QString eachModule, aModuleNames) {
624 Config_ModuleReader::addDependencyModule(eachModule.toStdString());
628 bool NewGeom_Module::abortAllOperations()
630 return workshop()->operationMgr()->abortAllOperations();