SILICON
Toggle main menu visibility
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
18
#include "
componentSearchBox.hpp
"
19
20
#include <stdexcept>
21
#include <vector>
22
23
#include <
ui/circuit/editor/componentSearchMatcher.hpp
>
24
#include <
ui/shell/theme.hpp
>
25
26
27
namespace
SILICON::ui
{
28
29
ComponentSearchBox::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"
));
41
titleItem
->setDefaultTextColor(
ThemeEngine::getColor
(
theme::ColorToken::Ink
));
42
titleItem
->setPlainText(title);
43
connect(&
ThemeEngine::instance
(), &
ThemeEngine::themeChanged
,
this
, [
this
] {
44
titleItem
->setDefaultTextColor(
ThemeEngine::getColor
(
theme::ColorToken::Ink
));
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
,
69
&
ComponentSearchBox::acceptCompletion
);
70
71
le
->installEventFilter(
this
);
72
completer
->popup()->installEventFilter(
this
);
73
completer
->setWidget(
le
);
74
75
this->setWidget(
le
);
76
this->setPos(0, 0);
77
updateCompletionModel
();
78
}
79
80
void
ComponentSearchBox::showCompleter
()
const
81
{
82
titleItem
->setDefaultTextColor(
ThemeEngine::getColor
(
theme::ColorToken::Ink
));
83
updateCompletionModel
();
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
94
void
ComponentSearchBox::keyPressEvent
(QKeyEvent* event)
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
116
bool
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
131
void
ComponentSearchBox::updateCompletionModel
()
const
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
151
std::string
ComponentSearchBox::selectedTypeName
()
const
152
{
153
return
typeNameForCompletion
(
activeCompletionText
());
154
}
155
156
QString
ComponentSearchBox::activeCompletionText
()
const
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
168
std::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
183
void
ComponentSearchBox::acceptCompletion
(QString completion)
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
SILICON::ui::ComponentSearchBox::requestHide
void requestHide()
SILICON::ui::ComponentSearchBox::activeCompletionText
QString activeCompletionText() const
Definition
componentSearchBox.cpp:156
SILICON::ui::ComponentSearchBox::keyPressEvent
void keyPressEvent(QKeyEvent *event) override
Definition
componentSearchBox.cpp:94
SILICON::ui::ComponentSearchBox::typeNameForCompletion
std::string typeNameForCompletion(QString completion) const
Definition
componentSearchBox.cpp:168
SILICON::ui::ComponentSearchBox::eventFilter
bool eventFilter(QObject *watched, QEvent *event) override
Definition
componentSearchBox.cpp:116
SILICON::ui::ComponentSearchBox::ComponentSearchBox
ComponentSearchBox(std::vector< std::string > completionList={}, QString title="Insert component...", QGraphicsItem *parent=nullptr)
Definition
componentSearchBox.cpp:29
SILICON::ui::ComponentSearchBox::le
QLineEdit * le
Definition
componentSearchBox.hpp:68
SILICON::ui::ComponentSearchBox::selectedComponent
void selectedComponent(std::string typeName, QPointF pos)
SILICON::ui::ComponentSearchBox::updateCompletionModel
void updateCompletionModel() const
Definition
componentSearchBox.cpp:131
SILICON::ui::ComponentSearchBox::selectedTypeName
std::string selectedTypeName() const
Definition
componentSearchBox.cpp:151
SILICON::ui::ComponentSearchBox::completionModel
QStringListModel * completionModel
Definition
componentSearchBox.hpp:70
SILICON::ui::ComponentSearchBox::showCompleter
void showCompleter() const
Definition
componentSearchBox.cpp:80
SILICON::ui::ComponentSearchBox::completer
QCompleter * completer
Definition
componentSearchBox.hpp:69
SILICON::ui::ComponentSearchBox::titleItem
QGraphicsTextItem * titleItem
Definition
componentSearchBox.hpp:71
SILICON::ui::ComponentSearchBox::requestCancel
void requestCancel()
SILICON::ui::ComponentSearchBox::completionList
std::vector< std::string > completionList
Definition
componentSearchBox.hpp:73
SILICON::ui::ComponentSearchBox::acceptCompletion
void acceptCompletion(QString completion)
Definition
componentSearchBox.cpp:183
SILICON::ui::ThemeEngine::getColor
static const QColor & getColor(theme::ColorToken token)
Definition
theme.hpp:92
SILICON::ui::ThemeEngine::instance
static ThemeEngine & instance()
Definition
theme.cpp:147
SILICON::ui::ThemeEngine::themeChanged
void themeChanged()
componentSearchBox.hpp
componentSearchMatcher.hpp
SILICON::ui::componentSearchMatcher::rank
std::vector< Match > rank(QStringList candidates, const QString &query, const bool includeNonMatches)
Definition
componentSearchMatcher.cpp:71
SILICON::ui::theme::ColorToken::Ink
@ Ink
Definition
theme.hpp:40
SILICON::ui
Definition
actionGroups.hpp:14
theme.hpp
src
ui
circuit
editor
componentSearchBox.cpp
Generated by
1.17.0