Verovio
Source code documentation
editortoolkit_shared.h
1 // Name: editortoolkit_shared.h
3 // Author: Laurent Pugin
4 // Created: 03/02/2026
5 // Copyright (c) Authors and others. All rights reserved.
7 
8 #ifndef __VRV_EDITOR_TOOLKIT_SHARED_H__
9 #define __VRV_EDITOR_TOOLKIT_SHARED_H__
10 
11 #include <cmath>
12 #include <deque>
13 #include <string>
14 #include <utility>
15 
16 //--------------------------------------------------------------------------------
17 
18 #include "cursor.h"
19 #include "doc.h"
20 #include "editortoolkit.h"
21 #include "view.h"
22 
23 #include "jsonxx.h"
24 
25 namespace vrv {
26 
27 class EditorTreeObject;
28 
29 //--------------------------------------------------------------------------------
30 // EditorToolkitShared
31 //--------------------------------------------------------------------------------
32 
34 public:
35  EditorToolkitShared(Doc *doc, View *view);
36  virtual ~EditorToolkitShared();
37  bool ParseEditorAction(const std::string &json_editorAction) override
38  {
39  return ParseEditorAction(json_editorAction, false);
40  }
41  bool ParseEditorAction(const std::string &json_editorAction, bool commitOnly = false);
42 
43 protected:
44 #ifndef NO_EDIT_SUPPORT
45 
46  enum SelectCustom : int8_t { SELECT_NONE = 0, SELECT_NOTE, SELECT_TEXT_PARENT };
47 
48  struct State {
49  std::string data;
50  std::string status;
51  int options;
52  };
53 
54  enum DeleteNavigation : int8_t { DELETE_NO_NAVIGATON = 0, DELETE_BACKSPACE, DELETE_FORWARD };
55 
60  bool Chain(const jsonxx::Array &actions);
61  bool ParseContextAction(const jsonxx::Object &param, std::string &elementId, bool &scores, bool &sections);
62  bool ParseDeleteAction(const jsonxx::Object &param, std::string &elementId, DeleteNavigation &navigation);
63  bool ParseDragAction(const jsonxx::Object &param, std::string &elementId, int &x, int &y);
64  bool ParseKeyDownAction(
65  const jsonxx::Object &param, std::string &elementid, int &key, bool &shiftKey, bool &ctrlKey);
66  bool ParseInsertAction(
67  const jsonxx::Object &param, std::string &elementName, std::string &elementId, std::string &insertMode);
68  bool ParseInsertControlAction(
69  const jsonxx::Object &param, std::string &elementName, std::string &startId, std::string &endId);
70  bool ParseNavigate(const jsonxx::Object &param, std::string &elementId, int &direction);
71  bool ParsePropertiesAction(const jsonxx::Object &param, std::string &scoreDef);
72  bool ParseResetCursorAction(const jsonxx::Object &param, bool &maintainChordMode);
73  bool ParseSelectAction(const jsonxx::Object &param, std::string &elementId, bool &secondary, SelectCustom &custom);
74  bool ParseSetAction(
75  const jsonxx::Object &param, std::string &elementId, std::string &attribute, std::string &value);
76  bool ParseSetCursorAction(
77  const jsonxx::Object &param, std::string &elementId, Cursor::InputMode &inputMode, bool &chordMode);
78  bool ParseUpdateCursorAction(
79  const jsonxx::Object &param, bool &restMode, bool &chordMode, Cursor::TieMode &tieMode);
80  bool ParseUpdatePitchAction(const jsonxx::Object &param, std::string &elementId, data_PITCHNAME &pname, int &oct,
81  data_ACCIDENTAL_WRITTEN &accid, int &midi);
82 
84 
85  void SetEditStatus();
86  void ReloadEditStatus(const std::string &statusStr, bool insertMode);
87  void PrepareUndo(bool ignoreInsertMode = false);
88  std::string GetCurrentState();
89  bool ReloadState(const State &state);
90  void TrimUndoMemory();
91  bool CanUndo() const;
92  bool CanRedo() const;
93  bool Undo();
94  bool Redo();
95 
100  bool SetCursor(std::string &elementId, Cursor::InputMode inputMode, bool chordMode);
101  bool UpdateCursor(bool restMode, bool chordMode, Cursor::TieMode tieMode);
102  bool ResetCursor(bool maintainChordMode);
103  bool Delete(std::string &elementId, DeleteNavigation navigation);
104  bool Drag(std::string &elementId, int x, int y);
105  bool InsertControl(std::string &elementName, std::string &startId, std::string &endId);
106  bool KeyDown(std::string &elementId, int key, bool shiftKey, bool ctrlKey);
107  bool Navigate(std::string &elementId, const int &direction);
108  bool Select(std::string &elementId, bool secondary, SelectCustom custom);
109  bool Set(std::string &elementId, const std::string &attribute, const std::string &value);
110  bool UpdatePitch(std::string &elementId, data_PITCHNAME pname, int oct, data_ACCIDENTAL_WRITTEN accid, int midi);
111 
113 
114  void ClearContext();
115  bool ContextForElement(std::string &elementId);
116  bool ContextForScores(bool updateResponse);
117  bool ContextForSections(bool updateResponse);
118 
119  bool GetScoreDef();
120  bool SetScoreDef(const std::string scoreDef);
121 
122  void ContextForObject(const Object *object, jsonxx::Object &element, bool recursive = false);
123  void ContextForObjects(const ArrayOfConstObjects &objects, jsonxx::Array &siblings);
124  void ContextForReferences(const ListOfObjectAttNamePairs &objects, jsonxx::Array &links);
125 
126  ArrayOfConstObjects GetScoreBasedChildrenFor(const Object *object);
127 
128  void CollectReferringObjects(
129  const Object *element, std::set<std::string> &toDelete, std::set<const Object *> &visited);
130  void PostProcessDeleteObjects(const Object *element, std::set<std::string> &toPostProcess);
131  void PostProcessDelete(const std::string &elementId);
132 
133  void PostEditRestriction(Object *element);
134 
135  void MoveCursor(LayerElement *element, bool maintainChordMode = false);
136 
137  data_ACCIDENTAL_WRITTEN GetAccidBefore(const LayerElement *element, data_PITCHNAME pname, int oct);
138  std::pair<data_ACCIDENTAL_WRITTEN, bool> GetActualAccid(Object *element, data_ACCIDENTAL_WRITTEN accid);
139 
140  const Measure *GetPreviousMeasure(const Measure *measure);
141  const Staff *GetPreviousStaff(const Staff *staff);
142  const Layer *GetPreviousLayer(const Layer *layer);
143  const Measure *GetNextMeasure(const Measure *measure);
144  const Staff *GetNextStaff(const Staff *staff);
145  const Layer *GetNextLayer(const Layer *layer);
146 
147 public:
148  //
149 protected:
150  bool m_undoPrepared;
151 
152  std::deque<State> m_undoStack;
153  std::deque<State> m_redoStack;
154  size_t m_undoMemoryUsage = 0;
155 
156  EditorTreeObject *m_scoreContext;
157  EditorTreeObject *m_sectionContext;
158  EditorTreeObject *m_currentContext;
159 
160 private:
161  struct MidiSpelling {
162  data_PITCHNAME pname;
163  data_ACCIDENTAL_WRITTEN accid;
164  };
165 
166  MidiSpelling SpellMidi(int midi, const data_KEYSIGNATURE &keySig);
167 };
168 
169 //----------------------------------------------------------------------------
170 // EditorTreeObject
171 //----------------------------------------------------------------------------
172 
177 
178 public:
184  EditorTreeObject(const Object *object, bool ownChildren);
185  virtual ~EditorTreeObject() {};
186  void Reset() override;
187  std::string GetClassName() const override { return m_className; }
189 
194  bool IsSupportedChild(ClassId classId) override { return true; }
196 
201  VisibilityDrawingInterface *GetVisibilityDrawingInterface() override
202  {
203  return vrv_cast<VisibilityDrawingInterface *>(this);
204  }
205  const VisibilityDrawingInterface *GetVisibilityDrawingInterface() const override
206  {
207  return vrv_cast<const VisibilityDrawingInterface *>(this);
208  }
210 
211  ArrayOfConstObjects GetChildObjects() const;
212 
213 private:
214  //
215 public:
216  std::string m_className;
217  const Object *m_object;
218 
219 private:
220  //
221 #endif /* NO_EDIT_SUPPORT */
222 };
223 
224 } // namespace vrv
225 
226 #endif
This class is a hold the data and corresponds to the model of a MVC design pattern.
Definition: doc.h:41
Definition: editortoolkit.h:30
Definition: editortoolkit_shared.h:33
bool ParseEditorAction(const std::string &json_editorAction) override
In child classes, this parses the provided editor action and then performs the correct action.
Definition: editortoolkit_shared.h:37
bool SetCursor(std::string &elementId, Cursor::InputMode inputMode, bool chordMode)
Experimental editor functions.
bool Chain(const jsonxx::Array &actions)
Parse JSON instructions for experimental editor functions.
This class stores an alignment position elements will point to.
Definition: editortoolkit_shared.h:176
bool IsSupportedChild(ClassId classId) override
Base method for checking if a child can be added.
Definition: editortoolkit_shared.h:194
This class is a base class for the Layer (<layer>) content.
Definition: layerelement.h:51
This class represents a layer in a laid-out score (Doc).
Definition: layer.h:40
This class represents a measure in a page-based score (Doc).
Definition: measure.h:46
This class represents a basic object.
Definition: object.h:66
This class represents a staff in a laid-out score (Doc).
Definition: staff.h:110
This class is a drawing context and corresponds to the view of a MVC design pattern.
Definition: view.h:109
This class is an interface for MEI element that can be hidden during drawing.
Definition: drawinginterface.h:448
Definition: editortoolkit_shared.h:48