]> SALOME platform Git repositories - modules/yacs.git/blob - src/salomegui/Yacsgui.cxx
Salome HOME
cae542ba031e30f54b31d2db5d559f3c8da0232d
[modules/yacs.git] / src / salomegui / Yacsgui.cxx
1 // Copyright (C) 2006-2024  CEA, EDF
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 <QDir>
46 #include <QInputDialog>
47 #include <QIcon>
48 #include <cassert>
49
50 #include "GenericGui.hxx"
51 #include "CatalogWidget.hxx"
52 #include "Resource.hxx"
53 #include "QtGuiContext.hxx"
54
55 //#define _DEVDEBUG_
56 #include "YacsTrace.hxx"
57
58 using namespace std;
59 using namespace YACS::HMI;
60
61 Yacsgui::Yacsgui() :
62   SalomeWrap_Module( "YACS" ) // default name
63 {
64   DEBTRACE("Yacsgui::Yacsgui");
65   _wrapper = 0;
66   _genericGui = 0;
67   _selectFromTree = false;
68   _studyContext = 0;
69 }
70
71 Yacsgui::~Yacsgui()
72 {
73   if ( getApp() )
74     disconnect( getApp(), SIGNAL(studyClosed()), this, SLOT  (onCleanOnExit()));
75   delete _wrapper;
76   delete _genericGui;
77 }
78
79 void Yacsgui::initialize( CAM_Application* app )
80 {
81   DEBTRACE("Yacsgui::initialize");
82   _currentSVW = 0;
83   SalomeApp_Module::initialize( app );
84
85   QWidget* aParent = application()->desktop();
86   DEBTRACE(app << "  " << application() << " " << application()->desktop() << " " << aParent);
87
88   SUIT_ResourceMgr* aResourceMgr = app->resourceMgr();
89   setResource(aResourceMgr);
90
91   _wrapper = new SuitWrapper(this);
92   _genericGui = new GenericGui(_wrapper, app->desktop());
93
94   if ( app && app->desktop() )
95     {
96       connect( app->desktop(), SIGNAL( windowActivated( SUIT_ViewWindow* ) ),
97                this, SLOT(onWindowActivated( SUIT_ViewWindow* )) );
98
99       connect( getApp(),
100                SIGNAL(objectDoubleClicked( SUIT_DataObject* )), 
101                this,
102                SLOT  (onDblClick( SUIT_DataObject* )));
103
104       connect( getApp(),
105                SIGNAL(studyClosed()),
106                this,
107                SLOT  (onCleanOnExit()));
108     }
109   _genericGui->createActions();
110   _genericGui->createMenus();
111   _genericGui->createTools();
112   _studyContext = QtGuiContext::getQtCurrent();
113
114   // VSR 23/10/2014: note that this is not a good way to create SComponent from this point
115   // as initialize() method can be potentially called when there's no yet open study;
116   // this is better to do in activateModule()
117   SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
118   if ( aStudy ) {
119     bool aLocked = (_PTR(AttributeStudyProperties)(aStudy->studyDS()->GetProperties()))->IsLocked();
120     if ( aLocked ) {
121       SUIT_MessageBox::warning ( app->desktop(), QObject::tr("WRN_WARNING"), QObject::tr("WRN_STUDY_LOCKED") );
122     }
123     else  {
124       if (createSComponent()) updateObjBrowser();
125     }
126   }
127
128   // Load SALOME module catalogs
129   QStringList appModules;
130   app->modules(appModules,false);
131   for ( QStringList::const_iterator it = appModules.begin(); it != appModules.end(); ++it )
132     {
133       QString aModule=*it;
134       QString modName = app->moduleName( aModule );                    // module name
135       if ( modName.isEmpty() ) modName = aModule;             
136       QString rootDir = QString( "%1_ROOT_DIR" ).arg( modName );       // module root dir variable
137       QString modDir  = getenv( rootDir.toLatin1().constData() );      // module root dir
138       if ( !modDir.isEmpty() ) 
139         {
140           QStringList cataLst = QStringList() << modDir << "share" << "salome" << "resources" << modName.toLower() << modName+"SchemaCatalog.xml";
141           QString cataFile = cataLst.join( QDir::separator() );          // YACS module catalog
142           if ( QFile::exists( cataFile ) ) 
143             _genericGui->getCatalogWidget()->addCatalogFromFile(cataFile.toStdString());
144         }
145     }
146 }
147
148 void Yacsgui::viewManagers( QStringList& list ) const
149 {
150   DEBTRACE("Yacsgui::viewManagers");
151   list.append( QxScene_Viewer::Type() );
152 }
153
154 bool Yacsgui::activateModule( SUIT_Study* theStudy )
155 {
156   DEBTRACE("Yacsgui::activateModule");
157   bool bOk = SalomeApp_Module::activateModule( theStudy );
158
159   QMainWindow* parent = application()->desktop();
160   if(Resource::dockWidgetPriority)
161     {
162       parent->setCorner(Qt::TopLeftCorner, Qt::LeftDockWidgetArea);
163       parent->setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea);
164       parent->setCorner(Qt::TopRightCorner, Qt::RightDockWidgetArea);
165       parent->setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea);
166     }
167   setMenuShown( true );
168   setToolShown( true );
169   _genericGui->showDockWidgets(true);
170
171   // import Python module that manages YACS plugins (need to be here because SalomePyQt API uses active module)
172   PyGILState_STATE gstate = PyGILState_Ensure();
173   PyObject* pluginsmanager=PyImport_ImportModule((char*)"salome_pluginsmanager");
174   if(pluginsmanager==NULL)
175     PyErr_Print();
176   else
177     {
178       PyObject* result=PyObject_CallMethod( pluginsmanager, (char*)"initialize", (char*)"isss",1,"yacs","YACS",tr("YACS_PLUGINS").toStdString().c_str());
179       if(result==NULL)
180         PyErr_Print();
181       Py_XDECREF(result);
182     }
183   Py_XDECREF(pluginsmanager);
184
185   PyGILState_Release(gstate);
186   // end of YACS plugins loading
187
188   if (_currentSVW)
189     onWindowActivated(_currentSVW);
190
191   return bOk;
192 }
193
194 bool Yacsgui::deactivateModule( SUIT_Study* theStudy )
195 {
196   DEBTRACE("Yacsgui::deactivateModule");
197
198   QMainWindow* parent = application()->desktop();
199   parent->setCorner(Qt::TopLeftCorner, Qt::TopDockWidgetArea);
200   parent->setCorner(Qt::BottomLeftCorner, Qt::BottomDockWidgetArea);
201   parent->setCorner(Qt::TopRightCorner, Qt::TopDockWidgetArea);
202   parent->setCorner(Qt::BottomRightCorner, Qt::BottomDockWidgetArea);
203
204   setMenuShown( false );
205   setToolShown( false );
206   _genericGui->showDockWidgets(false);
207   QtGuiContext *context = QtGuiContext::getQtCurrent();
208   _studyContext = context;
209   DEBTRACE("_studyContext " << theStudy << " " << context);
210   return SalomeApp_Module::deactivateModule( theStudy );
211 }
212
213 // --- Default windows
214
215 void Yacsgui::windows( QMap<int, int>& theMap ) const
216 {
217   DEBTRACE("Yacsgui::windows");
218   theMap.clear();
219   theMap.insert( SalomeApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea );
220   theMap.insert( SalomeApp_Application::WT_PyConsole,     Qt::BottomDockWidgetArea );
221 }
222
223 QString  Yacsgui::engineIOR() const
224 {
225   DEBTRACE("Yacsgui::engineIOR");
226   QString anEngineIOR = SalomeApp_Engine_i::EngineIORForComponent( "YACS", true ).c_str();
227   return anEngineIOR;
228 }
229
230 void Yacsgui::onDblClick(SUIT_DataObject*)
231 {
232   DEBTRACE("Yacsgui::onDblClick");
233   DataObjectList dol =getApp()->objectBrowser()->getSelected();
234   if (dol.isEmpty()) return;
235
236   SalomeApp_DataObject* item = dynamic_cast<SalomeApp_DataObject*>(dol[0]);
237   if (!item) return;
238
239   DEBTRACE(item->name().toStdString());
240   SalomeWrap_DataModel *model = dynamic_cast<SalomeWrap_DataModel*>(dataModel());
241   if (!model) return;
242   DEBTRACE(item->entry().toStdString());
243   QWidget * viewWindow = model->getViewWindow(item->entry().toStdString());
244   if (!_genericGui) return;
245   if (!viewWindow) return;
246   DEBTRACE("--- " << viewWindow << " "  << item->entry().toStdString());
247   if (getApp()->activeModule()->moduleName().compare("YACS") != 0)
248     getApp()->activateModule("YACS");
249
250   _selectFromTree = true;
251   viewWindow->setFocus();
252   _selectFromTree = false;
253 }
254
255 void Yacsgui::onWindowActivated( SUIT_ViewWindow* svw)
256 {
257   DEBTRACE("Yacsgui::onWindowActivated");
258   QxScene_ViewWindow* viewWindow = dynamic_cast<QxScene_ViewWindow*>(svw);
259   _currentSVW = svw;
260   if (!viewWindow)
261     {
262       _currentSVW = 0; // switch to another module
263       return;
264     }
265   DEBTRACE("viewWindow " << viewWindow);
266   DEBTRACE("activeModule()->moduleName() " << (getApp()->activeModule() ? getApp()->activeModule()->moduleName().toStdString() : "") );
267   if (!getApp()->activeModule() || getApp()->activeModule()->moduleName() != "YACS")
268     if ( !getApp()->activateModule("YACS") ) return;
269
270   disconnect(viewWindow, SIGNAL( tryClose( bool&, QxScene_ViewWindow* ) ),
271              this, SLOT(onTryClose( bool&, QxScene_ViewWindow* )) );
272   disconnect(viewWindow->getViewManager(), SIGNAL( deleteView( SUIT_ViewWindow* ) ),
273              this, SLOT(onWindowClosed( SUIT_ViewWindow* )) );
274   connect(viewWindow, SIGNAL( tryClose( bool&, QxScene_ViewWindow* ) ),
275           this, SLOT(onTryClose( bool&, QxScene_ViewWindow* )) );
276   connect(viewWindow->getViewManager(), SIGNAL( deleteView( SUIT_ViewWindow* ) ),
277           this, SLOT(onWindowClosed( SUIT_ViewWindow* )) );
278
279   YASSERT(_genericGui);
280   _genericGui->switchContext(viewWindow);
281   _studyContext = QtGuiContext::getQtCurrent();
282   
283   if (_selectFromTree) return;
284   SalomeWrap_DataModel *model = dynamic_cast<SalomeWrap_DataModel*>(dataModel());
285   if (!model) return;
286   model->setSelected(svw);
287 }
288
289 void Yacsgui::onWindowClosed( SUIT_ViewWindow* svw)
290 {
291   DEBTRACE("Yacsgui::onWindowClosed");
292   if ( svw && svw == _currentSVW )
293     _currentSVW = 0;
294 }
295
296 void Yacsgui::onTryClose(bool &isClosed, QxScene_ViewWindow* window)
297 {
298   DEBTRACE("Yacsgui::onTryClose");
299   YASSERT(_genericGui);
300   isClosed = _genericGui->closeContext(window);
301 }
302
303 CAM_DataModel* Yacsgui::createDataModel()
304 {
305   return new Yacsgui_DataModel(this);
306 }
307
308 bool Yacsgui::createSComponent()
309 {
310   DEBTRACE("Yacsgui::createSComponent");
311   _PTR(Study)            aStudy = (( SalomeApp_Study* )(getApp()->activeStudy()))->studyDS();
312   _PTR(StudyBuilder)     aBuilder (aStudy->NewBuilder());
313   _PTR(GenericAttribute) anAttr;
314   _PTR(AttributeName)    aName;
315
316   // --- Find or create "YACS" SComponent in the study
317
318   _PTR(SComponent) aComponent = aStudy->FindComponent("YACS");
319   if ( !aComponent )
320     {
321       aComponent = aBuilder->NewComponent("YACS");
322       anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributeName");
323       aName = _PTR(AttributeName) (anAttr);
324       aName->SetValue(getApp()->moduleTitle("YACS").toStdString());
325       
326       anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributePixMap");
327       _PTR(AttributePixMap) aPixmap(anAttr);
328       aPixmap->SetPixMap("ModuleYacs.png");
329       
330       aBuilder->DefineComponentInstance(aComponent, engineIOR().toLatin1().constData());
331
332       return true;
333     }
334   return false;
335 }
336
337 void Yacsgui::setResource(SUIT_ResourceMgr* r) 
338 {
339   DEBTRACE("Yacsgui::setResource");
340   _myresource = new Yacsgui_Resource(r);
341   _myresource->preferencesChanged();
342 }
343
344 void Yacsgui::createPreferences() 
345 {
346   DEBTRACE("Yacsgui::createPreferences");
347   _myresource->createPreferences(this);
348 }
349
350 void Yacsgui::preferencesChanged( const QString& sect, const QString& name ) 
351 {
352   DEBTRACE("Yacsgui::preferencesChanged");
353   _myresource->preferencesChanged(sect, name);
354   if(name=="userCatalog")
355     {
356       _genericGui->getCatalogWidget()->addCatalogFromFile(Resource::userCatalog.toStdString());
357     }
358 }
359
360 void Yacsgui::loadSchema(const std::string& filename,bool edit, bool arrangeLocalNodes)
361 {
362   _genericGui->loadSchema(filename,edit,arrangeLocalNodes);
363 }
364
365 void Yacsgui::onCleanOnExit()
366 {
367   if ( _genericGui )
368     _genericGui->onCleanOnExit();
369   _currentSVW = 0;
370 }
371
372 // --- Export the module
373
374 extern "C"
375 {
376   YACS_EXPORT CAM_Module* createModule()
377   {
378     return new Yacsgui();
379   }
380 }