Debian Codegraph @

layla/ligand_editor_canvas/core.hpp

raw
1 /* layla/ligand_editor_canvas/core.hpp
2 *
3 * Copyright 2023 by Global Phasing Ltd.
4 * Author: Jakub Smulski
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or (at
9 * your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * 02110-1301, USA
20 */
21
22 #ifndef COOT_LIGAND_EDITOR_CANVAS_CORE_HPP
23 #define COOT_LIGAND_EDITOR_CANVAS_CORE_HPP
24
25 #include <rdkit/GraphMol/RWMol.h>
26 #include <rdkit/GraphMol/SmilesParse/SmilesWrite.h>
27
28 #include <memory>
29 #include <vector>
30 #include <optional>
31 #include "render.hpp"
32 #include "model.hpp"
33 #include "tools.hpp"
34
35 #ifndef __EMSCRIPTEN__
36 #include <gtk/gtk.h>
37 #include <pango/pango-layout.h>
38
39 // GObject declaration
40 G_BEGIN_DECLS
41
42 #define COOT_LIGAND_EDITOR_CANVAS_TYPE (coot_ligand_editor_canvas_get_type ())
43 G_DECLARE_FINAL_TYPE (CootLigandEditorCanvas, coot_ligand_editor_canvas, COOT, COOT_LIGAND_EDITOR_CANVAS, GtkWidget)
44
45 G_END_DECLS
46
47 #define _LIGAND_EDITOR_SIGNAL_EMIT(_instance_, _signal_name_) g_signal_emit((gpointer)(_instance_),impl::_signal_name_,0)
48 #define _LIGAND_EDITOR_SIGNAL_EMIT_ARG(_instance_, _signal_name_, ...) g_signal_emit((gpointer)(_instance_),impl::_signal_name_,0,__VA_ARGS__)
49 #else // __EMSCRIPTEN__ defined
50 #include "../../lhasa/glog_replacement.hpp"
51 #include <sigc++-3.0/sigc++/sigc++.h>
52 #define _LIGAND_EDITOR_SIGNAL_EMIT(_instance_, _signal_name_) _instance_->_signal_name_.emit()
53 #define _LIGAND_EDITOR_SIGNAL_EMIT_ARG(_instance_, _signal_name_, ...) _instance_->_signal_name_.emit(__VA_ARGS__)
54 #endif
55
56
57 namespace coot::ligand_editor_canvas::impl {
58
59 #ifndef __EMSCRIPTEN__
60 inline guint status_updated_signal;
61 inline guint scale_changed_signal;
62 inline guint smiles_changed_signal;
63 inline guint molecule_deleted_signal;
64 inline guint qed_info_updated_signal;
65 #endif
66
67 /// This is here as a workaround.
68 ///
69 /// GObject expects the first data field in the struct
70 /// to be an instance of the base class.
71 /// If this condition is not met, you end up with a SEGFAULT.
72 ///
73 /// Thus CootLigandEditorCanvasPriv must first inherit from
74 /// a struct that satisfies this requirement.
75 /// This is how you can use (multiple) inheritance while
76 /// keeping the ABI happy.
77 struct CootLigandEditorCanvasPrivBase {
78 #ifndef __EMSCRIPTEN__
79 GtkWidget parent;
80 #else // __EMSCRIPTEN__ defined
81 // Lhasa-specific includes/definitions
82 #endif
83 };
84
85
86 struct StateSnapshot {
87 std::unique_ptr<std::vector<std::optional<CanvasMolecule>>> molecules;
88 std::unique_ptr<std::vector<std::optional<std::shared_ptr<RDKit::RWMol>>>> rdkit_molecules;
89
90 StateSnapshot(const WidgetCoreData& core_data);
91 };
92
93 /// Used for widget's struct as a base class.
94 /// Useful for exposing inner state to the active tool.
95 struct WidgetCoreData {
96 typedef std::vector<std::unique_ptr<StateSnapshot>> StateStack;
97 typedef std::pair<CanvasMolecule::AtomOrBond,unsigned int> AtomOrBondWithMolIdx;
98 typedef std::optional<AtomOrBondWithMolIdx> MaybeAtomOrBondWithMolIdx;
99
100 /// Max length of edition history
101 const static unsigned int MAX_STATE_STACK_LENGTH;
102 /// Numbers of elements to be removed from the `state_stack`
103 /// when its' maximum length gets exceeded
104 const static unsigned int STATE_STACK_TRIM_BATCH_SIZE;
105
106 protected:
107
108 /// Current position in the state_stack, counting from the back.
109 /// -1 if we're "fresh", that is, no action has been undone
110 int state_stack_pos;
111 /// For Edit->Undo/Redo.
112 /// To remember internal states
113 std::unique_ptr<StateStack> state_stack;
114
115 /// A snapshot preserving internal state
116 /// from before the current edition began.
117 /// nullptr if no edition is being done at the moment.
118 std::unique_ptr<StateSnapshot> state_before_edition;
119
120 public:
121 /// molecules on the screen
122 std::unique_ptr<std::vector<std::optional<CanvasMolecule>>> molecules;
123 /// molecules (RDKit)
124 std::unique_ptr<std::vector<std::optional<std::shared_ptr<RDKit::RWMol>>>> rdkit_molecules;
125 /// Bond being currently created via click'n'drag
126 std::optional<CurrentlyCreatedBond> currently_created_bond;
127
128 float scale;
129
130 bool allow_invalid_molecules;
131
132 DisplayMode display_mode;
133
134 void render(Renderer&);
135
136 /// Does Edit->Undo
137 void undo_edition();
138
139 /// Does Edit->Redo
140 void redo_edition();
141
142 /// Cancels the current edition
143 /// and resets the state to the current snapshot.
144 void rollback_current_edition();
145
146 /// Checks if we're currently inside an edition operation
147 bool is_in_edition();
148
149 /// Snapshots the current state and opens new edition.
150 ///
151 /// This function must be called if one wishes
152 /// to integrate any kind of state-altering operation
153 /// with the Edit->Undo/Redo system
154 void begin_edition();
155
156 /// Completes the current edition.
157 /// Moves the current snapshot to the state history stack.
158 ///
159 /// This function must be called after any kind
160 /// of state-altering operation if one wishes
161 /// to integrate it with the Edit->Undo/Redo system
162 void finalize_edition();
163
164 /// Goes over all molecules stored in the widget
165 /// and calls CanvasMolecule::resolve_click(x,y) on each of them
166 /// until an object matching the coordinates is found.
167 /// The index number indicates which molecule the object comes from.
168 /// If nothing matches the coordinates, nullopt is returned.
169 MaybeAtomOrBondWithMolIdx resolve_click(int x, int y) const noexcept;
170
171 void delete_molecule_with_idx(unsigned int idx, bool integrate_with_edit_undo = true) noexcept;
172
173 /// Emits 'status-updated' signal.
174 void update_status(const char* status_text) const noexcept;
175
176 /// Emits the 'smiles-changed' and 'qed-info-updated' signal
177 void emit_mutation_signals() const noexcept;
178
179 coot::ligand_editor_canvas::SmilesMap build_smiles() const;
180
181 unsigned int get_molecule_count_impl() const noexcept;
182 /// Returns -1 if none
183 int get_first_molecule_idx() const noexcept;
184
185 /// Abstraction over gtk_widget_queue_draw
186 void queue_redraw() const noexcept;
187 /// Abstraction over gtk_widget_queue_resize
188 void queue_resize() const noexcept;
189 };
190
191 /// This is the private struct for GObject
192 struct CootLigandEditorCanvasPriv : CootLigandEditorCanvasPrivBase, impl::WidgetCoreData {
193
194 std::unique_ptr<ActiveTool> active_tool;
195 };
196
197
198 } // namespace coot::ligand_editor_canvas::impl
199
200 #ifdef __EMSCRIPTEN__
201
202 //Forward declaration
203 namespace emscripten {
204 struct val;
205 }
206
207 /// For Lhasa
208 struct CootLigandEditorCanvas : coot::ligand_editor_canvas::impl::CootLigandEditorCanvasPriv {
209
210
211 sigc::signal<void(const char*)> status_updated_signal;
212 sigc::signal<void(float)> scale_changed_signal;
213 sigc::signal<void()> smiles_changed_signal;
214 sigc::signal<void(int)> molecule_deleted_signal;
215 sigc::signal<void(int, const coot::ligand_editor_canvas::CanvasMolecule::QEDInfo*)> qed_info_updated_signal;
216 // Lhasa-only signals (for JS handlers):
217 sigc::signal<void()> queue_redraw_signal;
218 sigc::signal<void()> queue_resize_signal;
219
220 public:
221
222 enum class MeasurementDirection :unsigned char {
223 HORIZONTAL = 0,
224 VERTICAL = 1
225 };
226
227 struct SizingInfo {
228 int requested_size;
229 };
230
231 // Implemented at 'ligand_editor_canvas.cpp'
232 CootLigandEditorCanvas() noexcept;
233 // Implemented at 'ligand_editor_canvas.cpp'
234 ~CootLigandEditorCanvas() noexcept;
235
236 void set_active_tool(std::unique_ptr<coot::ligand_editor_canvas::ActiveTool> active_tool);
237 /// Returns the ID of new molecule or '-1' on error
238 int append_molecule(std::shared_ptr<RDKit::RWMol> rdkit_mol) noexcept;
239 void update_molecule_from_smiles(unsigned int molecule_idx, const std::string& smiles);
240 void set_scale(float scale) noexcept;
241 float get_scale() noexcept;
242 void undo() noexcept;
243 void redo() noexcept;
244 // const RDKit::ROMol& get_rdkit_molecule(unsigned int index) noexcept;
245 unsigned int get_molecule_count() noexcept;
246 unsigned int get_idx_of_first_molecule() noexcept;
247 unsigned int get_max_molecule_idx() noexcept;
248 void set_allow_invalid_molecules(bool value) noexcept;
249 bool get_allow_invalid_molecules() noexcept;
250 coot::ligand_editor_canvas::DisplayMode get_display_mode() noexcept;
251 void set_display_mode(coot::ligand_editor_canvas::DisplayMode value) noexcept;
252 coot::ligand_editor_canvas::SmilesMap get_smiles() noexcept;
253 std::string get_smiles_for_molecule(unsigned int molecule_idx) noexcept;
254 std::string get_pickled_molecule(unsigned int molecule_idx) noexcept;
255 std::string get_pickled_molecule_base64(unsigned int molecule_idx) noexcept;
256 void clear_molecules() noexcept;
257
258 /// For connecting javascript handlers to signals
259 void connect(std::string signal_name, emscripten::val callback);
260
261 // Implemented at 'ligand_editor_canvas.cpp'
262 SizingInfo measure(MeasurementDirection orientation) const noexcept;
263 // Implemented at 'ligand_editor_canvas.cpp'
264 void on_hover(double x, double y, bool alt_pressed, bool control_pressed);
265 // Implemented at 'ligand_editor_canvas.cpp'
266 void on_scroll(double dx, double dy, bool control_pressed);
267 // Implemented at 'ligand_editor_canvas.cpp'
268 void on_left_click(double x, double y, bool alt_pressed, bool control_pressed, bool shift_pressed);
269 // Implemented at 'ligand_editor_canvas.cpp'
270 void on_left_click_released(double x, double y, bool alt_pressed, bool control_pressed, bool shift_pressed);
271 // Implemented at 'ligand_editor_canvas.cpp'
272 void on_right_click(double x, double y, bool alt_pressed, bool control_pressed, bool shift_pressed);
273 // Implemented at 'ligand_editor_canvas.cpp'
274 void on_right_click_released(double x, double y, bool alt_pressed, bool control_pressed, bool shift_pressed);
275
276
277 };
278 #endif
279
280
281 #endif //#define COOT_LIGAND_EDITOR_CANVAS_CORE_HPP