Salome HOME
Merge from OCC_development_generic_2006
[modules/gui.git] / src / CAM / CAM_Application.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
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.
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/
18 //
19 #include "CAM_Application.h"
20
21 #include "CAM_Study.h"
22 #include "CAM_Module.h"
23
24 #include <SUIT_Tools.h>
25 #include <SUIT_Session.h>
26 #include <SUIT_MessageBox.h>
27
28 #include <qfile.h> 
29 #include <qfileinfo.h>
30 #include <qtextstream.h>
31 #include <qlabel.h>
32 #include <qfont.h>
33 #include <qapplication.h>
34 #include <qregexp.h>
35
36 #ifdef WIN32
37 #include <windows.h>
38 #else
39 #include <dlfcn.h>
40 #endif
41
42 /*!Create new instance of CAM_Application*/
43 extern "C" CAM_EXPORT SUIT_Application* createApplication()
44 {
45   return new CAM_Application();
46 }
47
48 /*!Constructor. read module list.
49  * \param autoLoad - auto load flag.
50  */
51 CAM_Application::CAM_Application( const bool autoLoad )
52 : STD_Application(),
53 myModule( 0 ),
54 myAutoLoad( autoLoad )
55 {
56   readModuleList();
57 }
58
59 /*!Destructor. Do nothing.*/
60 CAM_Application::~CAM_Application()
61 {
62 }
63
64 /*! Load modules, if \a myAutoLoad flag is true.\n
65  * Start application - call start() method from parent class.
66  */
67 void CAM_Application::start()
68 {
69   if ( myAutoLoad )
70     loadModules();
71
72   STD_Application::start();
73 }
74
75 /*!Get active module.
76  * \retval CAM_Module - active module.
77  */
78 CAM_Module* CAM_Application::activeModule() const
79 {
80   return myModule;
81 }
82
83 /*!Get module with name \a modName from modules list.
84  * \retval CAM_Module pointer - module.
85  */
86 CAM_Module* CAM_Application::module(  const QString& modName ) const
87 {
88   CAM_Module* mod = 0;
89   for ( ModuleListIterator it( myModules ); it.current() && !mod; ++it )
90     if ( it.current()->moduleName() == modName )
91       mod = it.current();
92   return mod;
93 }
94
95 /*!Gets modules iterator.*/
96 CAM_Application::ModuleListIterator CAM_Application::modules() const
97 {
98   return ModuleListIterator( myModules );
99 }
100
101 /*!Gets modules list.
102  * \param out - output list of modules.
103  */
104 void CAM_Application::modules( CAM_Application::ModuleList& out ) const
105 {
106   out.setAutoDelete( false );
107   out.clear();
108
109   for ( ModuleListIterator it( myModules ); it.current(); ++it )
110     out.append( it.current() );
111 }
112
113 /*!Gets list of names for modules.\n
114  * Get loaded modules names, if \a loaded is true, else \n
115  * get names from information list.
116  * \param lst - output list of names.
117  * \param loaded - boolean flag.
118  */
119 void CAM_Application::modules( QStringList& lst, const bool loaded ) const
120 {
121   lst.clear();
122
123   if ( loaded )
124     for ( ModuleListIterator it( myModules ); it.current(); ++it )
125       lst.append( it.current()->moduleName() );
126   else
127     for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end(); ++it )
128       lst.append( (*it).title );
129 }
130
131 /*!Adding module \a mod to list.
132  *\param mod - module.
133  */
134 void CAM_Application::addModule( CAM_Module* mod )
135 {
136   if ( !mod || myModules.contains( mod ) )
137     return;
138
139   mod->initialize( this );
140
141   QMap<CAM_Module*, int> map;
142
143   ModuleList newList;
144   for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end(); ++it )
145   {
146     if ( (*it).title == mod->moduleName() )
147       newList.append( mod );
148     else
149     {
150       CAM_Module* curMod = module( (*it).title );
151       if ( curMod )
152         newList.append( curMod );
153     }
154     if ( !newList.isEmpty() )
155       map.insert( newList.getLast(), 0 );
156   }
157
158   for ( ModuleListIterator itr( myModules ); itr.current(); ++itr )
159   {
160     if ( !map.contains( itr.current() ) )
161       newList.append( itr.current() );
162   }
163
164   if ( !map.contains( mod ) )
165       newList.append( mod );
166
167   myModules = newList;
168
169   moduleAdded( mod );
170 }
171
172 /*!Load modules from information list.
173  * \warning If some of modules not loaded, error message appear on desktop.
174  */
175 void CAM_Application::loadModules()
176 {
177   for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end(); ++it )
178   {
179     CAM_Module* mod = loadModule( (*it).title );
180     if ( mod )
181       addModule( mod );
182     else
183       SUIT_MessageBox::error1( desktop(), tr( "Loading modules" ),
184                                tr( "Can not load module %1" ).arg( (*it).title ), tr( "Ok" ) );
185   }
186 }
187
188 /*!Load module with name \a modName.
189  *\param modName - module name for loading.
190  *\warning If information list is empty.
191  *\warning If module library (for module with \a modName) is empty.
192  *\warning If module library is not loaded.
193  */
194 CAM_Module* CAM_Application::loadModule( const QString& modName )
195 {
196   if ( myInfoList.isEmpty() )
197   {
198     qWarning( tr( "Modules configuration is not defined." ) );
199     return 0;
200   }
201
202   QString libName = moduleLibrary( modName );
203   if ( libName.isEmpty() )
204   {
205     qWarning( tr( "Information about module \"%1\" doesn't exist." ).arg( modName ) );
206     return 0;
207   }
208
209   QString err;
210   GET_MODULE_FUNC crtInst = 0;
211
212 #ifdef WIN32
213   HINSTANCE modLib = ::LoadLibrary( libName ); 
214   if ( !modLib )
215   {
216     LPVOID lpMsgBuf;
217     ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
218                      FORMAT_MESSAGE_IGNORE_INSERTS, 0, ::GetLastError(), 0, (LPTSTR)&lpMsgBuf, 0, 0 );
219     err = QString( "Failed to load  %1. %2" ).arg( libName ).arg( (LPTSTR)lpMsgBuf );
220     ::LocalFree( lpMsgBuf );
221   }
222   else
223   {
224     crtInst = (GET_MODULE_FUNC)::GetProcAddress( modLib, GET_MODULE_NAME );
225     if ( !crtInst )
226     {
227       LPVOID lpMsgBuf;
228       ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
229                        FORMAT_MESSAGE_IGNORE_INSERTS, 0, ::GetLastError(), 0, (LPTSTR)&lpMsgBuf, 0, 0 );
230     err = QString( "Failed to find  %1 function. %2" ).arg( GET_MODULE_NAME ).arg( (LPTSTR)lpMsgBuf );
231     ::LocalFree( lpMsgBuf );
232     }
233   }
234 #else
235   void* modLib = dlopen( (char*)libName.latin1(), RTLD_LAZY );
236   if ( !modLib )
237     err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
238   else
239   {
240     crtInst = (GET_MODULE_FUNC)dlsym( modLib, GET_MODULE_NAME );
241     if ( !crtInst )
242       err = QString( "Failed to find function %1. %2" ).arg( GET_MODULE_NAME ).arg( dlerror() );
243   }
244 #endif
245
246   CAM_Module* module = crtInst ? crtInst() : 0;
247   if ( module )
248   {
249     module->setModuleName( modName );
250     module->setName( moduleName( modName ) );
251   }
252
253   if ( !err.isEmpty() )
254     SUIT_MessageBox::warn1( desktop(), tr( "Error" ), err, tr( "Ok" ) );
255
256   return module;
257 }
258
259 /**@name Activate module group.*/
260 //@{
261 /*!Activate module with name \a modName.
262  *\param modName - module name.
263  *\ratval true, if module loaded and activated successful, else false.
264  */
265 bool CAM_Application::activateModule( const QString& modName )
266 {
267   if ( !modName.isEmpty() && !activeStudy() )
268     return false;
269
270   bool res = false;
271   if ( !modName.isEmpty() )
272   {
273     CAM_Module* mod = module( modName );
274     if ( !mod && !moduleLibrary( modName ).isEmpty() )
275     {
276       mod = loadModule( modName );
277       addModule( mod );
278     }
279
280     if ( mod )
281       res = activateModule( mod );
282   }
283   else
284     res = activateModule( 0 );
285
286   return res;
287 }
288
289 /*!Activate module \a mod
290  *\param mod - module for activation.
291  *\retval true - if all sucessful.
292  *\warning Error message if module not activated in active study.
293  */
294 bool CAM_Application::activateModule( CAM_Module* mod )
295 {
296   if ( mod && !activeStudy() )
297     return false;
298
299   if ( myModule == mod )
300     return true;
301
302   if ( myModule )
303   {
304     if ( !myModule->deactivateModule( activeStudy() ) )
305     {
306       // ....      
307     }    
308   }     
309   myModule = mod;
310
311   if ( myModule ){
312     // Connect the module to the active study
313     myModule->connectToStudy( dynamic_cast<CAM_Study*>( activeStudy() ) );
314     if ( !myModule->activateModule( activeStudy() ) )
315     {
316       myModule->setMenuShown( false );
317       myModule->setToolShown( false );
318       SUIT_MessageBox::error1( desktop(), tr( "ERROR_TLT" ), tr( "ERROR_ACTIVATE_MODULE_MSG" ).arg( myModule->moduleName() ), tr( "BUT_OK" ) );
319       myModule = 0;
320       return false;
321     }
322   }
323
324   updateCommandsStatus();
325
326   return true;
327 }
328 //@}
329
330 /*!Create new study for current application.
331  *\retval study pointer.
332  */
333 SUIT_Study* CAM_Application::createNewStudy() 
334
335   return new CAM_Study( this );
336 }
337
338 /*!Update commands status for parent class and for current class(if module is active)*/
339 void CAM_Application::updateCommandsStatus()
340 {
341   STD_Application::updateCommandsStatus();
342
343   if ( activeModule() )
344     activeModule()->updateCommandsStatus();
345 }
346
347 /*!Close all modules in study \a theDoc.
348  *\param theDoc - study
349  */
350 void CAM_Application::beforeCloseDoc( SUIT_Study* theDoc )
351 {
352   for ( ModuleListIterator it( myModules ); it.current(); ++it )
353     it.current()->studyClosed( theDoc );
354 }
355
356 /*!Sets active study for parent class.
357  *\param study - study.
358  */
359 void CAM_Application::setActiveStudy( SUIT_Study* study )
360 {
361   STD_Application::setActiveStudy( study );
362 }
363
364 /*!Do nothing.*/
365 void CAM_Application::moduleAdded( CAM_Module* mod )
366 {
367 //  CAM_Study* study = dynamic_cast<CAM_Study*>( activeStudy() );
368 //  if ( !study )
369 //    return;
370
371 //  study->insertDataModel( mod->dataModel() );
372 }
373
374 /*!Gets module name by title \a title
375  *\param title - title name
376  *\retval QString module name.
377  */
378 QString CAM_Application::moduleName( const QString& title ) const
379 {
380   QString res;
381   for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end() && res.isEmpty(); ++it )
382   {
383     if ( (*it).title == title )
384       res = (*it).name;
385   }
386   return res;
387 }
388
389 /*!Gets module title by module name \a name
390  *\param name - module name
391  *\retval QString module title.
392  */
393 QString CAM_Application::moduleTitle( const QString& name ) const
394 {
395   QString res;
396   for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end() && res.isEmpty(); ++it )
397   {
398     if ( (*it).name == name )
399       res = (*it).title;
400   }
401   return res;
402 }
403
404 /*!Get library name for module with title \a title.
405  *\param title - module title name.
406  *\param full  - boolean flag (if true - return full library name, else internal name)
407  *\retval QString - library name.
408  */
409 QString CAM_Application::moduleLibrary( const QString& title, const bool full ) const
410 {
411   QString res;
412   for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end() && res.isEmpty(); ++it )
413   {
414     if ( (*it).title == title )
415       res = (*it).internal;
416   }
417   if ( !res.isEmpty() && full )
418     res = SUIT_Tools::library( res );
419   return res;
420 }
421
422 /*!Read modules list*/
423 void CAM_Application::readModuleList()
424 {
425   if ( !myInfoList.isEmpty() )
426     return;
427
428   SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
429
430   QStringList modList;
431
432   QStringList args;
433   for (int i = 1; i < qApp->argc(); i++)
434     args.append( qApp->argv()[i] );
435
436   QRegExp rx("--modules\\s+\\(\\s*(.*)\\s*\\)");
437   rx.setMinimal( true );
438   if ( rx.search( args.join(" ") ) >= 0 && rx.capturedTexts().count() > 0 ) {
439     QString modules = rx.capturedTexts()[1];
440     QStringList mods = QStringList::split(":",modules,false);
441     for ( uint i = 0; i < mods.count(); i++ ) {
442       if ( !mods[i].stripWhiteSpace().isEmpty() )
443         modList.append( mods[i].stripWhiteSpace() );
444     }
445   }
446   if ( modList.isEmpty() ) {
447     QString mods = resMgr->stringValue( "launch", "modules", QString::null );
448     modList = QStringList::split( ",", mods );
449   }
450
451   for ( QStringList::const_iterator it = modList.begin(); it != modList.end(); ++it )
452   {
453     QString modName = (*it).stripWhiteSpace();
454     if ( modName.isEmpty() )
455       continue;
456
457     QString modTitle = resMgr->stringValue( *it, QString( "name" ), QString::null );
458     if ( modTitle.isEmpty() )
459       continue;
460
461     QString modLibrary = resMgr->stringValue( *it, QString( "library" ), QString::null ).stripWhiteSpace();
462     if ( !modLibrary.isEmpty() )
463     {
464       QString libExt;
465       modLibrary = SUIT_Tools::file( modLibrary.stripWhiteSpace() );
466       libExt = QString( "so" );
467       if ( SUIT_Tools::extension( modLibrary ).lower() == libExt )
468         modLibrary = modLibrary.mid( 0, modLibrary.length() - libExt.length() - 1 );
469       libExt = QString( "dll" );
470       if ( SUIT_Tools::extension( modLibrary ).lower() == libExt )
471         modLibrary = modLibrary.mid( 0, modLibrary.length() - libExt.length() - 1 );
472 #ifndef WIN32
473       if ( modLibrary.startsWith( "lib" ) )
474         modLibrary = modLibrary.mid( 3 );
475 #endif
476     }
477     else
478       modLibrary = modName;
479
480     ModuleInfo inf;
481     inf.name = modName;
482     inf.title = modTitle;
483     inf.internal = modLibrary;
484     myInfoList.append( inf );
485   }
486
487   if ( myInfoList.isEmpty() )
488     SUIT_MessageBox::error1( 0, tr( "Error" ), tr( "Can not load modules configuration file " ), tr( "Ok" ) );
489 }
490
491 /*!Add common items for popup menu ( if they are exist )
492  *\param type - type of popup menu
493  *\param thePopup - popup menu
494  *\param title - title of popup menu
495  */
496 void CAM_Application::contextMenuPopup( const QString& type, QPopupMenu* thePopup, QString& title )
497 {
498   // to do : add common items for popup menu ( if they are exist )
499   if ( activeModule() ) 
500     activeModule()->contextMenuPopup( type, thePopup, title );
501 }
502
503 /*!Create empty study.*/
504 void CAM_Application::createEmptyStudy()
505 {
506   SUIT_Study* study = activeStudy();
507
508   STD_Application::createEmptyStudy();
509 }