1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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
20 #include "ATOMSOLVGUI.h"
21 #include "ATOMSOLVGUI_DataModel.h"
22 #include "ATOMSOLVGUI_Displayer.h"
23 #include "ATOMSOLVGUI_Selection.h"
24 #include "ATOMSOLVGUI_TransparencyDlg.h"
26 #include <ATOMSOLV_version.h>
28 #include <SUIT_MessageBox.h>
29 #include <SUIT_ResourceMgr.h>
30 #include <SUIT_Session.h>
31 #include <SUIT_Desktop.h>
32 #include <SUIT_ViewManager.h>
34 #include <QtxPopupMgr.h>
36 #include <SalomeApp_Application.h>
37 #include <SalomeApp_Study.h>
39 #include <LightApp_DataOwner.h>
40 #include <LightApp_SelectionMgr.h>
41 #include <LightApp_Preferences.h>
43 #include <SVTK_ViewModel.h>
44 #include <SVTK_ViewWindow.h>
45 #include <SALOME_Actor.h>
47 #include <SALOME_LifeCycleCORBA.hxx>
50 #include <qinputdialog.h>
52 #include <qcolordialog.h>
53 #include <qstringlist.h>
56 #include <vtkActorCollection.h>
57 #include <vtkRenderer.h>
61 ATOMSOLV_ORB::ATOMSOLV_Gen_var ATOMSOLVGUI::myEngine = ATOMSOLV_ORB::ATOMSOLV_Gen::_nil();
64 ATOMSOLVGUI::ATOMSOLVGUI() :
65 SalomeApp_Module( "ATOMSOLV" ), // default name
66 LightApp_Module( "ATOMSOLV" )
70 // Initialize a reference to the module's engine
71 void ATOMSOLVGUI::InitATOMSOLVGen( SalomeApp_Application* app )
74 myEngine = ATOMSOLV_ORB::ATOMSOLV_Gen::_nil();
76 Engines::EngineComponent_var comp = app->lcc()->FindOrLoad_Component( "FactoryServer", "ATOMSOLV" );
77 ATOMSOLV_ORB::ATOMSOLV_Gen_ptr atomGen = ATOMSOLV_ORB::ATOMSOLV_Gen::_narrow(comp);
78 ASSERT( !CORBA::is_nil( atomGen ) );
83 // Gets an reference to the module's engine
84 ATOMSOLV_ORB::ATOMSOLV_Gen_var ATOMSOLVGUI::GetATOMSOLVGen()
86 if ( CORBA::is_nil( myEngine ) ) {
87 SUIT_Application* suitApp = SUIT_Session::session()->activeApplication();
88 SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( suitApp );
89 InitATOMSOLVGen( app );
94 // Module's initialization
95 void ATOMSOLVGUI::initialize( CAM_Application* app )
97 SalomeApp_Module::initialize( app );
99 InitATOMSOLVGen( dynamic_cast<SalomeApp_Application*>( app ) );
101 SUIT_Desktop* aParent = app->desktop();
102 SUIT_ResourceMgr* aResourceMgr = app->resourceMgr();
105 QPixmap aPixmap = aResourceMgr->loadPixmap( "ATOMSOLV",tr( "ICON_IMPORT_DATA" ) );
106 createAction( RetrieveData, tr( "TLT_RETRIEVE_DATA" ), QIcon( aPixmap ), tr( "MEN_RETRIEVE_DATA" ),
107 tr( "STS_RETRIEVE_DATA" ), 0, aParent, false, this, SLOT( OnRetrieveData() ) );
108 aPixmap = aResourceMgr->loadPixmap( "ATOMSOLV",tr( "ICON_PROCESS_DATA" ) );
109 createAction( ProcessData, tr( "TLT_PROCESS_DATA" ), QIcon( aPixmap ), tr( "MEN_PROCESS_DATA" ),
110 tr( "STS_PROCESS_DATA" ), 0, aParent, false, this, SLOT( OnProcessData() ) );
113 int aMenuId = createMenu( tr( "MEN_ATOMSOLV" ), -1, -1, 30 );
114 createMenu( RetrieveData, aMenuId, 10 );
115 createMenu( ProcessData, aMenuId, 10 );
118 int aToolId = createTool ( tr( "TOOL_ATOMSOLV" ) );
119 createTool( RetrieveData, aToolId );
120 createTool( ProcessData, aToolId );
122 // custom actions handled by displayer (display, erase, display mode, etc.)
123 aPixmap = aResourceMgr->loadPixmap( "ATOMSOLV",tr( "ICON_DISPLAY" ) );
124 createAction( Display, tr( "TLT_DISPLAY" ), QIcon( aPixmap ), tr( "MEN_DISPLAY" ),
125 tr( "STS_DISPLAY" ), 0, aParent, false, this, SLOT( OnDisplayerCommand() ) );
126 aPixmap = aResourceMgr->loadPixmap( "ATOMSOLV",tr( "ICON_ERASE" ) );
127 createAction( Erase, tr( "TLT_ERASE" ), QIcon( aPixmap ), tr( "MEN_ERASE" ),
128 tr( "STS_ERASE" ), 0, aParent, false, this, SLOT( OnDisplayerCommand() ) );
129 createAction( Shading, tr( "TLT_SHADING" ), QIcon(), tr( "MEN_SHADING" ),
130 tr( "STS_SHADING" ), 0, aParent, true, this, SLOT( OnDisplayerCommand() ) );
131 createAction( Wireframe, tr( "TLT_WIREFRAME" ), QIcon(), tr( "MEN_WIREFRAME" ),
132 tr( "STS_WIREFRAME" ), 0, aParent, true, this, SLOT( OnDisplayerCommand() ) );
133 createAction( PointsMode, tr( "TLT_POINTSMODE" ), QIcon(), tr( "MEN_POINTSMODE" ),
134 tr( "STS_POINTSMODE" ), 0, aParent, true, this, SLOT( OnDisplayerCommand() ) );
135 createAction( Color, tr( "TLT_COLOR" ), QIcon(), tr( "MEN_COLOR" ),
136 tr( "STS_COLOR" ), 0, aParent, false, this, SLOT( OnDisplayerCommand() ) );
137 createAction( Transparency, tr( "TLT_TRANSPARENCY" ), QIcon(), tr( "MEN_TRANSPARENCY" ),
138 tr( "STS_TRANSPARENCY" ), 0, aParent, false, this, SLOT( OnDisplayerCommand() ) );
140 QtxPopupMgr* mgr = popupMgr();
141 mgr->insert( action( Display ), -1, 0 );
142 mgr->insert( action( Erase ), -1, 0 );
143 int dispmodeId = mgr->insert( tr( "MEN_DISPLAY_MODE" ), -1, -1 ); // display mode menu
144 mgr->insert( action( PointsMode ), dispmodeId, -1 );
145 mgr->insert( action( Wireframe ), dispmodeId, -1 );
146 mgr->insert( action( Shading ), dispmodeId, -1 );
147 mgr->insert( action( Color ), -1, 0 );
148 mgr->insert( action( Transparency ), -1, 0 );
150 mgr->setRule( action( Display ), "true in $canBeDisplayed and activeModule='ATOMSOLV' and !isVisible" );
151 mgr->setRule( action( Erase ), "true in $canBeDisplayed and activeModule='ATOMSOLV' and isVisible" );
152 mgr->setRule( action( PointsMode ), "client='VTKViewer' and selcount>0 and isVisible" );
153 mgr->setRule( action( Wireframe ), "client='VTKViewer' and selcount>0 and isVisible" );
154 mgr->setRule( action( Shading ), "client='VTKViewer' and selcount>0 and isVisible" );
155 mgr->setRule( action( Color ), "client='VTKViewer' and selcount>0 and isVisible" );
156 mgr->setRule( action( Transparency ), "client='VTKViewer' and selcount>0 and isVisible" );
158 mgr->setRule( action( PointsMode ), "$displaymode={'Points'}", QtxPopupMgr::ToggleRule );
159 mgr->setRule( action( Wireframe ), "$displaymode={'Wireframe'}", QtxPopupMgr::ToggleRule );
160 mgr->setRule( action( Shading ), "$displaymode={'Surface'}", QtxPopupMgr::ToggleRule );
163 // Module's engine IOR
164 QString ATOMSOLVGUI::engineIOR() const
166 CORBA::String_var anIOR = getApp()->orb()->object_to_string( GetATOMSOLVGen() );
167 return QString( anIOR.in() );
170 // Module's activation
171 bool ATOMSOLVGUI::activateModule( SUIT_Study* theStudy )
173 bool bOk = SalomeApp_Module::activateModule( theStudy );
175 setMenuShown( true );
176 setToolShown( true );
181 // Module's deactivation
182 bool ATOMSOLVGUI::deactivateModule( SUIT_Study* theStudy )
184 setMenuShown( false );
185 setToolShown( false );
187 return SalomeApp_Module::deactivateModule( theStudy );
190 // Study closed callback
191 void ATOMSOLVGUI::studyClosed( SUIT_Study* theStudy )
194 ATOMSOLV_ORB::ATOMSOLV_Gen_var engine = GetATOMSOLVGen();
195 ATOMSOLV_ORB::TMoleculeList lst;
197 engine->setData( theStudy->id(), lst );
200 SalomeApp_Module::studyClosed( theStudy );
204 void ATOMSOLVGUI::windows( QMap<int, int>& theMap ) const
206 theMap.insert( SalomeApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea );
207 theMap.insert( SalomeApp_Application::WT_PyConsole, Qt::BottomDockWidgetArea );
210 // Default view managers
211 void ATOMSOLVGUI::viewManagers( QStringList& theViewMgrs ) const
213 theViewMgrs.append( SVTK_Viewer::Type() );
216 // Create custom data model
217 CAM_DataModel* ATOMSOLVGUI::createDataModel()
219 return new ATOMSOLVGUI_DataModel( this );
222 // Create custom selection object
223 LightApp_Selection* ATOMSOLVGUI::createSelection() const
225 return new ATOMSOLVGUI_Selection();
228 /*! Returns list of entities of selected objects. */
229 void ATOMSOLVGUI::selected( QStringList& entries, const bool multiple )
231 LightApp_SelectionMgr* mgr = getApp()->selectionMgr();
235 SUIT_DataOwnerPtrList anOwnersList;
236 mgr->selected( anOwnersList );
238 for ( int i = 0; i < anOwnersList.size(); i++ )
240 const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( anOwnersList[ i ].get() );
241 QStringList es = owner->entry().split( "_" );
242 if ( es.count() > 1 && es[ 0 ] == "ATOMSOLVGUI" && es[ 1 ] != "root" )
244 if ( !entries.contains( owner->entry() ) )
245 entries.append( owner->entry() );
253 void ATOMSOLVGUI::OnRetrieveData()
255 ATOMSOLV_ORB::ATOMSOLV_Gen_var engine = GetATOMSOLVGen();
256 SalomeApp_Application* app = getApp();
257 if ( !CORBA::is_nil( engine ) && app ) {
258 Engines::EngineComponent_var comp = app->lcc()->FindOrLoad_Component( "FactoryServerPy","ATOMGEN" );
259 ATOMGEN_ORB::ATOMGEN_Gen_var atomGen = ATOMGEN_ORB::ATOMGEN_Gen::_narrow( comp );
260 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
261 if ( !CORBA::is_nil( atomGen ) && appStudy ) {
262 const int studyID = appStudy->id();
263 // in case current study is not loaded by ATOMGEN component - call Load on it
264 if ( _PTR( Study ) studyDS = appStudy->studyDS() ) {
265 if ( _PTR( SComponent ) atomGenSComp = studyDS->FindComponent( "ATOMGEN" ) ) {
266 _PTR( StudyBuilder ) builder = studyDS->NewBuilder();
267 std::string atomGenIOR = app->orb()->object_to_string( atomGen );
268 builder->LoadWith( atomGenSComp, atomGenIOR );
272 // retrieve data from ATOMGEN
273 ATOMGEN_ORB::MoleculeList_var inData = atomGen->getData( studyID );
275 // "convert" Molecules to TMolecules, set default temperature of '0'
276 const int n = inData->length();
277 ATOMSOLV_ORB::TMoleculeList_var outData = new ATOMSOLV_ORB::TMoleculeList();
278 outData->length( n );
279 for ( int i = 0; i < n; i++ ) {
280 ATOMSOLV_ORB::TMolecule_var tmol = new ATOMSOLV_ORB::TMolecule();
281 tmol->molecule = ATOMGEN_ORB::Molecule::_duplicate( inData[i] );
282 tmol->temperature = 0;
286 engine->setData( studyID, outData );
288 app->updateObjectBrowser();
294 void ATOMSOLVGUI::OnProcessData()
296 ATOMSOLV_ORB::ATOMSOLV_Gen_var engine = GetATOMSOLVGen();
297 SalomeApp_Application* app = getApp();
298 if ( !CORBA::is_nil( engine ) && app ) {
299 if ( const int studyID = app->activeStudy()->id() > 0 ) {
300 // call processData on engine
301 engine->processData( studyID );
302 // update data objects in object browser
304 // redisplay atoms that are already displayed (as their temperature changed..)
305 ViewManagerList vmans;
306 getApp()->viewManagers( SVTK_Viewer::Type(), vmans );
307 for ( QList<SUIT_ViewManager*>::iterator it = vmans.begin(); it != vmans.end(); ++it ) {
308 QVector<SUIT_ViewWindow*> views = (*it)->getViews();
309 for ( int i = 0; i < views.size(); i++ ) {
310 if ( SVTK_ViewWindow* svtkView = dynamic_cast<SVTK_ViewWindow*>( views[ i ] ) ) {
311 vtkActorCollection* actors = svtkView->getRenderer()->GetActors();
312 actors->InitTraversal();
313 while ( vtkActor* actor = actors->GetNextActor() )
314 if ( SALOME_Actor* salomeActor = SALOME_Actor::SafeDownCast( actor ) )
315 if ( salomeActor->hasIO() ) {
316 // printf( " -- must redisplay actor salomeActor: %s\n", salomeActor->getIO()->getEntry() );
317 ATOMSOLVGUI_Displayer().updateActor( salomeActor );
327 void ATOMSOLVGUI::OnDisplayerCommand()
329 const QObject* obj = sender();
330 if ( obj && obj->inherits( "QAction" ) ) {
331 const int id = actionId ( (QAction*)obj );
335 selected ( entries, true );
336 ATOMSOLVGUI_Displayer d;
337 for ( QStringList::const_iterator it = entries.begin(), last = entries.end(); it != last; it++ )
338 d.Display( it->toLatin1(), /*updateviewer=*/false, 0 );
343 selected ( entries, true );
344 ATOMSOLVGUI_Displayer d;
345 for ( QStringList::const_iterator it = entries.begin(), last = entries.end(); it != last; it++ )
346 d.Erase( *it, /*forced=*/true, /*updateViewer=*/false, 0 );
351 selected ( entries, true );
352 ATOMSOLVGUI_Displayer().setDisplayMode( entries, "Surface" );
356 selected ( entries, true );
357 ATOMSOLVGUI_Displayer().setDisplayMode( entries, "Wireframe" );
361 selected ( entries, true );
362 ATOMSOLVGUI_Displayer().setDisplayMode( entries, "Points" );
366 selected ( entries, true );
367 QColor initialColor( "white" );
368 if ( entries.count() == 1 )
369 initialColor = ATOMSOLVGUI_Displayer().getColor( entries[ 0 ] );
370 QColor color = QColorDialog::getColor( initialColor, getApp()->desktop() );
371 if ( color.isValid() )
372 ATOMSOLVGUI_Displayer().setColor( entries, color );
374 case Transparency : {
376 selected ( entries, true );
377 ATOMSOLVGUI_TransparencyDlg( getApp()->desktop(), entries ).exec();
379 default: printf( "ERROR: Action with ID = %d was not found in ATOMSOLVGUI\n", id ); break;
384 // returns a custom displayer object
385 LightApp_Displayer* ATOMSOLVGUI::displayer()
387 return new ATOMSOLVGUI_Displayer();
390 // create preferences for ATOMSOLV component
391 void ATOMSOLVGUI::createPreferences()
393 int tabId = addPreference( tr( "ATOMSOLV_PREFERENCES" ) );
394 int groupId = addPreference( tr( "PRESENTATION_PREF_GROUP" ), tabId );
396 setPreferenceProperty( groupId, "columns", 1 );
398 // Representation mode preference
399 int dispModeId = addPreference( tr( "DISPLAY_MODE_PREF" ), groupId,
400 LightApp_Preferences::Selector, "ATOMSOLV", "Representation" );
401 QList<QVariant> intDispModes;
402 QStringList strDispModes;
403 intDispModes.append( 0 ); strDispModes.append( tr( "MEN_POINTSMODE" ) );
404 intDispModes.append( 1 ); strDispModes.append( tr( "MEN_WIREFRAME" ) );
405 intDispModes.append( 2 ); strDispModes.append( tr( "MEN_SHADING" ) );
407 setPreferenceProperty( dispModeId, "strings", strDispModes );
408 setPreferenceProperty( dispModeId, "indexes", intDispModes );
411 int radisusId = addPreference( tr( "RADIUS_PREF" ), groupId,
412 LightApp_Preferences::DblSpin, "ATOMSOLV", "Radius" );
413 setPreferenceProperty( radisusId, "min", .001 );
414 setPreferenceProperty( radisusId, "max", 1000 );
415 setPreferenceProperty( radisusId, "precision", 3 );
418 // preferences of changed: update the corresponding values
419 void ATOMSOLVGUI::preferencesChanged( const QString& group, const QString& param )
421 if ( group == "ATOMSOLV" && param == "Representation" )
422 ATOMSOLVGUI_Displayer::setDefaultRepresentation( getApp()->resourceMgr()->integerValue( group, param ) );
423 else if ( group == "ATOMSOLV" && param == "Radius" )
424 ATOMSOLVGUI_Displayer::setDefaultRadius( getApp()->resourceMgr()->doubleValue( group, param ) );
430 CAM_Module* createModule()
432 return new ATOMSOLVGUI();
435 char* getModuleVersion()
437 return (char*)ATOMSOLV_VERSION_STR;