From 0c59f626c7befba9c522fa58d500f946e10b471a Mon Sep 17 00:00:00 2001 From: Dean Lee Date: Thu, 8 Jul 2021 12:06:50 +0800 Subject: [PATCH] input.cc: close dialog when parent is hidden (#21318) * close dialog when parent is hidden * Q_OBJECT * apply reviews * rebase master * rebase master * fix wroing minLength * merge master old-commit-hash: 5be75f53541c83f0681e55959023bc03758ac62a --- selfdrive/ui/qt/offroad/networking.cc | 6 +++--- selfdrive/ui/qt/offroad/settings.cc | 8 -------- selfdrive/ui/qt/setup/setup.cc | 2 +- selfdrive/ui/qt/widgets/input.cc | 20 ++++++++++++++++---- selfdrive/ui/qt/widgets/input.h | 24 ++++++++++++++++-------- selfdrive/ui/qt/widgets/ssh_keys.cc | 8 ++++---- selfdrive/ui/qt/widgets/ssh_keys.h | 1 - 7 files changed, 40 insertions(+), 29 deletions(-) diff --git a/selfdrive/ui/qt/offroad/networking.cc b/selfdrive/ui/qt/offroad/networking.cc index 15035c1911..b708c44d78 100644 --- a/selfdrive/ui/qt/offroad/networking.cc +++ b/selfdrive/ui/qt/offroad/networking.cc @@ -82,7 +82,7 @@ void Networking::connectToNetwork(const Network &n) { } else if (n.security_type == SecurityType::OPEN) { wifi->connect(n); } else if (n.security_type == SecurityType::WPA) { - QString pass = InputDialog::getText("Enter password for \"" + n.ssid + "\"", 8); + QString pass = InputDialog::getText("Enter password for \"" + n.ssid + "\"", this, 8); if (!pass.isEmpty()) { wifi->connect(n, pass); } @@ -92,7 +92,7 @@ void Networking::connectToNetwork(const Network &n) { void Networking::wrongPassword(const QString &ssid) { for (Network n : wifi->seen_networks) { if (n.ssid == ssid) { - QString pass = InputDialog::getText("Wrong password for \"" + n.ssid +"\"", 8); + QString pass = InputDialog::getText("Wrong password for \"" + n.ssid +"\"", this, 8); if (!pass.isEmpty()) { wifi->connect(n, pass); } @@ -125,7 +125,7 @@ AdvancedNetworking::AdvancedNetworking(QWidget* parent, WifiManager* wifi): QWid // Change tethering password ButtonControl *editPasswordButton = new ButtonControl("Tethering Password", "EDIT"); connect(editPasswordButton, &ButtonControl::released, [=]() { - QString pass = InputDialog::getText("Enter new tethering password", 8, wifi->getTetheringPassword()); + QString pass = InputDialog::getText("Enter new tethering password", this, 8, wifi->getTetheringPassword()); if (!pass.isEmpty()) { wifi->changeTetheringPassword(pass); } diff --git a/selfdrive/ui/qt/offroad/settings.cc b/selfdrive/ui/qt/offroad/settings.cc index 6e8c7bc914..674002ea6e 100644 --- a/selfdrive/ui/qt/offroad/settings.cc +++ b/selfdrive/ui/qt/offroad/settings.cc @@ -394,12 +394,4 @@ void SettingsWindow::hideEvent(QHideEvent *event) { #ifdef QCOM HardwareEon::close_activities(); #endif - - // TODO: this should be handled by the Dialog classes - QList children = findChildren(); - for(auto &w : children) { - if(w->metaObject()->superClass()->className() == QString("QDialog")) { - w->close(); - } - } } diff --git a/selfdrive/ui/qt/setup/setup.cc b/selfdrive/ui/qt/setup/setup.cc index 7ac78b01a4..e6bbf64f0e 100644 --- a/selfdrive/ui/qt/setup/setup.cc +++ b/selfdrive/ui/qt/setup/setup.cc @@ -109,7 +109,7 @@ QWidget * Setup::software_selection() { QPushButton *custom_btn = new QPushButton("Custom"); main_layout->addWidget(custom_btn); QObject::connect(custom_btn, &QPushButton::released, this, [=]() { - QString input_url = InputDialog::getText("Enter URL"); + QString input_url = InputDialog::getText("Enter URL", this); if (input_url.size()) { this->download(input_url); } diff --git a/selfdrive/ui/qt/widgets/input.cc b/selfdrive/ui/qt/widgets/input.cc index 07bd0d31d4..7a4b849c9f 100644 --- a/selfdrive/ui/qt/widgets/input.cc +++ b/selfdrive/ui/qt/widgets/input.cc @@ -5,7 +5,19 @@ #include "selfdrive/ui/qt/qt_window.h" #include "selfdrive/hardware/hw.h" -InputDialog::InputDialog(const QString &prompt_text, QWidget *parent) : QDialog(parent) { +QDialogBase::QDialogBase(QWidget *parent) : QDialog(parent) { + Q_ASSERT(parent != nullptr); + parent->installEventFilter(this); +} + +bool QDialogBase::eventFilter(QObject *o, QEvent *e) { + if (o == parent() && e->type() == QEvent::Hide) { + reject(); + } + return QDialog::eventFilter(o, e); +} + +InputDialog::InputDialog(const QString &prompt_text, QWidget *parent) : QDialogBase(parent) { main_layout = new QVBoxLayout(this); main_layout->setContentsMargins(50, 50, 50, 50); main_layout->setSpacing(20); @@ -57,8 +69,8 @@ InputDialog::InputDialog(const QString &prompt_text, QWidget *parent) : QDialog( } -QString InputDialog::getText(const QString &prompt, int minLength, const QString &defaultText) { - InputDialog d = InputDialog(prompt); +QString InputDialog::getText(const QString &prompt, QWidget *parent, int minLength, const QString &defaultText) { + InputDialog d = InputDialog(prompt, parent); d.line->setText(defaultText); d.setMinLength(minLength); const int ret = d.exec(); @@ -114,7 +126,7 @@ void InputDialog::setMinLength(int length) { } ConfirmationDialog::ConfirmationDialog(const QString &prompt_text, const QString &confirm_text, const QString &cancel_text, - QWidget *parent):QDialog(parent) { + QWidget *parent) : QDialogBase(parent) { setWindowFlags(Qt::Popup); main_layout = new QVBoxLayout(this); main_layout->setMargin(25); diff --git a/selfdrive/ui/qt/widgets/input.h b/selfdrive/ui/qt/widgets/input.h index bf37b9181c..287727b9f9 100644 --- a/selfdrive/ui/qt/widgets/input.h +++ b/selfdrive/ui/qt/widgets/input.h @@ -9,12 +9,20 @@ #include "selfdrive/ui/qt/widgets/keyboard.h" -class InputDialog : public QDialog { +class QDialogBase : public QDialog { + Q_OBJECT + +protected: + QDialogBase(QWidget *parent); + bool eventFilter(QObject *o, QEvent *e) override; +}; + +class InputDialog : public QDialogBase { Q_OBJECT public: - explicit InputDialog(const QString &prompt_text, QWidget* parent = 0); - static QString getText(const QString &prompt, int minLength = -1, const QString &defaultText = ""); + explicit InputDialog(const QString &prompt_text, QWidget *parent); + static QString getText(const QString &prompt, QWidget *parent, int minLength = -1, const QString &defaultText = ""); QString text(); void setMessage(const QString &message, bool clearInputField = true); void setMinLength(int length); @@ -38,14 +46,14 @@ signals: void emitText(const QString &text); }; -class ConfirmationDialog : public QDialog { +class ConfirmationDialog : public QDialogBase { Q_OBJECT public: - explicit ConfirmationDialog(const QString &prompt_text, const QString &confirm_text = "Ok", - const QString &cancel_text = "Cancel", QWidget* parent = 0); - static bool alert(const QString &prompt_text, QWidget *parent = 0); - static bool confirm(const QString &prompt_text, QWidget *parent = 0); + explicit ConfirmationDialog(const QString &prompt_text, const QString &confirm_text, + const QString &cancel_text, QWidget* parent); + static bool alert(const QString &prompt_text, QWidget *parent); + static bool confirm(const QString &prompt_text, QWidget *parent); private: QLabel *prompt; diff --git a/selfdrive/ui/qt/widgets/ssh_keys.cc b/selfdrive/ui/qt/widgets/ssh_keys.cc index a56dd62adc..a9a03fbf16 100644 --- a/selfdrive/ui/qt/widgets/ssh_keys.cc +++ b/selfdrive/ui/qt/widgets/ssh_keys.cc @@ -11,7 +11,7 @@ SshControl::SshControl() : ButtonControl("SSH Keys", "", "Warning: This grants S QObject::connect(this, &ButtonControl::released, [=]() { if (text() == "ADD") { - QString username = InputDialog::getText("Enter your GitHub username"); + QString username = InputDialog::getText("Enter your GitHub username", this); if (username.length() > 0) { setText("LOADING"); setEnabled(false); @@ -46,18 +46,18 @@ void SshControl::getUserKeys(const QString &username) { params.put("GithubUsername", username.toStdString()); params.put("GithubSshKeys", resp.toStdString()); } else { - ConfirmationDialog::alert("Username '" + username + "' has no keys on GitHub"); + ConfirmationDialog::alert("Username '" + username + "' has no keys on GitHub", this); } refresh(); request->deleteLater(); }); QObject::connect(request, &HttpRequest::failedResponse, [=] { - ConfirmationDialog::alert("Username '" + username + "' doesn't exist on GitHub"); + ConfirmationDialog::alert("Username '" + username + "' doesn't exist on GitHub", this); refresh(); request->deleteLater(); }); QObject::connect(request, &HttpRequest::timeoutResponse, [=] { - ConfirmationDialog::alert("Request timed out"); + ConfirmationDialog::alert("Request timed out", this); refresh(); request->deleteLater(); }); diff --git a/selfdrive/ui/qt/widgets/ssh_keys.h b/selfdrive/ui/qt/widgets/ssh_keys.h index f670e3ae6a..0614e8c1d8 100644 --- a/selfdrive/ui/qt/widgets/ssh_keys.h +++ b/selfdrive/ui/qt/widgets/ssh_keys.h @@ -27,7 +27,6 @@ public: private: Params params; - QPushButton btn; QLabel username_label; void refresh();