This repository has been archived by the owner on Mar 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsearchwidget.cpp
86 lines (81 loc) · 2.58 KB
/
searchwidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "searchwidget.h"
#include <QApplication>
SearchWidget::SearchWidget(QWidget* parent)
: QWidget(parent)
{
input = new DSearchEdit(this);
result = new SearchResult(this, input);
//result->installEventFilter(this);
// installEventFilter(this);
//input->installEventFilter(this);
auto layout = new QHBoxLayout(this);
layout->addWidget(input, Qt::AlignCenter);
layout->setContentsMargins(0, 0, 0, 0);
setLayout(layout);
connect(input, &DSearchEdit::textEdited, result, &SearchResult::filter);
connect(input, &DSearchEdit::focusChanged, [=](bool b) mutable {if(!b)result->filter(""); });
//connect(input, &DSearchEdit::focusChanged, this, &SearchWidget::reset);
//connect(result, &SearchResult::widgetMoving, this, &SearchWidget::keepPopupPosition);
}
void SearchWidget::resizeEvent(QResizeEvent* e)
{
auto pos = mapToGlobal(input->rect().bottomLeft());
result->move(pos.x(), pos.y() + 5);
result->setFixedWidth(width());
QWidget::resizeEvent(e);
//result->show();
}
void SearchWidget::setModel(QAbstractItemModel* m)
{
result->setModel(m);
}
bool SearchWidget::eventFilter(QObject* o, QEvent* e)
{
// qDebug() << e;
// if (o == input && e->type() == QEvent::FocusAboutToChange) {
// input->setFocus();
// return true;
// }
if (o != result)
return QObject::eventFilter(o, e);
switch (e->type()) {
case QEvent::KeyPress: {
eatFocusOut = false;
static_cast<QObject*>(input)->event(e);
eatFocusOut = true;
if (e->isAccepted() || !result->isVisible()) {
//input lost focus, hide result
result->hide();
if (e->isAccepted())
return true;
}
return false;
}
case QEvent::KeyRelease: {
static_cast<QObject*>(input)->event(e);
break;
}
case QEvent::MouseButtonPress: {
auto source = qobject_cast<QWidget*>(o);
if (source) {
QWidget* target = QApplication::widgetAt(source->mapToGlobal(static_cast<QMouseEvent*>(e)->pos()));
if ((target && input->isAncestorOf(target)) || input == target) {
static_cast<QObject*>(target)->event(e);
return true;
}
}
if (!result->underMouse()) {
result->setVisible(false);
return true;
}
return false;
}
case QEvent::InputMethod:
case QEvent::ShortcutOverride:
QCoreApplication::sendEvent(input, e);
break;
default:
return false;
}
return false;
}