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