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>
20 #include <LightApp_Application.h>
21 #include <LightApp_SelectionMgr.h>
22 #include <LightApp_OCCSelector.h>
23 #include <LightApp_Study.h>
24 #include <OCCViewer_ViewModel.h>
26 #include <SUIT_Selector.h>
27 #include <SUIT_Desktop.h>
28 #include <SUIT_ViewManager.h>
29 #include <SUIT_ResourceMgr.h>
31 #include <QtxPopupMgr.h>
32 #include <QtxActionMenuMgr.h>
33 #include <QtxActionToolMgr.h>
34 #include <QtxResourceMgr.h>
36 #include <Config_PropManager.h>
37 #include <Config_ModuleReader.h>
39 #include <AIS_ListOfInteractive.hxx>
40 #include <AIS_ListIteratorOfListOfInteractive.hxx>
42 #include <QDockWidget>
49 NewGeom_EXPORT CAM_Module* createModule()
51 return new NewGeom_Module();
54 NewGeom_EXPORT char* getModuleVersion()
61 * Class for preferences management
63 class NewGeom_PrefMgr: public ModuleBase_IPrefMgr
67 /// \param theMgr preferences manager of SALOME
68 /// \param theModName name of the module
69 NewGeom_PrefMgr(LightApp_Preferences* theMgr, const QString& theModName):myMgr(theMgr), myModName(theModName) {}
71 virtual int addPreference(const QString& theLbl, int pId,
72 SUIT_PreferenceMgr::PrefItemType theType,
73 const QString& theSection, const QString& theName )
75 return myMgr->addPreference(myModName, theLbl, pId, theType, theSection, theName);
78 virtual void setItemProperty(const QString& thePropName,
79 const QVariant& theValue,
82 myMgr->setItemProperty(thePropName, theValue, theId);
86 virtual SUIT_PreferenceMgr* prefMgr() const { return myMgr; }
89 LightApp_Preferences* myMgr;
96 //******************************************************
97 NewGeom_Module::NewGeom_Module()
98 : LightApp_Module("NewGeom"),
99 mySelector(0), myIsOpened(0), myPopupMgr(0)
101 myWorkshop = new XGUI_Workshop(this);
102 connect(myWorkshop, SIGNAL(commandStatusUpdated()),
103 this, SLOT(onUpdateCommandStatus()));
105 myProxyViewer = new NewGeom_SalomeViewer(this);
107 ModuleBase_Preferences::setResourceMgr(application()->resourceMgr());
108 ModuleBase_Preferences::loadCustomProps();
111 //******************************************************
112 NewGeom_Module::~NewGeom_Module()
116 //******************************************************
117 void NewGeom_Module::initialize(CAM_Application* theApp)
119 LightApp_Module::initialize(theApp);
120 inspectSalomeModules();
122 myWorkshop->startApplication();
123 LightApp_Application* anApp = dynamic_cast<LightApp_Application*>(theApp);
125 connect(anApp, SIGNAL(preferenceResetToDefaults()), this, SLOT(onDefaultPreferences()));
128 //******************************************************
129 void NewGeom_Module::windows(QMap<int, int>& theWndMap) const
131 theWndMap.insert(LightApp_Application::WT_PyConsole, Qt::BottomDockWidgetArea);
134 //******************************************************
135 void NewGeom_Module::viewManagers(QStringList& theList) const
137 theList.append(OCCViewer_Viewer::Type());
140 //******************************************************
141 bool NewGeom_Module::activateModule(SUIT_Study* theStudy)
143 bool isDone = LightApp_Module::activateModule(theStudy);
148 QObject* aObj = myWorkshop->objectBrowser()->parent();
149 QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
151 QAction* aViewAct = aObjDoc->toggleViewAction();
152 aViewAct->setEnabled(true);
153 myWorkshop->objectBrowser()->setVisible(true);
154 aObjDoc->setVisible(true);
155 desktop()->tabifyDockWidget(aObjDoc, myWorkshop->propertyPanel());
159 ViewManagerList OCCViewManagers;
160 application()->viewManagers(OCCViewer_Viewer::Type(), OCCViewManagers);
161 if (OCCViewManagers.size() > 0) {
162 mySelector = createSelector(OCCViewManagers.first());
165 // it should be pefromed after the selector creation in order to have AISContext
166 myWorkshop->activateModule();
167 //action(myEraseAll)->setEnabled(false);
170 myWorkshop->objectBrowser()->rebuildDataTree();
171 myWorkshop->updateCommandStatus();
173 // the display all results is not necessary anymore, it was commented in XGUI_Workshop,
174 // so it should be commented here
175 //QTimer::singleShot(1000, myWorkshop, SLOT(displayAllResults()));
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 if (mySelector && mySelector->viewer()) {
195 Handle(AIS_InteractiveContext) aContext = mySelector->viewer()->getAISContext();
196 XGUI_Displayer* aDisp = myWorkshop->displayer();
197 QObjectPtrList aObjList = aDisp->displayedObjects();
199 AIS_ListOfInteractive aList;
200 aContext->DisplayedObjects(aList);
201 AIS_ListIteratorOfListOfInteractive aLIt;
202 Handle(AIS_InteractiveObject) anAISIO;
203 foreach (ObjectPtr aObj, aObjList) {
204 AISObjectPtr aPrs = aDisp->getAISObject(aObj);
205 Handle(AIS_InteractiveObject) aAIS = aPrs->impl<Handle(AIS_InteractiveObject)>();
207 for (aLIt.Initialize(aList); aLIt.More(); aLIt.Next()) {
208 anAISIO = aLIt.Value();
209 if (anAISIO.Access() == aAIS.Access()) {
215 aObj->setDisplayed(false);
216 //aDisp->erase(aObj, false);
219 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
221 myProxyViewer->activateViewer(true);
225 //******************************************************
226 bool NewGeom_Module::deactivateModule(SUIT_Study* theStudy)
228 myProxyViewer->activateViewer(false);
232 myWorkshop->deactivateModule();
234 QObject* aObj = myWorkshop->objectBrowser()->parent();
235 QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
237 aObjDoc->setVisible(false);
238 myWorkshop->objectBrowser()->setVisible(false);
239 QAction* aViewAct = aObjDoc->toggleViewAction();
240 aViewAct->setEnabled(false);
243 // the active operation should be stopped for the next activation.
244 // There should not be active operation and visualized preview.
245 // Abort operation should be performed before the selection's remove
246 // because the displayed objects should be removed from the viewer, but
247 // the AIS context is obtained from the selector.
248 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
249 while (anOperation) {
250 anOperation->abort();
251 anOperation = myWorkshop->operationMgr()->currentOperation();
253 // Delete selector because it has to be redefined on next activation
255 myProxyViewer->setSelector(0);
260 //myWorkshop->contextMenuMgr()->disconnectViewer();
262 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
263 aResMgr->setValue("Study", "store_positions", myIsStorePositions);
264 getApp()->setEditEnabled(myIsEditEnabled);
266 return LightApp_Module::deactivateModule(theStudy);
269 //******************************************************
270 void NewGeom_Module::onViewManagerAdded(SUIT_ViewManager* theMgr)
273 mySelector = createSelector(theMgr);
277 //******************************************************
278 void NewGeom_Module::onViewManagerRemoved(SUIT_ViewManager* theMgr)
281 if (theMgr->getType() == OCCViewer_Viewer::Type()) {
282 OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
283 if (mySelector->viewer() == aViewer) {
284 XGUI_Displayer* aDisp = myWorkshop->displayer();
285 QObjectPtrList aObjects = aDisp->displayedObjects();
286 foreach(ObjectPtr aObj, aObjects)
287 aObj->setDisplayed(false);
288 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
289 myProxyViewer->setSelector(0);
297 //******************************************************
298 QtxPopupMgr* NewGeom_Module::popupMgr()
301 myPopupMgr = new QtxPopupMgr( 0, this );
305 //******************************************************
306 void NewGeom_Module::onDefaultPreferences()
308 ModuleBase_Preferences::resetConfig();
309 ModuleBase_Preferences::updateResourcesByConfig();
311 LightApp_Preferences* pref = preferences();
316 //******************************************************
317 void NewGeom_Module::onUpdateCommandStatus()
319 getApp()->updateActions();
322 //******************************************************
323 NewGeom_OCCSelector* NewGeom_Module::createSelector(SUIT_ViewManager* theMgr)
325 if (theMgr->getType() == OCCViewer_Viewer::Type()) {
326 OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
327 NewGeom_OCCSelector* aSelector = new NewGeom_OCCSelector(aViewer, getApp()->selectionMgr());
328 LightApp_SelectionMgr* aMgr = getApp()->selectionMgr();
329 QList<SUIT_Selector*> aList;
330 aMgr->selectors(aList);
331 foreach(SUIT_Selector* aSel, aList)
333 aSel->setEnabled(aSel == aSelector);
335 myProxyViewer->setSelector(aSelector);
341 //******************************************************
342 CAM_DataModel* NewGeom_Module::createDataModel()
344 NewGeom_DataModel* aDataModel = new NewGeom_DataModel(this);
346 // Calling addComponent() for persistent functionality work in the SalomeApp_Study
347 LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>(application()->activeStudy() );
348 aStudy->addComponent(aDataModel);
353 QAction* NewGeom_Module::addFeature(const QString& theWBName, const ActionInfo& theInfo)
355 return addFeature(theWBName,
364 //******************************************************
365 QAction* NewGeom_Module::addFeature(const QString& theWBName, const QString& theId,
366 const QString& theTitle, const QString& theTip,
367 const QIcon& theIcon, const QKeySequence& theKeys,
370 static QString aLastTool = "";
372 if (aLastTool.isEmpty())
373 aLastTool = theWBName;
374 else if (theWBName != aLastTool) {
375 aLastTool = theWBName;
377 desktop()->addToolBarBreak();
383 int aMenu = createMenu(theWBName, -1, -1, 50);
384 int aTool = createTool(theWBName, theWBName);
386 int aId = myActionsList.size();
387 myActionsList.append(theId);
388 SUIT_Desktop* aDesk = application()->desktop();
390 for (unsigned int i = 0; i < theKeys.count(); i++)
392 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
394 aAction->setData(theId);
395 int aItemId = createMenu(aId, aMenu, -1, 10);
396 int aToolId = createTool(aId, aTool);
401 bool NewGeom_Module::isNestedFeature(const QAction* theAction)
403 return dynamic_cast<const NewGeom_NestedButton*>(theAction);
406 QAction* NewGeom_Module::addNestedFeature(const QString& theWBName,
407 const ActionInfo& theInfo,
408 const QList<QAction*>& theNestedActions)
410 int aMenu = createMenu(theWBName, -1, -1, 50);
411 int aTool = createTool(theWBName, theWBName);
413 int aId = myActionsList.size();
414 myActionsList.append(theInfo.id);
415 SUIT_Desktop* aDesk = application()->desktop();
416 NewGeom_NestedButton* anAction = new NewGeom_NestedButton(aDesk, theNestedActions);
417 anAction->setData(theInfo.id);
418 anAction->setCheckable(theInfo.checkable);
419 anAction->setChecked(theInfo.checked);
420 anAction->setEnabled(theInfo.enabled);
421 anAction->setVisible(theInfo.visible);
422 anAction->setIcon(theInfo.icon);
423 anAction->setText(theInfo.text);
424 anAction->setToolTip(theInfo.toolTip);
425 anAction->setShortcut(theInfo.shortcut);
426 anAction->setFont(theInfo.font);
428 //int aItemId = createMenu(aId, aMenu, -1, 10);
429 int aToolId = createTool(anAction, aTool, aId);
435 //******************************************************
436 QAction* NewGeom_Module::addDesktopCommand(const QString& theId, const QString& theTitle,
437 const QString& theTip, const QIcon& theIcon,
438 const QKeySequence& theKeys, bool isCheckable,
439 const char* theMenuSourceText, const int theMenuPosition)
441 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
443 int aId = myActionsList.size();
444 myActionsList.append(theId);
445 SUIT_Desktop* aDesk = application()->desktop();
447 for (unsigned int i = 0; i < theKeys.count(); i++)
449 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
451 aAction->setData(theId);
452 createMenu(aId, aMenu, theMenuPosition);
456 //******************************************************
457 void NewGeom_Module::addDesktopMenuSeparator(const char* theMenuSourceText, const int theMenuPosition)
459 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
460 createMenu(separator(), aMenu, -1, theMenuPosition);
463 bool NewGeom_Module::addActionInToolbar( QAction* theAction, const QString& theToolBarTitle )
468 SUIT_Desktop* aDesktop = application()->desktop();
472 QtxActionToolMgr* aToolMgr = aDesktop->toolMgr();
476 aToolMgr->append( theAction, theToolBarTitle );
480 //******************************************************
481 QList<QAction*> NewGeom_Module::commandList() const
483 QList<QAction*> aActions;
484 for (int i = 0; i < myActionsList.size(); i++) {
485 QAction* aCmd = action(i);
486 if (aCmd && myActionsList.contains(aCmd->data().toString()))
487 aActions.append(aCmd);
492 //******************************************************
493 QStringList NewGeom_Module::commandIdList() const
495 return myActionsList;
498 //******************************************************
499 QMainWindow* NewGeom_Module::desktop() const
501 return application()->desktop();
504 //******************************************************
505 QString NewGeom_Module::commandId(const QAction* theCmd) const
507 int aId = actionId(theCmd);
508 if (aId < myActionsList.size())
509 return myActionsList[aId];
513 //******************************************************
514 QAction* NewGeom_Module::command(const QString& theId) const
516 int aId = myActionsList.indexOf(theId);
517 if ((aId != -1) && (aId < myActionsList.size())) {
523 //******************************************************
524 void NewGeom_Module::setNestedActions(const QString& theId, const QStringList& theActions)
526 myNestedActions[theId] = theActions;
529 //******************************************************
530 QStringList NewGeom_Module::nestedActions(const QString& theId) const
532 if (myNestedActions.contains(theId))
533 return myNestedActions[theId];
534 return QStringList();
537 //******************************************************
538 void NewGeom_Module::setDocumentKind(const QString& theId, const QString& theKind)
540 myDocumentType[theId] = theKind;
543 //******************************************************
544 QString NewGeom_Module::documentKind(const QString& theId) const
546 if (myDocumentType.contains(theId))
547 return myDocumentType[theId];
552 //******************************************************
553 void NewGeom_Module::selectionChanged()
555 LightApp_Module::selectionChanged();
556 myWorkshop->salomeViewerSelectionChanged();
559 //******************************************************
560 void NewGeom_Module::contextMenuPopup(const QString& theClient, QMenu* theMenu, QString& theTitle)
562 myWorkshop->contextMenuMgr()->updateViewerMenu();
563 myWorkshop->contextMenuMgr()->addViewerMenu(theMenu);
564 LightApp_Module::contextMenuPopup(theClient, theMenu, theTitle);
568 //******************************************************
569 void NewGeom_Module::createPreferences()
571 LightApp_Preferences* pref = preferences();
574 ModuleBase_Preferences::updateConfigByResources();
575 QString aModName = moduleName();
577 QtxPreferenceItem* item = pref->findItem(aModName, true );
578 if ( item && (!item->isEmpty() )) {
579 item->parentItem()->removeItem(item);
583 int catId = pref->addPreference(aModName, -1 );
586 NewGeom_PrefMgr aMgr(pref, aModName);
587 ModuleBase_Preferences::createEditContent(&aMgr, catId);
591 //******************************************************
592 void NewGeom_Module::preferencesChanged(const QString& theSection, const QString& theParam)
594 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
595 QString aVal = aResMgr->stringValue(theSection, theParam);
596 Config_Prop* aProp = Config_PropManager::findProp(theSection.toStdString(), theParam.toStdString());
597 std::string aValue = aVal.toStdString();
598 if (aValue.empty()) {
599 aValue = aProp->defaultValue();
600 aResMgr->setValue(theSection, theParam, QString(aValue.c_str()));
602 LightApp_Preferences* pref = preferences();
606 aProp->setValue(aValue);
610 void NewGeom_Module::inspectSalomeModules()
612 QStringList aModuleNames;
613 getApp()->modules(aModuleNames, false);
614 foreach(QString eachModule, aModuleNames) {
615 Config_ModuleReader::addDependencyModule(eachModule.toStdString());