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