-
Notifications
You must be signed in to change notification settings - Fork 782
/
searchedit.cpp
171 lines (141 loc) · 4.96 KB
/
searchedit.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/****************************************************************************
**
** Copyright (C) 2015-2016 Oleg Shparber
** Copyright (C) 2013-2014 Jerzy Kozera
** Contact: https://go.zealdocs.org/l/contact
**
** This file is part of Zeal.
**
** Zeal is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Zeal is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Zeal. If not, see <https://www.gnu.org/licenses/>.
**
****************************************************************************/
#include "searchedit.h"
#include <registry/searchquery.h>
#include <QCompleter>
#include <QCoreApplication>
#include <QKeyEvent>
#include <QLabel>
#include <QTreeView>
using namespace Zeal;
using namespace Zeal::WidgetUi;
SearchEdit::SearchEdit(QWidget *parent) :
QLineEdit(parent)
{
setClearButtonEnabled(true);
setPlaceholderText(tr("Search"));
m_completionLabel = new QLabel(this);
m_completionLabel->setObjectName(QStringLiteral("completer"));
m_completionLabel->setStyleSheet(QStringLiteral("QLabel#completer { color: gray; }"));
m_completionLabel->setFont(font());
connect(this, &SearchEdit::textChanged, this, &SearchEdit::showCompletions);
}
// Makes the line edit use autocompletions.
void SearchEdit::setCompletions(const QStringList &completions)
{
delete m_prefixCompleter;
m_prefixCompleter = new QCompleter(completions, this);
m_prefixCompleter->setCompletionMode(QCompleter::InlineCompletion);
m_prefixCompleter->setCaseSensitivity(Qt::CaseInsensitive);
m_prefixCompleter->setWidget(this);
}
// Clear input with consideration to docset filters
void SearchEdit::clearQuery()
{
setText(text().left(queryStart()));
}
void SearchEdit::selectQuery()
{
if (text().isEmpty())
return;
const int pos = hasSelectedText() ? selectionStart() : cursorPosition();
const int queryPos = queryStart();
const int textSize = text().size();
if (pos >= queryPos && selectionEnd() < textSize) {
setSelection(queryPos, textSize);
return;
}
selectAll();
}
bool SearchEdit::event(QEvent *event)
{
switch (event->type()) {
case QEvent::KeyPress: {
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
// Tab key cannot be overriden in keyPressEvent().
if (keyEvent->key() == Qt::Key_Tab) {
const QString completed = currentCompletion(text());
if (!completed.isEmpty()) {
setText(completed);
}
return true;
} else if (keyEvent->key() == Qt::Key_Escape) {
clearQuery();
return true;
}
break;
}
case QEvent::ShortcutOverride: {
// TODO: Should be obtained from the ActionManager.
static const QStringList focusShortcuts = {
QStringLiteral("Ctrl+K"),
QStringLiteral("Ctrl+L")
};
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
const int keyCode = keyEvent->key() | static_cast<int>(keyEvent->modifiers());
if (focusShortcuts.contains(QKeySequence(keyCode).toString())) {
selectQuery();
event->accept();
return true;
}
break;
}
default:
break;
}
return QLineEdit::event(event);
}
void SearchEdit::focusInEvent(QFocusEvent *event)
{
QLineEdit::focusInEvent(event);
// Do not change the default behaviour when focused with mouse.
if (event->reason() == Qt::MouseFocusReason)
return;
selectQuery();
}
void SearchEdit::showCompletions(const QString &newValue)
{
if (!isVisible())
return;
const int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
const int textWidth = fontMetrics().width(newValue);
if (m_prefixCompleter)
m_prefixCompleter->setCompletionPrefix(text());
const QString completed = currentCompletion(newValue).mid(newValue.size());
const QSize labelSize(fontMetrics().width(completed), size().height());
m_completionLabel->setMinimumSize(labelSize);
m_completionLabel->move(frameWidth + 2 + textWidth, 0);
m_completionLabel->setText(completed);
}
QString SearchEdit::currentCompletion(const QString &text) const
{
if (text.isEmpty() || !m_prefixCompleter)
return QString();
return m_prefixCompleter->currentCompletion();
}
int SearchEdit::queryStart() const
{
const Registry::SearchQuery currentQuery = Registry::SearchQuery::fromString(text());
// Keep the filter for the first Escape press
return currentQuery.query().isEmpty() ? 0 : currentQuery.keywordPrefixSize();
}