1 // Copyright (C) 2006-2015 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 // VSR 23/10/2014: note that this is not a good way to create SComponent from this point
116 // as initialize() method can be potentially called when there's no yet open study;
117 // this is better to do in activateModule()
118 SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
120 bool aLocked = (_PTR(AttributeStudyProperties)(aStudy->studyDS()->GetProperties()))->IsLocked();
122 SUIT_MessageBox::warning ( app->desktop(), QObject::tr("WRN_WARNING"), QObject::tr("WRN_STUDY_LOCKED") );
125 if (createSComponent()) updateObjBrowser();
129 // Load SALOME module catalogs
130 QStringList appModules;
131 app->modules(appModules,false);
132 for ( QStringList::const_iterator it = appModules.begin(); it != appModules.end(); ++it )
135 QString modName = app->moduleName( aModule ); // module name
136 if ( modName.isEmpty() ) modName = aModule;
137 QString rootDir = QString( "%1_ROOT_DIR" ).arg( modName ); // module root dir variable
138 QString modDir = getenv( rootDir.toLatin1().constData() ); // module root dir
139 if ( !modDir.isEmpty() )
141 QStringList cataLst = QStringList() << modDir << "share" << "salome" << "resources" << modName.toLower() << modName+"SchemaCatalog.xml";
142 QString cataFile = cataLst.join( QDir::separator() ); // YACS module catalog
143 if ( QFile::exists( cataFile ) )
144 _genericGui->getCatalogWidget()->addCatalogFromFile(cataFile.toStdString());
149 void Yacsgui::viewManagers( QStringList& list ) const
151 DEBTRACE("Yacsgui::viewManagers");
152 list.append( QxScene_Viewer::Type() );
155 bool Yacsgui::activateModule( SUIT_Study* theStudy )
157 DEBTRACE("Yacsgui::activateModule");
158 bool bOk = SalomeApp_Module::activateModule( theStudy );
160 QMainWindow* parent = application()->desktop();
161 if(Resource::dockWidgetPriority)
163 parent->setCorner(Qt::TopLeftCorner, Qt::LeftDockWidgetArea);
164 parent->setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea);
165 parent->setCorner(Qt::TopRightCorner, Qt::RightDockWidgetArea);
166 parent->setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea);
168 setMenuShown( true );
169 setToolShown( true );
170 _genericGui->showDockWidgets(true);
172 // import Python module that manages YACS plugins (need to be here because SalomePyQt API uses active module)
173 PyGILState_STATE gstate = PyGILState_Ensure();
174 PyObject* pluginsmanager=PyImport_ImportModule((char*)"salome_pluginsmanager");
175 if(pluginsmanager==NULL)
179 PyObject* result=PyObject_CallMethod( pluginsmanager, (char*)"initialize", (char*)"isss",1,"yacs","YACS",tr("YACS_PLUGINS").toStdString().c_str());
184 Py_XDECREF(pluginsmanager);
186 PyGILState_Release(gstate);
187 // end of YACS plugins loading
190 onWindowActivated(_currentSVW);
195 bool Yacsgui::deactivateModule( SUIT_Study* theStudy )
197 DEBTRACE("Yacsgui::deactivateModule");
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);
205 setMenuShown( false );
206 setToolShown( false );
207 _genericGui->showDockWidgets(false);
208 QtGuiContext *context = QtGuiContext::getQtCurrent();
209 _studyContextMap[theStudy->id()] = context;
210 DEBTRACE("_studyContextMap[theStudy] " << theStudy << " " << context);
211 return SalomeApp_Module::deactivateModule( theStudy );
214 // --- Default windows
216 void Yacsgui::windows( QMap<int, int>& theMap ) const
218 DEBTRACE("Yacsgui::windows");
220 theMap.insert( SalomeApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea );
221 theMap.insert( SalomeApp_Application::WT_PyConsole, Qt::BottomDockWidgetArea );
224 QString Yacsgui::engineIOR() const
226 DEBTRACE("Yacsgui::engineIOR");
227 QString anEngineIOR = SalomeApp_Engine_i::EngineIORForComponent( "YACS", true ).c_str();
231 void Yacsgui::onDblClick(const QModelIndex& index)
233 DEBTRACE("Yacsgui::onDblClick");
234 DataObjectList dol =getApp()->objectBrowser()->getSelected();
235 if (dol.isEmpty()) return;
237 SalomeApp_DataObject* item = dynamic_cast<SalomeApp_DataObject*>(dol[0]);
240 DEBTRACE(item->name().toStdString());
241 SalomeWrap_DataModel *model = dynamic_cast<SalomeWrap_DataModel*>(dataModel());
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");
251 _selectFromTree = true;
252 viewWindow->setFocus();
253 _selectFromTree = false;
256 void Yacsgui::onWindowActivated( SUIT_ViewWindow* svw)
258 DEBTRACE("Yacsgui::onWindowActivated");
259 QxScene_ViewWindow* viewWindow = dynamic_cast<QxScene_ViewWindow*>(svw);
263 _currentSVW = 0; // switch to another module
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;
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* )) );
280 YASSERT(_genericGui);
281 _genericGui->switchContext(viewWindow);
282 _studyContextMap[getApp()->activeStudy()->id()] = QtGuiContext::getQtCurrent();
284 if (_selectFromTree) return;
285 SalomeWrap_DataModel *model = dynamic_cast<SalomeWrap_DataModel*>(dataModel());
287 model->setSelected(svw);
290 void Yacsgui::onWindowClosed( SUIT_ViewWindow* svw)
292 DEBTRACE("Yacsgui::onWindowClosed");
293 if ( svw && svw == _currentSVW )
297 void Yacsgui::onTryClose(bool &isClosed, QxScene_ViewWindow* window)
299 DEBTRACE("Yacsgui::onTryClose");
300 YASSERT(_genericGui);
301 isClosed = _genericGui->closeContext(window);
304 CAM_DataModel* Yacsgui::createDataModel()
306 return new Yacsgui_DataModel(this);
309 bool Yacsgui::createSComponent()
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;
317 // --- Find or create "YACS" SComponent in the study
319 _PTR(SComponent) aComponent = aStudy->FindComponent("YACS");
322 aComponent = aBuilder->NewComponent("YACS");
323 anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributeName");
324 aName = _PTR(AttributeName) (anAttr);
325 aName->SetValue(getApp()->moduleTitle("YACS").toStdString());
327 anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributePixMap");
328 _PTR(AttributePixMap) aPixmap(anAttr);
329 aPixmap->SetPixMap("ModuleYacs.png");
331 aBuilder->DefineComponentInstance(aComponent, engineIOR().toLatin1().constData());
338 void Yacsgui::setResource(SUIT_ResourceMgr* r)
340 DEBTRACE("Yacsgui::setResource");
341 _myresource = new Yacsgui_Resource(r);
342 _myresource->preferencesChanged();
345 void Yacsgui::createPreferences()
347 DEBTRACE("Yacsgui::createPreferences");
348 _myresource->createPreferences(this);
351 void Yacsgui::preferencesChanged( const QString& sect, const QString& name )
353 DEBTRACE("Yacsgui::preferencesChanged");
354 _myresource->preferencesChanged(sect, name);
355 if(name=="userCatalog")
357 _genericGui->getCatalogWidget()->addCatalogFromFile(Resource::userCatalog.toStdString());
361 void Yacsgui::studyActivated()
363 int newStudyId = getApp()->activeStudy()->id();
364 DEBTRACE("Yacsgui::studyActivated " << _oldStudyId << " " << newStudyId);
366 if (_oldStudyId != -1)
368 _studyContextMap[_oldStudyId] = QtGuiContext::getQtCurrent();
369 if (_studyContextMap.count(newStudyId))
371 DEBTRACE("switch to valid context " << QtGuiContext::getQtCurrent() << " " << _studyContextMap[newStudyId]);
372 QtGuiContext::setQtCurrent(_studyContextMap[newStudyId]);
376 DEBTRACE("no switch to null context");
379 _oldStudyId = newStudyId;
382 void Yacsgui::loadSchema(const std::string& filename,bool edit, bool arrangeLocalNodes)
384 _genericGui->loadSchema(filename,edit,arrangeLocalNodes);
387 void Yacsgui::onCleanOnExit()
390 _genericGui->onCleanOnExit();
394 // --- Export the module
398 YACS_EXPORT CAM_Module* createModule()
400 return new Yacsgui();