Salome HOME
Revert "Synchronize adm files"
[modules/yacs.git] / src / salomegui / Yacsgui.cxx
1 // Copyright (C) 2006-2014  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <Python.h>
21 #include "YACSExport.hxx"
22 #include "Yacsgui.hxx"
23 #include "Yacsgui_DataModel.hxx"
24 #include "Yacsgui_Resource.hxx"
25
26 #include <SUIT_MessageBox.h>
27 #include <SUIT_ResourceMgr.h>
28 #include <SUIT_Desktop.h>
29 #include <SUIT_ViewManager.h>
30 #include <SUIT_ViewWindow.h>
31 #include <SalomeApp_Application.h>
32 #include <SalomeApp_Engine_i.h>
33 #include <QxScene_ViewManager.h>
34 #include <QxScene_ViewModel.h>
35 #include <QxScene_ViewWindow.h>
36
37 #include <SalomeApp_DataObject.h>
38 #include <SalomeApp_Study.h>
39 #include <SalomeApp_Module.h>
40 #include <SUIT_DataBrowser.h>
41 #include <QtxTreeView.h>
42 #include <SUIT_DataObject.h>
43
44 #include <SALOME_LifeCycleCORBA.hxx>
45 #include <QInputDialog>
46 #include <QIcon>
47 #include <cassert>
48
49 #include "GenericGui.hxx"
50 #include "CatalogWidget.hxx"
51 #include "Resource.hxx"
52 #include "QtGuiContext.hxx"
53
54 //#define _DEVDEBUG_
55 #include "YacsTrace.hxx"
56
57 using namespace std;
58 using namespace YACS::HMI;
59
60 int  Yacsgui::_oldStudyId = -1;
61
62 Yacsgui::Yacsgui() :
63   SalomeWrap_Module( "YACS" ) // default name
64 {
65   DEBTRACE("Yacsgui::Yacsgui");
66   _wrapper = 0;
67   _genericGui = 0;
68   _selectFromTree = false;
69   _studyContextMap.clear();
70 }
71
72 Yacsgui::~Yacsgui()
73 {
74   if ( getApp() )
75     disconnect( getApp(), SIGNAL(studyClosed()), this, SLOT  (onCleanOnExit()));
76   delete _wrapper;
77   delete _genericGui;
78 }
79
80 void Yacsgui::initialize( CAM_Application* app )
81 {
82   DEBTRACE("Yacsgui::initialize");
83   _currentSVW = 0;
84   SalomeApp_Module::initialize( app );
85
86   QWidget* aParent = application()->desktop();
87   DEBTRACE(app << "  " << application() << " " << application()->desktop() << " " << aParent);
88
89   SUIT_ResourceMgr* aResourceMgr = app->resourceMgr();
90   setResource(aResourceMgr);
91
92   _wrapper = new SuitWrapper(this);
93   _genericGui = new GenericGui(_wrapper, app->desktop());
94
95   if ( app && app->desktop() )
96     {
97       connect( app->desktop(), SIGNAL( windowActivated( SUIT_ViewWindow* ) ),
98                this, SLOT(onWindowActivated( SUIT_ViewWindow* )) );
99
100       connect( getApp()->objectBrowser()->treeView(),
101                SIGNAL( doubleClicked(const QModelIndex&) ), 
102                this,
103                SLOT  ( onDblClick(const QModelIndex&) ) );
104
105       connect( getApp(),
106                SIGNAL(studyClosed()),
107                this,
108                SLOT  (onCleanOnExit()));
109     }
110   _genericGui->createActions();
111   _genericGui->createMenus();
112   _genericGui->createTools();
113   this->studyActivated();
114
115   if (createSComponent()) updateObjBrowser();
116
117   // Load SALOME module catalogs
118   QStringList appModules;
119   app->modules(appModules,false);
120   for ( QStringList::const_iterator it = appModules.begin(); it != appModules.end(); ++it )
121     {
122       QString aModule=*it;
123       QString modName = app->moduleName( aModule );                    // module name
124       if ( modName.isEmpty() ) modName = aModule;             
125       QString rootDir = QString( "%1_ROOT_DIR" ).arg( modName );       // module root dir variable
126       QString modDir  = getenv( rootDir.toLatin1().constData() );      // module root dir
127       if ( !modDir.isEmpty() ) 
128         {
129           QStringList cataLst = QStringList() << modDir << "share" << "salome" << "resources" << modName.toLower() << modName+"SchemaCatalog.xml";
130           QString cataFile = cataLst.join( QDir::separator() );          // YACS module catalog
131           if ( QFile::exists( cataFile ) ) 
132             _genericGui->getCatalogWidget()->addCatalogFromFile(cataFile.toStdString());
133         }
134     }
135 }
136
137 void Yacsgui::viewManagers( QStringList& list ) const
138 {
139   DEBTRACE("Yacsgui::viewManagers");
140   list.append( QxScene_Viewer::Type() );
141 }
142
143 bool Yacsgui::activateModule( SUIT_Study* theStudy )
144 {
145   DEBTRACE("Yacsgui::activateModule");
146   bool bOk = SalomeApp_Module::activateModule( theStudy );
147
148   QMainWindow* parent = application()->desktop();
149   if(Resource::dockWidgetPriority)
150     {
151       parent->setCorner(Qt::TopLeftCorner, Qt::LeftDockWidgetArea);
152       parent->setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea);
153       parent->setCorner(Qt::TopRightCorner, Qt::RightDockWidgetArea);
154       parent->setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea);
155     }
156   setMenuShown( true );
157   setToolShown( true );
158   _genericGui->showDockWidgets(true);
159
160   // import Python module that manages YACS plugins (need to be here because SalomePyQt API uses active module)
161   PyGILState_STATE gstate = PyGILState_Ensure();
162   PyObject* pluginsmanager=PyImport_ImportModule((char*)"salome_pluginsmanager");
163   if(pluginsmanager==NULL)
164     PyErr_Print();
165   else
166     {
167       PyObject* result=PyObject_CallMethod( pluginsmanager, (char*)"initialize", (char*)"isss",1,"yacs","YACS",tr("YACS_PLUGINS").toStdString().c_str());
168       if(result==NULL)
169         PyErr_Print();
170       Py_XDECREF(result);
171     }
172   Py_XDECREF(pluginsmanager);
173
174   PyGILState_Release(gstate);
175   // end of YACS plugins loading
176
177   if (_currentSVW)
178     onWindowActivated(_currentSVW);
179
180   return bOk;
181 }
182
183 bool Yacsgui::deactivateModule( SUIT_Study* theStudy )
184 {
185   DEBTRACE("Yacsgui::deactivateModule");
186
187   QMainWindow* parent = application()->desktop();
188   parent->setCorner(Qt::TopLeftCorner, Qt::TopDockWidgetArea);
189   parent->setCorner(Qt::BottomLeftCorner, Qt::BottomDockWidgetArea);
190   parent->setCorner(Qt::TopRightCorner, Qt::TopDockWidgetArea);
191   parent->setCorner(Qt::BottomRightCorner, Qt::BottomDockWidgetArea);
192
193   setMenuShown( false );
194   setToolShown( false );
195   _genericGui->showDockWidgets(false);
196   QtGuiContext *context = QtGuiContext::getQtCurrent();
197   _studyContextMap[theStudy->id()] = context;
198   DEBTRACE("_studyContextMap[theStudy] " << theStudy << " " << context);
199   return SalomeApp_Module::deactivateModule( theStudy );
200 }
201
202 // --- Default windows
203
204 void Yacsgui::windows( QMap<int, int>& theMap ) const
205 {
206   DEBTRACE("Yacsgui::windows");
207   theMap.clear();
208   theMap.insert( SalomeApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea );
209   theMap.insert( SalomeApp_Application::WT_PyConsole,     Qt::BottomDockWidgetArea );
210 }
211
212 QString  Yacsgui::engineIOR() const
213 {
214   DEBTRACE("Yacsgui::engineIOR");
215   QString anEngineIOR = SalomeApp_Engine_i::EngineIORForComponent( "YACS", true ).c_str();
216   return anEngineIOR;
217 }
218
219 void Yacsgui::onDblClick(const QModelIndex& index)
220 {
221   DEBTRACE("Yacsgui::onDblClick");
222   DataObjectList dol =getApp()->objectBrowser()->getSelected();
223   if (dol.isEmpty()) return;
224
225   SalomeApp_DataObject* item = dynamic_cast<SalomeApp_DataObject*>(dol[0]);
226   if (!item) return;
227
228   DEBTRACE(item->name().toStdString());
229   SalomeWrap_DataModel *model = dynamic_cast<SalomeWrap_DataModel*>(dataModel());
230   if (!model) return;
231   DEBTRACE(item->entry().toStdString());
232   QWidget * viewWindow = model->getViewWindow(item->entry().toStdString());
233   if (!_genericGui) return;
234   if (!viewWindow) return;
235   DEBTRACE("--- " << viewWindow << " "  << item->entry().toStdString());
236   if (getApp()->activeModule()->moduleName().compare("YACS") != 0)
237     getApp()->activateModule("YACS");
238
239   _selectFromTree = true;
240   viewWindow->setFocus();
241   _selectFromTree = false;
242 }
243
244 void Yacsgui::onWindowActivated( SUIT_ViewWindow* svw)
245 {
246   DEBTRACE("Yacsgui::onWindowActivated");
247   QxScene_ViewWindow* viewWindow = dynamic_cast<QxScene_ViewWindow*>(svw);
248   _currentSVW = svw;
249   if (!viewWindow)
250     {
251       _currentSVW = 0; // switch to another module
252       return;
253     }
254   DEBTRACE("viewWindow " << viewWindow);
255   DEBTRACE("activeModule()->moduleName() " << (getApp()->activeModule() ? getApp()->activeModule()->moduleName().toStdString() : "") );
256   if (!getApp()->activeModule() || getApp()->activeModule()->moduleName() != "YACS")
257     if ( !getApp()->activateModule("YACS") ) return;
258
259   disconnect(viewWindow, SIGNAL( tryClose( bool&, QxScene_ViewWindow* ) ),
260              this, SLOT(onTryClose( bool&, QxScene_ViewWindow* )) );
261   disconnect(viewWindow->getViewManager(), SIGNAL( deleteView( SUIT_ViewWindow* ) ),
262              this, SLOT(onWindowClosed( SUIT_ViewWindow* )) );
263   connect(viewWindow, SIGNAL( tryClose( bool&, QxScene_ViewWindow* ) ),
264           this, SLOT(onTryClose( bool&, QxScene_ViewWindow* )) );
265   connect(viewWindow->getViewManager(), SIGNAL( deleteView( SUIT_ViewWindow* ) ),
266           this, SLOT(onWindowClosed( SUIT_ViewWindow* )) );
267
268   YASSERT(_genericGui);
269   _genericGui->switchContext(viewWindow);
270   _studyContextMap[getApp()->activeStudy()->id()] = QtGuiContext::getQtCurrent();
271   
272   if (_selectFromTree) return;
273   SalomeWrap_DataModel *model = dynamic_cast<SalomeWrap_DataModel*>(dataModel());
274   if (!model) return;
275   model->setSelected(svw);
276 }
277
278 void Yacsgui::onWindowClosed( SUIT_ViewWindow* svw)
279 {
280   DEBTRACE("Yacsgui::onWindowClosed");
281   if ( svw && svw == _currentSVW )
282     _currentSVW = 0;
283 }
284
285 void Yacsgui::onTryClose(bool &isClosed, QxScene_ViewWindow* window)
286 {
287   DEBTRACE("Yacsgui::onTryClose");
288   YASSERT(_genericGui);
289   isClosed = _genericGui->closeContext(window);
290 }
291
292 CAM_DataModel* Yacsgui::createDataModel()
293 {
294   return new Yacsgui_DataModel(this);
295 }
296
297 bool Yacsgui::createSComponent()
298 {
299   DEBTRACE("Yacsgui::createSComponent");
300   _PTR(Study)            aStudy = (( SalomeApp_Study* )(getApp()->activeStudy()))->studyDS();
301   _PTR(StudyBuilder)     aBuilder (aStudy->NewBuilder());
302   _PTR(GenericAttribute) anAttr;
303   _PTR(AttributeName)    aName;
304
305   // --- Find or create "YACS" SComponent in the study
306
307   _PTR(SComponent) aComponent = aStudy->FindComponent("YACS");
308   if ( !aComponent )
309     {
310       aComponent = aBuilder->NewComponent("YACS");
311       anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributeName");
312       aName = _PTR(AttributeName) (anAttr);
313       aName->SetValue(getApp()->moduleTitle("YACS").toStdString());
314       
315       anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributePixMap");
316       _PTR(AttributePixMap) aPixmap(anAttr);
317       aPixmap->SetPixMap("ModuleYacs.png");
318       
319       aBuilder->DefineComponentInstance(aComponent, engineIOR().toLatin1().constData());
320
321       return true;
322     }
323   return false;
324 }
325
326 void Yacsgui::setResource(SUIT_ResourceMgr* r) 
327 {
328   DEBTRACE("Yacsgui::setResource");
329   _myresource = new Yacsgui_Resource(r);
330   _myresource->preferencesChanged();
331 }
332
333 void Yacsgui::createPreferences() 
334 {
335   DEBTRACE("Yacsgui::createPreferences");
336   _myresource->createPreferences(this);
337 }
338
339 void Yacsgui::preferencesChanged( const QString& sect, const QString& name ) 
340 {
341   DEBTRACE("Yacsgui::preferencesChanged");
342   _myresource->preferencesChanged(sect, name);
343   if(name=="userCatalog")
344     {
345       _genericGui->getCatalogWidget()->addCatalogFromFile(Resource::userCatalog.toStdString());
346     }
347 }
348
349 void Yacsgui::studyActivated()
350 {
351   int newStudyId = getApp()->activeStudy()->id();
352   DEBTRACE("Yacsgui::studyActivated " << _oldStudyId << " " << newStudyId);
353   
354   if (_oldStudyId != -1)
355     {
356       _studyContextMap[_oldStudyId] = QtGuiContext::getQtCurrent();      
357       if (_studyContextMap.count(newStudyId))
358         {
359           DEBTRACE("switch to valid context " << QtGuiContext::getQtCurrent() << " " << _studyContextMap[newStudyId]);
360           QtGuiContext::setQtCurrent(_studyContextMap[newStudyId]);
361         }
362       else
363         {
364           DEBTRACE("no switch to null context");
365         }
366     }
367   _oldStudyId = newStudyId;
368 }
369
370 void Yacsgui::loadSchema(const std::string& filename,bool edit, bool arrangeLocalNodes)
371 {
372   _genericGui->loadSchema(filename,edit,arrangeLocalNodes);
373 }
374
375 void Yacsgui::onCleanOnExit()
376 {
377   if ( _genericGui )
378     _genericGui->onCleanOnExit();
379   _currentSVW = 0;
380 }
381
382 // --- Export the module
383
384 extern "C"
385 {
386   YACS_EXPORT CAM_Module* createModule()
387   {
388     return new Yacsgui();
389   }
390 }