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 ModuleBase_Preferences::resetConfig();
316 ModuleBase_Preferences::updateResourcesByConfig();
318 LightApp_Preferences* pref = preferences();
322 myWorkshop->displayer()->redisplayObjects();
325 //******************************************************
326 void NewGeom_Module::onUpdateCommandStatus()
328 getApp()->updateActions();
331 //******************************************************
332 NewGeom_OCCSelector* NewGeom_Module::createSelector(SUIT_ViewManager* theMgr)
334 if (theMgr->getType() == OCCViewer_Viewer::Type()) {
335 OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
336 NewGeom_OCCSelector* aSelector = new NewGeom_OCCSelector(aViewer, getApp()->selectionMgr());
337 LightApp_SelectionMgr* aMgr = getApp()->selectionMgr();
338 QList<SUIT_Selector*> aList;
339 aMgr->selectors(aList);
340 foreach(SUIT_Selector* aSel, aList)
342 aSel->setEnabled(aSel == aSelector);
344 myProxyViewer->setSelector(aSelector);
350 //******************************************************
351 CAM_DataModel* NewGeom_Module::createDataModel()
353 NewGeom_DataModel* aDataModel = new NewGeom_DataModel(this);
355 // Calling addComponent() for persistent functionality work in the SalomeApp_Study
356 LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>(application()->activeStudy() );
357 aStudy->addComponent(aDataModel);
362 QAction* NewGeom_Module::addFeature(const QString& theWBName, const ActionInfo& theInfo)
364 return addFeature(theWBName,
373 //******************************************************
374 QAction* NewGeom_Module::addFeature(const QString& theWBName, const QString& theId,
375 const QString& theTitle, const QString& theTip,
376 const QIcon& theIcon, const QKeySequence& theKeys,
379 static QString aLastTool = "";
381 if (aLastTool.isEmpty())
382 aLastTool = theWBName;
383 else if (theWBName != aLastTool) {
384 aLastTool = theWBName;
386 desktop()->addToolBarBreak();
392 int aMenu = createMenu(theWBName, -1, -1, 50);
393 int aTool = createTool(theWBName, theWBName);
395 int aId = myActionsList.size();
396 myActionsList.append(theId);
397 SUIT_Desktop* aDesk = application()->desktop();
399 for (unsigned int i = 0; i < theKeys.count(); i++)
401 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
403 aAction->setData(theId);
404 int aItemId = createMenu(aId, aMenu, -1, 10);
405 int aToolId = createTool(aId, aTool);
410 bool NewGeom_Module::isFeatureOfNested(const QAction* theAction)
412 return dynamic_cast<const NewGeom_NestedButton*>(theAction);
415 QAction* NewGeom_Module::addFeatureOfNested(const QString& theWBName,
416 const ActionInfo& theInfo,
417 const QList<QAction*>& theNestedActions)
419 int aMenu = createMenu(theWBName, -1, -1, 50);
420 int aTool = createTool(theWBName, theWBName);
422 int aId = myActionsList.size();
423 myActionsList.append(theInfo.id);
424 SUIT_Desktop* aDesk = application()->desktop();
425 NewGeom_NestedButton* anAction = new NewGeom_NestedButton(aDesk, theNestedActions);
426 anAction->setData(theInfo.id);
427 anAction->setCheckable(theInfo.checkable);
428 anAction->setChecked(theInfo.checked);
429 anAction->setEnabled(theInfo.enabled);
430 anAction->setVisible(theInfo.visible);
431 anAction->setIcon(theInfo.icon);
432 anAction->setText(theInfo.text);
433 anAction->setToolTip(theInfo.toolTip);
434 anAction->setShortcut(theInfo.shortcut);
435 anAction->setFont(theInfo.font);
437 //int aItemId = createMenu(aId, aMenu, -1, 10);
438 int aToolId = createTool(anAction, aTool, aId);
444 //******************************************************
445 QAction* NewGeom_Module::addDesktopCommand(const QString& theId, const QString& theTitle,
446 const QString& theTip, const QIcon& theIcon,
447 const QKeySequence& theKeys, bool isCheckable,
448 const char* theMenuSourceText, const int theMenuPosition)
450 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
452 int aId = myActionsList.size();
453 myActionsList.append(theId);
454 SUIT_Desktop* aDesk = application()->desktop();
456 for (unsigned int i = 0; i < theKeys.count(); i++)
458 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
460 aAction->setData(theId);
461 createMenu(aId, aMenu, theMenuPosition);
465 //******************************************************
466 void NewGeom_Module::addDesktopMenuSeparator(const char* theMenuSourceText, const int theMenuPosition)
468 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
469 createMenu(separator(), aMenu, -1, theMenuPosition);
472 bool NewGeom_Module::addActionInToolbar( QAction* theAction, const QString& theToolBarTitle )
477 SUIT_Desktop* aDesktop = application()->desktop();
481 QtxActionToolMgr* aToolMgr = aDesktop->toolMgr();
485 aToolMgr->append( theAction, theToolBarTitle );
489 //******************************************************
490 QList<QAction*> NewGeom_Module::commandList() const
492 QList<QAction*> aActions;
493 for (int i = 0; i < myActionsList.size(); i++) {
494 QAction* aCmd = action(i);
495 if (aCmd && myActionsList.contains(aCmd->data().toString()))
496 aActions.append(aCmd);
501 //******************************************************
502 QStringList NewGeom_Module::commandIdList() const
504 return myActionsList;
507 //******************************************************
508 QMainWindow* NewGeom_Module::desktop() const
510 return application()->desktop();
513 //******************************************************
514 QString NewGeom_Module::commandId(const QAction* theCmd) const
516 int aId = actionId(theCmd);
517 if (aId < myActionsList.size())
518 return myActionsList[aId];
522 //******************************************************
523 QAction* NewGeom_Module::command(const QString& theId) const
525 int aId = myActionsList.indexOf(theId);
526 if ((aId != -1) && (aId < myActionsList.size())) {
532 //******************************************************
533 void NewGeom_Module::setNestedActions(const QString& theId, const QStringList& theActions)
535 myNestedActions[theId] = theActions;
538 //******************************************************
539 QStringList NewGeom_Module::nestedActions(const QString& theId) const
541 if (myNestedActions.contains(theId))
542 return myNestedActions[theId];
543 return QStringList();
546 //******************************************************
547 void NewGeom_Module::setDocumentKind(const QString& theId, const QString& theKind)
549 myDocumentType[theId] = theKind;
552 //******************************************************
553 QString NewGeom_Module::documentKind(const QString& theId) const
555 if (myDocumentType.contains(theId))
556 return myDocumentType[theId];
561 //******************************************************
562 void NewGeom_Module::selectionChanged()
564 LightApp_Module::selectionChanged();
565 myWorkshop->salomeViewerSelectionChanged();
568 //******************************************************
569 void NewGeom_Module::contextMenuPopup(const QString& theClient, QMenu* theMenu, QString& theTitle)
571 myWorkshop->contextMenuMgr()->updateViewerMenu();
572 myWorkshop->contextMenuMgr()->addViewerMenu(theMenu);
573 LightApp_Module::contextMenuPopup(theClient, theMenu, theTitle);
577 //******************************************************
578 void NewGeom_Module::createPreferences()
580 LightApp_Preferences* pref = preferences();
583 ModuleBase_Preferences::updateConfigByResources();
584 QString aModName = moduleName();
586 QtxPreferenceItem* item = pref->findItem(aModName, true );
587 if ( item && (!item->isEmpty() )) {
588 item->parentItem()->removeItem(item);
592 int catId = pref->addPreference(aModName, -1 );
595 NewGeom_PrefMgr aMgr(pref, aModName);
596 ModuleBase_Preferences::createEditContent(&aMgr, catId);
600 //******************************************************
601 void NewGeom_Module::preferencesChanged(const QString& theSection, const QString& theParam)
603 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
604 QString aVal = aResMgr->stringValue(theSection, theParam);
605 Config_Prop* aProp = Config_PropManager::findProp(theSection.toStdString(), theParam.toStdString());
606 std::string aValue = aVal.toStdString();
607 if (aValue.empty()) {
608 aValue = aProp->defaultValue();
609 aResMgr->setValue(theSection, theParam, QString(aValue.c_str()));
611 LightApp_Preferences* pref = preferences();
615 aProp->setValue(aValue);
617 myWorkshop->displayer()->redisplayObjects();
620 void NewGeom_Module::inspectSalomeModules()
622 QStringList aModuleNames;
623 getApp()->modules(aModuleNames, false);
624 foreach(QString eachModule, aModuleNames) {
625 Config_ModuleReader::addDependencyModule(eachModule.toStdString());
629 bool NewGeom_Module::abortAllOperations()
631 return workshop()->operationMgr()->abortAllOperations();