1 // Copyright (C) 2006-2014 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>
45 #include <QInputDialog>
49 #include "GenericGui.hxx"
50 #include "CatalogWidget.hxx"
51 #include "Resource.hxx"
52 #include "QtGuiContext.hxx"
55 #include "YacsTrace.hxx"
58 using namespace YACS::HMI;
60 int Yacsgui::_oldStudyId = -1;
63 SalomeWrap_Module( "YACS" ) // default name
65 DEBTRACE("Yacsgui::Yacsgui");
68 _selectFromTree = false;
69 _studyContextMap.clear();
75 disconnect( getApp(), SIGNAL(studyClosed()), this, SLOT (onCleanOnExit()));
80 void Yacsgui::initialize( CAM_Application* app )
82 DEBTRACE("Yacsgui::initialize");
84 SalomeApp_Module::initialize( app );
86 QWidget* aParent = application()->desktop();
87 DEBTRACE(app << " " << application() << " " << application()->desktop() << " " << aParent);
89 SUIT_ResourceMgr* aResourceMgr = app->resourceMgr();
90 setResource(aResourceMgr);
92 _wrapper = new SuitWrapper(this);
93 _genericGui = new GenericGui(_wrapper, app->desktop());
95 if ( app && app->desktop() )
97 connect( app->desktop(), SIGNAL( windowActivated( SUIT_ViewWindow* ) ),
98 this, SLOT(onWindowActivated( SUIT_ViewWindow* )) );
100 connect( getApp()->objectBrowser()->treeView(),
101 SIGNAL( doubleClicked(const QModelIndex&) ),
103 SLOT ( onDblClick(const QModelIndex&) ) );
106 SIGNAL(studyClosed()),
108 SLOT (onCleanOnExit()));
110 _genericGui->createActions();
111 _genericGui->createMenus();
112 _genericGui->createTools();
113 this->studyActivated();
115 if (createSComponent()) updateObjBrowser();
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 )
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() )
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());
137 void Yacsgui::viewManagers( QStringList& list ) const
139 DEBTRACE("Yacsgui::viewManagers");
140 list.append( QxScene_Viewer::Type() );
143 bool Yacsgui::activateModule( SUIT_Study* theStudy )
145 DEBTRACE("Yacsgui::activateModule");
146 bool bOk = SalomeApp_Module::activateModule( theStudy );
148 QMainWindow* parent = application()->desktop();
149 if(Resource::dockWidgetPriority)
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);
156 setMenuShown( true );
157 setToolShown( true );
158 _genericGui->showDockWidgets(true);
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)
167 PyObject* result=PyObject_CallMethod( pluginsmanager, (char*)"initialize", (char*)"isss",1,"yacs","YACS",tr("YACS_PLUGINS").toStdString().c_str());
172 Py_XDECREF(pluginsmanager);
174 PyGILState_Release(gstate);
175 // end of YACS plugins loading
178 onWindowActivated(_currentSVW);
183 bool Yacsgui::deactivateModule( SUIT_Study* theStudy )
185 DEBTRACE("Yacsgui::deactivateModule");
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);
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 );
202 // --- Default windows
204 void Yacsgui::windows( QMap<int, int>& theMap ) const
206 DEBTRACE("Yacsgui::windows");
208 theMap.insert( SalomeApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea );
209 theMap.insert( SalomeApp_Application::WT_PyConsole, Qt::BottomDockWidgetArea );
212 QString Yacsgui::engineIOR() const
214 DEBTRACE("Yacsgui::engineIOR");
215 QString anEngineIOR = SalomeApp_Engine_i::EngineIORForComponent( "YACS", true ).c_str();
219 void Yacsgui::onDblClick(const QModelIndex& index)
221 DEBTRACE("Yacsgui::onDblClick");
222 DataObjectList dol =getApp()->objectBrowser()->getSelected();
223 if (dol.isEmpty()) return;
225 SalomeApp_DataObject* item = dynamic_cast<SalomeApp_DataObject*>(dol[0]);
228 DEBTRACE(item->name().toStdString());
229 SalomeWrap_DataModel *model = dynamic_cast<SalomeWrap_DataModel*>(dataModel());
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");
239 _selectFromTree = true;
240 viewWindow->setFocus();
241 _selectFromTree = false;
244 void Yacsgui::onWindowActivated( SUIT_ViewWindow* svw)
246 DEBTRACE("Yacsgui::onWindowActivated");
247 QxScene_ViewWindow* viewWindow = dynamic_cast<QxScene_ViewWindow*>(svw);
251 _currentSVW = 0; // switch to another module
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;
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* )) );
268 YASSERT(_genericGui);
269 _genericGui->switchContext(viewWindow);
270 _studyContextMap[getApp()->activeStudy()->id()] = QtGuiContext::getQtCurrent();
272 if (_selectFromTree) return;
273 SalomeWrap_DataModel *model = dynamic_cast<SalomeWrap_DataModel*>(dataModel());
275 model->setSelected(svw);
278 void Yacsgui::onWindowClosed( SUIT_ViewWindow* svw)
280 DEBTRACE("Yacsgui::onWindowClosed");
281 if ( svw && svw == _currentSVW )
285 void Yacsgui::onTryClose(bool &isClosed, QxScene_ViewWindow* window)
287 DEBTRACE("Yacsgui::onTryClose");
288 YASSERT(_genericGui);
289 isClosed = _genericGui->closeContext(window);
292 CAM_DataModel* Yacsgui::createDataModel()
294 return new Yacsgui_DataModel(this);
297 bool Yacsgui::createSComponent()
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;
305 // --- Find or create "YACS" SComponent in the study
307 _PTR(SComponent) aComponent = aStudy->FindComponent("YACS");
310 aComponent = aBuilder->NewComponent("YACS");
311 anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributeName");
312 aName = _PTR(AttributeName) (anAttr);
313 aName->SetValue(getApp()->moduleTitle("YACS").toStdString());
315 anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributePixMap");
316 _PTR(AttributePixMap) aPixmap(anAttr);
317 aPixmap->SetPixMap("ModuleYacs.png");
319 aBuilder->DefineComponentInstance(aComponent, engineIOR().toLatin1().constData());
326 void Yacsgui::setResource(SUIT_ResourceMgr* r)
328 DEBTRACE("Yacsgui::setResource");
329 _myresource = new Yacsgui_Resource(r);
330 _myresource->preferencesChanged();
333 void Yacsgui::createPreferences()
335 DEBTRACE("Yacsgui::createPreferences");
336 _myresource->createPreferences(this);
339 void Yacsgui::preferencesChanged( const QString& sect, const QString& name )
341 DEBTRACE("Yacsgui::preferencesChanged");
342 _myresource->preferencesChanged(sect, name);
343 if(name=="userCatalog")
345 _genericGui->getCatalogWidget()->addCatalogFromFile(Resource::userCatalog.toStdString());
349 void Yacsgui::studyActivated()
351 int newStudyId = getApp()->activeStudy()->id();
352 DEBTRACE("Yacsgui::studyActivated " << _oldStudyId << " " << newStudyId);
354 if (_oldStudyId != -1)
356 _studyContextMap[_oldStudyId] = QtGuiContext::getQtCurrent();
357 if (_studyContextMap.count(newStudyId))
359 DEBTRACE("switch to valid context " << QtGuiContext::getQtCurrent() << " " << _studyContextMap[newStudyId]);
360 QtGuiContext::setQtCurrent(_studyContextMap[newStudyId]);
364 DEBTRACE("no switch to null context");
367 _oldStudyId = newStudyId;
370 void Yacsgui::loadSchema(const std::string& filename,bool edit, bool arrangeLocalNodes)
372 _genericGui->loadSchema(filename,edit,arrangeLocalNodes);
375 void Yacsgui::onCleanOnExit()
378 _genericGui->onCleanOnExit();
382 // --- Export the module
386 YACS_EXPORT CAM_Module* createModule()
388 return new Yacsgui();