HAL  v4.5.0-232-g8053ac4b3
The Hardware Analyzer - a comprehensive reverse engineering and manipulation framework for gate-level netlists.
action_set_parameter_value.cpp
Go to the documentation of this file.
2 
3 #include "gui/gui_globals.h"
6 
7 namespace hal
8 {
10  {
11  }
13 
15  {
16  return new ActionSetParameterValue;
17  }
18 
19  ActionSetParameterValue::ActionSetParameterValue(const QString& parameterName, const QString& value) : mParameterName(parameterName), mValue(value)
20  {
21  }
22 
24  {
26  {
27  return false;
28  }
29 
30  // Resolve the gate.
32  if (!gate)
33  {
34  return false;
35  }
36 
37  const std::string name = mParameterName.toStdString();
38 
39  // An empty value removes the instance value, so that the gate falls back to the default of its type.
40  // This is only used to undo setting a parameter that had no instance value before.
41  if (mValue.isEmpty())
42  {
43  if (!gate->has_parameter(name))
44  {
45  return false;
46  }
47  auto* undo = new ActionSetParameterValue(mParameterName, QString::fromStdString(gate->get_parameter_value(name).get()));
48  undo->setObject(mObject);
49  mUndoAction = undo;
50  gate->delete_parameter(name);
51  }
52  else
53  {
54  // The gate may not hold an instance value yet, in that case the declaration is taken from its type
55  // and undo removes the instance value again.
57  if (decl.is_error())
58  {
59  return false;
60  }
61 
62  // Capture the current value BEFORE mutating, for the undo action:
63  const QString oldValue = gate->has_parameter(name) ? QString::fromStdString(gate->get_parameter_value(name).get()) : QString();
64 
65  if (gate->set_parameter(decl.get(), mValue.toStdString()).is_error())
66  {
67  return false;
68  }
69 
70  auto* undo = new ActionSetParameterValue(mParameterName, oldValue);
71  undo->setObject(mObject);
72  mUndoAction = undo;
73  }
74 
76  return UserAction::exec();
77  }
78 
80  {
82  }
83 
85  {
86  xmlOut.writeTextElement("name", mParameterName);
87  xmlOut.writeTextElement("value", mValue);
88  }
89 
91  {
92  while (xmlIn.readNextStartElement())
93  {
94  if (xmlIn.name() == QString("name"))
95  {
96  mParameterName = xmlIn.readElementText();
97  }
98  else if (xmlIn.name() == QString("value"))
99  {
100  mValue = xmlIn.readElementText();
101  }
102  }
103  }
104 
106  {
107  cryptoHash.addData("name", 4);
108  cryptoHash.addData(mParameterName.toUtf8());
109  cryptoHash.addData("value", 5);
110  cryptoHash.addData(mValue.toUtf8());
111  }
112 } // namespace hal
The ActionSetParameterValueFactory class registers the action with the manager.
static ActionSetParameterValueFactory * sFactory
Sets the value of an existing typed parameter on a gate.
ActionSetParameterValue(const QString &parameterName=QString(), const QString &value=QString())
void writeToXml(QXmlStreamWriter &xmlOut) const override
void addToHash(QCryptographicHash &cryptoHash) const override
void readFromXml(QXmlStreamReader &xmlIn) override
Result< std::string > get_parameter_value(const std::string &name) const
bool has_parameter(const Parameter &param) const
Result< Parameter > get_parameter_declaration(const std::string &name) const
Result< std::monostate > set_parameter(const Parameter &param, const std::string &value)
bool delete_parameter(const std::string &name)
Definition: gate.h:59
GateType * get_type() const
Definition: gate.cpp:162
Result< Parameter > get_parameter(const std::string &name) const
Definition: gate_type.cpp:767
Gate * get_gate_by_id(const u32 gate_id) const
Definition: netlist.cpp:195
void gateBooleanFunctionChanged(Gate *g) const
const T & get() const
Definition: result.h:166
bool is_error() const
Definition: result.h:153
The UserActionFactory is the abstract base class for registration.
Definition: user_action.h:225
QString tagname() const
Definition: user_action.h:242
The UserAction class is the abstract base class for user interactions.
Definition: user_action.h:57
UserAction * mUndoAction
Definition: user_action.h:186
virtual bool exec()
Definition: user_action.cpp:23
UserActionObject mObject
Definition: user_action.h:183
UserActionObjectType::ObjectType type() const
Definition: defines.h:45
Netlist * gNetlist
Definition: gui_globals.h:69
NetlistRelay * gNetlistRelay
Definition: gui_globals.h:74
std::string name
void addData(const char *data, int length)
QString fromStdString(const std::string &str)
bool isEmpty() const const
std::string toStdString() const const
QByteArray toUtf8() const const
QStringRef name() const const
QString readElementText(QXmlStreamReader::ReadElementTextBehaviour behaviour)
bool readNextStartElement()
void writeTextElement(const QString &qualifiedName, const QString &text)