| 1 |
/* layla/ligand_editor_canvas/tools.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_TOOLS_HPP |
| 23 |
#define COOT_LIGAND_EDITOR_CANVAS_TOOLS_HPP |
| 24 |
#include "model.hpp" |
| 25 |
#include <set> |
| 26 |
|
| 27 |
namespace coot::ligand_editor_canvas { |
| 28 |
|
| 29 |
namespace impl { |
| 30 |
/// This is a forward declaration of a structure defined at |
| 31 |
/// "core.hpp" |
| 32 |
struct WidgetCoreData; |
| 33 |
/// This is a forward declaration of a structure defined at |
| 34 |
/// "core.hpp" |
| 35 |
struct CootLigandEditorCanvasPriv; |
| 36 |
} |
| 37 |
|
| 38 |
class TransformManager { |
| 39 |
public: |
| 40 |
enum class Mode { |
| 41 |
Rotation, |
| 42 |
Translation |
| 43 |
}; |
| 44 |
private: |
| 45 |
|
| 46 |
struct RotationState { |
| 47 |
double last_absolute_angle; |
| 48 |
std::pair<int,int> original_rotation_pos; |
| 49 |
std::pair<int,int> current_rotation_pos; |
| 50 |
|
| 51 |
double get_current_absolute_angle(bool snap_to_angle) const; |
| 52 |
double get_current_angle_diff(bool snap_to_angle) const; |
| 53 |
}; |
| 54 |
struct TranslationState { |
| 55 |
std::pair<int,int> prev_move_pos; |
| 56 |
std::pair<int,int> current_move_pos; |
| 57 |
|
| 58 |
std::pair<int,int> get_current_offset() const; |
| 59 |
}; |
| 60 |
|
| 61 |
class IdleState { |
| 62 |
// empty |
| 63 |
}; |
| 64 |
|
| 65 |
std::variant<RotationState, TranslationState, IdleState> state; |
| 66 |
std::optional<unsigned int> canvas_mol_idx; |
| 67 |
|
| 68 |
public: |
| 69 |
|
| 70 |
TransformManager() noexcept; |
| 71 |
|
| 72 |
bool is_active() const noexcept; |
| 73 |
|
| 74 |
void begin_transform(int x, int y, Mode mode) noexcept; |
| 75 |
void update_current_cursor_pos(int x, int y, bool snap) noexcept; |
| 76 |
|
| 77 |
void end_transform() noexcept; |
| 78 |
|
| 79 |
void set_canvas_molecule_index(unsigned int) noexcept; |
| 80 |
|
| 81 |
void apply_current_transform_state(impl::WidgetCoreData* widget_data, bool snapt_to_angle, bool about_to_end) const; |
| 82 |
}; |
| 83 |
|
| 84 |
class Tool { |
| 85 |
|
| 86 |
public: |
| 87 |
|
| 88 |
struct ClickContext { |
| 89 |
impl::WidgetCoreData& widget_data; |
| 90 |
bool control_pressed; |
| 91 |
bool alt_pressed; |
| 92 |
|
| 93 |
ClickContext(impl::WidgetCoreData& widget_data); |
| 94 |
}; |
| 95 |
|
| 96 |
struct MoleculeClickContext : public ClickContext { |
| 97 |
unsigned int mol_idx; |
| 98 |
std::shared_ptr<RDKit::RWMol>& rdkit_mol; |
| 99 |
CanvasMolecule& canvas_mol; |
| 100 |
|
| 101 |
MoleculeClickContext(ClickContext super, unsigned int mol_idx, std::shared_ptr<RDKit::RWMol>& rdkit_mol, CanvasMolecule& canvas_mol); |
| 102 |
}; |
| 103 |
|
| 104 |
/// Called when the tools is set in the ActiveTool |
| 105 |
virtual void on_load(impl::WidgetCoreData& widget_data); |
| 106 |
|
| 107 |
/// Called always, whenever there's been a click event. |
| 108 |
/// Called before other click methods get called. |
| 109 |
virtual void on_click(ClickContext& ctx, int x, int y); |
| 110 |
|
| 111 |
/// Called when the click coordinates do not correspond to anything on canvas |
| 112 |
virtual void on_blank_space_click(ClickContext& ctx, int x, int y); |
| 113 |
|
| 114 |
/// Called if the click lands on a molecule. |
| 115 |
/// Returns true if `on_bond_click()` or `on_atom_click()` (respectively to what's been clicked) |
| 116 |
/// should be called next (and then lastly `after_molecule_click()`) |
| 117 |
virtual bool on_molecule_click(MoleculeClickContext& ctx); |
| 118 |
|
| 119 |
virtual void on_bond_click(MoleculeClickContext& ctx, CanvasMolecule::Bond&); |
| 120 |
virtual void on_atom_click(MoleculeClickContext& ctx, CanvasMolecule::Atom&); |
| 121 |
|
| 122 |
/// Generic on-mouse-release event handler. |
| 123 |
/// No dedicated molecule/atom/bond handlers seem to be needed now. |
| 124 |
virtual void on_release(ClickContext& ctx, int x, int y); |
| 125 |
|
| 126 |
virtual void after_molecule_click(MoleculeClickContext& ctx); |
| 127 |
|
| 128 |
/// Called always, whenever there's been a right-click event. |
| 129 |
/// Called before other click methods get called. |
| 130 |
virtual void on_right_click(ClickContext& ctx, int x, int y); |
| 131 |
|
| 132 |
/// Called when the right-click coordinates do not correspond to anything on canvas |
| 133 |
virtual void on_blank_space_right_click(ClickContext& ctx, int x, int y); |
| 134 |
|
| 135 |
/// Called if the right-click lands on a molecule. |
| 136 |
/// Returns true if `on_bond_right_click()` or `on_atom_right_click()` (respectively to what's been clicked) |
| 137 |
/// should be called next (and then lastly `after_molecule_right_click()`) |
| 138 |
virtual bool on_molecule_right_click(MoleculeClickContext& ctx); |
| 139 |
|
| 140 |
virtual void on_bond_right_click(MoleculeClickContext& ctx, CanvasMolecule::Bond&); |
| 141 |
virtual void on_atom_right_click(MoleculeClickContext& ctx, CanvasMolecule::Atom&); |
| 142 |
|
| 143 |
virtual void after_molecule_right_click(MoleculeClickContext& ctx); |
| 144 |
|
| 145 |
/// Called always, whenever there's been a hover event. |
| 146 |
/// Called before other click methods get called. |
| 147 |
/// Returns if the hover event should be processed at all |
| 148 |
virtual bool on_hover(ClickContext& ctx, int x, int y); |
| 149 |
|
| 150 |
/// Called when the click coordinates do not correspond to anything on canvas |
| 151 |
virtual void on_blank_space_hover(ClickContext& ctx, int x, int y); |
| 152 |
|
| 153 |
/// Called if the hover lands on a molecule. |
| 154 |
/// Returns true if `on_bond_hover()` or `on_atom_hover()` (respectively to what's been hovered on) |
| 155 |
/// should be called next (and then lastly `after_molecule_hover()`) |
| 156 |
virtual bool on_molecule_hover(MoleculeClickContext& ctx); |
| 157 |
|
| 158 |
virtual void on_bond_hover(MoleculeClickContext& ctx, CanvasMolecule::Bond&); |
| 159 |
virtual void on_atom_hover(MoleculeClickContext& ctx, CanvasMolecule::Atom&); |
| 160 |
|
| 161 |
virtual void after_molecule_hover(MoleculeClickContext& ctx); |
| 162 |
|
| 163 |
/// Used to print tool-specific error messages should any handler throw an exception |
| 164 |
virtual std::string get_exception_message_prefix() const noexcept; |
| 165 |
|
| 166 |
/// Wraps `RDKit::MolOps::sanitizeMol()`. |
| 167 |
/// Used for checking validity and reversing kekulization. |
| 168 |
/// Does nothing when nonsensical molecules are allowed. |
| 169 |
static void sanitize_molecule(impl::WidgetCoreData&, RDKit::RWMol&); |
| 170 |
|
| 171 |
virtual ~Tool(); |
| 172 |
}; |
| 173 |
|
| 174 |
class BondModifier : public Tool { |
| 175 |
public: |
| 176 |
enum class BondModifierMode { |
| 177 |
Single, |
| 178 |
Double, |
| 179 |
Triple |
| 180 |
}; |
| 181 |
private: |
| 182 |
BondModifierMode mode; |
| 183 |
std::optional<std::pair<unsigned int,unsigned int>> molecule_idx_and_first_atom_of_new_bond; |
| 184 |
bool is_in_drag; |
| 185 |
|
| 186 |
public: |
| 187 |
BondModifier(BondModifierMode) noexcept; |
| 188 |
|
| 189 |
CanvasMolecule::BondType get_target_bond_type() const noexcept; |
| 190 |
bool is_creating_bond() const noexcept; |
| 191 |
void begin_creating_bond(unsigned int molecule_idx, unsigned int atom_idx) noexcept; |
| 192 |
void finish_creating_bond() noexcept; |
| 193 |
std::optional<std::pair<unsigned int,unsigned int>> get_molecule_idx_and_first_atom_of_new_bond() const noexcept; |
| 194 |
|
| 195 |
virtual bool on_molecule_click(MoleculeClickContext& ctx) override; |
| 196 |
virtual void on_bond_click(MoleculeClickContext& ctx, CanvasMolecule::Bond& bond) override; |
| 197 |
virtual void on_atom_click(MoleculeClickContext& ctx, CanvasMolecule::Atom& atom) override; |
| 198 |
virtual std::string get_exception_message_prefix() const noexcept override; |
| 199 |
virtual void on_release(ClickContext& ctx, int x, int y) override; |
| 200 |
}; |
| 201 |
|
| 202 |
class ElementInsertion : public Tool { |
| 203 |
public: |
| 204 |
enum class Element { |
| 205 |
C, |
| 206 |
N, |
| 207 |
O, |
| 208 |
S, |
| 209 |
P, |
| 210 |
H, |
| 211 |
F, |
| 212 |
Cl, |
| 213 |
Br, |
| 214 |
I |
| 215 |
}; |
| 216 |
|
| 217 |
private: |
| 218 |
std::variant<Element,unsigned int> element; |
| 219 |
public: |
| 220 |
ElementInsertion(const char* symbol); |
| 221 |
ElementInsertion(Element el) noexcept; |
| 222 |
|
| 223 |
unsigned int get_atomic_number() const noexcept; |
| 224 |
|
| 225 |
virtual bool on_molecule_click(MoleculeClickContext& ctx) override; |
| 226 |
virtual void on_bond_click(MoleculeClickContext& ctx, CanvasMolecule::Bond& bond) override; |
| 227 |
virtual void on_atom_click(MoleculeClickContext& ctx, CanvasMolecule::Atom& atom) override; |
| 228 |
virtual void after_molecule_click(MoleculeClickContext& ctx) override; |
| 229 |
virtual std::string get_exception_message_prefix() const noexcept override; |
| 230 |
}; |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
class StructureInsertion : public Tool { |
| 235 |
public: |
| 236 |
|
| 237 |
enum class Structure: unsigned int { |
| 238 |
CycloPropaneRing, |
| 239 |
CycloButaneRing, |
| 240 |
CycloPentaneRing, |
| 241 |
CycloHexaneRing, |
| 242 |
BenzeneRing, |
| 243 |
CycloHeptaneRing, |
| 244 |
CycloOctaneRing |
| 245 |
}; |
| 246 |
private: |
| 247 |
Structure structure; |
| 248 |
|
| 249 |
static unsigned int append_carbon(RDKit::RWMol*, unsigned int target_idx, RDKit::Bond::BondType bond_type = RDKit::Bond::SINGLE); |
| 250 |
static unsigned int append_carbon_chain(RDKit::RWMol*, unsigned int chain_start_idx, std::size_t atom_count); |
| 251 |
void append_structure_to_atom(RDKit::RWMol*, unsigned int atom_idx, bool spiro) const; |
| 252 |
|
| 253 |
public: |
| 254 |
StructureInsertion(Structure) noexcept; |
| 255 |
|
| 256 |
Structure get_structure() const noexcept; |
| 257 |
|
| 258 |
virtual bool on_molecule_click(MoleculeClickContext& ctx) override; |
| 259 |
virtual void on_bond_click(MoleculeClickContext& ctx, CanvasMolecule::Bond& bond) override; |
| 260 |
virtual void on_atom_click(MoleculeClickContext& ctx, CanvasMolecule::Atom& atom) override; |
| 261 |
virtual void after_molecule_click(MoleculeClickContext& ctx) override; |
| 262 |
virtual void on_blank_space_click(ClickContext& ctx, int x, int y) override; |
| 263 |
virtual std::string get_exception_message_prefix() const noexcept override; |
| 264 |
|
| 265 |
|
| 266 |
}; |
| 267 |
|
| 268 |
class ChargeModifier : public Tool { |
| 269 |
public: |
| 270 |
|
| 271 |
virtual void on_atom_click(MoleculeClickContext& ctx, CanvasMolecule::Atom& atom) override; |
| 272 |
virtual void on_atom_right_click(MoleculeClickContext& ctx, CanvasMolecule::Atom& atom) override; |
| 273 |
virtual std::string get_exception_message_prefix() const noexcept override; |
| 274 |
|
| 275 |
}; |
| 276 |
|
| 277 |
class DeleteTool : public Tool { |
| 278 |
|
| 279 |
public: |
| 280 |
// Represents either atom idx or a pair of atom indices forming a bond |
| 281 |
typedef std::variant<unsigned int, std::tuple<unsigned int, unsigned int>> AtomOrBond; |
| 282 |
typedef std::vector<AtomOrBond> ListOfAtomsOrBonds; |
| 283 |
private: |
| 284 |
|
| 285 |
static bool chain_contains_majority_of_atoms(const ListOfAtomsOrBonds& chain, const RDKit::ROMol* mol); |
| 286 |
static ListOfAtomsOrBonds trace_chain_impl(const RDKit::ROMol* mol, std::set<unsigned int>& processed_atoms, RDKit::Atom const* rdatom); |
| 287 |
|
| 288 |
/// Returns a vector of atoms IDs to be removed (if relevant) |
| 289 |
static ListOfAtomsOrBonds trace_rchain(const MoleculeClickContext& ctx, const CanvasMolecule::Bond& bond); |
| 290 |
|
| 291 |
/// Returns a vector of atoms IDs to be removed (if relevant) |
| 292 |
static ListOfAtomsOrBonds trace_rchain(const MoleculeClickContext& ctx, const CanvasMolecule::Atom& atom); |
| 293 |
|
| 294 |
void remove_rchain(const MoleculeClickContext& ctx, const ListOfAtomsOrBonds& chain); |
| 295 |
void highlight_rchain(const MoleculeClickContext& ctx, const ListOfAtomsOrBonds& chain); |
| 296 |
|
| 297 |
public: |
| 298 |
|
| 299 |
virtual bool on_molecule_click(MoleculeClickContext& ctx) override; |
| 300 |
virtual void on_bond_click(MoleculeClickContext& ctx, CanvasMolecule::Bond& bond) override; |
| 301 |
virtual void on_atom_click(MoleculeClickContext& ctx, CanvasMolecule::Atom& atom) override; |
| 302 |
virtual void after_molecule_click(MoleculeClickContext& ctx) override; |
| 303 |
|
| 304 |
virtual bool on_hover(ClickContext& ctx, int x, int y) override; |
| 305 |
virtual bool on_molecule_hover(MoleculeClickContext& ctx) override; |
| 306 |
virtual void on_bond_hover(MoleculeClickContext& ctx, CanvasMolecule::Bond& bond) override; |
| 307 |
virtual void on_atom_hover(MoleculeClickContext& ctx, CanvasMolecule::Atom& atom) override; |
| 308 |
|
| 309 |
virtual std::string get_exception_message_prefix() const noexcept override; |
| 310 |
}; |
| 311 |
|
| 312 |
/// Responsible for beginning transforms in the ActiveTool's TransformManager |
| 313 |
class TransformTool : public Tool { |
| 314 |
TransformManager::Mode mode; |
| 315 |
// Non-owning pointer |
| 316 |
TransformManager* transform_manager; |
| 317 |
|
| 318 |
public: |
| 319 |
TransformTool(TransformManager::Mode) noexcept; |
| 320 |
void set_transform_manager(TransformManager*) noexcept; |
| 321 |
|
| 322 |
virtual void on_click(ClickContext& ctx, int x, int y) override; |
| 323 |
virtual bool on_molecule_click(MoleculeClickContext& ctx) override; |
| 324 |
}; |
| 325 |
|
| 326 |
class GeometryModifier : public Tool { |
| 327 |
public: |
| 328 |
|
| 329 |
virtual void on_bond_click(MoleculeClickContext& ctx, CanvasMolecule::Bond& bond) override; |
| 330 |
virtual std::string get_exception_message_prefix() const noexcept override; |
| 331 |
}; |
| 332 |
|
| 333 |
class FormatTool : public Tool { |
| 334 |
public: |
| 335 |
|
| 336 |
virtual void on_load(impl::WidgetCoreData& widget_data) override; |
| 337 |
virtual bool on_molecule_click(MoleculeClickContext& ctx) override; |
| 338 |
virtual std::string get_exception_message_prefix() const noexcept override; |
| 339 |
}; |
| 340 |
|
| 341 |
class FlipTool : public Tool { |
| 342 |
FlipMode mode; |
| 343 |
public: |
| 344 |
FlipTool(FlipMode) noexcept; |
| 345 |
|
| 346 |
virtual void on_load(impl::WidgetCoreData& widget_data) override; |
| 347 |
virtual bool on_molecule_click(MoleculeClickContext& ctx) override; |
| 348 |
virtual std::string get_exception_message_prefix() const noexcept override; |
| 349 |
}; |
| 350 |
|
| 351 |
class RemoveHydrogensTool : public Tool { |
| 352 |
public: |
| 353 |
|
| 354 |
virtual void on_load(impl::WidgetCoreData& widget_data) override; |
| 355 |
virtual bool on_molecule_click(MoleculeClickContext& ctx) override; |
| 356 |
virtual std::string get_exception_message_prefix() const noexcept override; |
| 357 |
}; |
| 358 |
|
| 359 |
class ActiveTool { |
| 360 |
/// Non-owning pointer |
| 361 |
impl::WidgetCoreData* widget_data; |
| 362 |
std::unique_ptr<Tool> tool; |
| 363 |
TransformManager transform_manager; |
| 364 |
|
| 365 |
public: |
| 366 |
ActiveTool() noexcept; |
| 367 |
ActiveTool(ElementInsertion insertion) noexcept; |
| 368 |
ActiveTool(BondModifier modifier) noexcept; |
| 369 |
ActiveTool(DeleteTool) noexcept; |
| 370 |
ActiveTool(ChargeModifier) noexcept; |
| 371 |
ActiveTool(TransformTool) noexcept; |
| 372 |
ActiveTool(StructureInsertion insertion) noexcept; |
| 373 |
ActiveTool(GeometryModifier modifier) noexcept; |
| 374 |
ActiveTool(FormatTool) noexcept; |
| 375 |
ActiveTool(FlipTool) noexcept; |
| 376 |
ActiveTool(RemoveHydrogensTool) noexcept; |
| 377 |
|
| 378 |
/// Calles initialization code on the currently chosen tool (if there's any) |
| 379 |
void on_load(); |
| 380 |
|
| 381 |
/// Handles mouse click event for the currently chosen tool |
| 382 |
void on_click(bool alt_pressed, bool ctrl_pressed, int x, int y, bool right_click); |
| 383 |
/// Handles mouse-release event for the currently chosen tool |
| 384 |
void on_release(bool alt_pressed, bool ctrl_pressed, int x, int y, bool right_click); |
| 385 |
/// Handles mouse hover event for the currently chosen tool |
| 386 |
void on_hover(bool alt_pressed, bool ctrl_pressed, int x, int y); |
| 387 |
|
| 388 |
/// Returns true if a new bond is currently being create via click'n'drag |
| 389 |
bool is_creating_bond() const noexcept; |
| 390 |
/// Returns a pair of indices - index of the molecule followed by the index of the first atom |
| 391 |
/// of the newly created bond (if any) |
| 392 |
std::optional<std::pair<unsigned int,unsigned int>> get_molecule_idx_and_first_atom_of_new_bond() const noexcept; |
| 393 |
/// Updates the current mouse coordinates |
| 394 |
/// allowing to compute adequate viewport rotation / translation |
| 395 |
void update_transform_cursor_pos(int x, int y, bool snap_to_angle) noexcept; |
| 396 |
/// Ends move or rotation |
| 397 |
void end_transform(bool snap_to_angle); |
| 398 |
|
| 399 |
/// Begins rotation or translation |
| 400 |
void begin_transform(int x, int y, TransformManager::Mode); |
| 401 |
|
| 402 |
/// Returns if the user is currently dragging their mouse |
| 403 |
/// to shift the viewport / rotate the molecule. |
| 404 |
bool is_in_transform() const noexcept; |
| 405 |
|
| 406 |
/// Only meant to be invoked from within CootLigandEditorCanvas implementation |
| 407 |
/// |
| 408 |
/// Sets the pointer to access widget's data. |
| 409 |
void set_core_widget_data(impl::CootLigandEditorCanvasPriv* owning_widget) noexcept; |
| 410 |
}; |
| 411 |
|
| 412 |
} |
| 413 |
|
| 414 |
#endif // COOT_LIGAND_EDITOR_CANVAS_TOOLS_HPP |