From a8b551a96a3764799bf2db3c39c5a6391a2863a3 Mon Sep 17 00:00:00 2001 From: iaom <18504285112@163.com> Date: Mon, 26 Apr 2021 14:00:39 +0800 Subject: [PATCH] Add eventFilter in mainwindow for focus-out-exit. --- src/control/search-list-view.cpp | 4 +- src/custom-style.cpp | 11 +++++- src/custom-style.h | 5 ++- src/input-box.cpp | 11 ++++++ src/input-box.h | 1 + src/mainwindow.cpp | 68 ++++++++++++++++++++------------ src/mainwindow.h | 3 +- 7 files changed, 73 insertions(+), 30 deletions(-) diff --git a/src/control/search-list-view.cpp b/src/control/search-list-view.cpp index 818da9c..3f00743 100644 --- a/src/control/search-list-view.cpp +++ b/src/control/search-list-view.cpp @@ -25,8 +25,8 @@ SearchListView::SearchListView(QWidget * parent, const QStringList& list, const int& type) : QTreeView(parent) { - CustomStyle * style = new CustomStyle(GlobalSettings::getInstance()->getValue(STYLE_NAME_KEY).toString()); - this->setStyle(style); +// CustomStyle * style = new CustomStyle(GlobalSettings::getInstance()->getValue(STYLE_NAME_KEY).toString()); + this->setStyle(CustomStyle::getStyle()); this->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setSelectionBehavior(QAbstractItemView::SelectRows); diff --git a/src/custom-style.cpp b/src/custom-style.cpp index 9aa3429..4b170ce 100644 --- a/src/custom-style.cpp +++ b/src/custom-style.cpp @@ -21,10 +21,17 @@ #include "custom-style.h" -CustomStyle::CustomStyle(const QString &proxyStyleName, QObject *parent) : QProxyStyle(proxyStyleName) +static CustomStyle *customstyle_global_instance = nullptr; +CustomStyle::CustomStyle(QStyle *style) { } +CustomStyle *CustomStyle::getStyle() +{ + if (!customstyle_global_instance) + customstyle_global_instance = new CustomStyle; + return customstyle_global_instance; +} QSize CustomStyle::sizeFromContents(QStyle::ContentsType type, const QStyleOption *option, const QSize &contentsSize, const QWidget *widget) const { @@ -37,3 +44,5 @@ QSize CustomStyle::sizeFromContents(QStyle::ContentsType type, const QStyleOptio } return QProxyStyle::sizeFromContents(type, option, contentsSize, widget); } + + diff --git a/src/custom-style.h b/src/custom-style.h index 79ccad8..d6df430 100644 --- a/src/custom-style.h +++ b/src/custom-style.h @@ -28,8 +28,11 @@ class CustomStyle : public QProxyStyle { Q_OBJECT public: - explicit CustomStyle(const QString &proxyStyleName = "windows",QObject *parent = nullptr); + static CustomStyle *getStyle(); virtual QSize sizeFromContents(QStyle::ContentsType type, const QStyleOption *option, const QSize &contentsSize, const QWidget *widget = nullptr) const; +private: + explicit CustomStyle(QStyle *style = nullptr); + ~CustomStyle() override {} }; #endif // CUSTOMSTYLE_H diff --git a/src/input-box.cpp b/src/input-box.cpp index d6d751a..d566f7b 100644 --- a/src/input-box.cpp +++ b/src/input-box.cpp @@ -217,6 +217,17 @@ SearchLineEdit::SearchLineEdit() m_model->setStringList(GlobalSettings::getInstance()->getSearchRecord()); m_completer->setModel(m_model); m_completer->setCompletionMode(QCompleter::InlineCompletion); + //TODO make a popup window to show the completer. +// QListView *popView = new QListView(this); +// popView->setFocusPolicy(Qt::NoFocus); +// popView->setProperty("useCustomShadow", true); +// popView->setProperty("customShadowDarkness", 0.5); +// popView->setProperty("customShadowWidth", 20); +// popView->setProperty("customShadowRadius", QVector4D(6, 6, 6, 6)); +// popView->setProperty("customShadowMargins", QVector4D(20, 20, 20, 20)); +// popView->setAttribute(Qt::WA_TranslucentBackground); +// m_completer->setPopup(popView); +// m_completer->popup()->setStyle(CustomStyle::getStyle()); m_completer->setMaxVisibleItems(14); setCompleter(m_completer); diff --git a/src/input-box.h b/src/input-box.h index ab60339..e5f2554 100644 --- a/src/input-box.h +++ b/src/input-box.h @@ -30,6 +30,7 @@ #include #include #include +#include #include "global-settings.h" class SearchLineEdit; diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index f8706a8..b637c64 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -315,6 +315,7 @@ void MainWindow::initUi() } } }); + installEventFilter(this); } /** @@ -554,34 +555,34 @@ void MainWindow::setSearchMethod(const bool &is_index_search) * @param result * @return */ -bool MainWindow::nativeEvent(const QByteArray &eventType, void *message, long *result) -{ - Q_UNUSED(result); - if (eventType != "xcb_generic_event_t") { - return false; - } +//bool MainWindow::nativeEvent(const QByteArray &eventType, void *message, long *result) +//{ +// Q_UNUSED(result); +// if (eventType != "xcb_generic_event_t") { +// return false; +// } - xcb_generic_event_t *event = (xcb_generic_event_t*)message; +// xcb_generic_event_t *event = (xcb_generic_event_t*)message; - switch (event->response_type & ~0x80) { - case XCB_FOCUS_OUT: - if (!m_isAskDialogVisible) { - m_currentSearchAsked = false; - this->hide(); - m_askTimer->stop(); - m_researchTimer->stop(); - m_contentFrame->closeWebView(); - m_search_result_thread->requestInterruption(); - m_search_result_thread->quit(); - } -// m_seach_app_thread->stop(); - break; - default: - break; - } +// switch (event->response_type & ~0x80) { +// case XCB_FOCUS_OUT: +// if (!m_isAskDialogVisible) { +// m_currentSearchAsked = false; +// this->hide(); +// m_askTimer->stop(); +// m_researchTimer->stop(); +// m_contentFrame->closeWebView(); +// m_search_result_thread->requestInterruption(); +// m_search_result_thread->quit(); +// } +//// m_seach_app_thread->stop(); +// break; +// default: +// break; +// } - return false; -} +// return false; +//} void MainWindow::keyPressEvent(QKeyEvent *event) { @@ -595,6 +596,23 @@ void MainWindow::keyPressEvent(QKeyEvent *event) return QWidget::keyPressEvent(event); } +bool MainWindow::eventFilter(QObject *watched, QEvent *event) +{ + if (event->type() == QEvent::ActivationChange) { + if(QApplication::activeWindow() != this) { + if (!m_isAskDialogVisible) { + m_currentSearchAsked = false; + this->hide(); + m_askTimer->stop(); + m_researchTimer->stop(); + m_contentFrame->closeWebView(); + m_search_result_thread->requestInterruption(); + } + } + } + return QMainWindow::eventFilter(watched,event); +} + void MainWindow::paintEvent(QPaintEvent *event) { Q_UNUSED(event) diff --git a/src/mainwindow.h b/src/mainwindow.h index 21074a8..34e921b 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -86,7 +86,7 @@ public: private: // MainWindow quit when focus out. - bool nativeEvent(const QByteArray&, void*, long*); +// bool nativeEvent(const QByteArray&, void*, long*); QFrame * m_frame = nullptr; // Main frame QFrame * m_titleFrame = nullptr; // Title bar frame @@ -127,6 +127,7 @@ private: protected: void paintEvent(QPaintEvent *); void keyPressEvent(QKeyEvent *event); + bool eventFilter(QObject *watched, QEvent *event) override; void initUi(); Q_SIGNALS: