SILICON
Loading...
Searching...
No Matches
componentSearchBox.hpp
Go to the documentation of this file.
1/*
2 Copyright (c) 2025. 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#pragma once
19
20#include <ranges>
21
22#include <QAbstractItemView>
23#include <QCompleter>
24#include <QEvent>
25#include <QGraphicsProxyWidget>
26#include <QKeyEvent>
27#include <QLineEdit>
28#include <QStringListModel>
29
31
32
33namespace SILICON::ui {
34
35class ComponentSearchBox : public QGraphicsProxyWidget {
36 Q_OBJECT
37public:
38 explicit ComponentSearchBox(std::vector<std::string> completionList = {},
39 QString title = "Insert component...",
40 QGraphicsItem* parent = nullptr);
41 void showCompleter() const;
42
43 void keyPressEvent(QKeyEvent* event) override;
44 bool eventFilter(QObject* watched, QEvent* event) override;
45
46 void focus() { le->setFocus(Qt::OtherFocusReason); }
47 void clear() { le->clear(); }
48
49 void setCompletionList(std::vector<std::string> list)
50 {
51 this->completionList = std::move(list);
53 }
54
55signals:
56
59 void selectedComponent(std::string typeName, QPointF pos);
60
61private:
62 void updateCompletionModel() const;
63 [[nodiscard]] std::string selectedTypeName() const;
64 [[nodiscard]] QString activeCompletionText() const;
65 [[nodiscard]] std::string typeNameForCompletion(QString completion) const;
66 void acceptCompletion(QString completion);
67
68 QLineEdit* le;
69 QCompleter* completer;
70 QStringListModel* completionModel;
71 QGraphicsTextItem* titleItem;
72
73 std::vector<std::string> completionList;
74};
75
76} // namespace SILICON::ui
void setCompletionList(std::vector< std::string > list)
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)