1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA 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.
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/
19 #include "CAM_Application.h"
21 #include "CAM_Study.h"
22 #include "CAM_Module.h"
24 #include <SUIT_Tools.h>
25 #include <SUIT_Session.h>
26 #include <SUIT_MessageBox.h>
29 #include <qfileinfo.h>
30 #include <qtextstream.h>
33 #include <qapplication.h>
41 /*!Create new instance of CAM_Application*/
42 extern "C" CAM_EXPORT SUIT_Application* createApplication()
44 return new CAM_Application();
47 /*!Constructor. read module list.
48 * \param autoLoad - auto load flag.
50 CAM_Application::CAM_Application( const bool autoLoad )
53 myAutoLoad( autoLoad )
58 /*!Destructor. Do nothing.*/
59 CAM_Application::~CAM_Application()
63 /*! Load modules, if \a myAutoLoad flag is true.\n
64 * Start application - call start() method from parent class.
66 void CAM_Application::start()
71 STD_Application::start();
75 * \retval CAM_Module - active module.
77 CAM_Module* CAM_Application::activeModule() const
82 /*!Get module with name \a modName from modules list.
83 * \retval CAM_Module pointer - module.
85 CAM_Module* CAM_Application::module( const QString& modName ) const
88 for ( ModuleListIterator it( myModules ); it.current() && !mod; ++it )
89 if ( it.current()->moduleName() == modName )
94 /*!Gets modules iterator.*/
95 CAM_Application::ModuleListIterator CAM_Application::modules() const
97 return ModuleListIterator( myModules );
100 /*!Gets modules list.
101 * \param out - output list of modules.
103 void CAM_Application::modules( CAM_Application::ModuleList& out ) const
105 out.setAutoDelete( false );
108 for ( ModuleListIterator it( myModules ); it.current(); ++it )
109 out.append( it.current() );
112 /*!Gets list of names for modules.\n
113 * Get loaded modules names, if \a loaded is true, else \n
114 * get names from information list.
115 * \param lst - output list of names.
116 * \param loaded - boolean flag.
118 void CAM_Application::modules( QStringList& lst, const bool loaded ) const
123 for ( ModuleListIterator it( myModules ); it.current(); ++it )
124 lst.append( it.current()->moduleName() );
126 for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end(); ++it )
127 lst.append( (*it).title );
130 /*!Adding module \a mod to list.
131 *\param mod - module.
133 void CAM_Application::addModule( CAM_Module* mod )
135 if ( !mod || myModules.contains( mod ) )
138 mod->initialize( this );
140 QMap<CAM_Module*, int> map;
143 for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end(); ++it )
145 if ( (*it).title == mod->moduleName() )
146 newList.append( mod );
149 CAM_Module* curMod = module( (*it).title );
151 newList.append( curMod );
153 if ( !newList.isEmpty() )
154 map.insert( newList.getLast(), 0 );
157 for ( ModuleListIterator itr( myModules ); itr.current(); ++itr )
159 if ( !map.contains( itr.current() ) )
160 newList.append( itr.current() );
163 if ( !map.contains( mod ) )
164 newList.append( mod );
171 /*!Load modules from information list.
172 * \warning If some of modules not loaded, error message appear on desktop.
174 void CAM_Application::loadModules()
176 for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end(); ++it )
178 CAM_Module* mod = loadModule( (*it).title );
182 SUIT_MessageBox::error1( desktop(), tr( "Loading modules" ),
183 tr( "Can not load module %1" ).arg( (*it).title ), tr( "Ok" ) );
187 /*!Load module with name \a modName.
188 *\param modName - module name for loading.
189 *\warning If information list is empty.
190 *\warning If module library (for module with \a modName) is empty.
191 *\warning If module library is not loaded.
193 CAM_Module* CAM_Application::loadModule( const QString& modName )
195 if ( myInfoList.isEmpty() )
197 qWarning( tr( "Modules configuration is not defined." ) );
201 QString libName = moduleLibrary( modName );
202 if ( libName.isEmpty() )
204 qWarning( tr( "Information about module \"%1\" doesn't exist." ).arg( modName ) );
209 GET_MODULE_FUNC crtInst = 0;
212 HINSTANCE modLib = ::LoadLibrary( libName );
216 ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
217 FORMAT_MESSAGE_IGNORE_INSERTS, 0, ::GetLastError(), 0, (LPTSTR)&lpMsgBuf, 0, 0 );
218 err = QString( "Failed to load %1. %2" ).arg( libName ).arg( (LPTSTR)lpMsgBuf );
219 ::LocalFree( lpMsgBuf );
223 crtInst = (GET_MODULE_FUNC)::GetProcAddress( modLib, GET_MODULE_NAME );
227 ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
228 FORMAT_MESSAGE_IGNORE_INSERTS, 0, ::GetLastError(), 0, (LPTSTR)&lpMsgBuf, 0, 0 );
229 err = QString( "Failed to find %1 function. %2" ).arg( GET_MODULE_NAME ).arg( (LPTSTR)lpMsgBuf );
230 ::LocalFree( lpMsgBuf );
234 void* modLib = dlopen( (char*)libName.latin1(), RTLD_LAZY );
236 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
239 crtInst = (GET_MODULE_FUNC)dlsym( modLib, GET_MODULE_NAME );
241 err = QString( "Failed to find function %1. %2" ).arg( GET_MODULE_NAME ).arg( dlerror() );
245 CAM_Module* module = crtInst ? crtInst() : 0;
248 module->setModuleName( modName );
249 module->setName( moduleName( modName ) );
252 if ( !err.isEmpty() )
253 SUIT_MessageBox::warn1( desktop(), tr( "Error" ), err, tr( "Ok" ) );
258 /**@name Activate module group.*/
260 /*!Activate module with name \a modName.
261 *\param modName - module name.
262 *\ratval true, if module loaded and activated successful, else false.
264 bool CAM_Application::activateModule( const QString& modName )
266 if ( !modName.isEmpty() && !activeStudy() )
270 if ( !modName.isEmpty() )
272 CAM_Module* mod = module( modName );
273 if ( !mod && !moduleLibrary( modName ).isEmpty() )
275 mod = loadModule( modName );
280 res = activateModule( mod );
283 res = activateModule( 0 );
288 /*!Activate module \a mod
289 *\param mod - module for activation.
290 *\retval true - if all sucessful.
291 *\warning Error message if module not activated in active study.
293 bool CAM_Application::activateModule( CAM_Module* mod )
295 if ( mod && !activeStudy() )
298 if ( myModule == mod )
303 if ( !myModule->deactivateModule( activeStudy() ) )
311 // Connect the module to the active study
312 myModule->connectToStudy( dynamic_cast<CAM_Study*>( activeStudy() ) );
313 if ( !myModule->activateModule( activeStudy() ) )
315 myModule->setMenuShown( false );
316 myModule->setToolShown( false );
317 SUIT_MessageBox::error1( desktop(), tr( "ERROR_TLT" ), tr( "ERROR_ACTIVATE_MODULE_MSG" ).arg( myModule->moduleName() ), tr( "BUT_OK" ) );
323 updateCommandsStatus();
329 /*!Create new study for current application.
330 *\retval study pointer.
332 SUIT_Study* CAM_Application::createNewStudy()
334 return new CAM_Study( this );
337 /*!Update commands status for parent class and for current class(if module is active)*/
338 void CAM_Application::updateCommandsStatus()
340 STD_Application::updateCommandsStatus();
342 if ( activeModule() )
343 activeModule()->updateCommandsStatus();
346 /*!Close all modules in study \a theDoc.
347 *\param theDoc - study
349 void CAM_Application::beforeCloseDoc( SUIT_Study* theDoc )
351 for ( ModuleListIterator it( myModules ); it.current(); ++it )
352 it.current()->studyClosed( theDoc );
355 /*!Sets active study for parent class.
356 *\param study - study.
358 void CAM_Application::setActiveStudy( SUIT_Study* study )
360 STD_Application::setActiveStudy( study );
364 void CAM_Application::moduleAdded( CAM_Module* mod )
366 // CAM_Study* study = dynamic_cast<CAM_Study*>( activeStudy() );
370 // study->insertDataModel( mod->dataModel() );
373 /*!Gets module name by title \a title
374 *\param title - title name
375 *\retval QString module name.
377 QString CAM_Application::moduleName( const QString& title ) const
380 for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end() && res.isEmpty(); ++it )
382 if ( (*it).title == title )
388 /*!Gets module title by module name \a name
389 *\param name - module name
390 *\retval QString module title.
392 QString CAM_Application::moduleTitle( const QString& name ) const
395 for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end() && res.isEmpty(); ++it )
397 if ( (*it).name == name )
403 /*!Get library name for module with title \a title.
404 *\param title - module title name.
405 *\param full - boolean flag (if true - return full library name, else internal name)
406 *\retval QString - library name.
408 QString CAM_Application::moduleLibrary( const QString& title, const bool full ) const
411 for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end() && res.isEmpty(); ++it )
413 if ( (*it).title == title )
414 res = (*it).internal;
416 if ( !res.isEmpty() && full )
417 res = SUIT_Tools::library( res );
421 /*!Read modules list*/
422 void CAM_Application::readModuleList()
424 if ( !myInfoList.isEmpty() )
427 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
431 // parse command line arguments
432 int nbArgs = qApp->argc();
433 char** CmdLine = qApp->argv();
435 for ( int i = 0; i < nbArgs; i++ )
437 CmdStr.append(CmdLine[i]);
440 int startId = CmdStr.find("--modules (");
441 if ( startId != -1 ) { // application launch with --modules option
442 startId = CmdStr.find("(", startId);
443 int stopId = CmdStr.find(" )", startId);
444 QString ModStr = CmdStr.mid( startId+1, stopId - (startId+1) ).stripWhiteSpace();
446 while ( i < ModStr.length() )
448 int nextId = ModStr.find( ":", i );
449 modList.append( ModStr.mid( i, nextId - i ).stripWhiteSpace() );
454 QString modStr = resMgr->stringValue( "launch", "modules", QString::null );
455 modList = QStringList::split( ",", modStr );
458 for ( QStringList::const_iterator it = modList.begin(); it != modList.end(); ++it )
460 QString modName = (*it).stripWhiteSpace();
461 if ( modName.isEmpty() )
464 QString modTitle = resMgr->stringValue( *it, QString( "name" ), QString::null );
465 if ( modTitle.isEmpty() )
468 QString modLibrary = resMgr->stringValue( *it, QString( "library" ), QString::null ).stripWhiteSpace();
469 if ( !modLibrary.isEmpty() )
472 modLibrary = SUIT_Tools::file( modLibrary.stripWhiteSpace() );
473 libExt = QString( "so" );
474 if ( SUIT_Tools::extension( modLibrary ).lower() == libExt )
475 modLibrary = modLibrary.mid( 0, modLibrary.length() - libExt.length() - 1 );
476 libExt = QString( "dll" );
477 if ( SUIT_Tools::extension( modLibrary ).lower() == libExt )
478 modLibrary = modLibrary.mid( 0, modLibrary.length() - libExt.length() - 1 );
480 if ( modLibrary.startsWith( "lib" ) )
481 modLibrary = modLibrary.mid( 3 );
485 modLibrary = modName;
489 inf.title = modTitle;
490 inf.internal = modLibrary;
491 myInfoList.append( inf );
494 if ( myInfoList.isEmpty() )
495 SUIT_MessageBox::error1( 0, tr( "Error" ), tr( "Can not load modules configuration file " ), tr( "Ok" ) );
498 /*!Add common items for popup menu ( if they are exist )
499 *\param type - type of popup menu
500 *\param thePopup - popup menu
501 *\param title - title of popup menu
503 void CAM_Application::contextMenuPopup( const QString& type, QPopupMenu* thePopup, QString& title )
505 // to do : add common items for popup menu ( if they are exist )
506 if ( activeModule() )
507 activeModule()->contextMenuPopup( type, thePopup, title );
510 /*!Create empty study.*/
511 void CAM_Application::createEmptyStudy()
513 SUIT_Study* study = activeStudy();
515 STD_Application::createEmptyStudy();