Horizon
core.hpp
1 #pragma once
2 #include "canvas/selectables.hpp"
3 #include "common/object_descr.hpp"
4 #include "nlohmann/json_fwd.hpp"
5 #include <gdk/gdkkeysyms.h>
6 #include <deque>
7 #include <memory>
8 #include <sigc++/sigc++.h>
9 #include "tool.hpp"
10 #include "document/document.hpp"
11 #include "util/placement.hpp"
12 
13 namespace horizon {
14 
15 enum class ToolID;
16 
42 class Core : public virtual Document {
43 public:
44  class Block *get_block() override
45  {
46  return nullptr;
47  }
48 
49  class Rules *get_rules() override
50  {
51  return nullptr;
52  }
53 
54  class IPool &get_pool() override
55  {
56  return m_pool;
57  }
58  class IPool &get_pool_caching() override
59  {
60  return m_pool_caching;
61  }
62  virtual ObjectType get_object_type() const = 0;
63 
68  virtual void rebuild(bool from_undo = false);
69  ToolResponse tool_begin(ToolID tool_id, const ToolArgs &args, class ImpInterface *imp, bool transient = false);
70  ToolResponse tool_update(const ToolArgs &args);
71  std::pair<bool, bool> tool_can_begin(ToolID tool_id, const std::set<SelectableRef> &selection);
72  void save();
73  void autosave();
74  virtual void delete_autosave() = 0;
75 
76  void undo();
77  void redo();
78 
79  bool can_undo() const;
80  bool can_redo() const;
81 
82  inline bool tool_is_active()
83  {
84  return tool != nullptr;
85  }
86 
87  virtual bool set_property(ObjectType type, const UUID &uu, ObjectProperty::ID property,
88  const class PropertyValue &value);
89  virtual bool get_property(ObjectType type, const UUID &uu, ObjectProperty::ID property, class PropertyValue &value);
90  virtual bool get_property_meta(ObjectType type, const UUID &uu, ObjectProperty::ID property,
91  class PropertyMeta &meta);
92 
93  void set_property_begin();
94  void set_property_commit();
95  bool get_property_transaction() const;
96 
101  virtual json get_meta();
102 
103  virtual void update_rules()
104  {
105  }
106 
107  virtual std::pair<Coordi, Coordi> get_bbox() = 0;
108 
109  virtual ~Core()
110  {
111  }
112  std::set<SelectableRef> &get_tool_selection();
113  std::set<InToolActionID> get_tool_actions() const;
114 
115  bool get_needs_save() const;
116  void set_needs_save();
117 
118  virtual const std::string &get_filename() const = 0;
119 
120  typedef sigc::signal<void, ToolID> type_signal_tool_changed;
121  type_signal_tool_changed signal_tool_changed()
122  {
123  return s_signal_tool_changed;
124  }
125  typedef sigc::signal<void> type_signal_rebuilt;
126  type_signal_rebuilt signal_rebuilt()
127  {
128  return s_signal_rebuilt;
129  }
135  type_signal_rebuilt signal_save()
136  {
137  return s_signal_save;
138  }
139 
140  type_signal_rebuilt signal_modified()
141  {
142  return s_signal_modified;
143  }
144 
145  type_signal_rebuilt signal_can_undo_redo()
146  {
147  return s_signal_can_undo_redo;
148  }
149 
150  typedef sigc::signal<void, bool> type_signal_needs_save;
151  type_signal_needs_save signal_needs_save()
152  {
153  return s_signal_needs_save;
154  }
155 
156  typedef sigc::signal<json, ToolID> type_signal_load_tool_settings;
157  type_signal_load_tool_settings signal_load_tool_settings()
158  {
159  return s_signal_load_tool_settings;
160  }
161 
162  typedef sigc::signal<void, ToolID, json> type_signal_save_tool_settings;
163  type_signal_save_tool_settings signal_save_tool_settings()
164  {
165  return s_signal_save_tool_settings;
166  }
167 
168  virtual void reload_pool()
169  {
170  }
171 
172 protected:
173  Core(class IPool &pool, IPool *m_pool_caching);
174  class IPool &m_pool;
175  class IPool &m_pool_caching;
176 
177  std::unique_ptr<ToolBase> tool = nullptr;
178  type_signal_tool_changed s_signal_tool_changed;
179  type_signal_rebuilt s_signal_rebuilt;
180  type_signal_rebuilt s_signal_save;
181  type_signal_rebuilt s_signal_modified;
182  type_signal_rebuilt s_signal_can_undo_redo;
183  type_signal_needs_save s_signal_needs_save;
184  type_signal_load_tool_settings s_signal_load_tool_settings;
185  type_signal_save_tool_settings s_signal_save_tool_settings;
186  bool needs_save = false;
187  void set_needs_save(bool v);
188 
189  class HistoryItem {
190  public:
191  // Symbol sym;
192  // HistoryItem(const Symbol &s): sym(s) {}
193  std::string comment;
194  virtual ~HistoryItem()
195  {
196  }
197  };
198  std::deque<std::unique_ptr<HistoryItem>> history;
199  int history_current = -1;
200  virtual void history_push() = 0;
201  virtual void history_load(unsigned int i) = 0;
202  void history_clear();
203  void history_trim();
204 
205  bool property_transaction = false;
206 
207  void layers_to_meta(class PropertyMeta &meta);
208  void get_placement(const Placement &placement, class PropertyValue &value, ObjectProperty::ID property);
209  void set_placement(Placement &placement, const class PropertyValue &value, ObjectProperty::ID property);
210 
211  virtual void save(const std::string &suffix) = 0;
212  static const std::string autosave_suffix;
213  json get_meta_from_file(const std::string &filename);
214 
215 private:
216  std::unique_ptr<ToolBase> create_tool(ToolID tool_id);
217  std::set<SelectableRef> tool_selection;
218  void maybe_end_tool(const ToolResponse &r);
219 };
220 } // namespace horizon
A block is one level of hierarchy in the netlist.
Definition: block.hpp:26
Definition: core.hpp:189
Where Tools and and documents meet.
Definition: core.hpp:42
virtual json get_meta()
Definition: core.cpp:227
type_signal_rebuilt signal_save()
Gets emitted right before saving.
Definition: core.hpp:135
virtual void rebuild(bool from_undo=false)
Expands the non-working document.
Definition: core.cpp:142
Definition: document.hpp:5
Definition: ipool.hpp:14
Definition: imp_interface.hpp:12
Definition: placement.hpp:8
Definition: core_properties.hpp:90
Definition: core_properties.hpp:7
Definition: rules.hpp:49
This is what a Tool receives when the user did something.
Definition: tool.hpp:23
To signal back to the core what the Tool did, a Tool returns a ToolResponse.
Definition: tool.hpp:42
This class encapsulates a UUID and allows it to be uses as a value type.
Definition: uuid.hpp:16
a class to store JSON values
Definition: json.hpp:170