From 116a214097ad1ebe7397192749092532a91e3d88 Mon Sep 17 00:00:00 2001 From: donghualin Date: Tue, 4 Jun 2024 18:01:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=99=BB=E5=BD=95=E7=95=8C?= =?UTF-8?q?=E9=9D=A2=E7=9A=84=E8=BF=94=E5=9B=9E=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dp4-login-plugin/dp4_login_module.cpp | 11 +++-- dp4-login-plugin/dp4_login_module.h | 3 +- dp4-login-plugin/dp4loginwidget.cpp | 69 ++++++++++++++++++--------- dp4-login-plugin/dp4loginwidget.h | 9 +++- example/loginwidget.cpp | 1 + 5 files changed, 63 insertions(+), 30 deletions(-) diff --git a/dp4-login-plugin/dp4_login_module.cpp b/dp4-login-plugin/dp4_login_module.cpp index de15224..97b9226 100644 --- a/dp4-login-plugin/dp4_login_module.cpp +++ b/dp4-login-plugin/dp4_login_module.cpp @@ -17,11 +17,9 @@ Dp4LoginModule::Dp4LoginModule(QObject *parent) , m_callbackFun(nullptr) , m_callbackData(new AuthCallbackData) , m_messageCallbackFunc(nullptr) - , m_dp4Widget(nullptr) { setObjectName(QStringLiteral("Dp4LoginModule")); installTranslator(); - m_dp4Widget = new Dp4LoginWidget; // 以下部分爲測試代碼,具體如何寫請根據實際的業務來處理即可 /*m_callbackData->account = "uos"; m_callbackData->token = "1"; @@ -42,7 +40,9 @@ void Dp4LoginModule::init() QWidget *Dp4LoginModule::content() { - return m_dp4Widget; + // 此处返回窗体必须从此处创建,因为该插件是在子线程中加载,而窗体需要在主线程中显示,因此此处第一次获取的时候再返回它的窗体即可 + static Dp4LoginWidget dp4Widget; + return &dp4Widget; } void Dp4LoginModule::reset() @@ -94,9 +94,12 @@ std::string Dp4LoginModule::onMessage(const std::string &message) retDataObj["ShowAvatar"] = false; retDataObj["ShowUserName"] = false; retDataObj["ShowSwitchButton"] = false; - retDataObj["ShowLockButton"] = false; + retDataObj["ShowLockButton"] = true; retDataObj["DefaultAuthLevel"] = DefaultAuthLevel::StrongDefault; + retDataObj["showBackGroundColor"] = false; retDataObj["AuthType"] = AuthType::AT_Custom; + retDataObj["width"] = content()->width(); + retDataObj["height"] = content()->height(); retObj["Data"] = retDataObj; } else if (cmdType == "StartAuth") { diff --git a/dp4-login-plugin/dp4_login_module.h b/dp4-login-plugin/dp4_login_module.h index ceeb27f..b843bcd 100644 --- a/dp4-login-plugin/dp4_login_module.h +++ b/dp4-login-plugin/dp4_login_module.h @@ -26,7 +26,7 @@ public: void init() override; ModuleType type() const override - { return IpcAssistLoginType; } + { return CustomLoginType; } inline QString key() const override { return "dp4-login"; } QWidget *content() override; @@ -42,7 +42,6 @@ private: AuthCallbackFun m_callbackFun; AuthCallbackData *m_callbackData; MessageCallbackFun m_messageCallbackFunc; - Dp4LoginWidget *m_dp4Widget; }; } diff --git a/dp4-login-plugin/dp4loginwidget.cpp b/dp4-login-plugin/dp4loginwidget.cpp index 5de8e75..3b5dc24 100644 --- a/dp4-login-plugin/dp4loginwidget.cpp +++ b/dp4-login-plugin/dp4loginwidget.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include Dp4LoginWidget::Dp4LoginWidget(QWidget *parent) @@ -37,37 +38,59 @@ void Dp4LoginWidget::ResetPassword() void Dp4LoginWidget::initUi() { - QWidget *top = new QWidget(this); - - QVBoxLayout *layout = new QVBoxLayout(this); - layout->setContentsMargins(100,0,100,50);//left,top,right,bottom - QLabel *pic = new QLabel(top); - pic->setPixmap(QPixmap(USER_ICON)); - layout->addWidget(pic, 0, Qt::AlignCenter); - layout->addWidget(new QLabel(tr("Other User")), 0, Qt::AlignCenter); - m_username = new QLineEdit(top); + m_topWidget = new QWidget(this); + m_layout = new QVBoxLayout(m_topWidget); + m_layout->setContentsMargins(100, 0, 100, 0);//left,top,right,bottom + QPixmap pixmap(USER_ICON); + m_labelImage = new QLabel(m_topWidget); + m_labelImage->setPixmap(pixmap); + m_labelImage->setMinimumHeight(pixmap.height()); + m_layout->addWidget(m_labelImage, 0, Qt::AlignHCenter); + m_labelOtherUser = new QLabel(tr("Other User"), this); + m_layout->addWidget(m_labelOtherUser, 0, Qt::AlignCenter); + m_username = new QLineEdit(m_topWidget); m_username->setPlaceholderText(tr("Username")); m_username->setStyleSheet("background-color:royalblue;"); - layout->addWidget(m_username); - m_password = new QLineEdit(top); + m_layout->addWidget(m_username); + m_password = new QLineEdit(m_topWidget); m_password->setPlaceholderText(tr("Password")); m_password->setEchoMode(QLineEdit::PasswordEchoOnEdit); m_password->setStyleSheet("background-color:royalblue;"); - connect(m_password->addAction(QIcon::fromTheme("media-playback-start"),QLineEdit::TrailingPosition), &QAction::triggered, this, [=](){this->Logon();}); - layout->addWidget(m_password); - m_environment = new QComboBox(top); + connect(m_password->addAction(QIcon::fromTheme("media-playback-start"), QLineEdit::TrailingPosition), &QAction::triggered, this, [=](){this->Logon();}); + m_layout->addWidget(m_password); + m_environment = new QComboBox(m_topWidget); m_environment->addItem(tr("Production")); m_environment->addItem(tr("Development")); m_environment->setStyleSheet("background-color:royalblue;"); - layout->addWidget(m_environment); - QPushButton *pb = new QPushButton(tr("Forget Password"),top); - pb->setFlat(true); - connect(pb, &QPushButton::released,this,[=](){ this->ResetPassword(); }); - layout->addWidget(pb); + m_layout->addWidget(m_environment); + m_pbForgetPassword = new QPushButton(tr("Forget Password"), m_topWidget); + m_pbForgetPassword->setFlat(true); + connect(m_pbForgetPassword, &QPushButton::released, this, [this] { this->ResetPassword(); }); + m_layout->addWidget(m_pbForgetPassword); QString warningText = QString(tr("When you click the '➔' button or press 'Enter', indicates you understand and accept the following usage policy.")) + "\n\n" + QString(tr("'Unauthorized use of this Computer Terminal is a Criminal Offence under the Laws of Hong Kong.'")); - QLabel *labelText = new QLabel(top); - labelText->setWordWrap(true); - labelText->setText(warningText); - layout->addWidget(labelText); + m_labelText = new QLabel(m_topWidget); + m_labelText->setWordWrap(true); + m_labelText->setText(warningText); + m_layout->addWidget(m_labelText); + m_layout->addStretch(); + m_labelText->installEventFilter(this); + + QVBoxLayout *mainLayout = new QVBoxLayout(this); + mainLayout->addWidget(m_topWidget); +} + +bool Dp4LoginWidget::eventFilter(QObject *watchd, QEvent *event) +{ + if (watchd == m_labelText && event->type() == QEvent::Resize) { + int height = m_labelImage->height() + m_labelOtherUser->height() + + m_username->height() + m_password->height() + + m_environment->height() + m_pbForgetPassword->height() + + m_labelText->height() + m_layout->spacing() * 6 + + m_layout->contentsMargins().top() + m_layout->contentsMargins().bottom(); + + m_topWidget->setFixedHeight(height); + } + + return QWidget::eventFilter(watchd, event); } diff --git a/dp4-login-plugin/dp4loginwidget.h b/dp4-login-plugin/dp4loginwidget.h index 0076cc1..bfc60cf 100644 --- a/dp4-login-plugin/dp4loginwidget.h +++ b/dp4-login-plugin/dp4loginwidget.h @@ -9,17 +9,24 @@ class Dp4LoginWidget : public QWidget public: explicit Dp4LoginWidget(QWidget *parent = nullptr); - ~Dp4LoginWidget(); + ~Dp4LoginWidget() override; private: void Logon(); void ResetPassword(); void initUi(); + bool eventFilter(QObject *watchd, QEvent *event) override; private: + QWidget *m_topWidget; + class QLabel *m_labelImage; + QLabel *m_labelOtherUser; + QLabel *m_labelText; + class QVBoxLayout *m_layout; class QComboBox *m_environment; class QLineEdit *m_username; class QLineEdit *m_password; + class QPushButton *m_pbForgetPassword; }; #endif // TESTWIDGET_H diff --git a/example/loginwidget.cpp b/example/loginwidget.cpp index 54eba96..e581971 100644 --- a/example/loginwidget.cpp +++ b/example/loginwidget.cpp @@ -15,4 +15,5 @@ LoginWidget::LoginWidget(QWidget *parent) LoginWidget::~LoginWidget() { + m_dp4module->content()->setParent(nullptr); }