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 
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  bool tool_handles_esc();
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  bool get_needs_save() const;
122  void set_needs_save();
123 
124  virtual const std::string &get_filename() const = 0;
125 
126  typedef sigc::signal<void, ToolID> type_signal_tool_changed;
127  type_signal_tool_changed signal_tool_changed()
128  {
129  return s_signal_tool_changed;
130  }
131  typedef sigc::signal<void> type_signal_rebuilt;
132  type_signal_rebuilt signal_rebuilt()
133  {
134  return s_signal_rebuilt;
135  }
141  type_signal_rebuilt signal_save()
142  {
143  return s_signal_save;
144  }
145 
146  type_signal_rebuilt signal_modified()
147  {
148  return s_signal_modified;
149  }
150 
151  type_signal_rebuilt signal_can_undo_redo()
152  {
153  return s_signal_can_undo_redo;
154  }
155 
156  typedef sigc::signal<void, bool> type_signal_needs_save;
157  type_signal_needs_save signal_needs_save()
158  {
159  return s_signal_needs_save;
160  }
161 
162  typedef sigc::signal<json, ToolID> type_signal_load_tool_settings;
163  type_signal_load_tool_settings signal_load_tool_settings()
164  {
165  return s_signal_load_tool_settings;
166  }
167 
168  typedef sigc::signal<void, ToolID, json> type_signal_save_tool_settings;
169  type_signal_save_tool_settings signal_save_tool_settings()
170  {
171  return s_signal_save_tool_settings;
172  }
173 
174  virtual void reload_pool()
175  {
176  }
177 
178 protected:
179  std::unique_ptr<ToolBase> tool = nullptr;
180  type_signal_tool_changed s_signal_tool_changed;
181  type_signal_rebuilt s_signal_rebuilt;
182  type_signal_rebuilt s_signal_save;
183  type_signal_rebuilt s_signal_modified;
184  type_signal_rebuilt s_signal_can_undo_redo;
185  type_signal_needs_save s_signal_needs_save;
186  type_signal_load_tool_settings s_signal_load_tool_settings;
187  type_signal_save_tool_settings s_signal_save_tool_settings;
188  bool needs_save = false;
189  void set_needs_save(bool v);
190 
191  class HistoryItem {
192  public:
193  // Symbol sym;
194  // HistoryItem(const Symbol &s): sym(s) {}
195  std::string comment;
196  virtual ~HistoryItem()
197  {
198  }
199  };
200  std::deque<std::unique_ptr<HistoryItem>> history;
201  int history_current = -1;
202  virtual void history_push() = 0;
203  virtual void history_load(unsigned int i) = 0;
204  void history_clear();
205  void history_trim();
206 
207  bool property_transaction = false;
208 
209  void layers_to_meta(class PropertyMeta &meta);
210  void get_placement(const Placement &placement, class PropertyValue &value, ObjectProperty::ID property);
211  void set_placement(Placement &placement, const class PropertyValue &value, ObjectProperty::ID property);
212 
213  virtual void save(const std::string &suffix) = 0;
214  static const std::string autosave_suffix;
215  json get_meta_from_file(const std::string &filename);
216 
217 private:
218  std::unique_ptr<ToolBase> create_tool(ToolID tool_id);
219  std::set<SelectableRef> tool_selection;
220  void maybe_end_tool(const ToolResponse &r);
221 };
222 } // 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:7
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:77
horizon::Document
Definition: document.hpp:5
nlohmann::basic_json
a class to store JSON values
Definition: json.hpp:165
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:141
horizon::ToolResponse
To signal back to the core what the Tool did, a Tool returns a ToolResponse.
Definition: tool.hpp:48
horizon::ToolArgs
This is what a Tool receives when the user did something.
Definition: tool.hpp:22
horizon::Placement
Definition: placement.hpp:8
horizon::Core::HistoryItem
Definition: core.hpp:191
horizon::PropertyValue
Definition: core_properties.hpp:7