SILICON
Loading...
Searching...
No Matches
componentSearchBox.cpp
Go to the documentation of this file.
1/*
2 Copyright (c) 2026. Giulio Cocconi
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
19
20#include <stdexcept>
21#include <vector>
22
24#include <ui/shell/theme.hpp>
25
26
27namespace SILICON::ui {
28
29ComponentSearchBox::ComponentSearchBox(std::vector<std::string> list, QString title,
30 QGraphicsItem* parent)
31 : QGraphicsProxyWidget(parent)
32{
33 if (list.empty())
34 throw std::invalid_argument("ComponentSearchBox: completion list must not be empty");
35
36 this->completionList = std::move(list);
37 setFlag(QGraphicsItem::ItemIgnoresTransformations);
38
39 titleItem = new QGraphicsTextItem(this);
40 titleItem->setFont(QFont("Chango"));
42 titleItem->setPlainText(title);
43 connect(&ThemeEngine::instance(), &ThemeEngine::themeChanged, this, [this] {
45 });
46
47 const QFontMetrics fm(titleItem->font());
48 titleItem->setPos(0, -fm.ascent() * 1.5);
49
50 titleItem->setZValue(100);
51
52 const auto width = static_cast<int>(titleItem->boundingRect().width());
53 le = new QLineEdit();
54 le->setFixedHeight(30);
55 le->setFixedWidth(width);
56
57 completionModel = new QStringListModel(this);
58 completer = new QCompleter(completionModel, this);
59
60 completer->setCompletionMode(QCompleter::UnfilteredPopupCompletion);
61 completer->setCaseSensitivity(Qt::CaseInsensitive);
62 completer->setFilterMode(Qt::MatchContains);
63 completer->setMaxVisibleItems(5);
64 completer->popup()->setFixedWidth(le->width());
65 completer->popup()->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
66
67 connect(le, &QLineEdit::textChanged, this, &ComponentSearchBox::showCompleter);
68 connect(completer, qOverload<const QString&>(&QCompleter::activated), this,
70
71 le->installEventFilter(this);
72 completer->popup()->installEventFilter(this);
73 completer->setWidget(le);
74
75 this->setWidget(le);
76 this->setPos(0, 0);
78}
79
81{
84 if (completionModel->rowCount() == 0) {
85 completer->popup()->hide();
86 return;
87 }
88
89 completer->setCompletionPrefix(QString());
90 completer->popup()->setFixedWidth(le->width());
91 completer->complete(le->rect());
92}
93
95{
96 if (event->key() == Qt::Key_Escape) {
97 completer->popup()->hide();
98 emit requestCancel();
99 return;
100 }
101
102 if (event->key() == Qt::Key_Return) {
103 const auto typeName = selectedTypeName();
104 if (typeName.empty())
105 emit requestHide();
106 else
107 emit selectedComponent(typeName, scenePos());
108
109 event->accept();
110 return;
111 }
112
113 QGraphicsProxyWidget::keyPressEvent(event);
114}
115
116bool ComponentSearchBox::eventFilter(QObject* watched, QEvent* event)
117{
118 if ((watched == le || watched == completer->popup())
119 && event->type() == QEvent::KeyPress) {
120 const auto* keyEvent = static_cast<QKeyEvent*>(event);
121 if (keyEvent->key() == Qt::Key_Escape) {
122 completer->popup()->hide();
123 emit requestCancel();
124 return true;
125 }
126 }
127
128 return QGraphicsProxyWidget::eventFilter(watched, event);
129}
130
132{
133 const QString query = le->text().trimmed();
134
135 QStringList strings;
136 strings.reserve(static_cast<qsizetype>(completionList.size()));
137 for (const auto& item : completionList) {
138 strings.append(QString::fromStdString(item));
139 }
140
141 const auto ranked = SILICON::ui::componentSearchMatcher::rank(strings, query, true);
142
143 QStringList sortedStrings;
144 sortedStrings.reserve(static_cast<qsizetype>(ranked.size()));
145 for (const auto& match : ranked) {
146 sortedStrings.append(strings[match.index]);
147 }
148 completionModel->setStringList(sortedStrings);
149}
150
155
157{
158 if (completionModel->rowCount() == 0)
159 return {};
160
161 const auto* popup = completer->popup();
162 if (popup && popup->isVisible() && popup->currentIndex().isValid())
163 return popup->currentIndex().data().toString();
164
165 return completionModel->index(0, 0).data().toString();
166}
167
168std::string ComponentSearchBox::typeNameForCompletion(QString completion) const
169{
170 if (completion.isEmpty())
171 return {};
172
173 auto it = std::ranges::find_if(completionList, [&](const std::string& name) {
174 return QString::fromStdString(name).compare(completion, Qt::CaseInsensitive) == 0;
175 });
176
177 if (it == completionList.end())
178 return {};
179
180 return *it;
181}
182
184{
185 const auto typeName = typeNameForCompletion(std::move(completion));
186 if (typeName.empty()) {
187 emit requestHide();
188 return;
189 }
190
191 emit selectedComponent(typeName, scenePos());
192}
193
194} // namespace SILICON::ui
void keyPressEvent(QKeyEvent *event) override
std::string typeNameForCompletion(QString completion) const
bool eventFilter(QObject *watched, QEvent *event) override
ComponentSearchBox(std::vector< std::string > completionList={}, QString title="Insert component...", QGraphicsItem *parent=nullptr)
void selectedComponent(std::string typeName, QPointF pos)
std::vector< std::string > completionList
void acceptCompletion(QString completion)
static const QColor & getColor(theme::ColorToken token)
Definition theme.hpp:92
static ThemeEngine & instance()
Definition theme.cpp:147
std::vector< Match > rank(QStringList candidates, const QString &query, const bool includeNonMatches)