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 "cores.hpp"
8 #include "tool_data.hpp"
9 #include "dialogs/dialogs.hpp"
10 #include "nlohmann/json_fwd.hpp"
11 #include "pool/pool.hpp"
12 #include "pool/symbol.hpp"
13 #include <gdk/gdkkeysyms.h>
14 #include <iostream>
15 #include <memory>
16 #include <sigc++/sigc++.h>
17 #include "tool_id.hpp"
18 
19 namespace horizon {
20 enum class ToolEventType { MOVE, CLICK, CLICK_RELEASE, KEY, LAYER_CHANGE, DATA };
21 
22 
27 class ToolArgs {
28 public:
29  ToolEventType type;
30  Coordi coords;
31  std::set<SelectableRef> selection;
32  bool keep_selection = false;
33  unsigned int button;
34  unsigned int key;
35  Target target;
36  int work_layer;
37  std::unique_ptr<ToolData> data = nullptr;
38  ToolArgs()
39  {
40  }
41 };
42 
46 class ToolResponse {
47 public:
48  ToolID next_tool = ToolID::NONE;
49  bool end_tool = false;
50  int layer = 10000;
51  bool fast_draw = false;
52 
53  ToolResponse()
54  {
55  }
60  static ToolResponse end()
61  {
62  ToolResponse r;
63  r.end_tool = true;
64  return r;
65  }
66 
67  static ToolResponse fast()
68  {
69  ToolResponse r;
70  r.fast_draw = true;
71  return r;
72  }
73 
77  static ToolResponse change_layer(int l)
78  {
79  ToolResponse r;
80  r.layer = l;
81  return r;
82  }
83 
87  static ToolResponse next(ToolID t)
88  {
89  ToolResponse r;
90  r.end_tool = true;
91  r.next_tool = t;
92  return r;
93  };
94 };
95 
96 class ToolSettings {
97 public:
98  virtual void load_from_json(const json &j) = 0;
99  virtual json serialize() const = 0;
100  virtual ~ToolSettings()
101  {
102  }
103 };
104 
106 public:
107  ToolSettingsProxy(class ToolBase *t, ToolSettings *s) : tool(t), settings(s)
108  {
109  }
110  ToolSettings &operator*()
111  {
112  return *settings;
113  }
114  operator ToolSettings *()
115  {
116  return settings;
117  }
118  ToolSettings *operator->()
119  {
120  return settings;
121  }
123 
124 private:
125  ToolBase *tool;
126  ToolSettings *settings;
127 };
128 
129 
133 class ToolBase {
134 public:
135  ToolBase(class Core *c, ToolID tid);
136  void set_imp_interface(class ImpInterface *i);
137  void set_transient();
138  virtual ToolID get_tool_id_for_settings() const
139  {
140  return tool_id;
141  }
142  virtual const ToolSettings *get_settings_const() const
143  {
144  return nullptr;
145  }
146  ToolSettingsProxy get_settings_proxy()
147  {
148  return ToolSettingsProxy(this, get_settings());
149  }
150  virtual void apply_settings()
151  {
152  }
153 
154 
161  virtual ToolResponse begin(const ToolArgs &args) = 0;
162 
166  virtual ToolResponse update(const ToolArgs &args) = 0;
167 
171  virtual bool can_begin()
172  {
173  return false;
174  }
175 
179  virtual bool is_specific()
180  {
181  return false;
182  }
183 
187  virtual bool handles_esc()
188  {
189  return false;
190  }
191 
192  virtual ~ToolBase()
193  {
194  }
195 
196 protected:
197  Cores core;
198  class ImpInterface *imp = nullptr;
199  ToolID tool_id = ToolID::NONE;
200  bool is_transient = false;
201  virtual ToolSettings *get_settings()
202  {
203  return nullptr;
204  }
205 };
206 
232 class Core : public sigc::trackable {
233 public:
234  virtual bool has_object_type(ObjectType ty)
235  {
236  return false;
237  }
238 
239  virtual class Junction *insert_junction(const UUID &uu, bool work = true);
240  virtual class Junction *get_junction(const UUID &uu, bool work = true);
241  virtual void delete_junction(const UUID &uu, bool work = true);
242 
243  virtual class Line *insert_line(const UUID &uu, bool work = true);
244  virtual class Line *get_line(const UUID &uu, bool work = true);
245  virtual void delete_line(const UUID &uu, bool work = true);
246 
247  virtual class Arc *insert_arc(const UUID &uu, bool work = true);
248  virtual class Arc *get_arc(const UUID &uu, bool work = true);
249  virtual void delete_arc(const UUID &uu, bool work = true);
250 
251  virtual class Text *insert_text(const UUID &uu, bool work = true);
252  virtual class Text *get_text(const UUID &uu, bool work = true);
253  virtual void delete_text(const UUID &uu, bool work = true);
254 
255  virtual class Polygon *insert_polygon(const UUID &uu, bool work = true);
256  virtual class Polygon *get_polygon(const UUID &uu, bool work = true);
257  virtual void delete_polygon(const UUID &uu, bool work = true);
258 
259  virtual class Hole *insert_hole(const UUID &uu, bool work = true);
260  virtual class Hole *get_hole(const UUID &uu, bool work = true);
261  virtual void delete_hole(const UUID &uu, bool work = true);
262 
263  virtual class Dimension *insert_dimension(const UUID &uu);
264  virtual class Dimension *get_dimension(const UUID &uu);
265  virtual void delete_dimension(const UUID &uu);
266 
267  virtual class Keepout *insert_keepout(const UUID &uu);
268  virtual class Keepout *get_keepout(const UUID &uu);
269  virtual void delete_keepout(const UUID &uu);
270 
271  virtual std::vector<Line *> get_lines(bool work = true);
272  virtual std::vector<Arc *> get_arcs(bool work = true);
273  virtual std::vector<Keepout *> get_keepouts();
274 
275  virtual class Block *get_block(bool work = true)
276  {
277  return nullptr;
278  }
279 
284  virtual void rebuild(bool from_undo = false);
285  ToolResponse tool_begin(ToolID tool_id, const ToolArgs &args, class ImpInterface *imp, bool transient = false);
286  ToolResponse tool_update(const ToolArgs &args);
287  std::pair<bool, bool> tool_can_begin(ToolID tool_id, const std::set<SelectableRef> &selection);
288  bool tool_handles_esc();
289  virtual void commit() = 0;
290  virtual void revert() = 0;
291  virtual void save() = 0;
292  void undo();
293  void redo();
294 
295  bool can_undo() const;
296  bool can_redo() const;
297 
298  inline bool tool_is_active()
299  {
300  return tool != nullptr;
301  }
302 
303  virtual bool set_property(ObjectType type, const UUID &uu, ObjectProperty::ID property,
304  const class PropertyValue &value);
305  virtual bool get_property(ObjectType type, const UUID &uu, ObjectProperty::ID property, class PropertyValue &value);
306  virtual bool get_property_meta(ObjectType type, const UUID &uu, ObjectProperty::ID property,
307  class PropertyMeta &meta);
308 
309  virtual std::string get_display_name(ObjectType type, const UUID &uu);
310 
311  void set_property_begin();
312  void set_property_commit();
313  bool get_property_transaction() const;
314 
315  virtual class LayerProvider *get_layer_provider()
316  {
317  return nullptr;
318  };
319 
324  virtual json get_meta();
325 
326  virtual class Rules *get_rules()
327  {
328  return nullptr;
329  }
330  virtual void update_rules()
331  {
332  }
333 
334  virtual std::pair<Coordi, Coordi> get_bbox() = 0;
335 
336  virtual ~Core()
337  {
338  }
339  std::set<SelectableRef> selection;
340  Pool *m_pool;
341 
342  bool get_needs_save() const;
343  void set_needs_save();
344 
345  typedef sigc::signal<void, ToolID> type_signal_tool_changed;
346  type_signal_tool_changed signal_tool_changed()
347  {
348  return s_signal_tool_changed;
349  }
350  typedef sigc::signal<void> type_signal_rebuilt;
351  type_signal_rebuilt signal_rebuilt()
352  {
353  return s_signal_rebuilt;
354  }
360  type_signal_rebuilt signal_save()
361  {
362  return s_signal_save;
363  }
364 
365  type_signal_rebuilt signal_can_undo_redo()
366  {
367  return s_signal_can_undo_redo;
368  }
369 
370  typedef sigc::signal<json> type_signal_request_save_meta;
375  type_signal_request_save_meta signal_request_save_meta()
376  {
377  return s_signal_request_save_meta;
378  }
379 
380  typedef sigc::signal<void, bool> type_signal_needs_save;
381  type_signal_needs_save signal_needs_save()
382  {
383  return s_signal_needs_save;
384  }
385 
386  typedef sigc::signal<json, ToolID> type_signal_load_tool_settings;
387  type_signal_load_tool_settings signal_load_tool_settings()
388  {
389  return s_signal_load_tool_settings;
390  }
391 
392  typedef sigc::signal<void, ToolID, json> type_signal_save_tool_settings;
393  type_signal_save_tool_settings signal_save_tool_settings()
394  {
395  return s_signal_save_tool_settings;
396  }
397 
398  virtual void reload_pool()
399  {
400  }
401 
402 protected:
403  virtual std::map<UUID, Junction> *get_junction_map(bool work = true)
404  {
405  return nullptr;
406  }
407  virtual std::map<UUID, Line> *get_line_map(bool work = true)
408  {
409  return nullptr;
410  }
411  virtual std::map<UUID, Arc> *get_arc_map(bool work = true)
412  {
413  return nullptr;
414  }
415  virtual std::map<UUID, Text> *get_text_map(bool work = true)
416  {
417  return nullptr;
418  }
419  virtual std::map<UUID, Polygon> *get_polygon_map(bool work = true)
420  {
421  return nullptr;
422  }
423  virtual std::map<UUID, Hole> *get_hole_map(bool work = true)
424  {
425  return nullptr;
426  }
427  virtual std::map<UUID, Dimension> *get_dimension_map()
428  {
429  return nullptr;
430  }
431  virtual std::map<UUID, Keepout> *get_keepout_map()
432  {
433  return nullptr;
434  }
435 
436  bool reverted = false;
437  std::unique_ptr<ToolBase> tool = nullptr;
438  type_signal_tool_changed s_signal_tool_changed;
439  type_signal_rebuilt s_signal_rebuilt;
440  type_signal_rebuilt s_signal_save;
441  type_signal_rebuilt s_signal_can_undo_redo;
442  type_signal_request_save_meta s_signal_request_save_meta;
443  type_signal_needs_save s_signal_needs_save;
444  type_signal_load_tool_settings s_signal_load_tool_settings;
445  type_signal_save_tool_settings s_signal_save_tool_settings;
446  bool needs_save = false;
447  void set_needs_save(bool v);
448 
449  class HistoryItem {
450  public:
451  // Symbol sym;
452  // HistoryItem(const Symbol &s): sym(s) {}
453  std::string comment;
454  virtual ~HistoryItem()
455  {
456  }
457  };
458  std::deque<std::unique_ptr<HistoryItem>> history;
459  int history_current = -1;
460  virtual void history_push() = 0;
461  virtual void history_load(unsigned int i) = 0;
462  void history_clear();
463 
464  bool property_transaction = false;
465 
466  void layers_to_meta(class PropertyMeta &meta);
467  void get_placement(const Placement &placement, class PropertyValue &value, ObjectProperty::ID property);
468  void set_placement(Placement &placement, const class PropertyValue &value, ObjectProperty::ID property);
469 
470 private:
471  std::unique_ptr<ToolBase> create_tool(ToolID tool_id);
472 };
473 } // namespace horizon
virtual bool can_begin()
Definition: core.hpp:171
Definition: target.hpp:6
Polygon used in Padstack, Package and Board for specifying filled Regions.
Definition: polygon.hpp:27
a class to store JSON values
Definition: json.hpp:161
This is what a Tool receives when the user did something.
Definition: core.hpp:27
Definition: core.hpp:96
virtual bool handles_esc()
Definition: core.hpp:187
type_signal_request_save_meta signal_request_save_meta()
connect to this signal for providing meta information when the document is saved
Definition: core.hpp:375
Graphical line.
Definition: line.hpp:19
Definition: core_properties.hpp:7
Definition: placement.hpp:8
Definition: rules.hpp:44
Definition: keepout.hpp:9
A block is one level of hierarchy in the netlist.
Definition: block.hpp:26
Tools use this class to actually access the core.
Definition: cores.hpp:13
static ToolResponse change_layer(int l)
Use this for changing the work layer from a Tool.
Definition: core.hpp:77
Used wherever a user-editable text is needed.
Definition: text.hpp:19
Definition: imp_interface.hpp:7
Where Tools and and documents meet.
Definition: core.hpp:232
Definition: dimension.hpp:12
virtual bool is_specific()
Definition: core.hpp:179
Definition: core.hpp:105
Definition: layer_provider.hpp:7
Definition: core_properties.hpp:77
This class encapsulates a UUID and allows it to be uses as a value type.
Definition: uuid.hpp:16
static ToolResponse next(ToolID t)
If you want another Tool to be launched you&#39;ve finished, use this one.
Definition: core.hpp:87
To signal back to the core what the Tool did, a Tool returns a ToolResponse.
Definition: core.hpp:46
static ToolResponse end()
Use this if you&#39;re done.
Definition: core.hpp:60
Stores objects (Unit, Entity, Symbol, Part, etc.) from the pool.
Definition: pool.hpp:19
Definition: block.cpp:9
type_signal_rebuilt signal_save()
Gets emitted right before saving.
Definition: core.hpp:360
Definition: core.hpp:449
Common interface for all Tools.
Definition: core.hpp:133
A Junction is a point in 2D-Space.
Definition: junction.hpp:25
Graphical arc.
Definition: arc.hpp:20
A hole with diameter and position, that&#39;s it.
Definition: hole.hpp:19