-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathllamasearchtoolbar.cpp
More file actions
125 lines (101 loc) · 3.32 KB
/
llamasearchtoolbar.cpp
File metadata and controls
125 lines (101 loc) · 3.32 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <QCloseEvent>
#include <QHBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QWidget>
#include "llamasearchtoolbar.h"
#include "llamatheme.h"
#include "llamatr.h"
namespace LlamaCpp {
SearchToolbar::SearchToolbar(QWidget *parent)
: QWidget(parent)
{
setWindowFlags(Qt::Tool | Qt::WindowCloseButtonHint | Qt::WindowStaysOnTopHint
| Qt::WindowTitleHint | Qt::CustomizeWindowHint);
setAttribute(Qt::WA_DeleteOnClose, false);
setObjectName("SearchToolbar");
setWindowTitle(Tr::tr("Search"));
auto toolbarLayout = new QHBoxLayout(this);
toolbarLayout->setContentsMargins(5, 5, 5, 5);
toolbarLayout->setSpacing(4);
m_searchEdit = new QLineEdit(this);
m_searchEdit->setPlaceholderText(Tr::tr("Find ..."));
m_searchEdit->setObjectName("SearchEdit");
m_prevSearchBtn = new QPushButton("M", this);
m_prevSearchBtn->setToolTip(Tr::tr("Previous result"));
m_nextSearchBtn = new QPushButton("N", this);
m_nextSearchBtn->setToolTip(Tr::tr("Next result"));
m_searchEdit->installEventFilter(this);
m_indexLabel = new QLabel(this);
m_indexLabel->setText("0/0");
toolbarLayout->addWidget(m_searchEdit);
toolbarLayout->addWidget(m_indexLabel);
toolbarLayout->addWidget(m_prevSearchBtn);
toolbarLayout->addWidget(m_nextSearchBtn);
hide();
connect(m_prevSearchBtn, &QPushButton::clicked, this, &SearchToolbar::onPrevSearchClicked);
connect(m_nextSearchBtn, &QPushButton::clicked, this, &SearchToolbar::onNextSearchClicked);
connect(m_searchEdit, &QLineEdit::textChanged, this, &SearchToolbar::onSearchTextChanged);
applyStyleSheet();
}
void SearchToolbar::setIndexLabel(const QString &label)
{
m_indexLabel->setText(label);
}
void SearchToolbar::closeEvent(QCloseEvent *event)
{
m_searchEdit->clear();
hide();
event->ignore();
emit onCloseEvent();
}
bool SearchToolbar::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::ShortcutOverride) {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
if (keyEvent->key() == Qt::Key_Escape) {
m_searchEdit->clear();
hide();
return true;
}
}
if (obj == this && event->type() == QEvent::FocusIn) {
m_searchEdit->setFocus();
return true;
}
return QWidget::eventFilter(obj, event);
}
void SearchToolbar::applyStyleSheet()
{
setAttribute(Qt::WA_StyledBackground, true);
setStyleSheet(replaceThemeColorNamesWithRGBNames(R"(
QWidget#SearchEdit {
background-color: Token_Background_Muted;
border: 1px solid Token_Foreground_Muted;
border-radius: 8px;
padding: 4px;
}
QWidget#SearchEdit:hover {
border: 1px solid Token_Foreground_Default;
}
QTextEdit {
background-color: Token_Background_Muted;
border: 8px
}
QLabel {
color: Token_Text_Muted;
}
QPushButton {
border: 1px solid Token_Foreground_Muted;
font-family: heroicons_outline;
font-size: 14px;
border-radius: 6px;
padding: 6px 2px;
}
QPushButton:hover {
background-color: Token_Foreground_Muted;
}
)"));
}
} // namespace LlamaCpp