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.hpp"
10 #include "document/document.hpp"
11 #include "util/placement.hpp"
12 
13 namespace horizon {
14 
15 enum class ToolID;
16 
42 class Core : public virtual Document {
43 public:
44  class Block *get_block() override
45  {
46  return nullptr;
47  }
48 
49  class Rules *get_rules() override
50  {
51  return nullptr;
52  }
53 
54  class IPool &get_pool() override
55  {
56  return m_pool;
57  }
58  virtual ObjectType get_object_type() const = 0;
59 
64  virtual void rebuild(bool from_undo = false);
65  ToolResponse tool_begin(ToolID tool_id, const ToolArgs &args, class ImpInterface *imp, bool transient = false);
66  ToolResponse tool_update(const ToolArgs &args);
67  std::pair<bool, bool> tool_can_begin(ToolID tool_id, const std::set<SelectableRef> &selection);
68  void save();
69  void autosave();
70  virtual void delete_autosave() = 0;
71 
72  void undo();
73  void redo();
74 
75  bool can_undo() const;
76  bool can_redo() const;
77 
78  inline bool tool_is_active()
79  {
80  return tool != nullptr;
81  }
82 
83  virtual bool set_property(ObjectType type, const UUID &uu, ObjectProperty::ID property,
84  const class PropertyValue &value);
85  virtual bool get_property(ObjectType type, const UUID &uu, ObjectProperty::ID property, class PropertyValue &value);
86  virtual bool get_property_meta(ObjectType type, const UUID &uu, ObjectProperty::ID property,
87  class PropertyMeta &meta);
88 
89  void set_property_begin();
90  void set_property_commit();
91  bool get_property_transaction() const;
92 
97  virtual json get_meta();
98 
99  virtual void update_rules()
100  {
101  }
102 
103  virtual std::pair<Coordi, Coordi> get_bbox() = 0;
104 
105  virtual ~Core()
106  {
107  }
108  std::set<SelectableRef> &get_tool_selection();
109  std::set<InToolActionID> get_tool_actions() const;
110 
111  bool get_needs_save() const;
112  void set_needs_save();
113 
114  virtual const std::string &get_filename() const = 0;
115 
116  typedef sigc::signal<void, ToolID> type_signal_tool_changed;
117  type_signal_tool_changed signal_tool_changed()
118  {
119  return s_signal_tool_changed;
120  }
121  typedef sigc::signal<void> type_signal_rebuilt;
122  type_signal_rebuilt signal_rebuilt()
123  {
124  return s_signal_rebuilt;
125  }
131  type_signal_rebuilt signal_save()
132  {
133  return s_signal_save;
134  }
135 
136  type_signal_rebuilt signal_modified()
137  {
138  return s_signal_modified;
139  }
140 
141  type_signal_rebuilt signal_can_undo_redo()
142  {
143  return s_signal_can_undo_redo;
144  }
145 
146  typedef sigc::signal<void, bool> type_signal_needs_save;
147  type_signal_needs_save signal_needs_save()
148  {
149  return s_signal_needs_save;
150  }
151 
152  typedef sigc::signal<json, ToolID> type_signal_load_tool_settings;
153  type_signal_load_tool_settings signal_load_tool_settings()
154  {
155  return s_signal_load_tool_settings;
156  }
157 
158  typedef sigc::signal<void, ToolID, json> type_signal_save_tool_settings;
159  type_signal_save_tool_settings signal_save_tool_settings()
160  {
161  return s_signal_save_tool_settings;
162  }
163 
164  virtual void reload_pool()
165  {
166  }
167 
168 protected:
169  Core(class IPool &pool);
170  class IPool &m_pool;
171 
172  std::unique_ptr<ToolBase> tool = nullptr;
173  type_signal_tool_changed s_signal_tool_changed;
174  type_signal_rebuilt s_signal_rebuilt;
175  type_signal_rebuilt s_signal_save;
176  type_signal_rebuilt s_signal_modified;
177  type_signal_rebuilt s_signal_can_undo_redo;
178  type_signal_needs_save s_signal_needs_save;
179  type_signal_load_tool_settings s_signal_load_tool_settings;
180  type_signal_save_tool_settings s_signal_save_tool_settings;
181  bool needs_save = false;
182  void set_needs_save(bool v);
183 
184  class HistoryItem {
185  public:
186  // Symbol sym;
187  // HistoryItem(const Symbol &s): sym(s) {}
188  std::string comment;
189  virtual ~HistoryItem()
190  {
191  }
192  };
193  std::deque<std::unique_ptr<HistoryItem>> history;
194  int history_current = -1;
195  virtual void history_push() = 0;
196  virtual void history_load(unsigned int i) = 0;
197  void history_clear();
198  void history_trim();
199 
200  bool property_transaction = false;
201 
202  void layers_to_meta(class PropertyMeta &meta);
203  void get_placement(const Placement &placement, class PropertyValue &value, ObjectProperty::ID property);
204  void set_placement(Placement &placement, const class PropertyValue &value, ObjectProperty::ID property);
205 
206  virtual void save(const std::string &suffix) = 0;
207  static const std::string autosave_suffix;
208  json get_meta_from_file(const std::string &filename);
209 
210 private:
211  std::unique_ptr<ToolBase> create_tool(ToolID tool_id);
212  std::set<SelectableRef> tool_selection;
213  void maybe_end_tool(const ToolResponse &r);
214 };
215 } // namespace horizon
horizon::Core::get_meta
virtual json get_meta()
horizon::ImpInterface
Definition: imp_interface.hpp:12
horizon::Core::rebuild
virtual void rebuild(bool from_undo=false)
Expands the non-working document.
horizon::Core
Where Tools and and documents meet.
Definition: core.hpp:42
horizon::PropertyMeta
Definition: core_properties.hpp:90
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::Core::signal_save
type_signal_rebuilt signal_save()
Gets emitted right before saving.
Definition: core.hpp:131
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::PropertyValue
Definition: core_properties.hpp:7