tlogin.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include "tlogin.h"
  2. #include "appevent.h"
  3. #include "qlabel.h"
  4. #include "qlineedit.h"
  5. #include <QGroupBox>
  6. #include <QMessageBox>
  7. #include <QMouseEvent>
  8. #include <QRegExpValidator>
  9. #include "qpushbutton.h"
  10. #include "qstyle.h"
  11. #include "qwidget.h"
  12. #include "utils/layoutbuilder.h"
  13. #include "api/tloginapi.h"
  14. TLogin::TLogin(QWidget *parent)
  15. : QDialog(parent)
  16. {
  17. setObjectName("Login");
  18. setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
  19. setWindowTitle(tr("Login"));
  20. using namespace Layouting;
  21. info = new QLabel;
  22. info->setStyleSheet("color: white;");
  23. info->setText(tr("link failure"));
  24. // 创建登录组件
  25. usernameEdit = new QLineEdit();
  26. usernameEdit->setPlaceholderText(tr("Enter your user name"));
  27. passwordEdit = new QLineEdit();
  28. // passwordEdit->setEchoMode(QLineEdit::Password);
  29. passwordEdit->setPlaceholderText(tr("Enter your ExamNo"));
  30. // QRegExp regExp("[a-zA-Z0-9]{6,12}");
  31. // QValidator *validator = new QRegExpValidator(regExp, passwordEdit);
  32. // passwordEdit->setValidator(validator);
  33. QWidget *w = Group{Column{Row{st, tr("Login"), st},
  34. Form{tr("Username:"), usernameEdit, br, tr("ExamNo:"), passwordEdit},
  35. PushButton{bindTo(&loginPushButton),
  36. text(tr("Login")),
  37. onClicked(std::bind(&TLogin::on_login_clicked, this))}}}
  38. .emerge();
  39. QPushButton *close;
  40. auto h = Row{"", st, PushButton{bindTo(&close), "test"}};
  41. Column{h, st, Row{st, w, st}, st, info, noMargin}.attachTo(this);
  42. QString styleSheet = R"(
  43. QDialog#Login {
  44. background-image: url(:/bg/bg1.png);
  45. }
  46. QGroupBox {
  47. background-color: rgba(255, 255, 255, 128);
  48. }
  49. )";
  50. setStyleSheet(styleSheet);
  51. QPixmap closePix = style()->standardPixmap(QStyle::SP_TitleBarCloseButton);
  52. close->setIcon(closePix);
  53. close->setStyleSheet(R"__(
  54. QPushButton {
  55. border: none;
  56. background-color: #ffffff; /* 红色背景 */
  57. color: white; /* 文字颜色 */
  58. padding: 10px 10px; /* 内边距 */
  59. text-align: center; /* 文字居中 */
  60. text-decoration: none; /* 去掉下划线 */
  61. font-size: 12px; /* 字体大小 */
  62. margin: 2px 2px; /* 外边距 */
  63. border-radius: 10px; /* 圆角 */
  64. }
  65. QPushButton:hover {
  66. background-color: #d32f2f; /* 鼠标悬停时背景色 */
  67. }
  68. )__");
  69. resize({400, 400});
  70. // usernameEdit->setText("test");
  71. // passwordEdit->setText("SW1111112");
  72. loginPushButton->setDisabled(true);
  73. QObject::connect(close, &QPushButton::clicked, this, &TLogin::close);
  74. // QObject::connect(AppEvent::instance(), &AppEvent::serverLinkSignals, this, [this](bool isok) {
  75. // if (isok) {
  76. // info->setText(tr("link succeeded"));
  77. // loginPushButton->setDisabled(false);
  78. // } else {
  79. // info->setText(tr("link failure"));
  80. // loginPushButton->setDisabled(true);
  81. // }
  82. // });
  83. }
  84. void TLogin::mousePressEvent(QMouseEvent *event)
  85. {
  86. if (event->button() == Qt::LeftButton) {
  87. m_dragPosition = event->globalPos() - frameGeometry().topLeft();
  88. event->accept();
  89. }
  90. }
  91. void TLogin::mouseMoveEvent(QMouseEvent *event)
  92. {
  93. if (event->buttons() & Qt::LeftButton) {
  94. move(event->globalPos() - m_dragPosition);
  95. event->accept();
  96. }
  97. }
  98. void TLogin::on_login_clicked()
  99. {
  100. const QString userName = usernameEdit->text();
  101. const QString password = passwordEdit->text();
  102. if (userName.isEmpty() || password.isEmpty()) {
  103. QMessageBox::warning(this,
  104. tr("Login failed"),
  105. tr("User name or password error, please try again."));
  106. return;
  107. }
  108. bool ok = TC::Login(userName, password).post();
  109. if (ok && AppEvent::instance()->isLogin()) {
  110. // 登录成功,发出信号
  111. emit loginSuccessful();
  112. close();
  113. } else {
  114. // 登录失败,显示提示框
  115. QMessageBox::warning(this,
  116. tr("Login failed"),
  117. tr("User name or password error, please try again."));
  118. return;
  119. }
  120. }