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 
23 #ifdef G_OS_WIN32
24 #undef DELETE
25 #undef DUPLICATE
26 #undef ERROR
27 #endif
28 
29 namespace horizon {
30 
31 class PoolParams {
32 public:
33  PoolParams(const std::string &bp, const std::string &cp = "") : base_path(bp), cache_path(cp)
34  {
35  }
36  std::string base_path;
37  std::string cache_path;
38 };
39 
40 std::unique_ptr<Pool> make_pool(const PoolParams &params);
41 
42 class ImpBase {
43  friend class ImpInterface;
44 
45 public:
46  ImpBase(const PoolParams &params);
47  void run(int argc, char *argv[]);
48  virtual void handle_tool_change(ToolID id);
49  virtual void construct() = 0;
50  void canvas_update_from_pp();
51  virtual ~ImpBase()
52  {
53  }
54  void set_read_only(bool v);
55 
56  std::set<ObjectRef> highlights;
57  virtual void update_highlights(){};
58 
60  public:
62  {
63  }
64  SelectionFilterInfo(const std::vector<int> &l, bool o) : layers(l), has_others(o)
65  {
66  }
67  std::vector<int> layers;
68  bool has_others = false;
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 
224  virtual ToolID get_tool_for_drag_move(bool ctrl, const std::set<SelectableRef> &sel) const;
225 
226 private:
227  void fix_cursor_pos();
228  Glib::RefPtr<Gio::FileMonitor> preferences_monitor;
229  void handle_drag(bool ctrl);
230  void update_selection_label();
231  std::string get_tool_settings_filename(ToolID id);
232 
233  void hud_update();
234 
235  std::map<std::string, Glib::RefPtr<Gio::FileMonitor>> file_monitors;
236 
237  void handle_file_changed(const Glib::RefPtr<Gio::File> &file1, const Glib::RefPtr<Gio::File> &file2,
238  Gio::FileMonitorEvent ev);
239 
240  ActionConnection &connect_action(ActionID action_id, ToolID tool_id,
241  std::function<void(const ActionConnection &)> cb);
242 
243  void create_context_menu(Gtk::Menu *parent, const std::set<SelectableRef> &sel);
244  Gtk::MenuItem *create_context_menu_item(ActionToolID act);
245 
246  KeySequence keys_current;
247  bool keys_match(const KeySequence &keys) const;
248  bool handle_action_key(GdkEventKey *ev);
249  void handle_tool_action(const ActionConnection &conn);
250  void handle_select_polygon(const ActionConnection &a);
251 
252  void handle_search();
253  void search_go(int dir);
254  std::list<Searcher::SearchResult> search_results;
255  unsigned int search_result_current = 0;
256  void update_search_markers();
257  void update_search_types_label();
258  void set_search_mode(bool enabled, bool focus = true);
259  std::map<Searcher::Type, Gtk::CheckButton *> search_check_buttons;
260 
261  class LogWindow *log_window = nullptr;
262  std::set<SelectableRef> selection_for_drag_move;
263  Coordf cursor_pos_drag_begin;
264  Coordi cursor_pos_grid_drag_begin;
265 
266  std::map<ActionToolID, bool> action_sensitivity;
267  type_signal_action_sensitive s_signal_action_sensitive;
268 
269  GdkModifierType grid_fine_modifier = GDK_MOD1_MASK;
270 
271  bool property_panel_has_focus();
272 
273  sigc::connection initial_view_all_conn;
274 
275  bool sockets_broken = false;
276  void show_sockets_broken_dialog(const std::string &msg = "");
277  bool needs_autosave = false;
278  bool queue_autosave = false;
279 
280  void update_property_panels();
281  std::map<CanvasGL::SelectionTool, CanvasGL::SelectionQualifier> selection_qualifiers;
282  std::list<class ActionButtonBase *> action_buttons;
283 
284  Glib::RefPtr<Gio::SimpleAction> bottom_view_action;
285  Glib::RefPtr<Gio::SimpleAction> distraction_free_action;
286 
287  int left_panel_width = 0;
288 
289  void tool_bar_set_actions(const std::vector<ActionLabelInfo> &labels);
290  void tool_bar_append_action(InToolActionID action1, InToolActionID action2, const std::string &s);
291  void tool_bar_clear_actions();
292  InToolKeySequencesPreferences in_tool_key_sequeces_preferences;
293  std::vector<ActionLabelInfo> in_tool_action_label_infos;
294 };
295 } // namespace horizon
horizon::ImpBase::SelectionFilterInfo
Definition: imp.hpp:59
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:75
horizon::PoolParams
Definition: imp.hpp:31
horizon::Core
Where Tools and and documents meet.
Definition: core.hpp:47
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:42
horizon::ActionConnection
Definition: action.hpp:129
horizon::PropertyPanels
Definition: property_panels.hpp:8