1 #include "CAM_Application.h"
4 #include "CAM_Module.h"
6 #include <SUIT_Tools.h>
7 #include <SUIT_Session.h>
8 #include <SUIT_MessageBox.h>
11 #include <qfileinfo.h>
12 #include <qtextstream.h>
22 /*!Create new instance of CAM_Application*/
23 extern "C" CAM_EXPORT SUIT_Application* createApplication()
25 return new CAM_Application();
28 /*!Constructor. read module list.
29 * \param autoLoad - auto load flag.
31 CAM_Application::CAM_Application( const bool autoLoad )
34 myAutoLoad( autoLoad )
39 /*!Destructor. Do nothing.*/
40 CAM_Application::~CAM_Application()
44 /*! Load modules, if \a myAutoLoad flag is true.\n
45 * Start application - call start() method from parent class.
47 void CAM_Application::start()
52 STD_Application::start();
56 * \retval CAM_Module - active module.
58 CAM_Module* CAM_Application::activeModule() const
63 /*!Get module with name \a modName from modules list.
64 * \retval CAM_Module pointer - module.
66 CAM_Module* CAM_Application::module( const QString& modName ) const
69 for ( ModuleListIterator it( myModules ); it.current() && !mod; ++it )
70 if ( it.current()->moduleName() == modName )
75 /*!Gets modules iterator.*/
76 CAM_Application::ModuleListIterator CAM_Application::modules() const
78 return ModuleListIterator( myModules );
82 * \param out - output list of modules.
84 void CAM_Application::modules( CAM_Application::ModuleList& out ) const
86 out.setAutoDelete( false );
89 for ( ModuleListIterator it( myModules ); it.current(); ++it )
90 out.append( it.current() );
93 /*!Gets list of names for modules.\n
94 * Get loaded modules names, if \a loaded is true, else \n
95 * get names from information list.
96 * \param lst - output list of names.
97 * \param loaded - boolean flag.
99 void CAM_Application::modules( QStringList& lst, const bool loaded ) const
104 for ( ModuleListIterator it( myModules ); it.current(); ++it )
105 lst.append( it.current()->moduleName() );
107 for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end(); ++it )
108 lst.append( (*it).title );
111 /*!Adding module \a mod to list.
112 *\param mod - module.
114 void CAM_Application::addModule( CAM_Module* mod )
116 if ( !mod || myModules.contains( mod ) )
119 mod->initialize( this );
121 QMap<CAM_Module*, int> map;
124 for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end(); ++it )
126 if ( (*it).title == mod->moduleName() )
127 newList.append( mod );
130 CAM_Module* curMod = module( (*it).title );
132 newList.append( curMod );
134 if ( !newList.isEmpty() )
135 map.insert( newList.getLast(), 0 );
138 for ( ModuleListIterator itr( myModules ); itr.current(); ++itr )
140 if ( !map.contains( itr.current() ) )
141 newList.append( itr.current() );
144 if ( !map.contains( mod ) )
145 newList.append( mod );
152 /*!Load modules from information list.
153 * \warning If some of modules not loaded, error message appear on desktop.
155 void CAM_Application::loadModules()
157 for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end(); ++it )
159 CAM_Module* mod = loadModule( (*it).title );
163 SUIT_MessageBox::error1( desktop(), tr( "Loading modules" ),
164 tr( "Can not load module %1" ).arg( (*it).title ), tr( "Ok" ) );
168 /*!Load module with name \a modName.
169 *\param modName - module name for loading.
170 *\warning If information list is empty.
171 *\warning If module library (for module with \a modName) is empty.
172 *\warning If module library is not loaded.
174 CAM_Module* CAM_Application::loadModule( const QString& modName )
176 if ( myInfoList.isEmpty() )
178 qWarning( tr( "Modules configuration is not defined." ) );
182 QString libName = moduleLibrary( modName );
183 if ( libName.isEmpty() )
185 qWarning( tr( "Information about module \"%1\" doesn't exist." ).arg( modName ) );
190 GET_MODULE_FUNC crtInst = 0;
193 HINSTANCE modLib = ::LoadLibrary( libName );
197 ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
198 FORMAT_MESSAGE_IGNORE_INSERTS, 0, ::GetLastError(), 0, (LPTSTR)&lpMsgBuf, 0, 0 );
199 err = QString( "Failed to load %1. %2" ).arg( libName ).arg( (LPTSTR)lpMsgBuf );
200 ::LocalFree( lpMsgBuf );
204 crtInst = (GET_MODULE_FUNC)::GetProcAddress( modLib, GET_MODULE_NAME );
208 ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
209 FORMAT_MESSAGE_IGNORE_INSERTS, 0, ::GetLastError(), 0, (LPTSTR)&lpMsgBuf, 0, 0 );
210 err = QString( "Failed to find %1 function. %2" ).arg( GET_MODULE_NAME ).arg( (LPTSTR)lpMsgBuf );
211 ::LocalFree( lpMsgBuf );
215 void* modLib = dlopen( (char*)libName.latin1(), RTLD_LAZY );
217 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
220 crtInst = (GET_MODULE_FUNC)dlsym( modLib, GET_MODULE_NAME );
222 err = QString( "Failed to find function %1. %2" ).arg( GET_MODULE_NAME ).arg( dlerror() );
226 CAM_Module* module = crtInst ? crtInst() : 0;
229 module->setModuleName( modName );
230 module->setName( moduleName( modName ) );
233 if ( !err.isEmpty() )
234 SUIT_MessageBox::warn1( desktop(), tr( "Error" ), err, tr( "Ok" ) );
239 /**@name Activate module group.*/
241 /*!Activate module with name \a modName.
242 *\param modName - module name.
243 *\ratval true, if module loaded and activated successful, else false.
245 bool CAM_Application::activateModule( const QString& modName )
247 if ( !modName.isEmpty() && !activeStudy() )
251 if ( !modName.isEmpty() )
253 CAM_Module* mod = module( modName );
254 if ( !mod && !moduleLibrary( modName ).isEmpty() )
256 mod = loadModule( modName );
261 res = activateModule( mod );
264 res = activateModule( 0 );
269 /*!Activate module \a mod
270 *\param mod - module for activation.
271 *\retval true - if all sucessful.
272 *\warning Error message if module not activated in active study.
274 bool CAM_Application::activateModule( CAM_Module* mod )
276 if ( mod && !activeStudy() )
279 if ( myModule == mod )
284 if ( !myModule->deactivateModule( activeStudy() ) )
292 // Connect the module to the active study
293 CAM_Study* camStudy = dynamic_cast<CAM_Study*>( activeStudy() );
295 CAM_DataModel* prev = 0;
296 for ( ModuleListIterator it( myModules ); it.current(); ++it ) {
297 CAM_DataModel* dm = it.current()->dataModel();
298 if ( it.current() == myModule && !camStudy->containsDataModel( dm ) ){
300 camStudy->insertDataModel( it.current()->dataModel(), prev );
302 camStudy->insertDataModel( it.current()->dataModel(), 0 );
307 if ( !myModule->activateModule( activeStudy() ) )
309 myModule->setMenuShown( false );
310 myModule->setToolShown( false );
311 SUIT_MessageBox::error1( desktop(), tr( "ERROR_TLT" ), tr( "ERROR_ACTIVATE_MODULE_MSG" ).arg( myModule->moduleName() ), tr( "BUT_OK" ) );
317 updateCommandsStatus();
323 /*!Create new study for current application.
324 *\retval study pointer.
326 SUIT_Study* CAM_Application::createNewStudy()
328 return new CAM_Study( this );
331 /*!Update commands status for parent class and for current class(if module is active)*/
332 void CAM_Application::updateCommandsStatus()
334 STD_Application::updateCommandsStatus();
336 if ( activeModule() )
337 activeModule()->updateCommandsStatus();
340 /*!Close all modules in study \a theDoc.
341 *\param theDoc - study
343 void CAM_Application::beforeCloseDoc( SUIT_Study* theDoc )
345 for ( ModuleListIterator it( myModules ); it.current(); ++it )
346 it.current()->studyClosed( theDoc );
349 /*!Sets active study for parent class.
350 *\param study - study.
352 void CAM_Application::setActiveStudy( SUIT_Study* study )
354 STD_Application::setActiveStudy( study );
358 void CAM_Application::moduleAdded( CAM_Module* mod )
360 // CAM_Study* study = dynamic_cast<CAM_Study*>( activeStudy() );
364 // study->insertDataModel( mod->dataModel() );
367 /*!Gets module name by title \a title
368 *\param title - title name
369 *\retval QString module name.
371 QString CAM_Application::moduleName( const QString& title ) const
374 for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end() && res.isEmpty(); ++it )
376 if ( (*it).title == title )
382 /*!Gets module title by module name \a name
383 *\param name - module name
384 *\retval QString module title.
386 QString CAM_Application::moduleTitle( const QString& name ) const
389 for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end() && res.isEmpty(); ++it )
391 if ( (*it).name == name )
397 /*!Get library name for module with title \a title.
398 *\param title - module title name.
399 *\param full - boolean flag (if true - return full library name, else internal name)
400 *\retval QString - library name.
402 QString CAM_Application::moduleLibrary( const QString& title, const bool full ) const
405 for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end() && res.isEmpty(); ++it )
407 if ( (*it).title == title )
408 res = (*it).internal;
410 if ( !res.isEmpty() && full )
411 res = SUIT_Tools::library( res );
415 /*!Read modules list*/
416 void CAM_Application::readModuleList()
418 if ( !myInfoList.isEmpty() )
421 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
423 QString modStr = resMgr->stringValue( "launch", "modules", QString::null );
424 QStringList modList = QStringList::split( ",", modStr );
426 for ( QStringList::const_iterator it = modList.begin(); it != modList.end(); ++it )
428 QString modName = (*it).stripWhiteSpace();
429 if ( modName.isEmpty() )
432 QString modTitle = resMgr->stringValue( *it, QString( "name" ), QString::null );
433 if ( modTitle.isEmpty() )
436 QString modLibrary = resMgr->stringValue( *it, QString( "library" ), QString::null ).stripWhiteSpace();
437 if ( !modLibrary.isEmpty() )
440 modLibrary = SUIT_Tools::file( modLibrary.stripWhiteSpace() );
441 libExt = QString( "so" );
442 if ( SUIT_Tools::extension( modLibrary ).lower() == libExt )
443 modLibrary = modLibrary.mid( 0, modLibrary.length() - libExt.length() - 1 );
444 libExt = QString( "dll" );
445 if ( SUIT_Tools::extension( modLibrary ).lower() == libExt )
446 modLibrary = modLibrary.mid( 0, modLibrary.length() - libExt.length() - 1 );
448 if ( modLibrary.startsWith( "lib" ) )
449 modLibrary = modLibrary.mid( 3 );
453 modLibrary = modName;
457 inf.title = modTitle;
458 inf.internal = modLibrary;
459 myInfoList.append( inf );
462 if ( myInfoList.isEmpty() )
463 SUIT_MessageBox::error1( 0, tr( "Error" ), tr( "Can not load modules configuration file " ), tr( "Ok" ) );
466 /*!Add common items for popup menu ( if they are exist )
467 *\param type - type of popup menu
468 *\param thePopup - popup menu
469 *\param title - title of popup menu
471 void CAM_Application::contextMenuPopup( const QString& type, QPopupMenu* thePopup, QString& title )
473 // to do : add common items for popup menu ( if they are exist )
474 if ( activeModule() )
475 activeModule()->contextMenuPopup( type, thePopup, title );
478 /*!Create empty study.*/
479 void CAM_Application::createEmptyStudy()
481 SUIT_Study* study = activeStudy();
483 STD_Application::createEmptyStudy();
485 SUIT_Study* newStudy = activeStudy();
486 if ( study != newStudy )
488 CAM_Study* camStudy = dynamic_cast<CAM_Study*>( newStudy );
489 for ( ModuleListIterator it( myModules ); it.current(); ++it )
490 camStudy->insertDataModel( it.current()->dataModel() );