You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

widget.cpp 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include "widget.h"
  2. #include "ui_widget.h"
  3. #include <QShortcut>
  4. Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget)
  5. {
  6. ui->setupUi(this);
  7. QShortcut* quit = new QShortcut(QKeySequence("Ctrl+Q"), this);
  8. connect(quit, SIGNAL(activated()), qApp, SLOT(quit()));
  9. QShortcut* save = new QShortcut(QKeySequence("Ctrl+S"), this);
  10. connect(save, SIGNAL(activated()), this, SLOT(saveCurrent()));
  11. setFixedSize(size());
  12. connect(ui->btnAdd, SIGNAL(clicked()), this, SLOT(addUser()));
  13. connect(ui->btnRemove, SIGNAL(clicked()), this, SLOT(removeCurrent()));
  14. connect(ui->btnSave, SIGNAL(clicked()), this, SLOT(saveCurrent()));
  15. foreach(QLineEdit* line, findChildren<QLineEdit*>())
  16. connect(line, SIGNAL(returnPressed()), this, SLOT(saveCurrent()));
  17. load();
  18. }
  19. Widget::~Widget()
  20. {
  21. delete ui;
  22. }
  23. void Widget::load()
  24. {
  25. EpiUsers users = Epimafia::loadUsers();
  26. foreach(EpiUser user, users)
  27. ui->comboLogin->addItem(user.getLogin(), user.toVariant());
  28. ui->btnRemove->setEnabled(users.size() != 0);
  29. }
  30. void Widget::removeCurrent()
  31. {
  32. EpiUser usr = qvariant_cast<EpiUser>(ui->comboLogin->itemData(ui->comboLogin->currentIndex()));
  33. Epimafia::removeUser(usr);
  34. foreach(QLineEdit* line, findChildren<QLineEdit*>())
  35. line->clear();
  36. ui->comboLogin->removeItem(ui->comboLogin->currentIndex());
  37. ui->btnRemove->setEnabled(ui->comboLogin->count() != 0);
  38. }
  39. void Widget::addUser()
  40. {
  41. EpiUser usr;
  42. usr.setLogin(Epimafia::getUnknownUser());
  43. Epimafia::saveUser(usr);
  44. ui->comboLogin->addItem(usr.getLogin(), usr.toVariant());
  45. ui->comboLogin->setCurrentIndex(ui->comboLogin->count() - 1);
  46. ui->btnRemove->setEnabled(true);
  47. }
  48. void Widget::saveCurrent()
  49. {
  50. EpiUser usr = qvariant_cast<EpiUser>(ui->comboLogin->itemData(ui->comboLogin->currentIndex()));
  51. usr.setLogin(ui->lineLogin->text());
  52. usr.setPassword(ui->linePass->text());
  53. usr.setPwd_num(ui->linePwdNum->text());
  54. usr.setPwd_ppp(ui->linePwdPPP->text());
  55. usr.setPwd_socks(ui->linePwdSocks->text());
  56. ui->comboLogin->setItemData(ui->comboLogin->currentIndex(), usr.toVariant());
  57. ui->comboLogin->setItemText(ui->comboLogin->currentIndex(), ui->lineLogin->text());
  58. Epimafia::saveUser(usr);
  59. }
  60. void Widget::setUser(EpiUser user)
  61. {
  62. ui->lineLogin->setText(user.getLogin());
  63. ui->linePass->setText(user.getPassword());
  64. ui->linePwdNum->setText(user.getPwd_num());
  65. ui->linePwdPPP->setText(user.getPwd_ppp());
  66. ui->linePwdSocks->setText(user.getPwd_socks());
  67. }
  68. void Widget::on_comboLogin_currentIndexChanged(int index)
  69. {
  70. setUser(qvariant_cast<EpiUser>(ui->comboLogin->itemData(index)));
  71. }
  72. void Widget::on_btnShowPass_toggled(bool checked)
  73. {
  74. ui->linePass->setEchoMode(checked ? QLineEdit::Normal : QLineEdit::Password);
  75. }
  76. void Widget::on_pushShowNum_toggled(bool checked)
  77. {
  78. ui->linePwdNum->setEchoMode(checked ? QLineEdit::Normal : QLineEdit::Password);
  79. }
  80. void Widget::on_btnShowPPP_toggled(bool checked)
  81. {
  82. ui->linePwdPPP->setEchoMode(checked ? QLineEdit::Normal : QLineEdit::Password);
  83. }
  84. void Widget::on_btnShowSocks_toggled(bool checked)
  85. {
  86. ui->linePwdSocks->setEchoMode(checked ? QLineEdit::Normal : QLineEdit::Password);
  87. }