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 //action(myEraseAll)->setEnabled(false);
165 myWorkshop->objectBrowser()->rebuildDataTree();
166 myWorkshop->updateCommandStatus();
168 QTimer::singleShot(1000, myWorkshop, SLOT(displayAllResults()));
171 myWorkshop->updateCommandStatus();
173 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
174 myIsStorePositions = aResMgr->booleanValue("Study", "store_positions", true);
175 myIsEditEnabled = getApp()->isEditEnabled();
176 getApp()->setEditEnabled(false);
178 // this following row is caused by #187 bug.
179 // SALOME saves the dock widget positions before deactivateModule() and
180 // load it after the module activation. So, if the panel is visible before
181 // deactivate, it becomes visible after activate.
182 // In order to avoid the visible property panel, the widget position save is
183 // switch off in this module
184 aResMgr->setValue("Study", "store_positions", false);
186 // Synchronize displayed objects
187 if (mySelector && mySelector->viewer()) {
188 Handle(AIS_InteractiveContext) aContext = mySelector->viewer()->getAISContext();
189 XGUI_Displayer* aDisp = myWorkshop->displayer();
190 QObjectPtrList aObjList = aDisp->displayedObjects();
192 AIS_ListOfInteractive aList;
193 aContext->DisplayedObjects(aList);
194 AIS_ListIteratorOfListOfInteractive aLIt;
195 Handle(AIS_InteractiveObject) anAISIO;
196 foreach (ObjectPtr aObj, aObjList) {
197 AISObjectPtr aPrs = aDisp->getAISObject(aObj);
198 Handle(AIS_InteractiveObject) aAIS = aPrs->impl<Handle(AIS_InteractiveObject)>();
200 for (aLIt.Initialize(aList); aLIt.More(); aLIt.Next()) {
201 anAISIO = aLIt.Value();
202 if (anAISIO.Access() == aAIS.Access()) {
208 aDisp->erase(aObj, false);
216 //******************************************************
217 bool NewGeom_Module::deactivateModule(SUIT_Study* theStudy)
222 QObject* aObj = myWorkshop->objectBrowser()->parent();
223 QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
225 aObjDoc->setVisible(false);
226 myWorkshop->objectBrowser()->setVisible(false);
227 QAction* aViewAct = aObjDoc->toggleViewAction();
228 aViewAct->setEnabled(false);
231 // the active operation should be stopped for the next activation.
232 // There should not be active operation and visualized preview.
233 // Abort operation should be performed before the selection's remove
234 // because the displayed objects should be removed from the viewer, but
235 // the AIS context is obtained from the selector.
236 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
237 while (anOperation) {
238 anOperation->abort();
239 anOperation = myWorkshop->operationMgr()->currentOperation();
241 // Delete selector because it has to be redefined on next activation
243 myProxyViewer->setSelector(0);
248 //myWorkshop->contextMenuMgr()->disconnectViewer();
250 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
251 aResMgr->setValue("Study", "store_positions", myIsStorePositions);
252 getApp()->setEditEnabled(myIsEditEnabled);
254 return LightApp_Module::deactivateModule(theStudy);
257 //******************************************************
258 void NewGeom_Module::onViewManagerAdded(SUIT_ViewManager* theMgr)
261 mySelector = createSelector(theMgr);
265 //******************************************************
266 void NewGeom_Module::onViewManagerRemoved(SUIT_ViewManager* theMgr)
269 if (theMgr->getType() == OCCViewer_Viewer::Type()) {
270 OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
271 if (mySelector->viewer() == aViewer) {
272 myWorkshop->displayer()->eraseAll(false);
273 myProxyViewer->setSelector(0);
281 //******************************************************
282 QtxPopupMgr* NewGeom_Module::popupMgr()
285 myPopupMgr = new QtxPopupMgr( 0, this );
289 //******************************************************
290 void NewGeom_Module::onDefaultPreferences()
292 ModuleBase_Preferences::resetConfig();
293 ModuleBase_Preferences::updateResourcesByConfig();
295 LightApp_Preferences* pref = preferences();
300 //******************************************************
301 void NewGeom_Module::onUpdateCommandStatus()
303 getApp()->updateActions();
306 //******************************************************
307 NewGeom_OCCSelector* NewGeom_Module::createSelector(SUIT_ViewManager* theMgr)
309 if (theMgr->getType() == OCCViewer_Viewer::Type()) {
310 OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
311 NewGeom_OCCSelector* aSelector = new NewGeom_OCCSelector(aViewer, getApp()->selectionMgr());
312 LightApp_SelectionMgr* aMgr = getApp()->selectionMgr();
313 QList<SUIT_Selector*> aList;
314 aMgr->selectors(aList);
315 foreach(SUIT_Selector* aSel, aList)
317 aSel->setEnabled(aSel == aSelector);
319 myProxyViewer->setSelector(aSelector);
325 //******************************************************
326 CAM_DataModel* NewGeom_Module::createDataModel()
328 NewGeom_DataModel* aDataModel = new NewGeom_DataModel(this);
330 // Calling addComponent() for persistent functionality work in the SalomeApp_Study
331 LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>(application()->activeStudy() );
332 aStudy->addComponent(aDataModel);
337 QAction* NewGeom_Module::addFeature(const QString& theWBName, const ActionInfo& theInfo)
339 return addFeature(theWBName,
347 //******************************************************
348 QAction* NewGeom_Module::addFeature(const QString& theWBName, const QString& theId,
349 const QString& theTitle, const QString& theTip,
350 const QIcon& theIcon, const QKeySequence& theKeys,
353 int aMenu = createMenu(theWBName, -1, -1, 50);
354 int aTool = createTool(theWBName);
356 int aId = myActionsList.size();
357 myActionsList.append(theId);
358 SUIT_Desktop* aDesk = application()->desktop();
360 for (unsigned int i = 0; i < theKeys.count(); i++)
362 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
364 aAction->setData(theId);
365 int aItemId = createMenu(aId, aMenu, -1, 10);
366 int aToolId = createTool(aId, aTool);
372 QAction* NewGeom_Module::addNestedFeature(const QString& theWBName,
373 const ActionInfo& theInfo,
374 const QList<QAction*>& theNestedActions)
376 int aMenu = createMenu(theWBName, -1, -1, 50);
377 int aTool = createTool(theWBName);
379 int aId = myActionsList.size();
380 myActionsList.append(theInfo.id);
381 SUIT_Desktop* aDesk = application()->desktop();
382 NewGeom_NestedButton* anAction = new NewGeom_NestedButton(aDesk, theNestedActions);
383 anAction->setData(theInfo.id);
384 anAction->setCheckable(theInfo.checkable);
385 anAction->setChecked(theInfo.checked);
386 anAction->setEnabled(theInfo.enabled);
387 anAction->setVisible(theInfo.visible);
388 anAction->setIcon(theInfo.icon);
389 anAction->setText(theInfo.text);
390 anAction->setToolTip(theInfo.toolTip);
391 anAction->setShortcut(theInfo.shortcut);
392 anAction->setFont(theInfo.font);
394 //int aItemId = createMenu(aId, aMenu, -1, 10);
395 int aToolId = createTool(anAction, aTool, aId);
401 //******************************************************
402 QAction* NewGeom_Module::addDesktopCommand(const QString& theId, const QString& theTitle,
403 const QString& theTip, const QIcon& theIcon,
404 const QKeySequence& theKeys, bool isCheckable,
405 const char* theMenuSourceText, const int theMenuPosition)
407 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
409 int aId = myActionsList.size();
410 myActionsList.append(theId);
411 SUIT_Desktop* aDesk = application()->desktop();
413 for (unsigned int i = 0; i < theKeys.count(); i++)
415 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
417 aAction->setData(theId);
418 createMenu(aId, aMenu, theMenuPosition);
422 //******************************************************
423 void NewGeom_Module::addDesktopMenuSeparator(const char* theMenuSourceText, const int theMenuPosition)
425 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
426 createMenu(separator(), aMenu, -1, theMenuPosition);
429 //******************************************************
430 QList<QAction*> NewGeom_Module::commandList() const
432 QList<QAction*> aActions;
433 for (int i = 0; i < myActionsList.size(); i++)
434 aActions.append(action(i));
438 //******************************************************
439 QStringList NewGeom_Module::commandIdList() const
441 return myActionsList;
444 //******************************************************
445 QMainWindow* NewGeom_Module::desktop() const
447 return application()->desktop();
450 //******************************************************
451 QString NewGeom_Module::commandId(const QAction* theCmd) const
453 int aId = actionId(theCmd);
454 if (aId < myActionsList.size())
455 return myActionsList[aId];
459 //******************************************************
460 QAction* NewGeom_Module::command(const QString& theId) const
462 int aId = myActionsList.indexOf(theId);
463 if ((aId != -1) && (aId < myActionsList.size())) {
469 //******************************************************
470 void NewGeom_Module::setNestedActions(const QString& theId, const QStringList& theActions)
472 myNestedActions[theId] = theActions;
475 //******************************************************
476 QStringList NewGeom_Module::nestedActions(const QString& theId) const
478 if (myNestedActions.contains(theId))
479 return myNestedActions[theId];
480 return QStringList();
483 //******************************************************
484 void NewGeom_Module::setDocumentKind(const QString& theId, const QString& theKind)
486 myDocumentType[theId] = theKind;
489 //******************************************************
490 QString NewGeom_Module::documentKind(const QString& theId) const
492 if (myDocumentType.contains(theId))
493 return myDocumentType[theId];
498 //******************************************************
499 void NewGeom_Module::selectionChanged()
501 LightApp_Module::selectionChanged();
502 myWorkshop->salomeViewerSelectionChanged();
505 //******************************************************
506 void NewGeom_Module::contextMenuPopup(const QString& theClient, QMenu* theMenu, QString& theTitle)
508 myWorkshop->contextMenuMgr()->addViewerItems(theMenu);
509 LightApp_Module::contextMenuPopup(theClient, theMenu, theTitle);
513 //******************************************************
514 void NewGeom_Module::createPreferences()
516 LightApp_Preferences* pref = preferences();
519 ModuleBase_Preferences::updateConfigByResources();
520 QString aModName = moduleName();
522 QtxPreferenceItem* item = pref->findItem(aModName, true );
523 if ( item && (!item->isEmpty() )) {
524 item->parentItem()->removeItem(item);
528 int catId = pref->addPreference(aModName, -1 );
531 NewGeom_PrefMgr aMgr(pref, aModName);
532 ModuleBase_Preferences::createEditContent(&aMgr, catId);
536 //******************************************************
537 void NewGeom_Module::preferencesChanged(const QString& theSection, const QString& theParam)
539 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
540 QString aVal = aResMgr->stringValue(theSection, theParam);
541 Config_Prop* aProp = Config_PropManager::findProp(theSection.toStdString(), theParam.toStdString());
542 std::string aValue = aVal.toStdString();
543 if (aValue.empty()) {
544 aValue = aProp->defaultValue();
545 aResMgr->setValue(theSection, theParam, QString(aValue.c_str()));
547 LightApp_Preferences* pref = preferences();
551 aProp->setValue(aValue);
555 void NewGeom_Module::inspectSalomeModules()
557 QStringList aModuleNames;
558 getApp()->modules(aModuleNames, false);
559 foreach(QString eachModule, aModuleNames) {
560 Config_ModuleReader::addDependencyModule(eachModule.toStdString());