HAL
comment_manager.cpp
Go to the documentation of this file.
3 #include <QDir>
4 #include <QJsonArray>
5 #include <QJsonDocument>
6 #include <QJsonObject>
7 
8 namespace hal
9 {
10 
12  : QObject(parent), ProjectSerializer("comments")
13  {;}
14 
16  {
17  clear();
18  }
19 
20  std::string CommentManager::serialize(Netlist* netlist, const std::filesystem::path& savedir, bool isAutosave)
21  {
22  Q_UNUSED(netlist);
23  Q_UNUSED(isAutosave);
24  QString comFilename("comments.json");
25  QFile comFile(QDir(QString::fromStdString(savedir.string())).absoluteFilePath(comFilename));
26  if (!comFile.open(QIODevice::WriteOnly)) return std::string();
27 
28  QJsonObject comObj;
29  QJsonArray comArr;
30 
31  for (auto it = mEntries.constBegin(); it != mEntries.constEnd(); ++it)
32  {
33  const CommentEntry* comEnt = it.value();
34  Q_ASSERT(comEnt);
35  comArr.append(comEnt->toJson());
36  }
37 
38  comObj["comments"] = comArr;
39 
40  comFile.write(QJsonDocument(comObj).toJson(QJsonDocument::Compact));
41 
42  return comFilename.toStdString();
43  }
44 
45  void CommentManager::deserialize(Netlist* netlist, const std::filesystem::path& loaddir)
46  {
47  Q_UNUSED(netlist);
48  std::string relname = ProjectManager::instance()->get_filename(m_name);
49  if (!relname.empty())
50  restoreInternal(loaddir, relname);
51  }
52 
54  {
56  std::string relname = pm->get_filename(m_name);
57  if (relname.empty()) return false;
58 
59  restoreInternal(pm->get_project_directory(), relname);
60  return false;
61  }
62 
63  bool CommentManager::restoreInternal(const std::filesystem::path& loaddir, const std::string& relFilename)
64  {
65  QFile comFile(QDir(QString::fromStdString(loaddir.string())).absoluteFilePath(QString::fromStdString(relFilename)));
66  if (!comFile.open(QIODevice::ReadOnly))
67  return false;
68  QJsonParseError parseStatus;
69  QJsonDocument jsonDoc = QJsonDocument::fromJson(comFile.readAll(),&parseStatus);
70  if (parseStatus.error != QJsonParseError::NoError)
71  return false;
72  const QJsonObject& json = jsonDoc.object();
73 
74  if (json.contains("comments") && json["comments"].isArray())
75  {
76  QJsonArray comArr = json["comments"].toArray();
77  int nCom = comArr.size();
78  for (int iCom = 0; iCom < nCom; iCom++)
79  {
80  QJsonObject comElem = comArr.at(iCom).toObject();
81  CommentEntry* comEntry = new CommentEntry(comElem);
82  if (comEntry->isInvalid())
83  {
84  delete comEntry;
85  continue;
86  }
87  mEntries.insertMulti(comEntry->getNode(),comEntry);
88  }
89  }
90 
91  // for developer: dump entries
92 // for (const CommentEntry* ce : mEntries.values())
93 // {
94 // ce->dump();
95 // }
96 
97  return true;
98  }
99 
101  {
102  for (CommentEntry* ce : mEntries)
103  delete ce;
104  mEntries.clear();
105  }
106 
108  {
109  return mEntries.values(nd);
110  }
111 
112  bool CommentManager::contains(const Node& nd) const
113  {
114  return mEntries.contains(nd);
115  }
116 
118  {
119  auto list = mEntries.values(entry->getNode());
120  if(!list.contains(entry)) // in case someone created an entry outside of the CommentManager context
121  return;
122 
123  // all observer must finish handling the signal, otherwise big problems arise when the entry
124  // is deleted here. If the above signal is asynchronous, the entry cannot but used to identify
125  // the comment, but rather an id that is stored in the comment widgets and items seperately.
126  mEntries.remove(entry->getNode(), entry); // check if this only removes the entry, not the entire node
127 
129 
130  delete entry;
131  }
132 
134  {
135  // sanity check, dont add same entry twice
136  for(const auto &item : mEntries.values(entry->getNode()))
137  if(item == entry) return;
138 
139  mEntries.insertMulti(entry->getNode(), entry);
140  Q_EMIT entryAdded(entry);
141  }
142 
144  {
145  Q_EMIT entryModified(entry);
146  }
147 
148 }
The CommentEntry class encapsulated information related to a comment.
Definition: comment_entry.h:43
bool isInvalid() const
QJsonObject toJson() const
Node getNode() const
void deserialize(Netlist *netlist, const std::filesystem::path &loaddir) override
CommentManager(QObject *parent=nullptr)
void addComment(CommentEntry *entry)
std::string serialize(Netlist *netlist, const std::filesystem::path &savedir, bool isAutosave) override
void relayEntryModified(CommentEntry *entry)
void entryAboutToBeDeleted(CommentEntry *entry)
bool contains(const Node &nd) const
QList< CommentEntry * > getEntriesForNode(const Node &nd) const
void entryAdded(CommentEntry *entry)
void deleteComment(CommentEntry *entry)
void entryModified(CommentEntry *entry)
The Node class object represents a module or a gate.
Definition: gui_def.h:61
static ProjectManager * instance()
std::string get_filename(const std::string &serializer_name)
const ProjectDirectory & get_project_directory() const
QString absoluteFilePath(const QString &fileName) const const
virtual bool open(QIODevice::OpenMode mode) override
qint64 write(const char *data, qint64 maxSize)
void append(const QJsonValue &value)
QJsonValue at(int i) const const
int size() const const
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
QJsonObject object() const const
bool contains(const QString &key) const const
QJsonObject toObject() const const
Q_EMITQ_EMIT
QString fromStdString(const std::string &str)
std::string toStdString() const const