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>
47 NewGeom_EXPORT CAM_Module* createModule()
49 return new NewGeom_Module();
52 NewGeom_EXPORT char* getModuleVersion()
59 * Class for preferences management
61 class NewGeom_PrefMgr: public ModuleBase_IPrefMgr
65 /// \param theMgr preferences manager of SALOME
66 /// \param theModName name of the module
67 NewGeom_PrefMgr(LightApp_Preferences* theMgr, const QString& theModName):myMgr(theMgr), myModName(theModName) {}
69 virtual int addPreference(const QString& theLbl, int pId,
70 SUIT_PreferenceMgr::PrefItemType theType,
71 const QString& theSection, const QString& theName )
73 return myMgr->addPreference(myModName, theLbl, pId, theType, theSection, theName);
76 virtual void setItemProperty(const QString& thePropName,
77 const QVariant& theValue,
80 myMgr->setItemProperty(thePropName, theValue, theId);
84 virtual SUIT_PreferenceMgr* prefMgr() const { return myMgr; }
87 LightApp_Preferences* myMgr;
94 //******************************************************
95 NewGeom_Module::NewGeom_Module()
96 : LightApp_Module("NewGeom"),
97 mySelector(0), myIsOpened(0), myPopupMgr(0)
99 myWorkshop = new XGUI_Workshop(this);
100 connect(myWorkshop, SIGNAL(commandStatusUpdated()),
101 this, SLOT(onUpdateCommandStatus()));
103 myProxyViewer = new NewGeom_SalomeViewer(this);
105 ModuleBase_Preferences::setResourceMgr(application()->resourceMgr());
106 ModuleBase_Preferences::loadCustomProps();
109 //******************************************************
110 NewGeom_Module::~NewGeom_Module()
114 //******************************************************
115 void NewGeom_Module::initialize(CAM_Application* theApp)
117 LightApp_Module::initialize(theApp);
118 inspectSalomeModules();
120 myWorkshop->startApplication();
121 LightApp_Application* anApp = dynamic_cast<LightApp_Application*>(theApp);
123 connect(anApp, SIGNAL(preferenceResetToDefaults()), this, SLOT(onDefaultPreferences()));
126 //******************************************************
127 void NewGeom_Module::windows(QMap<int, int>& theWndMap) const
129 theWndMap.insert(LightApp_Application::WT_PyConsole, Qt::BottomDockWidgetArea);
132 //******************************************************
133 void NewGeom_Module::viewManagers(QStringList& theList) const
135 theList.append(OCCViewer_Viewer::Type());
138 //******************************************************
139 bool NewGeom_Module::activateModule(SUIT_Study* theStudy)
141 bool isDone = LightApp_Module::activateModule(theStudy);
146 QObject* aObj = myWorkshop->objectBrowser()->parent();
147 QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
149 QAction* aViewAct = aObjDoc->toggleViewAction();
150 aViewAct->setEnabled(true);
151 myWorkshop->objectBrowser()->setVisible(true);
152 aObjDoc->setVisible(true);
156 ViewManagerList OCCViewManagers;
157 application()->viewManagers(OCCViewer_Viewer::Type(), OCCViewManagers);
158 if (OCCViewManagers.size() > 0) {
159 mySelector = createSelector(OCCViewManagers.first());
162 // it should be pefromed after the selector creation in order to have AISContext
163 myWorkshop->activateModule();
164 //action(myEraseAll)->setEnabled(false);
167 myWorkshop->objectBrowser()->rebuildDataTree();
168 myWorkshop->updateCommandStatus();
170 // the display all results is not necessary anymore, it was commented in XGUI_Workshop,
171 // so it should be commented here
172 //QTimer::singleShot(1000, myWorkshop, SLOT(displayAllResults()));
175 myWorkshop->updateCommandStatus();
177 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
178 myIsStorePositions = aResMgr->booleanValue("Study", "store_positions", true);
179 myIsEditEnabled = getApp()->isEditEnabled();
180 getApp()->setEditEnabled(false);
182 // this following row is caused by #187 bug.
183 // SALOME saves the dock widget positions before deactivateModule() and
184 // load it after the module activation. So, if the panel is visible before
185 // deactivate, it becomes visible after activate.
186 // In order to avoid the visible property panel, the widget position save is
187 // switch off in this module
188 aResMgr->setValue("Study", "store_positions", false);
190 // Synchronize displayed objects
191 if (mySelector && mySelector->viewer()) {
192 Handle(AIS_InteractiveContext) aContext = mySelector->viewer()->getAISContext();
193 XGUI_Displayer* aDisp = myWorkshop->displayer();
194 QObjectPtrList aObjList = aDisp->displayedObjects();
196 AIS_ListOfInteractive aList;
197 aContext->DisplayedObjects(aList);
198 AIS_ListIteratorOfListOfInteractive aLIt;
199 Handle(AIS_InteractiveObject) anAISIO;
200 foreach (ObjectPtr aObj, aObjList) {
201 AISObjectPtr aPrs = aDisp->getAISObject(aObj);
202 Handle(AIS_InteractiveObject) aAIS = aPrs->impl<Handle(AIS_InteractiveObject)>();
204 for (aLIt.Initialize(aList); aLIt.More(); aLIt.Next()) {
205 anAISIO = aLIt.Value();
206 if (anAISIO.Access() == aAIS.Access()) {
212 aObj->setDisplayed(false);
213 //aDisp->erase(aObj, false);
216 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
222 //******************************************************
223 bool NewGeom_Module::deactivateModule(SUIT_Study* theStudy)
228 myWorkshop->deactivateModule();
230 QObject* aObj = myWorkshop->objectBrowser()->parent();
231 QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
233 aObjDoc->setVisible(false);
234 myWorkshop->objectBrowser()->setVisible(false);
235 QAction* aViewAct = aObjDoc->toggleViewAction();
236 aViewAct->setEnabled(false);
239 // the active operation should be stopped for the next activation.
240 // There should not be active operation and visualized preview.
241 // Abort operation should be performed before the selection's remove
242 // because the displayed objects should be removed from the viewer, but
243 // the AIS context is obtained from the selector.
244 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
245 while (anOperation) {
246 anOperation->abort();
247 anOperation = myWorkshop->operationMgr()->currentOperation();
249 // Delete selector because it has to be redefined on next activation
251 myProxyViewer->setSelector(0);
256 //myWorkshop->contextMenuMgr()->disconnectViewer();
258 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
259 aResMgr->setValue("Study", "store_positions", myIsStorePositions);
260 getApp()->setEditEnabled(myIsEditEnabled);
262 return LightApp_Module::deactivateModule(theStudy);
265 //******************************************************
266 void NewGeom_Module::onViewManagerAdded(SUIT_ViewManager* theMgr)
269 mySelector = createSelector(theMgr);
273 //******************************************************
274 void NewGeom_Module::onViewManagerRemoved(SUIT_ViewManager* theMgr)
277 if (theMgr->getType() == OCCViewer_Viewer::Type()) {
278 OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
279 if (mySelector->viewer() == aViewer) {
280 XGUI_Displayer* aDisp = myWorkshop->displayer();
281 QObjectPtrList aObjects = aDisp->displayedObjects();
282 foreach(ObjectPtr aObj, aObjects)
283 aObj->setDisplayed(false);
284 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
285 myProxyViewer->setSelector(0);
293 //******************************************************
294 QtxPopupMgr* NewGeom_Module::popupMgr()
297 myPopupMgr = new QtxPopupMgr( 0, this );
301 //******************************************************
302 void NewGeom_Module::onDefaultPreferences()
304 ModuleBase_Preferences::resetConfig();
305 ModuleBase_Preferences::updateResourcesByConfig();
307 LightApp_Preferences* pref = preferences();
312 //******************************************************
313 void NewGeom_Module::onUpdateCommandStatus()
315 getApp()->updateActions();
318 //******************************************************
319 NewGeom_OCCSelector* NewGeom_Module::createSelector(SUIT_ViewManager* theMgr)
321 if (theMgr->getType() == OCCViewer_Viewer::Type()) {
322 OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
323 NewGeom_OCCSelector* aSelector = new NewGeom_OCCSelector(aViewer, getApp()->selectionMgr());
324 LightApp_SelectionMgr* aMgr = getApp()->selectionMgr();
325 QList<SUIT_Selector*> aList;
326 aMgr->selectors(aList);
327 foreach(SUIT_Selector* aSel, aList)
329 aSel->setEnabled(aSel == aSelector);
331 myProxyViewer->setSelector(aSelector);
337 //******************************************************
338 CAM_DataModel* NewGeom_Module::createDataModel()
340 NewGeom_DataModel* aDataModel = new NewGeom_DataModel(this);
342 // Calling addComponent() for persistent functionality work in the SalomeApp_Study
343 LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>(application()->activeStudy() );
344 aStudy->addComponent(aDataModel);
349 QAction* NewGeom_Module::addFeature(const QString& theWBName, const ActionInfo& theInfo)
351 return addFeature(theWBName,
360 //******************************************************
361 QAction* NewGeom_Module::addFeature(const QString& theWBName, const QString& theId,
362 const QString& theTitle, const QString& theTip,
363 const QIcon& theIcon, const QKeySequence& theKeys,
366 int aMenu = createMenu(theWBName, -1, -1, 50);
367 int aTool = createTool(theWBName);
369 int aId = myActionsList.size();
370 myActionsList.append(theId);
371 SUIT_Desktop* aDesk = application()->desktop();
373 for (unsigned int i = 0; i < theKeys.count(); i++)
375 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
377 aAction->setData(theId);
378 int aItemId = createMenu(aId, aMenu, -1, 10);
379 int aToolId = createTool(aId, aTool);
385 QAction* NewGeom_Module::addNestedFeature(const QString& theWBName,
386 const ActionInfo& theInfo,
387 const QList<QAction*>& theNestedActions)
389 int aMenu = createMenu(theWBName, -1, -1, 50);
390 int aTool = createTool(theWBName);
392 int aId = myActionsList.size();
393 myActionsList.append(theInfo.id);
394 SUIT_Desktop* aDesk = application()->desktop();
395 NewGeom_NestedButton* anAction = new NewGeom_NestedButton(aDesk, theNestedActions);
396 anAction->setData(theInfo.id);
397 anAction->setCheckable(theInfo.checkable);
398 anAction->setChecked(theInfo.checked);
399 anAction->setEnabled(theInfo.enabled);
400 anAction->setVisible(theInfo.visible);
401 anAction->setIcon(theInfo.icon);
402 anAction->setText(theInfo.text);
403 anAction->setToolTip(theInfo.toolTip);
404 anAction->setShortcut(theInfo.shortcut);
405 anAction->setFont(theInfo.font);
407 //int aItemId = createMenu(aId, aMenu, -1, 10);
408 int aToolId = createTool(anAction, aTool, aId);
414 //******************************************************
415 QAction* NewGeom_Module::addDesktopCommand(const QString& theId, const QString& theTitle,
416 const QString& theTip, const QIcon& theIcon,
417 const QKeySequence& theKeys, bool isCheckable,
418 const char* theMenuSourceText, const int theMenuPosition)
420 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
422 int aId = myActionsList.size();
423 myActionsList.append(theId);
424 SUIT_Desktop* aDesk = application()->desktop();
426 for (unsigned int i = 0; i < theKeys.count(); i++)
428 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
430 aAction->setData(theId);
431 createMenu(aId, aMenu, theMenuPosition);
435 //******************************************************
436 void NewGeom_Module::addDesktopMenuSeparator(const char* theMenuSourceText, const int theMenuPosition)
438 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
439 createMenu(separator(), aMenu, -1, theMenuPosition);
442 //******************************************************
443 QList<QAction*> NewGeom_Module::commandList() const
445 QList<QAction*> aActions;
446 for (int i = 0; i < myActionsList.size(); i++) {
447 QAction* aCmd = action(i);
448 if (aCmd && myActionsList.contains(aCmd->data().toString()))
449 aActions.append(aCmd);
454 //******************************************************
455 QStringList NewGeom_Module::commandIdList() const
457 return myActionsList;
460 //******************************************************
461 QMainWindow* NewGeom_Module::desktop() const
463 return application()->desktop();
466 //******************************************************
467 QString NewGeom_Module::commandId(const QAction* theCmd) const
469 int aId = actionId(theCmd);
470 if (aId < myActionsList.size())
471 return myActionsList[aId];
475 //******************************************************
476 QAction* NewGeom_Module::command(const QString& theId) const
478 int aId = myActionsList.indexOf(theId);
479 if ((aId != -1) && (aId < myActionsList.size())) {
485 //******************************************************
486 void NewGeom_Module::setNestedActions(const QString& theId, const QStringList& theActions)
488 myNestedActions[theId] = theActions;
491 //******************************************************
492 QStringList NewGeom_Module::nestedActions(const QString& theId) const
494 if (myNestedActions.contains(theId))
495 return myNestedActions[theId];
496 return QStringList();
499 //******************************************************
500 void NewGeom_Module::setDocumentKind(const QString& theId, const QString& theKind)
502 myDocumentType[theId] = theKind;
505 //******************************************************
506 QString NewGeom_Module::documentKind(const QString& theId) const
508 if (myDocumentType.contains(theId))
509 return myDocumentType[theId];
514 //******************************************************
515 void NewGeom_Module::selectionChanged()
517 LightApp_Module::selectionChanged();
518 myWorkshop->salomeViewerSelectionChanged();
521 //******************************************************
522 void NewGeom_Module::contextMenuPopup(const QString& theClient, QMenu* theMenu, QString& theTitle)
524 myWorkshop->contextMenuMgr()->addViewerMenu(theMenu);
525 LightApp_Module::contextMenuPopup(theClient, theMenu, theTitle);
529 //******************************************************
530 void NewGeom_Module::createPreferences()
532 LightApp_Preferences* pref = preferences();
535 ModuleBase_Preferences::updateConfigByResources();
536 QString aModName = moduleName();
538 QtxPreferenceItem* item = pref->findItem(aModName, true );
539 if ( item && (!item->isEmpty() )) {
540 item->parentItem()->removeItem(item);
544 int catId = pref->addPreference(aModName, -1 );
547 NewGeom_PrefMgr aMgr(pref, aModName);
548 ModuleBase_Preferences::createEditContent(&aMgr, catId);
552 //******************************************************
553 void NewGeom_Module::preferencesChanged(const QString& theSection, const QString& theParam)
555 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
556 QString aVal = aResMgr->stringValue(theSection, theParam);
557 Config_Prop* aProp = Config_PropManager::findProp(theSection.toStdString(), theParam.toStdString());
558 std::string aValue = aVal.toStdString();
559 if (aValue.empty()) {
560 aValue = aProp->defaultValue();
561 aResMgr->setValue(theSection, theParam, QString(aValue.c_str()));
563 LightApp_Preferences* pref = preferences();
567 aProp->setValue(aValue);
571 void NewGeom_Module::inspectSalomeModules()
573 QStringList aModuleNames;
574 getApp()->modules(aModuleNames, false);
575 foreach(QString eachModule, aModuleNames) {
576 Config_ModuleReader::addDependencyModule(eachModule.toStdString());