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 "cores.hpp"
7 #include "dialogs/dialogs.hpp"
8 #include "nlohmann/json_fwd.hpp"
9 #include "pool/pool.hpp"
10 #include "pool/symbol.hpp"
11 #include <gdk/gdkkeysyms.h>
12 #include <iostream>
13 #include <memory>
14 #include <sigc++/sigc++.h>
15 
16 namespace horizon {
17 enum class ToolEventType { MOVE, CLICK, CLICK_RELEASE, KEY, LAYER_CHANGE };
18 
22 enum class ToolID {
23  NONE,
24  MOVE,
25  PLACE_JUNCTION,
26  DRAW_LINE,
27  DELETE,
28  DRAW_ARC,
29  ROTATE,
30  MIRROR_X,
31  MIRROR_Y,
32  MAP_PIN,
33  MAP_SYMBOL,
34  DRAW_NET,
35  ADD_COMPONENT,
36  PLACE_TEXT,
37  PLACE_NET_LABEL,
38  DISCONNECT,
39  BEND_LINE_NET,
40  SELECT_NET_SEGMENT,
41  SELECT_NET,
42  PLACE_POWER_SYMBOL,
43  MOVE_NET_SEGMENT,
44  MOVE_NET_SEGMENT_NEW,
45  EDIT_SYMBOL_PIN_NAMES,
46  PLACE_BUS_LABEL,
47  PLACE_BUS_RIPPER,
48  MANAGE_BUSES,
49  DRAW_POLYGON,
50  ENTER_DATUM,
51  MOVE_EXACTLY,
52  PLACE_HOLE,
53  PLACE_PAD,
54  PASTE,
55  ASSIGN_PART,
56  MAP_PACKAGE,
57  DRAW_TRACK,
58  PLACE_VIA,
59  ROUTE_TRACK,
60  DRAG_KEEP_SLOPE,
61  ADD_PART,
62  ANNOTATE,
63  SMASH,
64  UNSMASH,
65  PLACE_SHAPE,
66  EDIT_SHAPE,
67  IMPORT_DXF,
68  MANAGE_NET_CLASSES,
69  EDIT_PARAMETER_SET,
70  EDIT_PARAMETER_PROGRAM,
71  EDIT_PAD_PARAMETER_SET,
72  DRAW_POLYGON_RECTANGLE,
73  DRAW_LINE_RECTANGLE,
74  EDIT_LINE_RECTANGLE,
75  EDIT_SCHEMATIC_PROPERTIES,
76  ROUTE_TRACK_INTERACTIVE,
77  EDIT_VIA,
78  ROTATE_ARBITRARY,
79  ADD_PLANE,
80  EDIT_PLANE,
81  UPDATE_PLANE,
82  UPDATE_ALL_PLANES,
83  CLEAR_PLANE,
84  CLEAR_ALL_PLANES,
85  EDIT_STACKUP,
86  DRAW_DIMENSION,
87  SET_DIFFPAIR,
88  CLEAR_DIFFPAIR,
89  ROUTE_DIFFPAIR_INTERACTIVE,
90  SELECT_MORE,
91  SET_VIA_NET,
92  CLEAR_VIA_NET,
93  DRAG_TRACK_INTERACTIVE,
94  LOCK,
95  UNLOCK,
96  UNLOCK_ALL,
97  ADD_VERTEX,
98  MANAGE_POWER_NETS,
99  PLACE_BOARD_HOLE,
100  EDIT_BOARD_HOLE,
101  GENERATE_COURTYARD
102 };
103 
108 class ToolArgs {
109 public:
110  ToolEventType type;
111  Coordi coords;
112  std::set<SelectableRef> selection;
113  bool keep_selection = false;
114  unsigned int button;
115  unsigned int key;
116  Target target;
117  int work_layer;
118  ToolArgs()
119  {
120  }
121 };
122 
127 public:
128  ToolID next_tool = ToolID::NONE;
129  bool end_tool = false;
130  int layer = 10000;
131  ToolResponse()
132  {
133  }
138  static ToolResponse end()
139  {
140  ToolResponse r;
141  r.end_tool = true;
142  return r;
143  }
144 
149  {
150  ToolResponse r;
151  r.layer = l;
152  return r;
153  }
154 
158  static ToolResponse next(ToolID t)
159  {
160  ToolResponse r;
161  r.end_tool = true;
162  r.next_tool = t;
163  return r;
164  };
165 };
166 
170 class ToolBase {
171 public:
172  ToolBase(class Core *c, ToolID tid);
173  void set_imp_interface(class ImpInterface *i);
174  void set_transient();
175 
182  virtual ToolResponse begin(const ToolArgs &args) = 0;
183 
187  virtual ToolResponse update(const ToolArgs &args) = 0;
188 
192  virtual bool can_begin()
193  {
194  return false;
195  }
196 
200  virtual bool is_specific()
201  {
202  return false;
203  }
204 
208  virtual bool handles_esc()
209  {
210  return false;
211  }
212 
213  virtual ~ToolBase()
214  {
215  }
216 
217 protected:
218  Cores core;
219  class ImpInterface *imp = nullptr;
220  ToolID tool_id = ToolID::NONE;
221  bool is_transient = false;
222 };
223 
249 class Core : public sigc::trackable {
250 public:
251  virtual bool has_object_type(ObjectType ty)
252  {
253  return false;
254  }
255 
256  virtual class Junction *insert_junction(const UUID &uu, bool work = true);
257  virtual class Junction *get_junction(const UUID &uu, bool work = true);
258  virtual void delete_junction(const UUID &uu, bool work = true);
259 
260  virtual class Line *insert_line(const UUID &uu, bool work = true);
261  virtual class Line *get_line(const UUID &uu, bool work = true);
262  virtual void delete_line(const UUID &uu, bool work = true);
263 
264  virtual class Arc *insert_arc(const UUID &uu, bool work = true);
265  virtual class Arc *get_arc(const UUID &uu, bool work = true);
266  virtual void delete_arc(const UUID &uu, bool work = true);
267 
268  virtual class Text *insert_text(const UUID &uu, bool work = true);
269  virtual class Text *get_text(const UUID &uu, bool work = true);
270  virtual void delete_text(const UUID &uu, bool work = true);
271 
272  virtual class Polygon *insert_polygon(const UUID &uu, bool work = true);
273  virtual class Polygon *get_polygon(const UUID &uu, bool work = true);
274  virtual void delete_polygon(const UUID &uu, bool work = true);
275 
276  virtual class Hole *insert_hole(const UUID &uu, bool work = true);
277  virtual class Hole *get_hole(const UUID &uu, bool work = true);
278  virtual void delete_hole(const UUID &uu, bool work = true);
279 
280  virtual class Dimension *insert_dimension(const UUID &uu);
281  virtual class Dimension *get_dimension(const UUID &uu);
282  virtual void delete_dimension(const UUID &uu);
283 
284  virtual std::vector<Line *> get_lines(bool work = true);
285  virtual std::vector<Arc *> get_arcs(bool work = true);
286 
287  virtual class Block *get_block(bool work = true)
288  {
289  return nullptr;
290  }
291 
296  virtual void rebuild(bool from_undo = false);
297  ToolResponse tool_begin(ToolID tool_id, const ToolArgs &args, class ImpInterface *imp, bool transient = false);
298  ToolResponse tool_update(const ToolArgs &args);
299  std::pair<bool, bool> tool_can_begin(ToolID tool_id, const std::set<SelectableRef> &selection);
300  bool tool_handles_esc();
301  virtual void commit() = 0;
302  virtual void revert() = 0;
303  virtual void save() = 0;
304  void undo();
305  void redo();
306 
307  bool can_undo() const;
308  bool can_redo() const;
309 
310  inline bool tool_is_active()
311  {
312  return tool != nullptr;
313  }
314 
315  virtual bool set_property(ObjectType type, const UUID &uu, ObjectProperty::ID property,
316  const class PropertyValue &value);
317  virtual bool get_property(ObjectType type, const UUID &uu, ObjectProperty::ID property, class PropertyValue &value);
318  virtual bool get_property_meta(ObjectType type, const UUID &uu, ObjectProperty::ID property,
319  class PropertyMeta &meta);
320 
321  virtual std::string get_display_name(ObjectType type, const UUID &uu);
322 
323  void set_property_begin();
324  void set_property_commit();
325  bool get_property_transaction() const;
326 
327  virtual class LayerProvider *get_layer_provider()
328  {
329  return nullptr;
330  };
331 
336  virtual json get_meta();
337 
338  virtual class Rules *get_rules()
339  {
340  return nullptr;
341  }
342  virtual void update_rules()
343  {
344  }
345 
346  virtual std::pair<Coordi, Coordi> get_bbox() = 0;
347 
348  virtual ~Core()
349  {
350  }
351  std::set<SelectableRef> selection;
352  Pool *m_pool;
353 
354  bool get_needs_save() const;
355  void set_needs_save();
356 
357  typedef sigc::signal<void, ToolID> type_signal_tool_changed;
358  type_signal_tool_changed signal_tool_changed()
359  {
360  return s_signal_tool_changed;
361  }
362  typedef sigc::signal<void> type_signal_rebuilt;
363  type_signal_rebuilt signal_rebuilt()
364  {
365  return s_signal_rebuilt;
366  }
372  type_signal_rebuilt signal_save()
373  {
374  return s_signal_save;
375  }
376 
377  type_signal_rebuilt signal_can_undo_redo()
378  {
379  return s_signal_can_undo_redo;
380  }
381 
382  typedef sigc::signal<json> type_signal_request_save_meta;
387  type_signal_request_save_meta signal_request_save_meta()
388  {
389  return s_signal_request_save_meta;
390  }
391 
392  typedef sigc::signal<void, bool> type_signal_needs_save;
393  type_signal_needs_save signal_needs_save()
394  {
395  return s_signal_needs_save;
396  }
397 
398 protected:
399  virtual std::map<UUID, Junction> *get_junction_map(bool work = true)
400  {
401  return nullptr;
402  }
403  virtual std::map<UUID, Line> *get_line_map(bool work = true)
404  {
405  return nullptr;
406  }
407  virtual std::map<UUID, Arc> *get_arc_map(bool work = true)
408  {
409  return nullptr;
410  }
411  virtual std::map<UUID, Text> *get_text_map(bool work = true)
412  {
413  return nullptr;
414  }
415  virtual std::map<UUID, Polygon> *get_polygon_map(bool work = true)
416  {
417  return nullptr;
418  }
419  virtual std::map<UUID, Hole> *get_hole_map(bool work = true)
420  {
421  return nullptr;
422  }
423  virtual std::map<UUID, Dimension> *get_dimension_map()
424  {
425  return nullptr;
426  }
427 
428  bool reverted = false;
429  std::unique_ptr<ToolBase> tool = nullptr;
430  type_signal_tool_changed s_signal_tool_changed;
431  type_signal_rebuilt s_signal_rebuilt;
432  type_signal_rebuilt s_signal_save;
433  type_signal_rebuilt s_signal_can_undo_redo;
434  type_signal_request_save_meta s_signal_request_save_meta;
435  type_signal_needs_save s_signal_needs_save;
436  bool needs_save = false;
437  void set_needs_save(bool v);
438 
439  class HistoryItem {
440  public:
441  // Symbol sym;
442  // HistoryItem(const Symbol &s): sym(s) {}
443  std::string comment;
444  virtual ~HistoryItem()
445  {
446  }
447  };
448  std::deque<std::unique_ptr<HistoryItem>> history;
449  int history_current = -1;
450  virtual void history_push() = 0;
451  virtual void history_load(unsigned int i) = 0;
452  bool property_transaction = false;
453 
454  void layers_to_meta(class PropertyMeta &meta);
455  void get_placement(const Placement &placement, class PropertyValue &value, ObjectProperty::ID property);
456  void set_placement(Placement &placement, const class PropertyValue &value, ObjectProperty::ID property);
457 
458 private:
459  std::unique_ptr<ToolBase> create_tool(ToolID tool_id);
460 };
461 } // namespace horizon
virtual bool can_begin()
Definition: core.hpp:192
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:108
virtual bool handles_esc()
Definition: core.hpp:208
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:387
Graphical line.
Definition: line.hpp:19
Definition: core_properties.hpp:7
Definition: placement.hpp:8
Definition: rules.hpp:41
A block is one level of hierarchy in the netlist.
Definition: block.hpp:25
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:148
Used wherever a user-editable text is needed.
Definition: text.hpp:18
Definition: imp_interface.hpp:6
Where Tools and and documents meet.
Definition: core.hpp:249
Definition: dimension.hpp:12
virtual bool is_specific()
Definition: core.hpp:200
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:158
To signal back to the core what the Tool did, a Tool returns a ToolResponse.
Definition: core.hpp:126
static ToolResponse end()
Use this if you&#39;re done.
Definition: core.hpp:138
Stores objects (Unit, Entity, Symbol, Part, etc.) from the pool.
Definition: pool.hpp:18
Definition: block.cpp:7
type_signal_rebuilt signal_save()
Gets emitted right before saving.
Definition: core.hpp:372
Definition: core.hpp:439
Common interface for all Tools.
Definition: core.hpp:170
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