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 myModule->connectToStudy( dynamic_cast<CAM_Study*>( activeStudy() ) );
294 if ( !myModule->activateModule( activeStudy() ) )
296 myModule->setMenuShown( false );
297 myModule->setToolShown( false );
298 SUIT_MessageBox::error1( desktop(), tr( "ERROR_TLT" ), tr( "ERROR_ACTIVATE_MODULE_MSG" ).arg( myModule->moduleName() ), tr( "BUT_OK" ) );
304 updateCommandsStatus();
310 /*!Create new study for current application.
311 *\retval study pointer.
313 SUIT_Study* CAM_Application::createNewStudy()
315 return new CAM_Study( this );
318 /*!Update commands status for parent class and for current class(if module is active)*/
319 void CAM_Application::updateCommandsStatus()
321 STD_Application::updateCommandsStatus();
323 if ( activeModule() )
324 activeModule()->updateCommandsStatus();
327 /*!Close all modules in study \a theDoc.
328 *\param theDoc - study
330 void CAM_Application::beforeCloseDoc( SUIT_Study* theDoc )
332 for ( ModuleListIterator it( myModules ); it.current(); ++it )
333 it.current()->studyClosed( theDoc );
336 /*!Sets active study for parent class.
337 *\param study - study.
339 void CAM_Application::setActiveStudy( SUIT_Study* study )
341 STD_Application::setActiveStudy( study );
345 void CAM_Application::moduleAdded( CAM_Module* mod )
347 // CAM_Study* study = dynamic_cast<CAM_Study*>( activeStudy() );
351 // study->insertDataModel( mod->dataModel() );
354 /*!Gets module name by title \a title
355 *\param title - title name
356 *\retval QString module name.
358 QString CAM_Application::moduleName( const QString& title ) const
361 for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end() && res.isEmpty(); ++it )
363 if ( (*it).title == title )
369 /*!Gets module title by module name \a name
370 *\param name - module name
371 *\retval QString module title.
373 QString CAM_Application::moduleTitle( const QString& name ) const
376 for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end() && res.isEmpty(); ++it )
378 if ( (*it).name == name )
384 /*!Get library name for module with title \a title.
385 *\param title - module title name.
386 *\param full - boolean flag (if true - return full library name, else internal name)
387 *\retval QString - library name.
389 QString CAM_Application::moduleLibrary( const QString& title, const bool full ) const
392 for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end() && res.isEmpty(); ++it )
394 if ( (*it).title == title )
395 res = (*it).internal;
397 if ( !res.isEmpty() && full )
398 res = SUIT_Tools::library( res );
402 /*!Read modules list*/
403 void CAM_Application::readModuleList()
405 if ( !myInfoList.isEmpty() )
408 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
410 QString modStr = resMgr->stringValue( "launch", "modules", QString::null );
411 QStringList modList = QStringList::split( ",", modStr );
413 for ( QStringList::const_iterator it = modList.begin(); it != modList.end(); ++it )
415 QString modName = (*it).stripWhiteSpace();
416 if ( modName.isEmpty() )
419 QString modTitle = resMgr->stringValue( *it, QString( "name" ), QString::null );
420 if ( modTitle.isEmpty() )
423 QString modLibrary = resMgr->stringValue( *it, QString( "library" ), QString::null ).stripWhiteSpace();
424 if ( !modLibrary.isEmpty() )
427 modLibrary = SUIT_Tools::file( modLibrary.stripWhiteSpace() );
428 libExt = QString( "so" );
429 if ( SUIT_Tools::extension( modLibrary ).lower() == libExt )
430 modLibrary = modLibrary.mid( 0, modLibrary.length() - libExt.length() - 1 );
431 libExt = QString( "dll" );
432 if ( SUIT_Tools::extension( modLibrary ).lower() == libExt )
433 modLibrary = modLibrary.mid( 0, modLibrary.length() - libExt.length() - 1 );
435 if ( modLibrary.startsWith( "lib" ) )
436 modLibrary = modLibrary.mid( 3 );
440 modLibrary = modName;
444 inf.title = modTitle;
445 inf.internal = modLibrary;
446 myInfoList.append( inf );
449 if ( myInfoList.isEmpty() )
450 SUIT_MessageBox::error1( 0, tr( "Error" ), tr( "Can not load modules configuration file " ), tr( "Ok" ) );
453 /*!Add common items for popup menu ( if they are exist )
454 *\param type - type of popup menu
455 *\param thePopup - popup menu
456 *\param title - title of popup menu
458 void CAM_Application::contextMenuPopup( const QString& type, QPopupMenu* thePopup, QString& title )
460 // to do : add common items for popup menu ( if they are exist )
461 if ( activeModule() )
462 activeModule()->contextMenuPopup( type, thePopup, title );
465 /*!Create empty study.*/
466 void CAM_Application::createEmptyStudy()
468 SUIT_Study* study = activeStudy();
470 STD_Application::createEmptyStudy();
472 SUIT_Study* newStudy = activeStudy();
473 if ( study != newStudy )
475 CAM_Study* camStudy = dynamic_cast<CAM_Study*>( newStudy );
476 for ( ModuleListIterator it( myModules ); it.current(); ++it )
477 camStudy->insertDataModel( it.current()->dataModel() );