Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-Studio-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2026 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 :
23 : #pragma once
24 :
25 : #include <map>
26 : #include <optional>
27 : #include <vector>
28 : #include <QPointF>
29 : #include <QQuickItem>
30 :
31 : #include "project/iprojectsession.h"
32 : #include "async/asyncable.h"
33 : #include "ui/iuiconfiguration.h"
34 : #include "ui/iuicontextconfiguration.h"
35 : #include "notation/notationtypes.h"
36 : #include "notation/inotationconfiguration.h"
37 : #include "notation/inotationcontextconfiguration.h"
38 : #include "engraving/automation/automationdata.h"
39 : #include "engraving/automation/automationtypes.h"
40 :
41 : namespace muse::uicomponents {
42 : class PolylinePlot;
43 : }
44 :
45 : namespace mu::engraving {
46 : class Staff;
47 : struct ScoreChanges;
48 : }
49 :
50 : namespace mu::notation {
51 : struct DynamicsDisplayRange;
52 :
53 : class NotationAutomationController : public muse::Contextable, public muse::async::Asyncable
54 : {
55 : muse::ContextInject<mu::project::IProjectSession> projectSession = { this };
56 : muse::ContextInject<muse::ui::IUiContextConfiguration> uiContextConfiguration = { this };
57 : muse::GlobalInject<muse::ui::IUiConfiguration> uiConfiguration;
58 : muse::GlobalInject<INotationConfiguration> notationConfiguration;
59 : muse::ContextInject<INotationContextConfiguration> notationContextConfiguration = { this };
60 : muse::GlobalInject<mu::engraving::IEngravingConfiguration> engravingConfiguration;
61 :
62 : public:
63 : NotationAutomationController(QQuickItem* linesParent, const muse::modularity::ContextPtr& iocCtx);
64 :
65 : void init();
66 : void setViewMatrix(const muse::draw::Transform& viewMatrix);
67 :
68 : private:
69 : // A polyline can be uniquely identified using the system, staff, and startTick...
70 : struct PolylineKey {
71 : const System* system = nullptr;
72 : const staff_idx_t staffIdx = muse::nidx;
73 : const int startTick = -1;
74 :
75 0 : bool isValid() const
76 : {
77 0 : return system && !system->measures().empty() && staffIdx != muse::nidx && startTick != -1;
78 : }
79 :
80 : bool operator==(const PolylineKey& k) const
81 : {
82 : IF_ASSERT_FAILED(isValid() && k.isValid()) {
83 : return false;
84 : }
85 : return system == k.system && staffIdx == k.staffIdx && startTick == k.startTick;
86 : }
87 :
88 0 : bool operator<(const PolylineKey& k) const
89 : {
90 0 : IF_ASSERT_FAILED(isValid() && k.isValid()) {
91 0 : return false;
92 : }
93 0 : if (system != k.system) {
94 : // On different systems...
95 0 : return system->first()->index() < k.system->first()->index();
96 : }
97 0 : if (staffIdx != k.staffIdx) {
98 : // On different staves...
99 0 : return staffIdx < k.staffIdx;
100 : }
101 : // On same staff...
102 0 : return startTick < k.startTick;
103 : }
104 : };
105 :
106 : struct PointData {
107 : enum class PointType : unsigned char {
108 : UNKNOWN,
109 : IN,
110 : OUT,
111 : BOTH
112 : };
113 : int polylinePointIndex = -1;
114 : int tick = -1;
115 : QPointF qPointF;
116 : PointType pointType = PointType::UNKNOWN;
117 : bool isPending = false; // true while the user is adding/dragging the point (before committing to the model)
118 : };
119 :
120 : struct PolylineData {
121 : muse::uicomponents::PolylinePlot* polyline = nullptr;
122 : QVector<PointData> pointsData;
123 : //! NOTE: Horizontal boxes can break up the staves of a system, so a polyline covers a "region" - a run
124 : //! of measures bounded by horizontal boxes and/or the system itself...
125 : const Measure* startMeasure = nullptr;
126 : const Measure* endMeasure = nullptr;
127 : };
128 :
129 : struct TickStaffRange {
130 : int tickFrom = -1;
131 : int tickTo = -1;
132 : staff_idx_t staffIdxFrom = muse::nidx;
133 : staff_idx_t staffIdxTo = muse::nidx;
134 : };
135 :
136 : struct PendingScoreState {
137 : bool hasChanges = false;
138 : bool structural = false;
139 : std::optional<TickStaffRange> boundary;
140 : };
141 :
142 : static int tickFromXInPolyline(const PolylineKey& key, const PolylineData& data, qreal x);
143 : QVector<PointData> pointsDataInStaffRegion(const mu::engraving::Staff* staff, const muse::RectF& regionRect, int startTick,
144 : int endTick) const;
145 :
146 : mu::engraving::AutomationType currentAutomationType() const;
147 : mu::engraving::AutomationCurveKey currentCurveKey(const mu::engraving::Staff* staff) const;
148 :
149 : void applyPolylineStyle(muse::uicomponents::PolylinePlot* polyline, const PolylineKey& key) const;
150 : void applyPolylineColors(muse::uicomponents::PolylinePlot* polyline, const PolylineKey& key) const;
151 : // TODO: apply within a range? (for efficiency)
152 : void applyPolylineColorsUnderLine(muse::uicomponents::PolylinePlot* polyline, const PolylineKey& key) const;
153 : void applyPolylineEdgeValues(muse::uicomponents::PolylinePlot* polyline, const PolylineKey& key) const;
154 :
155 : QColor inversionRelativeColor(const muse::ui::ThemeStyleKey& key) const;
156 :
157 : void updatePolylinesGeometry();
158 : void updatePolylinesColors();
159 : void onCurrentNotationChanged();
160 :
161 : void rebuildAllPolylines();
162 : void buildAndAddPolylinesForSystem(const System* system);
163 : void buildAndAddPolylinesForStaff(const System* system, staff_idx_t staffIdx);
164 : void buildAndAddPolylineForStaffRegion(const System* system, const mu::engraving::Staff* staff, const Measure* startMeasure,
165 : const Measure* endMeasure);
166 :
167 : void rebuildPolylinePoints(const PolylineKey& key);
168 :
169 : void mergePendingChanges(const mu::engraving::AutomationChanges& changes);
170 : void mergePendingScoreChanges(const mu::engraving::ScoreChanges& changes);
171 : void scheduleUpdate();
172 : void processPendingChanges();
173 : void applyAutomationChanges(const mu::engraving::AutomationChanges& changes);
174 :
175 : bool requestEditPoint(const PointData& oldPointData, const PolylineKey& key, qreal x, qreal y);
176 : bool requestAddPoint(const PolylineKey& key, qreal x, qreal y, bool useCurveValue);
177 : bool requestRemovePoint(const PointData& pointData, const PolylineKey& key);
178 : void editAutomationPoints(const mu::engraving::AutomationCurveKey& key, mu::engraving::AutomationPointEdits& edits);
179 :
180 : PolylineData polylineData(const PolylineKey& key) const;
181 : void setPolylinePointsData(const PolylineKey& key, const QVector<PointData>& pointsData);
182 :
183 : const mu::engraving::AutomationPoint* automationPointAt(const PolylineKey& key, int tick) const;
184 :
185 : const DynamicsDisplayRange& dynamicsDisplayRange(const mu::engraving::Staff* staff, int tick) const;
186 : double automationValueToDisplay(mu::engraving::AutomationType type, const mu::engraving::Staff* staff, int tick,
187 : muse::real_t value) const;
188 : muse::real_t automationValueFromDisplay(mu::engraving::AutomationType type, const mu::engraving::Staff* staff, int tick,
189 0 : double displayValue, std::optional<muse::real_t> existingValue = std::nullopt) const;
190 :
191 : INotationAutomationPtr automation() const;
192 : mu::engraving::AutomationDataConstPtr automationData() const;
193 : INotationPtr currentNotation() const;
194 : mu::engraving::Score* score() const;
195 :
196 : QQuickItem* m_linesParent = nullptr;
197 :
198 : std::map<PolylineKey, PolylineData> m_polylinesDataMap;
199 : muse::draw::Transform m_viewMatrix;
200 :
201 : mu::engraving::AutomationChanges m_pendingChanges;
202 : PendingScoreState m_pendingScoreState;
203 : bool m_updateScheduled = false;
204 :
205 : muse::uicomponents::PolylinePlot* m_currentlyEditedPolyline = nullptr;
206 : };
207 : }
|