Salome HOME
Merge from V6_main 11/02/2013
[modules/yacs.git] / src / salomegui / Yacsgui.cxx
1 // Copyright (C) 2006-2012  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.
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   PyGILState_Release(gstate);
173   // end of YACS plugins loading
174
175   if (_currentSVW)
176     onWindowActivated(_currentSVW);
177
178   return bOk;
179 }
180
181 bool Yacsgui::deactivateModule( SUIT_Study* theStudy )
182 {
183   DEBTRACE("Yacsgui::deactivateModule");
184
185   QMainWindow* parent = application()->desktop();
186   parent->setCorner(Qt::TopLeftCorner, Qt::TopDockWidgetArea);
187   parent->setCorner(Qt::BottomLeftCorner, Qt::BottomDockWidgetArea);
188   parent->setCorner(Qt::TopRightCorner, Qt::TopDockWidgetArea);
189   parent->setCorner(Qt::BottomRightCorner, Qt::BottomDockWidgetArea);
190
191   setMenuShown( false );
192   setToolShown( false );
193   _genericGui->showDockWidgets(false);
194   QtGuiContext *context = QtGuiContext::getQtCurrent();
195   _studyContextMap[theStudy->id()] = context;
196   DEBTRACE("_studyContextMap[theStudy] " << theStudy << " " << context);
197   return SalomeApp_Module::deactivateModule( theStudy );
198 }
199
200 // --- Default windows
201
202 void Yacsgui::windows( QMap<int, int>& theMap ) const
203 {
204   DEBTRACE("Yacsgui::windows");
205   theMap.clear();
206   theMap.insert( SalomeApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea );
207 //   theMap.insert( SalomeApp_Application::WT_PyConsole,     Qt::BottomDockWidgetArea );
208 }
209
210 QString  Yacsgui::engineIOR() const
211 {
212   DEBTRACE("Yacsgui::engineIOR");
213   QString anEngineIOR = SalomeApp_Engine_i::EngineIORForComponent( "YACS", true ).c_str();
214   return anEngineIOR;
215 }
216
217 void Yacsgui::onDblClick(const QModelIndex& index)
218 {
219   DEBTRACE("Yacsgui::onDblClick");
220   DataObjectList dol =getApp()->objectBrowser()->getSelected();
221   if (dol.isEmpty()) return;
222
223   SalomeApp_DataObject* item = dynamic_cast<SalomeApp_DataObject*>(dol[0]);
224   if (!item) return;
225
226   DEBTRACE(item->name().toStdString());
227   SalomeWrap_DataModel *model = dynamic_cast<SalomeWrap_DataModel*>(dataModel());
228   if (!model) return;
229   DEBTRACE(item->entry().toStdString());
230   QWidget * viewWindow = model->getViewWindow(item->entry().toStdString());
231   if (!_genericGui) return;
232   if (!viewWindow) return;
233   DEBTRACE("--- " << viewWindow << " "  << item->entry().toStdString());
234   if (getApp()->activeModule()->moduleName().compare("YACS") != 0)
235     getApp()->activateModule("YACS");
236
237   _selectFromTree = true;
238   viewWindow->setFocus();
239   _selectFromTree = false;
240 }
241
242 void Yacsgui::onWindowActivated( SUIT_ViewWindow* svw)
243 {
244   DEBTRACE("Yacsgui::onWindowActivated");
245   QxScene_ViewWindow* viewWindow = dynamic_cast<QxScene_ViewWindow*>(svw);
246   _currentSVW = svw;
247   if (!viewWindow)
248     {
249       _currentSVW = 0; // switch to another module
250       return;
251     }
252   DEBTRACE("viewWindow " << viewWindow);
253   DEBTRACE("activeModule()->moduleName() " << (getApp()->activeModule() ? getApp()->activeModule()->moduleName().toStdString() : "") );
254   if (!getApp()->activeModule() || getApp()->activeModule()->moduleName() != "YACS")
255     if ( !getApp()->activateModule("YACS") ) return;
256
257   disconnect(viewWindow, SIGNAL( tryClose( bool&, QxScene_ViewWindow* ) ),
258              this, SLOT(onTryClose( bool&, QxScene_ViewWindow* )) );
259   disconnect(viewWindow->getViewManager(), SIGNAL( deleteView( SUIT_ViewWindow* ) ),
260              this, SLOT(onWindowClosed( SUIT_ViewWindow* )) );
261   connect(viewWindow, SIGNAL( tryClose( bool&, QxScene_ViewWindow* ) ),
262           this, SLOT(onTryClose( bool&, QxScene_ViewWindow* )) );
263   connect(viewWindow->getViewManager(), SIGNAL( deleteView( SUIT_ViewWindow* ) ),
264           this, SLOT(onWindowClosed( SUIT_ViewWindow* )) );
265
266   YASSERT(_genericGui);
267   _genericGui->switchContext(viewWindow);
268   _studyContextMap[getApp()->activeStudy()->id()] = QtGuiContext::getQtCurrent();
269   
270   if (_selectFromTree) return;
271   SalomeWrap_DataModel *model = dynamic_cast<SalomeWrap_DataModel*>(dataModel());
272   if (!model) return;
273   model->setSelected(svw);
274 }
275
276 void Yacsgui::onWindowClosed( SUIT_ViewWindow* svw)
277 {
278   DEBTRACE("Yacsgui::onWindowClosed");
279   if ( svw && svw == _currentSVW )
280     _currentSVW = 0;
281 }
282
283 void Yacsgui::onTryClose(bool &isClosed, QxScene_ViewWindow* window)
284 {
285   DEBTRACE("Yacsgui::onTryClose");
286   YASSERT(_genericGui);
287   isClosed = _genericGui->closeContext(window);
288 }
289
290 CAM_DataModel* Yacsgui::createDataModel()
291 {
292   return new Yacsgui_DataModel(this);
293 }
294
295 bool Yacsgui::createSComponent()
296 {
297   DEBTRACE("Yacsgui::createSComponent");
298   _PTR(Study)            aStudy = (( SalomeApp_Study* )(getApp()->activeStudy()))->studyDS();
299   _PTR(StudyBuilder)     aBuilder (aStudy->NewBuilder());
300   _PTR(GenericAttribute) anAttr;
301   _PTR(AttributeName)    aName;
302
303   // --- Find or create "YACS" SComponent in the study
304
305   _PTR(SComponent) aComponent = aStudy->FindComponent("YACS");
306   if ( !aComponent )
307     {
308       aComponent = aBuilder->NewComponent("YACS");
309       anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributeName");
310       aName = _PTR(AttributeName) (anAttr);
311       aName->SetValue(getApp()->moduleTitle("YACS").toStdString());
312       
313       anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributePixMap");
314       _PTR(AttributePixMap) aPixmap(anAttr);
315       aPixmap->SetPixMap("ModuleYacs.png");
316       
317       aBuilder->DefineComponentInstance(aComponent, engineIOR().toLatin1().constData());
318
319       return true;
320     }
321   return false;
322 }
323
324 void Yacsgui::setResource(SUIT_ResourceMgr* r) 
325 {
326   DEBTRACE("Yacsgui::setResource");
327   _myresource = new Yacsgui_Resource(r);
328   _myresource->preferencesChanged();
329 }
330
331 void Yacsgui::createPreferences() 
332 {
333   DEBTRACE("Yacsgui::createPreferences");
334   _myresource->createPreferences(this);
335 }
336
337 void Yacsgui::preferencesChanged( const QString& sect, const QString& name ) 
338 {
339   DEBTRACE("Yacsgui::preferencesChanged");
340   _myresource->preferencesChanged(sect, name);
341   if(name=="userCatalog")
342     {
343       _genericGui->getCatalogWidget()->addCatalogFromFile(Resource::userCatalog.toStdString());
344     }
345 }
346
347 void Yacsgui::studyActivated()
348 {
349   int newStudyId = getApp()->activeStudy()->id();
350   DEBTRACE("Yacsgui::studyActivated " << _oldStudyId << " " << newStudyId);
351   
352   if (_oldStudyId != -1)
353     {
354       _studyContextMap[_oldStudyId] = QtGuiContext::getQtCurrent();      
355       if (_studyContextMap.count(newStudyId))
356         {
357           DEBTRACE("switch to valid context " << QtGuiContext::getQtCurrent() << " " << _studyContextMap[newStudyId]);
358           QtGuiContext::setQtCurrent(_studyContextMap[newStudyId]);
359         }
360       else
361         {
362           DEBTRACE("no switch to null context");
363         }
364     }
365   _oldStudyId = newStudyId;
366 }
367
368 void Yacsgui::loadSchema(const std::string& filename,bool edit, bool arrangeLocalNodes)
369 {
370   _genericGui->loadSchema(filename,edit,arrangeLocalNodes);
371 }
372
373 void Yacsgui::onCleanOnExit()
374 {
375   if ( _genericGui )
376     _genericGui->onCleanOnExit();
377   _currentSVW = 0;
378 }
379
380 // --- Export the module
381
382 extern "C"
383 {
384   YACS_EXPORT CAM_Module* createModule()
385   {
386     return new Yacsgui();
387   }
388 }