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