wrapper_plugin.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # Copyright 2015 Airbus
00004 # Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00005 #
00006 # Licensed under the Apache License, Version 2.0 (the "License");
00007 # you may not use this file except in compliance with the License.
00008 # You may obtain a copy of the License at
00009 #
00010 #   http://www.apache.org/licenses/LICENSE-2.0
00011 #
00012 # Unless required by applicable law or agreed to in writing, software
00013 # distributed under the License is distributed on an "AS IS" BASIS,
00014 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 # See the License for the specific language governing permissions and
00016 # limitations under the License.
00017 
00018 import rospy
00019 import uuid
00020 import os
00021 from roslib.packages import get_pkg_dir
00022 from python_qt_binding.QtGui import *
00023 from python_qt_binding.QtCore import *
00024 
00025 from airbus_cobot_gui.account import Privilege, User
00026 from airbus_cobot_gui.alarm import Alarm
00027 from airbus_cobot_gui.util import CobotGuiException
00028 
00029 from airbus_pyqt_extend.QtAgiCore import get_pkg_dir_from_prefix
00030 
00031 from airbus_cobot_gui.res import R
00032 
00033 ## @package: plugin
00034 ##
00035 ## @version 4.0
00036 ## @author  Matignon Martin
00037 ## @date    Last modified 28/02/2014
00038 
00039 class LauncherPlugin(QPushButton):
00040     
00041     def __init__(self, plugin):
00042         QPushButton.__init__(self)
00043         
00044         self.setFocusPolicy(Qt.NoFocus)
00045         self.setObjectName(plugin.getPluginName())
00046         self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)
00047         self.setEnabled(False)
00048         
00049         self._plugin = plugin
00050         self._access_rights = Privilege.OPERATOR
00051         
00052         plugin.getContext().addUserEventListener(self.onUserChanged)
00053         
00054     def setup(self, xsetup):
00055         
00056         xicon = xsetup.find('icon')
00057          
00058         if xicon is not None:
00059              
00060             ic_path = get_pkg_dir_from_prefix(xicon.text)
00061              
00062             if os.path.isfile(ic_path):
00063                 self.setIcon(QIcon(ic_path))
00064                 self.setIconSize(QSize(80,80))
00065             else:
00066                 self.setStyleSheet(R.values.styles.default_launch)
00067                 self.setText(self._plugin.getPluginName())
00068         else:
00069             self.logErr("Cannot found '<icon>' into %s/plugin_descriptor.xml"%self.objectName())
00070             
00071         xaccess = xsetup.find('access-rights')
00072         
00073         if xaccess is not None:
00074             try:
00075                 self._access_rights = Privilege.TOLEVEL[xaccess.text.lower()]
00076             except:
00077                 self._access_rights = Privilege.OPERATOR
00078                 self.logWarn("Invalid access rights from %s"%self.objectName())
00079         else:
00080             self.logErr("Cannot found node '<access-rights>' into %s/plugin_descriptor.xml"%self.objectName())
00081         
00082     def onUserChanged(self, user):
00083         
00084         if user.getUserPrivilege() < self.getAccessRights():
00085             self.setEnabled(False)
00086         else:
00087             self.setEnabled(True)
00088     
00089     def setAccessRights(self, access_rights):
00090         self._access_rights = access_rights
00091         
00092     def getAccessRights(self):
00093         return self._access_rights
00094     
00095     def getPluginAttached(self):
00096         return self._plugin
00097     
00098     # Remap method name
00099     getView = getPluginAttached
00100 
00101 ## @class WrapperPlugin
00102 ## @brief Base class for install base plugin components.
00103 class WrapperPlugin(QWidget):
00104     
00105     def __init__(self, context):
00106         QWidget.__init__(self)
00107         
00108         self._context = context
00109         self._plugin_name = str(self.__class__.__name__)
00110         self._launcher = LauncherPlugin(self)
00111         
00112         self.connect(self._launcher, SIGNAL('clicked()'), self.onRequestDisplayView)
00113         
00114         context.addUserEventListener(self.onUserChanged)
00115         context.addLanguageEventListner(self.onTranslate)
00116         context.addControlModeEventListener(self.onControlModeChanged)
00117         context.addEmergencyStopEventListner(self.onEmergencyStop)
00118         context.addCloseEventListner(self.tryToDestroy)
00119         
00120     def setup(self, plugin_descriptor, param):
00121         
00122         xsetup = plugin_descriptor.find('setup')
00123         
00124         if xsetup is not None:
00125             self._launcher.setup(xsetup)
00126         else:
00127             self.logErr("Cannot found '<setup>' into %s/plugin_descriptor.xml"%self._plugin_name)
00128             
00129         self.tryToCreate(param)
00130     
00131     def getContext(self):
00132         return self._context
00133     
00134     def getLauncher(self):
00135         return self._launcher
00136     
00137     def getPluginName(self):
00138         return self._plugin_name
00139     
00140     getName = getPluginName
00141     
00142     def logInfo(self,):
00143         self._context.getLogger().info(msg)
00144         
00145     def logWarn(self, msg):
00146         self._context.getLogger().warn(msg)
00147         
00148     def logErr(self, msg):
00149         self._context.getLogger().err(msg)
00150         
00151     def sendAlarm(self, level, msg):
00152         self._context.sendAlarm(level, msg)
00153         
00154     def onRequestDisplayView(self):
00155         self._context.requestDisplayView(self)
00156     
00157     def onCreate(self, param):
00158         pass
00159     
00160     def tryToCreate(self, param):
00161         
00162         try:
00163             self.onCreate(param)
00164         except Exception as ex:
00165             rospy.logerr("GUI ERROR : %s"%str(ex))
00166             self.getContext().sendAlarm(Alarm.WARNING, str(ex))
00167     
00168     def onPause(self):
00169         pass
00170     
00171     def tryToPause(self):
00172         
00173         try:
00174             self.onPause()
00175         except Exception as ex:
00176             
00177             rospy.logerr("GUI ERROR : %s"%str(ex))
00178             self.getContext().sendAlarm(Alarm.WARNING, str(ex))
00179     
00180     def onResume(self):
00181         pass
00182     
00183     def tryToResume(self):
00184         try:
00185             self.onResume()
00186         except Exception as ex:
00187             rospy.logerr("GUI ERROR : %s"%str(ex))
00188             self.getContext().sendAlarm(Alarm.WARNING, str(ex))
00189     
00190     def onControlModeChanged(self, mode):
00191         pass
00192         
00193     def onUserChanged(self, user_info):
00194         pass
00195     
00196     def onTranslate(self, lng):
00197         pass
00198     
00199     def onEmergencyStop(self, state):
00200         pass
00201     
00202     def onDestroy(self):
00203         raise NotImplementedError("Need to surchage onDestroy(self)")
00204     
00205     def tryToDestroy(self):
00206         rospy.logdebug("Closing : %s"%self._plugin_name)
00207         try:
00208             self.onDestroy()
00209         except Exception as ex:
00210             rospy.logerr("GUI ERROR : %s"%str(ex))
00211             self.getContext().sendAlarm(Alarm.WARNING, str(ex))
00212     
00213     def closeEvent(self, event):
00214         self.tryToDestroy()
00215 
00216 #End of file
00217 


airbus_cobot_gui
Author(s): Martin Matignon
autogenerated on Thu Jun 6 2019 17:59:19