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 
19 #ifdef G_OS_WIN32
20 #undef DELETE
21 #undef DUPLICATE
22 #undef ERROR
23 #endif
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:
58  {
59  }
60  SelectionFilterInfo(const std::vector<int> &l, bool o) : layers(l), has_others(o)
61  {
62  }
63  std::vector<int> layers;
64  bool has_others = false;
65  };
66 
67  virtual std::map<ObjectType, SelectionFilterInfo> get_selection_filter_info() const;
68  virtual bool is_layered() const
69  {
70  return false;
71  };
72 
73 protected:
74  MainWindow *main_window;
75  class CanvasGL *canvas;
76  class PropertyPanels *panels;
77  WarningsBox *warnings_box;
78  class ToolPopover *tool_popover;
79  Gtk::Menu *context_menu = nullptr;
80  SpinButtonDim *grid_spin_button;
81  std::unique_ptr<SelectionFilterDialog> selection_filter_dialog;
82 
83  std::unique_ptr<Pool> pool;
84  class Core *core = nullptr;
85  std::unique_ptr<ClipboardManager> clipboard = nullptr;
86  std::unique_ptr<KeySequenceDialog> key_sequence_dialog = nullptr;
87  std::unique_ptr<ImpInterface> imp_interface = nullptr;
88  Glib::RefPtr<Glib::Binding> grid_spacing_binding;
89 
90  std::map<std::pair<ActionID, ToolID>, ActionConnection> action_connections;
91  ActionConnection &connect_action(ToolID tool_id, std::function<void(const ActionConnection &)> cb);
92  ActionConnection &connect_action(ToolID tool_id);
93  ActionConnection &connect_action(ActionID action_id, std::function<void(const ActionConnection &)> cb);
94 
95  class RulesWindow *rules_window = nullptr;
96 
97  zmq::context_t zctx;
98  zmq::socket_t sock_broadcast_rx;
99  zmq::socket_t sock_project;
100  bool sockets_connected = false;
101  int mgr_pid = -1;
102  bool no_update = false;
103 
104  virtual void canvas_update() = 0;
105  virtual void expand_selection_for_property_panel(std::set<SelectableRef> &sel_extra,
106  const std::set<SelectableRef> &sel);
107  void handle_selection_changed(void);
108  bool handle_key_press(GdkEventKey *key_event);
109  void handle_cursor_move(const Coordi &pos);
110  bool handle_click(GdkEventButton *button_event);
111  bool handle_click_release(GdkEventButton *button_event);
112  bool handle_context_menu(GdkEventButton *button_event);
113  void tool_process(ToolResponse &resp);
114  void tool_begin(ToolID id, bool override_selection = false, const std::set<SelectableRef> &sel = {},
115  std::unique_ptr<ToolData> data = nullptr);
116  void add_tool_button(ToolID id, const std::string &label, bool left = true);
117  void handle_warning_selected(const Coordi &pos);
118  virtual bool handle_broadcast(const json &j);
119  bool handle_close(GdkEventAny *ev);
120  json send_json(const json &j);
121 
122  bool trigger_action(const std::pair<ActionID, ToolID> &action);
123  bool trigger_action(ActionID aid);
124  bool trigger_action(ToolID tid);
125 
126  void add_tool_action(ToolID tid, const std::string &action);
127  void add_hamburger_menu();
128 
129  Preferences preferences;
130 
131  virtual CanvasPreferences *get_canvas_preferences()
132  {
133  return &preferences.canvas_non_layer;
134  }
135  virtual void apply_preferences();
136 
137  std::unique_ptr<WindowStateStore> state_store = nullptr;
138 
139  virtual void handle_maybe_drag();
140 
141  virtual ActionCatalogItem::Availability get_editor_type_for_action() const = 0;
142  virtual ObjectType get_editor_type() const = 0;
143 
144  void layer_up_down(bool up);
145  void goto_layer(int layer);
146 
147  Gtk::Button *create_action_button(std::pair<ActionID, ToolID> action);
148 
149  void set_action_sensitive(std::pair<ActionID, ToolID>, bool v);
150  bool get_action_sensitive(std::pair<ActionID, ToolID>) const;
151  virtual void update_action_sensitivity();
152 
153  typedef sigc::signal<void> type_signal_action_sensitive;
154  type_signal_action_sensitive signal_action_sensitive()
155  {
156  return s_signal_action_sensitive;
157  }
158 
159  virtual std::string get_hud_text(std::set<SelectableRef> &sel);
160  std::string get_hud_text_for_component(const Component *comp);
161  std::string get_hud_text_for_net(const Net *net);
162 
163  void set_monitor_files(const std::set<std::string> &files);
164  void set_monitor_items(const ItemSet &items);
165  virtual void update_monitor()
166  {
167  }
168  void edit_pool_item(ObjectType type, const UUID &uu);
169 
170  void parameter_window_add_polygon_expand(class ParameterWindow *parameter_window);
171 
172  bool read_only = false;
173 
174  void tool_update_data(std::unique_ptr<ToolData> &data);
175 
176  virtual void search_center(const Searcher::SearchResult &res);
177  virtual std::pair<ActionID, ToolID> get_doubleclick_action(ObjectType type, const UUID &uu);
178 
179  Glib::RefPtr<Gio::Menu> hamburger_menu;
180 
181  json m_meta;
182  void load_meta();
183  static const std::string meta_suffix;
184 
185  virtual void get_save_meta(json &j)
186  {
187  }
188 
189  void set_window_title(const std::string &s);
190  void set_window_title_from_block();
191 
192  void update_view_hints();
193  virtual std::vector<std::string> get_view_hints();
194 
195  virtual Searcher *get_searcher_ptr()
196  {
197  return nullptr;
198  }
199 
200  bool has_searcher()
201  {
202  return get_searcher_ptr();
203  }
204 
205  Searcher &get_searcher()
206  {
207  auto s = get_searcher_ptr();
208  if (!s)
209  throw std::runtime_error("not implemented");
210  return *s;
211  }
212 
213 private:
214  void fix_cursor_pos();
215  Glib::RefPtr<Gio::FileMonitor> preferences_monitor;
216  void handle_drag(bool ctrl);
217  void update_selection_label();
218  std::string get_tool_settings_filename(ToolID id);
219 
220  void hud_update();
221 
222  std::map<std::string, Glib::RefPtr<Gio::FileMonitor>> file_monitors;
223 
224  void handle_file_changed(const Glib::RefPtr<Gio::File> &file1, const Glib::RefPtr<Gio::File> &file2,
225  Gio::FileMonitorEvent ev);
226 
227  ActionConnection &connect_action(ActionID action_id, ToolID tool_id,
228  std::function<void(const ActionConnection &)> cb);
229 
230  void create_context_menu(Gtk::Menu *parent, const std::set<SelectableRef> &sel);
231 
232  KeySequence keys_current;
233  bool handle_action_key(GdkEventKey *ev);
234  void handle_tool_action(const ActionConnection &conn);
235  void handle_select_polygon(const ActionConnection &a);
236 
237  void handle_search();
238  void search_go(int dir);
239  std::list<Searcher::SearchResult> search_results;
240  unsigned int search_result_current = 0;
241  void update_search_markers();
242  void update_search_types_label();
243  void set_search_mode(bool enabled, bool focus = true);
244  std::map<Searcher::Type, Gtk::CheckButton *> search_check_buttons;
245 
246  class LogWindow *log_window = nullptr;
247  std::set<SelectableRef> selection_for_drag_move;
248  Coordf cursor_pos_drag_begin;
249  Coordi cursor_pos_grid_drag_begin;
250 
251  std::map<std::pair<ActionID, ToolID>, bool> action_sensitivity;
252  type_signal_action_sensitive s_signal_action_sensitive;
253 
254  GdkModifierType grid_fine_modifier = GDK_MOD1_MASK;
255 
256  bool property_panel_has_focus();
257 
258  sigc::connection initial_view_all_conn;
259 
260  bool sockets_broken = false;
261  void show_sockets_broken_dialog(const std::string &msg = "");
262  bool needs_autosave = false;
263  bool queue_autosave = false;
264 
265  void update_property_panels();
266 };
267 } // namespace horizon
horizon::ImpBase::SelectionFilterInfo
Definition: imp.hpp:55
horizon::ToolPopover
Definition: tool_popover.hpp:8
horizon::ImpInterface
Definition: imp_interface.hpp:7
horizon::RulesWindow
Definition: rules_window.hpp:13
horizon::CanvasGL
Definition: canvas_gl.hpp:15
horizon::MainWindow
Definition: main_window.hpp:7
horizon::SpinButtonDim
Definition: spin_button_dim.hpp:5
horizon::WarningsBox
Definition: warnings_box.hpp:7
horizon::Coord
Your typical coordinate class.
Definition: common.hpp:74
horizon::PoolParams
Definition: imp.hpp:27
horizon::Core
Where Tools and and documents meet.
Definition: core.hpp:47
nlohmann::basic_json
a class to store JSON values
Definition: json.hpp:165
horizon::ToolResponse
To signal back to the core what the Tool did, a Tool returns a ToolResponse.
Definition: tool.hpp:48
horizon::ImpBase
Definition: imp.hpp:38
horizon::ActionConnection
Definition: action.hpp:115
horizon::PropertyPanels
Definition: property_panels.hpp:8