Debian Codegraph @

layla/ligand_editor_canvas/core.cpp

raw
1 /* layla/ligand_editor_canvas/core.cpp
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 #include "core.hpp"
23 #include "../ligand_editor_canvas.hpp"
24 #include <iterator>
25 #ifdef __EMSCRIPTEN__
26 #include <emscripten/val.h>
27 #endif
28
29 using namespace coot::ligand_editor_canvas;
30 using namespace coot::ligand_editor_canvas::impl;
31
32 StateSnapshot::StateSnapshot(const WidgetCoreData& core_data) {
33 this->molecules = std::make_unique<std::vector<std::optional<CanvasMolecule>>>(*core_data.molecules);
34 const std::vector<std::optional<std::shared_ptr<RDKit::RWMol>>>& original_rdkit_molecules = *core_data.rdkit_molecules;
35 std::vector<std::optional<std::shared_ptr<RDKit::RWMol>>> copied_rdkit_molecules;
36 for(const auto& rwmol_shptr_opt: original_rdkit_molecules) {
37 if(rwmol_shptr_opt.has_value()) {
38 const auto& rwmol = *rwmol_shptr_opt->get();
39 copied_rdkit_molecules.push_back(std::make_shared<RDKit::RWMol>(RDKit::RWMol(rwmol)));
40 } else {
41 copied_rdkit_molecules.push_back(std::nullopt);
42 }
43 }
44 for(unsigned int i = 0;i < this->molecules->size();i++) {
45 auto& mol_opt = this->molecules->at(i);
46 if(mol_opt.has_value()) {
47 mol_opt->update_source_molecule(*copied_rdkit_molecules.at(i));
48 }
49 }
50 this->rdkit_molecules = std::make_unique<std::vector<std::optional<std::shared_ptr<RDKit::RWMol>>>>(std::move(copied_rdkit_molecules));
51 }
52
53 #ifdef __EMSCRIPTEN__
54 const unsigned int WidgetCoreData::MAX_STATE_STACK_LENGTH = 20;
55 const unsigned int WidgetCoreData::STATE_STACK_TRIM_BATCH_SIZE = 5;
56 #else
57 const unsigned int WidgetCoreData::MAX_STATE_STACK_LENGTH = 100;
58 const unsigned int WidgetCoreData::STATE_STACK_TRIM_BATCH_SIZE = 30;
59 #endif
60
61 WidgetCoreData::MaybeAtomOrBondWithMolIdx WidgetCoreData::resolve_click(int x, int y) const noexcept {
62 const auto* molecules_vec = this->molecules.get();
63 unsigned int idx = 0;
64 for(const auto& mol_opt: *molecules_vec) {
65 if(mol_opt.has_value()) {
66 auto result = mol_opt->resolve_click(x, y);
67 if(result.has_value()) {
68 return std::pair(result.value(),idx);
69 }
70 }
71 idx++;
72 }
73 return std::nullopt;
74 }
75
76 void WidgetCoreData::update_status(const char* status_text) const noexcept {
77 #ifndef __EMSCRIPTEN__
78 auto* widget_ptr = static_cast<const CootLigandEditorCanvasPriv*>(this);
79 #else
80 auto* widget_ptr = static_cast<const ::CootLigandEditorCanvas*>(this);
81 #endif
82 _LIGAND_EDITOR_SIGNAL_EMIT_ARG(widget_ptr, status_updated_signal, status_text);
83 }
84
85 void WidgetCoreData::emit_mutation_signals() const noexcept {
86 #ifndef __EMSCRIPTEN__
87 auto* widget_ptr = static_cast<const CootLigandEditorCanvasPriv*>(this);
88 #else
89 auto* widget_ptr = static_cast<const ::CootLigandEditorCanvas*>(this);
90 #endif
91 _LIGAND_EDITOR_SIGNAL_EMIT(widget_ptr, smiles_changed_signal);
92 const auto* molecules_vec = this->molecules.get();
93 for(unsigned int id = 0; id < molecules_vec->size(); id++) {
94 const auto& mol_opt = molecules_vec->at(id);
95 if(mol_opt.has_value()) {
96 auto qed_info = mol_opt->get_qed_info();
97 if(qed_info.has_value()) {
98 const auto& qed_info_value = *qed_info;
99 _LIGAND_EDITOR_SIGNAL_EMIT_ARG(widget_ptr, qed_info_updated_signal, id, &qed_info_value);
100 }
101 }
102 }
103 }
104
105
106 void WidgetCoreData::undo_edition() {
107 int state_stack_size = this->state_stack->size();
108 if(state_stack_size > this->state_stack_pos + 1) {
109 if(this->state_stack_pos == -1) {
110 // Special case.
111 // We need to backup the current state
112 // so that we can later go back to it via "Redo"
113 this->state_stack->push_back(std::make_unique<StateSnapshot>(*this));
114 this->state_stack_pos++;
115 }
116 this->state_stack_pos++;
117 auto iterator = this->state_stack->rbegin();
118 std::advance(iterator,this->state_stack_pos);
119 auto& target_state = *iterator;
120 this->molecules = std::make_unique<std::vector<std::optional<CanvasMolecule>>>(*target_state->molecules);
121 this->rdkit_molecules = std::make_unique<std::vector<std::optional<std::shared_ptr<RDKit::RWMol>>>>(*target_state->rdkit_molecules);
122 update_status("");
123 // this is done elsewhere, namely in the caller of this function
124 // auto* widget_ptr = static_cast<const CootLigandEditorCanvasPriv*>(this);
125 // gtk_widget_queue_draw(GTK_WIDGET(widget_ptr));
126 } else {
127 //g_debug("Nothing to be undone. Stack size: %zu Stack pos: %i",this->state_stack->size(),this->state_stack_pos);
128 update_status("Nothing to be undone.");
129 }
130 }
131
132
133 void WidgetCoreData::redo_edition() {
134 if(this->state_stack_pos == 0) {
135 g_error("Internal error: Undo/Redo stack position should never stay at 0.");
136 }
137 if(this->state_stack_pos != -1) {
138 //g_debug("Initial stack pos: %u",this->state_stack_pos);
139 this->state_stack_pos--;
140 //g_debug("Target stack pos: %u",this->state_stack_pos);
141 auto iterator = this->state_stack->rbegin();
142 std::advance(iterator,this->state_stack_pos);
143
144 auto& target_state = *iterator;
145 this->molecules = std::make_unique<std::vector<std::optional<CanvasMolecule>>>(*target_state->molecules);
146 this->rdkit_molecules = std::make_unique<std::vector<std::optional<std::shared_ptr<RDKit::RWMol>>>>(*target_state->rdkit_molecules);
147 if(this->state_stack_pos == 0) {
148 // Special case. We're now at pos 0 which means
149 // that we've reached the newest state.
150 // We should remove it from the state history stack
151 // and set the position to -1 to denote that we're "fresh", at the newest change.
152
153 // g_debug(
154 // "We're now at position 0. The newest state has been reached. "
155 // "Resetting position to -1 and removing the last element from the stack to prevent duplication."
156 // );
157 this->state_stack->pop_back();
158 this->state_stack_pos = -1;
159 }
160 update_status("");
161 } else {
162 //g_debug("Position in stack is at -1 (fresh change). Nothing to redo. Stack size: %zu",this->state_stack->size());
163 update_status("Nothing to be redone.");
164 }
165 }
166
167 bool WidgetCoreData::is_in_edition() {
168 return this->state_before_edition.get() != nullptr;
169 }
170
171 void WidgetCoreData::rollback_current_edition() {
172 if(this->state_before_edition) {
173 this->molecules = std::move(this->state_before_edition->molecules);
174 this->rdkit_molecules = std::move(this->state_before_edition->rdkit_molecules);
175 this->state_before_edition.reset(nullptr);
176 }
177 }
178
179
180 void WidgetCoreData::begin_edition() {
181 this->state_before_edition = std::make_unique<StateSnapshot>(*this);
182 }
183
184 void WidgetCoreData::finalize_edition() {
185 if(this->state_before_edition) {
186 if (this->state_stack_pos != -1) {
187 auto& state_stack = *this->state_stack;
188 auto it1 = state_stack.begin();
189 //g_debug("Finalizing edition with pos in stack: %i. The stack has to be trimmed.",this->state_stack_pos);
190 //g_debug("Current stack size: %zu Iterator advanced by: %zu",this->state_stack->size(),this->state_stack->size() - this->state_stack_pos - 1);
191 std::advance(it1,state_stack.size() - this->state_stack_pos - 1);
192 state_stack.erase(it1);
193 this->state_stack_pos = -1;
194 //g_debug("Stack size after trim: %zu",this->state_stack->size());
195 }
196 this->state_stack->push_back(std::move(this->state_before_edition));
197
198 if(this->state_stack->size() > MAX_STATE_STACK_LENGTH) {
199 auto last_iter = state_stack->begin();
200 std::advance(last_iter, STATE_STACK_TRIM_BATCH_SIZE);
201 this->state_stack->erase(this->state_stack->begin(), last_iter);
202 }
203
204 this->queue_resize();
205 this->queue_redraw();
206 this->emit_mutation_signals();
207 }
208 }
209
210 void WidgetCoreData::delete_molecule_with_idx(unsigned int idx, bool integrate_with_edit_undo) noexcept {
211 if(idx >= this->rdkit_molecules->size()) {
212 return;
213 }
214 auto& mol = this->molecules->at(idx);
215 if(mol.has_value()) {
216 if(integrate_with_edit_undo) {
217 this->begin_edition();
218 }
219 mol = std::nullopt;
220 this->rdkit_molecules->at(idx) = std::nullopt;
221
222 if(integrate_with_edit_undo) {
223 this->finalize_edition();
224 this->update_status("Molecule deleted.");
225 }
226
227 // Already called by finalize_edition()
228 // this->queue_redraw();
229
230 #ifndef __EMSCRIPTEN__
231 auto* widget_ptr = static_cast<const CootLigandEditorCanvasPriv*>(this);
232 #else
233 auto* widget_ptr = static_cast<const ::CootLigandEditorCanvas*>(this);
234 #endif
235 _LIGAND_EDITOR_SIGNAL_EMIT_ARG(widget_ptr, molecule_deleted_signal, idx);
236 }
237 }
238
239 coot::ligand_editor_canvas::SmilesMap WidgetCoreData::build_smiles() const {
240 coot::ligand_editor_canvas::SmilesMap ret;
241 auto it = this->rdkit_molecules->cbegin();
242 unsigned int idx = 0;
243
244 auto append_smiles = [&](){
245 const auto& mol_ptr_opt = *it;
246 if(mol_ptr_opt.has_value()) {
247 RDKit::RWMol* mol_ptr = mol_ptr_opt->get();
248 ret.emplace(idx, RDKit::MolToSmiles(*mol_ptr));
249 }
250 };
251
252 while(it != this->rdkit_molecules->cend()) {
253 append_smiles();
254 it++;
255 idx++;
256 }
257 return ret;
258 }
259
260 unsigned int WidgetCoreData::get_molecule_count_impl() const noexcept {
261 unsigned int ret = 0;
262 for(const auto& mol_opt: *this->rdkit_molecules) {
263 if(mol_opt.has_value()) {
264 ret += 1;
265 }
266 }
267 return ret;
268 }
269
270 int WidgetCoreData::get_first_molecule_idx() const noexcept {
271 int idx = 0;
272 for(const auto& mol_opt: *this->rdkit_molecules) {
273 if(mol_opt.has_value()) {
274 return idx;
275 }
276 idx += 1;
277 }
278 return -1;
279 }
280
281 void WidgetCoreData::render(Renderer& ren) {
282 if (this->molecules) {
283 for(auto& drawn_molecule_opt: *this->molecules) {
284 if(drawn_molecule_opt.has_value()) {
285 drawn_molecule_opt->set_canvas_scale(this->scale);
286 drawn_molecule_opt->draw(ren,this->display_mode);
287 }
288 }
289 } else {
290 g_error("Molecules vector not initialized!");
291 }
292 if(this->currently_created_bond.has_value()) {
293 auto& bond = *this->currently_created_bond;
294 ren.set_line_width(4.0);
295 ren.set_source_rgb(1.0, 0.5, 1.0);
296 ren.move_to(bond.first_atom_x, bond.first_atom_y);
297 ren.line_to(bond.second_atom_x, bond.second_atom_y);
298 ren.stroke();
299 }
300 }
301
302 void WidgetCoreData::queue_redraw() const noexcept {
303 #ifndef __EMSCRIPTEN__
304 auto* widget_ptr = static_cast<const CootLigandEditorCanvasPriv*>(this);
305 gtk_widget_queue_draw(GTK_WIDGET(widget_ptr));
306 #else
307 auto* widget_ptr = static_cast<const::CootLigandEditorCanvas*>(this);
308 _LIGAND_EDITOR_SIGNAL_EMIT(widget_ptr, queue_redraw_signal);
309 #endif
310 }
311
312 void WidgetCoreData::queue_resize() const noexcept {
313 #ifndef __EMSCRIPTEN__
314 auto* widget_ptr = static_cast<const CootLigandEditorCanvasPriv*>(this);
315 gtk_widget_queue_resize(GTK_WIDGET(widget_ptr));
316 #else
317 auto* widget_ptr = static_cast<const::CootLigandEditorCanvas*>(this);
318 _LIGAND_EDITOR_SIGNAL_EMIT(widget_ptr, queue_resize_signal);
319 #endif
320 }
321
322 #ifdef __EMSCRIPTEN__
323
324 void CootLigandEditorCanvas::set_active_tool(std::unique_ptr<coot::ligand_editor_canvas::ActiveTool> active_tool) {
325 coot_ligand_editor_canvas_set_active_tool(this, std::move(active_tool));
326 }
327
328 int CootLigandEditorCanvas::append_molecule(std::shared_ptr<RDKit::RWMol> rdkit_mol) noexcept {
329 return coot_ligand_editor_canvas_append_molecule(this, std::move(rdkit_mol));
330 }
331
332 void CootLigandEditorCanvas::update_molecule_from_smiles(unsigned int molecule_idx, const std::string& smiles) {
333 coot_ligand_editor_canvas_update_molecule_from_smiles(this, molecule_idx, smiles.c_str());
334 }
335
336 void CootLigandEditorCanvas::set_scale(float scale) noexcept {
337 coot_ligand_editor_canvas_set_scale(this, scale);
338 }
339
340 float CootLigandEditorCanvas::get_scale() noexcept {
341 return coot_ligand_editor_canvas_get_scale(this);
342 }
343
344 void CootLigandEditorCanvas::undo() noexcept {
345 coot_ligand_editor_canvas_undo_edition(this);
346 }
347
348 void CootLigandEditorCanvas::redo() noexcept {
349 coot_ligand_editor_canvas_redo_edition(this);
350 }
351
352 // const RDKit::ROMol& CanvasMolecule::get_rdkit_molecule(unsigned int index) noexcept {
353 // RDKit::ROMol* coot_ligand_editor_canvas_get_rdkit_molecule(CootLigandEditorCanvas* self, unsigned int index) noexcept;
354 // }
355
356 unsigned int CootLigandEditorCanvas::get_molecule_count() noexcept {
357 return coot_ligand_editor_canvas_get_molecule_count(this);
358 }
359
360 unsigned int CootLigandEditorCanvas::get_idx_of_first_molecule() noexcept {
361 return coot_ligand_editor_canvas_get_idx_of_first_molecule(this);
362 }
363
364 unsigned int CootLigandEditorCanvas::get_max_molecule_idx() noexcept {
365 return coot_ligand_editor_canvas_get_max_molecule_idx(this);
366 }
367
368 void CootLigandEditorCanvas::set_allow_invalid_molecules(bool value) noexcept {
369 coot_ligand_editor_canvas_set_allow_invalid_molecules(this, value);
370 }
371
372 bool CootLigandEditorCanvas::get_allow_invalid_molecules() noexcept {
373 return coot_ligand_editor_canvas_get_allow_invalid_molecules(this);
374 }
375
376 coot::ligand_editor_canvas::DisplayMode CootLigandEditorCanvas::get_display_mode() noexcept {
377 return coot_ligand_editor_canvas_get_display_mode(this);
378 }
379
380 void CootLigandEditorCanvas::set_display_mode(coot::ligand_editor_canvas::DisplayMode value) noexcept {
381 coot_ligand_editor_canvas_set_display_mode(this, value);
382 }
383
384 coot::ligand_editor_canvas::SmilesMap CootLigandEditorCanvas::get_smiles() noexcept {
385 return coot_ligand_editor_canvas_get_smiles(this);
386 }
387
388 std::string CootLigandEditorCanvas::get_smiles_for_molecule(unsigned int molecule_idx) noexcept {
389 return coot_ligand_editor_canvas_get_smiles_for_molecule(this, molecule_idx);
390 }
391
392 std::string CootLigandEditorCanvas::get_pickled_molecule(unsigned int molecule_idx) noexcept {
393 return coot_ligand_editor_canvas_get_pickled_molecule(this, molecule_idx);
394 }
395
396 std::string CootLigandEditorCanvas::get_pickled_molecule_base64(unsigned int molecule_idx) noexcept {
397 return coot_ligand_editor_canvas_get_pickled_molecule_base64(this, molecule_idx);
398 }
399
400 void CootLigandEditorCanvas::clear_molecules() noexcept {
401 coot_ligand_editor_canvas_clear_molecules(this);
402 }
403
404 void CootLigandEditorCanvas::connect(std::string signal_name, emscripten::val callback) {
405
406 if(signal_name == "status_updated") {
407 status_updated_signal.connect([=](const char* new_status){
408 callback(std::string(new_status));
409 });
410 } else if(signal_name == "scale_changed") {
411 scale_changed_signal.connect([=](float new_scale){
412 callback(new_scale);
413 });
414 } else if(signal_name == "smiles_changed") {
415 smiles_changed_signal.connect([=](){
416 callback();
417 });
418 } else if(signal_name == "molecule_deleted") {
419 molecule_deleted_signal.connect([=](int molecule_idx){
420 callback(molecule_idx);
421 });
422 } else if(signal_name == "qed_info_updated") {
423 qed_info_updated_signal.connect([=](int molecule_idx, const coot::ligand_editor_canvas::CanvasMolecule::QEDInfo* qed_info){
424 callback(molecule_idx, *qed_info);
425 });
426 } else if(signal_name == "queue_redraw") {
427 queue_redraw_signal.connect([=](){
428 callback();
429 });
430 } else if(signal_name == "queue_resize") {
431 queue_resize_signal.connect([=](){
432 callback();
433 });
434 } else {
435 g_critical("No such signal!");
436 }
437 }
438 #endif // EMSCRIPTEN