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"
8 #include <XGUI_Workshop.h>
9 #include <XGUI_PropertyPanel.h>
10 #include <XGUI_ContextMenuMgr.h>
11 #include <XGUI_ObjectsBrowser.h>
12 #include <XGUI_OperationMgr.h>
14 #include <ModuleBase_Operation.h>
15 #include <ModuleBase_Preferences.h>
17 #include <LightApp_Application.h>
18 #include <LightApp_SelectionMgr.h>
19 #include <LightApp_OCCSelector.h>
20 #include <LightApp_Study.h>
21 #include <OCCViewer_ViewModel.h>
23 #include <SUIT_Selector.h>
24 #include <SUIT_Desktop.h>
25 #include <SUIT_ViewManager.h>
26 #include <SUIT_ResourceMgr.h>
28 #include <QtxPopupMgr.h>
29 #include <QtxActionMenuMgr.h>
30 #include <QtxResourceMgr.h>
32 #include <Config_PropManager.h>
34 #include <QDockWidget>
40 NewGeom_EXPORT CAM_Module* createModule()
42 return new NewGeom_Module();
45 NewGeom_EXPORT char* getModuleVersion()
51 class NewGeom_PrefMgr: public ModuleBase_IPrefMgr
54 NewGeom_PrefMgr(LightApp_Preferences* theMgr, const QString& theModName):myMgr(theMgr), myModName(theModName) {}
56 virtual int addPreference(const QString& theLbl, int pId,
57 SUIT_PreferenceMgr::PrefItemType theType,
58 const QString& theSection, const QString& theName )
60 return myMgr->addPreference(myModName, theLbl, pId, theType, theSection, theName);
63 virtual void setItemProperty(const QString& thePropName,
64 const QVariant& theValue,
67 myMgr->setItemProperty(thePropName, theValue, theId);
71 virtual SUIT_PreferenceMgr* prefMgr() const { return myMgr; }
74 LightApp_Preferences* myMgr;
81 //******************************************************
82 NewGeom_Module::NewGeom_Module()
83 : LightApp_Module("NewGeom"),
84 mySelector(0), myIsOpened(0), myPopupMgr(0)
86 myWorkshop = new XGUI_Workshop(this);
87 connect(myWorkshop, SIGNAL(commandStatusUpdated()),
88 this, SLOT(onUpdateCommandStatus()));
90 myProxyViewer = new NewGeom_SalomeViewer(this);
92 ModuleBase_Preferences::setResourceMgr(application()->resourceMgr());
93 ModuleBase_Preferences::loadCustomProps();
96 //******************************************************
97 NewGeom_Module::~NewGeom_Module()
101 //******************************************************
102 void NewGeom_Module::initialize(CAM_Application* theApp)
104 LightApp_Module::initialize(theApp);
106 myWorkshop->startApplication();
107 LightApp_Application* anApp = dynamic_cast<LightApp_Application*>(theApp);
109 connect(anApp, SIGNAL(preferenceResetToDefaults()), this, SLOT(onDefaultPreferences()));
112 //******************************************************
113 void NewGeom_Module::windows(QMap<int, int>& theWndMap) const
115 theWndMap.insert(LightApp_Application::WT_PyConsole, Qt::BottomDockWidgetArea);
118 //******************************************************
119 void NewGeom_Module::viewManagers(QStringList& theList) const
121 theList.append(OCCViewer_Viewer::Type());
124 //******************************************************
125 bool NewGeom_Module::activateModule(SUIT_Study* theStudy)
127 bool isDone = LightApp_Module::activateModule(theStudy);
132 QObject* aObj = myWorkshop->objectBrowser()->parent();
133 QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
135 QAction* aViewAct = aObjDoc->toggleViewAction();
136 aViewAct->setEnabled(true);
137 myWorkshop->objectBrowser()->setVisible(true);
138 aObjDoc->setVisible(true);
142 ViewManagerList OCCViewManagers;
143 application()->viewManagers(OCCViewer_Viewer::Type(), OCCViewManagers);
144 if (OCCViewManagers.size() > 0) {
145 mySelector = createSelector(OCCViewManagers.first());
148 //action(myEraseAll)->setEnabled(false);
151 myWorkshop->objectBrowser()->rebuildDataTree();
152 myWorkshop->updateCommandStatus();
154 QTimer::singleShot(1000, myWorkshop, SLOT(displayAllResults()));
157 myWorkshop->updateCommandStatus();
159 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
160 myIsStorePositions = aResMgr->booleanValue("Study", "store_positions", true);
161 myIsEditEnabled = getApp()->isEditEnabled();
162 getApp()->setEditEnabled(false);
164 // this following row is caused by #187 bug.
165 // SALOME saves the dock widget positions before deactivateModule() and
166 // load it after the module activation. So, if the panel is visible before
167 // deactivate, it becomes visible after activate.
168 // In order to avoid the visible property panel, the widget position save is
169 // switch off in this module
170 aResMgr->setValue("Study", "store_positions", false);
175 //******************************************************
176 bool NewGeom_Module::deactivateModule(SUIT_Study* theStudy)
181 QObject* aObj = myWorkshop->objectBrowser()->parent();
182 QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
184 aObjDoc->setVisible(false);
185 myWorkshop->objectBrowser()->setVisible(false);
186 QAction* aViewAct = aObjDoc->toggleViewAction();
187 aViewAct->setEnabled(false);
190 // the active operation should be stopped for the next activation.
191 // There should not be active operation and visualized preview.
192 // Abort operation should be performed before the selection's remove
193 // because the displayed objects should be removed from the viewer, but
194 // the AIS context is obtained from the selector.
195 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
197 anOperation->abort();
199 // Delete selector because it has to be redefined on next activation
201 myProxyViewer->setSelector(0);
206 //myWorkshop->contextMenuMgr()->disconnectViewer();
208 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
209 aResMgr->setValue("Study", "store_positions", myIsStorePositions);
210 getApp()->setEditEnabled(myIsEditEnabled);
212 return LightApp_Module::deactivateModule(theStudy);
215 //******************************************************
216 void NewGeom_Module::onViewManagerAdded(SUIT_ViewManager* theMgr)
219 mySelector = createSelector(theMgr);
223 //******************************************************
224 QtxPopupMgr* NewGeom_Module::popupMgr()
227 myPopupMgr = new QtxPopupMgr( 0, this );
231 //******************************************************
232 void NewGeom_Module::onDefaultPreferences()
234 ModuleBase_Preferences::resetConfig();
235 ModuleBase_Preferences::updateResourcesByConfig();
237 LightApp_Preferences* pref = preferences();
242 //******************************************************
243 void NewGeom_Module::onUpdateCommandStatus()
245 getApp()->updateActions();
248 //******************************************************
249 NewGeom_OCCSelector* NewGeom_Module::createSelector(SUIT_ViewManager* theMgr)
251 if (theMgr->getType() == OCCViewer_Viewer::Type()) {
252 OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
253 NewGeom_OCCSelector* aSelector = new NewGeom_OCCSelector(aViewer, getApp()->selectionMgr());
254 LightApp_SelectionMgr* aMgr = getApp()->selectionMgr();
255 QList<SUIT_Selector*> aList;
256 aMgr->selectors(aList);
257 foreach(SUIT_Selector* aSel, aList)
259 aSel->setEnabled(aSel == aSelector);
261 myProxyViewer->setSelector(aSelector);
267 //******************************************************
268 CAM_DataModel* NewGeom_Module::createDataModel()
270 NewGeom_DataModel* aDataModel = new NewGeom_DataModel(this);
272 // Calling addComponent() for persistent functionality work in the SalomeApp_Study
273 LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>(application()->activeStudy() );
274 aStudy->addComponent(aDataModel);
279 //******************************************************
280 QAction* NewGeom_Module::addFeature(const QString& theWBName, const QString& theId,
281 const QString& theTitle, const QString& theTip,
282 const QIcon& theIcon, const QKeySequence& theKeys,
285 int aMenu = createMenu(theWBName, -1, -1, 50);
286 int aTool = createTool(theWBName);
288 int aId = myActionsList.size();
289 myActionsList.append(theId);
290 SUIT_Desktop* aDesk = application()->desktop();
292 for (unsigned int i = 0; i < theKeys.count(); i++)
294 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
296 aAction->setData(theId);
297 int aItemId = createMenu(aId, aMenu, -1, 10);
298 int aToolId = createTool(aId, aTool);
302 //******************************************************
303 QAction* NewGeom_Module::addDesktopCommand(const QString& theId, const QString& theTitle,
304 const QString& theTip, const QIcon& theIcon,
305 const QKeySequence& theKeys, bool isCheckable,
306 const char* theMenuSourceText, const int theMenuPosition)
308 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
310 int aId = myActionsList.size();
311 myActionsList.append(theId);
312 SUIT_Desktop* aDesk = application()->desktop();
314 for (unsigned int i = 0; i < theKeys.count(); i++)
316 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
318 aAction->setData(theId);
319 createMenu(aId, aMenu, theMenuPosition);
323 //******************************************************
324 void NewGeom_Module::addDesktopMenuSeparator(const char* theMenuSourceText, const int theMenuPosition)
326 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
327 createMenu(separator(), aMenu, -1, theMenuPosition);
330 //******************************************************
331 QList<QAction*> NewGeom_Module::commandList() const
333 QList<QAction*> aActions;
334 for (int i = 0; i < myActionsList.size(); i++)
335 aActions.append(action(i));
339 //******************************************************
340 QStringList NewGeom_Module::commandIdList() const
342 return myActionsList;
345 //******************************************************
346 QMainWindow* NewGeom_Module::desktop() const
348 return application()->desktop();
351 //******************************************************
352 QString NewGeom_Module::commandId(const QAction* theCmd) const
354 int aId = actionId(theCmd);
355 if (aId < myActionsList.size())
356 return myActionsList[aId];
360 //******************************************************
361 QAction* NewGeom_Module::command(const QString& theId) const
363 int aId = myActionsList.indexOf(theId);
364 if ((aId != -1) && (aId < myActionsList.size())) {
370 //******************************************************
371 void NewGeom_Module::setNestedActions(const QString& theId, const QStringList& theActions)
373 myNestedActions[theId] = theActions;
376 //******************************************************
377 QStringList NewGeom_Module::nestedActions(const QString& theId) const
379 if (myNestedActions.contains(theId))
380 return myNestedActions[theId];
381 return QStringList();
384 //******************************************************
385 void NewGeom_Module::setDocumentKind(const QString& theId, const QString& theKind)
387 myDocumentType[theId] = theKind;
390 //******************************************************
391 QString NewGeom_Module::documentKind(const QString& theId) const
393 if (myDocumentType.contains(theId))
394 return myDocumentType[theId];
399 //******************************************************
400 void NewGeom_Module::selectionChanged()
402 LightApp_Module::selectionChanged();
403 myWorkshop->salomeViewerSelectionChanged();
406 //******************************************************
407 void NewGeom_Module::contextMenuPopup(const QString& theClient, QMenu* theMenu, QString& theTitle)
409 myWorkshop->contextMenuMgr()->addViewerItems(theMenu);
410 LightApp_Module::contextMenuPopup(theClient, theMenu, theTitle);
414 //******************************************************
415 void NewGeom_Module::createPreferences()
417 LightApp_Preferences* pref = preferences();
420 ModuleBase_Preferences::updateConfigByResources();
421 QString aModName = moduleName();
423 QtxPreferenceItem* item = pref->findItem(aModName, true );
424 if ( item && (!item->isEmpty() )) {
425 item->parentItem()->removeItem(item);
429 int catId = pref->addPreference(aModName, -1 );
432 NewGeom_PrefMgr aMgr(pref, aModName);
433 ModuleBase_Preferences::createEditContent(&aMgr, catId);
437 //******************************************************
438 void NewGeom_Module::preferencesChanged(const QString& theSection, const QString& theParam)
440 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
441 QString aVal = aResMgr->stringValue(theSection, theParam);
442 Config_Prop* aProp = Config_PropManager::findProp(theSection.toStdString(), theParam.toStdString());
443 std::string aValue = aVal.toStdString();
444 if (aValue.empty()) {
445 aValue = aProp->defaultValue();
446 aResMgr->setValue(theSection, theParam, QString(aValue.c_str()));
448 LightApp_Preferences* pref = preferences();
452 aProp->setValue(aValue);