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();
278 myWorkshop->synchronizeViewer();
282 //******************************************************
283 void NewGeom_Module::onViewManagerRemoved(SUIT_ViewManager* theMgr)
286 if (theMgr->getType() == OCCViewer_Viewer::Type()) {
287 OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
288 if (mySelector->viewer() == aViewer) {
289 XGUI_Displayer* aDisp = myWorkshop->displayer();
290 QObjectPtrList aObjects = aDisp->displayedObjects();
291 foreach(ObjectPtr aObj, aObjects)
292 aObj->setDisplayed(false);
293 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
294 myProxyViewer->setSelector(0);
298 myWorkshop->module()->clearViewer();
304 //******************************************************
305 QtxPopupMgr* NewGeom_Module::popupMgr()
308 myPopupMgr = new QtxPopupMgr( 0, this );
312 //******************************************************
313 void NewGeom_Module::onDefaultPreferences()
315 // reset main resources
316 ModuleBase_Preferences::resetResourcePreferences(preferences());
317 // reset plugin's resources
318 ModuleBase_Preferences::resetConfigPropPreferences(preferences());
320 myWorkshop->displayer()->redisplayObjects();
323 //******************************************************
324 void NewGeom_Module::onUpdateCommandStatus()
326 getApp()->updateActions();
329 //******************************************************
330 NewGeom_OCCSelector* NewGeom_Module::createSelector(SUIT_ViewManager* theMgr)
332 if (theMgr->getType() == OCCViewer_Viewer::Type()) {
333 OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
334 NewGeom_OCCSelector* aSelector = new NewGeom_OCCSelector(aViewer, getApp()->selectionMgr());
335 LightApp_SelectionMgr* aMgr = getApp()->selectionMgr();
336 QList<SUIT_Selector*> aList;
337 aMgr->selectors(aList);
338 foreach(SUIT_Selector* aSel, aList)
340 aSel->setEnabled(aSel == aSelector);
342 myProxyViewer->setSelector(aSelector);
348 //******************************************************
349 CAM_DataModel* NewGeom_Module::createDataModel()
351 NewGeom_DataModel* aDataModel = new NewGeom_DataModel(this);
353 // Calling addComponent() for persistent functionality work in the SalomeApp_Study
354 LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>(application()->activeStudy() );
355 aStudy->addComponent(aDataModel);
360 QAction* NewGeom_Module::addFeature(const QString& theWBName, const ActionInfo& theInfo)
362 return addFeature(theWBName,
371 //******************************************************
372 QAction* NewGeom_Module::addFeature(const QString& theWBName, const QString& theId,
373 const QString& theTitle, const QString& theTip,
374 const QIcon& theIcon, const QKeySequence& theKeys,
377 static QString aLastTool = "";
379 if (aLastTool.isEmpty())
380 aLastTool = theWBName;
381 else if (theWBName != aLastTool) {
382 aLastTool = theWBName;
384 desktop()->addToolBarBreak();
390 int aMenu = createMenu(theWBName, -1, -1, 50);
391 int aTool = createTool(theWBName, theWBName);
393 int aId = myActionsList.size();
394 myActionsList.append(theId);
395 SUIT_Desktop* aDesk = application()->desktop();
397 for (unsigned int i = 0; i < theKeys.count(); i++)
399 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
401 aAction->setData(theId);
402 int aItemId = createMenu(aId, aMenu, -1, 10);
403 int aToolId = createTool(aId, aTool);
408 bool NewGeom_Module::isFeatureOfNested(const QAction* theAction)
410 return dynamic_cast<const NewGeom_NestedButton*>(theAction);
413 QAction* NewGeom_Module::addFeatureOfNested(const QString& theWBName,
414 const ActionInfo& theInfo,
415 const QList<QAction*>& theNestedActions)
417 int aMenu = createMenu(theWBName, -1, -1, 50);
418 int aTool = createTool(theWBName, theWBName);
420 int aId = myActionsList.size();
421 myActionsList.append(theInfo.id);
422 SUIT_Desktop* aDesk = application()->desktop();
423 NewGeom_NestedButton* anAction = new NewGeom_NestedButton(aDesk, theNestedActions);
424 anAction->setData(theInfo.id);
425 anAction->setCheckable(theInfo.checkable);
426 anAction->setChecked(theInfo.checked);
427 anAction->setEnabled(theInfo.enabled);
428 anAction->setVisible(theInfo.visible);
429 anAction->setIcon(theInfo.icon);
430 anAction->setText(theInfo.text);
431 anAction->setToolTip(theInfo.toolTip);
432 anAction->setShortcut(theInfo.shortcut);
433 anAction->setFont(theInfo.font);
435 //int aItemId = createMenu(aId, aMenu, -1, 10);
436 int aToolId = createTool(anAction, aTool, aId);
442 //******************************************************
443 QAction* NewGeom_Module::addDesktopCommand(const QString& theId, const QString& theTitle,
444 const QString& theTip, const QIcon& theIcon,
445 const QKeySequence& theKeys, bool isCheckable,
446 const char* theMenuSourceText, const int theMenuPosition)
448 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
450 int aId = myActionsList.size();
451 myActionsList.append(theId);
452 SUIT_Desktop* aDesk = application()->desktop();
454 for (unsigned int i = 0; i < theKeys.count(); i++)
456 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
458 aAction->setData(theId);
459 createMenu(aId, aMenu, theMenuPosition);
463 //******************************************************
464 void NewGeom_Module::addDesktopMenuSeparator(const char* theMenuSourceText, const int theMenuPosition)
466 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
467 createMenu(separator(), aMenu, -1, theMenuPosition);
470 bool NewGeom_Module::addActionInToolbar( QAction* theAction, const QString& theToolBarTitle )
475 SUIT_Desktop* aDesktop = application()->desktop();
479 QtxActionToolMgr* aToolMgr = aDesktop->toolMgr();
483 aToolMgr->append( theAction, theToolBarTitle );
487 //******************************************************
488 QList<QAction*> NewGeom_Module::commandList() const
490 QList<QAction*> aActions;
491 for (int i = 0; i < myActionsList.size(); i++) {
492 QAction* aCmd = action(i);
493 if (aCmd && myActionsList.contains(aCmd->data().toString()))
494 aActions.append(aCmd);
499 //******************************************************
500 QStringList NewGeom_Module::commandIdList() const
502 return myActionsList;
505 //******************************************************
506 QMainWindow* NewGeom_Module::desktop() const
508 return application()->desktop();
511 //******************************************************
512 QString NewGeom_Module::commandId(const QAction* theCmd) const
514 int aId = actionId(theCmd);
515 if (aId < myActionsList.size())
516 return myActionsList[aId];
520 //******************************************************
521 QAction* NewGeom_Module::command(const QString& theId) const
523 int aId = myActionsList.indexOf(theId);
524 if ((aId != -1) && (aId < myActionsList.size())) {
530 //******************************************************
531 void NewGeom_Module::setNestedActions(const QString& theId, const QStringList& theActions)
533 myNestedActions[theId] = theActions;
536 //******************************************************
537 QStringList NewGeom_Module::nestedActions(const QString& theId) const
539 if (myNestedActions.contains(theId))
540 return myNestedActions[theId];
541 return QStringList();
544 //******************************************************
545 void NewGeom_Module::setDocumentKind(const QString& theId, const QString& theKind)
547 myDocumentType[theId] = theKind;
550 //******************************************************
551 QString NewGeom_Module::documentKind(const QString& theId) const
553 if (myDocumentType.contains(theId))
554 return myDocumentType[theId];
559 //******************************************************
560 void NewGeom_Module::selectionChanged()
562 LightApp_Module::selectionChanged();
563 myWorkshop->salomeViewerSelectionChanged();
566 //******************************************************
567 void NewGeom_Module::contextMenuPopup(const QString& theClient, QMenu* theMenu, QString& theTitle)
569 myWorkshop->contextMenuMgr()->updateViewerMenu();
570 myWorkshop->contextMenuMgr()->addViewerMenu(theMenu);
571 LightApp_Module::contextMenuPopup(theClient, theMenu, theTitle);
575 //******************************************************
576 void NewGeom_Module::createPreferences()
578 LightApp_Preferences* pref = preferences();
581 ModuleBase_Preferences::updateConfigByResources();
582 QString aModName = moduleName();
584 QtxPreferenceItem* item = pref->findItem(aModName, true );
585 if ( item && (!item->isEmpty() )) {
586 item->parentItem()->removeItem(item);
590 int catId = pref->addPreference(aModName, -1 );
593 NewGeom_PrefMgr aMgr(pref, aModName);
594 ModuleBase_Preferences::createEditContent(&aMgr, catId);
598 //******************************************************
599 void NewGeom_Module::preferencesChanged(const QString& theSection, const QString& theParam)
601 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
602 QString aVal = aResMgr->stringValue(theSection, theParam);
603 Config_Prop* aProp = Config_PropManager::findProp(theSection.toStdString(), theParam.toStdString());
604 std::string aValue = aVal.toStdString();
605 if (aValue.empty()) {
606 aValue = aProp->defaultValue();
607 aResMgr->setValue(theSection, theParam, QString(aValue.c_str()));
609 LightApp_Preferences* pref = preferences();
613 aProp->setValue(aValue);
615 myWorkshop->displayer()->redisplayObjects();
618 void NewGeom_Module::inspectSalomeModules()
620 QStringList aModuleNames;
621 getApp()->modules(aModuleNames, false);
622 foreach(QString eachModule, aModuleNames) {
623 Config_ModuleReader::addDependencyModule(eachModule.toStdString());
627 bool NewGeom_Module::abortAllOperations()
629 return workshop()->operationMgr()->abortAllOperations();