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 extern "C" CAM_EXPORT SUIT_Application* createApplication()
24 return new CAM_Application();
27 CAM_Application::CAM_Application( const bool autoLoad )
30 myAutoLoad( autoLoad )
35 CAM_Application::~CAM_Application()
39 void CAM_Application::start()
44 STD_Application::start();
47 CAM_Module* CAM_Application::activeModule() const
52 CAM_Module* CAM_Application::module( const QString& modName ) const
55 for ( ModuleListIterator it( myModules ); it.current() && !mod; ++it )
56 if ( it.current()->moduleName() == modName )
61 CAM_Application::ModuleListIterator CAM_Application::modules() const
63 return ModuleListIterator( myModules );
66 void CAM_Application::modules( CAM_Application::ModuleList& out ) const
68 out.setAutoDelete( false );
71 for ( ModuleListIterator it( myModules ); it.current(); ++it )
72 out.append( it.current() );
75 void CAM_Application::modules( QStringList& lst, const bool loaded ) const
80 for ( ModuleListIterator it( myModules ); it.current(); ++it )
81 lst.append( it.current()->moduleName() );
83 for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end(); ++it )
84 lst.append( (*it).title );
87 void CAM_Application::addModule( CAM_Module* mod )
89 if ( !mod || myModules.contains( mod ) )
92 mod->initialize( this );
94 QMap<CAM_Module*, int> map;
97 for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end(); ++it )
99 if ( (*it).title == mod->moduleName() )
100 newList.append( mod );
103 CAM_Module* curMod = module( (*it).title );
105 newList.append( curMod );
107 if ( !newList.isEmpty() )
108 map.insert( newList.getLast(), 0 );
111 for ( ModuleListIterator itr( myModules ); itr.current(); ++itr )
113 if ( !map.contains( itr.current() ) )
114 newList.append( itr.current() );
117 if ( !map.contains( mod ) )
118 newList.append( mod );
125 void CAM_Application::loadModules()
127 for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end(); ++it )
129 CAM_Module* mod = loadModule( (*it).title );
133 SUIT_MessageBox::error1( desktop(), tr( "Loading modules" ),
134 tr( "Can not load module %1" ).arg( (*it).title ), tr( "Ok" ) );
138 CAM_Module* CAM_Application::loadModule( const QString& modName )
140 if ( myInfoList.isEmpty() )
142 qWarning( tr( "Modules configuration is not defined." ) );
146 QString libName = moduleLibrary( modName );
147 if ( libName.isEmpty() )
149 qWarning( tr( "Information about module \"%1\" doesn't exist." ).arg( modName ) );
154 GET_MODULE_FUNC crtInst = 0;
157 HINSTANCE modLib = ::LoadLibrary( libName );
161 ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
162 FORMAT_MESSAGE_IGNORE_INSERTS, 0, ::GetLastError(), 0, (LPTSTR)&lpMsgBuf, 0, 0 );
163 err = QString( "Failed to load %1. %2" ).arg( libName ).arg( (LPTSTR)lpMsgBuf );
164 ::LocalFree( lpMsgBuf );
168 crtInst = (GET_MODULE_FUNC)::GetProcAddress( modLib, GET_MODULE_NAME );
172 ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
173 FORMAT_MESSAGE_IGNORE_INSERTS, 0, ::GetLastError(), 0, (LPTSTR)&lpMsgBuf, 0, 0 );
174 err = QString( "Failed to find %1 function. %2" ).arg( GET_MODULE_NAME ).arg( (LPTSTR)lpMsgBuf );
175 ::LocalFree( lpMsgBuf );
179 void* modLib = dlopen( (char*)libName.latin1(), RTLD_LAZY );
181 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
184 crtInst = (GET_MODULE_FUNC)dlsym( modLib, GET_MODULE_NAME );
186 err = QString( "Failed to find function %1. %2" ).arg( GET_MODULE_NAME ).arg( dlerror() );
190 CAM_Module* module = crtInst ? crtInst() : 0;
193 module->setModuleName( modName );
194 module->setName( moduleName( modName ) );
197 if ( !err.isEmpty() )
198 SUIT_MessageBox::warn1( desktop(), tr( "Error" ), err, tr( "Ok" ) );
203 bool CAM_Application::activateModule( const QString& modName )
205 if ( !modName.isEmpty() && !activeStudy() )
209 if ( !modName.isEmpty() )
211 CAM_Module* mod = module( modName );
212 if ( !mod && !moduleLibrary( modName ).isEmpty() )
214 mod = loadModule( modName );
219 res = activateModule( mod );
222 res = activateModule( 0 );
227 bool CAM_Application::activateModule( CAM_Module* mod )
229 if ( mod && !activeStudy() )
232 if ( myModule == mod )
236 myModule->deactivateModule( activeStudy() );
241 // Connect the module to the active study
242 CAM_Study* camStudy = dynamic_cast<CAM_Study*>( activeStudy() );
244 CAM_DataModel* prev = 0;
245 for ( ModuleListIterator it( myModules ); it.current(); ++it ) {
246 CAM_DataModel* dm = it.current()->dataModel();
247 if ( it.current() == myModule && !camStudy->containsDataModel( dm ) ){
249 camStudy->insertDataModel( it.current()->dataModel(), prev );
251 camStudy->insertDataModel( it.current()->dataModel(), 0 );
256 myModule->activateModule( activeStudy() );
259 updateCommandsStatus();
264 SUIT_Study* CAM_Application::createNewStudy()
266 return new CAM_Study( this );
269 void CAM_Application::updateCommandsStatus()
271 STD_Application::updateCommandsStatus();
273 if ( activeModule() )
274 activeModule()->updateCommandsStatus();
277 void CAM_Application::beforeCloseDoc( SUIT_Study* theDoc )
279 for ( ModuleListIterator it( myModules ); it.current(); ++it )
280 it.current()->studyClosed( theDoc );
283 void CAM_Application::setActiveStudy( SUIT_Study* study )
285 STD_Application::setActiveStudy( study );
288 void CAM_Application::moduleAdded( CAM_Module* mod )
290 // CAM_Study* study = dynamic_cast<CAM_Study*>( activeStudy() );
294 // study->insertDataModel( mod->dataModel() );
297 QString CAM_Application::moduleName( const QString& title ) const
300 for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end() && res.isEmpty(); ++it )
302 if ( (*it).title == title )
308 QString CAM_Application::moduleTitle( const QString& name ) const
311 for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end() && res.isEmpty(); ++it )
313 if ( (*it).name == name )
319 QString CAM_Application::moduleLibrary( const QString& title, const bool full ) const
322 for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end() && res.isEmpty(); ++it )
324 if ( (*it).title == title )
325 res = (*it).internal;
327 if ( !res.isEmpty() && full )
328 res = SUIT_Tools::library( res );
332 void CAM_Application::readModuleList()
334 if ( !myInfoList.isEmpty() )
337 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
339 QString modStr = resMgr->stringValue( "launch", "modules", QString::null );
340 QStringList modList = QStringList::split( ",", modStr );
342 for ( QStringList::const_iterator it = modList.begin(); it != modList.end(); ++it )
344 QString modName = (*it).stripWhiteSpace();
345 if ( modName.isEmpty() )
348 QString modTitle = resMgr->stringValue( *it, QString( "name" ), QString::null );
349 if ( modTitle.isEmpty() )
352 QString modLibrary = resMgr->stringValue( *it, QString( "library" ), QString::null ).stripWhiteSpace();
353 if ( !modLibrary.isEmpty() )
356 modLibrary = SUIT_Tools::file( modLibrary.stripWhiteSpace() );
357 libExt = QString( "so" );
358 if ( SUIT_Tools::extension( modLibrary ).lower() == libExt )
359 modLibrary = modLibrary.mid( 0, modLibrary.length() - libExt.length() - 1 );
360 libExt = QString( "dll" );
361 if ( SUIT_Tools::extension( modLibrary ).lower() == libExt )
362 modLibrary = modLibrary.mid( 0, modLibrary.length() - libExt.length() - 1 );
364 if ( modLibrary.startsWith( "lib" ) )
365 modLibrary = modLibrary.mid( 3 );
369 modLibrary = modName;
373 inf.title = modTitle;
374 inf.internal = modLibrary;
375 myInfoList.append( inf );
378 if ( myInfoList.isEmpty() )
379 SUIT_MessageBox::error1( 0, tr( "Error" ), tr( "Can not load modules configuration file " ), tr( "Ok" ) );
382 void CAM_Application::contextMenuPopup( const QString& type, QPopupMenu* thePopup, QString& title )
384 // to do : add common items for popup menu ( if they are exist )
385 if ( activeModule() )
386 activeModule()->contextMenuPopup( type, thePopup, title );