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 )
237 if ( !myModule->deactivateModule( activeStudy() ) )
245 // Connect the module to the active study
246 CAM_Study* camStudy = dynamic_cast<CAM_Study*>( activeStudy() );
248 CAM_DataModel* prev = 0;
249 for ( ModuleListIterator it( myModules ); it.current(); ++it ) {
250 CAM_DataModel* dm = it.current()->dataModel();
251 if ( it.current() == myModule && !camStudy->containsDataModel( dm ) ){
253 camStudy->insertDataModel( it.current()->dataModel(), prev );
255 camStudy->insertDataModel( it.current()->dataModel(), 0 );
260 if ( !myModule->activateModule( activeStudy() ) )
262 myModule->setMenuShown( false );
263 myModule->setToolShown( false );
264 SUIT_MessageBox::error1( desktop(), tr( "ERROR_TLT" ), tr( "ERROR_ACTIVATE_MODULE_MSG" ).arg( myModule->moduleName() ), tr( "BUT_OK" ) );
270 updateCommandsStatus();
275 SUIT_Study* CAM_Application::createNewStudy()
277 return new CAM_Study( this );
280 void CAM_Application::updateCommandsStatus()
282 STD_Application::updateCommandsStatus();
284 if ( activeModule() )
285 activeModule()->updateCommandsStatus();
288 void CAM_Application::beforeCloseDoc( SUIT_Study* theDoc )
290 for ( ModuleListIterator it( myModules ); it.current(); ++it )
291 it.current()->studyClosed( theDoc );
294 void CAM_Application::setActiveStudy( SUIT_Study* study )
296 STD_Application::setActiveStudy( study );
299 void CAM_Application::moduleAdded( CAM_Module* mod )
301 // CAM_Study* study = dynamic_cast<CAM_Study*>( activeStudy() );
305 // study->insertDataModel( mod->dataModel() );
308 QString CAM_Application::moduleName( const QString& title ) const
311 for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end() && res.isEmpty(); ++it )
313 if ( (*it).title == title )
319 QString CAM_Application::moduleTitle( const QString& name ) const
322 for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end() && res.isEmpty(); ++it )
324 if ( (*it).name == name )
330 QString CAM_Application::moduleLibrary( const QString& title, const bool full ) const
333 for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end() && res.isEmpty(); ++it )
335 if ( (*it).title == title )
336 res = (*it).internal;
338 if ( !res.isEmpty() && full )
339 res = SUIT_Tools::library( res );
343 void CAM_Application::readModuleList()
345 if ( !myInfoList.isEmpty() )
348 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
350 QString modStr = resMgr->stringValue( "launch", "modules", QString::null );
351 QStringList modList = QStringList::split( ",", modStr );
353 for ( QStringList::const_iterator it = modList.begin(); it != modList.end(); ++it )
355 QString modName = (*it).stripWhiteSpace();
356 if ( modName.isEmpty() )
359 QString modTitle = resMgr->stringValue( *it, QString( "name" ), QString::null );
360 if ( modTitle.isEmpty() )
363 QString modLibrary = resMgr->stringValue( *it, QString( "library" ), QString::null ).stripWhiteSpace();
364 if ( !modLibrary.isEmpty() )
367 modLibrary = SUIT_Tools::file( modLibrary.stripWhiteSpace() );
368 libExt = QString( "so" );
369 if ( SUIT_Tools::extension( modLibrary ).lower() == libExt )
370 modLibrary = modLibrary.mid( 0, modLibrary.length() - libExt.length() - 1 );
371 libExt = QString( "dll" );
372 if ( SUIT_Tools::extension( modLibrary ).lower() == libExt )
373 modLibrary = modLibrary.mid( 0, modLibrary.length() - libExt.length() - 1 );
375 if ( modLibrary.startsWith( "lib" ) )
376 modLibrary = modLibrary.mid( 3 );
380 modLibrary = modName;
384 inf.title = modTitle;
385 inf.internal = modLibrary;
386 myInfoList.append( inf );
389 if ( myInfoList.isEmpty() )
390 SUIT_MessageBox::error1( 0, tr( "Error" ), tr( "Can not load modules configuration file " ), tr( "Ok" ) );
393 void CAM_Application::contextMenuPopup( const QString& type, QPopupMenu* thePopup, QString& title )
395 // to do : add common items for popup menu ( if they are exist )
396 if ( activeModule() )
397 activeModule()->contextMenuPopup( type, thePopup, title );
400 void CAM_Application::createEmptyStudy()
402 SUIT_Study* study = activeStudy();
404 STD_Application::createEmptyStudy();
406 SUIT_Study* newStudy = activeStudy();
407 if ( study != newStudy )
409 CAM_Study* camStudy = dynamic_cast<CAM_Study*>( newStudy );
410 for ( ModuleListIterator it( myModules ); it.current(); ++it )
411 camStudy->insertDataModel( it.current()->dataModel() );