Horizon
Loading...
Searching...
No Matches
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#include "util/history_manager.hpp"
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(ToolArgs &args);
76 std::optional<ToolArgs> get_pending_tool_args();
77 std::pair<bool, bool> tool_can_begin(ToolID tool_id, const std::set<SelectableRef> &selection);
78 void save();
79 void autosave();
80 virtual void delete_autosave() = 0;
81
82 void undo();
83 void redo();
84
85 bool can_undo() const;
86 bool can_redo() const;
87
88 const std::string &get_undo_comment() const;
89 const std::string &get_redo_comment() const;
90
91 void set_history_max(unsigned int m);
92 void set_history_never_forgets(bool x);
93
94 inline bool tool_is_active()
95 {
96 return tool != nullptr;
97 }
98
99 virtual bool set_property(ObjectType type, const UUID &uu, ObjectProperty::ID property,
100 const class PropertyValue &value);
101 virtual bool get_property(ObjectType type, const UUID &uu, ObjectProperty::ID property, class PropertyValue &value);
102 virtual bool get_property_meta(ObjectType type, const UUID &uu, ObjectProperty::ID property,
103 class PropertyMeta &meta);
104
105 void set_property_begin();
106 void set_property_commit();
107 bool get_property_transaction() const;
108
113 virtual json get_meta();
114
115 virtual void update_rules()
116 {
117 }
118
119 virtual std::pair<Coordi, Coordi> get_bbox() = 0;
120
121 virtual ~Core()
122 {
123 }
124 std::set<SelectableRef> &get_tool_selection();
125 std::set<InToolActionID> get_tool_actions() const;
126
127 bool get_needs_save() const;
128 void set_needs_save();
129
130 virtual const std::string &get_filename() const = 0;
131
132 typedef sigc::signal<void, ToolID> type_signal_tool_changed;
133 type_signal_tool_changed signal_tool_changed()
134 {
135 return s_signal_tool_changed;
136 }
137 typedef sigc::signal<void> type_signal_rebuilt;
138 type_signal_rebuilt signal_rebuilt()
139 {
140 return s_signal_rebuilt;
141 }
147 type_signal_rebuilt signal_save()
148 {
149 return s_signal_save;
150 }
151
152 type_signal_rebuilt signal_modified()
153 {
154 return s_signal_modified;
155 }
156
157 type_signal_rebuilt signal_can_undo_redo()
158 {
159 return s_signal_can_undo_redo;
160 }
161
162 typedef sigc::signal<void, bool> type_signal_needs_save;
163 type_signal_needs_save signal_needs_save()
164 {
165 return s_signal_needs_save;
166 }
167
168 typedef sigc::signal<json, ToolID> type_signal_load_tool_settings;
169 type_signal_load_tool_settings signal_load_tool_settings()
170 {
171 return s_signal_load_tool_settings;
172 }
173
174 typedef sigc::signal<void, ToolID, json> type_signal_save_tool_settings;
175 type_signal_save_tool_settings signal_save_tool_settings()
176 {
177 return s_signal_save_tool_settings;
178 }
179
180 virtual void reload_pool()
181 {
182 }
183
184protected:
185 Core(class IPool &pool, IPool *m_pool_caching);
186 class IPool &m_pool;
187 class IPool &m_pool_caching;
188
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
202 HistoryManager history_manager;
203
204 virtual std::unique_ptr<HistoryManager::HistoryItem> make_history_item(const std::string &comment) = 0;
205 virtual void history_load(const HistoryManager::HistoryItem &it) = 0;
206 virtual std::string get_history_comment_context() const;
207
208 bool property_transaction = false;
209
210 void layers_to_meta(class PropertyMeta &meta);
211 void get_placement(const Placement &placement, class PropertyValue &value, ObjectProperty::ID property);
212 void set_placement(Placement &placement, const class PropertyValue &value, ObjectProperty::ID property);
213
214 virtual void save(const std::string &suffix) = 0;
215 static const std::string autosave_suffix;
216 json get_meta_from_file(const std::string &filename);
217
218private:
219 std::unique_ptr<ToolBase> create_tool(ToolID tool_id);
220 std::set<SelectableRef> tool_selection;
221 void maybe_end_tool(const ToolResponse &r);
222 virtual void rebuild_internal(bool from_undo, const std::string &comment) = 0;
223
224 ToolID tool_id_current;
225 std::unique_ptr<ToolBase> tool = nullptr;
226 enum class ToolState { NONE, BEGINNING, UPDATING };
227 ToolState tool_state = ToolState::NONE;
228
229 std::list<ToolArgs> pending_tool_args;
230
231 class ToolStateSetter {
232 public:
233 ToolStateSetter(ToolState &s, ToolState target);
234 bool check_error() const
235 {
236 return error;
237 }
238 ~ToolStateSetter();
239
240 private:
241 ToolState &state;
242 bool error = false;
243 static std::string tool_state_to_string(ToolState s);
244 };
245};
246} // namespace horizon
A block is one level of hierarchy in the netlist.
Definition block.hpp:29
Where Tools and and documents meet.
Definition core.hpp:42
virtual json get_meta()
Definition core.cpp:305
void rebuild(const std::string &comment)
Expands the non-working document.
Definition core.cpp:212
type_signal_rebuilt signal_save()
Gets emitted right before saving.
Definition core.hpp:147
Definition document.hpp:5
Definition grid_settings.hpp:9
Definition ipool.hpp:15
Definition imp_interface.hpp:12
Definition core_properties.hpp:104
Definition core_properties.hpp:8
Definition rules.hpp:54
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
Tiny metaprogramming library.
Definition meta.hpp:116
@ error
throw a parse_error exception in case of a tag