HAL  v4.5.0-83-g30c8f0afc
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
dot_viewer_factory.cpp
Go to the documentation of this file.
4 
5 #include "gui/gui_globals.h"
6 
7 #include <QJsonArray>
8 #include <QJsonDocument>
9 #include <QJsonObject>
10 #include <QDir>
11 #include <QFile>
12 #include <QMap>
13 
14 namespace hal {
15 
17  : ContentFactory(name), ProjectSerializer(name.toStdString())
18  {;}
19 
21  {
22  DotViewer* dv = new DotViewer(name());
23  if (!mFilename.isEmpty()) dv->loadDotFile(mFilename, mCreator);
24  return dv;
25  }
26 
28  {
30  std::string relname = pm->get_filename(m_name);
31  if (!relname.empty())
33  }
34 
35  std::string DotViewerFactory::serialize(Netlist* netlist, const std::filesystem::path& savedir, bool isAutosave)
36  {
37  Q_UNUSED(netlist);
38  Q_UNUSED(isAutosave);
39  QString dvFilename("dot_viewer.json");
40  QFile dvFile(QDir(QString::fromStdString(savedir.string())).absoluteFilePath(dvFilename));
41  if (!dvFile.open(QIODevice::WriteOnly)) return std::string();
42 
43 
45  if (!dv) return std::string();
46 
47  QJsonObject dvObj;
48  std::filesystem::path relDotPath = ProjectManager::instance()->get_project_directory().get_relative_file_path(dv->filename().toStdString());
49  dvObj["filename"] = QString::fromStdString(relDotPath);
50  dvObj["creator"] = dv->creatorPlugin();
51 
52  dvFile.write(QJsonDocument(dvObj).toJson(QJsonDocument::Compact));
53 
54  return dvFilename.toStdString();
55  }
56 
57  void DotViewerFactory::deserialize(Netlist* netlist, const std::filesystem::path& loaddir)
58  {
59  Q_UNUSED(netlist);
60  std::string relname = ProjectManager::instance()->get_filename(m_name);
61  if (!relname.empty())
62  restoreDotViewer(loaddir, relname);
63  }
64 
65  void DotViewerFactory::restoreDotViewer(const std::filesystem::path& loaddir, const std::string& jsonfile)
66  {
67  QFile dvFile(QDir(QString::fromStdString(loaddir.string())).absoluteFilePath(QString::fromStdString(jsonfile)));
68  if (!dvFile.open(QIODevice::ReadOnly))
69  return;
70  QJsonDocument jsonDoc = QJsonDocument::fromJson(dvFile.readAll());
71  const QJsonObject& json = jsonDoc.object();
72 
73  if (json.contains("filename") && json["filename"].isString())
74  {
75  std::filesystem::path dotfilePath(json["filename"].toString().toStdString());
76  if (dotfilePath.is_relative())
77  dotfilePath = ProjectManager::instance()->get_project_directory() / dotfilePath;
78  mFilename = QString::fromStdString(dotfilePath.string());
79  }
80 
81  if (json.contains("creator") && json["creator"].isString())
82  mCreator = json["creator"].toString();
83 
85  if (!dv) return;
86 
87  if (!mFilename.isEmpty())
88  dv->loadDotFile(mFilename, mCreator);
89  }
90 }
QString name() const
std::string serialize(Netlist *netlist, const std::filesystem::path &savedir, bool isAutosave) override
void restoreDotViewer(const std::filesystem::path &loaddir, const std::string &jsonfile)
ExternalContentWidget * contentFactory() const
DotViewerFactory(const QString &name)
void deserialize(Netlist *netlist, const std::filesystem::path &loaddir) override
static DotViewer * getDotviewerInstance()
Definition: dot_viewer.cpp:68
bool loadDotFile(const QString &fileName, const QString &creator=QString())
Definition: dot_viewer.cpp:108
QString creatorPlugin() const
Definition: dot_viewer.h:97
QString filename() const
Definition: dot_viewer.h:90
std::filesystem::path get_relative_file_path(const std::string &filename) const
static ProjectManager * instance()
std::string get_filename(const std::string &serializer_name)
const ProjectDirectory & get_project_directory() const
Definition: defines.h:45
std::string name
QString absoluteFilePath(const QString &fileName) const const
virtual bool open(QIODevice::OpenMode mode) override
QByteArray readAll()
qint64 write(const char *data, qint64 maxSize)
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
QJsonObject object() const const
bool contains(const QString &key) const const
QString fromStdString(const std::string &str)
bool isEmpty() const const
std::string toStdString() const const