From a9906e5d2a1107c27e3f946e78f8564151851965 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Thu, 21 Jul 2022 22:13:53 +0800 Subject: [PATCH] Fix validation text doesn't update on mouse click selection --- src/gui/fspathedit_p.cpp | 64 ++++++++++++++++++++++------------------ src/gui/fspathedit_p.h | 4 ++- 2 files changed, 38 insertions(+), 30 deletions(-) diff --git a/src/gui/fspathedit_p.cpp b/src/gui/fspathedit_p.cpp index d40e17045..c0488fff1 100644 --- a/src/gui/fspathedit_p.cpp +++ b/src/gui/fspathedit_p.cpp @@ -162,6 +162,8 @@ Private::FileLineEdit::FileLineEdit(QWidget *parent) m_completer->setModel(m_completerModel); setCompleter(m_completer); + + connect(this, &QLineEdit::textChanged, this, &FileLineEdit::validateText); } Private::FileLineEdit::~FileLineEdit() @@ -215,35 +217,6 @@ void Private::FileLineEdit::keyPressEvent(QKeyEvent *e) m_completerModel->setRootPath(Path(text()).data()); showCompletionPopup(); } - - const auto *validator = qobject_cast(this->validator()); - if (validator) - { - const FileSystemPathValidator::TestResult lastTestResult = validator->lastTestResult(); - const QValidator::State lastState = validator->lastValidationState(); - if (lastTestResult == FileSystemPathValidator::TestResult::OK) - { - delete m_warningAction; - m_warningAction = nullptr; - } - else - { - if (!m_warningAction) - { - m_warningAction = new QAction(this); - addAction(m_warningAction, QLineEdit::TrailingPosition); - } - } - - if (m_warningAction) - { - if (lastState == QValidator::Invalid) - m_warningAction->setIcon(style()->standardIcon(QStyle::SP_MessageBoxCritical)); - else if (lastState == QValidator::Intermediate) - m_warningAction->setIcon(style()->standardIcon(QStyle::SP_MessageBoxInformation)); - m_warningAction->setToolTip(warningText(lastTestResult)); - } - } } void Private::FileLineEdit::contextMenuEvent(QContextMenuEvent *event) @@ -266,6 +239,39 @@ void Private::FileLineEdit::showCompletionPopup() m_completer->complete(); } +void Private::FileLineEdit::validateText() +{ + const auto *validator = qobject_cast(this->validator()); + if (!validator) + return; + + const FileSystemPathValidator::TestResult lastTestResult = validator->lastTestResult(); + const QValidator::State lastState = validator->lastValidationState(); + + if (lastTestResult == FileSystemPathValidator::TestResult::OK) + { + delete m_warningAction; + m_warningAction = nullptr; + } + else + { + if (!m_warningAction) + { + m_warningAction = new QAction(this); + addAction(m_warningAction, QLineEdit::TrailingPosition); + } + } + + if (m_warningAction) + { + if (lastState == QValidator::Invalid) + m_warningAction->setIcon(style()->standardIcon(QStyle::SP_MessageBoxCritical)); + else if (lastState == QValidator::Intermediate) + m_warningAction->setIcon(style()->standardIcon(QStyle::SP_MessageBoxInformation)); + m_warningAction->setToolTip(warningText(lastTestResult)); + } +} + QString Private::FileLineEdit::warningText(const FileSystemPathValidator::TestResult result) { using TestResult = FileSystemPathValidator::TestResult; diff --git a/src/gui/fspathedit_p.h b/src/gui/fspathedit_p.h index 92f109004..deb1fde2a 100644 --- a/src/gui/fspathedit_p.h +++ b/src/gui/fspathedit_p.h @@ -130,8 +130,10 @@ namespace Private void contextMenuEvent(QContextMenuEvent *event) override; private: - static QString warningText(FileSystemPathValidator::TestResult result); void showCompletionPopup(); + void validateText(); + + static QString warningText(FileSystemPathValidator::TestResult result); QFileSystemModel *m_completerModel = nullptr; QCompleter *m_completer = nullptr;