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);
402 QAction* NewGeom_Module::addNestedFeature(const QString& theWBName,
403 const ActionInfo& theInfo,
404 const QList<QAction*>& theNestedActions)
406 int aMenu = createMenu(theWBName, -1, -1, 50);
407 int aTool = createTool(theWBName, theWBName);
409 int aId = myActionsList.size();
410 myActionsList.append(theInfo.id);
411 SUIT_Desktop* aDesk = application()->desktop();
412 NewGeom_NestedButton* anAction = new NewGeom_NestedButton(aDesk, theNestedActions);
413 anAction->setData(theInfo.id);
414 anAction->setCheckable(theInfo.checkable);
415 anAction->setChecked(theInfo.checked);
416 anAction->setEnabled(theInfo.enabled);
417 anAction->setVisible(theInfo.visible);
418 anAction->setIcon(theInfo.icon);
419 anAction->setText(theInfo.text);
420 anAction->setToolTip(theInfo.toolTip);
421 anAction->setShortcut(theInfo.shortcut);
422 anAction->setFont(theInfo.font);
424 //int aItemId = createMenu(aId, aMenu, -1, 10);
425 int aToolId = createTool(anAction, aTool, aId);
431 //******************************************************
432 QAction* NewGeom_Module::addDesktopCommand(const QString& theId, const QString& theTitle,
433 const QString& theTip, const QIcon& theIcon,
434 const QKeySequence& theKeys, bool isCheckable,
435 const char* theMenuSourceText, const int theMenuPosition)
437 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
439 int aId = myActionsList.size();
440 myActionsList.append(theId);
441 SUIT_Desktop* aDesk = application()->desktop();
443 for (unsigned int i = 0; i < theKeys.count(); i++)
445 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
447 aAction->setData(theId);
448 createMenu(aId, aMenu, theMenuPosition);
452 //******************************************************
453 void NewGeom_Module::addDesktopMenuSeparator(const char* theMenuSourceText, const int theMenuPosition)
455 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
456 createMenu(separator(), aMenu, -1, theMenuPosition);
459 bool NewGeom_Module::addActionInToolbar( QAction* theAction, const QString& theToolBarTitle )
464 SUIT_Desktop* aDesktop = application()->desktop();
468 QtxActionToolMgr* aToolMgr = aDesktop->toolMgr();
472 aToolMgr->append( theAction, theToolBarTitle );
476 //******************************************************
477 QList<QAction*> NewGeom_Module::commandList() const
479 QList<QAction*> aActions;
480 for (int i = 0; i < myActionsList.size(); i++) {
481 QAction* aCmd = action(i);
482 if (aCmd && myActionsList.contains(aCmd->data().toString()))
483 aActions.append(aCmd);
488 //******************************************************
489 QStringList NewGeom_Module::commandIdList() const
491 return myActionsList;
494 //******************************************************
495 QMainWindow* NewGeom_Module::desktop() const
497 return application()->desktop();
500 //******************************************************
501 QString NewGeom_Module::commandId(const QAction* theCmd) const
503 int aId = actionId(theCmd);
504 if (aId < myActionsList.size())
505 return myActionsList[aId];
509 //******************************************************
510 QAction* NewGeom_Module::command(const QString& theId) const
512 int aId = myActionsList.indexOf(theId);
513 if ((aId != -1) && (aId < myActionsList.size())) {
519 //******************************************************
520 void NewGeom_Module::setNestedActions(const QString& theId, const QStringList& theActions)
522 myNestedActions[theId] = theActions;
525 //******************************************************
526 QStringList NewGeom_Module::nestedActions(const QString& theId) const
528 if (myNestedActions.contains(theId))
529 return myNestedActions[theId];
530 return QStringList();
533 //******************************************************
534 void NewGeom_Module::setDocumentKind(const QString& theId, const QString& theKind)
536 myDocumentType[theId] = theKind;
539 //******************************************************
540 QString NewGeom_Module::documentKind(const QString& theId) const
542 if (myDocumentType.contains(theId))
543 return myDocumentType[theId];
548 //******************************************************
549 void NewGeom_Module::selectionChanged()
551 LightApp_Module::selectionChanged();
552 myWorkshop->salomeViewerSelectionChanged();
555 //******************************************************
556 void NewGeom_Module::contextMenuPopup(const QString& theClient, QMenu* theMenu, QString& theTitle)
558 myWorkshop->contextMenuMgr()->updateViewerMenu();
559 myWorkshop->contextMenuMgr()->addViewerMenu(theMenu);
560 LightApp_Module::contextMenuPopup(theClient, theMenu, theTitle);
564 //******************************************************
565 void NewGeom_Module::createPreferences()
567 LightApp_Preferences* pref = preferences();
570 ModuleBase_Preferences::updateConfigByResources();
571 QString aModName = moduleName();
573 QtxPreferenceItem* item = pref->findItem(aModName, true );
574 if ( item && (!item->isEmpty() )) {
575 item->parentItem()->removeItem(item);
579 int catId = pref->addPreference(aModName, -1 );
582 NewGeom_PrefMgr aMgr(pref, aModName);
583 ModuleBase_Preferences::createEditContent(&aMgr, catId);
587 //******************************************************
588 void NewGeom_Module::preferencesChanged(const QString& theSection, const QString& theParam)
590 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
591 QString aVal = aResMgr->stringValue(theSection, theParam);
592 Config_Prop* aProp = Config_PropManager::findProp(theSection.toStdString(), theParam.toStdString());
593 std::string aValue = aVal.toStdString();
594 if (aValue.empty()) {
595 aValue = aProp->defaultValue();
596 aResMgr->setValue(theSection, theParam, QString(aValue.c_str()));
598 LightApp_Preferences* pref = preferences();
602 aProp->setValue(aValue);
606 void NewGeom_Module::inspectSalomeModules()
608 QStringList aModuleNames;
609 getApp()->modules(aModuleNames, false);
610 foreach(QString eachModule, aModuleNames) {
611 Config_ModuleReader::addDependencyModule(eachModule.toStdString());