Version: 9.15.0
Yacsgui.cxx
Go to the documentation of this file.
1 // Copyright (C) 2006-2025 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 
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 
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  }
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 );
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  Py_ssize_t l = 12; // size of YACS_PLUGINS string
179  PyObject* result=PyObject_CallMethod( pluginsmanager, (char*)"initialize", (char*)"isss",1,"yacs","YACS",tr("YACS_PLUGINS").toStdString().c_str(),l);
180  if(result==NULL)
181  PyErr_Print();
182  Py_XDECREF(result);
183  }
184  Py_XDECREF(pluginsmanager);
185 
186  PyGILState_Release(gstate);
187  // end of YACS plugins loading
188 
189  if (_currentSVW)
191 
192  return bOk;
193 }
194 
195 bool Yacsgui::deactivateModule( SUIT_Study* theStudy )
196 {
197  DEBTRACE("Yacsgui::deactivateModule");
198 
199  QMainWindow* parent = application()->desktop();
200  parent->setCorner(Qt::TopLeftCorner, Qt::TopDockWidgetArea);
201  parent->setCorner(Qt::BottomLeftCorner, Qt::BottomDockWidgetArea);
202  parent->setCorner(Qt::TopRightCorner, Qt::TopDockWidgetArea);
203  parent->setCorner(Qt::BottomRightCorner, Qt::BottomDockWidgetArea);
204 
205  setMenuShown( false );
206  setToolShown( false );
208  QtGuiContext *context = QtGuiContext::getQtCurrent();
209  _studyContext = context;
210  DEBTRACE("_studyContext " << theStudy << " " << context);
211  return SalomeApp_Module::deactivateModule( theStudy );
212 }
213 
214 // --- Default windows
215 
216 void Yacsgui::windows( QMap<int, int>& theMap ) const
217 {
218  DEBTRACE("Yacsgui::windows");
219  theMap.clear();
220  theMap.insert( SalomeApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea );
221  theMap.insert( SalomeApp_Application::WT_PyConsole, Qt::BottomDockWidgetArea );
222 }
223 
224 QString Yacsgui::engineIOR() const
225 {
226  DEBTRACE("Yacsgui::engineIOR");
227  QString anEngineIOR = SalomeApp_Engine_i::EngineIORForComponent( "YACS", true ).c_str();
228  return anEngineIOR;
229 }
230 
231 void Yacsgui::onDblClick(SUIT_DataObject*)
232 {
233  DEBTRACE("Yacsgui::onDblClick");
234  DataObjectList dol =getApp()->objectBrowser()->getSelected();
235  if (dol.isEmpty()) return;
236 
237  SalomeApp_DataObject* item = dynamic_cast<SalomeApp_DataObject*>(dol[0]);
238  if (!item) return;
239 
240  DEBTRACE(item->name().toStdString());
241  SalomeWrap_DataModel *model = dynamic_cast<SalomeWrap_DataModel*>(dataModel());
242  if (!model) return;
243  DEBTRACE(item->entry().toStdString());
244  QWidget * viewWindow = model->getViewWindow(item->entry().toStdString());
245  if (!_genericGui) return;
246  if (!viewWindow) return;
247  DEBTRACE("--- " << viewWindow << " " << item->entry().toStdString());
248  if (getApp()->activeModule()->moduleName().compare("YACS") != 0)
249  getApp()->activateModule("YACS");
250 
251  _selectFromTree = true;
252  viewWindow->setFocus();
253  _selectFromTree = false;
254 }
255 
256 void Yacsgui::onWindowActivated( SUIT_ViewWindow* svw)
257 {
258  DEBTRACE("Yacsgui::onWindowActivated");
259  QxScene_ViewWindow* viewWindow = dynamic_cast<QxScene_ViewWindow*>(svw);
260  _currentSVW = svw;
261  if (!viewWindow)
262  {
263  _currentSVW = 0; // switch to another module
264  return;
265  }
266  DEBTRACE("viewWindow " << viewWindow);
267  DEBTRACE("activeModule()->moduleName() " << (getApp()->activeModule() ? getApp()->activeModule()->moduleName().toStdString() : "") );
268  if (!getApp()->activeModule() || getApp()->activeModule()->moduleName() != "YACS")
269  if ( !getApp()->activateModule("YACS") ) return;
270 
271  disconnect(viewWindow, SIGNAL( tryClose( bool&, QxScene_ViewWindow* ) ),
272  this, SLOT(onTryClose( bool&, QxScene_ViewWindow* )) );
273  disconnect(viewWindow->getViewManager(), SIGNAL( deleteView( SUIT_ViewWindow* ) ),
274  this, SLOT(onWindowClosed( SUIT_ViewWindow* )) );
275  connect(viewWindow, SIGNAL( tryClose( bool&, QxScene_ViewWindow* ) ),
276  this, SLOT(onTryClose( bool&, QxScene_ViewWindow* )) );
277  connect(viewWindow->getViewManager(), SIGNAL( deleteView( SUIT_ViewWindow* ) ),
278  this, SLOT(onWindowClosed( SUIT_ViewWindow* )) );
279 
281  _genericGui->switchContext(viewWindow);
282  _studyContext = QtGuiContext::getQtCurrent();
283 
284  if (_selectFromTree) return;
285  SalomeWrap_DataModel *model = dynamic_cast<SalomeWrap_DataModel*>(dataModel());
286  if (!model) return;
287  model->setSelected(svw);
288 }
289 
290 void Yacsgui::onWindowClosed( SUIT_ViewWindow* svw)
291 {
292  DEBTRACE("Yacsgui::onWindowClosed");
293  if ( svw && svw == _currentSVW )
294  _currentSVW = 0;
295 }
296 
297 void Yacsgui::onTryClose(bool &isClosed, QxScene_ViewWindow* window)
298 {
299  DEBTRACE("Yacsgui::onTryClose");
301  isClosed = _genericGui->closeContext(window);
302 }
303 
304 CAM_DataModel* Yacsgui::createDataModel()
305 {
306  return new Yacsgui_DataModel(this);
307 }
308 
310 {
311  DEBTRACE("Yacsgui::createSComponent");
312  _PTR(Study) aStudy = (( SalomeApp_Study* )(getApp()->activeStudy()))->studyDS();
313  _PTR(StudyBuilder) aBuilder (aStudy->NewBuilder());
314  _PTR(GenericAttribute) anAttr;
315  _PTR(AttributeName) aName;
316 
317  // --- Find or create "YACS" SComponent in the study
318 
319  _PTR(SComponent) aComponent = aStudy->FindComponent("YACS");
320  if ( !aComponent )
321  {
322  aComponent = aBuilder->NewComponent("YACS");
323  anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributeName");
324  aName = _PTR(AttributeName) (anAttr);
325  aName->SetValue(getApp()->moduleTitle("YACS").toStdString());
326 
327  anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributePixMap");
328  _PTR(AttributePixMap) aPixmap(anAttr);
329  aPixmap->SetPixMap("ModuleYacs.png");
330 
331  aBuilder->DefineComponentInstance(aComponent, engineIOR().toLatin1().constData());
332 
333  return true;
334  }
335  return false;
336 }
337 
338 void Yacsgui::setResource(SUIT_ResourceMgr* r)
339 {
340  DEBTRACE("Yacsgui::setResource");
343 }
344 
346 {
347  DEBTRACE("Yacsgui::createPreferences");
349 }
350 
351 void Yacsgui::preferencesChanged( const QString& sect, const QString& name )
352 {
353  DEBTRACE("Yacsgui::preferencesChanged");
354  _myresource->preferencesChanged(sect, name);
355  if(name=="userCatalog")
356  {
357  _genericGui->getCatalogWidget()->addCatalogFromFile(Resource::userCatalog.toStdString());
358  }
359 }
360 
361 void Yacsgui::loadSchema(const std::string& filename,bool edit, bool arrangeLocalNodes)
362 {
363  _genericGui->loadSchema(filename,edit,arrangeLocalNodes);
364 }
365 
367 {
368  if ( _genericGui )
370  _currentSVW = 0;
371 }
372 
373 // --- Export the module
374 
375 extern "C"
376 {
377  YACS_EXPORT CAM_Module* createModule()
378  {
379  return new Yacsgui();
380  }
381 }
int Py_ssize_t
Definition: PythonNode.cxx:45
#define YACS_EXPORT
Definition: YACSExport.hxx:30
#define YASSERT(val)
YASSERT macro is always defined, used like assert, but throw a YACS::Exception instead of abort.
Definition: YacsTrace.hxx:59
#define DEBTRACE(msg)
Definition: YacsTrace.hxx:31
YACS_EXPORT CAM_Module * createModule()
Definition: Yacsgui.cxx:377
virtual void setSelected(QWidget *viewWindow)
QWidget * getViewWindow(std::string entry)
virtual bool addCatalogFromFile(std::string fileName)
void switchContext(QWidget *view, bool onExit=false)
Definition: GenericGui.cxx:824
bool closeContext(QWidget *view, bool onExit=false)
Definition: GenericGui.cxx:863
virtual void loadSchema(const std::string &filename, bool edit=true, bool arrangeLocalNodes=false)
CatalogWidget * getCatalogWidget()
Definition: GenericGui.hxx:77
void showDockWidgets(bool isVisible)
Definition: GenericGui.cxx:983
viewer QGraphicsScene: deriver SUIT_ViewModel comme QxGraph_Viewer
Definition: SuitWrapper.hxx:43
virtual void preferencesChanged(const QString &sect, const QString &name)
virtual void createPreferences(Yacsgui *swm)
virtual ~Yacsgui()
Definition: Yacsgui.cxx:71
virtual void createPreferences()
Definition: Yacsgui.cxx:345
friend class Yacsgui_Resource
Definition: Yacsgui.hxx:50
void onWindowClosed(SUIT_ViewWindow *svw)
Definition: Yacsgui.cxx:290
void initialize(CAM_Application *app)
Definition: Yacsgui.cxx:79
bool _selectFromTree
Definition: Yacsgui.hxx:83
void windows(QMap< int, int > &theMap) const
Definition: Yacsgui.cxx:216
Yacsgui_Resource * _myresource
Definition: Yacsgui.hxx:84
virtual CAM_DataModel * createDataModel()
Definition: Yacsgui.cxx:304
bool deactivateModule(SUIT_Study *theStudy)
Definition: Yacsgui.cxx:195
virtual void loadSchema(const std::string &filename, bool edit=true, bool arrangeLocalNodes=false)
Definition: Yacsgui.cxx:361
void onDblClick(SUIT_DataObject *object)
Definition: Yacsgui.cxx:231
void onWindowActivated(SUIT_ViewWindow *svw)
Definition: Yacsgui.cxx:256
YACS::HMI::SuitWrapper * _wrapper
Definition: Yacsgui.hxx:81
YACS::HMI::QtGuiContext * _studyContext
Definition: Yacsgui.hxx:86
virtual void viewManagers(QStringList &list) const
Definition: Yacsgui.cxx:148
virtual void setResource(SUIT_ResourceMgr *r)
Definition: Yacsgui.cxx:338
bool createSComponent()
Definition: Yacsgui.cxx:309
void onTryClose(bool &isClosed, QxScene_ViewWindow *window)
Definition: Yacsgui.cxx:297
virtual QString engineIOR() const
Definition: Yacsgui.cxx:224
SUIT_ViewWindow * _currentSVW
Definition: Yacsgui.hxx:85
Yacsgui()
Definition: Yacsgui.cxx:61
void onCleanOnExit()
Definition: Yacsgui.cxx:366
YACS::HMI::GenericGui * _genericGui
Definition: Yacsgui.hxx:82
bool activateModule(SUIT_Study *theStudy)
Definition: Yacsgui.cxx:154
virtual void preferencesChanged(const QString &sect, const QString &name)
Definition: Yacsgui.cxx:351