Horizon
imp.hpp
1 #pragma once
2 #include "core/clipboard.hpp"
3 #include "core/core.hpp"
4 #include "imp_interface.hpp"
5 #include "keyseq_dialog.hpp"
6 #include "main_window.hpp"
7 #include "pool/pool.hpp"
8 #include "preferences/preferences.hpp"
9 #include "selection_filter_dialog.hpp"
10 #include "util/window_state_store.hpp"
11 #include "widgets/spin_button_dim.hpp"
12 #include "widgets/warnings_box.hpp"
13 #include "action.hpp"
14 #include "nlohmann/json.hpp"
15 #include "search/searcher.hpp"
16 #include <zmq.hpp>
17 #include "util/item_set.hpp"
18 #include "canvas/canvas_gl.hpp"
19 #include "grid_controller.hpp"
20 #include "util/action_label.hpp"
21 #include <optional>
22 #include "util/win32_undef.hpp"
23 
24 namespace horizon {
25 
26 class PoolParams {
27 public:
28  PoolParams(const std::string &bp, const std::string &cp = "") : base_path(bp), cache_path(cp)
29  {
30  }
31  std::string base_path;
32  std::string cache_path;
33 };
34 
35 std::unique_ptr<Pool> make_pool(const PoolParams &params);
36 
37 class ImpBase {
38  friend class ImpInterface;
39 
40 public:
41  ImpBase(const PoolParams &params);
42  void run(int argc, char *argv[]);
43  virtual void handle_tool_change(ToolID id);
44  virtual void construct() = 0;
45  void canvas_update_from_pp();
46  virtual ~ImpBase()
47  {
48  }
49  void set_read_only(bool v);
50 
51  std::set<ObjectRef> highlights;
52  virtual void update_highlights(){};
53 
55  public:
56  enum class Flag {
57  DEFAULT,
58  HAS_OTHERS,
59  WORK_LAYER_ONLY_ENABLED,
60  };
62  {
63  }
64  SelectionFilterInfo(const std::vector<int> &l, Flag fl) : layers(l), flags(fl)
65  {
66  }
67  std::vector<int> layers;
68  Flag flags = Flag::DEFAULT;
69  };
70 
71  virtual std::map<ObjectType, SelectionFilterInfo> get_selection_filter_info() const;
72  virtual bool is_layered() const
73  {
74  return false;
75  };
76 
77 protected:
78  MainWindow *main_window;
79  CanvasGL *canvas;
80  class PropertyPanels *panels;
81  WarningsBox *warnings_box;
82  class ToolPopover *tool_popover;
83  Gtk::Menu *context_menu = nullptr;
84  std::unique_ptr<SelectionFilterDialog> selection_filter_dialog;
85  std::optional<GridController> grid_controller;
86 
87  std::unique_ptr<Pool> pool;
88  class Core *core = nullptr;
89  std::unique_ptr<ClipboardManager> clipboard = nullptr;
90  std::unique_ptr<KeySequenceDialog> key_sequence_dialog = nullptr;
91  std::unique_ptr<ImpInterface> imp_interface = nullptr;
92  Glib::RefPtr<Glib::Binding> grid_spacing_binding;
93 
94  std::map<ActionToolID, ActionConnection> action_connections;
95  ActionConnection &connect_action(ToolID tool_id, std::function<void(const ActionConnection &)> cb);
96  ActionConnection &connect_action(ToolID tool_id);
97  ActionConnection &connect_action(ActionID action_id, std::function<void(const ActionConnection &)> cb);
98 
99  class RulesWindow *rules_window = nullptr;
100 
101  zmq::context_t zctx;
102  zmq::socket_t sock_broadcast_rx;
103  zmq::socket_t sock_project;
104  bool sockets_connected = false;
105  int mgr_pid = -1;
106  bool no_update = false;
107  bool distraction_free = false;
108 
109  virtual void canvas_update() = 0;
110  virtual void expand_selection_for_property_panel(std::set<SelectableRef> &sel_extra,
111  const std::set<SelectableRef> &sel);
112  void handle_selection_changed(void);
113  bool handle_key_press(GdkEventKey *key_event);
114  void handle_cursor_move(const Coordi &pos);
115  bool handle_click(GdkEventButton *button_event);
116  bool handle_click_release(GdkEventButton *button_event);
117  bool handle_context_menu(GdkEventButton *button_event);
118  void tool_process(ToolResponse &resp);
119  void tool_begin(ToolID id, bool override_selection = false, const std::set<SelectableRef> &sel = {},
120  std::unique_ptr<ToolData> data = nullptr);
121  void add_tool_button(ToolID id, const std::string &label, bool left = true);
122  void handle_warning_selected(const Coordi &pos);
123  virtual bool handle_broadcast(const json &j);
124  bool handle_close(GdkEventAny *ev);
125  json send_json(const json &j);
126 
127  bool trigger_action(const ActionToolID &action);
128  bool trigger_action(ActionID aid);
129  bool trigger_action(ToolID tid);
130 
131  void add_tool_action(ToolID tid, const std::string &action);
132  void add_tool_action(ActionID aid, const std::string &action);
133  void add_hamburger_menu();
134 
135  Preferences preferences;
136 
137  virtual CanvasPreferences *get_canvas_preferences()
138  {
139  return &preferences.canvas_non_layer;
140  }
141  virtual void apply_preferences();
142 
143  std::unique_ptr<WindowStateStore> state_store = nullptr;
144 
145  virtual void handle_maybe_drag();
146 
147  virtual ActionCatalogItem::Availability get_editor_type_for_action() const = 0;
148  ObjectType get_editor_type() const;
149 
150  void layer_up_down(bool up);
151  void goto_layer(int layer);
152 
153  Gtk::Button *create_action_button(ActionToolID action);
154 
155  void set_action_sensitive(ActionToolID, bool v);
156  bool get_action_sensitive(ActionToolID) const;
157  virtual void update_action_sensitivity();
158 
159  typedef sigc::signal<void> type_signal_action_sensitive;
160  type_signal_action_sensitive signal_action_sensitive()
161  {
162  return s_signal_action_sensitive;
163  }
164 
165  virtual std::string get_hud_text(std::set<SelectableRef> &sel);
166  std::string get_hud_text_for_component(const Component *comp);
167  std::string get_hud_text_for_net(const Net *net);
168 
169  void set_monitor_files(const std::set<std::string> &files);
170  void set_monitor_items(const ItemSet &items);
171  virtual void update_monitor()
172  {
173  }
174  void edit_pool_item(ObjectType type, const UUID &uu);
175 
176  void parameter_window_add_polygon_expand(class ParameterWindow *parameter_window);
177 
178  bool read_only = false;
179 
180  void tool_update_data(std::unique_ptr<ToolData> &data);
181 
182  virtual void search_center(const Searcher::SearchResult &res);
183  virtual ActionToolID get_doubleclick_action(ObjectType type, const UUID &uu);
184 
185  Glib::RefPtr<Gio::Menu> hamburger_menu;
186 
187  json m_meta;
188  void load_meta();
189  static const std::string meta_suffix;
190 
191  virtual void get_save_meta(json &j)
192  {
193  }
194 
195  void set_window_title(const std::string &s);
196  void set_window_title_from_block();
197 
198  void update_view_hints();
199  virtual std::vector<std::string> get_view_hints();
200 
201  Glib::RefPtr<Gio::Menu> view_options_menu;
202 
203  virtual Searcher *get_searcher_ptr()
204  {
205  return nullptr;
206  }
207 
208  bool has_searcher()
209  {
210  return get_searcher_ptr();
211  }
212 
213  Searcher &get_searcher()
214  {
215  auto s = get_searcher_ptr();
216  if (!s)
217  throw std::runtime_error("not implemented");
218  return *s;
219  }
220 
221  class ActionButton &add_action_button(ActionToolID action);
222  class ActionButtonMenu &add_action_button_menu(const char *icon_name);
223  class ActionButton &add_action_button_polygon();
224  class ActionButton &add_action_button_line();
225 
226  virtual ToolID get_tool_for_drag_move(bool ctrl, const std::set<SelectableRef> &sel) const;
227  void force_end_tool();
228 
229 private:
230  void fix_cursor_pos();
231  Glib::RefPtr<Gio::FileMonitor> preferences_monitor;
232  void handle_drag(bool ctrl);
233  void update_selection_label();
234  std::string get_tool_settings_filename(ToolID id);
235 
236  void hud_update();
237 
238  std::map<std::string, Glib::RefPtr<Gio::FileMonitor>> file_monitors;
239 
240  void handle_file_changed(const Glib::RefPtr<Gio::File> &file1, const Glib::RefPtr<Gio::File> &file2,
241  Gio::FileMonitorEvent ev);
242 
243  ActionConnection &connect_action(ActionID action_id, ToolID tool_id,
244  std::function<void(const ActionConnection &)> cb);
245 
246  void create_context_menu(Gtk::Menu *parent, const std::set<SelectableRef> &sel);
247  Gtk::MenuItem *create_context_menu_item(ActionToolID act);
248 
249  KeySequence keys_current;
250  enum class MatchResult { NONE, PREFIX, COMPLETE };
251  MatchResult keys_match(const KeySequence &keys) const;
252  bool handle_action_key(GdkEventKey *ev);
253  void handle_tool_action(const ActionConnection &conn);
254  void handle_select_polygon(const ActionConnection &a);
255 
256  void handle_search();
257  void search_go(int dir);
258  std::list<Searcher::SearchResult> search_results;
259  unsigned int search_result_current = 0;
260  void update_search_markers();
261  void update_search_types_label();
262  void set_search_mode(bool enabled, bool focus = true);
263  std::map<Searcher::Type, Gtk::CheckButton *> search_check_buttons;
264 
265  class LogWindow *log_window = nullptr;
266  std::set<SelectableRef> selection_for_drag_move;
267  Coordf cursor_pos_drag_begin;
268  Coordi cursor_pos_grid_drag_begin;
269 
270  std::map<ActionToolID, bool> action_sensitivity;
271  type_signal_action_sensitive s_signal_action_sensitive;
272 
273  bool property_panel_has_focus();
274 
275  sigc::connection initial_view_all_conn;
276 
277  bool sockets_broken = false;
278  void show_sockets_broken_dialog(const std::string &msg = "");
279  bool needs_autosave = false;
280  bool queue_autosave = false;
281 
282  void update_property_panels();
283  std::map<CanvasGL::SelectionTool, CanvasGL::SelectionQualifier> selection_qualifiers;
284  std::list<class ActionButtonBase *> action_buttons;
285 
286  Glib::RefPtr<Gio::SimpleAction> bottom_view_action;
287  Glib::RefPtr<Gio::SimpleAction> distraction_free_action;
288 
289  int left_panel_width = 0;
290 
291  void tool_bar_set_actions(const std::vector<ActionLabelInfo> &labels);
292  void tool_bar_append_action(InToolActionID action1, InToolActionID action2, const std::string &s);
293  void tool_bar_clear_actions();
294  InToolKeySequencesPreferences in_tool_key_sequeces_preferences;
295  std::vector<ActionLabelInfo> in_tool_action_label_infos;
296 
297  void show_preferences(std::optional<std::string> page = std::nullopt);
298 
299  void init_search();
300  void init_key();
301  void init_action();
302 
303  void handle_pan_action(const ActionConnection &c);
304  void handle_zoom_action(const ActionConnection &c);
305 
306  std::string get_complete_display_name(const SelectableRef &sr);
307 
308  void set_flip_view(bool flip);
309  void apply_arrow_keys();
310  void check_version();
311 };
312 } // namespace horizon
horizon::ImpBase::SelectionFilterInfo
Definition: imp.hpp:54
horizon::ToolPopover
Definition: tool_popover.hpp:8
horizon::ImpInterface
Definition: imp_interface.hpp:12
horizon::RulesWindow
Definition: rules_window.hpp:13
horizon::CanvasGL
Definition: canvas_gl.hpp:18
horizon::MainWindow
Definition: main_window.hpp:7
horizon::WarningsBox
Definition: warnings_box.hpp:7
horizon::Coord
Your typical coordinate class.
Definition: common.hpp:78
horizon::PoolParams
Definition: imp.hpp:26
horizon::Core
Where Tools and and documents meet.
Definition: core.hpp:42
nlohmann::basic_json
a class to store JSON values
Definition: json.hpp:166
horizon::ToolResponse
To signal back to the core what the Tool did, a Tool returns a ToolResponse.
Definition: tool.hpp:42
horizon::ImpBase
Definition: imp.hpp:37
horizon::ActionConnection
Definition: action.hpp:137
horizon::PropertyPanels
Definition: property_panels.hpp:8