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>
42 /*!Create new instance of CAM_Application*/
43 extern "C" CAM_EXPORT SUIT_Application* createApplication()
45 return new CAM_Application();
48 /*!Constructor. read module list.
49 * \param autoLoad - auto load flag.
51 CAM_Application::CAM_Application( const bool autoLoad )
54 myAutoLoad( autoLoad )
59 /*!Destructor. Do nothing.*/
60 CAM_Application::~CAM_Application()
64 /*! Load modules, if \a myAutoLoad flag is true.\n
65 * Start application - call start() method from parent class.
67 void CAM_Application::start()
72 STD_Application::start();
76 * \retval CAM_Module - active module.
78 CAM_Module* CAM_Application::activeModule() const
83 /*!Get module with name \a modName from modules list.
84 * \retval CAM_Module pointer - module.
86 CAM_Module* CAM_Application::module( const QString& modName ) const
89 for ( ModuleListIterator it( myModules ); it.current() && !mod; ++it )
90 if ( it.current()->moduleName() == modName )
95 /*!Gets modules iterator.*/
96 CAM_Application::ModuleListIterator CAM_Application::modules() const
98 return ModuleListIterator( myModules );
101 /*!Gets modules list.
102 * \param out - output list of modules.
104 void CAM_Application::modules( CAM_Application::ModuleList& out ) const
106 out.setAutoDelete( false );
109 for ( ModuleListIterator it( myModules ); it.current(); ++it )
110 out.append( it.current() );
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.
119 void CAM_Application::modules( QStringList& lst, const bool loaded ) const
124 for ( ModuleListIterator it( myModules ); it.current(); ++it )
125 lst.append( it.current()->moduleName() );
127 for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end(); ++it )
128 lst.append( (*it).title );
131 /*!Adding module \a mod to list.
132 *\param mod - module.
134 void CAM_Application::addModule( CAM_Module* mod )
136 if ( !mod || myModules.contains( mod ) )
139 mod->initialize( this );
141 QMap<CAM_Module*, int> map;
144 for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end(); ++it )
146 if ( (*it).title == mod->moduleName() )
147 newList.append( mod );
150 CAM_Module* curMod = module( (*it).title );
152 newList.append( curMod );
154 if ( !newList.isEmpty() )
155 map.insert( newList.getLast(), 0 );
158 for ( ModuleListIterator itr( myModules ); itr.current(); ++itr )
160 if ( !map.contains( itr.current() ) )
161 newList.append( itr.current() );
164 if ( !map.contains( mod ) )
165 newList.append( mod );
172 /*!Load modules from information list.
173 * \warning If some of modules not loaded, error message appear on desktop.
175 void CAM_Application::loadModules()
177 for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end(); ++it )
179 CAM_Module* mod = loadModule( (*it).title );
183 SUIT_MessageBox::error1( desktop(), tr( "Loading modules" ),
184 tr( "Can not load module %1" ).arg( (*it).title ), tr( "Ok" ) );
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.
194 CAM_Module* CAM_Application::loadModule( const QString& modName )
196 if ( myInfoList.isEmpty() )
198 qWarning( tr( "Modules configuration is not defined." ) );
202 QString libName = moduleLibrary( modName );
203 if ( libName.isEmpty() )
205 qWarning( tr( "Information about module \"%1\" doesn't exist." ).arg( modName ) );
210 GET_MODULE_FUNC crtInst = 0;
213 HINSTANCE modLib = ::LoadLibrary( libName );
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 );
224 crtInst = (GET_MODULE_FUNC)::GetProcAddress( modLib, GET_MODULE_NAME );
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 );
235 void* modLib = dlopen( (char*)libName.latin1(), RTLD_LAZY );
237 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
240 crtInst = (GET_MODULE_FUNC)dlsym( modLib, GET_MODULE_NAME );
242 err = QString( "Failed to find function %1. %2" ).arg( GET_MODULE_NAME ).arg( dlerror() );
246 CAM_Module* module = crtInst ? crtInst() : 0;
249 module->setModuleName( modName );
250 module->setName( moduleName( modName ) );
253 if ( !err.isEmpty() )
254 SUIT_MessageBox::warn1( desktop(), tr( "Error" ), err, tr( "Ok" ) );
259 /**@name Activate module group.*/
261 /*!Activate module with name \a modName.
262 *\param modName - module name.
263 *\ratval true, if module loaded and activated successful, else false.
265 bool CAM_Application::activateModule( const QString& modName )
267 if ( !modName.isEmpty() && !activeStudy() )
271 if ( !modName.isEmpty() )
273 CAM_Module* mod = module( modName );
274 if ( !mod && !moduleLibrary( modName ).isEmpty() )
276 mod = loadModule( modName );
281 res = activateModule( mod );
284 res = activateModule( 0 );
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.
294 bool CAM_Application::activateModule( CAM_Module* mod )
296 if ( mod && !activeStudy() )
299 if ( myModule == mod )
304 if ( !myModule->deactivateModule( activeStudy() ) )
312 // Connect the module to the active study
313 myModule->connectToStudy( dynamic_cast<CAM_Study*>( activeStudy() ) );
314 if ( !myModule->activateModule( activeStudy() ) )
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" ) );
324 updateCommandsStatus();
330 /*!Create new study for current application.
331 *\retval study pointer.
333 SUIT_Study* CAM_Application::createNewStudy()
335 return new CAM_Study( this );
338 /*!Update commands status for parent class and for current class(if module is active)*/
339 void CAM_Application::updateCommandsStatus()
341 STD_Application::updateCommandsStatus();
343 if ( activeModule() )
344 activeModule()->updateCommandsStatus();
347 /*!Close all modules in study \a theDoc.
348 *\param theDoc - study
350 void CAM_Application::beforeCloseDoc( SUIT_Study* theDoc )
352 for ( ModuleListIterator it( myModules ); it.current(); ++it )
353 it.current()->studyClosed( theDoc );
356 /*!Sets active study for parent class.
357 *\param study - study.
359 void CAM_Application::setActiveStudy( SUIT_Study* study )
361 STD_Application::setActiveStudy( study );
365 void CAM_Application::moduleAdded( CAM_Module* mod )
367 // CAM_Study* study = dynamic_cast<CAM_Study*>( activeStudy() );
371 // study->insertDataModel( mod->dataModel() );
374 /*!Gets module name by title \a title
375 *\param title - title name
376 *\retval QString module name.
378 QString CAM_Application::moduleName( const QString& title ) const
381 for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end() && res.isEmpty(); ++it )
383 if ( (*it).title == title )
389 /*!Gets module title by module name \a name
390 *\param name - module name
391 *\retval QString module title.
393 QString CAM_Application::moduleTitle( const QString& name ) const
396 for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end() && res.isEmpty(); ++it )
398 if ( (*it).name == name )
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.
409 QString CAM_Application::moduleLibrary( const QString& title, const bool full ) const
412 for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end() && res.isEmpty(); ++it )
414 if ( (*it).title == title )
415 res = (*it).internal;
417 if ( !res.isEmpty() && full )
418 res = SUIT_Tools::library( res );
422 /*!Read modules list*/
423 void CAM_Application::readModuleList()
425 if ( !myInfoList.isEmpty() )
428 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
433 for (int i = 1; i < qApp->argc(); i++)
434 args.append( qApp->argv()[i] );
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() );
446 if ( modList.isEmpty() ) {
447 QString mods = resMgr->stringValue( "launch", "modules", QString::null );
448 modList = QStringList::split( ",", mods );
451 for ( QStringList::const_iterator it = modList.begin(); it != modList.end(); ++it )
453 QString modName = (*it).stripWhiteSpace();
454 if ( modName.isEmpty() )
457 QString modTitle = resMgr->stringValue( *it, QString( "name" ), QString::null );
458 if ( modTitle.isEmpty() )
461 QString modLibrary = resMgr->stringValue( *it, QString( "library" ), QString::null ).stripWhiteSpace();
462 if ( !modLibrary.isEmpty() )
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 );
473 if ( modLibrary.startsWith( "lib" ) )
474 modLibrary = modLibrary.mid( 3 );
478 modLibrary = modName;
482 inf.title = modTitle;
483 inf.internal = modLibrary;
484 myInfoList.append( inf );
487 if ( myInfoList.isEmpty() )
488 SUIT_MessageBox::error1( 0, tr( "Error" ), tr( "Can not load modules configuration file " ), tr( "Ok" ) );
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
496 void CAM_Application::contextMenuPopup( const QString& type, QPopupMenu* thePopup, QString& title )
498 // to do : add common items for popup menu ( if they are exist )
499 if ( activeModule() )
500 activeModule()->contextMenuPopup( type, thePopup, title );
503 /*!Create empty study.*/
504 void CAM_Application::createEmptyStudy()
506 SUIT_Study* study = activeStudy();
508 STD_Application::createEmptyStudy();