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);
126 connect(anApp, SIGNAL(preferenceResetToDefaults()), this, SLOT(onDefaultPreferences()));
129 //******************************************************
130 void NewGeom_Module::windows(QMap<int, int>& theWndMap) const
132 theWndMap.insert(LightApp_Application::WT_PyConsole, Qt::BottomDockWidgetArea);
135 //******************************************************
136 void NewGeom_Module::viewManagers(QStringList& theList) const
138 theList.append(OCCViewer_Viewer::Type());
141 //******************************************************
142 bool NewGeom_Module::activateModule(SUIT_Study* theStudy)
144 bool isDone = LightApp_Module::activateModule(theStudy);
149 QObject* aObj = myWorkshop->objectBrowser()->parent();
150 QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
152 QAction* aViewAct = aObjDoc->toggleViewAction();
153 aViewAct->setEnabled(true);
154 myWorkshop->objectBrowser()->setVisible(true);
155 aObjDoc->setVisible(true);
156 desktop()->tabifyDockWidget(aObjDoc, myWorkshop->propertyPanel());
160 ViewManagerList OCCViewManagers;
161 application()->viewManagers(OCCViewer_Viewer::Type(), OCCViewManagers);
162 if (OCCViewManagers.size() > 0) {
163 mySelector = createSelector(OCCViewManagers.first());
166 // it should be pefromed after the selector creation in order to have AISContext
167 myWorkshop->activateModule();
168 //action(myEraseAll)->setEnabled(false);
171 myWorkshop->objectBrowser()->rebuildDataTree();
172 myWorkshop->updateCommandStatus();
174 // the display all results is not necessary anymore, it was commented in XGUI_Workshop,
175 // so it should be commented here
176 //QTimer::singleShot(1000, myWorkshop, SLOT(displayAllResults()));
179 myWorkshop->updateCommandStatus();
181 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
182 myIsStorePositions = aResMgr->booleanValue("Study", "store_positions", true);
183 myIsEditEnabled = getApp()->isEditEnabled();
184 getApp()->setEditEnabled(false);
186 // this following row is caused by #187 bug.
187 // SALOME saves the dock widget positions before deactivateModule() and
188 // load it after the module activation. So, if the panel is visible before
189 // deactivate, it becomes visible after activate.
190 // In order to avoid the visible property panel, the widget position save is
191 // switch off in this module
192 aResMgr->setValue("Study", "store_positions", false);
194 // Synchronize displayed objects
195 if (mySelector && mySelector->viewer()) {
196 Handle(AIS_InteractiveContext) aContext = mySelector->viewer()->getAISContext();
197 XGUI_Displayer* aDisp = myWorkshop->displayer();
198 QObjectPtrList aObjList = aDisp->displayedObjects();
200 AIS_ListOfInteractive aList;
201 aContext->DisplayedObjects(aList);
202 AIS_ListIteratorOfListOfInteractive aLIt;
203 Handle(AIS_InteractiveObject) anAISIO;
204 foreach (ObjectPtr aObj, aObjList) {
205 AISObjectPtr aPrs = aDisp->getAISObject(aObj);
206 Handle(AIS_InteractiveObject) aAIS = aPrs->impl<Handle(AIS_InteractiveObject)>();
208 for (aLIt.Initialize(aList); aLIt.More(); aLIt.Next()) {
209 anAISIO = aLIt.Value();
210 if (anAISIO.Access() == aAIS.Access()) {
216 aObj->setDisplayed(false);
217 //aDisp->erase(aObj, false);
220 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
222 myProxyViewer->activateViewer(true);
226 //******************************************************
227 bool NewGeom_Module::deactivateModule(SUIT_Study* theStudy)
229 myProxyViewer->activateViewer(false);
233 myWorkshop->deactivateModule();
235 QObject* aObj = myWorkshop->objectBrowser()->parent();
236 QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
238 aObjDoc->setVisible(false);
239 myWorkshop->objectBrowser()->setVisible(false);
240 QAction* aViewAct = aObjDoc->toggleViewAction();
241 aViewAct->setEnabled(false);
244 // the active operation should be stopped for the next activation.
245 // There should not be active operation and visualized preview.
246 // Abort operation should be performed before the selection's remove
247 // because the displayed objects should be removed from the viewer, but
248 // the AIS context is obtained from the selector.
249 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
250 while (anOperation) {
251 anOperation->abort();
252 anOperation = myWorkshop->operationMgr()->currentOperation();
254 // Delete selector because it has to be redefined on next activation
256 myProxyViewer->setSelector(0);
261 //myWorkshop->contextMenuMgr()->disconnectViewer();
263 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
264 aResMgr->setValue("Study", "store_positions", myIsStorePositions);
265 getApp()->setEditEnabled(myIsEditEnabled);
267 return LightApp_Module::deactivateModule(theStudy);
270 //******************************************************
271 void NewGeom_Module::onViewManagerAdded(SUIT_ViewManager* theMgr)
274 mySelector = createSelector(theMgr);
278 //******************************************************
279 void NewGeom_Module::onViewManagerRemoved(SUIT_ViewManager* theMgr)
282 if (theMgr->getType() == OCCViewer_Viewer::Type()) {
283 OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
284 if (mySelector->viewer() == aViewer) {
285 XGUI_Displayer* aDisp = myWorkshop->displayer();
286 QObjectPtrList aObjects = aDisp->displayedObjects();
287 foreach(ObjectPtr aObj, aObjects)
288 aObj->setDisplayed(false);
289 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
290 myProxyViewer->setSelector(0);
294 myWorkshop->module()->clearViewer();
300 //******************************************************
301 QtxPopupMgr* NewGeom_Module::popupMgr()
304 myPopupMgr = new QtxPopupMgr( 0, this );
308 //******************************************************
309 void NewGeom_Module::onDefaultPreferences()
311 ModuleBase_Preferences::resetConfig();
312 ModuleBase_Preferences::updateResourcesByConfig();
314 LightApp_Preferences* pref = preferences();
319 //******************************************************
320 void NewGeom_Module::onUpdateCommandStatus()
322 getApp()->updateActions();
325 //******************************************************
326 NewGeom_OCCSelector* NewGeom_Module::createSelector(SUIT_ViewManager* theMgr)
328 if (theMgr->getType() == OCCViewer_Viewer::Type()) {
329 OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
330 NewGeom_OCCSelector* aSelector = new NewGeom_OCCSelector(aViewer, getApp()->selectionMgr());
331 LightApp_SelectionMgr* aMgr = getApp()->selectionMgr();
332 QList<SUIT_Selector*> aList;
333 aMgr->selectors(aList);
334 foreach(SUIT_Selector* aSel, aList)
336 aSel->setEnabled(aSel == aSelector);
338 myProxyViewer->setSelector(aSelector);
344 //******************************************************
345 CAM_DataModel* NewGeom_Module::createDataModel()
347 NewGeom_DataModel* aDataModel = new NewGeom_DataModel(this);
349 // Calling addComponent() for persistent functionality work in the SalomeApp_Study
350 LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>(application()->activeStudy() );
351 aStudy->addComponent(aDataModel);
356 QAction* NewGeom_Module::addFeature(const QString& theWBName, const ActionInfo& theInfo)
358 return addFeature(theWBName,
367 //******************************************************
368 QAction* NewGeom_Module::addFeature(const QString& theWBName, const QString& theId,
369 const QString& theTitle, const QString& theTip,
370 const QIcon& theIcon, const QKeySequence& theKeys,
373 static QString aLastTool = "";
375 if (aLastTool.isEmpty())
376 aLastTool = theWBName;
377 else if (theWBName != aLastTool) {
378 aLastTool = theWBName;
380 desktop()->addToolBarBreak();
386 int aMenu = createMenu(theWBName, -1, -1, 50);
387 int aTool = createTool(theWBName, theWBName);
389 int aId = myActionsList.size();
390 myActionsList.append(theId);
391 SUIT_Desktop* aDesk = application()->desktop();
393 for (unsigned int i = 0; i < theKeys.count(); i++)
395 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
397 aAction->setData(theId);
398 int aItemId = createMenu(aId, aMenu, -1, 10);
399 int aToolId = createTool(aId, aTool);
404 bool NewGeom_Module::isNestedFeature(const QAction* theAction)
406 return dynamic_cast<const NewGeom_NestedButton*>(theAction);
409 QAction* NewGeom_Module::addNestedFeature(const QString& theWBName,
410 const ActionInfo& theInfo,
411 const QList<QAction*>& theNestedActions)
413 int aMenu = createMenu(theWBName, -1, -1, 50);
414 int aTool = createTool(theWBName, theWBName);
416 int aId = myActionsList.size();
417 myActionsList.append(theInfo.id);
418 SUIT_Desktop* aDesk = application()->desktop();
419 NewGeom_NestedButton* anAction = new NewGeom_NestedButton(aDesk, theNestedActions);
420 anAction->setData(theInfo.id);
421 anAction->setCheckable(theInfo.checkable);
422 anAction->setChecked(theInfo.checked);
423 anAction->setEnabled(theInfo.enabled);
424 anAction->setVisible(theInfo.visible);
425 anAction->setIcon(theInfo.icon);
426 anAction->setText(theInfo.text);
427 anAction->setToolTip(theInfo.toolTip);
428 anAction->setShortcut(theInfo.shortcut);
429 anAction->setFont(theInfo.font);
431 //int aItemId = createMenu(aId, aMenu, -1, 10);
432 int aToolId = createTool(anAction, aTool, aId);
438 //******************************************************
439 QAction* NewGeom_Module::addDesktopCommand(const QString& theId, const QString& theTitle,
440 const QString& theTip, const QIcon& theIcon,
441 const QKeySequence& theKeys, bool isCheckable,
442 const char* theMenuSourceText, const int theMenuPosition)
444 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
446 int aId = myActionsList.size();
447 myActionsList.append(theId);
448 SUIT_Desktop* aDesk = application()->desktop();
450 for (unsigned int i = 0; i < theKeys.count(); i++)
452 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
454 aAction->setData(theId);
455 createMenu(aId, aMenu, theMenuPosition);
459 //******************************************************
460 void NewGeom_Module::addDesktopMenuSeparator(const char* theMenuSourceText, const int theMenuPosition)
462 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
463 createMenu(separator(), aMenu, -1, theMenuPosition);
466 bool NewGeom_Module::addActionInToolbar( QAction* theAction, const QString& theToolBarTitle )
471 SUIT_Desktop* aDesktop = application()->desktop();
475 QtxActionToolMgr* aToolMgr = aDesktop->toolMgr();
479 aToolMgr->append( theAction, theToolBarTitle );
483 //******************************************************
484 QList<QAction*> NewGeom_Module::commandList() const
486 QList<QAction*> aActions;
487 for (int i = 0; i < myActionsList.size(); i++) {
488 QAction* aCmd = action(i);
489 if (aCmd && myActionsList.contains(aCmd->data().toString()))
490 aActions.append(aCmd);
495 //******************************************************
496 QStringList NewGeom_Module::commandIdList() const
498 return myActionsList;
501 //******************************************************
502 QMainWindow* NewGeom_Module::desktop() const
504 return application()->desktop();
507 //******************************************************
508 QString NewGeom_Module::commandId(const QAction* theCmd) const
510 int aId = actionId(theCmd);
511 if (aId < myActionsList.size())
512 return myActionsList[aId];
516 //******************************************************
517 QAction* NewGeom_Module::command(const QString& theId) const
519 int aId = myActionsList.indexOf(theId);
520 if ((aId != -1) && (aId < myActionsList.size())) {
526 //******************************************************
527 void NewGeom_Module::setNestedActions(const QString& theId, const QStringList& theActions)
529 myNestedActions[theId] = theActions;
532 //******************************************************
533 QStringList NewGeom_Module::nestedActions(const QString& theId) const
535 if (myNestedActions.contains(theId))
536 return myNestedActions[theId];
537 return QStringList();
540 //******************************************************
541 void NewGeom_Module::setDocumentKind(const QString& theId, const QString& theKind)
543 myDocumentType[theId] = theKind;
546 //******************************************************
547 QString NewGeom_Module::documentKind(const QString& theId) const
549 if (myDocumentType.contains(theId))
550 return myDocumentType[theId];
555 //******************************************************
556 void NewGeom_Module::selectionChanged()
558 LightApp_Module::selectionChanged();
559 myWorkshop->salomeViewerSelectionChanged();
562 //******************************************************
563 void NewGeom_Module::contextMenuPopup(const QString& theClient, QMenu* theMenu, QString& theTitle)
565 myWorkshop->contextMenuMgr()->updateViewerMenu();
566 myWorkshop->contextMenuMgr()->addViewerMenu(theMenu);
567 LightApp_Module::contextMenuPopup(theClient, theMenu, theTitle);
571 //******************************************************
572 void NewGeom_Module::createPreferences()
574 LightApp_Preferences* pref = preferences();
577 ModuleBase_Preferences::updateConfigByResources();
578 QString aModName = moduleName();
580 QtxPreferenceItem* item = pref->findItem(aModName, true );
581 if ( item && (!item->isEmpty() )) {
582 item->parentItem()->removeItem(item);
586 int catId = pref->addPreference(aModName, -1 );
589 NewGeom_PrefMgr aMgr(pref, aModName);
590 ModuleBase_Preferences::createEditContent(&aMgr, catId);
594 //******************************************************
595 void NewGeom_Module::preferencesChanged(const QString& theSection, const QString& theParam)
597 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
598 QString aVal = aResMgr->stringValue(theSection, theParam);
599 Config_Prop* aProp = Config_PropManager::findProp(theSection.toStdString(), theParam.toStdString());
600 std::string aValue = aVal.toStdString();
601 if (aValue.empty()) {
602 aValue = aProp->defaultValue();
603 aResMgr->setValue(theSection, theParam, QString(aValue.c_str()));
605 LightApp_Preferences* pref = preferences();
609 aProp->setValue(aValue);
611 // redisplay objects visualized in the viewer
612 static Events_ID EVENT_DISP = Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY);
613 static const ModelAPI_EventCreator* aECreator = ModelAPI_EventCreator::get();
614 QObjectPtrList aDisplayed = myWorkshop->displayer()->displayedObjects();
615 QObjectPtrList::const_iterator anIt = aDisplayed.begin(), aLast = aDisplayed.end();
616 for (; anIt != aLast; anIt++) {
617 aECreator->sendUpdated(*anIt, EVENT_DISP);
619 Events_Loop::loop()->flush(EVENT_DISP);
623 void NewGeom_Module::inspectSalomeModules()
625 QStringList aModuleNames;
626 getApp()->modules(aModuleNames, false);
627 foreach(QString eachModule, aModuleNames) {
628 Config_ModuleReader::addDependencyModule(eachModule.toStdString());