source: src-qt4/PCDM/src/loginWidget.cpp @ bac293d

releng/10.0releng/10.0.1releng/10.0.2releng/10.0.3releng/10.1
Last change on this file since bac293d was bac293d, checked in by Ken Moore <ken@…>, 13 months ago

Fix a bug in PCDM where the newly sorted desktop sessions had their icons scrambled.

  • Property mode set to 100644
File size: 11.6 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  listDE = new QComboBox;
51  deIcon = new QLabel;
52       
53  //Add the items to the grid
54    //user not yet selected widgets
55      hlayout1->addWidget(userIcon);
56    vlayout->addLayout(hlayout1);
57    vlayout->addWidget(listUserBig);
58    //User selected widgets
59      flayout->addRow(pushUserIcon, listUsers);
60      flayout->addRow(pushViewPassword, linePassword);
61      flayout->addRow(deIcon,listDE);
62    vlayout->addSpacing(15);
63    vlayout->addLayout(flayout);
64    vlayout->addSpacing(20);
65      hlayout2->addWidget(pushLogin);
66    vlayout->addLayout(hlayout2);
67   
68  //Setup the Signals/Slots
69  connect(pushLogin,SIGNAL(clicked()),this,SLOT(slotTryLogin()));
70  connect(pushUserIcon,SIGNAL(triggered(QAction*)), this, SLOT(slotUserActivated()));
71  connect(userIcon,SIGNAL(triggered(QAction*)), this, SLOT(slotUserActivated()));
72  connect(listUsers,SIGNAL(activated(int)),this,SLOT(slotChooseUser(int)));
73  connect(listUserBig,SIGNAL(itemClicked(QListWidgetItem*)),this,SLOT(slotUserSelected()) );
74  connect(listUserBig,SIGNAL(currentRowChanged(int)),this,SLOT(slotUserHighlighted(int)) );
75  connect(listDE,SIGNAL(currentIndexChanged(int)),this,SLOT(slotDesktopChanged(int)) );
76  allowPasswordView(allowPWVisible); //setup signal/slots for pushViewPassword
77  //Set this layout for the loginWidget
78  this->setLayout(vlayout);
79 
80  //Run the display setup to ensure appropriate visiblility/usage
81  updateWidget();
82 
83}
84
85LoginWidget::~LoginWidget(){
86}
87
88void LoginWidget::updateWidget(){
89  //Setup the visibility/sizes
90  if(userSelected){
91    userIcon->setVisible(FALSE);
92    listUserBig->setVisible(FALSE);
93    pushUserIcon->setVisible(TRUE);
94    listUsers->setVisible(TRUE);
95    linePassword->setVisible(TRUE);
96    pushLogin->setVisible(TRUE);
97    pushViewPassword->setVisible(TRUE);
98    if( listDE->count() < 1 ){ listDE->setVisible(FALSE); deIcon->setVisible(FALSE); }
99    else{ listDE->setVisible(TRUE); deIcon->setVisible(TRUE); }
100  }else{
101    userIcon->setVisible(TRUE);
102    listUserBig->setVisible(TRUE);
103    pushUserIcon->setVisible(FALSE);
104    listUsers->setVisible(FALSE);
105    linePassword->setVisible(FALSE);
106    pushLogin->setVisible(FALSE);
107    pushViewPassword->setVisible(FALSE); 
108    listDE->setVisible(FALSE);
109    deIcon->setVisible(FALSE);
110  }
111  if(pwVisible){
112    linePassword->setEchoMode(QLineEdit::Normal);         
113  }else{
114    linePassword->setEchoMode(QLineEdit::Password);       
115  }
116  retranslateUi();
117}
118
119void LoginWidget::keyPressEvent(QKeyEvent *e){
120  if( (e->key()==Qt::Key_Enter) || (e->key()==Qt::Key_Return) ){
121    if(userSelected){
122      slotTryLogin();
123    }else{
124      slotUserSelected();     
125    }
126  }else if(e->key() == Qt::Key_Escape){
127    if(userSelected){
128      slotUserUnselected();
129    }else{
130      emit escapePressed();
131    }
132  }
133}
134
135//-------------------------------------
136//    PRIVATE SLOTS
137//-------------------------------------
138void LoginWidget::slotUserActivated(){
139    //Toggle the user box as to what is visible
140    if(!userSelected){ 
141        slotUserSelected(); 
142    }else{
143        slotUserUnselected();
144    }
145}
146
147/*void LoginWidget::slotUserClicked(QListWidgetItem* item){
148  slotUserSelected();   
149}*/
150
151void LoginWidget::slotUserHighlighted(int row){
152  emit UserChanged(idL[row]);   
153}
154
155void LoginWidget::slotTryLogin(){
156  QString user = listUsers->currentText();
157  QString pw = linePassword->text();
158  emit loginRequested(user,pw);
159  linePassword->setText(""); //always clear the box after a login attempt
160}
161
162void LoginWidget::slotChooseUser(int i){
163  if(i!=-1){ slotUserSelected(); }     
164}
165
166void LoginWidget::slotUserSelected(){
167  if(userSelected){ //make sure the big user widget is updated as well
168    listUserBig->setCurrentRow( listUsers->currentIndex() );
169  }else{ //make sure the small user widget is updated as well
170    listUsers->setCurrentIndex( listUserBig->currentRow() );
171  }
172  userSelected = TRUE;
173  pwVisible = FALSE;
174  linePassword->clear(); //make sure the password is cleared if the user is changed
175  updateWidget();
176  linePassword->setFocus();
177  emit UserSelected(listUsers->currentText());
178}
179
180void LoginWidget::slotUserUnselected(){
181  userSelected = FALSE;
182  pwVisible = FALSE;
183  linePassword->clear(); //make sure the password is cleared if the user is changed
184  updateWidget();
185  listUserBig->setFocus();
186  emit UserSelected("");
187}
188
189void LoginWidget::slotChangePWView(){
190  if(pwVisible){
191    pwVisible = FALSE;
192  }else{
193    pwVisible = TRUE;
194  }
195  updateWidget();
196}
197
198void LoginWidget::slotDesktopChanged(int index){
199  if(index == -1){
200    deIcon->setPixmap(QPixmap(""));
201    deIcon->setToolTip("");
202  }else{
203    deIcon->setPixmap( QPixmap(desktopIcons[index]).scaled(desktopIconSize) );
204    deIcon->setToolTip(desktopInfo[index]);
205  }
206}
207
208//-----------------------------
209//     PUBLIC FUNCTIONS
210//-----------------------------
211QString LoginWidget::currentUsername(){
212  QString id = listUsers->currentText();
213  return id;
214}
215
216QString LoginWidget::currentPassword(){
217  QString pw = linePassword->text();
218  return pw;
219}
220
221QString LoginWidget::currentDE(){
222  QString de = listDE->currentText();
223  return de;
224}
225
226void LoginWidget::setCurrentUser(QString id){
227  int index = idL.indexOf(id);
228  if(index == -1){
229    qDebug() << "LoginWidget: Item does not exist -" << id;
230  }else{
231    listUsers->setCurrentIndex(index);
232    listUserBig->setCurrentRow(index);
233    emit UserChanged(id);
234  }
235}
236
237void LoginWidget::setCurrentDE(QString de){
238  for(int i=0; i<listDE->count(); i++){
239    if( listDE->itemText(i) == de ){
240      listDE->setCurrentIndex(i);
241      break;
242    }
243  }
244  updateWidget();
245}
246
247void LoginWidget::setUsernames(QStringList uList){
248  if(uList.isEmpty()){ return; }
249  //Make sure that the two user widgets are identical
250  listUsers->clear();
251  listUsers->addItems(uList);
252  listUserBig->clear();
253  listUserBig->addItems(uList);
254  idL.clear();
255  idL = uList; //save for later
256  listUsers->setCurrentIndex(0);
257  listUserBig->setCurrentRow(0);
258  //Automatically select the user if there is only one
259  if(uList.length() == 1){
260    qDebug() << "Single User System Detected";
261    slotUserSelected(); 
262  }
263}
264
265void LoginWidget::setDesktops(QStringList text, QStringList icon, QStringList info){
266  if((text.length() != icon.length()) && (text.length() != info.length()) ){ qDebug() << "LoginWidget: Unequal text/icon desktop lists"; return; }
267  listDE->clear();
268  desktopIcons.clear(); desktopInfo.clear();
269  desktopIcons = icon; //save for later
270  desktopInfo = info; //save for later
271  for(int i=0; i<text.length(); i++){
272    listDE->addItem(text[i]);
273  }
274}
275
276void LoginWidget::displayHostName(QString name){
277  hostName = name;
278}
279
280void LoginWidget::changeButtonIcon(QString button, QString iconFile, QSize iconSize){
281  if(!QFile::exists(iconFile)){ qDebug() << "LoginWidget: invalid image file"<<iconFile; return; }
282  if(button.toLower() == "display"){ 
283    pushUserIcon->setIcon(QIcon(iconFile));
284    pushUserIcon->setIconSize(iconSize);
285    userIcon->setIcon(QIcon(iconFile));
286    userIcon->setIconSize(iconSize);
287  }else if(button.toLower() == "login"){ 
288    pushLogin->setIcon(QIcon(iconFile));
289    pushLogin->setIconSize(iconSize);
290  }else if(button.toLower() == "pwview"){ 
291    pushViewPassword->setIcon(QIcon(iconFile));
292    pushViewPassword->setIconSize(iconSize);
293  }else{ 
294    qDebug() << "LoginWidget: Cannot change the icon for button" << button << " - valid buttons are \"display\", \"login\", and \"pwview\""; 
295  }
296}
297
298void LoginWidget::setDesktopIconSize(QSize iconsize){
299  desktopIconSize = iconsize;
300}
301
302void LoginWidget::changeStyleSheet(QString item, QString style){
303  //Propagate the style sheet to both the buttons and the iconViewer
304  if(item.toLower()=="login"){ pushLogin->setStyleSheet(style); }
305  else if(item.toLower()=="pwview"){ pushViewPassword->setStyleSheet(style); }
306  else if(item.toLower()=="user"){ listUsers->setStyleSheet(style); }
307  else if(item.toLower()=="password"){ linePassword->setStyleSheet(style); }
308  else{ 
309    qDebug() << "LoginWidget: Invalid item to change style" << item << " - valid items are \"login\", \"pwview\", \"user\", and \"password\""; 
310  }
311}
312
313void LoginWidget::retranslateUi(){
314  //Set all the text for the widget (to easily allow changing the locale)
315  pushUserIcon->setText(tr("Select")); 
316  pushUserIcon->setToolTip(tr("Select an alternate user and clear the password field"));
317  userIcon->setText(tr("Select"));
318  userIcon->setToolTip(tr("Select this user"));
319 
320  pushLogin->setText(tr("Login"));
321  pushLogin->setToolTip(tr("Login to the system with the current user and password"));
322  pushViewPassword->setText(tr("Password"));
323  if(allowPWVisible){
324    pushViewPassword->setToolTip(tr("Hold to view the currently entered password"));
325  }else{
326    pushViewPassword->setToolTip(tr("Login password for the selected user"));     
327  }
328  listUsers->setToolTip(tr("Available users"));
329  listUserBig->setToolTip(tr("Available users"));
330  linePassword->setToolTip(tr("Login password for the selected user"));
331  listDE->setToolTip(tr("Available desktop environments"));
332  //Setup the computer/host name display
333  if( hostName.isEmpty() ){
334    this->setTitle("");
335  }else{
336    this->setTitle( QString(tr("Login to %1")).arg(hostName) );
337  }
338}
339
340void LoginWidget::resetFocus(QString item){
341  //Check for appropriate action if not specified
342  if(item.isEmpty() && userSelected ){ item="password"; }
343  else if(item.isEmpty() && !userSelected ){ item="userlist"; }   
344  //Set the proper keyboard focus
345  if(item == "userlist"){
346    if(userSelected){ listUsers->setFocus(); }
347    else{ listUserBig->setFocus(); }
348  }else if(item == "password"){
349    linePassword->setFocus();
350  }else{
351    //By default, de-select the user and set focus on the user selection
352    slotUserUnselected();   
353    listUserBig->setFocus();
354  }
355}
356
357void LoginWidget::allowPasswordView(bool allow){
358  //Enable/disable the password view button as desired
359  allowPWVisible = allow;
360  pushViewPassword->disconnect(); //disconnect the signal/slot (keep nice appearance)
361  if(allow){
362    connect(pushViewPassword,SIGNAL(pressed()),this,SLOT(slotChangePWView()));
363    connect(pushViewPassword,SIGNAL(released()),this,SLOT(slotChangePWView()));   
364  }
365}
Note: See TracBrowser for help on using the repository browser.