Skip to content

Commit

Permalink
Moved module color management to separate class
Browse files Browse the repository at this point in the history
  • Loading branch information
joern274 committed Nov 7, 2023
1 parent f99d000 commit 52edfec
Show file tree
Hide file tree
Showing 14 changed files with 315 additions and 212 deletions.
15 changes: 0 additions & 15 deletions plugins/gui/include/gui/grouping/grouping_color_serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

namespace hal {
class GroupingTableModel;
class ModuleModel;

class GroupingColorSerializer : public ProjectSerializer
{
Expand All @@ -44,18 +43,4 @@ namespace hal {

void restore(GroupingTableModel *gtm);
};

class ModuleColorSerializer : public ProjectSerializer
{
void restoreModuleColor(const std::filesystem::path& loaddir, const std::string& jsonfile, ModuleModel *mm = nullptr);
void serializeColorRecursion(QJsonArray& mcArr, const ModuleModel* mm, QModelIndex parent=QModelIndex());
public:
ModuleColorSerializer();

std::string serialize(Netlist* netlist, const std::filesystem::path& savedir, bool isAutosave) override;

void deserialize(Netlist* netlist, const std::filesystem::path& loaddir) override;

void restore(ModuleModel *mm);
};
}
9 changes: 0 additions & 9 deletions plugins/gui/include/gui/gui_utils/graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,5 @@ namespace hal
* @return The (perhabs styled) icon.
*/
extern QIcon getStyledSvgIcon(const QString& from_to_colors, const QString& svg_path);

/**
* Returns a somewhat random color through a funny method (should be the same order
* of colors each time the program starts). This brilliant piece of code MUST NEVER
* BE REMOVED, PURE COMEDY!
*
* @return The "random" color.
*/
extern QColor getRandomColor();
}
}
125 changes: 125 additions & 0 deletions plugins/gui/include/gui/module_model/module_color_manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// MIT License
//
// Copyright (c) 2019 Ruhr University Bochum, Chair for Embedded Security. All Rights reserved.
// Copyright (c) 2019 Marc Fyrbiak, Sebastian Wallat, Max Hoffmann ("ORIGINAL AUTHORS"). All rights reserved.
// Copyright (c) 2021 Max Planck Institute for Security and Privacy. All Rights reserved.
// Copyright (c) 2021 Jörn Langheinrich, Julian Speith, Nils Albartus, René Walendy, Simon Klix ("ORIGINAL AUTHORS"). All Rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#pragma once

#include "hal_core/defines.h"
#include "hal_core/netlist/project_serializer.h"
#include <QColor>
#include <QObject>
#include <QMap>

namespace hal
{
/**
* Manages the module colors which will be assigned either by an "random" algorithm or manually by user.
* Quotes around "random" indicate it is not random at all, it is some homemade spinning around in HSV color circle.
*/
class ModuleColorManager : public QObject
{
Q_OBJECT

Q_SIGNALS:
/**
* Q_SIGNAL to notify that the color of a module has been changed.
*
* @param id - Id of the module with the changed color
*/
void moduleColorChanged(u32 id) const;

public:
ModuleColorManager(QObject* parent = nullptr);

/**
* Gets the module color of a module of a specific id.
*
* @param id - The module id of the module to get the color for
* @returns the color of the module
*/
QColor moduleColor(u32 id) const;

/**
* Changes the color of a module.
*
* @param id - The id of the module
* @param col - The new color
* @returns the old color of the module (used to create an undo action easier)
*/
QColor setModuleColor(u32 id, const QColor& col);

/**
* Changes the color of a module to a random color.
*
* @param id - The id of the module
* @returns the old color of the module (used to create an undo action easier)
*/
QColor setRandomColor(u32 id);

/**
* Removes the color that belongs to the given id.
*
* @param id - The module id for which to remove the color.
*/
void removeColor(u32 id);

/**
* Getter for map of all module colores
*
* @returns The map of all module colores
*/
QMap<u32, QColor> getColorMap() const;

void clear();
private:


/**
* Returns a somewhat random color through a funny method (should be the same order
* of colors each time the program starts).
*
* @return The "random" color.
*/
QColor getRandomColor();

QMap<u32, QColor> mModuleColors;
};

/**
* Persist module color settings to file and restore module color assignment from previous session
*/
class ModuleColorSerializer : public ProjectSerializer
{
void restoreModuleColor(const std::filesystem::path& loaddir, const std::string& jsonfile, ModuleColorManager *mcm = nullptr);
void serializeColorMap(QJsonArray& mcArr, const ModuleColorManager* mcm);
public:
ModuleColorSerializer();

std::string serialize(Netlist* netlist, const std::filesystem::path& savedir, bool isAutosave) override;

void deserialize(Netlist* netlist, const std::filesystem::path& loaddir) override;

void restore(ModuleColorManager *mcm);
};
}
42 changes: 0 additions & 42 deletions plugins/gui/include/gui/module_model/module_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
#include <QAbstractItemModel>
#include <QModelIndex>
#include <QVariant>
#include <QColor>
#include <set>
#include <array>

