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);
154 desktop()->tabifyDockWidget(aObjDoc, myWorkshop->propertyPanel());
158 ViewManagerList OCCViewManagers;
159 application()->viewManagers(OCCViewer_Viewer::Type(), OCCViewManagers);
160 if (OCCViewManagers.size() > 0) {
161 mySelector = createSelector(OCCViewManagers.first());
164 // it should be pefromed after the selector creation in order to have AISContext
165 myWorkshop->activateModule();
166 //action(myEraseAll)->setEnabled(false);
169 myWorkshop->objectBrowser()->rebuildDataTree();
170 myWorkshop->updateCommandStatus();
172 // the display all results is not necessary anymore, it was commented in XGUI_Workshop,
173 // so it should be commented here
174 //QTimer::singleShot(1000, myWorkshop, SLOT(displayAllResults()));
177 myWorkshop->updateCommandStatus();
179 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
180 myIsStorePositions = aResMgr->booleanValue("Study", "store_positions", true);
181 myIsEditEnabled = getApp()->isEditEnabled();
182 getApp()->setEditEnabled(false);
184 // this following row is caused by #187 bug.
185 // SALOME saves the dock widget positions before deactivateModule() and
186 // load it after the module activation. So, if the panel is visible before
187 // deactivate, it becomes visible after activate.
188 // In order to avoid the visible property panel, the widget position save is
189 // switch off in this module
190 aResMgr->setValue("Study", "store_positions", false);
192 // Synchronize displayed objects
193 if (mySelector && mySelector->viewer()) {
194 Handle(AIS_InteractiveContext) aContext = mySelector->viewer()->getAISContext();
195 XGUI_Displayer* aDisp = myWorkshop->displayer();
196 QObjectPtrList aObjList = aDisp->displayedObjects();
198 AIS_ListOfInteractive aList;
199 aContext->DisplayedObjects(aList);
200 AIS_ListIteratorOfListOfInteractive aLIt;
201 Handle(AIS_InteractiveObject) anAISIO;
202 foreach (ObjectPtr aObj, aObjList) {
203 AISObjectPtr aPrs = aDisp->getAISObject(aObj);
204 Handle(AIS_InteractiveObject) aAIS = aPrs->impl<Handle(AIS_InteractiveObject)>();
206 for (aLIt.Initialize(aList); aLIt.More(); aLIt.Next()) {
207 anAISIO = aLIt.Value();
208 if (anAISIO.Access() == aAIS.Access()) {
214 aObj->setDisplayed(false);
215 //aDisp->erase(aObj, false);
218 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
220 myProxyViewer->activateViewer(true);
224 //******************************************************
225 bool NewGeom_Module::deactivateModule(SUIT_Study* theStudy)
227 myProxyViewer->activateViewer(false);
231 myWorkshop->deactivateModule();
233 QObject* aObj = myWorkshop->objectBrowser()->parent();
234 QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
236 aObjDoc->setVisible(false);
237 myWorkshop->objectBrowser()->setVisible(false);
238 QAction* aViewAct = aObjDoc->toggleViewAction();
239 aViewAct->setEnabled(false);
242 // the active operation should be stopped for the next activation.
243 // There should not be active operation and visualized preview.
244 // Abort operation should be performed before the selection's remove
245 // because the displayed objects should be removed from the viewer, but
246 // the AIS context is obtained from the selector.
247 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
248 while (anOperation) {
249 anOperation->abort();
250 anOperation = myWorkshop->operationMgr()->currentOperation();
252 // Delete selector because it has to be redefined on next activation
254 myProxyViewer->setSelector(0);
259 //myWorkshop->contextMenuMgr()->disconnectViewer();
261 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
262 aResMgr->setValue("Study", "store_positions", myIsStorePositions);
263 getApp()->setEditEnabled(myIsEditEnabled);
265 return LightApp_Module::deactivateModule(theStudy);
268 //******************************************************
269 void NewGeom_Module::onViewManagerAdded(SUIT_ViewManager* theMgr)
272 mySelector = createSelector(theMgr);
276 //******************************************************
277 void NewGeom_Module::onViewManagerRemoved(SUIT_ViewManager* theMgr)
280 if (theMgr->getType() == OCCViewer_Viewer::Type()) {
281 OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
282 if (mySelector->viewer() == aViewer) {
283 XGUI_Displayer* aDisp = myWorkshop->displayer();
284 QObjectPtrList aObjects = aDisp->displayedObjects();
285 foreach(ObjectPtr aObj, aObjects)
286 aObj->setDisplayed(false);
287 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
288 myProxyViewer->setSelector(0);
296 //******************************************************
297 QtxPopupMgr* NewGeom_Module::popupMgr()
300 myPopupMgr = new QtxPopupMgr( 0, this );
304 //******************************************************
305 void NewGeom_Module::onDefaultPreferences()
307 ModuleBase_Preferences::resetConfig();
308 ModuleBase_Preferences::updateResourcesByConfig();
310 LightApp_Preferences* pref = preferences();
315 //******************************************************
316 void NewGeom_Module::onUpdateCommandStatus()
318 getApp()->updateActions();
321 //******************************************************
322 NewGeom_OCCSelector* NewGeom_Module::createSelector(SUIT_ViewManager* theMgr)
324 if (theMgr->getType() == OCCViewer_Viewer::Type()) {
325 OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
326 NewGeom_OCCSelector* aSelector = new NewGeom_OCCSelector(aViewer, getApp()->selectionMgr());
327 LightApp_SelectionMgr* aMgr = getApp()->selectionMgr();
328 QList<SUIT_Selector*> aList;
329 aMgr->selectors(aList);
330 foreach(SUIT_Selector* aSel, aList)
332 aSel->setEnabled(aSel == aSelector);
334 myProxyViewer->setSelector(aSelector);
340 //******************************************************
341 CAM_DataModel* NewGeom_Module::createDataModel()
343 NewGeom_DataModel* aDataModel = new NewGeom_DataModel(this);
345 // Calling addComponent() for persistent functionality work in the SalomeApp_Study
346 LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>(application()->activeStudy() );
347 aStudy->addComponent(aDataModel);
352 QAction* NewGeom_Module::addFeature(const QString& theWBName, const ActionInfo& theInfo)
354 return addFeature(theWBName,
363 //******************************************************
364 QAction* NewGeom_Module::addFeature(const QString& theWBName, const QString& theId,
365 const QString& theTitle, const QString& theTip,
366 const QIcon& theIcon, const QKeySequence& theKeys,
369 static QString aLastTool = "";
371 if (aLastTool.isEmpty())
372 aLastTool = theWBName;
373 else if (theWBName != aLastTool) {
374 aLastTool = theWBName;
376 desktop()->addToolBarBreak();
382 int aMenu = createMenu(theWBName, -1, -1, 50);
383 int aTool = createTool(theWBName, theWBName);
385 int aId = myActionsList.size();
386 myActionsList.append(theId);
387 SUIT_Desktop* aDesk = application()->desktop();
389 for (unsigned int i = 0; i < theKeys.count(); i++)
391 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
393 aAction->setData(theId);
394 int aItemId = createMenu(aId, aMenu, -1, 10);
395 int aToolId = createTool(aId, aTool);
401 QAction* NewGeom_Module::addNestedFeature(const QString& theWBName,
402 const ActionInfo& theInfo,
403 const QList<QAction*>& theNestedActions)
405 int aMenu = createMenu(theWBName, -1, -1, 50);
406 int aTool = createTool(theWBName, theWBName);
408 int aId = myActionsList.size();
409 myActionsList.append(theInfo.id);
410 SUIT_Desktop* aDesk = application()->desktop();
411 NewGeom_NestedButton* anAction = new NewGeom_NestedButton(aDesk, theNestedActions);
412 anAction->setData(theInfo.id);
413 anAction->setCheckable(theInfo.checkable);
414 anAction->setChecked(theInfo.checked);
415 anAction->setEnabled(theInfo.enabled);
416 anAction->setVisible(theInfo.visible);
417 anAction->setIcon(theInfo.icon);
418 anAction->setText(theInfo.text);
419 anAction->setToolTip(theInfo.toolTip);
420 anAction->setShortcut(theInfo.shortcut);
421 anAction->setFont(theInfo.font);
423 //int aItemId = createMenu(aId, aMenu, -1, 10);
424 int aToolId = createTool(anAction, aTool, aId);
430 //******************************************************
431 QAction* NewGeom_Module::addDesktopCommand(const QString& theId, const QString& theTitle,
432 const QString& theTip, const QIcon& theIcon,
433 const QKeySequence& theKeys, bool isCheckable,
434 const char* theMenuSourceText, const int theMenuPosition)
436 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
438 int aId = myActionsList.size();
439 myActionsList.append(theId);
440 SUIT_Desktop* aDesk = application()->desktop();
442 for (unsigned int i = 0; i < theKeys.count(); i++)
444 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
446 aAction->setData(theId);
447 createMenu(aId, aMenu, theMenuPosition);
451 //******************************************************
452 void NewGeom_Module::addDesktopMenuSeparator(const char* theMenuSourceText, const int theMenuPosition)
454 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
455 createMenu(separator(), aMenu, -1, theMenuPosition);
458 //******************************************************
459 QList<QAction*> NewGeom_Module::commandList() const
461 QList<QAction*> aActions;
462 for (int i = 0; i < myActionsList.size(); i++) {
463 QAction* aCmd = action(i);
464 if (aCmd && myActionsList.contains(aCmd->data().toString()))
465 aActions.append(aCmd);
470 //******************************************************
471 QStringList NewGeom_Module::commandIdList() const
473 return myActionsList;
476 //******************************************************
477 QMainWindow* NewGeom_Module::desktop() const
479 return application()->desktop();
482 //******************************************************
483 QString NewGeom_Module::commandId(const QAction* theCmd) const
485 int aId = actionId(theCmd);
486 if (aId < myActionsList.size())
487 return myActionsList[aId];
491 //******************************************************
492 QAction* NewGeom_Module::command(const QString& theId) const
494 int aId = myActionsList.indexOf(theId);
495 if ((aId != -1) && (aId < myActionsList.size())) {
501 //******************************************************
502 void NewGeom_Module::setNestedActions(const QString& theId, const QStringList& theActions)
504 myNestedActions[theId] = theActions;
507 //******************************************************
508 QStringList NewGeom_Module::nestedActions(const QString& theId) const
510 if (myNestedActions.contains(theId))
511 return myNestedActions[theId];
512 return QStringList();
515 //******************************************************
516 void NewGeom_Module::setDocumentKind(const QString& theId, const QString& theKind)
518 myDocumentType[theId] = theKind;
521 //******************************************************
522 QString NewGeom_Module::documentKind(const QString& theId) const
524 if (myDocumentType.contains(theId))
525 return myDocumentType[theId];
530 //******************************************************
531 void NewGeom_Module::selectionChanged()
533 LightApp_Module::selectionChanged();
534 myWorkshop->salomeViewerSelectionChanged();
537 //******************************************************
538 void NewGeom_Module::contextMenuPopup(const QString& theClient, QMenu* theMenu, QString& theTitle)
540 myWorkshop->contextMenuMgr()->updateViewerMenu();
541 myWorkshop->contextMenuMgr()->addViewerMenu(theMenu);
542 LightApp_Module::contextMenuPopup(theClient, theMenu, theTitle);
546 //******************************************************
547 void NewGeom_Module::createPreferences()
549 LightApp_Preferences* pref = preferences();
552 ModuleBase_Preferences::updateConfigByResources();
553 QString aModName = moduleName();
555 QtxPreferenceItem* item = pref->findItem(aModName, true );
556 if ( item && (!item->isEmpty() )) {
557 item->parentItem()->removeItem(item);
561 int catId = pref->addPreference(aModName, -1 );
564 NewGeom_PrefMgr aMgr(pref, aModName);
565 ModuleBase_Preferences::createEditContent(&aMgr, catId);
569 //******************************************************
570 void NewGeom_Module::preferencesChanged(const QString& theSection, const QString& theParam)
572 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
573 QString aVal = aResMgr->stringValue(theSection, theParam);
574 Config_Prop* aProp = Config_PropManager::findProp(theSection.toStdString(), theParam.toStdString());
575 std::string aValue = aVal.toStdString();
576 if (aValue.empty()) {
577 aValue = aProp->defaultValue();
578 aResMgr->setValue(theSection, theParam, QString(aValue.c_str()));
580 LightApp_Preferences* pref = preferences();
584 aProp->setValue(aValue);
588 void NewGeom_Module::inspectSalomeModules()
590 QStringList aModuleNames;
591 getApp()->modules(aModuleNames, false);
592 foreach(QString eachModule, aModuleNames) {
593 Config_ModuleReader::addDependencyModule(eachModule.toStdString());