Salome HOME
72206871e2168419de017e95c2981c4229aec8b8
[samples/atomsolv.git] / src / ATOMSOLVGUI / ATOMSOLVGUI.cxx
1 // Copyright (C) 2007-2023  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "ATOMSOLVGUI.h"
21 #include "ATOMSOLVGUI_DataModel.h"
22 #include "ATOMSOLVGUI_Displayer.h"
23 #include "ATOMSOLVGUI_Selection.h"
24 #include "ATOMSOLVGUI_TransparencyDlg.h"
25
26 #include <ATOMSOLV_version.h>
27
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>
33
34 #include <QtxPopupMgr.h>
35
36 #include <SalomeApp_Application.h>
37 #include <SalomeApp_Study.h>
38
39 #include "SALOME_NamingService_Abstract.hxx"
40 #include "SALOME_KernelServices.hxx"
41
42 #include <LightApp_DataOwner.h>
43 #include <LightApp_SelectionMgr.h>
44 #include <LightApp_Preferences.h>
45
46 #include <SVTK_ViewModel.h>
47 #include <SVTK_ViewWindow.h>
48 #include <SALOME_Actor.h>
49
50 #include <SALOME_LifeCycleCORBA.hxx>
51
52 // QT includes
53 #include <qinputdialog.h>
54 #include <qaction.h>
55 #include <qcolordialog.h>
56 #include <qstringlist.h>
57
58 // VTK includes
59 #include <vtkActorCollection.h>
60 #include <vtkRenderer.h>
61
62 #include <utilities.h>
63
64 ATOMSOLV_ORB::ATOMSOLV_Gen_var ATOMSOLVGUI::myEngine = ATOMSOLV_ORB::ATOMSOLV_Gen::_nil();
65
66 // Constructor
67 ATOMSOLVGUI::ATOMSOLVGUI() :
68   SalomeApp_Module( "ATOMSOLV" )
69 {
70 }
71
72 // Initialize a reference to the module's engine
73 void ATOMSOLVGUI::InitATOMSOLVGen( SalomeApp_Application* app )
74 {
75   if ( !app )
76     myEngine = ATOMSOLV_ORB::ATOMSOLV_Gen::_nil();
77   else {
78     SALOME_NamingService_Abstract *ns = SalomeApp_Application::namingService();
79     Engines::EngineComponent_var comp = app->lcc()->FindOrLoad_Component( "FactoryServer", "ATOMSOLV" );
80     ATOMSOLV_ORB::ATOMSOLV_Gen_ptr atomGen = ATOMSOLV_ORB::ATOMSOLV_Gen::_narrow(comp);
81     ASSERT( !CORBA::is_nil( atomGen ) );
82     myEngine = atomGen;
83   }
84 }
85
86 // Gets an reference to the module's engine
87 ATOMSOLV_ORB::ATOMSOLV_Gen_var ATOMSOLVGUI::GetATOMSOLVGen()
88 {
89   if ( CORBA::is_nil( myEngine ) ) {
90     SUIT_Application* suitApp = SUIT_Session::session()->activeApplication();
91     SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( suitApp );
92     InitATOMSOLVGen( app );
93   }
94   return myEngine;
95 }
96
97 // Module's initialization
98 void ATOMSOLVGUI::initialize( CAM_Application* app )
99 {
100   SalomeApp_Module::initialize( app );
101
102   InitATOMSOLVGen( dynamic_cast<SalomeApp_Application*>( app ) );
103
104   SUIT_Desktop* aParent = app->desktop();
105   SUIT_ResourceMgr* aResourceMgr = app->resourceMgr();
106
107   // create actions
108   QPixmap aPixmap = aResourceMgr->loadPixmap( "ATOMSOLV",tr( "ICON_IMPORT_DATA" ) );
109   createAction( RetrieveData, tr( "TLT_RETRIEVE_DATA" ), QIcon( aPixmap ), tr( "MEN_RETRIEVE_DATA" ),
110                 tr( "STS_RETRIEVE_DATA" ), 0, aParent, false, this, SLOT( OnRetrieveData() ) );
111   aPixmap = aResourceMgr->loadPixmap( "ATOMSOLV",tr( "ICON_PROCESS_DATA" ) );
112   createAction( ProcessData, tr( "TLT_PROCESS_DATA" ), QIcon( aPixmap ), tr( "MEN_PROCESS_DATA" ),
113                 tr( "STS_PROCESS_DATA" ), 0, aParent, false, this, SLOT( OnProcessData() ) );
114
115   // create menus
116   int aMenuId = createMenu( tr( "MEN_ATOMSOLV" ), -1, -1, 30 );
117   createMenu( RetrieveData, aMenuId, 10 );
118   createMenu( ProcessData, aMenuId, 10 );
119
120   // create toolbars
121   int aToolId = createTool ( tr( "TOOL_ATOMSOLV" ), QString( "AtomSolv" ) );
122   createTool( RetrieveData, aToolId );
123   createTool( ProcessData, aToolId );
124
125   // custom actions handled by displayer (display, erase, display mode, etc.)
126   aPixmap = aResourceMgr->loadPixmap( "ATOMSOLV",tr( "ICON_DISPLAY" ) );
127   createAction( Display, tr( "TLT_DISPLAY" ), QIcon( aPixmap ), tr( "MEN_DISPLAY" ),
128                 tr( "STS_DISPLAY" ), 0, aParent, false, this, SLOT( OnDisplayerCommand() ) );
129   aPixmap = aResourceMgr->loadPixmap( "ATOMSOLV",tr( "ICON_ERASE" ) );
130   createAction( Erase, tr( "TLT_ERASE" ), QIcon( aPixmap ), tr( "MEN_ERASE" ),
131                 tr( "STS_ERASE" ), 0, aParent, false, this, SLOT( OnDisplayerCommand() ) );
132   createAction( Shading, tr( "TLT_SHADING" ), QIcon(), tr( "MEN_SHADING" ),
133                 tr( "STS_SHADING" ), 0, aParent, true, this, SLOT( OnDisplayerCommand() ) );
134   createAction( Wireframe, tr( "TLT_WIREFRAME" ), QIcon(), tr( "MEN_WIREFRAME" ),
135                 tr( "STS_WIREFRAME" ), 0, aParent, true, this, SLOT( OnDisplayerCommand() ) );
136   createAction( PointsMode, tr( "TLT_POINTSMODE" ), QIcon(), tr( "MEN_POINTSMODE" ),
137                 tr( "STS_POINTSMODE" ), 0, aParent, true, this, SLOT( OnDisplayerCommand() ) );
138   createAction( Color, tr( "TLT_COLOR" ), QIcon(), tr( "MEN_COLOR" ),
139                 tr( "STS_COLOR" ), 0, aParent, false, this, SLOT( OnDisplayerCommand() ) );
140   createAction( Transparency, tr( "TLT_TRANSPARENCY" ), QIcon(), tr( "MEN_TRANSPARENCY" ),
141                 tr( "STS_TRANSPARENCY" ), 0, aParent, false, this, SLOT( OnDisplayerCommand() ) );
142
143   QtxPopupMgr* mgr = popupMgr();
144   mgr->insert( action( Display ), -1, 0 );
145   mgr->insert( action( Erase ), -1, 0 );
146   int dispmodeId = mgr->insert(  tr( "MEN_DISPLAY_MODE" ), -1, -1 ); // display mode menu
147   mgr->insert( action(  PointsMode ), dispmodeId, -1 );
148   mgr->insert( action(  Wireframe ), dispmodeId, -1 );
149   mgr->insert( action(  Shading ), dispmodeId, -1 );
150   mgr->insert( action( Color ), -1, 0 );
151   mgr->insert( action( Transparency ), -1, 0 );
152
153   mgr->setRule( action( Display ), "true in $canBeDisplayed and activeModule='ATOMSOLV' and !isVisible" );
154   mgr->setRule( action( Erase ), "true in $canBeDisplayed and activeModule='ATOMSOLV' and isVisible" );
155   mgr->setRule( action( PointsMode ), "client='VTKViewer' and selcount>0 and isVisible" );
156   mgr->setRule( action( Wireframe ), "client='VTKViewer' and selcount>0 and isVisible" );
157   mgr->setRule( action( Shading ), "client='VTKViewer' and selcount>0 and isVisible" );
158   mgr->setRule( action( Color ), "client='VTKViewer' and selcount>0 and isVisible" );
159   mgr->setRule( action( Transparency ), "client='VTKViewer' and selcount>0 and isVisible" );
160
161   mgr->setRule( action( PointsMode ), "$displaymode={'Points'}", QtxPopupMgr::ToggleRule );
162   mgr->setRule( action( Wireframe ), "$displaymode={'Wireframe'}", QtxPopupMgr::ToggleRule );
163   mgr->setRule( action( Shading ), "$displaymode={'Surface'}", QtxPopupMgr::ToggleRule );
164 }
165
166 // Module's engine IOR
167 QString ATOMSOLVGUI::engineIOR() const
168 {
169   CORBA::String_var anIOR = getApp()->orb()->object_to_string( GetATOMSOLVGen() );
170   return QString( anIOR.in() );
171 }
172
173 // Module's activation
174 bool ATOMSOLVGUI::activateModule( SUIT_Study* theStudy )
175 {
176   bool bOk = SalomeApp_Module::activateModule( theStudy );
177
178   setMenuShown( true );
179   setToolShown( true );
180
181   return bOk;
182 }
183
184 // Module's deactivation
185 bool ATOMSOLVGUI::deactivateModule( SUIT_Study* theStudy )
186 {
187   setMenuShown( false );
188   setToolShown( false );
189
190   return SalomeApp_Module::deactivateModule( theStudy );
191 }
192
193 // Study closed callback
194 void ATOMSOLVGUI::studyClosed( SUIT_Study* theStudy )
195 {
196   if ( theStudy ) {
197     ATOMSOLV_ORB::ATOMSOLV_Gen_var engine = GetATOMSOLVGen();
198     ATOMSOLV_ORB::TMoleculeList lst;
199     lst.length( 0 );
200     engine->setData( lst );
201   }
202
203   SalomeApp_Module::studyClosed( theStudy );
204 }
205
206 // Default windows
207 void ATOMSOLVGUI::windows( QMap<int, int>& theMap ) const
208 {
209   theMap.insert( SalomeApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea );
210 #ifndef DISABLE_PYCONSOLE
211   theMap.insert( SalomeApp_Application::WT_PyConsole,     Qt::BottomDockWidgetArea );
212 #endif
213 }
214
215 // Default view managers
216 void ATOMSOLVGUI::viewManagers( QStringList& theViewMgrs ) const
217 {
218   theViewMgrs.append( SVTK_Viewer::Type() );
219 }
220
221 // Create custom data model
222 CAM_DataModel* ATOMSOLVGUI::createDataModel()
223 {
224   return new ATOMSOLVGUI_DataModel( this );
225 }
226
227 // Create custom selection object
228 LightApp_Selection* ATOMSOLVGUI::createSelection() const
229 {
230   return new ATOMSOLVGUI_Selection();
231 }
232
233 /*! Returns list of entities of selected objects. */
234 void ATOMSOLVGUI::selected( QStringList& entries, const bool multiple )
235 {
236   LightApp_SelectionMgr* mgr = getApp()->selectionMgr();
237   if( !mgr )
238     return;
239
240   SUIT_DataOwnerPtrList anOwnersList;
241   mgr->selected( anOwnersList );
242
243   for ( int i = 0; i < anOwnersList.size(); i++ )
244   {
245     const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( anOwnersList[ i ].get() );
246     QStringList es = owner->entry().split( "_" );
247     if ( es.count() > 1 && es[ 0 ] == "ATOMSOLVGUI" && es[ 1 ] != "root" )
248     {
249       if ( !entries.contains( owner->entry() ) )
250         entries.append( owner->entry() );
251       if( !multiple )
252         break;
253     }
254   }
255 }
256
257 // Action slot
258 void ATOMSOLVGUI::OnRetrieveData()
259 {
260   ATOMSOLV_ORB::ATOMSOLV_Gen_var engine = GetATOMSOLVGen();
261   SalomeApp_Application* app = getApp();
262   if ( !CORBA::is_nil( engine ) && app ) {
263     Engines::EngineComponent_var comp = app->lcc()->FindOrLoad_Component( "FactoryServer","ATOMGEN" );
264     ATOMGEN_ORB::ATOMGEN_Gen_var atomGen = ATOMGEN_ORB::ATOMGEN_Gen::_narrow( comp );
265     SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
266     if ( !CORBA::is_nil( atomGen ) && appStudy ) {
267       // in case current study is not loaded by ATOMGEN component - call Load on it
268       if ( _PTR( Study ) studyDS = appStudy->studyDS() ) {
269         if ( _PTR( SComponent ) atomGenSComp = studyDS->FindComponent( "ATOMGEN" ) ) {
270           _PTR( StudyBuilder ) builder = studyDS->NewBuilder();
271           std::string atomGenIOR = app->orb()->object_to_string( atomGen );
272           builder->LoadWith( atomGenSComp, atomGenIOR );
273         }
274       }
275
276       // retrieve data from ATOMGEN
277       ATOMGEN_ORB::MoleculeList_var inData = atomGen->getData();
278
279       // "convert" Molecules to TMolecules, set default temperature of '0'
280       const int n = inData->length();
281       ATOMSOLV_ORB::TMoleculeList_var outData = new ATOMSOLV_ORB::TMoleculeList();
282       outData->length( n );
283       for ( int i = 0; i < n; i++ ) {
284         ATOMSOLV_ORB::TMolecule_var tmol = new ATOMSOLV_ORB::TMolecule();
285         tmol->molecule = ATOMGEN_ORB::Molecule::_duplicate( inData[i] );
286         tmol->temperature = 0;
287         outData[ i ] = tmol;
288       }
289
290       engine->setData( outData );
291
292       app->updateObjectBrowser();
293     }
294   }
295 }
296
297 // Action slot
298 void ATOMSOLVGUI::OnProcessData()
299 {
300   ATOMSOLV_ORB::ATOMSOLV_Gen_var engine = GetATOMSOLVGen();
301   SalomeApp_Application* app = getApp();
302   if ( !CORBA::is_nil( engine ) && app ) {
303     // call processData on engine
304     engine->processData();
305     // update data objects in object browser
306     updateObjBrowser();
307     // redisplay atoms that are already displayed (as their temperature changed..)
308     ViewManagerList vmans;
309     getApp()->viewManagers( SVTK_Viewer::Type(), vmans );
310     for ( QList<SUIT_ViewManager*>::iterator it = vmans.begin(); it != vmans.end(); ++it ) {
311       QVector<SUIT_ViewWindow*> views = (*it)->getViews();
312       for ( int i = 0; i < views.size(); i++ ) {
313         if ( SVTK_ViewWindow* svtkView = dynamic_cast<SVTK_ViewWindow*>( views[ i ] ) ) {
314           vtkActorCollection* actors = svtkView->getRenderer()->GetActors();
315           actors->InitTraversal();
316           while ( vtkActor* actor = actors->GetNextActor() )
317             if ( SALOME_Actor* salomeActor = SALOME_Actor::SafeDownCast( actor ) )
318               if ( salomeActor->hasIO() ) {
319                 //                  printf( " -- must redisplay actor salomeActor: %s\n", salomeActor->getIO()->getEntry() );
320                 ATOMSOLVGUI_Displayer().updateActor( salomeActor );
321               }
322         }
323       }
324     }
325   }
326 }
327
328 // Action slot
329 void ATOMSOLVGUI::OnDisplayerCommand()
330 {
331   const QObject* obj = sender();
332   if ( obj && obj->inherits( "QAction" ) ) {
333     const int id = actionId ( (QAction*)obj );
334     switch ( id ) {
335     case Display : {
336       QStringList entries;
337       selected ( entries, true );
338       ATOMSOLVGUI_Displayer d;
339       for ( QStringList::const_iterator it = entries.begin(), last = entries.end(); it != last; it++ )
340         d.Display( it->toLatin1(), /*updateviewer=*/false, 0 );
341       d.UpdateViewer();
342     } break;
343     case Erase   : {
344       QStringList entries;
345       selected ( entries, true );
346       ATOMSOLVGUI_Displayer d;
347       for ( QStringList::const_iterator it = entries.begin(), last = entries.end(); it != last; it++ )
348         d.Erase( *it, /*forced=*/true, /*updateViewer=*/false, 0 );
349       d.UpdateViewer();
350     } break;
351     case Shading   : {
352       QStringList entries;
353       selected ( entries, true );
354       ATOMSOLVGUI_Displayer().setDisplayMode( entries, "Surface" );
355     } break;
356     case Wireframe   : {
357       QStringList entries;
358       selected ( entries, true );
359       ATOMSOLVGUI_Displayer().setDisplayMode( entries, "Wireframe" );
360     } break;
361     case PointsMode   : {
362       QStringList entries;
363       selected ( entries, true );
364       ATOMSOLVGUI_Displayer().setDisplayMode( entries, "Points" );
365     } break;
366     case Color   : {
367       QStringList entries;
368       selected ( entries, true );
369       QColor initialColor( "white" );
370       if ( entries.count() == 1 )
371         initialColor = ATOMSOLVGUI_Displayer().getColor( entries[ 0 ] );
372       QColor color = QColorDialog::getColor( initialColor, getApp()->desktop() );
373       if ( color.isValid() )
374         ATOMSOLVGUI_Displayer().setColor( entries, color );
375     } break;
376     case Transparency   : {
377       QStringList entries;
378       selected ( entries, true );
379       ATOMSOLVGUI_TransparencyDlg( getApp()->desktop(), entries ).exec();
380     } break;
381     default: printf( "ERROR: Action with ID = %d was not found in ATOMSOLVGUI\n", id ); break;
382     }
383   }
384 }
385
386 // returns a custom displayer object
387 LightApp_Displayer* ATOMSOLVGUI::displayer()
388 {
389   return new ATOMSOLVGUI_Displayer();
390 }
391
392 // create preferences for ATOMSOLV component
393 void ATOMSOLVGUI::createPreferences()
394 {
395   int tabId = addPreference( tr( "ATOMSOLV_PREFERENCES" ) );
396   int groupId = addPreference( tr( "PRESENTATION_PREF_GROUP" ), tabId );
397
398   setPreferenceProperty( groupId, "columns", 1 );
399
400   // Representation mode preference
401   int dispModeId = addPreference( tr( "DISPLAY_MODE_PREF" ), groupId,
402                                   LightApp_Preferences::Selector, "ATOMSOLV", "Representation" );
403   QList<QVariant> intDispModes;
404   QStringList strDispModes;
405   intDispModes.append( 0 ); strDispModes.append( tr( "MEN_POINTSMODE" ) );
406   intDispModes.append( 1 ); strDispModes.append( tr( "MEN_WIREFRAME" ) );
407   intDispModes.append( 2 ); strDispModes.append( tr( "MEN_SHADING" ) );
408
409   setPreferenceProperty( dispModeId, "strings", strDispModes );
410   setPreferenceProperty( dispModeId, "indexes", intDispModes );
411
412   // Radius preference
413   int radisusId = addPreference( tr( "RADIUS_PREF" ), groupId,
414                                  LightApp_Preferences::DblSpin, "ATOMSOLV", "Radius" );
415   setPreferenceProperty( radisusId, "min", .001 );
416   setPreferenceProperty( radisusId, "max", 1000 );
417   setPreferenceProperty( radisusId, "precision", 3 );
418 }
419
420 // preferences of changed: update the corresponding values
421 void ATOMSOLVGUI::preferencesChanged( const QString& group, const QString& param )
422 {
423   if ( group == "ATOMSOLV" && param == "Representation" )
424     ATOMSOLVGUI_Displayer::setDefaultRepresentation( getApp()->resourceMgr()->integerValue( group, param ) );
425   else if ( group == "ATOMSOLV" && param == "Radius" )
426     ATOMSOLVGUI_Displayer::setDefaultRadius( getApp()->resourceMgr()->doubleValue( group, param ) );
427 }
428
429
430 // Export the module
431 extern "C" {
432   ATOMSOLVGUI_EXPORT
433   CAM_Module* createModule()
434   {
435     return new ATOMSOLVGUI();
436   }
437   ATOMSOLVGUI_EXPORT
438   char* getModuleVersion() 
439   {
440     return (char*)ATOMSOLV_VERSION_STR;
441   }
442 }