Expand All @@ -51,14 +50,6 @@ namespace hal
{
Q_OBJECT

Q_SIGNALS:
/**
* Q_SIGNAL to notify that the color of a module has been changed.
*
* @param id - Id of the module with the changed color
*/
void moduleColorChanged(u32 id) const;

public:
/**
* Constructor. <br>
Expand Down Expand Up @@ -258,46 +249,13 @@ namespace hal
*/
bool isModifying();

/**
* Gets the module color of a module of a specific id.
*
* @param id - The module id of the module to get the color for
* @returns the color of the module
*/
QColor moduleColor(u32 id) const;

/**
* Changes the color of a module.
*
* @param id - The id of the module
* @param col - The new color
* @returns the old color of the module (used to create an undo action easier)
*/
QColor setModuleColor(u32 id, const QColor& col);

/**
* Changes the color of a module to a random color.
*
* @param id - The id of the module
* @returns the old color of the module (used to create an undo action easier)
*/
QColor setRandomColor(u32 id);

/**
* Removes the color that belongs to the given id.
*
* @param id - The module id for which to remove the color.
*/
void removeColor(u32 id);

private:
ModuleItem* mTopModuleItem;

QMap<u32, ModuleItem*> mModuleMap;
QMap<u32, ModuleItem*> mGateMap;
QMap<u32, ModuleItem*> mNetMap;
std::array<QMap<u32, ModuleItem*>*, 3> mModuleItemMaps = {&mModuleMap, &mGateMap, &mNetMap};;
QMap<u32, QColor> mModuleColors;

bool mIsModifying;
};
Expand Down
13 changes: 12 additions & 1 deletion plugins/gui/include/gui/netlist_relay/netlist_relay.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,16 @@

#include "hal_core/netlist/event_system/event_handler.h"
#include "gui/grouping/grouping_color_serializer.h"
#include "gui/module_model/module_color_manager.h"
#include <QMap>
#include <QObject>

