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);
275 myWorkshop->module()->activateSelectionFilters();
279 //******************************************************
280 void NewGeom_Module::onViewManagerRemoved(SUIT_ViewManager* theMgr)
283 if (theMgr->getType() == OCCViewer_Viewer::Type()) {
284 OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
285 if (mySelector->viewer() == aViewer) {
286 XGUI_Displayer* aDisp = myWorkshop->displayer();
287 QObjectPtrList aObjects = aDisp->displayedObjects();
288 foreach(ObjectPtr aObj, aObjects)
289 aObj->setDisplayed(false);
290 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
291 myProxyViewer->setSelector(0);
295 myWorkshop->module()->clearViewer();
301 //******************************************************
302 QtxPopupMgr* NewGeom_Module::popupMgr()
305 myPopupMgr = new QtxPopupMgr( 0, this );
309 //******************************************************
310 void NewGeom_Module::onDefaultPreferences()
312 ModuleBase_Preferences::resetConfig();
313 ModuleBase_Preferences::updateResourcesByConfig();
315 LightApp_Preferences* pref = preferences();
319 myWorkshop->displayer()->redisplayObjects();
322 //******************************************************
323 void NewGeom_Module::onUpdateCommandStatus()
325 getApp()->updateActions();
328 //******************************************************
329 NewGeom_OCCSelector* NewGeom_Module::createSelector(SUIT_ViewManager* theMgr)
331 if (theMgr->getType() == OCCViewer_Viewer::Type()) {
332 OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
333 NewGeom_OCCSelector* aSelector = new NewGeom_OCCSelector(aViewer, getApp()->selectionMgr());
334 LightApp_SelectionMgr* aMgr = getApp()->selectionMgr();
335 QList<SUIT_Selector*> aList;
336 aMgr->selectors(aList);
337 foreach(SUIT_Selector* aSel, aList)
339 aSel->setEnabled(aSel == aSelector);
341 myProxyViewer->setSelector(aSelector);
347 //******************************************************
348 CAM_DataModel* NewGeom_Module::createDataModel()
350 NewGeom_DataModel* aDataModel = new NewGeom_DataModel(this);
352 // Calling addComponent() for persistent functionality work in the SalomeApp_Study
353 LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>(application()->activeStudy() );
354 aStudy->addComponent(aDataModel);
359 QAction* NewGeom_Module::addFeature(const QString& theWBName, const ActionInfo& theInfo)
361 return addFeature(theWBName,
370 //******************************************************
371 QAction* NewGeom_Module::addFeature(const QString& theWBName, const QString& theId,
372 const QString& theTitle, const QString& theTip,
373 const QIcon& theIcon, const QKeySequence& theKeys,
376 static QString aLastTool = "";
378 if (aLastTool.isEmpty())
379 aLastTool = theWBName;
380 else if (theWBName != aLastTool) {
381 aLastTool = theWBName;
383 desktop()->addToolBarBreak();
389 int aMenu = createMenu(theWBName, -1, -1, 50);
390 int aTool = createTool(theWBName, theWBName);
392 int aId = myActionsList.size();
393 myActionsList.append(theId);
394 SUIT_Desktop* aDesk = application()->desktop();
396 for (unsigned int i = 0; i < theKeys.count(); i++)
398 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
400 aAction->setData(theId);
401 int aItemId = createMenu(aId, aMenu, -1, 10);
402 int aToolId = createTool(aId, aTool);
407 bool NewGeom_Module::isNestedFeature(const QAction* theAction)
409 return dynamic_cast<const NewGeom_NestedButton*>(theAction);
412 QAction* NewGeom_Module::addNestedFeature(const QString& theWBName,
413 const ActionInfo& theInfo,
414 const QList<QAction*>& theNestedActions)
416 int aMenu = createMenu(theWBName, -1, -1, 50);
417 int aTool = createTool(theWBName, theWBName);
419 int aId = myActionsList.size();
420 myActionsList.append(theInfo.id);
421 SUIT_Desktop* aDesk = application()->desktop();
422 NewGeom_NestedButton* anAction = new NewGeom_NestedButton(aDesk, theNestedActions);
423 anAction->setData(theInfo.id);
424 anAction->setCheckable(theInfo.checkable);
425 anAction->setChecked(theInfo.checked);
426 anAction->setEnabled(theInfo.enabled);
427 anAction->setVisible(theInfo.visible);
428 anAction->setIcon(theInfo.icon);
429 anAction->setText(theInfo.text);
430 anAction->setToolTip(theInfo.toolTip);
431 anAction->setShortcut(theInfo.shortcut);
432 anAction->setFont(theInfo.font);
434 //int aItemId = createMenu(aId, aMenu, -1, 10);
435 int aToolId = createTool(anAction, aTool, aId);
441 //******************************************************
442 QAction* NewGeom_Module::addDesktopCommand(const QString& theId, const QString& theTitle,
443 const QString& theTip, const QIcon& theIcon,
444 const QKeySequence& theKeys, bool isCheckable,
445 const char* theMenuSourceText, const int theMenuPosition)
447 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
449 int aId = myActionsList.size();
450 myActionsList.append(theId);
451 SUIT_Desktop* aDesk = application()->desktop();
453 for (unsigned int i = 0; i < theKeys.count(); i++)
455 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
457 aAction->setData(theId);
458 createMenu(aId, aMenu, theMenuPosition);
462 //******************************************************
463 void NewGeom_Module::addDesktopMenuSeparator(const char* theMenuSourceText, const int theMenuPosition)
465 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
466 createMenu(separator(), aMenu, -1, theMenuPosition);
469 bool NewGeom_Module::addActionInToolbar( QAction* theAction, const QString& theToolBarTitle )
474 SUIT_Desktop* aDesktop = application()->desktop();
478 QtxActionToolMgr* aToolMgr = aDesktop->toolMgr();
482 aToolMgr->append( theAction, theToolBarTitle );
486 //******************************************************
487 QList<QAction*> NewGeom_Module::commandList() const
489 QList<QAction*> aActions;
490 for (int i = 0; i < myActionsList.size(); i++) {
491 QAction* aCmd = action(i);
492 if (aCmd && myActionsList.contains(aCmd->data().toString()))
493 aActions.append(aCmd);
498 //******************************************************
499 QStringList NewGeom_Module::commandIdList() const
501 return myActionsList;
504 //******************************************************
505 QMainWindow* NewGeom_Module::desktop() const
507 return application()->desktop();
510 //******************************************************
511 QString NewGeom_Module::commandId(const QAction* theCmd) const
513 int aId = actionId(theCmd);
514 if (aId < myActionsList.size())
515 return myActionsList[aId];
519 //******************************************************
520 QAction* NewGeom_Module::command(const QString& theId) const
522 int aId = myActionsList.indexOf(theId);
523 if ((aId != -1) && (aId < myActionsList.size())) {
529 //******************************************************
530 void NewGeom_Module::setNestedActions(const QString& theId, const QStringList& theActions)
532 myNestedActions[theId] = theActions;
535 //******************************************************
536 QStringList NewGeom_Module::nestedActions(const QString& theId) const
538 if (myNestedActions.contains(theId))
539 return myNestedActions[theId];
540 return QStringList();
543 //******************************************************
544 void NewGeom_Module::setDocumentKind(const QString& theId, const QString& theKind)
546 myDocumentType[theId] = theKind;
549 //******************************************************
550 QString NewGeom_Module::documentKind(const QString& theId) const
552 if (myDocumentType.contains(theId))
553 return myDocumentType[theId];
558 //******************************************************
559 void NewGeom_Module::selectionChanged()
561 LightApp_Module::selectionChanged();
562 myWorkshop->salomeViewerSelectionChanged();
565 //******************************************************
566 void NewGeom_Module::contextMenuPopup(const QString& theClient, QMenu* theMenu, QString& theTitle)
568 myWorkshop->contextMenuMgr()->updateViewerMenu();
569 myWorkshop->contextMenuMgr()->addViewerMenu(theMenu);
570 LightApp_Module::contextMenuPopup(theClient, theMenu, theTitle);
574 //******************************************************
575 void NewGeom_Module::createPreferences()
577 LightApp_Preferences* pref = preferences();
580 ModuleBase_Preferences::updateConfigByResources();
581 QString aModName = moduleName();
583 QtxPreferenceItem* item = pref->findItem(aModName, true );
584 if ( item && (!item->isEmpty() )) {
585 item->parentItem()->removeItem(item);
589 int catId = pref->addPreference(aModName, -1 );
592 NewGeom_PrefMgr aMgr(pref, aModName);
593 ModuleBase_Preferences::createEditContent(&aMgr, catId);
597 //******************************************************
598 void NewGeom_Module::preferencesChanged(const QString& theSection, const QString& theParam)
600 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
601 QString aVal = aResMgr->stringValue(theSection, theParam);
602 Config_Prop* aProp = Config_PropManager::findProp(theSection.toStdString(), theParam.toStdString());
603 std::string aValue = aVal.toStdString();
604 if (aValue.empty()) {
605 aValue = aProp->defaultValue();
606 aResMgr->setValue(theSection, theParam, QString(aValue.c_str()));
608 LightApp_Preferences* pref = preferences();
612 aProp->setValue(aValue);
614 myWorkshop->displayer()->redisplayObjects();
617 void NewGeom_Module::inspectSalomeModules()
619 QStringList aModuleNames;
620 getApp()->modules(aModuleNames, false);
621 foreach(QString eachModule, aModuleNames) {
622 Config_ModuleReader::addDependencyModule(eachModule.toStdString());