wiRedPanda
Logic Circuit Simulator
Loading...
Searching...
No Matches
ICRegistry.h
Go to the documentation of this file.
1// Copyright 2015 - 2026, GIBIS-UNIFESP and the wiRedPanda contributors
2// SPDX-License-Identifier: GPL-3.0-or-later
3
7
8#pragma once
9
10#include <functional>
11
12#include <QFileSystemWatcher>
13#include <QMap>
14#include <QObject>
15#include <QSet>
16
17class GraphicElement;
18class IC;
19class Scene;
20
29class ICRegistry : public QObject
30{
31 Q_OBJECT
32
33public:
34 explicit ICRegistry(Scene *scene);
35
37 const QByteArray &cachedFileBytes(const QString &filePath);
38
40 void invalidate(const QString &filePath);
41
43 void watchFile(const QString &filePath);
44
46 QList<GraphicElement *> findICsByFile(const QString &fileName) const;
47
48 // --- Embedded IC blob storage ---
49
51 bool hasBlob(const QString &name) const;
53 QByteArray blob(const QString &name) const;
55 void setBlob(const QString &name, const QByteArray &data);
57 void registerBlob(const QString &name, const QByteArray &data);
59 void removeBlob(const QString &name);
61 void renameBlob(const QString &oldName, const QString &newName);
63 const QMap<QString, QByteArray> &blobMap() const { return m_blobs; }
68 QMap<QString, QByteArray> &blobMapRef() { return m_blobs; }
70 void clearBlobs();
71
73 QList<GraphicElement *> findICsByBlobName(const QString &blobName) const;
74
76 bool initEmbeddedIC(IC *ic, const QString &blobName);
77
79 QString uniqueBlobName(const QString &baseName) const;
80
81 // --- Batch operations (undo-aware) ---
82
84 IC *createEmbeddedIC(const QString &blobName, const QByteArray &fileBytes, const QString &contextDir);
85
87 int embedICsByFile(const QString &fileName, const QByteArray &fileBytes,
88 const QString &blobName);
89
91 int extractToFile(const QString &blobName, const QString &filePath);
92
93 // --- Serialization helpers ---
94
96 static QByteArray captureSnapshot(const QList<GraphicElement *> &targets);
97
99 static void rollbackElements(const QList<GraphicElement *> &elements, const QByteArray &snapshot,
100 Scene *scene);
101
102signals:
104 void definitionChanged(const QString &filePath);
105
108 void blobRenamed(const QString &oldName, const QString &newName);
109
110private slots:
112 void onFileChanged(const QString &filePath);
113
114private:
119 static constexpr int kMaxBlobNestingDepth = 16;
120
125 void makeBlobSelfContained(const QString &name, QSet<QString> &visited,
126 QMap<QString, QByteArray> &blobs, int depth = 0);
127
129 static void renameBlobReference(QByteArray &blobData, const QString &oldName, const QString &newName);
130
137 void reloadTargetsAtomically(const QList<GraphicElement *> &targets, const QByteArray &oldData,
138 const std::function<void(IC *)> &mutate);
139
140 Scene *m_scene;
141 QMap<QString, QByteArray> m_fileCache;
142 QMap<QString, QByteArray> m_blobs;
143 QFileSystemWatcher m_fileWatcher;
144};
Abstract base class for all graphical circuit elements in wiRedPanda.
void renameBlob(const QString &oldName, const QString &newName)
Renames a blob from oldName to newName, updating the cache key.
void setBlob(const QString &name, const QByteArray &data)
Stores or replaces the blob data under name and invalidates the cached definition.
ICRegistry(Scene *scene)
int embedICsByFile(const QString &fileName, const QByteArray &fileBytes, const QString &blobName)
Converts all file-backed IC elements referencing fileName to embedded ICs using blobName.
bool initEmbeddedIC(IC *ic, const QString &blobName)
Initializes an embedded IC by looking up its blob in the registry.
QByteArray blob(const QString &name) const
Returns the raw .panda bytes for the embedded IC named name.
void clearBlobs()
Removes all stored blobs.
const QMap< QString, QByteArray > & blobMap() const
Returns a const reference to the full blob map (name → .panda bytes).
Definition ICRegistry.h:63
QMap< QString, QByteArray > & blobMapRef()
Definition ICRegistry.h:68
QString uniqueBlobName(const QString &baseName) const
Returns baseName if available, or appends a numeric suffix to avoid collision.
QList< GraphicElement * > findICsByBlobName(const QString &blobName) const
Finds all embedded IC elements with blobName.
int extractToFile(const QString &blobName, const QString &filePath)
Writes the blob to disk and converts all embedded ICs with blobName to file-backed.
void invalidate(const QString &filePath)
Invalidates a cached definition (e.g., after file change).
const QByteArray & cachedFileBytes(const QString &filePath)
Returns cached file bytes, reading from disk on first access. Returns empty on failure.
void definitionChanged(const QString &filePath)
Emitted when an IC definition file changes on disk and its cached definition is invalidated.
void blobRenamed(const QString &oldName, const QString &newName)
void removeBlob(const QString &name)
Removes the blob named name from the registry.
IC * createEmbeddedIC(const QString &blobName, const QByteArray &fileBytes, const QString &contextDir)
Creates a new embedded IC from file bytes, registers the blob, and pushes an undo command.
QList< GraphicElement * > findICsByFile(const QString &fileName) const
Finds all IC elements in the scene that reference fileName.
static void rollbackElements(const QList< GraphicElement * > &elements, const QByteArray &snapshot, Scene *scene)
Restores elements from a previously captured snapshot (used for atomic rollback).
void registerBlob(const QString &name, const QByteArray &data)
Stores blob data under name without invalidating the definition cache.
void watchFile(const QString &filePath)
Registers a file for watching. Called when an IC element is added to the scene.
bool hasBlob(const QString &name) const
Returns true if a blob named name is stored in the registry.
static QByteArray captureSnapshot(const QList< GraphicElement * > &targets)
Serializes targets into a self-contained .panda byte array (used for embedding).
Graphic element representing an Integrated Circuit (sub-circuit) box.
Definition IC.h:31
Main circuit editing scene.
Definition Scene.h:56