27#include <QAbstractItemView>
28#include <QApplication>
29#include <QGraphicsScene>
30#include <QGraphicsView>
37#include <QSignalBlocker>
39#include <QTableWidget>
40#include <QTableWidgetItem>
53 using namespace SILICON::core;
57 constexpr int CATALOG_PREVIEW_PADDING = 8;
58 constexpr int CATALOG_CATEGORY_ROW_HEIGHT = 32;
59 constexpr int CATALOG_MINIMUM_COMPONENT_ROW_HEIGHT = 32;
60 constexpr int CATALOG_NAME_COLUMN_PADDING = 28;
63 const SILICON::project::DocumentStore& documents,
68 subcircuit->setProjectContext(&documents, resolver);
69 if (!rowData.initialProperties.empty()) {
70 for (
const auto& [key, value] : rowData.initialProperties)
71 component->applyProperty(key, value);
73 const QRectF bounds = component->sceneBoundingRect();
75 static_cast<int>(std::ceil(bounds.width())) + 2 * CATALOG_PREVIEW_PADDING,
76 static_cast<int>(std::ceil(bounds.height())) + 2 * CATALOG_PREVIEW_PADDING);
79 pixmap.fill(Qt::transparent);
81 QGraphicsScene previewScene;
82 previewScene.addItem(component.get());
84 QPainter painter(&pixmap);
85 painter.setRenderHint(QPainter::Antialiasing);
88 QRectF(QPointF(CATALOG_PREVIEW_PADDING, CATALOG_PREVIEW_PADDING), bounds.size()),
89 bounds, Qt::KeepAspectRatio);
90 previewScene.removeItem(component.get());
95 std::vector<ComponentCatalogOverlay::CatalogRow>
96 componentCatalogRows(
const bool editingSubcircuit,
97 const SILICON::project::DocumentStore& documents)
99 Q_UNUSED(editingSubcircuit);
104 std::vector<ComponentCatalogOverlay::CatalogRow> catalogRows;
105 for (
const std::string& guiType : guiFactory.availableTypes()) {
106 const auto& entryMetadata = guiFactory.metadata(guiType);
107 catalogRows.push_back(
109 .metadata = entryMetadata.graphicalMetadata
110 ? *entryMetadata.graphicalMetadata
111 : coreFactory.metadata(entryMetadata.coreType),
112 .initialProperties = {}});
122 .value_or(std::string{});
124 initialProperties.emplace(std::string(
"slug"),
PropertyValue(slug));
126 .metadata = {.displayName = slug,
127 .description =
"Project subcircuit",
128 .category = ComponentCategory::Subcircuits},
129 .initialProperties = std::move(initialProperties)});
132 std::ranges::sort(catalogRows, [](
const auto& lhs,
const auto& rhs) {
133 if (lhs.metadata.category != rhs.metadata.category)
134 return std::to_underlying(lhs.metadata.category)
135 < std::to_underlying(rhs.metadata.category);
136 return lhs.metadata.displayName < rhs.metadata.displayName;
149 setObjectName(QStringLiteral(
"componentCatalogOverlay"));
150 setAutoFillBackground(
true);
152 auto* layout =
new QVBoxLayout(
this);
153 layout->setContentsMargins(12, 12, 12, 12);
156 searchInput->setPlaceholderText(tr(
"Search components"));
159 table =
new QTableWidget(
this);
160 table->setColumnCount(3);
161 table->setHorizontalHeaderLabels(
162 {tr(
"Component shape"), tr(
"Component name"), tr(
"Component description")});
163 table->verticalHeader()->hide();
164 table->setSelectionBehavior(QAbstractItemView::SelectRows);
165 table->setSelectionMode(QAbstractItemView::SingleSelection);
166 table->setEditTriggers(QAbstractItemView::NoEditTriggers);
167 table->setShowGrid(
false);
168 table->setTextElideMode(Qt::ElideNone);
169 table->horizontalHeader()->setStretchLastSection(
true);
170 table->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
171 table->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Interactive);
173 layout->addWidget(
table);
181 for (
int row = 0; row <
table->rowCount(); ++row) {
182 const auto* name =
table->item(row, 1);
183 auto* preview = qobject_cast<QLabel*>(
table->cellWidget(row, 0));
184 if (!name || !preview)
186 const int index = name->data(Qt::UserRole + 1).toInt();
187 if (index >= 0 &&
static_cast<std::size_t
>(index) <
catalogRows.size())
189 componentPreviewPixmap(
catalogRows[index], this->documents, this->resolver));
194 callbacks.connect(
table, &QTableWidget::itemActivated, [
this](QTableWidgetItem* item) {
222 if (event->key() == Qt::Key_Escape && event->modifiers() == Qt::NoModifier) {
227 QWidget::keyPressEvent(event);
235 const auto* focus = QApplication::focusWidget();
236 const bool restoreFocus = !focus || focus ==
this || isAncestorOf(focus);
239 if (!restoreFocus || QApplication::activeModalWidget()
240 || QApplication::activePopupWidget() || !window()->isActiveWindow())
244 if (view->isVisible() && view->isAncestorOf(
this)) {
245 view->setFocus(Qt::OtherFocusReason);
253 table->clearContents();
254 table->setRowCount(0);
256 const QString query =
searchInput->text().trimmed();
257 const bool showCategories = query.isEmpty();
259 std::vector<const CatalogRow*> visibleRows;
262 if (showCategories) {
264 visibleRows.push_back(&row);
266 QStringList candidates;
267 std::vector<size_t> candidateRows;
268 candidates.reserve(
static_cast<qsizetype
>(
catalogRows.size() * 4));
271 for (
size_t rowIndex = 0; rowIndex <
catalogRows.size(); ++rowIndex) {
273 candidates.append(field);
274 candidateRows.push_back(rowIndex);
278 std::vector<bool> selected(
catalogRows.size(),
false);
279 for (
const auto& match :
281 const size_t rowIndex = candidateRows[
static_cast<size_t>(match.index)];
282 if (selected[rowIndex])
286 selected[rowIndex] =
true;
290 std::optional<ComponentRegistry::ComponentCategory> currentCategory;
293 && (!currentCategory || *currentCategory != row->metadata.category)) {
295 currentCategory = row->metadata.category;
307 const int row =
table->rowCount();
308 table->insertRow(row);
309 table->setSpan(row, 0, 1, 3);
310 table->setRowHeight(row, CATALOG_CATEGORY_ROW_HEIGHT);
312 auto* item =
new QTableWidgetItem(
314 QFont font = item->font();
317 item->setFlags(Qt::NoItemFlags);
318 table->setItem(row, 0, item);
323 const int row =
table->rowCount();
324 table->insertRow(row);
327 auto* preview =
new QLabel(
table);
328 preview->setFixedSize(previewPixmap.size());
329 preview->setAlignment(Qt::AlignCenter);
330 preview->setPixmap(previewPixmap);
331 table->setCellWidget(row, 0, preview);
333 row, std::max(CATALOG_MINIMUM_COMPONENT_ROW_HEIGHT, previewPixmap.height()));
337 name->setData(Qt::UserRole, QString::fromStdString(rowData.
guiType));
338 name->setData(Qt::UserRole + 1,
static_cast<int>(&rowData -
catalogRows.data()));
339 table->setItem(row, 1, name);
343 description->setData(Qt::UserRole, QString::fromStdString(rowData.
guiType));
344 description->setData(Qt::UserRole + 1,
346 table->setItem(row, 2, description);
354 QTableWidgetItem* item =
table->item(row, 1);
356 item =
table->item(row, 2);
360 const QString typeName = item->data(Qt::UserRole).toString();
361 if (typeName.isEmpty())
364 const int catalogIndex = item->data(Qt::UserRole + 1).toInt();
366 catalogIndex >= 0 && catalogIndex < static_cast<int>(
catalogRows.size())
367 ?
catalogRows[
static_cast<size_t>(catalogIndex)].initialProperties
370 if (
diagramScene->getInteractionMode() != DiagramScene::InteractionMode::NORMAL_MODE)
371 diagramScene->setInteractionMode(DiagramScene::InteractionMode::NORMAL_MODE);
373 diagramScene->placeComponent(typeName.toStdString(),
false, initialProperties);
379 for (
int row = 0; row <
table->rowCount(); ++row) {
380 const auto* item =
table->item(row, 1);
381 if (item && !item->data(Qt::UserRole).toString().isEmpty()) {
382 table->selectRow(row);
390 table->resizeColumnToContents(0);
392 const QFontMetrics fontMetrics(
table->font());
393 int nameColumnWidth = 0;
394 if (
const QTableWidgetItem* headerItem =
table->horizontalHeaderItem(1))
395 nameColumnWidth = fontMetrics.horizontalAdvance(headerItem->text());
397 for (
int row = 0; row <
table->rowCount(); ++row) {
398 const QTableWidgetItem* item =
table->item(row, 1);
403 std::max(nameColumnWidth, fontMetrics.horizontalAdvance(item->text()));
406 table->setColumnWidth(1, nameColumnWidth + CATALOG_NAME_COLUMN_PADDING);
412 QString::fromStdString(rowData.
guiType),
414 QString::fromStdString(
SILICON::core::ComponentCategory ComponentCategory
static ComponentRegistry & instance()
static constexpr std::string_view Type
const std::vector< Document > & getDocuments() const noexcept
ComponentCatalogOverlay(DiagramScene *scene, const SILICON::project::DocumentStore &documents, const CircuitResolver *resolver, QWidget *parent=nullptr)
static QStringList searchableFields(const CatalogRow &rowData)
void addComponentRow(const CatalogRow &rowData)
void activateRow(int row)
void keyPressEvent(QKeyEvent *event) override
void addCategoryRow(ComponentRegistry::ComponentCategory category)
const SILICON::project::DocumentStore & documents
const CircuitResolver * resolver
void selectFirstComponentRow()
std::vector< CatalogRow > catalogRows
void resizeCatalogColumns()
DiagramScene * diagramScene
Graphics scene for the circuit diagram editor.
static GUIComponentFactory & instance()
Gets the global singleton factory instance.
std::unique_ptr< GraphicalComponent > create(std::string_view type, QGraphicsItem *parent=nullptr) const
Creates a component by type name.
Graphical component wrapper for a registered subcircuit.
static ThemeEngine & instance()
std::variant< int, bool, std::string, BusValue > PropertyValue
Variant type for component properties.
std::unordered_map< std::string, PropertyValue, TransparentStringHash, TransparentStringEqual > PropertyMap
Map from property names to their values.
std::string_view componentCategoryName(const ComponentCategory category)
std::optional< std::string > documentSlugForPath(const std::string_view path)
std::vector< Match > rank(QStringList candidates, const QString &query, const bool includeNonMatches)
bool subcircuitHasGraphicalMetadata(std::string_view sceneJson)
Checks whether a scene document contains valid graphical metadata.
ComponentRegistry::ComponentMetadata metadata