accounts_ui.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 ################################################################################
3 #
4 # Copyright Airbus Group SAS 2015
5 # All rigths reserved.
6 #
7 # File Name : accounts_ui.py
8 # Authors : Martin Matignon
9 #
10 # If you find any bug or if you have any question please contact
11 # Adolfo Suarez Roos <adolfo.suarez@airbus.com>
12 # Martin Matignon <martin.matignon.external@airbus.com>
13 #
14 #
15 ################################################################################
16 import rospy
17 import os
18 import sys
19 from roslib.packages import get_pkg_dir
20 
21 from python_qt_binding.QtGui import *
22 from python_qt_binding.QtCore import *
23 from python_qt_binding import loadUi
24 
25 from accounts import User, \
26  Privilege, \
27  UserAccounts, \
28  GUserAccountCommunicate, \
29  base64
30 
32 from airbus_cobot_gui import airbus_cobot_gui_dir
33 from airbus_cobot_gui.translator import trUtf8
34 from airbus_cobot_gui.python_qt_extend import MessageBox
35 
36 ## @package: user_account
37 ##
38 ## @version 2.2
39 ## @author Matignon Martin
40 ## @date Last modified 22/05/2014
41 
42 ## @class LoginDialog
43 ## @brief Login user interface.
44 class LoginDialog(QDialog):
45 
46  def __init__(self, parent = None, closable = True):
47  """! The constructor."""
48 
49  QDialog.__init__(self, parent, Qt.FramelessWindowHint)
50 
51  # Get path to UI file which is a sibling of this file
52  ui_file = airbus_cobot_gui_dir('resources', 'ui', 'login_dialog.ui')
53  # Extend the widget with all attributes and children from UI file
54  loadUi(ui_file, self)
55 
56  self.setStyleSheet("QDialog{ \
57  background-color:qlineargradient(x1: 0, \
58  y1: 0, \
59  x2: 0, \
60  y2: 1, \
61  stop: 0 #616763, \
62  stop: 1 #89928c); \
63  }")
64 
65  self.setModal(True)
66 
67  self.adjustSize()
68 
69  self.password_edit.setEchoMode(QLineEdit.Password);
70 
71  self.connect(self.login_button, SIGNAL("clicked()"),
72  self.account_validation)
73 
74  if closable:
75  self.connect(self.exit_button, SIGNAL("clicked()"), self.close)
76  self.exit_button.setVisible(True)
77  else:
78  self.exit_button.setVisible(False)
79 
80  self.translate()
81 
82  def account_validation(self):
83  """! Check user account validity and connect user."""
84 
85  accounts = UserAccounts()
86 
87  user_info = accounts.find(self.user_id_edit.text())
88 
89  if user_info is None:
90  msg_box = MessageBox()
91  msg_box.setText(trUtf8("Invalid user id",'!'))
92  msg_box.setIcon(QMessageBox.Critical)
93  msg_box.setStandardButtons(QMessageBox.Ok)
94  msg_box.button(QMessageBox.Ok).setMinimumSize(100,40)
95  msg_box.exec_()
96  elif user_info.password == base64.b64encode(self.password_edit.text()):
97  GUserAccountCommunicate.update(user_info)
98  self.close()
99  else:
100  msg_box = MessageBox()
101  msg_box.setText(trUtf8("Invalid password",'!'))
102  msg_box.setIcon(QMessageBox.Critical)
103  msg_box.setStandardButtons(QMessageBox.Ok)
104  msg_box.button(QMessageBox.Ok).setMinimumSize(100,40)
105  msg_box.exec_()
106 
107  def translate(self):
108  self.user_id_label.setText(trUtf8('User id'))
109  self.password_label.setText(trUtf8('Password'))
110 
111 ## @class AddUserAccountWidget
112 ## @brief Add user account ui.
113 class AddUserAccountWidget(QWidget):
114 
115  def __init__(self):
116  """! The constructor."""
117 
118  QWidget.__init__(self)
119 
120  # Get path to UI file which is a sibling of this file
121  ui_file = airbus_cobot_gui_dir('resources', 'ui', 'add_user_widget.ui')
122  # Extend the widget with all attributes and children from UI file
123  loadUi(ui_file, self)
124 
125  self.password_edit.setEchoMode(QLineEdit.Password)
126  self.confirm_password_edit.setEchoMode(QLineEdit.Password);
127 
128  self.connect(self.add_user_button, SIGNAL("clicked()"),
129  self.add_user_account)
130 
131  self.translate()
132 
133  def add_user_account(self):
134  """! Check fields and add user account."""
135 
136  accounts = UserAccounts()
137 
138  user = User(self.user_id_edit.text())
139 
140  password = self.password_edit.text()
141  confirm = self.confirm_password_edit.text()
142 
143  if user.userid == "" or \
144  password == "" or \
145  confirm == "":
146  msg_box = MessageBox()
147  msg_box.setText(trUtf8("Some fields are not filled",'!'))
148  msg_box.setIcon(QMessageBox.Critical)
149  msg_box.setStandardButtons(QMessageBox.Ok)
150  msg_box.button(QMessageBox.Ok).setMinimumSize(100,40)
151  msg_box.exec_()
152  return
153 
154  if confirm != password:
155  msg_box = MessageBox()
156  msg_box.setText(trUtf8('The passwords are different','!'))
157  msg_box.setIcon(QMessageBox.Critical)
158  msg_box.setStandardButtons(QMessageBox.Ok)
159  msg_box.button(QMessageBox.Ok).setMinimumSize(100,40)
160  msg_box.exec_()
161  return
162  else:
163  user.set_password(password)
164 
165  user.privilege = Privilege.TOLEVEL[self.access_rights_combo.currentText().lower()]
166 
167  try:
168  accounts.add(user)
169  except Exception as e:
170  msg_box = MessageBox()
171  msg_box.setText(str(e))
172  msg_box.setIcon(QMessageBox.Critical)
173  msg_box.setStandardButtons(QMessageBox.Ok)
174  msg_box.button(QMessageBox.Ok).setMinimumSize(100,40)
175  msg_box.exec_()
176  return
177 
178  msg_box = MessageBox()
179  msg_box.setText(trUtf8('The user was added successfully','.'))
180  msg_box.setIcon(QMessageBox.Information)
181  msg_box.setStandardButtons(QMessageBox.Ok)
182  msg_box.button(QMessageBox.Ok).setMinimumSize(100,40)
183  msg_box.exec_()
184 
185  self.user_id_edit.setText("")
186  self.password_edit.setText("")
187  self.confirm_password_edit.setText("")
188  self.access_rights_combo.setCurrentIndex(0)
189 
190  def translate(self):
191 
192  self.user_id_label.setText(trUtf8('User id'))
193  self.password_label.setText(trUtf8('Password'))
194  self.cinfirm_password_label.setText(trUtf8('Confirm password'))
195  self.access_rights_label.setText(trUtf8('Access rights'))
196  self.add_user_button.setText(trUtf8('Ok'))
197 
198 
199 ## @class ModifUserAccountWidget
200 ## @brief Modif user account ui.
201 class ModifUserAccountWidget(QWidget):
202 
203  def __init__(self):
204  """! The constructor."""
205 
206  QWidget.__init__(self)
207 
208  # Get path to UI file which is a sibling of this file
209  ui_file = airbus_cobot_gui_dir('resources', 'ui', 'modif_account_widget.ui')
210  # Extend the widget with all attributes and children from UI file
211  loadUi(ui_file, self)
212 
213  self.new_password_edit.setEchoMode(QLineEdit.Password)
214  self.confirm_new_password_edit.setEchoMode(QLineEdit.Password)
215 
216  self._accounts = UserAccounts()
217  self.user_selected = User()
218  self.user_dst = User()
219 
220  self.users_list_combo.addItems(self._accounts.user_list())
221  self.users_list_combo.currentIndexChanged.connect(self.update_user_info)
222 
223  self.connect(self.modif_user_account_button, SIGNAL("clicked()"),
224  self.modif_user_account)
225 
226  self.connect(self.check_password_button, SIGNAL("clicked()"),
227  self.check_password)
228 
229  self.current_password_edit.textChanged[str].connect(self.current_password_changed)
230 
231  self.translate()
232 
234  self.init_fields(False)
235 
236  def check_password(self):
237 
238  text = self.current_password_edit.text()
239 
240  if text == self.user_selected.get_password(True):
241 
242  self.current_password_edit.setStyleSheet("""background-color: #ffffff;
243  border-radius: 5px;
244  font-size: 16pt;
245  font-weight:40;
246  color: rgb(0,255,0);""")
247  privilege = self.user_selected.privilege
248  self.access_rights_combo.setCurrentIndex(privilege+1)
249  self.new_password_edit.setText(self.user_selected.get_password(True))
250  self.confirm_new_password_edit.setText(self.user_selected.get_password(True))
251 
252  self.new_password_edit.setEnabled(True)
253  self.confirm_new_password_edit.setEnabled(True)
254  self.access_rights_combo.setEnabled(True)
255  self.modif_user_account_button.setEnabled(True)
256 
257  else:
258 
259  self.init_fields(False)
260 
261  self.current_password_edit.setStyleSheet("""background-color: #ffffff;
262  border-radius: 5px;
263  font-size: 16pt;
264  font-weight:40;
265  color: rgb(255,0,0);""")
266 
267 
268  def update_user_info(self, index):
269  """! Update user information with user selected.
270  @param index: user list index.
271  @type index: int.
272  """
273  self.init_fields()
274 
275  if index == 0:
276  self.current_password_edit.setEnabled(False)
277  else:
278  self.user_selected = self._accounts.find(self.users_list_combo.itemText(index))
279  self.current_password_edit.setEnabled(True)
280 
282  """! Check fields and modify user account."""
283 
284  user_modified = User(self.user_selected.userid)
285 
286  if self.access_rights_combo.currentIndex() == 0:
287 
288  msg_box = MessageBox()
289  msg_box.setText(trUtf8('Select access rights','!'))
290  msg_box.setIcon(QMessageBox.Critical)
291  msg_box.setStandardButtons(QMessageBox.Ok)
292  msg_box.button(QMessageBox.Ok).setMinimumSize(100,40)
293  msg_box.exec_()
294  return
295  elif self.new_password_edit.text() != \
296  self.confirm_new_password_edit.text():
297 
298  msg_box = MessageBox()
299  msg_box.setText(trUtf8('The passwords are different','!'))
300  msg_box.setIcon(QMessageBox.Critical)
301  msg_box.setStandardButtons(QMessageBox.Ok)
302  msg_box.button(QMessageBox.Ok).setMinimumSize(100,40)
303  msg_box.exec_()
304  return
305  else:
306  user_modified.privilege = Privilege.TOLEVEL[self.access_rights_combo.currentText().lower()]
307  user_modified.set_password(self.new_password_edit.text())
308 
309  try:
310 
311  self._accounts.modif(self.user_selected, user_modified)
312  except Exception as e:
313 
314  msg_box = MessageBox()
315  msg_box.setText(str(e))
316  msg_box.setIcon(QMessageBox.Critical)
317  msg_box.setStandardButtons(QMessageBox.Ok)
318  msg_box.button(QMessageBox.Ok).setMinimumSize(100,40)
319  msg_box.exec_()
320  return
321 
322  msg_box = MessageBox()
323  msg_box.setText(trUtf8('The user was modified successfully','.'))
324  msg_box.setIcon(QMessageBox.Information)
325  msg_box.setStandardButtons(QMessageBox.Ok)
326  msg_box.button(QMessageBox.Ok).setMinimumSize(100,40)
327  msg_box.exec_()
328 
329  self.users_list_combo.setCurrentIndex(0)
330  self.init_fields()
331 
332  def init_fields(self, clear_all = True):
333 
334  self.access_rights_combo.setCurrentIndex(0)
335  self.new_password_edit.setText('')
336  self.confirm_new_password_edit.setText('')
337  self.new_password_edit.setEnabled(False)
338  self.confirm_new_password_edit.setEnabled(False)
339  self.access_rights_combo.setEnabled(False)
340  self.modif_user_account_button.setEnabled(False)
341 
342  if clear_all:
343  self.current_password_edit.setText('')
344 
345  self.current_password_edit.setStyleSheet("""background-color: #ffffff;
346  border-radius: 5px;
347  font-size: 16pt;
348  font-weight: 40;
349  color: #494842;""")
350  def translate(self):
351  self.select_user_label.setText(trUtf8('Select user'))
352  self.current_password_label.setText(trUtf8('Current password'))
353  self.check_password_button.setText(trUtf8('Ok'))
354  self.access_rights_label.setText(trUtf8('Access rights'))
355  self.new_password_label.setText(trUtf8('New password'))
356  self.confirm_password_label.setText(trUtf8('Confirm password'))
357  self.modif_user_account_button.setText(trUtf8('Ok'))
358 
359 ## @class RemoveUserAccountWidget
360 ## @brief Remove user account ui.
361 class RemoveUserAccountWidget(QWidget):
362 
363  def __init__(self):
364  """! The constructor."""
365 
366  QWidget.__init__(self)
367 
368  # Get path to UI file which is a sibling of this file
369  ui_file = airbus_cobot_gui_dir('resources', 'ui', 'remove_account_widget.ui')
370  # Extend the widget with all attributes and children from UI file
371  loadUi(ui_file, self)
372 
373  self.connect(self.remove_button, SIGNAL("clicked()"),
374  self.remove_account)
375 
376  self._accounts = UserAccounts()
377 
378  self.users_list_combo.addItems(self._accounts.user_list())
379 
380  self.translate()
381 
382  def remove_account(self):
383  """! Remove user account slected in user list."""
384 
385  if self.users_list_combo.currentIndex() == 0:
386  msg_box = MessageBox()
387  msg_box.setText(trUtf8('Select user','!'))
388  msg_box.setIcon(QMessageBox.Critical)
389  msg_box.setStandardButtons(QMessageBox.Ok)
390  msg_box.button(QMessageBox.Ok).setMinimumSize(100,40)
391  msg_box.exec_()
392  return
393 
394  user_id = self.users_list_combo.currentText()
395  try:
396  self._accounts.remove(User(user_id))
397  self.users_list_combo.removeItem(self.users_list_combo.currentIndex())
398  except Exception as e:
399  msg_box = MessageBox()
400  msg_box.setText(str(e))
401  msg_box.setIcon(QMessageBox.Critical)
402  msg_box.setStandardButtons(QMessageBox.Ok)
403  msg_box.button(QMessageBox.Ok).setMinimumSize(100,40)
404  msg_box.exec_()
405  return
406 
407  self.users_list_combo.setCurrentIndex(0)
408  msg_box = MessageBox()
409  msg_box.setText(trUtf8('The user was removed successfully','.'))
410  msg_box.setIcon(QMessageBox.Information)
411  msg_box.setStandardButtons(QMessageBox.Ok)
412  msg_box.button(QMessageBox.Ok).setMinimumSize(100,40)
413  msg_box.exec_()
414 
415  def translate(self):
416  self.user_list_label.setText(trUtf8('User list'))
417  self.remove_button.setText(trUtf8('Ok'))
418 
419 ## @class UsersAccountsManagerDialog
420 ## @brief User accounts manager ui.
422 
423  def __init__(self, parent):
424  """! The constructor."""
425 
426  QDialog.__init__(self, parent, Qt.FramelessWindowHint)
427 
428  # Get path to UI file which is a sibling of this file
429  ui_file = airbus_cobot_gui_dir('resources', 'ui', 'users_accounts_dialog.ui')
430  # Extend the widget with all attributes and children from UI file
431  loadUi(ui_file, self)
432 
433  self.setModal(True)
434 
435  self.connect(self.exit_button, SIGNAL("clicked()"), self.close)
436  self.connect(self.add_button, SIGNAL("clicked()"), self.load_add_user_ui)
437  self.connect(self.modif_button, SIGNAL("clicked()"), self.load_modif_user_ui)
438  self.connect(self.remove_button, SIGNAL("clicked()"), self.load_remove_user_ui)
439 
440  self.translate()
441 
442  self.add_button.click()
443 
444  def load_add_user_ui(self):
445  """! Open add user account ui."""
446  self.header_label.setText(trUtf8('Add user account'))
447 
448  self.viewer_area.takeWidget()
449  add_account_ui = AddUserAccountWidget()
450  add_account_ui.resize(self.viewer_area.width()-2,
451  self.viewer_area.height()-2)
452  self.viewer_area.setWidget(add_account_ui)
453 
455  """! Open modif user account ui."""
456  self.header_label.setText(trUtf8('Modif user account'))
457 
458  self.viewer_area.takeWidget()
459  modif_account_ui = ModifUserAccountWidget()
460  modif_account_ui.resize(self.viewer_area.width()-2,
461  self.viewer_area.height()-2)
462  self.viewer_area.setWidget(modif_account_ui)
463 
465  """! Open remove user account ui."""
466  self.header_label.setText(trUtf8('Remove user account'))
467 
468  self.viewer_area.takeWidget()
469  remove_account = RemoveUserAccountWidget()
470  remove_account.resize(self.viewer_area.width()-2,
471  self.viewer_area.height()-2)
472  self.viewer_area.setWidget(remove_account)
473 
474  def translate(self):
475  self.settings_label.setText(trUtf8('Settings'))
476  self.header_label.setText(trUtf8('Account manager'))
477  self.add_button.setText(trUtf8('Add'))
478  self.modif_button.setText(trUtf8('Modif'))
479  self.remove_button.setText(trUtf8('Remove'))
480  self.exit_button.setText(trUtf8('Exit'))
481 
482 ## @class UserAccountPopup
483 ## @brief User accounts popup ui.
485 
486  def __init__(self, parent):
487  """! The constructor."""
488 
489  QPopup.__init__(self, parent)
490 
491  self.set_corners_link(QPopup.TopRight, QPopup.BottomRight)
492 
494 
495  # Get path to UI file which is a sibling of this file
496  ui_file = os.path.join(self.resources_dir,'ui','account_popup.ui')
497  # Extend the widget with all attributes and children from UI file
498  loadUi(ui_file, self)
499 
500  user_icon = QPixmap(os.path.join(self.resources_dir,
501  'icons','ico_user.png'))
502  self.user_icon_label.setPixmap(user_icon)
503 
504  self.connect(self.connection_button, SIGNAL("clicked()"),
505  self.open_login_dialog)
506  self.connect(self.accounts_manager_button, SIGNAL("clicked()"),
508  self.connect(self.deconnection_button, SIGNAL("clicked()"),
510  self.deconnection_button.setEnabled(True)
511 
512  user_info = GUserAccountCommunicate.get_current_user_account()
513 
514  if user_info.privilege < Privilege.EXPERT:
515  self.accounts_manager_button.setEnabled(False)
516  else:
517  self.accounts_manager_button.setEnabled(True)
518 
519  self.user_id_label.setText(user_info.userid)
520 
521  privilege = Privilege.TOSTR[user_info.privilege]
522  self.access_rights_label.setText(privilege[0].upper()+privilege[1:])
523  self.language_label.setText(trUtf8.language)
524  self.time_label.setText('2h:38m')
525 
526  self.adjustSize()
527 
529  """! Refresh connection time."""
530  self.time_label.setText(self._user.connection_time())
531 
532  def open_login_dialog(self):
533  """! Open login ui."""
534  login = LoginDialog(self)
535  login.show()
536 
538  """! Open account manager ui."""
539  manager = UsersAccountsManagerDialog(self)
540  manager.show()
541 
543  """! Disconnect current user."""
544  GUserAccountCommunicate.update(User())
545  login = LoginDialog(self, closable = False)
546  login.show()
547 
548  def translate(self):
549 
550  self.header_label.setText(trUtf8('User account'))
551  self.user_id_header_label.setText(trUtf8('User'))
552  self.access_rights_header_label.setText(trUtf8('Rights'))
553  self.language_header_label.setText(trUtf8('Language'))
554  self.time_header_label.setText(trUtf8('Time'))
555  self.connection_button.setText(trUtf8('Connection'))
556  self.deconnection_button.setText(trUtf8('Disconnection'))
557  self.accounts_manager_button.setText(trUtf8('Account manager'))
558 
559 ## @class UserAccountsWidget
560 ## @brief User connected information displaying on dashboard.
561 class UserAccountsWidget(QWidget):
562 
563  def __init__(self):
564  """! The constructor."""
565 
566  QWidget.__init__(self)
567 
568  self.setMinimumSize(QSize(50, 10))
569  self.setMaximumSize(QSize(600, 30))
570 
571  ######
572 
573  self._central_layout = QHBoxLayout(self)
574  self._central_layout.setContentsMargins(0, 0, 0, 0)
575  self._central_layout.setSpacing(5)
576 
577  ######
578 
579  self._user_icon_label = QLabel()
580  self._user_icon_label.setStyleSheet('background-color: transparent;')
581  self._user_icon_label.setMinimumSize(QSize(25,10))
582  self._user_icon_label.setMaximumSize(QSize(30,30))
583 
584  ######
585 
586  self._user_id_label = QLabel()
587  self._user_id_label.setStyleSheet('background-color: transparent;')
588 
589  ######
590 
591  self._central_layout.addWidget(self._user_icon_label)
592  self._central_layout.addWidget(self._user_id_label)
593 
594  self.connect(GUserAccountCommunicate,
595  SIGNAL('userAccountChanged'),
596  self.update_user_info)
597 
598  def update_user_info(self, user):
599  """! Update user information.
600  @param user_info: user connected information.
601  @type user_info: UserInfo.
602  """
603  self._user_id_label.setText(user.userid)
604 
605  def resizeEvent(self,event):
606  """! Redefine qt methode for resize widget.
607  @param event: qt event.
608  @type event: QEvent.
609  """
610 
611  self.setStyleSheet("QLabel {font-size: %ipt;}"%(int(self.height()/2)))
612  self.resize(self.width(),self.height())
613 
614  ico_user = QPixmap(os.path.join(get_pkg_dir('airbus_cobot_gui'),
615  'resources',
616  'icons',
617  'ico_user.png'))
618 
619  ico_user = ico_user.scaled(self._user_icon_label.width(),
620  self._user_icon_label.height(),
621  Qt.KeepAspectRatio,
622  Qt.SmoothTransformation)
623  self._user_icon_label.setPixmap(ico_user)
624 
625  def mousePressEvent(self, event):
626  """! Redefine qt methode for resize widget.
627  @param event: qt event.
628  @type event: QEvent.
629  """
630  popup = UserAccountPopup(self)
631  popup.translate()
632  popup.show_()
633 
634  def translate(self):
635  pass
636 
637 if __name__ == "__main__":
638 
639  rospy.init_node('unittest_account_ui')
640 
641  import sys
642 
643  class Widget(QWidget):
644  def __init__(self):
645  QWidget.__init__(self)
646 
647  def mousePressEvent(self, event):
648  popup = UserAccountPopup(self)
649  popup.show_()
650 
651  a = QApplication(sys.argv)
652 
653  translator = QTranslator()
654  trans_dir = airbus_cobot_gui_dir('translations')
655  translator.load(os.path.join(trans_dir,'fr.qm'))
656  qApp.installTranslator(translator)
657 
658  utt_appli = QMainWindow()
659 
660  user = User()
661  user.userid = 'AirbusGroup'
662  user.privilege = Privilege.EXPERT
663 
664  account_ui = UserAccountsWidget()
665 
666  GUserAccountCommunicate.update(user)
667 
668  utt_appli.setCentralWidget(account_ui)
669 
670  utt_appli.show()
671  a.exec_()
672 
673 #End of file
def account_validation
Check user account validity and connect user.
Definition: accounts_ui.py:82
def add_user_account
Check fields and add user account.
Definition: accounts_ui.py:133
def remove_account
Remove user account slected in user list.
Definition: accounts_ui.py:382
def modif_user_account
Check fields and modify user account.
Definition: accounts_ui.py:281
User connected information displaying on dashboard.
Definition: accounts_ui.py:561
def mousePressEvent
Redefine qt methode for resize widget.
Definition: accounts_ui.py:625
def open_accounts_manager_dialog
Open account manager ui.
Definition: accounts_ui.py:537
def resizeEvent
Redefine qt methode for resize widget.
Definition: accounts_ui.py:605
def disconnect_user_account
Disconnect current user.
Definition: accounts_ui.py:542
def update_user_info
Update user information with user selected.
Definition: accounts_ui.py:268
def _resfresh_connection_time
Refresh connection time.
Definition: accounts_ui.py:528
def set_corners_link
Geometric link between parent and popup object.
Definition: qpopup.py:69


airbus_cobot_gui
Author(s):
autogenerated on Thu Dec 17 2015 11:42:05