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>
15 #include <qapplication.h>
23 /*!Create new instance of CAM_Application*/
24 extern "C" CAM_EXPORT SUIT_Application* createApplication()
26 return new CAM_Application();
29 /*!Constructor. read module list.
30 * \param autoLoad - auto load flag.
32 CAM_Application::CAM_Application( const bool autoLoad )
35 myAutoLoad( autoLoad )
40 /*!Destructor. Do nothing.*/
41 CAM_Application::~CAM_Application()
45 /*! Load modules, if \a myAutoLoad flag is true.\n
46 * Start application - call start() method from parent class.
48 void CAM_Application::start()
53 STD_Application::start();
57 * \retval CAM_Module - active module.
59 CAM_Module* CAM_Application::activeModule() const
64 /*!Get module with name \a modName from modules list.
65 * \retval CAM_Module pointer - module.
67 CAM_Module* CAM_Application::module( const QString& modName ) const
70 for ( ModuleListIterator it( myModules ); it.current() && !mod; ++it )
71 if ( it.current()->moduleName() == modName )
76 /*!Gets modules iterator.*/
77 CAM_Application::ModuleListIterator CAM_Application::modules() const
79 return ModuleListIterator( myModules );
83 * \param out - output list of modules.
85 void CAM_Application::modules( CAM_Application::ModuleList& out ) const
87 out.setAutoDelete( false );
90 for ( ModuleListIterator it( myModules ); it.current(); ++it )
91 out.append( it.current() );
94 /*!Gets list of names for modules.\n
95 * Get loaded modules names, if \a loaded is true, else \n
96 * get names from information list.
97 * \param lst - output list of names.
98 * \param loaded - boolean flag.
100 void CAM_Application::modules( QStringList& lst, const bool loaded ) const
105 for ( ModuleListIterator it( myModules ); it.current(); ++it )
106 lst.append( it.current()->moduleName() );
108 for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end(); ++it )
109 lst.append( (*it).title );
112 /*!Adding module \a mod to list.
113 *\param mod - module.
115 void CAM_Application::addModule( CAM_Module* mod )
117 if ( !mod || myModules.contains( mod ) )
120 mod->initialize( this );
122 QMap<CAM_Module*, int> map;
125 for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end(); ++it )
127 if ( (*it).title == mod->moduleName() )
128 newList.append( mod );
131 CAM_Module* curMod = module( (*it).title );
133 newList.append( curMod );
135 if ( !newList.isEmpty() )
136 map.insert( newList.getLast(), 0 );
139 for ( ModuleListIterator itr( myModules ); itr.current(); ++itr )
141 if ( !map.contains( itr.current() ) )
142 newList.append( itr.current() );
145 if ( !map.contains( mod ) )
146 newList.append( mod );
153 /*!Load modules from information list.
154 * \warning If some of modules not loaded, error message appear on desktop.
156 void CAM_Application::loadModules()
158 for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end(); ++it )
160 CAM_Module* mod = loadModule( (*it).title );
164 SUIT_MessageBox::error1( desktop(), tr( "Loading modules" ),
165 tr( "Can not load module %1" ).arg( (*it).title ), tr( "Ok" ) );
169 /*!Load module with name \a modName.
170 *\param modName - module name for loading.
171 *\warning If information list is empty.
172 *\warning If module library (for module with \a modName) is empty.
173 *\warning If module library is not loaded.
175 CAM_Module* CAM_Application::loadModule( const QString& modName )
177 if ( myInfoList.isEmpty() )
179 qWarning( tr( "Modules configuration is not defined." ) );
183 QString libName = moduleLibrary( modName );
184 if ( libName.isEmpty() )
186 qWarning( tr( "Information about module \"%1\" doesn't exist." ).arg( modName ) );
191 GET_MODULE_FUNC crtInst = 0;
194 HINSTANCE modLib = ::LoadLibrary( libName );
198 ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
199 FORMAT_MESSAGE_IGNORE_INSERTS, 0, ::GetLastError(), 0, (LPTSTR)&lpMsgBuf, 0, 0 );
200 err = QString( "Failed to load %1. %2" ).arg( libName ).arg( (LPTSTR)lpMsgBuf );
201 ::LocalFree( lpMsgBuf );
205 crtInst = (GET_MODULE_FUNC)::GetProcAddress( modLib, GET_MODULE_NAME );
209 ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
210 FORMAT_MESSAGE_IGNORE_INSERTS, 0, ::GetLastError(), 0, (LPTSTR)&lpMsgBuf, 0, 0 );
211 err = QString( "Failed to find %1 function. %2" ).arg( GET_MODULE_NAME ).arg( (LPTSTR)lpMsgBuf );
212 ::LocalFree( lpMsgBuf );
216 void* modLib = dlopen( (char*)libName.latin1(), RTLD_LAZY );
218 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
221 crtInst = (GET_MODULE_FUNC)dlsym( modLib, GET_MODULE_NAME );
223 err = QString( "Failed to find function %1. %2" ).arg( GET_MODULE_NAME ).arg( dlerror() );
227 CAM_Module* module = crtInst ? crtInst() : 0;
230 module->setModuleName( modName );
231 module->setName( moduleName( modName ) );
234 if ( !err.isEmpty() )
235 SUIT_MessageBox::warn1( desktop(), tr( "Error" ), err, tr( "Ok" ) );
240 /**@name Activate module group.*/
242 /*!Activate module with name \a modName.
243 *\param modName - module name.
244 *\ratval true, if module loaded and activated successful, else false.
246 bool CAM_Application::activateModule( const QString& modName )
248 if ( !modName.isEmpty() && !activeStudy() )
252 if ( !modName.isEmpty() )
254 CAM_Module* mod = module( modName );
255 if ( !mod && !moduleLibrary( modName ).isEmpty() )
257 mod = loadModule( modName );
262 res = activateModule( mod );
265 res = activateModule( 0 );
270 /*!Activate module \a mod
271 *\param mod - module for activation.
272 *\retval true - if all sucessful.
273 *\warning Error message if module not activated in active study.
275 bool CAM_Application::activateModule( CAM_Module* mod )
277 if ( mod && !activeStudy() )
280 if ( myModule == mod )
285 if ( !myModule->deactivateModule( activeStudy() ) )
293 // Connect the module to the active study
294 myModule->connectToStudy( dynamic_cast<CAM_Study*>( activeStudy() ) );
295 if ( !myModule->activateModule( activeStudy() ) )
297 myModule->setMenuShown( false );
298 myModule->setToolShown( false );
299 SUIT_MessageBox::error1( desktop(), tr( "ERROR_TLT" ), tr( "ERROR_ACTIVATE_MODULE_MSG" ).arg( myModule->moduleName() ), tr( "BUT_OK" ) );
305 updateCommandsStatus();
311 /*!Create new study for current application.
312 *\retval study pointer.
314 SUIT_Study* CAM_Application::createNewStudy()
316 return new CAM_Study( this );
319 /*!Update commands status for parent class and for current class(if module is active)*/
320 void CAM_Application::updateCommandsStatus()
322 STD_Application::updateCommandsStatus();
324 if ( activeModule() )
325 activeModule()->updateCommandsStatus();
328 /*!Close all modules in study \a theDoc.
329 *\param theDoc - study
331 void CAM_Application::beforeCloseDoc( SUIT_Study* theDoc )
333 for ( ModuleListIterator it( myModules ); it.current(); ++it )
334 it.current()->studyClosed( theDoc );
337 /*!Sets active study for parent class.
338 *\param study - study.
340 void CAM_Application::setActiveStudy( SUIT_Study* study )
342 STD_Application::setActiveStudy( study );
346 void CAM_Application::moduleAdded( CAM_Module* mod )
348 // CAM_Study* study = dynamic_cast<CAM_Study*>( activeStudy() );
352 // study->insertDataModel( mod->dataModel() );
355 /*!Gets module name by title \a title
356 *\param title - title name
357 *\retval QString module name.
359 QString CAM_Application::moduleName( const QString& title ) const
362 for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end() && res.isEmpty(); ++it )
364 if ( (*it).title == title )
370 /*!Gets module title by module name \a name
371 *\param name - module name
372 *\retval QString module title.
374 QString CAM_Application::moduleTitle( const QString& name ) const
377 for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end() && res.isEmpty(); ++it )
379 if ( (*it).name == name )
385 /*!Get library name for module with title \a title.
386 *\param title - module title name.
387 *\param full - boolean flag (if true - return full library name, else internal name)
388 *\retval QString - library name.
390 QString CAM_Application::moduleLibrary( const QString& title, const bool full ) const
393 for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end() && res.isEmpty(); ++it )
395 if ( (*it).title == title )
396 res = (*it).internal;
398 if ( !res.isEmpty() && full )
399 res = SUIT_Tools::library( res );
403 /*!Read modules list*/
404 void CAM_Application::readModuleList()
406 if ( !myInfoList.isEmpty() )
409 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
413 // parse command line arguments
414 int nbArgs = qApp->argc();
415 char** CmdLine = qApp->argv();
417 for ( int i = 0; i < nbArgs; i++ )
419 CmdStr.append(CmdLine[i]);
422 int startId = CmdStr.find("--modules (");
423 if ( startId != -1 ) { // application launch with --modules option
424 startId = CmdStr.find("(", startId);
425 int stopId = CmdStr.find(" )", startId);
426 QString ModStr = CmdStr.mid( startId+1, stopId - (startId+1) ).stripWhiteSpace();
428 while ( i < ModStr.length() )
430 int nextId = ModStr.find( ":", i );
431 modList.append( ModStr.mid( i, nextId - i ).stripWhiteSpace() );
436 QString modStr = resMgr->stringValue( "launch", "modules", QString::null );
437 modList = QStringList::split( ",", modStr );
440 for ( QStringList::const_iterator it = modList.begin(); it != modList.end(); ++it )
442 QString modName = (*it).stripWhiteSpace();
443 if ( modName.isEmpty() )
446 QString modTitle = resMgr->stringValue( *it, QString( "name" ), QString::null );
447 if ( modTitle.isEmpty() )
450 QString modLibrary = resMgr->stringValue( *it, QString( "library" ), QString::null ).stripWhiteSpace();
451 if ( !modLibrary.isEmpty() )
454 modLibrary = SUIT_Tools::file( modLibrary.stripWhiteSpace() );
455 libExt = QString( "so" );
456 if ( SUIT_Tools::extension( modLibrary ).lower() == libExt )
457 modLibrary = modLibrary.mid( 0, modLibrary.length() - libExt.length() - 1 );
458 libExt = QString( "dll" );
459 if ( SUIT_Tools::extension( modLibrary ).lower() == libExt )
460 modLibrary = modLibrary.mid( 0, modLibrary.length() - libExt.length() - 1 );
462 if ( modLibrary.startsWith( "lib" ) )
463 modLibrary = modLibrary.mid( 3 );
467 modLibrary = modName;
471 inf.title = modTitle;
472 inf.internal = modLibrary;
473 myInfoList.append( inf );
476 if ( myInfoList.isEmpty() )
477 SUIT_MessageBox::error1( 0, tr( "Error" ), tr( "Can not load modules configuration file " ), tr( "Ok" ) );
480 /*!Add common items for popup menu ( if they are exist )
481 *\param type - type of popup menu
482 *\param thePopup - popup menu
483 *\param title - title of popup menu
485 void CAM_Application::contextMenuPopup( const QString& type, QPopupMenu* thePopup, QString& title )
487 // to do : add common items for popup menu ( if they are exist )
488 if ( activeModule() )
489 activeModule()->contextMenuPopup( type, thePopup, title );
492 /*!Create empty study.*/
493 void CAM_Application::createEmptyStudy()
495 SUIT_Study* study = activeStudy();
497 STD_Application::createEmptyStudy();