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) 2021 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 : #pragma once
23 :
24 : #include <QtEvents>
25 :
26 : #include "../../../internal/inotationviewcontroller.h"
27 :
28 : #include "modularity/ioc.h"
29 : #include "interactive/iinteractive.h"
30 :
31 : #include "async/asyncable.h"
32 :
33 : #include "project/iprojectsession.h"
34 :
35 : #include "notation/inotationinteraction.h"
36 : #include "notation/inotationplayback.h"
37 : #include "notation/inotationconfiguration.h"
38 : #include "notation/inotationcontextconfiguration.h"
39 : #include "notation/types/viewmode.h"
40 :
41 : #include "notationscene/inotationcommandscontroller.h"
42 :
43 : #include "playback/iplaybackcontroller.h"
44 :
45 : #include "global/iglobalconfiguration.h"
46 : #include "ui/idragcontroller.h"
47 : #include "ui/iuiconfiguration.h"
48 : #include "rcommand/icommanddispatcher.h"
49 :
50 : #include "abstractelementpopupmodel.h"
51 :
52 : class QQuickItem;
53 :
54 : namespace mu::notation {
55 : class IControlledView
56 : {
57 : public:
58 17 : virtual ~IControlledView() = default;
59 :
60 : virtual qreal width() const = 0;
61 : virtual qreal height() const = 0;
62 :
63 : virtual muse::PointF viewportTopLeft() const = 0;
64 :
65 : //! muse::Returns true if the canvas has been moved
66 : virtual bool moveCanvas(qreal dx, qreal dy) = 0;
67 : virtual void moveCanvasHorizontal(qreal dx) = 0;
68 : virtual void moveCanvasVertical(qreal dy) = 0;
69 :
70 : virtual muse::RectF notationContentRect() const = 0;
71 : virtual qreal currentScaling() const = 0;
72 : virtual void setScaling(qreal scaling, const muse::PointF& pos, bool overrideZoomType = true) = 0;
73 :
74 : virtual muse::PointF toLogical(const muse::PointF& p) const = 0;
75 : virtual muse::PointF toLogical(const QPointF& p) const = 0;
76 : virtual muse::PointF fromLogical(const muse::PointF& r) const = 0;
77 : virtual muse::RectF fromLogical(const muse::RectF& r) const = 0;
78 :
79 : virtual bool isNoteEnterMode() const = 0;
80 :
81 : virtual void showContextMenu(const ElementType& elementType, const QPointF& pos) = 0;
82 : virtual void hideContextMenu() = 0;
83 :
84 : virtual void showSearch() = 0;
85 :
86 : virtual void showElementPopup(const ElementType& elementType) = 0;
87 : virtual void hideElementPopup(const ElementType& elementType) = 0;
88 : virtual void hideElementPopup(PopupModelType modelType = PopupModelType::TYPE_UNDEFINED) = 0;
89 : virtual void toggleElementPopup(const ElementType& elementType) = 0;
90 :
91 : virtual bool elementPopupIsOpen(const ElementType& elementType) const = 0;
92 :
93 : virtual INotationInteractionPtr notationInteraction() const = 0;
94 : virtual INotationPlaybackPtr notationPlayback() const = 0;
95 :
96 : virtual QQuickItem* asItem() = 0;
97 :
98 : virtual void scheduleRedraw(const muse::RectF& rect = muse::RectF()) = 0;
99 : };
100 :
101 : class NotationViewInputController : public INotationViewController, public muse::Contextable, public muse::async::Asyncable
102 : {
103 : public:
104 : muse::GlobalInject<muse::IGlobalConfiguration> globalConfiguration;
105 : muse::GlobalInject<INotationConfiguration> configuration;
106 : muse::GlobalInject<muse::ui::IUiConfiguration> uiConfiguration;
107 : muse::ContextInject<INotationContextConfiguration> contextConfiguration = { this };
108 : muse::ContextInject<muse::rcommand::ICommandDispatcher> commandDispatcher = { this };
109 : muse::ContextInject<playback::IPlaybackController> playbackController = { this };
110 : muse::ContextInject<project::IProjectSession> projectSession = { this };
111 : muse::ContextInject<muse::ui::IDragController> dragController = { this };
112 : muse::ContextInject<muse::IInteractive> interactive = { this };
113 : muse::ContextInject<INotationCommandsController> commandsController = { this };
114 :
115 : public:
116 : NotationViewInputController(IControlledView* view, const muse::modularity::ContextPtr& iocCtx);
117 : ~NotationViewInputController();
118 :
119 : void init();
120 : void deinit();
121 :
122 : // INotationViewController
123 : void zoomIn() override;
124 : void zoomOut() override;
125 : void zoomToPageWidth() override;
126 : void zoomToWholePage() override;
127 : void zoomToTwoPages() override;
128 : void setZoom(int zoomPercentage) override;
129 :
130 : void setViewMode(ViewMode viewMode) override;
131 :
132 : void nextScreen() override;
133 : void previousScreen() override;
134 : void nextPage() override;
135 : void previousPage() override;
136 : void startOfScore() override;
137 : void endOfScore() override;
138 :
139 : void openContextMenuOfSelection() override;
140 :
141 : void togglePopupForItemIfSupports(const EngravingItem* item) override;
142 :
143 : void showSearch() override;
144 :
145 : void redrawView() override;
146 : // -----------------------
147 :
148 : void initZoom();
149 : void updateZoomAfterSizeChange();
150 :
151 : void initCanvasPos();
152 :
153 : bool readonly() const;
154 : void setReadonly(bool readonly);
155 :
156 : void pinchToZoom(qreal scaleFactor, const QPointF& pos);
157 : void wheelEvent(QWheelEvent* event);
158 : void mousePressEvent(QMouseEvent* event);
159 : void mouseMoveEvent(QMouseEvent* event);
160 : void mouseReleaseEvent(QMouseEvent* event);
161 : void mouseDoubleClickEvent(QMouseEvent* event);
162 : void hoverMoveEvent(QHoverEvent* event);
163 : void hoverLeaveEvent(QHoverEvent* event);
164 : bool shortcutOverrideEvent(QKeyEvent* event);
165 : void keyPressEvent(QKeyEvent* event);
166 : void keyReleaseEvent(QKeyEvent* event);
167 : void inputMethodEvent(QInputMethodEvent* event);
168 :
169 : bool canHandleInputMethodQuery(Qt::InputMethodQuery query) const;
170 : QVariant inputMethodQuery(Qt::InputMethodQuery query) const;
171 :
172 0 : bool ignoreNextMouseContextMenuEvent() const { return m_ignoreNextMouseContextMenuEvent; }
173 :
174 : void dragEnterEvent(QDragEnterEvent* event);
175 : void dragLeaveEvent(QDragLeaveEvent* event);
176 : void dragMoveEvent(QDragMoveEvent* event);
177 : void dropEvent(QDropEvent* event);
178 :
179 : ElementType selectionType() const;
180 : muse::PointF selectionElementPos() const;
181 :
182 : private:
183 : INotationPtr currentNotation() const;
184 : INotationStylePtr notationStyle() const;
185 : INotationInteractionPtr viewInteraction() const;
186 : const INotationInteraction::HitElementContext& hitElementContext() const;
187 :
188 : void onNotationChanged();
189 :
190 : void doZoomToPageWidth();
191 : void doZoomToWholePage();
192 : void doZoomToTwoPages();
193 : int currentZoomIndex() const;
194 : int currentZoomPercentage() const;
195 : muse::PointF findZoomFocusPoint() const;
196 : void doSetZoom(int zoomPercentage, const muse::PointF& pos);
197 : qreal scalingFromZoomPercentage(int zoomPercentage) const;
198 : int zoomPercentageFromScaling(qreal scaling) const;
199 :
200 : void moveScreen(int direction);
201 : void movePage(int direction);
202 :
203 : void setScaling(qreal scaling, const muse::PointF& pos = muse::PointF(), bool overrideZoomType = true);
204 :
205 : void startDragElements(ElementType elementsType, const muse::PointF& elementsOffset);
206 :
207 : void updateShadowNotePopupVisibility(bool forceHide = false);
208 :
209 : float hitWidth() const;
210 :
211 : struct ClickContext {
212 : muse::PointF logicClickPos;
213 : const QMouseEvent* event = nullptr;
214 : mu::engraving::EngravingItem* hitElement = nullptr;
215 : mu::engraving::staff_idx_t hitStaff = muse::nidx;
216 : bool isHitGrip = false;
217 : };
218 :
219 : void handleClickInNoteInputMode(QMouseEvent* event);
220 : bool mousePress_considerGrip(const ClickContext& ctx); // returns true if event is consumed
221 : bool mousePress_considerDragOutgoingElement(const ClickContext& ctx);
222 : void mousePress_considerSelect(const ClickContext& ctx);
223 : void mousePress_seekSelection(const ClickContext& ctx);
224 : void cycleOverlappingHitElements(const std::vector<EngravingItem*>& hitElements, staff_idx_t hitStaffIndex);
225 : bool mousePress_considerDragOutgoingRange(const ClickContext& ctx);
226 : bool mousePress_considerStartPasteRangeOnRelease(const ClickContext& ctx);
227 : void handleLeftClick(const ClickContext& ctx);
228 : void handleRightClick();
229 : void handleLeftClickRelease(const QPointF& releasePoint);
230 :
231 : bool startTextEditingAllowed() const;
232 : void updateTextCursorPosition();
233 :
234 : bool isAnchorEditingEvent(QKeyEvent* event) const;
235 :
236 : bool tryPercussionShortcut(QKeyEvent* event);
237 :
238 : IControlledView* m_view = nullptr;
239 :
240 : QList<int> m_possibleZoomPercentages;
241 :
242 : bool m_readonly = false;
243 : bool m_isCanvasDragged = false;
244 : bool m_tripleClickPending = false;
245 :
246 : struct MouseDownInfo {
247 : enum DragAction {
248 : DragOutgoingElement,
249 : DragOutgoingRange,
250 : PasteRangeOnRelease,
251 : Other,
252 : Nothing
253 : } dragAction = Other;
254 :
255 : QPointF physicalBeginPoint;
256 : muse::PointF logicalBeginPoint;
257 : } m_mouseDownInfo;
258 :
259 : struct DragMoveEvent {
260 : QPointF position;
261 : Qt::KeyboardModifiers modifiers;
262 : Qt::DropAction dropAction = Qt::DropAction::CopyAction;
263 : QObject* source = nullptr;
264 : };
265 :
266 : bool dropEvent(const DragMoveEvent& event, const QMimeData* mimeData = nullptr);
267 :
268 : std::vector<int> pitchesBeingDragged() const;
269 :
270 : DragMoveEvent m_lastDragMoveEvent;
271 : bool m_isDropConfirmationPending = false;
272 :
273 : const mu::engraving::EngravingItem* m_prevSelectedElement = nullptr;
274 : std::vector<const mu::engraving::EngravingItem*> m_notesBeingDragged;
275 :
276 : bool m_hitElementWasAlreadySingleSelected = false;
277 : bool m_shouldSelectOnLeftClickRelease = false;
278 : bool m_shouldStartEditOnLeftClickRelease = false;
279 : bool m_ignoreNextMouseContextMenuEvent = false;
280 : };
281 : }
|