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 "nlohmann/json_fwd.hpp"
10 #include "pool/pool.hpp"
11 #include "pool/symbol.hpp"
12 #include <gdk/gdkkeysyms.h>
13 #include <iostream>
14 #include <memory>
15 #include <sigc++/sigc++.h>
16 #include "tool_id.hpp"
17 
18 namespace horizon {
19 enum class ToolEventType { NONE, MOVE, CLICK, CLICK_RELEASE, KEY, LAYER_CHANGE, DATA };
20 
21 
26 class ToolArgs {
27 public:
28  ToolEventType type = ToolEventType::NONE;
29  Coordi coords;
30  std::set<SelectableRef> selection;
31  bool keep_selection = false;
32  unsigned int button = 0;
33  unsigned int key = 0;
34  enum Modifieres {
35  MOD_FINE = (1 << 0),
36  MOD_ALT = (1 << 1),
37  MOD_CTRL = (1 << 2),
38  };
39  unsigned int mod = 0;
40 
41  Target target;
42  int work_layer = 0;
43  std::unique_ptr<ToolData> data = nullptr;
44  ToolArgs()
45  {
46  }
47 };
48 
52 class ToolResponse {
53 public:
54  ToolID next_tool = ToolID::NONE;
55  std::unique_ptr<ToolData> data = nullptr;
56  bool end_tool = false;
57  int layer = 10000;
58  bool fast_draw = false;
59 
60  ToolResponse()
61  {
62  }
67  static ToolResponse end()
68  {
69  ToolResponse r;
70  r.end_tool = true;
71  return r;
72  }
73 
74  static ToolResponse fast()
75  {
76  ToolResponse r;
77  r.fast_draw = true;
78  return r;
79  }
80 
84  static ToolResponse change_layer(int l)
85  {
86  ToolResponse r;
87  r.layer = l;
88  return r;
89  }
90 
94  static ToolResponse next(ToolID t, std::unique_ptr<ToolData> data = nullptr)
95  {
96  ToolResponse r;
97  r.end_tool = true;
98  r.next_tool = t;
99  r.data = std::move(data);
100  return r;
101  };
102 };
103 
105 public:
106  virtual void load_from_json(const json &j) = 0;
107  virtual json serialize() const = 0;
108  virtual ~ToolSettings()
109  {
110  }
111 };
112 
114 public:
115  ToolSettingsProxy(class ToolBase *t, ToolSettings *s) : tool(t), settings(s)
116  {
117  }
118  ToolSettings &operator*()
119  {
120  return *settings;
121  }
122  operator ToolSettings *()
123  {
124  return settings;
125  }
126  ToolSettings *operator->()
127  {
128  return settings;
129  }
131 
132 private:
133  ToolBase *tool;
134  ToolSettings *settings;
135 };
136 
137 
141 class ToolBase {
142 public:
143  ToolBase(class Core *c, ToolID tid);
144  void set_imp_interface(class ImpInterface *i);
145  void set_transient();
146  virtual ToolID get_tool_id_for_settings() const
147  {
148  return tool_id;
149  }
150  virtual const ToolSettings *get_settings_const() const
151  {
152  return nullptr;
153  }
154  ToolSettingsProxy get_settings_proxy()
155  {
156  return ToolSettingsProxy(this, get_settings());
157  }
158  virtual void apply_settings()
159  {
160  }
161 
162 
169  virtual ToolResponse begin(const ToolArgs &args) = 0;
170 
174  virtual ToolResponse update(const ToolArgs &args) = 0;
175 
179  virtual bool can_begin()
180  {
181  return false;
182  }
183 
187  virtual bool is_specific()
188  {
189  return false;
190  }
191 
195  virtual bool handles_esc()
196  {
197  return false;
198  }
199 
200  virtual ~ToolBase()
201  {
202  }
203 
204 protected:
205  Cores core;
206  class ImpInterface *imp = nullptr;
207  ToolID tool_id = ToolID::NONE;
208  bool is_transient = false;
209  virtual ToolSettings *get_settings()
210  {
211  return nullptr;
212  }
213 };
214 
240 class Core {
241 public:
242  virtual bool has_object_type(ObjectType ty) const
243  {
244  return false;
245  }
246 
247  virtual class Junction *insert_junction(const UUID &uu, bool work = true);
248  virtual class Junction *get_junction(const UUID &uu, bool work = true);
249  virtual void delete_junction(const UUID &uu, bool work = true);
250 
251  virtual class Line *insert_line(const UUID &uu, bool work = true);
252  virtual class Line *get_line(const UUID &uu, bool work = true);
253  virtual void delete_line(const UUID &uu, bool work = true);
254 
255  virtual class Arc *insert_arc(const UUID &uu, bool work = true);
256  virtual class Arc *get_arc(const UUID &uu, bool work = true);
257  virtual void delete_arc(const UUID &uu, bool work = true);
258 
259  virtual class Text *insert_text(const UUID &uu, bool work = true);
260  virtual class Text *get_text(const UUID &uu, bool work = true);
261  virtual void delete_text(const UUID &uu, bool work = true);
262 
263  virtual class Polygon *insert_polygon(const UUID &uu, bool work = true);
264  virtual class Polygon *get_polygon(const UUID &uu, bool work = true);
265  virtual void delete_polygon(const UUID &uu, bool work = true);
266 
267  virtual class Hole *insert_hole(const UUID &uu, bool work = true);
268  virtual class Hole *get_hole(const UUID &uu, bool work = true);
269  virtual void delete_hole(const UUID &uu, bool work = true);
270 
271  virtual class Dimension *insert_dimension(const UUID &uu);
272  virtual class Dimension *get_dimension(const UUID &uu);
273  virtual void delete_dimension(const UUID &uu);
274 
275  virtual class Keepout *insert_keepout(const UUID &uu);
276  virtual class Keepout *get_keepout(const UUID &uu);
277  virtual void delete_keepout(const UUID &uu);
278 
279  virtual std::vector<Line *> get_lines(bool work = true);
280  virtual std::vector<Arc *> get_arcs(bool work = true);
281  virtual std::vector<Keepout *> get_keepouts();
282 
283  virtual class Block *get_block(bool work = true)
284  {
285  return nullptr;
286  }
287 
292  virtual void rebuild(bool from_undo = false);
293  ToolResponse tool_begin(ToolID tool_id, const ToolArgs &args, class ImpInterface *imp, bool transient = false);
294  ToolResponse tool_update(const ToolArgs &args);
295  std::pair<bool, bool> tool_can_begin(ToolID tool_id, const std::set<SelectableRef> &selection);
296  bool tool_handles_esc();
297  virtual void commit() = 0;
298  virtual void revert() = 0;
299  void save();
300  void autosave();
301  virtual void delete_autosave() = 0;
302 
303  void undo();
304  void redo();
305 
306  bool can_undo() const;
307  bool can_redo() const;
308 
309  inline bool tool_is_active()
310  {
311  return tool != nullptr;
312  }
313 
314  virtual bool set_property(ObjectType type, const UUID &uu, ObjectProperty::ID property,
315  const class PropertyValue &value);
316  virtual bool get_property(ObjectType type, const UUID &uu, ObjectProperty::ID property, class PropertyValue &value);
317  virtual bool get_property_meta(ObjectType type, const UUID &uu, ObjectProperty::ID property,
318  class PropertyMeta &meta);
319 
320  virtual std::string get_display_name(ObjectType type, const UUID &uu);
321  virtual std::string get_display_name(ObjectType type, const UUID &uu, const UUID &sheet);
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 bool can_search_for_object_type(ObjectType type) const
347  {
348  return false;
349  };
350 
351  class SearchQuery {
352  public:
353  std::string query;
354  std::set<ObjectType> types;
355  std::pair<Coordf, Coordf> area_visible;
356  };
357 
358  class SearchResult {
359  public:
360  SearchResult(ObjectType ty, const UUID &uu) : type(ty), uuid(uu)
361  {
362  }
363  ObjectType type;
364  UUID uuid;
365  Coordi location;
366  UUID sheet;
367  bool selectable = false;
368  };
369 
370  virtual std::list<SearchResult> search(const SearchQuery &q)
371  {
372  return {};
373  };
374 
375  virtual std::pair<Coordi, Coordi> get_bbox() = 0;
376 
377  virtual ~Core()
378  {
379  }
380  std::set<SelectableRef> selection;
381  Pool *m_pool;
382 
383  bool get_needs_save() const;
384  void set_needs_save();
385 
386  virtual const std::string &get_filename() const = 0;
387 
388  typedef sigc::signal<void, ToolID> type_signal_tool_changed;
389  type_signal_tool_changed signal_tool_changed()
390  {
391  return s_signal_tool_changed;
392  }
393  typedef sigc::signal<void> type_signal_rebuilt;
394  type_signal_rebuilt signal_rebuilt()
395  {
396  return s_signal_rebuilt;
397  }
403  type_signal_rebuilt signal_save()
404  {
405  return s_signal_save;
406  }
407 
408  type_signal_rebuilt signal_modified()
409  {
410  return s_signal_modified;
411  }
412 
413  type_signal_rebuilt signal_can_undo_redo()
414  {
415  return s_signal_can_undo_redo;
416  }
417 
418  typedef sigc::signal<json> type_signal_request_save_meta;
423  type_signal_request_save_meta signal_request_save_meta()
424  {
425  return s_signal_request_save_meta;
426  }
427 
428  typedef sigc::signal<void, bool> type_signal_needs_save;
429  type_signal_needs_save signal_needs_save()
430  {
431  return s_signal_needs_save;
432  }
433 
434  typedef sigc::signal<json, ToolID> type_signal_load_tool_settings;
435  type_signal_load_tool_settings signal_load_tool_settings()
436  {
437  return s_signal_load_tool_settings;
438  }
439 
440  typedef sigc::signal<void, ToolID, json> type_signal_save_tool_settings;
441  type_signal_save_tool_settings signal_save_tool_settings()
442  {
443  return s_signal_save_tool_settings;
444  }
445 
446  virtual void reload_pool()
447  {
448  }
449 
450 protected:
451  virtual std::map<UUID, Junction> *get_junction_map(bool work = true)
452  {
453  return nullptr;
454  }
455  virtual std::map<UUID, Line> *get_line_map(bool work = true)
456  {
457  return nullptr;
458  }
459  virtual std::map<UUID, Arc> *get_arc_map(bool work = true)
460  {
461  return nullptr;
462  }
463  virtual std::map<UUID, Text> *get_text_map(bool work = true)
464  {
465  return nullptr;
466  }
467  virtual std::map<UUID, Polygon> *get_polygon_map(bool work = true)
468  {
469  return nullptr;
470  }
471  virtual std::map<UUID, Hole> *get_hole_map(bool work = true)
472  {
473  return nullptr;
474  }
475  virtual std::map<UUID, Dimension> *get_dimension_map()
476  {
477  return nullptr;
478  }
479  virtual std::map<UUID, Keepout> *get_keepout_map()
480  {
481  return nullptr;
482  }
483 
484  bool reverted = false;
485  std::unique_ptr<ToolBase> tool = nullptr;
486  type_signal_tool_changed s_signal_tool_changed;
487  type_signal_rebuilt s_signal_rebuilt;
488  type_signal_rebuilt s_signal_save;
489  type_signal_rebuilt s_signal_modified;
490  type_signal_rebuilt s_signal_can_undo_redo;
491  type_signal_request_save_meta s_signal_request_save_meta;
492  type_signal_needs_save s_signal_needs_save;
493  type_signal_load_tool_settings s_signal_load_tool_settings;
494  type_signal_save_tool_settings s_signal_save_tool_settings;
495  bool needs_save = false;
496  void set_needs_save(bool v);
497 
498  class HistoryItem {
499  public:
500  // Symbol sym;
501  // HistoryItem(const Symbol &s): sym(s) {}
502  std::string comment;
503  virtual ~HistoryItem()
504  {
505  }
506  };
507  std::deque<std::unique_ptr<HistoryItem>> history;
508  int history_current = -1;
509  virtual void history_push() = 0;
510  virtual void history_load(unsigned int i) = 0;
511  void history_clear();
512 
513  bool property_transaction = false;
514 
515  void layers_to_meta(class PropertyMeta &meta);
516  void get_placement(const Placement &placement, class PropertyValue &value, ObjectProperty::ID property);
517  void set_placement(Placement &placement, const class PropertyValue &value, ObjectProperty::ID property);
518 
519  void sort_search_results(std::list<Core::SearchResult> &results, const SearchQuery &q);
520 
521  virtual void save(const std::string &suffix) = 0;
522  static const std::string autosave_suffix;
523 
524 private:
525  std::unique_ptr<ToolBase> create_tool(ToolID tool_id);
526 };
527 } // namespace horizon
horizon::ToolSettings
Definition: core.hpp:104
horizon::Polygon
Polygon used in Padstack, Package and Board for specifying filled Regions.
Definition: polygon.hpp:27
horizon::ToolBase::can_begin
virtual bool can_begin()
Definition: core.hpp:179
horizon::Core::rebuild
virtual void rebuild(bool from_undo=false)
Expands the non-working document.
Definition: core.cpp:312
horizon::ToolBase::update
virtual ToolResponse update(const ToolArgs &args)=0
Gets called whenever the user generated some sort of input.
horizon::ToolBase::begin
virtual ToolResponse begin(const ToolArgs &args)=0
Gets called right after the constructor has finished.
horizon::Core::get_meta
virtual json get_meta()
Definition: core.cpp:388
horizon::Line
Graphical line.
Definition: line.hpp:19
horizon::Rules
Definition: rules.hpp:44
horizon::ImpInterface
Definition: imp_interface.hpp:7
horizon::Core::SearchResult
Definition: core.hpp:358
horizon::ToolBase::handles_esc
virtual bool handles_esc()
Definition: core.hpp:195
horizon::ToolBase::is_specific
virtual bool is_specific()
Definition: core.hpp:187
horizon::Dimension
Definition: dimension.hpp:12
horizon::Arc
Graphical arc.
Definition: arc.hpp:20
horizon::ToolSettingsProxy
Definition: core.hpp:113
horizon::Keepout
Definition: keepout.hpp:9
horizon::ToolResponse::change_layer
static ToolResponse change_layer(int l)
Use this for changing the work layer from a Tool.
Definition: core.hpp:84
horizon::Block
A block is one level of hierarchy in the netlist.
Definition: block.hpp:26
horizon::LayerProvider
Definition: layer_provider.hpp:7
horizon::Coord< int64_t >
horizon::Core::signal_request_save_meta
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:423
horizon::Core
Where Tools and and documents meet.
Definition: core.hpp:240
horizon::Target
Definition: target.hpp:6
horizon::Junction
A Junction is a point in 2D-Space.
Definition: junction.hpp:25
horizon::PropertyMeta
Definition: core_properties.hpp:77
horizon::Text
Used wherever a user-editable text is needed.
Definition: text.hpp:19
horizon::Core::SearchQuery
Definition: core.hpp:351
nlohmann::basic_json
a class to store JSON values
Definition: json.hpp:161
horizon::ToolBase
Common interface for all Tools.
Definition: core.hpp:141
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:403
horizon::ToolResponse
To signal back to the core what the Tool did, a Tool returns a ToolResponse.
Definition: core.hpp:52
horizon::ToolArgs
This is what a Tool receives when the user did something.
Definition: core.hpp:26
horizon::Placement
Definition: placement.hpp:8
horizon::ToolResponse::end
static ToolResponse end()
Use this if you're done.
Definition: core.hpp:67
horizon::Hole
A hole with diameter and position, that's it.
Definition: hole.hpp:19
horizon::Core::HistoryItem
Definition: core.hpp:498
horizon::PropertyValue
Definition: core_properties.hpp:7
horizon::ToolResponse::next
static ToolResponse next(ToolID t, std::unique_ptr< ToolData > data=nullptr)
If you want another Tool to be launched you've finished, use this one.
Definition: core.hpp:94