namespace hal
{
class ModuleItem;
class ModuleModel;
class ModuleColorManager;
class ModuleColorSerializer;
class Module;

/**
Expand Down Expand Up @@ -88,7 +91,14 @@ namespace hal
*
* @returns the module model
*/
ModuleModel* getModuleModel();
ModuleModel* getModuleModel() const;

/**
* Accesses the module color manager
*
* @returns the module color manager
*/
ModuleColorManager* getModuleColorManager() const;

/**
* Changes the name of a specific module by asking the user for a new name in a 'Rename'-dialogue.
Expand Down Expand Up @@ -610,6 +620,7 @@ namespace hal

QMap<u32, QColor> mModuleColors;
ModuleModel* mModuleModel;
ModuleColorManager* mModuleColorManager;
ModuleColorSerializer mColorSerializer;
enum ThreadEventType { TetNetlist, TetModule, TetGate, TetNet, TetGrouping };
};
Expand Down
91 changes: 1 addition & 90 deletions plugins/gui/src/grouping/grouping_color_serializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "gui/grouping/grouping_table_model.h"
#include "gui/module_model/module_model.h"
#include "gui/module_model/module_item.h"
#include "gui/module_model/module_color_manager.h"

#include <QJsonArray>
#include <QJsonDocument>
Expand Down Expand Up @@ -104,94 +105,4 @@ namespace hal {
gtm->setData(gtm->index(irow,2), color, Qt::EditRole);
}
}

//---------------------------------------
ModuleColorSerializer::ModuleColorSerializer()
: ProjectSerializer("modulecolor")
{;}

void ModuleColorSerializer::restore(ModuleModel* mm)
{
ProjectManager* pm = ProjectManager::instance();
std::string relname = pm->get_filename(m_name);
if (!relname.empty())
restoreModuleColor(pm->get_project_directory(), relname, mm);
}

void ModuleColorSerializer::serializeColorRecursion(QJsonArray& mcArr, const ModuleModel* mm, QModelIndex parent)
{
int nrows = mm->rowCount(parent);
for (int irow=0; irow<nrows; irow++)
{
QModelIndex inx = mm->index(irow,0,parent);
const ModuleItem* mItem = mm->getItem(inx);
if (!mItem) continue;
if(mItem->getType() != ModuleItem::TreeItemType::Module) continue;

QJsonObject mcEntry;
mcEntry["id"] = (int) mItem->id();
mcEntry["color"] = mm->moduleColor(mItem->id()).name(QColor::HexArgb);
mcArr.append(mcEntry);
serializeColorRecursion(mcArr,mm,inx);
}
}

std::string ModuleColorSerializer::serialize(Netlist* netlist, const std::filesystem::path& savedir, bool isAutosave)
{
Q_UNUSED(netlist);
Q_UNUSED(isAutosave);
QString mcFilename("modulecolor.json");
QFile mcFile(QDir(QString::fromStdString(savedir.string())).absoluteFilePath(mcFilename));
if (!mcFile.open(QIODevice::WriteOnly)) return std::string();

QJsonObject mcObj;
QJsonArray mcArr;

const ModuleModel* mm = gNetlistRelay->getModuleModel();
if (!mm) return std::string();

serializeColorRecursion(mcArr,mm);

mcObj["modcolors"] = mcArr;

mcFile.write(QJsonDocument(mcObj).toJson(QJsonDocument::Compact));

return mcFilename.toStdString();
}


void ModuleColorSerializer::deserialize(Netlist* netlist, const std::filesystem::path& loaddir)
{
Q_UNUSED(netlist);
std::string relname = ProjectManager::instance()->get_filename(m_name);
if (!relname.empty())
restoreModuleColor(loaddir, relname);
}

void ModuleColorSerializer::restoreModuleColor(const std::filesystem::path& loaddir, const std::string& jsonfile, ModuleModel* mm)
{
if (!mm)
{
mm = gNetlistRelay->getModuleModel();
if (!mm) return;
}

QFile mcFile(QDir(QString::fromStdString(loaddir.string())).absoluteFilePath(QString::fromStdString(jsonfile)));
if (!mcFile.open(QIODevice::ReadOnly))
return;
QJsonDocument jsonDoc = QJsonDocument::fromJson(mcFile.readAll());
const QJsonObject& json = jsonDoc.object();

if (json.contains("modcolors") && json["modcolors"].isArray())
{
QJsonArray mcArr = json["modcolors"].toArray();
int nmc = mcArr.size();
for (int imc = 0; imc < nmc; imc++)
{
QJsonObject mcEntry = mcArr.at(imc).toObject();
u32 moduleId = mcEntry["id"].toInt();
mm->setModuleColor(moduleId,QColor(mcEntry["color"].toString()));
}
}
}
}
Loading

0 comments on commit 52edfec

Please sign in to comment.