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
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
93 inline bool tool_is_active()
94 {
95 return tool != nullptr;
96 }
97
98 virtual bool set_property(ObjectType type, const UUID &uu, ObjectProperty::ID property,
99 const class PropertyValue &value);
100 virtual bool get_property(ObjectType type, const UUID &uu, ObjectProperty::ID property, class PropertyValue &value);
101 virtual bool get_property_meta(ObjectType type, const UUID &uu, ObjectProperty::ID property,
102 class PropertyMeta &meta);
103
104 void set_property_begin();
105 void set_property_commit();
106 bool get_property_transaction() const;
107
112 virtual json get_meta();
113
114 virtual void update_rules()
115 {
116 }
117
118 virtual std::pair<Coordi, Coordi> get_bbox() = 0;
119
120 virtual ~Core()
121 {
122 }
123 std::set<SelectableRef> &get_tool_selection();
124 std::set<InToolActionID> get_tool_actions() const;
125
126 bool get_needs_save() const;
127 void set_needs_save();
128
129 virtual const std::string &get_filename() const = 0;
130
131 typedef sigc::signal<void, ToolID> type_signal_tool_changed;
132 type_signal_tool_changed signal_tool_changed()
133 {
134 return s_signal_tool_changed;
135 }
136 typedef sigc::signal<void> type_signal_rebuilt;
137 type_signal_rebuilt signal_rebuilt()
138 {
139 return s_signal_rebuilt;
140 }
146 type_signal_rebuilt signal_save()
147 {
148 return s_signal_save;
149 }
150
151 type_signal_rebuilt signal_modified()
152 {
153 return s_signal_modified;
154 }
155
156 type_signal_rebuilt signal_can_undo_redo()
157 {
158 return s_signal_can_undo_redo;
159 }
160
161 typedef sigc::signal<void, bool> type_signal_needs_save;
162 type_signal_needs_save signal_needs_save()
163 {
164 return s_signal_needs_save;
165 }
166
167 typedef sigc::signal<json, ToolID> type_signal_load_tool_settings;
168 type_signal_load_tool_settings signal_load_tool_settings()
169 {
170 return s_signal_load_tool_settings;
171 }
172
173 typedef sigc::signal<void, ToolID, json> type_signal_save_tool_settings;
174 type_signal_save_tool_settings signal_save_tool_settings()
175 {
176 return s_signal_save_tool_settings;
177 }
178
179 virtual void reload_pool()
180 {
181 }
182
183protected:
184 Core(class IPool &pool, IPool *m_pool_caching);
185 class IPool &m_pool;
186 class IPool &m_pool_caching;
187
188 type_signal_tool_changed s_signal_tool_changed;
189 type_signal_rebuilt s_signal_rebuilt;
190 type_signal_rebuilt s_signal_save;
191 type_signal_rebuilt s_signal_modified;
192 type_signal_rebuilt s_signal_can_undo_redo;
193 type_signal_needs_save s_signal_needs_save;
194 type_signal_load_tool_settings s_signal_load_tool_settings;
195 type_signal_save_tool_settings s_signal_save_tool_settings;
196 bool needs_save = false;
197 void set_needs_save(bool v);
198
199 void rebuild_finish(bool from_undo, const std::string &comment);
200
202 public:
203 HistoryItem(const std::string &c) : comment(c)
204 {
205 }
206 const std::string comment;
207 virtual ~HistoryItem()
208 {
209 }
210 };
211 std::deque<std::unique_ptr<HistoryItem>> history;
212 int history_current = -1;
213 size_t history_max = 50;
214 virtual void history_push(const std::string &comment) = 0;
215 virtual void history_load(unsigned int i) = 0;
216 virtual std::string get_history_comment_context() const;
217 void history_clear();
218 void history_trim();
219
220 bool property_transaction = false;
221
222 void layers_to_meta(class PropertyMeta &meta);
223 void get_placement(const Placement &placement, class PropertyValue &value, ObjectProperty::ID property);
224 void set_placement(Placement &placement, const class PropertyValue &value, ObjectProperty::ID property);
225
226 virtual void save(const std::string &suffix) = 0;
227 static const std::string autosave_suffix;
228 json get_meta_from_file(const std::string &filename);
229
230private:
231 std::unique_ptr<ToolBase> create_tool(ToolID tool_id);
232 std::set<SelectableRef> tool_selection;
233 void maybe_end_tool(const ToolResponse &r);
234 virtual void rebuild_internal(bool from_undo, const std::string &comment) = 0;
235
236 ToolID tool_id_current;
237 std::unique_ptr<ToolBase> tool = nullptr;
238 enum class ToolState { NONE, BEGINNING, UPDATING };
239 ToolState tool_state = ToolState::NONE;
240
241 std::list<ToolArgs> pending_tool_args;
242
243 class ToolStateSetter {
244 public:
245 ToolStateSetter(ToolState &s, ToolState target);
246 bool check_error() const
247 {
248 return error;
249 }
250 ~ToolStateSetter();
251
252 private:
253 ToolState &state;
254 bool error = false;
255 static std::string tool_state_to_string(ToolState s);
256 };
257};
258} // namespace horizon
A block is one level of hierarchy in the netlist.
Definition block.hpp:29
Definition core.hpp:201
Where Tools and and documents meet.
Definition core.hpp:42
virtual json get_meta()
Definition core.cpp:326
void rebuild(const std::string &comment)
Expands the non-working document.
Definition core.cpp:204
type_signal_rebuilt signal_save()
Gets emitted right before saving.
Definition core.hpp:146
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
Tiny metaprogramming library.
Definition meta.hpp:116