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