Add eventFilter in mainwindow for focus-out-exit.

This commit is contained in:
iaom 2021-04-26 14:00:39 +08:00
parent beb5c83894
commit a8b551a96a
7 changed files with 73 additions and 30 deletions

View File

@ -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);

View File

@ -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);
}

View File

@ -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

View File

@ -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);

View File

@ -30,6 +30,7 @@
#include <QCompleter>
#include <QAbstractItemView>
#include <QVector4D>
#include <QListView>
#include "global-settings.h"
class SearchLineEdit;

View File

@ -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)

View File

@ -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: