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 <QtxResourceMgr.h>
35 #include <Config_PropManager.h>
36 #include <Config_ModuleReader.h>
38 #include <AIS_ListOfInteractive.hxx>
39 #include <AIS_ListIteratorOfListOfInteractive.hxx>
41 #include <QDockWidget>
48 NewGeom_EXPORT CAM_Module* createModule()
50 return new NewGeom_Module();
53 NewGeom_EXPORT char* getModuleVersion()
60 * Class for preferences management
62 class NewGeom_PrefMgr: public ModuleBase_IPrefMgr
66 /// \param theMgr preferences manager of SALOME
67 /// \param theModName name of the module
68 NewGeom_PrefMgr(LightApp_Preferences* theMgr, const QString& theModName):myMgr(theMgr), myModName(theModName) {}
70 virtual int addPreference(const QString& theLbl, int pId,
71 SUIT_PreferenceMgr::PrefItemType theType,
72 const QString& theSection, const QString& theName )
74 return myMgr->addPreference(myModName, theLbl, pId, theType, theSection, theName);
77 virtual void setItemProperty(const QString& thePropName,
78 const QVariant& theValue,
81 myMgr->setItemProperty(thePropName, theValue, theId);
85 virtual SUIT_PreferenceMgr* prefMgr() const { return myMgr; }
88 LightApp_Preferences* myMgr;
95 //******************************************************
96 NewGeom_Module::NewGeom_Module()
97 : LightApp_Module("NewGeom"),
98 mySelector(0), myIsOpened(0), myPopupMgr(0)
100 myWorkshop = new XGUI_Workshop(this);
101 connect(myWorkshop, SIGNAL(commandStatusUpdated()),
102 this, SLOT(onUpdateCommandStatus()));
104 myProxyViewer = new NewGeom_SalomeViewer(this);
106 ModuleBase_Preferences::setResourceMgr(application()->resourceMgr());
107 ModuleBase_Preferences::loadCustomProps();
110 //******************************************************
111 NewGeom_Module::~NewGeom_Module()
115 //******************************************************
116 void NewGeom_Module::initialize(CAM_Application* theApp)
118 LightApp_Module::initialize(theApp);
119 inspectSalomeModules();
121 myWorkshop->startApplication();
122 LightApp_Application* anApp = dynamic_cast<LightApp_Application*>(theApp);
124 connect(anApp, SIGNAL(preferenceResetToDefaults()), this, SLOT(onDefaultPreferences()));
127 //******************************************************
128 void NewGeom_Module::windows(QMap<int, int>& theWndMap) const
130 theWndMap.insert(LightApp_Application::WT_PyConsole, Qt::BottomDockWidgetArea);
133 //******************************************************
134 void NewGeom_Module::viewManagers(QStringList& theList) const
136 theList.append(OCCViewer_Viewer::Type());
139 //******************************************************
140 bool NewGeom_Module::activateModule(SUIT_Study* theStudy)
142 bool isDone = LightApp_Module::activateModule(theStudy);
147 QObject* aObj = myWorkshop->objectBrowser()->parent();
148 QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
150 QAction* aViewAct = aObjDoc->toggleViewAction();
151 aViewAct->setEnabled(true);
152 myWorkshop->objectBrowser()->setVisible(true);
153 aObjDoc->setVisible(true);
157 ViewManagerList OCCViewManagers;
158 application()->viewManagers(OCCViewer_Viewer::Type(), OCCViewManagers);
159 if (OCCViewManagers.size() > 0) {
160 mySelector = createSelector(OCCViewManagers.first());
163 // it should be pefromed after the selector creation in order to have AISContext
164 myWorkshop->activateModule();
165 //action(myEraseAll)->setEnabled(false);
168 myWorkshop->objectBrowser()->rebuildDataTree();
169 myWorkshop->updateCommandStatus();
171 // the display all results is not necessary anymore, it was commented in XGUI_Workshop,
172 // so it should be commented here
173 //QTimer::singleShot(1000, myWorkshop, SLOT(displayAllResults()));
176 myWorkshop->updateCommandStatus();
178 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
179 myIsStorePositions = aResMgr->booleanValue("Study", "store_positions", true);
180 myIsEditEnabled = getApp()->isEditEnabled();
181 getApp()->setEditEnabled(false);
183 // this following row is caused by #187 bug.
184 // SALOME saves the dock widget positions before deactivateModule() and
185 // load it after the module activation. So, if the panel is visible before
186 // deactivate, it becomes visible after activate.
187 // In order to avoid the visible property panel, the widget position save is
188 // switch off in this module
189 aResMgr->setValue("Study", "store_positions", false);
191 // Synchronize displayed objects
192 if (mySelector && mySelector->viewer()) {
193 Handle(AIS_InteractiveContext) aContext = mySelector->viewer()->getAISContext();
194 XGUI_Displayer* aDisp = myWorkshop->displayer();
195 QObjectPtrList aObjList = aDisp->displayedObjects();
197 AIS_ListOfInteractive aList;
198 aContext->DisplayedObjects(aList);
199 AIS_ListIteratorOfListOfInteractive aLIt;
200 Handle(AIS_InteractiveObject) anAISIO;
201 foreach (ObjectPtr aObj, aObjList) {
202 AISObjectPtr aPrs = aDisp->getAISObject(aObj);
203 Handle(AIS_InteractiveObject) aAIS = aPrs->impl<Handle(AIS_InteractiveObject)>();
205 for (aLIt.Initialize(aList); aLIt.More(); aLIt.Next()) {
206 anAISIO = aLIt.Value();
207 if (anAISIO.Access() == aAIS.Access()) {
213 aObj->setDisplayed(false);
214 //aDisp->erase(aObj, false);
217 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
219 myProxyViewer->activateViewer(true);
223 //******************************************************
224 bool NewGeom_Module::deactivateModule(SUIT_Study* theStudy)
226 myProxyViewer->activateViewer(false);
230 myWorkshop->deactivateModule();
232 QObject* aObj = myWorkshop->objectBrowser()->parent();
233 QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
235 aObjDoc->setVisible(false);
236 myWorkshop->objectBrowser()->setVisible(false);
237 QAction* aViewAct = aObjDoc->toggleViewAction();
238 aViewAct->setEnabled(false);
241 // the active operation should be stopped for the next activation.
242 // There should not be active operation and visualized preview.
243 // Abort operation should be performed before the selection's remove
244 // because the displayed objects should be removed from the viewer, but
245 // the AIS context is obtained from the selector.
246 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
247 while (anOperation) {
248 anOperation->abort();
249 anOperation = myWorkshop->operationMgr()->currentOperation();
251 // Delete selector because it has to be redefined on next activation
253 myProxyViewer->setSelector(0);
258 //myWorkshop->contextMenuMgr()->disconnectViewer();
260 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
261 aResMgr->setValue("Study", "store_positions", myIsStorePositions);
262 getApp()->setEditEnabled(myIsEditEnabled);
264 return LightApp_Module::deactivateModule(theStudy);
267 //******************************************************
268 void NewGeom_Module::onViewManagerAdded(SUIT_ViewManager* theMgr)
271 mySelector = createSelector(theMgr);
275 //******************************************************
276 void NewGeom_Module::onViewManagerRemoved(SUIT_ViewManager* theMgr)
279 if (theMgr->getType() == OCCViewer_Viewer::Type()) {
280 OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
281 if (mySelector->viewer() == aViewer) {
282 XGUI_Displayer* aDisp = myWorkshop->displayer();
283 QObjectPtrList aObjects = aDisp->displayedObjects();
284 foreach(ObjectPtr aObj, aObjects)
285 aObj->setDisplayed(false);
286 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
287 myProxyViewer->setSelector(0);
295 //******************************************************
296 QtxPopupMgr* NewGeom_Module::popupMgr()
299 myPopupMgr = new QtxPopupMgr( 0, this );
303 //******************************************************
304 void NewGeom_Module::onDefaultPreferences()
306 ModuleBase_Preferences::resetConfig();
307 ModuleBase_Preferences::updateResourcesByConfig();
309 LightApp_Preferences* pref = preferences();
314 //******************************************************
315 void NewGeom_Module::onUpdateCommandStatus()
317 getApp()->updateActions();
320 //******************************************************
321 NewGeom_OCCSelector* NewGeom_Module::createSelector(SUIT_ViewManager* theMgr)
323 if (theMgr->getType() == OCCViewer_Viewer::Type()) {
324 OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
325 NewGeom_OCCSelector* aSelector = new NewGeom_OCCSelector(aViewer, getApp()->selectionMgr());
326 LightApp_SelectionMgr* aMgr = getApp()->selectionMgr();
327 QList<SUIT_Selector*> aList;
328 aMgr->selectors(aList);
329 foreach(SUIT_Selector* aSel, aList)
331 aSel->setEnabled(aSel == aSelector);
333 myProxyViewer->setSelector(aSelector);
339 //******************************************************
340 CAM_DataModel* NewGeom_Module::createDataModel()
342 NewGeom_DataModel* aDataModel = new NewGeom_DataModel(this);
344 // Calling addComponent() for persistent functionality work in the SalomeApp_Study
345 LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>(application()->activeStudy() );
346 aStudy->addComponent(aDataModel);
351 QAction* NewGeom_Module::addFeature(const QString& theWBName, const ActionInfo& theInfo)
353 return addFeature(theWBName,
362 //******************************************************
363 QAction* NewGeom_Module::addFeature(const QString& theWBName, const QString& theId,
364 const QString& theTitle, const QString& theTip,
365 const QIcon& theIcon, const QKeySequence& theKeys,
368 int aMenu = createMenu(theWBName, -1, -1, 50);
369 int aTool = createTool(theWBName);
371 int aId = myActionsList.size();
372 myActionsList.append(theId);
373 SUIT_Desktop* aDesk = application()->desktop();
375 for (unsigned int i = 0; i < theKeys.count(); i++)
377 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
379 aAction->setData(theId);
380 int aItemId = createMenu(aId, aMenu, -1, 10);
381 int aToolId = createTool(aId, aTool);
387 QAction* NewGeom_Module::addNestedFeature(const QString& theWBName,
388 const ActionInfo& theInfo,
389 const QList<QAction*>& theNestedActions)
391 int aMenu = createMenu(theWBName, -1, -1, 50);
392 int aTool = createTool(theWBName);
394 int aId = myActionsList.size();
395 myActionsList.append(theInfo.id);
396 SUIT_Desktop* aDesk = application()->desktop();
397 NewGeom_NestedButton* anAction = new NewGeom_NestedButton(aDesk, theNestedActions);
398 anAction->setData(theInfo.id);
399 anAction->setCheckable(theInfo.checkable);
400 anAction->setChecked(theInfo.checked);
401 anAction->setEnabled(theInfo.enabled);
402 anAction->setVisible(theInfo.visible);
403 anAction->setIcon(theInfo.icon);
404 anAction->setText(theInfo.text);
405 anAction->setToolTip(theInfo.toolTip);
406 anAction->setShortcut(theInfo.shortcut);
407 anAction->setFont(theInfo.font);
409 //int aItemId = createMenu(aId, aMenu, -1, 10);
410 int aToolId = createTool(anAction, aTool, aId);
416 //******************************************************
417 QAction* NewGeom_Module::addDesktopCommand(const QString& theId, const QString& theTitle,
418 const QString& theTip, const QIcon& theIcon,
419 const QKeySequence& theKeys, bool isCheckable,
420 const char* theMenuSourceText, const int theMenuPosition)
422 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
424 int aId = myActionsList.size();
425 myActionsList.append(theId);
426 SUIT_Desktop* aDesk = application()->desktop();
428 for (unsigned int i = 0; i < theKeys.count(); i++)
430 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
432 aAction->setData(theId);
433 createMenu(aId, aMenu, theMenuPosition);
437 //******************************************************
438 void NewGeom_Module::addDesktopMenuSeparator(const char* theMenuSourceText, const int theMenuPosition)
440 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
441 createMenu(separator(), aMenu, -1, theMenuPosition);
444 //******************************************************
445 QList<QAction*> NewGeom_Module::commandList() const
447 QList<QAction*> aActions;
448 for (int i = 0; i < myActionsList.size(); i++) {
449 QAction* aCmd = action(i);
450 if (aCmd && myActionsList.contains(aCmd->data().toString()))
451 aActions.append(aCmd);
456 //******************************************************
457 QStringList NewGeom_Module::commandIdList() const
459 return myActionsList;
462 //******************************************************
463 QMainWindow* NewGeom_Module::desktop() const
465 return application()->desktop();
468 //******************************************************
469 QString NewGeom_Module::commandId(const QAction* theCmd) const
471 int aId = actionId(theCmd);
472 if (aId < myActionsList.size())
473 return myActionsList[aId];
477 //******************************************************
478 QAction* NewGeom_Module::command(const QString& theId) const
480 int aId = myActionsList.indexOf(theId);
481 if ((aId != -1) && (aId < myActionsList.size())) {
487 //******************************************************
488 void NewGeom_Module::setNestedActions(const QString& theId, const QStringList& theActions)
490 myNestedActions[theId] = theActions;
493 //******************************************************
494 QStringList NewGeom_Module::nestedActions(const QString& theId) const
496 if (myNestedActions.contains(theId))
497 return myNestedActions[theId];
498 return QStringList();
501 //******************************************************
502 void NewGeom_Module::setDocumentKind(const QString& theId, const QString& theKind)
504 myDocumentType[theId] = theKind;
507 //******************************************************
508 QString NewGeom_Module::documentKind(const QString& theId) const
510 if (myDocumentType.contains(theId))
511 return myDocumentType[theId];
516 //******************************************************
517 void NewGeom_Module::selectionChanged()
519 LightApp_Module::selectionChanged();
520 myWorkshop->salomeViewerSelectionChanged();
523 //******************************************************
524 void NewGeom_Module::contextMenuPopup(const QString& theClient, QMenu* theMenu, QString& theTitle)
526 QMenu* aMenu = myWorkshop->contextMenuMgr()->viewerMenu();
527 theMenu->addActions(aMenu->actions());
528 //myWorkshop->contextMenuMgr()->addViewerMenu(theMenu);
529 LightApp_Module::contextMenuPopup(theClient, theMenu, theTitle);
530 delete aMenu; // Delete menu because it is not controlled by menu manager
534 //******************************************************
535 void NewGeom_Module::createPreferences()
537 LightApp_Preferences* pref = preferences();
540 ModuleBase_Preferences::updateConfigByResources();
541 QString aModName = moduleName();
543 QtxPreferenceItem* item = pref->findItem(aModName, true );
544 if ( item && (!item->isEmpty() )) {
545 item->parentItem()->removeItem(item);
549 int catId = pref->addPreference(aModName, -1 );
552 NewGeom_PrefMgr aMgr(pref, aModName);
553 ModuleBase_Preferences::createEditContent(&aMgr, catId);
557 //******************************************************
558 void NewGeom_Module::preferencesChanged(const QString& theSection, const QString& theParam)
560 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
561 QString aVal = aResMgr->stringValue(theSection, theParam);
562 Config_Prop* aProp = Config_PropManager::findProp(theSection.toStdString(), theParam.toStdString());
563 std::string aValue = aVal.toStdString();
564 if (aValue.empty()) {
565 aValue = aProp->defaultValue();
566 aResMgr->setValue(theSection, theParam, QString(aValue.c_str()));
568 LightApp_Preferences* pref = preferences();
572 aProp->setValue(aValue);
576 void NewGeom_Module::inspectSalomeModules()
578 QStringList aModuleNames;
579 getApp()->modules(aModuleNames, false);
580 foreach(QString eachModule, aModuleNames) {
581 Config_ModuleReader::addDependencyModule(eachModule.toStdString());