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 class ScoreDef;
29 class Staff;
30 
31 //--------------------------------------------------------------------------------
32 // EditorToolkitShared
33 //--------------------------------------------------------------------------------
34 
36 public:
37  EditorToolkitShared(Doc *doc, View *view);
38  virtual ~EditorToolkitShared();
39  bool ParseEditorAction(const std::string &json_editorAction) override
40  {
41  return ParseEditorAction(json_editorAction, false);
42  }
43  bool ParseEditorAction(const std::string &json_editorAction, bool commitOnly = false);
44 
45 protected:
46 #ifndef NO_EDIT_SUPPORT
47 
48  enum SelectCustom : int8_t { SELECT_NONE = 0, SELECT_NOTE, SELECT_TEXT_PARENT };
49 
50  struct State {
51  std::string data;
52  std::string status;
53  int options;
54  };
55 
56  enum DeleteNavigation : int8_t { DELETE_NO_NAVIGATON = 0, DELETE_BACKSPACE, DELETE_FORWARD };
57 
58  enum ScoreDefUpdate : int8_t { INSERT_ABOVE = 0, INSERT_BELOW, MOVE_UP, MOVE_DOWN, DELETE_STAFF };
59 
64  bool Chain(const jsonxx::Array &actions);
65  bool ParseContextAction(const jsonxx::Object &param, std::string &elementId, bool &scores, bool &sections);
66  bool ParseDeleteAction(const jsonxx::Object &param, std::string &elementId, DeleteNavigation &navigation);
67  bool ParseDragAction(const jsonxx::Object &param, std::string &elementId, int &x, int &y);
68  bool ParseKeyDownAction(
69  const jsonxx::Object &param, std::string &elementid, int &key, bool &shiftKey, bool &ctrlKey);
70  bool ParseInsertAction(
71  const jsonxx::Object &param, std::string &elementName, std::string &elementId, std::string &insertMode);
72  bool ParseInsertControlAction(
73  const jsonxx::Object &param, std::string &elementName, std::string &startId, std::string &endId);
74  bool ParseNavigate(const jsonxx::Object &param, std::string &elementId, int &direction);
75  bool ParseGetScoreDefAction(const jsonxx::Object &param);
76  bool ParseGetScoreDefKeySigAction(const jsonxx::Object &param, bool &selected);
77  bool ParseGetScoreDefMeterSigAction(const jsonxx::Object &param, bool &selected);
78  bool ParseGetScoreDefStaffDefAction(const jsonxx::Object &param);
79  bool ParseGetScoreDefStaffGrpAction(const jsonxx::Object &param, bool &selected);
80  bool ParseResetCursorAction(const jsonxx::Object &param, bool &maintainChordMode);
81  bool ParseSelectAction(const jsonxx::Object &param, std::string &elementId, bool &secondary, SelectCustom &custom);
82  bool ParseSetAction(
83  const jsonxx::Object &param, std::string &elementId, std::string &attribute, std::string &value);
84  bool ParseSetCursorAction(const jsonxx::Object &param, std::string &elementId, Cursor::InputMode &inputMode,
85  bool &chordMode, bool &autoBeam);
86  bool ParseUpdateCursorAction(
87  const jsonxx::Object &param, bool &restMode, bool &chordMode, Cursor::TieMode &tieMode, bool &autoBeam);
88  bool ParseUpdatePitchAction(const jsonxx::Object &param, std::string &elementId, data_PITCHNAME &pname, int &oct,
89  data_ACCIDENTAL_WRITTEN &accid, int &midi);
90  bool ParseSetScoreDefAction(const jsonxx::Object &param, jsonxx::Object &subTree);
91  bool ParseSetScoreDefKeySigAction(const jsonxx::Object &param, bool &selected, jsonxx::Object &subTree);
92  bool ParseSetScoreDefMeterSigAction(const jsonxx::Object &param, bool &selected, jsonxx::Object &subTree);
93  bool ParseSetScoreDefStaffDefAction(const jsonxx::Object &param, jsonxx::Object &subTree);
94  bool ParseSetScoreDefStaffGrpAction(const jsonxx::Object &param, bool &selected, jsonxx::Object &subTree);
95  bool ParseUpdateScoreDefAction(const jsonxx::Object &param, ScoreDefUpdate &update);
96 
98 
99  void SetEditStatus();
100  void ReloadEditStatus(const std::string &statusStr, bool insertMode);
101  void PrepareUndo(bool ignoreInsertMode = false);
102  std::string GetCurrentState();
103  bool ReloadState(const State &state);
104  void TrimUndoMemory();
105  bool CanUndo() const;
106  bool CanRedo() const;
107  bool Undo();
108  bool Redo();
109 
114  bool SetCursor(std::string &elementId, Cursor::InputMode inputMode, bool chordMode, bool autoBeam);
115  bool UpdateCursor(bool restMode, bool chordMode, Cursor::TieMode tieMode, bool autoBeam);
116  bool ResetCursor(bool maintainChordMode);
117  bool Delete(std::string &elementId, DeleteNavigation navigation);
118  bool Drag(std::string &elementId, int x, int y);
119  bool InsertControl(std::string &elementName, std::string &startId, std::string &endId);
120  bool KeyDown(std::string &elementId, int key, bool shiftKey, bool ctrlKey);
121  bool Navigate(std::string &elementId, const int &direction);
122  bool Select(std::string &elementId, bool secondary, SelectCustom custom);
123  bool Set(std::string &elementId, const std::string &attribute, const std::string &value);
124  bool UpdatePitch(std::string &elementId, data_PITCHNAME pname, int oct, data_ACCIDENTAL_WRITTEN accid, int midi);
125 
127 
128  void ClearContext();
129  bool ContextForElement(std::string &elementId);
130  bool ContextForScores(bool updateResponse);
131  bool ContextForSections(bool updateResponse);
132 
133  bool GetScoreDef();
134  bool GetScoreDefKeySig(bool selected);
135  bool GetScoreDefMeterSig(bool selected);
136  bool GetScoreDefStaffDef();
137  bool GetScoreDefStaffGrp(bool selected);
138  bool SetScoreDef(const jsonxx::Object &subTree);
139  bool SetScoreDefKeySig(bool selected, const jsonxx::Object &subTree);
140  bool SetScoreDefMeterSig(bool selected, const jsonxx::Object &subTree);
141  bool SetScoreDefStaffDef(const jsonxx::Object &subTree);
142  bool SetScoreDefStaffGrp(bool selected, const jsonxx::Object &subTree);
143  bool UpdateScoreDef(ScoreDefUpdate update);
144 
145  ScoreDef *ResolveScoreDef(bool selected);
146  Staff *ResolveScoreDefStaff();
147  void FinalizeScoreDefUpdate();
148 
149  void ContextForObject(const Object *object, jsonxx::Object &element, bool recursive = false);
150  void ContextForObjects(const ArrayOfConstObjects &objects, jsonxx::Array &siblings);
151  void ContextForReferences(const ListOfObjectAttNamePairs &objects, jsonxx::Array &links);
152 
153  ArrayOfConstObjects GetScoreBasedChildrenFor(const Object *object);
154 
155  void PostProcessDeleteObjects(const Object *element, std::set<std::string> &toPostProcess);
156  void PostProcessDelete(const std::string &elementId);
157 
158  void PostEditRestriction(Object *element);
159 
160  void MoveCursor(LayerElement *element, bool maintainChordMode = false);
161 
162  data_ACCIDENTAL_WRITTEN GetAccidBefore(const LayerElement *element, data_PITCHNAME pname, int oct);
163  std::pair<data_ACCIDENTAL_WRITTEN, bool> GetActualAccid(Object *element, data_ACCIDENTAL_WRITTEN accid);
164 
165  const Measure *GetPreviousMeasure(const Measure *measure);
166  const Staff *GetPreviousStaff(const Staff *staff);
167  const Layer *GetPreviousLayer(const Layer *layer);
168  const Measure *GetNextMeasure(const Measure *measure);
169  const Staff *GetNextStaff(const Staff *staff);
170  const Layer *GetNextLayer(const Layer *layer);
171 
172 public:
173  //
174 protected:
175  bool m_undoPrepared;
176 
177  std::deque<State> m_undoStack;
178  std::deque<State> m_redoStack;
179  size_t m_undoMemoryUsage = 0;
180 
181  EditorTreeObject *m_scoreContext;
182  EditorTreeObject *m_sectionContext;
183  EditorTreeObject *m_currentContext;
184 
185 private:
186  struct MidiSpelling {
187  data_PITCHNAME pname;
188  data_ACCIDENTAL_WRITTEN accid;
189  };
190 
191  MidiSpelling SpellMidi(int midi, const data_KEYSIGNATURE &keySig);
192 };
193 
194 //----------------------------------------------------------------------------
195 // EditorTreeObject
196 //----------------------------------------------------------------------------
197 
202 
203 public:
209  EditorTreeObject(const Object *object, bool ownChildren);
210  virtual ~EditorTreeObject() {};
211  void Reset() override;
212  std::string GetClassName() const override { return m_className; }
214 
219  bool IsSupportedChild(ClassId classId) override { return true; }
221 
226  VisibilityDrawingInterface *GetVisibilityDrawingInterface() override
227  {
228  return vrv_cast<VisibilityDrawingInterface *>(this);
229  }
230  const VisibilityDrawingInterface *GetVisibilityDrawingInterface() const override
231  {
232  return vrv_cast<const VisibilityDrawingInterface *>(this);
233  }
235 
236  ArrayOfConstObjects GetChildObjects() const;
237 
238 private:
239  //
240 public:
241  std::string m_className;
242  const Object *m_object;
243 
244 private:
245  //
246 #endif /* NO_EDIT_SUPPORT */
247 };
248 
249 } // namespace vrv
250 
251 #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:35
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:39
bool Chain(const jsonxx::Array &actions)
Parse JSON instructions for experimental editor functions.
bool SetCursor(std::string &elementId, Cursor::InputMode inputMode, bool chordMode, bool autoBeam)
Experimental editor functions.
This class stores an alignment position elements will point to.
Definition: editortoolkit_shared.h:201
bool IsSupportedChild(ClassId classId) override
Base method for checking if a child can be added.
Definition: editortoolkit_shared.h:219
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 MEI scoreDef.
Definition: scoredef.h:136
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:50