1 // Copyright (C) 2007-2021 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "CAM_Application.h"
25 #include "CAM_Study.h"
26 #include "CAM_Module.h"
28 #include <SUIT_Tools.h>
29 #include <SUIT_Desktop.h>
30 #include <SUIT_Session.h>
31 #include <SUIT_MessageBox.h>
32 #include <SUIT_ResourceMgr.h>
34 #include <KERNEL_version.h>
35 #include <GUI_version.h>
37 #include <QApplication>
54 BusyLocker( bool& busy ) : myPrev( busy ), myBusy( busy ) { myBusy = true; }
55 ~BusyLocker() { myBusy = myPrev; }
63 \brief Create new instance of CAM_Application.
64 \return new instance of CAM_Application class
66 extern "C" CAM_EXPORT SUIT_Application* createApplication()
68 return new CAM_Application();
72 \class CAM_Application
73 \brief Introduces an application class which provides modular architecture.
75 This class defines multi-modular application configuration and behaviour.
76 Each module (CAM_Module) can have own data model, document windows and
79 An application provides all necessary functionality for modules management,
82 - modules activation/deactivation
86 CAM_Application::ModuleInfoList CAM_Application::myInfoList;
91 Read modules list (from command line or from resource file).
92 If \a autoLoad parameter is \c true all the modules will be loaded
93 immediately after application starting, otherwise each module will
94 be loaded by demand (with activateModule()).
96 \param autoLoad auto loading flag
98 CAM_Application::CAM_Application( const bool autoLoad )
101 myAutoLoad( autoLoad ),
110 Does nothing currently.
112 CAM_Application::~CAM_Application()
114 for ( QList<CAM_Module*>::const_iterator it = myModules.begin(); it != myModules.end(); ++it )
120 \brief Start an application.
122 Load all modules, if "auto loading" flag has been set to \c true.
124 \sa CAM_Application()
126 void CAM_Application::start()
129 for ( ModuleInfoList::iterator it = myInfoList.begin();
130 it != myInfoList.end(); ++it )
132 if ( (*it).status == stUnknown )
133 (*it).status = checkModule( (*it).title ) ? stReady : stInaccessible;
140 STD_Application::start();
144 \brief Get active module.
145 \return active module or 0 if there are no any
147 CAM_Module* CAM_Application::activeModule() const
153 \brief Get the module with specified name.
154 \return module or 0 if not found
156 CAM_Module* CAM_Application::module( const QString& modName ) const
159 for ( QList<CAM_Module*>::const_iterator it = myModules.begin();
160 it != myModules.end() && !mod; ++it )
161 if ( (*it)->moduleName() == modName )
167 \brief Get all loaded modules.
168 \return list of modules
170 CAM_Application::ModuleList CAM_Application::modules() const
176 \brief Get all loaded modules.
177 \param returning list of modules
179 void CAM_Application::modules( CAM_Application::ModuleList& out ) const
183 for ( QList<CAM_Module*>::const_iterator it = myModules.begin();
184 it != myModules.end(); ++it )
189 \brief Get names of all modules.
191 Get loaded modules names if \a loaded is \c true,
192 otherwise get all avaiable modules names.
194 \param lst output list of modules names
195 \param loaded boolean flag, defines what modules names to return
197 void CAM_Application::modules( QStringList& lst, const bool loaded ) const
203 for ( QList<CAM_Module*>::const_iterator it = myModules.begin();
204 it != myModules.end(); ++it )
205 lst.append( (*it)->moduleName() );
209 for ( ModuleInfoList::const_iterator it = myInfoList.begin();
210 it != myInfoList.end(); ++it )
211 if ( (*it).status != stNoGui )
212 lst.append( (*it).title );
217 \brief Add module \a mod to the modules list.
219 Performes module initialization. Does nothing if the module
222 \param mod module being added
223 \sa CAM_Module::initialize()
225 void CAM_Application::addModule( CAM_Module* mod )
227 if ( !mod || myModules.contains( mod ) )
230 mod->initialize( this );
232 QMap<CAM_Module*, int> map;
235 for ( ModuleInfoList::const_iterator it = myInfoList.begin();
236 it != myInfoList.end(); ++it )
238 if ( (*it).title == mod->moduleName() )
239 newList.append( mod );
242 CAM_Module* curMod = module( (*it).title );
244 newList.append( curMod );
248 for ( QList<CAM_Module*>::const_iterator it = myModules.begin();
249 it != myModules.end(); ++it )
251 if ( !newList.contains( *it ) )
252 newList.append( *it );
255 if ( !newList.contains( mod ) )
256 newList.append( mod );
264 \brief Load modules from the modules information list.
266 If some module can not be loaded, an error message is shown.
268 void CAM_Application::loadModules()
270 for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end(); ++it )
272 CAM_Module* mod = loadModule( (*it).title );
276 QString wrn = tr( "Can not load module %1" ).arg( (*it).title );
277 if ( desktop() && desktop()->isVisible() )
278 SUIT_MessageBox::critical( desktop(), tr( "Loading modules" ), wrn );
280 qWarning( qPrintable( wrn ) );
286 \brief Load module \a modName.
288 The function prints warning message if:
289 - modules information list is empty
290 - modules information list does not include specified module info
291 - module library can not be loaded by some reason
293 \param modName module name
294 \return module object pointer or 0 if module could not be loaded
296 CAM_Module* CAM_Application::loadModule( const QString& modName, const bool showMsg )
298 if ( myInfoList.isEmpty() )
300 qWarning( qPrintable( tr( "Modules configuration is not defined." ) ) );
304 QString libName = moduleLibrary( modName );
305 if ( libName.isEmpty() )
307 qWarning( qPrintable( tr( "Information about module \"%1\" doesn't exist." ).arg( modName ) ) );
312 GET_MODULE_FUNC crtInst = 0;
313 GET_VERSION_FUNC getVersion = 0;
317 LPTSTR str_libname = new TCHAR[libName.length() + 1];
318 str_libname[libName.toWCharArray(str_libname)] = '\0';
320 QByteArray arr = libName.toLatin1();
321 LPTSTR str_libname = arr.constData();
323 HINSTANCE modLib = ::LoadLibrary( str_libname );
330 ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
331 FORMAT_MESSAGE_IGNORE_INSERTS, 0, ::GetLastError(), 0, (LPTSTR)&lpMsgBuf, 0, 0 );
333 QString out_err = QString::fromWCharArray((LPTSTR)lpMsgBuf);
335 QString out_err = (LPTSTR)lpMsgBuf;
337 err = QString( "Failed to load %1. %2" ).arg( libName ).arg(out_err);
338 ::LocalFree( lpMsgBuf );
342 crtInst = (GET_MODULE_FUNC)::GetProcAddress( modLib, GET_MODULE_NAME );
346 ::FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
347 FORMAT_MESSAGE_IGNORE_INSERTS, 0, ::GetLastError(), 0, (LPTSTR)&lpMsgBuf, 0, 0 );
349 QString out_err = QString::fromWCharArray((LPTSTR)lpMsgBuf);
351 QString out_err = (LPTSTR)lpMsgBuf;
354 err = QString( "Failed to find %1 function. %2" ).arg( GET_MODULE_NAME ).arg( out_err );
355 ::LocalFree( lpMsgBuf );
358 getVersion = (GET_VERSION_FUNC)::GetProcAddress( modLib, GET_VERSION_NAME );
361 void* modLib = dlopen( libName.toUtf8(), RTLD_LAZY | RTLD_GLOBAL );
363 err = QString( "Can not load library %1. %2" ).arg( libName ).arg( dlerror() );
366 crtInst = (GET_MODULE_FUNC)dlsym( modLib, GET_MODULE_NAME );
368 err = QString( "Failed to find function %1. %2" ).arg( GET_MODULE_NAME ).arg( dlerror() );
370 getVersion = (GET_VERSION_FUNC)dlsym( modLib, GET_VERSION_NAME );
374 CAM_Module* module = crtInst ? crtInst() : 0;
377 module->setModuleName( modName );
378 module->setName( moduleName( modName ) );
381 if ( !err.isEmpty() && showMsg ) {
382 if ( desktop() && desktop()->isVisible() )
383 SUIT_MessageBox::warning( desktop(), tr( "Error" ), err );
385 qWarning( qPrintable( err ) );
388 char* version = getVersion ? getVersion() : 0;
391 for ( ModuleInfoList::iterator it = myInfoList.begin(); it != myInfoList.end(); ++it ) {
392 if ( (*it).title == modName ) {
393 if( (*it).version.isEmpty() ) {
394 (*it).version = QString(version);
405 \brief Activate module \a modName.
406 \param modName module name
407 \return \c true, if module is loaded and activated successfully and \c false otherwise
409 bool CAM_Application::activateModule( const QString& modName )
411 if ( (!modName.isEmpty() && !activeStudy()) || myBlocked )
414 // VSR 25/10/2011: prevent nested activation/deactivation
415 // See issues 0021307, 0021373
416 BusyLocker lock( myBlocked );
419 if ( !modName.isEmpty() )
421 setProperty("activateModule", true);
422 CAM_Module* mod = module( modName );
424 mod = loadModule( modName );
428 res = activateModule( mod );
429 setProperty("activateModule", QVariant());
432 res = activateModule( 0 );
438 \brief Activate module \a mod.
440 Shows error message if module could not be activated in the current study.
442 \param mod module object pointer
443 \return \c true, if module is loaded and activated successfully and \c false otherwise
445 bool CAM_Application::activateModule( CAM_Module* mod )
447 if ( mod && !activeStudy() )
450 if ( myModule == mod )
455 if ( !myModule->deactivateModule( activeStudy() ) )
463 // Connect the module to the active study
464 myModule->connectToStudy( dynamic_cast<CAM_Study*>( activeStudy() ) );
465 if ( !myModule->activateModule( activeStudy() ) )
467 myModule->setMenuShown( false );
468 myModule->setToolShown( false );
469 QString wrn = tr( "ERROR_ACTIVATE_MODULE_MSG" ).arg( myModule->moduleName() );
470 if ( desktop() && desktop()->isVisible() )
471 SUIT_MessageBox::critical( desktop(), tr( "ERROR_TLT" ), wrn );
473 qWarning( qPrintable( wrn ) );
479 updateCommandsStatus();
485 \brief Load module \a modName and activate its operation, corresponding to \a actionId.
486 This method is dedicated to run operations of some module from any other module.
487 \param modName module name
488 \param actionId is a numerical unique operation identifier
489 \return \c true in case of success and \c false otherwise
491 bool CAM_Application::activateOperation( const QString& modName, int actionId )
493 CAM_Module* mod = loadModule(modName, false);
496 return mod->activateOperation(actionId);
502 \brief Load module \a modName and activate its operation, corresponding to \a actionId.
503 This method is dedicated to run operations of some module from any other module.
504 \param modName module name
505 \param actionId is a string unique operation identifier
506 \return \c true in case of success and \c false otherwise
508 bool CAM_Application::activateOperation( const QString& modName, const QString& actionId )
510 CAM_Module* mod = loadModule(modName, false);
513 return mod->activateOperation(actionId);
519 \brief Load module \a modName and activate its operation,
520 corresponding to \a actionId and \a pluginName.
521 This method is dedicated to run operations of some module from any other module.
522 \param modName module name
523 \param actionId is a string unique operation identifier
524 \param pluginName is a name of a plugin where the operation is implemented
525 \return \c true in case of success and \c false otherwise
527 bool CAM_Application::activateOperation( const QString& modName,
528 const QString& actionId,
529 const QString& pluginName )
531 CAM_Module* mod = loadModule(modName, false);
534 return mod->activateOperation(actionId, pluginName);
540 \brief Create new study.
541 \return study object pointer
543 SUIT_Study* CAM_Application::createNewStudy()
545 return new CAM_Study( this );
549 \brief Update menu commands status.
551 void CAM_Application::updateCommandsStatus()
553 STD_Application::updateCommandsStatus();
555 if ( activeModule() )
556 activeModule()->updateCommandsStatus();
560 \brief Prepare application to study closing.
562 Closes all modules in study \a theDoc.
566 void CAM_Application::beforeCloseDoc( SUIT_Study* theDoc )
568 for ( QList<CAM_Module*>::iterator it = myModules.begin(); it != myModules.end(); ++it )
569 (*it)->studyClosed( theDoc );
572 void CAM_Application::afterCloseDoc()
577 \brief Set active study.
578 \param study study to be made active
580 void CAM_Application::setActiveStudy( SUIT_Study* study )
582 STD_Application::setActiveStudy( study );
586 \brief Check module availability.
588 The method can be redefined in successors. Default implementation returns \c true.
590 \param title module title
591 \return \c true if module is accessible; \c false otherwise
593 bool CAM_Application::checkModule( const QString& )
599 \brief Callback function, called when the module is added to the application.
601 This virtual method can be re-implemented in the successors. Base implementation
604 \param mod module being added
606 void CAM_Application::moduleAdded( CAM_Module* /*mod*/ )
611 \brief Get module name by its title (user name).
612 \param title module title (user name)
613 \return module name or null QString if module is not found
615 QString CAM_Application::moduleName( const QString& title )
618 for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end() && res.isEmpty(); ++it )
620 if ( (*it).title == title )
627 \brief Get module title (user name) by its name.
628 \param name module name
629 \return module title (user name) or null QString if module is not found
631 QString CAM_Application::moduleTitle( const QString& name )
634 for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end() && res.isEmpty(); ++it )
636 if ( (*it).name == name )
643 \brief Get module icon name.
644 \param name module name or title
645 \return module icon or null QString if module is not found
647 QString CAM_Application::moduleIcon( const QString& name )
650 for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end() && res.isNull(); ++it )
652 if ( (*it).name == name || (*it).title == name )
659 \brief Get module description.
660 \param name module name or title
661 \return module description or null QString if description is not provided in config file.
663 QString CAM_Application::moduleDescription( const QString& name )
666 for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end() && res.isNull(); ++it )
668 if ( (*it).name == name || (*it).title == name )
669 res = tr((*it).description.toUtf8());
675 \brief Get module library name by its title (user name).
676 \param title module name or title
677 \param full if \c true, return full library name, otherwise return its internal name
678 \return module library name or null QString if module is not found
680 QString CAM_Application::moduleLibrary( const QString& name, const bool full )
683 for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end() && res.isEmpty(); ++it )
685 if ( (*it).name == name || (*it).title == name )
688 if ( !res.isEmpty() && full )
689 res = SUIT_Tools::library( res );
694 \brief Get displayer proxy for given module, by its title (user name).
695 \param name module name or title
696 \return name of module which provides displayer for requested module
698 QString CAM_Application::moduleDisplayer( const QString& name )
702 if ( !name.isEmpty() )
704 for ( ModuleInfoList::const_iterator it = myInfoList.begin(); it != myInfoList.end() && res.isEmpty(); ++it )
706 if ( (*it).title == name || (*it).name == name ) {
707 res = (*it).displayer;
718 \brief Read modules information list
720 This function first tries to get the modules names list by parsing
721 the application command line arguments, looking for the
722 "--modules ( <mod_name>[:<mod_name>...] )" option.
723 List of modules is separated by colon symbol (":").
725 If "--modules" command line option is not used, the list of modules
726 is retrieved from the application resource file: parameter "modules" of
727 the section "launch".
729 Then the information about each module (module title (user name),
730 library name) is retrieved from the corresponding section of resource
731 file with help of resources manager.
733 Shows the warning message, if module information list is empty.
737 void CAM_Application::readModuleList()
739 if ( !myInfoList.isEmpty() )
742 SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
746 QString args = QApplication::arguments().join( " " );
748 QRegExp rx1("--modules=([\\w,]*)");
749 rx1.setMinimal( false );
750 QRegExp rx2("--modules\\s+\\(\\s*(.*)\\s*\\)");
751 rx2.setMinimal( true );
755 int pos1 = rx1.indexIn( args, pos );
756 int pos2 = rx2.indexIn( args, pos );
757 if ( pos1 != -1 && pos2 != -1 ) {
758 modules = pos1 < pos2 ? rx1.cap( 1 ) : rx2.cap(1);
759 pos = pos1 < pos2 ? pos1 + rx1.matchedLength() : pos2 + rx2.matchedLength();
761 else if ( pos1 != -1 ) {
762 modules = rx1.cap( 1 );
763 pos = pos1 + rx1.matchedLength();
765 else if ( pos2 != -1 ) {
766 modules = rx2.cap( 1 );
767 pos = pos2 + rx2.matchedLength();
774 QStringList mods = modules.split( QRegExp( "[:|,\\s]" ), QString::SkipEmptyParts );
775 for ( int i = 0; i < mods.count(); i++ ) {
776 if ( !mods[i].trimmed().isEmpty() )
777 modList.append( mods[i].trimmed() );
781 if ( modList.isEmpty() ) {
782 QString mods = resMgr->stringValue( "launch", "modules", QString() );
783 modList = mods.split( ",", QString::SkipEmptyParts );
786 for ( QStringList::const_iterator it = modList.begin(); it != modList.end(); ++it )
788 QString modName = (*it).trimmed();
790 if ( modName.isEmpty() )
791 continue; // empty module name
793 if ( !moduleTitle( modName ).isEmpty() )
794 continue; // already added
796 if ( modName == "KERNEL" || modName == "GUI" )
797 continue; // omit KERNEL and GUI modules
799 bool hasGui = resMgr->booleanValue( *it, "gui", true );
801 QString modTitle, modIcon, modLibrary, modDescription;
805 // if module has GUI, check that it is present
806 modTitle = resMgr->stringValue( *it, "name", QString() );
807 if ( modTitle.isEmpty() )
809 printf( "****************************************************************\n" );
810 printf( " Warning: module %s is improperly configured!\n", qPrintable(*it) );
811 printf( " Module %s will not be available in GUI mode!\n", qPrintable(*it) );
812 printf( "****************************************************************\n" );
816 modIcon = resMgr->stringValue( *it, "icon", QString() );
818 modDescription = resMgr->stringValue( *it, "description", QString() );
820 modLibrary = resMgr->stringValue( *it, "library", QString() ).trimmed();
821 if ( !modLibrary.isEmpty() )
823 modLibrary = SUIT_Tools::file( modLibrary.trimmed() );
825 QString libExt = QString( "dll" );
826 #elif defined(__APPLE__)
827 QString libExt = QString( "dylib" );
829 QString libExt = QString( "so" );
831 if ( SUIT_Tools::extension( modLibrary ).toLower() == libExt )
832 modLibrary.truncate( modLibrary.length() - libExt.length() - 1 );
834 QString prefix = QString( "lib" );
835 if ( modLibrary.startsWith( prefix ) )
836 modLibrary.remove( 0, prefix.length() );
840 modLibrary = modName;
843 QString version = resMgr->stringValue( *it, "version", QString() );
845 QString modDisplayer = resMgr->stringValue( *it, "displayer", QString() );
849 inf.title = modTitle;
850 inf.status = hasGui ? stUnknown : stNoGui;
851 if ( hasGui ) inf.library = modLibrary;
853 inf.description = modDescription;
854 inf.displayer = modDisplayer;
855 inf.version = version;
856 myInfoList.append( inf );
859 if ( myInfoList.isEmpty() ) {
860 if ( desktop() && desktop()->isVisible() )
861 SUIT_MessageBox::warning( desktop(), tr( "Warning" ), tr( "Modules list is empty" ) );
864 printf( "****************************************************************\n" );
865 printf( "* Warning: modules list is empty.\n" );
866 printf( "****************************************************************\n" );
872 \brief Add common menu items to the popup menu.
874 Menu items list is defined by the active module.
876 \param type popup menu context
877 \param menu popup menu
878 \param title popup menu title, which can be set by the module if required
880 void CAM_Application::contextMenuPopup( const QString& type, QMenu* menu, QString& title )
882 // to do : add common items for popup menu ( if they are exist )
883 if ( activeModule() )
884 activeModule()->contextMenuPopup( type, menu, title );
888 \brief Create new empty study.
890 void CAM_Application::createEmptyStudy()
892 /*SUIT_Study* study = */activeStudy();
893 STD_Application::createEmptyStudy();
897 \brief Return information about version of the each module.
899 CAM_Application::ModuleShortInfoList CAM_Application::getVersionInfo()
901 ModuleShortInfoList info;
903 ModuleShortInfo kernel;
904 kernel.name = "KERNEL";
905 kernel.version = KERNEL_VERSION_STR;
910 gui.version = GUI_VERSION_STR;
913 for(int i = 0; i < myInfoList.size(); i++) {
914 ModuleShortInfo infoItem;
915 infoItem.name = myInfoList.at(i).title;
916 infoItem.version = myInfoList.at(i).version;
917 info.append(infoItem);
923 \brief Abort active operations if there are any
925 Iterates through all modules and asks each of them if there are pending operations that cannot be aborted.
927 \return \c false if some operation cannot be aborted
929 bool CAM_Application::abortAllOperations()
932 for ( QList<CAM_Module*>::const_iterator it = myModules.begin(); it != myModules.end() && aborted; ++it )
934 aborted = (*it)->abortAllOperations();