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_pub.hpp"
10#include "document/document.hpp"
11#include "util/placement.hpp"
12
13namespace horizon {
14
15enum class ToolID;
16
42class Core : public virtual Document {
43public:
44 class Block *get_top_block() override
45 {
46 return nullptr;
47 }
48
49 class Rules *get_rules() override
50 {
51 return nullptr;
52 }
53
54 class GridSettings *get_grid_settings() override
55 {
56 return nullptr;
57 }
58
59 class IPool &get_pool() override
60 {
61 return m_pool;
62 }
63 class IPool &get_pool_caching() override
64 {
65 return m_pool_caching;
66 }
67 virtual ObjectType get_object_type() const = 0;
68
73 void rebuild(const std::string &comment);
74 ToolResponse tool_begin(ToolID tool_id, const ToolArgs &args, class ImpInterface *imp, bool transient = false);
75 ToolResponse tool_update(const ToolArgs &args);
76 std::pair<bool, bool> tool_can_begin(ToolID tool_id, const std::set<SelectableRef> &selection);
77 void save();
78 void autosave();
79 virtual void delete_autosave() = 0;
80
81 void undo();
82 void redo();
83
84 bool can_undo() const;
85 bool can_redo() const;
86
87 const std::string &get_undo_comment() const;
88 const std::string &get_redo_comment() const;
89
90 void set_history_max(unsigned int m);
91
92 inline bool tool_is_active()
93 {
94 return tool != nullptr;
95 }
96
97 virtual bool set_property(ObjectType type, const UUID &uu, ObjectProperty::ID property,
98 const class PropertyValue &value);
99 virtual bool get_property(ObjectType type, const UUID &uu, ObjectProperty::ID property, class PropertyValue &value);
100 virtual bool get_property_meta(ObjectType type, const UUID &uu, ObjectProperty::ID property,
101 class PropertyMeta &meta);
102
103 void set_property_begin();
104 void set_property_commit();
105 bool get_property_transaction() const;
106
111 virtual json get_meta();
112
113 virtual void update_rules()
114 {
115 }
116
117 virtual std::pair<Coordi, Coordi> get_bbox() = 0;
118
119 virtual ~Core()
120 {
121 }
122 std::set<SelectableRef> &get_tool_selection();
123 std::set<InToolActionID> get_tool_actions() const;
124
125 bool get_needs_save() const;
126 void set_needs_save();
127
128 virtual const std::string &get_filename() const = 0;
129
130 typedef sigc::signal<void, ToolID> type_signal_tool_changed;
131 type_signal_tool_changed signal_tool_changed()
132 {
133 return s_signal_tool_changed;
134 }
135 typedef sigc::signal<void> type_signal_rebuilt;
136 type_signal_rebuilt signal_rebuilt()
137 {
138 return s_signal_rebuilt;
139 }
145 type_signal_rebuilt signal_save()
146 {
147 return s_signal_save;
148 }
149
150 type_signal_rebuilt signal_modified()
151 {
152 return s_signal_modified;
153 }
154
155 type_signal_rebuilt signal_can_undo_redo()
156 {
157 return s_signal_can_undo_redo;
158 }
159
160 typedef sigc::signal<void, bool> type_signal_needs_save;
161 type_signal_needs_save signal_needs_save()
162 {
163 return s_signal_needs_save;
164 }
165
166 typedef sigc::signal<json, ToolID> type_signal_load_tool_settings;
167 type_signal_load_tool_settings signal_load_tool_settings()
168 {
169 return s_signal_load_tool_settings;
170 }
171
172 typedef sigc::signal<void, ToolID, json> type_signal_save_tool_settings;
173 type_signal_save_tool_settings signal_save_tool_settings()
174 {
175 return s_signal_save_tool_settings;
176 }
177
178 virtual void reload_pool()
179 {
180 }
181
182protected:
183 Core(class IPool &pool, IPool *m_pool_caching);
184 class IPool &m_pool;
185 class IPool &m_pool_caching;
186
187 ToolID tool_id_current;
188 std::unique_ptr<ToolBase> tool = nullptr;
189 type_signal_tool_changed s_signal_tool_changed;
190 type_signal_rebuilt s_signal_rebuilt;
191 type_signal_rebuilt s_signal_save;
192 type_signal_rebuilt s_signal_modified;
193 type_signal_rebuilt s_signal_can_undo_redo;
194 type_signal_needs_save s_signal_needs_save;
195 type_signal_load_tool_settings s_signal_load_tool_settings;
196 type_signal_save_tool_settings s_signal_save_tool_settings;
197 bool needs_save = false;
198 void set_needs_save(bool v);
199
200 void rebuild_finish(bool from_undo, const std::string &comment);
201
203 public:
204 HistoryItem(const std::string &c) : comment(c)
205 {
206 }
207 const std::string comment;
208 virtual ~HistoryItem()
209 {
210 }
211 };
212 std::deque<std::unique_ptr<HistoryItem>> history;
213 int history_current = -1;
214 size_t history_max = 50;
215 virtual void history_push(const std::string &comment) = 0;
216 virtual void history_load(unsigned int i) = 0;
217 virtual std::string get_history_comment_context() const;
218 void history_clear();
219 void history_trim();
220
221 bool property_transaction = false;
222
223 void layers_to_meta(class PropertyMeta &meta);
224 void get_placement(const Placement &placement, class PropertyValue &value, ObjectProperty::ID property);
225 void set_placement(Placement &placement, const class PropertyValue &value, ObjectProperty::ID property);
226
227 virtual void save(const std::string &suffix) = 0;
228 static const std::string autosave_suffix;
229 json get_meta_from_file(const std::string &filename);
230
231private:
232 std::unique_ptr<ToolBase> create_tool(ToolID tool_id);
233 std::set<SelectableRef> tool_selection;
234 void maybe_end_tool(const ToolResponse &r);
235 virtual void rebuild_internal(bool from_undo, const std::string &comment) = 0;
236};
237} // namespace horizon
A block is one level of hierarchy in the netlist.
Definition: block.hpp:29
Definition: core.hpp:202
Where Tools and and documents meet.
Definition: core.hpp:42
virtual json get_meta()
Definition: core.cpp:274
void rebuild(const std::string &comment)
Expands the non-working document.
Definition: core.cpp:151
type_signal_rebuilt signal_save()
Gets emitted right before saving.
Definition: core.hpp:145
Definition: document.hpp:5
Definition: grid_settings.hpp:9
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:53
This is what a Tool receives when the user did something.
Definition: tool_pub.hpp:23
To signal back to the core what the Tool did, a Tool returns a ToolResponse.
Definition: tool_pub.hpp:40
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:177