source: src-qt4/PCDM/src/loginWidget.cpp @ 2d615ae

9.2-releasereleng/10.0releng/10.0.1releng/10.0.2releng/10.0.3
Last change on this file since 2d615ae was 2d615ae, checked in by Ken Moore <ken@…>, 17 months ago

Fix the PCDM background to the main window only, and remove an extra function from the loginwidget class.

  • Property mode set to 100644
File size: 10.1 KB
Line 
1/* PCDM Login Manager:
2*  Written by Ken Moore (ken@pcbsd.org) 2012/2013
3*  Copyright(c) 2013 by the PC-BSD Project
4*  Available under the 3-clause BSD license
5*/
6
7/*
8Sub-classed widget for inputting login information
9*/
10
11#include "loginWidget.h"
12
13LoginWidget::LoginWidget(QWidget* parent) : QGroupBox(parent)
14{
15  //Set the default values for the current item
16  idL.clear(); //Clear the login names available
17  userSelected = FALSE; //focus on the username first
18  pwVisible = FALSE; //Have the password box hide the input behind dots
19  allowPWVisible = TRUE; //Allow the password button to show the password text
20 
21  //Create the Grid layout
22  QHBoxLayout* hlayout1 = new QHBoxLayout();
23  QHBoxLayout* hlayout2 = new QHBoxLayout();
24  QVBoxLayout* vlayout = new QVBoxLayout();
25  QFormLayout* flayout = new QFormLayout();
26  //Create the items
27  userIcon = new QToolButton;
28        QAction* tmp = new QAction(this);
29        userIcon->setDefaultAction( tmp );
30        userIcon->setFocusPolicy(Qt::NoFocus);
31  listUserBig = new QListWidget;
32        listUserBig->setFocusPolicy(Qt::StrongFocus);
33        listUserBig->setSelectionMode(QAbstractItemView::SingleSelection);
34  listUsers = new QComboBox;
35        listUsers->setFocusPolicy(Qt::NoFocus); //big one gets keyboard focus instead
36  linePassword = new QLineEdit;
37        linePassword->setFocusPolicy(Qt::StrongFocus);
38  pushLogin = new QToolButton;
39        QAction* tmp1 = new QAction(this);
40        pushLogin->setDefaultAction( tmp1 );
41        pushLogin->setFocusPolicy(Qt::StrongFocus);
42  pushViewPassword = new QToolButton;
43        QAction* tmp2 = new QAction(this);
44        pushViewPassword->setDefaultAction( tmp2 );
45        pushViewPassword->setFocusPolicy(Qt::NoFocus);
46  pushUserIcon = new QToolButton;
47        QAction* tmp3 = new QAction(this);
48        pushUserIcon->setDefaultAction( tmp3 );
49        pushUserIcon->setFocusPolicy(Qt::NoFocus);
50  //Add the items to the grid
51    //user not yet selected widgets
52      hlayout1->addWidget(userIcon);
53    vlayout->addLayout(hlayout1);
54    vlayout->addWidget(listUserBig);
55    //User selected widgets
56      flayout->addRow(pushUserIcon, listUsers);
57      flayout->addRow(pushViewPassword, linePassword);
58    vlayout->addLayout(flayout);
59      hlayout2->addWidget(pushLogin);
60    vlayout->addLayout(hlayout2);
61   
62  //Setup the Signals/Slots
63  connect(pushLogin,SIGNAL(clicked()),this,SLOT(slotTryLogin()));
64  connect(pushUserIcon,SIGNAL(triggered(QAction*)), this, SLOT(slotUserActivated(QAction*)));
65  connect(userIcon,SIGNAL(triggered(QAction*)), this, SLOT(slotUserActivated(QAction*)));
66  connect(listUsers,SIGNAL(activated(int)),this,SLOT(slotChooseUser(int)));
67  connect(listUserBig,SIGNAL(itemClicked(QListWidgetItem*)),this,SLOT(slotUserSelected()) );
68  connect(listUserBig,SIGNAL(currentRowChanged(int)),this,SLOT(slotUserHighlighted(int)) );
69  allowPasswordView(allowPWVisible); //setup signal/slots for pushViewPassword
70  //Set this layout for the loginWidget
71  this->setLayout(vlayout);
72 
73  //Run the display setup to ensure appropriate visiblility/usage
74  updateWidget();
75 
76}
77
78LoginWidget::~LoginWidget(){
79}
80
81void LoginWidget::updateWidget(){
82  //Setup the visibility/sizes
83  if(userSelected){
84    userIcon->setVisible(FALSE);
85    listUserBig->setVisible(FALSE);
86    pushUserIcon->setVisible(TRUE);
87    listUsers->setVisible(TRUE);
88    linePassword->setVisible(TRUE);
89    pushLogin->setVisible(TRUE);
90    pushViewPassword->setVisible(TRUE);
91  }else{
92    userIcon->setVisible(TRUE);
93    listUserBig->setVisible(TRUE);
94    pushUserIcon->setVisible(FALSE);
95    listUsers->setVisible(FALSE);
96    linePassword->setVisible(FALSE);
97    pushLogin->setVisible(FALSE);
98    pushViewPassword->setVisible(FALSE);   
99  }
100  if(pwVisible){
101    linePassword->setEchoMode(QLineEdit::Normal);         
102  }else{
103    linePassword->setEchoMode(QLineEdit::Password);       
104  }
105  retranslateUi();
106}
107
108void LoginWidget::keyPressEvent(QKeyEvent *e){
109  if( (e->key()==Qt::Key_Enter) || (e->key()==Qt::Key_Return) ){
110    if(userSelected){
111      slotTryLogin();
112    }else{
113      slotUserSelected();     
114    }
115  }else if(e->key() == Qt::Key_Escape){
116    if(userSelected){
117      slotUserUnselected();
118    }else{
119      emit escapePressed();
120    }
121  }
122}
123
124//-------------------------------------
125//    PRIVATE SLOTS
126//-------------------------------------
127void LoginWidget::slotUserActivated(QAction* act){
128    //Toggle the user box as to what is visible
129    if(!userSelected){ 
130        slotUserSelected(); 
131    }else{
132        slotUserUnselected();
133    }
134}
135
136/*void LoginWidget::slotUserClicked(QListWidgetItem* item){
137  slotUserSelected();   
138}*/
139
140void LoginWidget::slotUserHighlighted(int row){
141  emit UserChanged(idL[row]);   
142}
143
144void LoginWidget::slotTryLogin(){
145  QString user = listUsers->currentText();
146  QString pw = linePassword->text();
147  emit loginRequested(user,pw);
148  linePassword->setText(""); //always clear the box after a login attempt
149}
150
151void LoginWidget::slotChooseUser(int i){
152  if(i!=-1){ slotUserSelected(); }     
153}
154
155void LoginWidget::slotUserSelected(){
156  if(userSelected){ //make sure the big user widget is updated as well
157    listUserBig->setCurrentRow( listUsers->currentIndex() );
158  }else{ //make sure the small user widget is updated as well
159    listUsers->setCurrentIndex( listUserBig->currentRow() );
160  }
161  userSelected = TRUE;
162  pwVisible = FALSE;
163  linePassword->clear(); //make sure the password is cleared if the user is changed
164  updateWidget();
165  linePassword->setFocus();
166  emit UserSelected(listUsers->currentText());
167}
168
169void LoginWidget::slotUserUnselected(){
170  userSelected = FALSE;
171  pwVisible = FALSE;
172  linePassword->clear(); //make sure the password is cleared if the user is changed
173  updateWidget();
174  listUserBig->setFocus();
175  emit UserSelected("");
176}
177
178void LoginWidget::slotChangePWView(){
179  if(pwVisible){
180    pwVisible = FALSE;
181  }else{
182    pwVisible = TRUE;
183  }
184  updateWidget();
185}
186
187//-----------------------------
188//     PUBLIC FUNCTIONS
189//-----------------------------
190QString LoginWidget::currentUsername(){
191  QString id = listUsers->currentText();
192  return id;
193}
194
195QString LoginWidget::currentPassword(){
196  QString pw = linePassword->text();
197  return pw;
198}
199
200void LoginWidget::setCurrentUser(QString id){
201  int index = idL.indexOf(id);
202  if(index == -1){
203    qDebug() << "LoginWidget: Item does not exist -" << id;
204  }else{
205    listUsers->setCurrentIndex(index);
206    listUserBig->setCurrentRow(index);
207    emit UserChanged(id);
208  }
209}
210
211void LoginWidget::setUsernames(QStringList uList){
212  if(uList.isEmpty()){ return; }
213  //Make sure that the two user widgets are identical
214  listUsers->clear();
215  listUsers->addItems(uList);
216  listUserBig->clear();
217  listUserBig->addItems(uList);
218  idL.clear();
219  idL = uList; //save for later
220  listUsers->setCurrentIndex(0);
221  listUserBig->setCurrentRow(0);
222  //Automatically select the user if there is only one
223  if(uList.length() == 1){
224    qDebug() << "Single User System Detected";
225    slotUserSelected(); 
226  }
227}
228
229void LoginWidget::displayHostName(QString name){
230  hostName = name;
231}
232
233void LoginWidget::changeButtonIcon(QString button, QString iconFile, QSize iconSize){
234  if(!QFile::exists(iconFile)){ qDebug() << "LoginWidget: invalid image file"<<iconFile; return; }
235  if(button.toLower() == "display"){ 
236    pushUserIcon->setIcon(QIcon(iconFile));
237    pushUserIcon->setIconSize(iconSize);
238    userIcon->setIcon(QIcon(iconFile));
239    userIcon->setIconSize(iconSize);
240  }else if(button.toLower() == "login"){ 
241    pushLogin->setIcon(QIcon(iconFile));
242    pushLogin->setIconSize(iconSize);
243  }else if(button.toLower() == "pwview"){ 
244    pushViewPassword->setIcon(QIcon(iconFile));
245    pushViewPassword->setIconSize(iconSize);
246  }else{ 
247    qDebug() << "LoginWidget: Cannot change the icon for button" << button << " - valid buttons are \"display\", \"login\", and \"pwview\""; 
248  }
249}
250
251void LoginWidget::changeStyleSheet(QString item, QString style){
252  //Propagate the style sheet to both the buttons and the iconViewer
253  if(item.toLower()=="login"){ pushLogin->setStyleSheet(style); }
254  else if(item.toLower()=="pwview"){ pushViewPassword->setStyleSheet(style); }
255  else if(item.toLower()=="user"){ listUsers->setStyleSheet(style); }
256  else if(item.toLower()=="password"){ linePassword->setStyleSheet(style); }
257  else{ 
258    qDebug() << "LoginWidget: Invalid item to change style" << item << " - valid items are \"login\", \"pwview\", \"user\", and \"password\""; 
259  }
260}
261
262void LoginWidget::retranslateUi(){
263  //Set all the text for the widget (to easily allow changing the locale)
264  pushUserIcon->setText(tr("Select")); 
265  pushUserIcon->setToolTip(tr("Select an alternate user and clear the password field"));
266  userIcon->setText(tr("Select"));
267  userIcon->setToolTip(tr("Select this user"));
268 
269  pushLogin->setText(tr("Login"));
270  pushLogin->setToolTip(tr("Login to the system with the current user and password"));
271  pushViewPassword->setText(tr("Password"));
272  if(allowPWVisible){
273    pushViewPassword->setToolTip(tr("Hold to view the currently entered password"));
274  }else{
275    pushViewPassword->setToolTip(tr("Login password for the selected user"));     
276  }
277  listUsers->setToolTip(tr("Available users"));
278  listUserBig->setToolTip(tr("Available users"));
279  linePassword->setToolTip(tr("Login password for the selected user"));
280  //Setup the computer/host name display
281  if( hostName.isEmpty() ){
282    this->setTitle("");
283  }else{
284    this->setTitle( QString(tr("Login to %1")).arg(hostName) );
285  }
286}
287
288void LoginWidget::resetFocus(QString item){
289  //Check for appropriate action if not specified
290  if(item.isEmpty() && userSelected ){ item="password"; }
291  else if(item.isEmpty() && !userSelected ){ item="userlist"; }   
292  //Set the proper keyboard focus
293  if(item == "userlist"){
294    if(userSelected){ listUsers->setFocus(); }
295    else{ listUserBig->setFocus(); }
296  }else if(item == "password"){
297    linePassword->setFocus();
298  }else{
299    //By default, de-select the user and set focus on the user selection
300    slotUserUnselected();   
301    listUserBig->setFocus();
302  }
303}
304
305void LoginWidget::allowPasswordView(bool allow){
306  //Enable/disable the password view button as desired
307  allowPWVisible = allow;
308  pushViewPassword->disconnect(); //disconnect the signal/slot (keep nice appearance)
309  if(allow){
310    connect(pushViewPassword,SIGNAL(pressed()),this,SLOT(slotChangePWView()));
311    connect(pushViewPassword,SIGNAL(released()),this,SLOT(slotChangePWView()));   
312  }
313}
Note: See TracBrowser for help on using the repository browser.