1 // Copyright (C) 2006-2021 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;
62 SalomeWrap_Module( "YACS" ) // default name
64 DEBTRACE("Yacsgui::Yacsgui");
67 _selectFromTree = false;
74 disconnect( getApp(), SIGNAL(studyClosed()), this, SLOT (onCleanOnExit()));
79 void Yacsgui::initialize( CAM_Application* app )
81 DEBTRACE("Yacsgui::initialize");
83 SalomeApp_Module::initialize( app );
85 QWidget* aParent = application()->desktop();
86 DEBTRACE(app << " " << application() << " " << application()->desktop() << " " << aParent);
88 SUIT_ResourceMgr* aResourceMgr = app->resourceMgr();
89 setResource(aResourceMgr);
91 _wrapper = new SuitWrapper(this);
92 _genericGui = new GenericGui(_wrapper, app->desktop());
94 if ( app && app->desktop() )
96 connect( app->desktop(), SIGNAL( windowActivated( SUIT_ViewWindow* ) ),
97 this, SLOT(onWindowActivated( SUIT_ViewWindow* )) );
100 SIGNAL(objectDoubleClicked( SUIT_DataObject* )),
102 SLOT (onDblClick( SUIT_DataObject* )));
105 SIGNAL(studyClosed()),
107 SLOT (onCleanOnExit()));
109 _genericGui->createActions();
110 _genericGui->createMenus();
111 _genericGui->createTools();
112 _studyContext = QtGuiContext::getQtCurrent();
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() );
119 bool aLocked = (_PTR(AttributeStudyProperties)(aStudy->studyDS()->GetProperties()))->IsLocked();
121 SUIT_MessageBox::warning ( app->desktop(), QObject::tr("WRN_WARNING"), QObject::tr("WRN_STUDY_LOCKED") );
124 if (createSComponent()) updateObjBrowser();
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 )
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() )
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());
148 void Yacsgui::viewManagers( QStringList& list ) const
150 DEBTRACE("Yacsgui::viewManagers");
151 list.append( QxScene_Viewer::Type() );
154 bool Yacsgui::activateModule( SUIT_Study* theStudy )
156 DEBTRACE("Yacsgui::activateModule");
157 bool bOk = SalomeApp_Module::activateModule( theStudy );
159 QMainWindow* parent = application()->desktop();
160 if(Resource::dockWidgetPriority)
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);
167 setMenuShown( true );
168 setToolShown( true );
169 _genericGui->showDockWidgets(true);
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)
178 PyObject* result=PyObject_CallMethod( pluginsmanager, (char*)"initialize", (char*)"isss",1,"yacs","YACS",tr("YACS_PLUGINS").toStdString().c_str());
183 Py_XDECREF(pluginsmanager);
185 PyGILState_Release(gstate);
186 // end of YACS plugins loading
189 onWindowActivated(_currentSVW);
194 bool Yacsgui::deactivateModule( SUIT_Study* theStudy )
196 DEBTRACE("Yacsgui::deactivateModule");
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);
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 );
213 // --- Default windows
215 void Yacsgui::windows( QMap<int, int>& theMap ) const
217 DEBTRACE("Yacsgui::windows");
219 theMap.insert( SalomeApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea );
220 theMap.insert( SalomeApp_Application::WT_PyConsole, Qt::BottomDockWidgetArea );
223 QString Yacsgui::engineIOR() const
225 DEBTRACE("Yacsgui::engineIOR");
226 QString anEngineIOR = SalomeApp_Engine_i::EngineIORForComponent( "YACS", true ).c_str();
230 void Yacsgui::onDblClick(SUIT_DataObject*)
232 DEBTRACE("Yacsgui::onDblClick");
233 DataObjectList dol =getApp()->objectBrowser()->getSelected();
234 if (dol.isEmpty()) return;
236 SalomeApp_DataObject* item = dynamic_cast<SalomeApp_DataObject*>(dol[0]);
239 DEBTRACE(item->name().toStdString());
240 SalomeWrap_DataModel *model = dynamic_cast<SalomeWrap_DataModel*>(dataModel());
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");
250 _selectFromTree = true;
251 viewWindow->setFocus();
252 _selectFromTree = false;
255 void Yacsgui::onWindowActivated( SUIT_ViewWindow* svw)
257 DEBTRACE("Yacsgui::onWindowActivated");
258 QxScene_ViewWindow* viewWindow = dynamic_cast<QxScene_ViewWindow*>(svw);
262 _currentSVW = 0; // switch to another module
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;
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* )) );
279 YASSERT(_genericGui);
280 _genericGui->switchContext(viewWindow);
281 _studyContext = QtGuiContext::getQtCurrent();
283 if (_selectFromTree) return;
284 SalomeWrap_DataModel *model = dynamic_cast<SalomeWrap_DataModel*>(dataModel());
286 model->setSelected(svw);
289 void Yacsgui::onWindowClosed( SUIT_ViewWindow* svw)
291 DEBTRACE("Yacsgui::onWindowClosed");
292 if ( svw && svw == _currentSVW )
296 void Yacsgui::onTryClose(bool &isClosed, QxScene_ViewWindow* window)
298 DEBTRACE("Yacsgui::onTryClose");
299 YASSERT(_genericGui);
300 isClosed = _genericGui->closeContext(window);
303 CAM_DataModel* Yacsgui::createDataModel()
305 return new Yacsgui_DataModel(this);
308 bool Yacsgui::createSComponent()
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;
316 // --- Find or create "YACS" SComponent in the study
318 _PTR(SComponent) aComponent = aStudy->FindComponent("YACS");
321 aComponent = aBuilder->NewComponent("YACS");
322 anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributeName");
323 aName = _PTR(AttributeName) (anAttr);
324 aName->SetValue(getApp()->moduleTitle("YACS").toStdString());
326 anAttr = aBuilder->FindOrCreateAttribute(aComponent, "AttributePixMap");
327 _PTR(AttributePixMap) aPixmap(anAttr);
328 aPixmap->SetPixMap("ModuleYacs.png");
330 aBuilder->DefineComponentInstance(aComponent, engineIOR().toLatin1().constData());
337 void Yacsgui::setResource(SUIT_ResourceMgr* r)
339 DEBTRACE("Yacsgui::setResource");
340 _myresource = new Yacsgui_Resource(r);
341 _myresource->preferencesChanged();
344 void Yacsgui::createPreferences()
346 DEBTRACE("Yacsgui::createPreferences");
347 _myresource->createPreferences(this);
350 void Yacsgui::preferencesChanged( const QString& sect, const QString& name )
352 DEBTRACE("Yacsgui::preferencesChanged");
353 _myresource->preferencesChanged(sect, name);
354 if(name=="userCatalog")
356 _genericGui->getCatalogWidget()->addCatalogFromFile(Resource::userCatalog.toStdString());
360 void Yacsgui::loadSchema(const std::string& filename,bool edit, bool arrangeLocalNodes)
362 _genericGui->loadSchema(filename,edit,arrangeLocalNodes);
365 void Yacsgui::onCleanOnExit()
368 _genericGui->onCleanOnExit();
372 // --- Export the module
376 YACS_EXPORT CAM_Module* createModule()
378 return new Yacsgui();