HAL
grouping_color_serializer.cpp
Go to the documentation of this file.
3 
4 #include "gui/gui_globals.h"
10 
11 #include <QJsonArray>
12 #include <QJsonDocument>
13 #include <QJsonObject>
14 #include <QDir>
15 #include <QFile>
16 #include <QMap>
17 
18 namespace hal {
19 
21  : ProjectSerializer("groupingcolor")
22  {;}
23 
25  {
27  std::string relname = pm->get_filename(m_name);
28  if (!relname.empty())
29  restoreGroupingColor(pm->get_project_directory(), relname, gtm);
30  }
31 
32  std::string GroupingColorSerializer::serialize(Netlist* netlist, const std::filesystem::path& savedir, bool isAutosave)
33  {
34  Q_UNUSED(netlist);
35  Q_UNUSED(isAutosave);
36  QString gcFilename("groupingcolor.json");
37  QFile gcFile(QDir(QString::fromStdString(savedir.string())).absoluteFilePath(gcFilename));
38  if (!gcFile.open(QIODevice::WriteOnly)) return std::string();
39 
40  QJsonObject gcObj;
41  QJsonArray gcArr;
42 
44  if (!gmw) std::string();
45  const GroupingTableModel* gtm = gmw->getModel();
46  if (!gtm) std::string();
47 
48  for (int irow=0; irow<gtm->rowCount(); irow++)
49  {
50  GroupingTableEntry gtme = gtm->groupingAt(irow);
51  QJsonObject gcEntry;
52  gcEntry["id"] = (int)gtme.id();
53  gcEntry["color"] = gtme.color().name(QColor::HexArgb);
54  gcArr.append(gcEntry);
55  }
56 
57  gcObj["grpcolors"] = gcArr;
58 
60 
61  return gcFilename.toStdString();
62  }
63 
64  void GroupingColorSerializer::deserialize(Netlist* netlist, const std::filesystem::path& loaddir)
65  {
66  Q_UNUSED(netlist);
67  std::string relname = ProjectManager::instance()->get_filename(m_name);
68  if (!relname.empty())
69  restoreGroupingColor(loaddir, relname);
70  }
71 
72  void GroupingColorSerializer::restoreGroupingColor(const std::filesystem::path& loaddir, const std::string& jsonfile, GroupingTableModel* gtm)
73  {
74  if (!gtm)
75  {
77  if (!gmw) return;
78  gtm = gmw->getModel();
79  if (!gtm) return;
80  }
81 
82  QFile gcFile(QDir(QString::fromStdString(loaddir.string())).absoluteFilePath(QString::fromStdString(jsonfile)));
83  if (!gcFile.open(QIODevice::ReadOnly))
84  return;
85  QJsonDocument jsonDoc = QJsonDocument::fromJson(gcFile.readAll());
86  const QJsonObject& json = jsonDoc.object();
87 
88  QMap<int,QColor> colorMap;
89  if (json.contains("grpcolors") && json["grpcolors"].isArray())
90  {
91  QJsonArray gcArr = json["grpcolors"].toArray();
92  int ngc = gcArr.size();
93  for (int igc = 0; igc < ngc; igc++)
94  {
95  QJsonObject gcEntry = gcArr.at(igc).toObject();
96  colorMap[gcEntry["id"].toInt()] = QColor(gcEntry["color"].toString());
97  }
98  }
99 
100  for (int irow=0; irow < gtm->rowCount(); irow++)
101  {
102  u32 id = gtm->data(gtm->index(irow,1),Qt::DisplayRole).toInt();
103  QColor color = colorMap.value(id);
104  if (color.isValid())
105  gtm->setData(gtm->index(irow,2), color, Qt::EditRole);
106  }
107  }
108 }
GroupingManagerWidget * getGroupingManagerWidget()
void restore(GroupingTableModel *gtm)
void deserialize(Netlist *netlist, const std::filesystem::path &loaddir) override
std::string serialize(Netlist *netlist, const std::filesystem::path &savedir, bool isAutosave) override
User interface for Groupings.
GroupingTableModel * getModel() const
An entry within a GroupingTableModel.
QColor color() const
u32 id() const
Table that holds information about all groupings.
bool setData(const QModelIndex &index, const QVariant &value, int role) override
QVariant data(const QModelIndex &index, int role) const override
int rowCount(const QModelIndex &parent=QModelIndex()) const override
GroupingTableEntry groupingAt(int irow) const
static ProjectManager * instance()
std::string get_filename(const std::string &serializer_name)
const ProjectDirectory & get_project_directory() const
ContentManager * gContentManager
Definition: plugin_gui.cpp:78
quint32 u32
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
bool isValid() const const
QString name() const 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
QByteArray toJson() const const
bool contains(const QString &key) const const
QJsonObject toObject() const const
const T value(const Key &key, const T &defaultValue) const const
QString fromStdString(const std::string &str)
std::string toStdString() const const
DisplayRole
int toInt(bool *ok) const const