accounts_ui.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 os
00020 import sys
00021 from roslib.packages import get_pkg_dir
00022 
00023 from python_qt_binding.QtGui import *
00024 from python_qt_binding.QtCore import *
00025 from python_qt_binding import loadUi
00026 
00027 from accounts import User, \
00028                      Privilege, \
00029                      UserAccounts, \
00030                      base64
00031                      
00032 from airbus_pyqt_extend.QtAgiGui import QAgiPopup, QAgiMessageBox
00033 
00034 from airbus_cobot_gui.dashboard import Dashboard, DashboardPopup
00035 
00036 from airbus_cobot_gui.res import R
00037 
00038 ## @package: user_account
00039 ##
00040 ## @version 2.2
00041 ## @author  Matignon Martin
00042 ## @date    Last modified 22/05/2014
00043 
00044 ## @class LoginDialog
00045 ## @brief Login user interface.
00046 class LoginDialog(QDialog):
00047     
00048     def __init__(self, parent, closable = True):
00049         """! The constructor."""
00050         QDialog.__init__(self, parent, Qt.FramelessWindowHint)
00051         
00052         self._context = parent.getContext()
00053         self._lng = self._context.getLanguage()
00054         
00055         # Extend the widget with all attributes and children from UI file
00056         loadUi(R.layouts.login_dialog, self)
00057         
00058         self.setStyleSheet(R.values.styles.login)
00059         
00060         self.setModal(True)
00061         self.adjustSize()
00062         
00063         self.login_button.setText(R.values.strings.login(self._lng))
00064         self.exit_button.setText(R.values.strings.exit(self._lng))
00065         
00066         self.user_id_label.setText(R.values.strings.user_id(self._lng))
00067         self.password_label.setText(R.values.strings.password(self._lng))
00068         
00069         self.password_edit.setEchoMode(QLineEdit.Password)
00070         
00071         self.connect(self.login_button, SIGNAL("clicked()"),
00072                       self.account_validation)
00073         
00074         if closable:
00075             self.connect(self.exit_button, SIGNAL("clicked()"), self.close)
00076             self.exit_button.setVisible(True)
00077         else:
00078             self.exit_button.setVisible(False)
00079         
00080         self.adjustSize()
00081     
00082     def account_validation(self):
00083         """! Check user account validity and connect user."""
00084         
00085         accounts = UserAccounts()
00086         
00087         user = accounts.find(self.user_id_edit.text())
00088         
00089         if user is None:
00090             msg_box = QAgiMessageBox()
00091             msg_box.setText(R.values.strings.invalid_user_id(self._lng))
00092             msg_box.setIcon(QAgiMessageBox.Critical)
00093             msg_box.setStandardButtons(QAgiMessageBox.Ok)
00094             msg_box.button(QAgiMessageBox.Ok).setMinimumSize(100,40)
00095             msg_box.exec_()
00096         elif user.password == base64.b64encode(self.password_edit.text()):
00097             self._context.switchUser(user)
00098             self.close()
00099         else:
00100             msg_box = QAgiMessageBox()
00101             msg_box.setText(R.values.strings.invalid_password(self._lng))
00102             msg_box.setIcon(QAgiMessageBox.Critical)
00103             msg_box.setStandardButtons(QAgiMessageBox.Ok)
00104             msg_box.button(QAgiMessageBox.Ok).setMinimumSize(100,40)
00105             msg_box.exec_()
00106         
00107 ## @class AddUserAccountWidget
00108 ## @brief Add user account ui.
00109 class AddUserAccountWidget(QWidget):
00110     
00111     def __init__(self, parent):
00112         """! The constructor."""
00113         QWidget.__init__(self)
00114         
00115         self._context = parent.getContext()
00116         self._lng = self._context.getLanguage()
00117         
00118         # Extend the widget with all attributes and children from UI file
00119         loadUi(R.layouts.add_user_widget, self)
00120         
00121         self.user_id_label.setText(R.values.strings.user_id(self._lng))
00122         self.password_label.setText(R.values.strings.password(self._lng))
00123         self.cinfirm_password_label.setText(R.values.strings.confirm_password(self._lng))
00124         self.access_rights_label.setText(R.values.strings.access_rights(self._lng))
00125         self.add_user_button.setText(R.values.strings.ok(self._lng))
00126         
00127         self.password_edit.setEchoMode(QLineEdit.Password)
00128         self.confirm_password_edit.setEchoMode(QLineEdit.Password);
00129         
00130         self.connect(self.add_user_button, SIGNAL("clicked()"),
00131                       self.add_user_account)
00132         
00133     def add_user_account(self):
00134         """! Check fields and add user account."""
00135         
00136         accounts = UserAccounts()
00137         
00138         user = User(self.user_id_edit.text())
00139         
00140         password = self.password_edit.text()
00141         confirm = self.confirm_password_edit.text()
00142         
00143         if user.userid == "" or \
00144            password == "" or \
00145            confirm == "":
00146             msg_box = QAgiMessageBox()
00147             msg_box.setText(R.values.strings.fields_not_filled(self._lng))
00148             msg_box.setIcon(QAgiMessageBox.Critical)
00149             msg_box.setStandardButtons(QAgiMessageBox.Ok)
00150             msg_box.button(QAgiMessageBox.Ok).setMinimumSize(100,40)
00151             msg_box.exec_()
00152             return
00153         
00154         if confirm != password:
00155             msg_box = QAgiMessageBox()
00156             msg_box.setText(R.values.strings.passwords_different(self._lng))
00157             msg_box.setIcon(QAgiMessageBox.Critical)
00158             msg_box.setStandardButtons(QAgiMessageBox.Ok)
00159             msg_box.button(QAgiMessageBox.Ok).setMinimumSize(100,40)
00160             msg_box.exec_()
00161             return
00162         else:
00163             user.setUserPassword(password)
00164         
00165         user.setUserPrivilege(Privilege.TOLEVEL[self.access_rights_combo.currentText().lower()])
00166         
00167         try:
00168             accounts.add(user)
00169         except Exception as e:
00170             msg_box = QAgiMessageBox()
00171             msg_box.setText(str(e))
00172             msg_box.setIcon(QAgiMessageBox.Critical)
00173             msg_box.setStandardButtons(QAgiMessageBox.Ok)
00174             msg_box.button(QAgiMessageBox.Ok).setMinimumSize(100,40)
00175             msg_box.exec_()
00176             return
00177         
00178         msg_box = QAgiMessageBox()
00179         msg_box.setText(R.values.strings.add_user_success(self._lng))
00180         msg_box.setIcon(QAgiMessageBox.Information)
00181         msg_box.setStandardButtons(QAgiMessageBox.Ok)
00182         msg_box.button(QAgiMessageBox.Ok).setMinimumSize(100,40)
00183         msg_box.exec_()
00184         
00185         self.user_id_edit.setText("")
00186         self.password_edit.setText("")
00187         self.confirm_password_edit.setText("")
00188         self.access_rights_combo.setCurrentIndex(0)
00189         
00190 
00191 ## @class ModifUserAccountWidget
00192 ## @brief Modif user account ui.
00193 class ModifUserAccountWidget(QWidget):
00194     
00195     def __init__(self, parent):
00196         """! The constructor."""
00197         QWidget.__init__(self)
00198         
00199         self._context = parent.getContext()
00200         self._lng = self._context.getLanguage()
00201         
00202         # Extend the widget with all attributes and children from UI file
00203         loadUi(R.layouts.modif_account_widget, self)
00204         
00205         self.select_user_label.setText(R.values.strings.select_user(self._lng))
00206         self.current_password_label.setText(R.values.strings.current_password(self._lng))
00207         self.check_password_button.setText(R.values.strings.ok(self._lng))
00208         self.access_rights_label.setText(R.values.strings.access_rights(self._lng))
00209         self.new_password_label.setText(R.values.strings.new_password(self._lng))
00210         self.confirm_password_label.setText(R.values.strings.confirm_password(self._lng))
00211         self.modif_user_account_button.setText(R.values.strings.ok(self._lng))
00212         
00213         self.new_password_edit.setEchoMode(QLineEdit.Password)
00214         self.confirm_new_password_edit.setEchoMode(QLineEdit.Password)
00215         
00216         self._accounts = UserAccounts()
00217         self.user_selected = User()
00218         self.user_dst = User()
00219         
00220         self.users_list_combo.addItems(self._accounts.user_list())
00221         self.users_list_combo.currentIndexChanged.connect(self.update_user_info)
00222         
00223         self.connect(self.modif_user_account_button, SIGNAL("clicked()"),
00224                       self.modif_user_account)
00225         
00226         self.connect(self.check_password_button, SIGNAL("clicked()"),
00227                      self.check_password)
00228         
00229         self.current_password_edit.textChanged[str].connect(self.current_password_changed)
00230         
00231     def current_password_changed(self):
00232         self.init_fields(False)
00233         
00234     def check_password(self):
00235         
00236         password = self.current_password_edit.text()
00237         
00238         if password == self.user_selected.getUserPassword(True):
00239             
00240             self.current_password_edit.setStyleSheet(R.values.styles.good_password)
00241             
00242             privilege = self.user_selected.getUserPrivilege()
00243             self.access_rights_combo.setCurrentIndex(privilege+1)
00244             self.new_password_edit.setText(self.user_selected.getUserPassword(True))
00245             self.confirm_new_password_edit.setText(self.user_selected.getUserPassword(True))
00246             
00247             self.new_password_edit.setEnabled(True)
00248             self.confirm_new_password_edit.setEnabled(True)
00249             self.access_rights_combo.setEnabled(True)
00250             self.modif_user_account_button.setEnabled(True)
00251             
00252         else:
00253             
00254             self.init_fields(False)
00255             
00256             self.current_password_edit.setStyleSheet(R.values.styles.bad_password)
00257             
00258         
00259     def update_user_info(self, index):
00260         """! Update user information with user selected.
00261         @param index: user list index.
00262         @type index: int.
00263         """
00264         self.init_fields()
00265         
00266         if index == 0:
00267             self.current_password_edit.setEnabled(False)
00268         else:
00269             self.user_selected = self._accounts.find(self.users_list_combo.itemText(index))
00270             self.current_password_edit.setEnabled(True)
00271             
00272     def modif_user_account(self):
00273         """! Check fields and modify user account."""
00274         
00275         user_modified = User(self.user_selected.getUserId())
00276         
00277         if self.access_rights_combo.currentIndex() == 0:
00278             
00279             msg_box = QAgiMessageBox()
00280             msg_box.setText(R.values.strings.select_access_rights(self._lng))
00281             msg_box.setIcon(QAgiMessageBox.Critical)
00282             msg_box.setStandardButtons(QAgiMessageBox.Ok)
00283             msg_box.button(QAgiMessageBox.Ok).setMinimumSize(100,40)
00284             msg_box.exec_()
00285             return
00286         elif self.new_password_edit.text() != \
00287              self.confirm_new_password_edit.text():
00288             
00289             msg_box = QAgiMessageBox()
00290             msg_box.setText(R.values.strings.passwords_different(self._lng))
00291             msg_box.setIcon(QAgiMessageBox.Critical)
00292             msg_box.setStandardButtons(QAgiMessageBox.Ok)
00293             msg_box.button(QAgiMessageBox.Ok).setMinimumSize(100,40)
00294             msg_box.exec_()
00295             return
00296         else:
00297             user_modified.privilege = Privilege.TOLEVEL[self.access_rights_combo.currentText().lower()]
00298             user_modified.setUserPassword(self.new_password_edit.text())
00299         
00300         try:
00301             
00302             self._accounts.modif(self.user_selected, user_modified)
00303         except Exception as e:
00304             
00305             msg_box = QAgiMessageBox()
00306             msg_box.setText(str(e))
00307             msg_box.setIcon(QAgiMessageBox.Critical)
00308             msg_box.setStandardButtons(QAgiMessageBox.Ok)
00309             msg_box.button(QAgiMessageBox.Ok).setMinimumSize(100,40)
00310             msg_box.exec_()
00311             return
00312             
00313         msg_box = QAgiMessageBox()
00314         msg_box.setText(R.values.strings.user_mv_success(self._lng))
00315         msg_box.setIcon(QAgiMessageBox.Information)
00316         msg_box.setStandardButtons(QAgiMessageBox.Ok)
00317         msg_box.button(QAgiMessageBox.Ok).setMinimumSize(100,40)
00318         msg_box.exec_()
00319         
00320         self.users_list_combo.setCurrentIndex(0)
00321         self.init_fields()
00322         
00323     def init_fields(self, clear_all = True):
00324         
00325         self.access_rights_combo.setCurrentIndex(0)
00326         self.new_password_edit.setText('')
00327         self.confirm_new_password_edit.setText('')
00328         self.new_password_edit.setEnabled(False)
00329         self.confirm_new_password_edit.setEnabled(False)
00330         self.access_rights_combo.setEnabled(False)
00331         self.modif_user_account_button.setEnabled(False)
00332         
00333         if clear_all:
00334             self.current_password_edit.setText('')
00335             
00336         self.current_password_edit.setStyleSheet(R.values.styles.no_password)
00337         
00338 ## @class RemoveUserAccountWidget
00339 ## @brief Remove user account ui.
00340 class RemoveUserAccountWidget(QWidget):
00341     
00342     def __init__(self, parent):
00343         """! The constructor."""
00344         
00345         QWidget.__init__(self)
00346         # Extend the widget with all attributes and children from UI file
00347         loadUi(R.layouts.remove_account_widget, self)
00348         
00349         self._context = parent.getContext()
00350         self._lng = self._context.getLanguage()
00351         
00352         self.user_list_label.setText(R.values.strings.user_list(self._lng))
00353         self.remove_button.setText(R.values.strings.ok(self._lng))
00354         
00355         self.connect(self.remove_button, SIGNAL("clicked()"),
00356                       self.remove_account)
00357          
00358         self._accounts = UserAccounts()
00359          
00360         self.users_list_combo.addItems(self._accounts.user_list())
00361         
00362     def remove_account(self):
00363         """! Remove user account slected in user list."""
00364         
00365         if self.users_list_combo.currentIndex() == 0:
00366             msg_box = QAgiMessageBox()
00367             msg_box.setText(R.values.strings.select_user(self._lng))
00368             msg_box.setIcon(QAgiMessageBox.Critical)
00369             msg_box.setStandardButtons(QAgiMessageBox.Ok)
00370             msg_box.button(QAgiMessageBox.Ok).setMinimumSize(100,40)
00371             msg_box.exec_()
00372             return
00373         
00374         user_id = self.users_list_combo.currentText()
00375         try:
00376             self._accounts.remove(User(user_id))
00377             self.users_list_combo.removeItem(self.users_list_combo.currentIndex())
00378         except Exception as e:
00379             msg_box = QAgiMessageBox()
00380             msg_box.setText(str(e))
00381             msg_box.setIcon(QAgiMessageBox.Critical)
00382             msg_box.setStandardButtons(QAgiMessageBox.Ok)
00383             msg_box.button(QAgiMessageBox.Ok).setMinimumSize(100,40)
00384             msg_box.exec_()
00385             return
00386             
00387         self.users_list_combo.setCurrentIndex(0)
00388         msg_box = QAgiMessageBox()
00389         msg_box.setText(R.values.strings.user_rm_success(self._lng))
00390         msg_box.setIcon(QAgiMessageBox.Information)
00391         msg_box.setStandardButtons(QAgiMessageBox.Ok)
00392         msg_box.button(QAgiMessageBox.Ok).setMinimumSize(100,40)
00393         msg_box.exec_()
00394         
00395 ## @class UsersAccountsManagerDialog
00396 ## @brief User accounts manager ui.
00397 class UsersAccountsManagerDialog(QDialog):
00398     
00399     def __init__(self, parent):
00400         """! The constructor."""
00401         QDialog.__init__(self, parent, Qt.FramelessWindowHint)
00402         
00403         self._parent = parent
00404         self._lng = self._parent.getContext().getLanguage()
00405         
00406         # Extend the widget with all attributes and children from UI file
00407         loadUi(R.layouts.users_accounts_dialog, self)
00408         
00409         self.setModal(True)
00410         
00411         self.settings_label.setText(R.values.strings.settings(self._lng))
00412         self.header_label.setText(R.values.strings.account_manager(self._lng))
00413         self.add_button.setText(R.values.strings.add(self._lng))
00414         self.modif_button.setText(R.values.strings.modif(self._lng))
00415         self.remove_button.setText(R.values.strings.remove(self._lng))
00416         self.exit_button.setText(R.values.strings.exit(self._lng))
00417         
00418         self.connect(self.exit_button, SIGNAL("clicked()"), self.close)
00419         self.connect(self.add_button, SIGNAL("clicked()"), self.load_add_user_ui)
00420         self.connect(self.modif_button, SIGNAL("clicked()"), self.load_modif_user_ui)
00421         self.connect(self.remove_button, SIGNAL("clicked()"), self.load_remove_user_ui)
00422         
00423         self.add_button.click()
00424         
00425     def load_add_user_ui(self):
00426         """! Open add user account ui."""
00427         self.header_label.setText(R.values.strings.add_user(self._lng))
00428         
00429         self.viewer_area.takeWidget()
00430         add_account_ui = AddUserAccountWidget(self._parent)
00431         add_account_ui.resize(self.viewer_area.width()-2,
00432                               self.viewer_area.height()-2)
00433         self.viewer_area.setWidget(add_account_ui)
00434     
00435     def load_modif_user_ui(self):
00436         """! Open modif user account ui."""
00437         self.header_label.setText(R.values.strings.modif_user_account(self._lng))
00438         
00439         self.viewer_area.takeWidget()
00440         modif_account_ui = ModifUserAccountWidget(self._parent)
00441         modif_account_ui.resize(self.viewer_area.width()-2,
00442                                 self.viewer_area.height()-2)
00443         self.viewer_area.setWidget(modif_account_ui)
00444     
00445     def load_remove_user_ui(self):
00446         """! Open remove user account ui."""
00447         self.header_label.setText(R.values.strings.remove_user_account(self._lng))
00448         
00449         self.viewer_area.takeWidget()
00450         remove_account = RemoveUserAccountWidget(self._parent)
00451         remove_account.resize(self.viewer_area.width()-2,
00452                               self.viewer_area.height()-2)
00453         self.viewer_area.setWidget(remove_account)
00454         
00455 ## @class UserAccountPopup
00456 ## @brief User accounts popup ui.
00457 class UserAccountPopup(DashboardPopup):
00458     
00459     def __init__(self, parent):
00460         """! The constructor."""
00461         DashboardPopup.__init__(self, parent)
00462         
00463         self.setRelativePosition(DashboardPopup.TopRight,
00464                                  DashboardPopup.BottomRight)
00465         
00466     def onCreate(self, param):
00467         # Extend the widget with all attributes and children from UI file
00468         loadUi(R.layouts.account_popup, self)
00469         
00470         self.user_icon_label.setPixmap(R.getPixmapById('ico_user'))
00471         
00472         user_info = self.getParent().getContext().getUserInfo()
00473         
00474         if user_info.privilege < Privilege.EXPERT:
00475             self.accounts_manager_button.setEnabled(False)
00476         else:
00477             self.accounts_manager_button.setEnabled(True)
00478         
00479         self.user_id_label.setText(user_info.userid)
00480         
00481         privilege = Privilege.TOSTR[user_info.privilege]
00482         self.access_rights_label.setText(privilege[0].upper()+privilege[1:])
00483         self.time_label.setText('2h:38m')
00484         
00485         self.connect(self.connection_button, SIGNAL("clicked()"),
00486                      self.open_login_dialog)
00487         self.connect(self.accounts_manager_button, SIGNAL("clicked()"), 
00488                      self.open_accounts_manager_dialog)
00489         self.connect(self.deconnection_button, SIGNAL("clicked()"), 
00490                      self.disconnect_user_account)
00491         self.deconnection_button.setEnabled(True)
00492         
00493         self.adjustSize()
00494         
00495     def _resfresh_connection_time(self):
00496         """! Refresh connection time."""
00497         self.time_label.setText(self._user.connection_time())
00498         
00499     def open_login_dialog(self):
00500         """! Open login ui."""
00501         login = LoginDialog(self.getParent())
00502         login.show()
00503         
00504     def open_accounts_manager_dialog(self):
00505         """! Open account manager ui."""
00506         manager = UsersAccountsManagerDialog(self.getParent())
00507         manager.show()
00508         
00509     def disconnect_user_account(self):
00510         """! Disconnect current user."""
00511         login = LoginDialog(self.getParent(), closable = False)
00512         login.show()
00513         
00514     def onTranslate(self, lng):
00515         
00516         self.header_label.setText(R.values.strings.user_account(lng))
00517         self.user_id_header_label.setText(R.values.strings.user(lng))
00518         self.access_rights_header_label.setText(R.values.strings.rights(lng))
00519         self.language_header_label.setText(R.values.strings.language(lng))
00520         self.time_header_label.setText(R.values.strings.time(lng))
00521         self.connection_button.setText(R.values.strings.connection(lng))
00522         self.deconnection_button.setText(R.values.strings.disconnection(lng))
00523         self.accounts_manager_button.setText(R.values.strings.account_manager(lng))
00524         self.language_label.setText(R.values.strings.language(lng))
00525         
00526     def onDestroy(self):
00527         pass
00528 
00529 ## @class UserAccountsWidget
00530 ## @brief User connected information displaying on dashboard.
00531 class UserAccountsWidget(Dashboard):
00532      
00533     def __init__(self, context):
00534         Dashboard.__init__(self, context)
00535     
00536     def onCreate(self, param):
00537         
00538         self._user_icon_label = QLabel()
00539         self._user_icon_label.setStyleSheet(R.values.styles.text)
00540         self._user_icon_label.setFixedSize(QSize(25,25))
00541         self._user_icon_label.setPixmap(R.getPixmapById('ico_user').scaled(
00542                                    self._user_icon_label.width(),
00543                                    self._user_icon_label.height(),
00544                                    Qt.KeepAspectRatio,
00545                                    Qt.SmoothTransformation))
00546         ######
00547          
00548         self._user_id_label = QLabel()
00549         self._user_id_label.setStyleSheet(R.values.styles.transparent_background)
00550          
00551         ######
00552          
00553         self.getLayout().addWidget(self._user_icon_label)
00554         self.getLayout().addWidget(self._user_id_label)
00555          
00556     def onUserChanged(self, user):
00557         """! Update user information.
00558         @param user_info: user connected information.
00559         @type user_info: UserInfo.
00560         """
00561         self._user_id_label.setText(user.userid)
00562         
00563     def onControlModeChanged(self, mode):
00564         pass
00565     
00566     def onRequestPopup(self):
00567         return UserAccountPopup(self)
00568          
00569     def onTranslate(self):
00570         pass
00571     
00572     def onEmergencyStop(self, state):
00573         pass
00574     
00575     def onDestroy(self):
00576         pass
00577         
00578     
00579 #End of file


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