1 // Copyright (C) 2006-2016 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 #include "YACSExport.hxx"
22 #include "Yacsgui.hxx"
23 #include "Yacsgui_DataModel.hxx"
24 #include "Yacsgui_Resource.hxx"
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>
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>
44 #include <SALOME_LifeCycleCORBA.hxx>
46 #include <QInputDialog>
50 #include "GenericGui.hxx"
51 #include "CatalogWidget.hxx"
52 #include "Resource.hxx"
53 #include "QtGuiContext.hxx"
56 #include "YacsTrace.hxx"
59 using namespace YACS::HMI;
61 int Yacsgui::_oldStudyId = -1;
64 SalomeWrap_Module( "YACS" ) // default name
66 DEBTRACE("Yacsgui::Yacsgui");
69 _selectFromTree = false;
70 _studyContextMap.clear();
76 disconnect( getApp(), SIGNAL(studyClosed()), this, SLOT (onCleanOnExit()));
81 void Yacsgui::initialize( CAM_Application* app )
83 DEBTRACE("Yacsgui::initialize");
85 SalomeApp_Module::initialize( app );
87 QWidget* aParent = application()->desktop();
88 DEBTRACE(app << " " << application() << " " << application()->desktop() << " " << aParent);
90 SUIT_ResourceMgr* aResourceMgr = app->resourceMgr();
91 setResource(aResourceMgr);
93 _wrapper = new SuitWrapper(this);
94 _genericGui = new GenericGui(_wrapper, app->desktop());
96 if ( app && app->desktop() )
98 connect( app->desktop(), SIGNAL( windowActivated( SUIT_ViewWindow* ) ),
99 this, SLOT(onWindowActivated( SUIT_ViewWindow* )) );
102 SIGNAL(objectDoubleClicked( SUIT_DataObject* )),
104 SLOT (onDblClick( SUIT_DataObject* )));
107 SIGNAL(studyClosed()),
109 SLOT (onCleanOnExit()));
111 _genericGui->createActions();
112 _genericGui->createMenus();
113 _genericGui->createTools();
114 this->studyActivated();
116 // VSR 23/10/2014: note that this is not a good way to create SComponent from this point
117 // as initialize() method can be potentially called when there's no yet open study;
118 // this is better to do in activateModule()
119 SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
121 bool aLocked = (_PTR(AttributeStudyProperties)(aStudy->studyDS()->GetProperties()))->IsLocked();
123 SUIT_MessageBox::warning ( app->desktop(), QObject::tr("WRN_WARNING"), QObject::tr("WRN_STUDY_LOCKED") );
126 if (createSComponent()) updateObjBrowser();
130 // Load SALOME module catalogs
131 QStringList appModules;
132 app->modules(appModules,false);
133 for ( QStringList::const_iterator it = appModules.begin(); it != appModules.end(); ++it )
136 QString modName = app->moduleName( aModule ); // module name
137 if ( modName.isEmpty() ) modName = aModule;
138 QString rootDir = QString( "%1_ROOT_DIR" ).arg( modName ); // module root dir variable
139 QString modDir = getenv( rootDir.toLatin1().constData() ); // module root dir
140 if ( !modDir.isEmpty() )
142 QStringList cataLst = QStringList() << modDir << "share" << "salome" << "resources" << modName.toLower() << modName+"SchemaCatalog.xml";
143 QString cataFile = cataLst.join( QDir::separator() ); // YACS module catalog
144 if ( QFile::exists( cataFile ) )
145 _genericGui->getCatalogWidget()->addCatalogFromFile(cataFile.toStdString());
150 void Yacsgui::viewManagers( QStringList& list ) const
152 DEBTRACE("Yacsgui::viewManagers");
153 list.append( QxScene_Viewer::Type() );
156 bool Yacsgui::activateModule( SUIT_Study* theStudy )
158 DEBTRACE("Yacsgui::activateModule");
159 bool bOk = SalomeApp_Module::activateModule( theStudy );
161 QMainWindow* parent = application()->desktop();
162 if(Resource::dockWidgetPriority)
164 parent->setCorner(Qt::TopLeftCorner, Qt::LeftDockWidgetArea);
165 parent->setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea);
166 parent->setCorner(Qt::TopRightCorner, Qt::RightDockWidgetArea);
167 parent->setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea);
169 setMenuShown( true );
170 setToolShown( true );
171 _genericGui->showDockWidgets(true);
173 // import Python module that manages YACS plugins (need to be here because SalomePyQt API uses active module)
174 PyGILState_STATE gstate = PyGILState_Ensure();
175 PyObject* pluginsmanager=PyImport_ImportModule((char*)"salome_pluginsmanager");
176 if(pluginsmanager==NULL)
180 PyObject* result=PyObject_CallMethod( pluginsmanager, (char*)"initialize", (char*)"isss",1,"yacs","YACS",tr("YACS_PLUGINS").toStdString().c_str());
185 Py_XDECREF(pluginsmanager);
187 PyGILState_Release(gstate);
188 // end of YACS plugins loading
191 onWindowActivated(_currentSVW);
196 bool Yacsgui::deactivateModule( SUIT_Study* theStudy )
198 DEBTRACE("Yacsgui::deactivateModule");
200 QMainWindow* parent = application()->desktop();
201 parent->setCorner(Qt::TopLeftCorner, Qt::TopDockWidgetArea);
202 parent->setCorner(Qt::BottomLeftCorner, Qt::BottomDockWidgetArea);
203 parent->setCorner(Qt::TopRightCorner, Qt::TopDockWidgetArea);
204 parent->setCorner(Qt::BottomRightCorner, Qt::BottomDockWidgetArea);
206 setMenuShown( false );
207 setToolShown( false );
208 _genericGui->showDockWidgets(false);
209 QtGuiContext *context = QtGuiContext::getQtCurrent();
210 _studyContextMap[theStudy->id()] = context;
211 DEBTRACE("_studyContextMap[theStudy] " << theStudy << " " << context);
212 return SalomeApp_Module::deactivateModule( theStudy );
215 // --- Default windows
217 void Yacsgui::windows( QMap<int, int>& theMap ) const
219 DEBTRACE("Yacsgui::windows");
221 theMap.insert( SalomeApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea );
222 theMap.insert( SalomeApp_Application::WT_PyConsole, Qt::BottomDockWidgetArea );
225 QString Yacsgui::engineIOR() const
227 DEBTRACE("Yacsgui::engineIOR");
228 QString anEngineIOR = SalomeApp_Engine_i::EngineIORForComponent( "YACS", true ).c_str();
232 void Yacsgui::onDblClick(SUIT_DataObject*)
234 DEBTRACE("Yacsgui::onDblClick");
235 DataObjectList dol =getApp()->objectBrowser()->getSelected();
236 if (dol.isEmpty()) return;
238 SalomeApp_DataObject* item = dynamic_cast<SalomeApp_DataObject*>(dol[0]);
241 DEBTRACE(item->name().toStdString());
242 SalomeWrap_DataModel *model = dynamic_cast<SalomeWrap_DataModel*>(dataModel());
244 DEBTRACE(item->entry().toStdString());
245 QWidget * viewWindow = model->getViewWindow(item->entry().toStdString());
246 if (!_genericGui) return;
247 if (!viewWindow) return;
248 DEBTRACE("--- " << viewWindow << " " << item->entry().toStdString());
249 if (getApp()->activeModule()->moduleName().compare("YACS") != 0)
250 getApp()->activateModule("YACS");
252 _selectFromTree = true;
253 viewWindow->setFocus();
254 _selectFromTree = false;
257 void Yacsgui::onWindowActivated( SUIT_ViewWindow* svw)
259 DEBTRACE("Yacsgui::onWindowActivated");
260 QxScene_ViewWindow* viewWindow = dynamic_cast<QxScene_ViewWindow*>(svw);
264 _currentSVW = 0; // switch to another module
267 DEBTRACE("viewWindow " << viewWindow);
268 DEBTRACE("activeModule()->moduleName() " << (getApp()->activeModule() ? getApp()->activeModule()->moduleName().toStdString() : "") );
269 if (!getApp()->activeModule() || getApp()->activeModule()->moduleName() != "YACS")
270 if ( !getApp()->activateModule("YACS") ) return;
272 disconnect(viewWindow, SIGNAL( tryClose( bool&, QxScene_ViewWindow* ) ),
273 this, SLOT(onTryClose( bool&, QxScene_ViewWindow* )) );
274 disconnect(viewWindow->getViewManager(), SIGNAL( deleteView( SUIT_ViewWindow* ) ),
275 this, SLOT(onWindowClosed( SUIT_ViewWindow* )) );
276 connect(viewWindow, SIGNAL( tryClose( bool&, QxScene_ViewWindow* ) ),
277 this, SLOT(onTryClose( bool&, QxScene_ViewWindow* )) );
278 connect(viewWindow->getViewManager(), SIGNAL( deleteView( SUIT_ViewWindow* ) ),
279 this, SLOT(onWindowClosed( SUIT_ViewWindow* )) );
281 YASSERT(_genericGui);
282 _genericGui->switchContext(viewWindow);
283 _studyContextMap[getApp()->activeStudy()->id()] = QtGuiContext::getQtCurrent();
285 if (_selectFromTree) return;
286 SalomeWrap_DataModel *model = dynamic_cast<SalomeWrap_DataModel*>(dataModel());
288 model->setSelected(svw);
291 void Yacsgui::onWindowClosed( SUIT_ViewWindow* svw)
293 DEBTRACE("Yacsgui::onWindowClosed");
294 if ( svw && svw == _currentSVW )
298 void Yacsgui::onTryClose(bool &isClosed, QxScene_ViewWindow* window)
300 DEBTRACE("Yacsgui::onTryClose");
301 YASSERT(_genericGui);
302 isClosed = _genericGui->closeContext(window);
305 CAM_DataModel* Yacsgui::createDataModel()
307 return new Yacsgui_DataModel(this);
310 bool Yacsgui::createSComponent()
312 DEBTRACE("Yacsgui::createSComponent");
313 _PTR(Study) aStudy = (( SalomeApp_Study* )(getApp()->activeStudy()))->studyDS();
314 _PTR(StudyBuilder) aBuilder (aStudy->NewBuilder());
315 _PTR(GenericAttribute) anAttr;
316 _PTR(AttributeName) aName;
318 // --- Find or create "YACS" SComponent in the study
320 _PTR(SComponent) aComponent = aStudy->FindComponent("YACS");
323 aComponent = aBuilder->NewComponent("YACS");
324 anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributeName");
325 aName = _PTR(AttributeName) (anAttr);
326 aName->SetValue(getApp()->moduleTitle("YACS").toStdString());
328 anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributePixMap");
329 _PTR(AttributePixMap) aPixmap(anAttr);
330 aPixmap->SetPixMap("ModuleYacs.png");
332 aBuilder->DefineComponentInstance(aComponent, engineIOR().toLatin1().constData());
339 void Yacsgui::setResource(SUIT_ResourceMgr* r)
341 DEBTRACE("Yacsgui::setResource");
342 _myresource = new Yacsgui_Resource(r);
343 _myresource->preferencesChanged();
346 void Yacsgui::createPreferences()
348 DEBTRACE("Yacsgui::createPreferences");
349 _myresource->createPreferences(this);
352 void Yacsgui::preferencesChanged( const QString& sect, const QString& name )
354 DEBTRACE("Yacsgui::preferencesChanged");
355 _myresource->preferencesChanged(sect, name);
356 if(name=="userCatalog")
358 _genericGui->getCatalogWidget()->addCatalogFromFile(Resource::userCatalog.toStdString());
362 void Yacsgui::studyActivated()
364 int newStudyId = getApp()->activeStudy()->id();
365 DEBTRACE("Yacsgui::studyActivated " << _oldStudyId << " " << newStudyId);
367 if (_oldStudyId != -1)
369 _studyContextMap[_oldStudyId] = QtGuiContext::getQtCurrent();
370 if (_studyContextMap.count(newStudyId))
372 DEBTRACE("switch to valid context " << QtGuiContext::getQtCurrent() << " " << _studyContextMap[newStudyId]);
373 QtGuiContext::setQtCurrent(_studyContextMap[newStudyId]);
377 DEBTRACE("no switch to null context");
380 _oldStudyId = newStudyId;
383 void Yacsgui::loadSchema(const std::string& filename,bool edit, bool arrangeLocalNodes)
385 _genericGui->loadSchema(filename,edit,arrangeLocalNodes);
388 void Yacsgui::onCleanOnExit()
391 _genericGui->onCleanOnExit();
395 // --- Export the module
399 YACS_EXPORT CAM_Module* createModule()
401 return new Yacsgui();