Back to index

salome-gui  6.5.0
StandardApp_Module.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2011-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 // Author : Guillaume Boulant (EDF) 
00020 
00021 #include "StandardApp_Module.hxx"
00022 
00023 #include <SUIT_Desktop.h>
00024 #include <SUIT_Study.h>
00025 #include <SalomeApp_Application.h>
00026 #include <SALOME_LifeCycleCORBA.hxx>
00027 #include "QtxPopupMgr.h"
00028 
00029 #include CORBA_CLIENT_HEADER(SALOMEDS)
00030 #include CORBA_CLIENT_HEADER(SALOMEDS_Attributes)
00031 
00032 // QT Includes
00033 #include <QIcon>
00034 #include <QAction>
00035 
00036 #include <Basics_Utils.hxx>
00037 #include "QtHelper.hxx"
00038 
00039 // Constructor
00040 StandardApp_Module::StandardApp_Module() :
00041   SalomeApp_Module( "" ),
00042   LightApp_Module( "" )
00043 {
00044   // Note that it's no use to specify here the moduleName because it's
00045   // automatically determined by the name of the GUI library
00046   // (lib<moduleName>.so) just before initialize step. If you want
00047   // nevertheless to setup the module name you should specified this
00048   // name in the initialize function below using the CAM_module
00049   // function setModuleName(QString&). You are warn then that the
00050   // loading of resource files may be incorect and should be manually
00051   // managed. In conclusion, if you don't need a special
00052   // configuration, please conform to the standard convention: if
00053   // <moduleName> is the name of the module as declared in the GUI
00054   // configuration file (SalomeApp.xml), then you just have to provide
00055   // a library whose name is lib<moduleName>.so and which contains a
00056   // factory "extern C" function 'CAM_Module* createModule()'
00057 }
00058 
00059 int StandardApp_Module::ACTIONID_DEFAULT_INIT_VALUE = 910;
00060 int StandardApp_Module::ACTIONID_UNDEFINED = -1;
00061 
00062 // Module's initialization
00063 void StandardApp_Module::initialize( CAM_Application* app )
00064 {
00065   //
00066   // ----------------------------------------------------------------
00067   // Standard initialization
00068   // ----------------------------------------------------------------
00069   //
00070   SalomeApp_Module::initialize( app );
00071   _resourceMgr = app->resourceMgr();
00072   // Please note that the moduleName() function is inherited from
00073   // CAM_module classe, and that its value is determined automatically
00074   // from the name specified in the SalomeApp.xml associated to this
00075   // module and which corresponds to the name of the GUI library
00076   // (lib<moduleName>.so). For XSALOME, see
00077   // share/salome/resources/xsalome/SalomeApp.xml in the install
00078   // directory.
00079   _defaultMenuId = this->createMenu( QCHARSTAR(moduleName()), -1, -1, 30 );
00080   _defaultToolbarId = this->createTool ( QCHARSTAR(moduleName()) );
00081   _actionId_internalCount = StandardApp_Module::ACTIONID_DEFAULT_INIT_VALUE;
00082 
00083   //
00084   // ----------------------------------------------------------------
00085   // The following initializes the GUI widget and associated actions
00086   // ----------------------------------------------------------------
00087   // These functions may be redefined in specialized version of this class
00088   this->createModuleWidgets();
00089   this->createModuleActions();
00090 }
00091 
00092 QIcon StandardApp_Module::createIcon(const QString& iconName) {
00093   // The icon file is supposed to be available througth the resource
00094   // manager, i.e. in the folder containing the XSALOME resources (see
00095   // specification in the module file SalomeApp.xml).
00096   QPixmap aPixmap = _resourceMgr->loadPixmap( QCHARSTAR(moduleName()), iconName );
00097   return QIcon( aPixmap );
00098 }
00099 
00100 int StandardApp_Module::newActionId() {
00101   _actionId_internalCount++;
00102   return _actionId_internalCount;
00103 }
00104 
00117 int StandardApp_Module::createStandardAction(const QString& label,
00118                                         QObject * slotobject,
00119                                         const char* slotmember,
00120                                         const QString& iconName,
00121                                         const QString& tooltip,
00122                                         const int identifier)
00123 {
00124 
00125   // If the tooltip is not defined, we choose instead the label text.
00126   QString effToolTip(tooltip);
00127   if ( effToolTip.isEmpty() )
00128     effToolTip = label;
00129 
00130   QIcon actionIcon = this->createIcon(iconName);
00131 
00132   // If the identifier is not specified, then we use an internal
00133   // counter.
00134   int effIdentifier = identifier;
00135   if ( effIdentifier == StandardApp_Module::ACTIONID_UNDEFINED ) {
00136     effIdentifier=newActionId();
00137   }
00138 
00139   // Creating the action
00140   QAction * action= this->createAction( effIdentifier, label, actionIcon, 
00141                                         label, effToolTip, 0, getApp()->desktop(),
00142                                         false, slotobject, slotmember);
00143 
00144   return effIdentifier;
00145 }
00146 
00150 void StandardApp_Module::addActionInToolbar(int actionId) {
00151   this->createTool( actionId, _defaultToolbarId );
00152 }
00153 
00157 void StandardApp_Module::addActionInMenubar(int actionId) {
00158   this->createMenu( actionId, _defaultMenuId, 10 );
00159 }
00160 
00165 void StandardApp_Module::addActionInPopupMenu(int actionId,const QString& rule) {
00166   // _GBO_ for a fine customization of the rule (for example with a
00167   // test on the type of the selected object), see the LIGTH module:
00168   // implement "LightApp_Selection*    createSelection() const;"
00169   int parentId = -1;
00170   QtxPopupMgr* mgr = this->popupMgr();
00171   mgr->insert ( this->action( actionId ), parentId, 0 );
00172   mgr->setRule( this->action( actionId ), rule, QtxPopupMgr::VisibleRule );
00173 }
00174 
00180 void StandardApp_Module::createModuleWidgets() {
00181   // Nothing to do in this default gui
00182 }
00183 
00192 void StandardApp_Module::createModuleActions() {
00193   int actionId = this->createStandardAction("Test", this, SLOT(OnTest()),
00194                                        "f1.png", "Run the default test function");
00195   this->addActionInToolbar(actionId);
00196 }
00197 
00198 // Get compatible dockable windows.
00199 void StandardApp_Module::windows( QMap<int, int>& theMap ) const
00200 {
00201   theMap.clear();
00202   theMap.insert( SalomeApp_Application::WT_ObjectBrowser, Qt::LeftDockWidgetArea );
00203   theMap.insert( SalomeApp_Application::WT_PyConsole,     Qt::BottomDockWidgetArea );
00204 }
00205 
00206 // Module's engine IOR
00207 QString StandardApp_Module::engineIOR() const
00208 {
00209   CORBA::String_var anIOR = getApp()->orb()->object_to_string(getEngine());
00210   return QString( anIOR.in() );
00211 }
00212 
00221 QString StandardApp_Module::studyIconName()
00222 {
00223   // By default, we return the module icone name
00224   return iconName(); // inherited from CAM_Module
00225 }
00226 
00236 void StandardApp_Module::setDockLayout(DockLayoutType layoutType) {
00237   SUIT_Desktop* desk = getApp()->desktop();
00238   _areaAtBottomLeftCorner = desk->corner(Qt::BottomLeftCorner);
00239   _areaAtBottomRightCorner = desk->corner(Qt::BottomRightCorner);
00240   
00241   if ( layoutType == DOCKLAYOUT_LEFT_VLARGE ) {
00242     desk->setCorner(Qt::BottomLeftCorner, Qt::LeftDockWidgetArea);
00243     desk->setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea);
00244   } else {
00245     desk->setCorner(Qt::BottomLeftCorner, Qt::BottomDockWidgetArea);
00246     desk->setCorner(Qt::BottomRightCorner, Qt::RightDockWidgetArea);    
00247   }
00248 }
00249 
00254 void StandardApp_Module::unsetDockLayout() {
00255   SUIT_Desktop* desk = getApp()->desktop();
00256   desk->setCorner(Qt::BottomLeftCorner, _areaAtBottomLeftCorner);
00257   desk->setCorner(Qt::BottomRightCorner, _areaAtBottomRightCorner);
00258 }
00259 
00260 // Module's activation
00261 bool StandardApp_Module::activateModule( SUIT_Study* theStudy )
00262 {
00263   bool bOk = SalomeApp_Module::activateModule( theStudy );
00264 
00265   setMenuShown( true );
00266   setToolShown( true );
00267 
00268   if ( this->createStudyComponentAtActivation() ) {
00269     this->createStudyComponent(theStudy);
00270   }
00271 
00272   return bOk;
00273 }
00274 
00281 bool StandardApp_Module::createStudyComponentAtActivation() {
00282   return true;
00283 }
00284 
00291 void StandardApp_Module::createStudyComponent(SUIT_Study* theStudy) {
00292 
00293   SALOME_NamingService *aNamingService = SalomeApp_Application::namingService();
00294   CORBA::Object_var aSMObject = aNamingService->Resolve("/myStudyManager");
00295   SALOMEDS::StudyManager_var aStudyManager = SALOMEDS::StudyManager::_narrow(aSMObject);
00296   SALOMEDS::Study_var aDSStudy = aStudyManager->GetStudyByID(theStudy->id());
00297 
00298   SALOMEDS::SComponent_var aFather = aDSStudy->FindComponent(QCHARSTAR(moduleName()));
00299   if (aFather->_is_nil())
00300     {
00301       SALOMEDS::StudyBuilder_var aStudyBuilder = aDSStudy->NewBuilder();
00302       aFather = aStudyBuilder->NewComponent(QCHARSTAR(moduleName()));
00303       SALOMEDS::GenericAttribute_var anAttr = aStudyBuilder->FindOrCreateAttribute(aFather, "AttributeName");
00304       SALOMEDS::AttributeName_var aName = SALOMEDS::AttributeName::_narrow(anAttr);
00305       aName->SetValue(QCHARSTAR(moduleName()));
00306       aName->Destroy();
00307       anAttr = aStudyBuilder->FindOrCreateAttribute(aFather, "AttributePixMap");
00308       SALOMEDS::AttributePixMap_var aPixMap = SALOMEDS::AttributePixMap::_narrow(anAttr);
00309       aPixMap->SetPixMap(QCHARSTAR(studyIconName()));
00310 
00311       // WARN: The engine should be associated to the SComponent IF
00312       // AND ONLY IF it is a SALOMEDS::Driver. Otherwise, there is no
00313       // need to do that, and it could even lead to exception
00314       // raising (eh, you work on SALOME isn't it?)
00315       SALOMEDS::Driver_var driver = SALOMEDS::Driver::_narrow(this->getEngine());
00316       if ( ! driver->_is_nil() ) {
00317        STDLOG("Associate the SComponent to the engine");
00318        aStudyBuilder->DefineComponentInstance(aFather, this->getEngine());
00319       }
00320     }
00321 
00322 }
00323 
00324 // Module's deactivation
00325 bool StandardApp_Module::deactivateModule( SUIT_Study* theStudy )
00326 {
00327   setMenuShown( false );
00328   setToolShown( false );
00329 
00330   return SalomeApp_Module::deactivateModule( theStudy );
00331 }
00332 
00333 void StandardApp_Module::OnTest()
00334 {
00335   // Just for test
00336   STDLOG("OnTest: engine IOR = "<<QCHARSTAR(engineIOR()));
00337 }
00338