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 : #include "notationviewstate.h"
23 :
24 : #include <QJsonDocument>
25 : #include <QJsonObject>
26 :
27 : #include "engraving/infrastructure/mscreader.h"
28 : #include "engraving/infrastructure/mscwriter.h"
29 :
30 : #include "notation.h"
31 :
32 : using namespace mu;
33 : using namespace mu::notation;
34 : using namespace muse;
35 : using namespace muse::draw;
36 :
37 0 : static ViewMode viewModeFromString(const QString& str)
38 : {
39 0 : if ("page" == str) {
40 0 : return ViewMode::PAGE;
41 : }
42 :
43 0 : if ("float" == str) {
44 0 : return ViewMode::FLOAT;
45 : }
46 :
47 0 : if ("continuous_v" == str) {
48 0 : return ViewMode::LINE;
49 : }
50 :
51 0 : if ("continuous_h" == str) {
52 0 : return ViewMode::SYSTEM;
53 : }
54 :
55 0 : if ("continuous_h_fixed" == str) {
56 0 : return ViewMode::HORIZONTAL_FIXED;
57 : }
58 :
59 0 : return ViewMode::PAGE;
60 : }
61 :
62 0 : static QString viewModeToString(ViewMode m)
63 : {
64 0 : switch (m) {
65 0 : case ViewMode::PAGE: return "page";
66 0 : case ViewMode::FLOAT: return "float";
67 0 : case ViewMode::LINE: return "continuous_v";
68 0 : case ViewMode::SYSTEM: return "continuous_h";
69 0 : case ViewMode::HORIZONTAL_FIXED: return "continuous_h_fixed";
70 : }
71 0 : return "";
72 : }
73 :
74 0 : NotationViewState::NotationViewState(Notation* notation, const modularity::ContextPtr& ctx)
75 0 : : muse::Contextable(ctx)
76 : {
77 0 : notation->openChanged().onNotify(this, [this, notation]() {
78 0 : if (!notation->isOpen()) {
79 0 : m_isMatrixInited = false;
80 0 : setMatrix(Transform(), nullptr);
81 : }
82 0 : });
83 0 : }
84 :
85 0 : Ret NotationViewState::read(const engraving::MscReader& reader, const muse::io::path_t& pathPrefix)
86 : {
87 0 : ByteArray json = reader.readViewSettingsJsonFile(pathPrefix);
88 0 : QJsonObject rootObj = QJsonDocument::fromJson(json.toQByteArrayNoCopy()).object();
89 0 : QJsonObject notationObj = rootObj.value("notation").toObject();
90 :
91 0 : m_viewMode = viewModeFromString(notationObj.value("viewMode").toString());
92 :
93 0 : return make_ret(Ret::Code::Ok);
94 0 : }
95 :
96 0 : Ret NotationViewState::write(engraving::MscWriter& writer, const muse::io::path_t& pathPrefix)
97 : {
98 0 : QJsonObject notationObj;
99 0 : notationObj["viewMode"] = viewModeToString(m_viewMode);
100 :
101 0 : QJsonObject rootObj;
102 0 : rootObj["notation"] = notationObj;
103 :
104 0 : QByteArray json = QJsonDocument(rootObj).toJson();
105 0 : writer.writeViewSettingsJsonFile(ByteArray::fromQByteArrayNoCopy(json), pathPrefix);
106 :
107 0 : return make_ret(Ret::Code::Ok);
108 0 : }
109 :
110 0 : bool NotationViewState::isMatrixInited() const
111 : {
112 0 : return m_isMatrixInited;
113 : }
114 :
115 0 : void NotationViewState::setMatrixInited(bool inited)
116 : {
117 0 : m_isMatrixInited = inited;
118 0 : }
119 :
120 0 : const Transform& NotationViewState::matrix() const
121 : {
122 0 : return m_matrix;
123 : }
124 :
125 0 : muse::async::Channel<Transform, NotationPaintView*> NotationViewState::matrixChanged() const
126 : {
127 0 : return m_matrixChanged;
128 : }
129 :
130 0 : void NotationViewState::setMatrix(const Transform& matrix, NotationPaintView* sender)
131 : {
132 0 : if (m_matrix == matrix) {
133 0 : return;
134 : }
135 :
136 0 : m_matrix = matrix;
137 0 : m_matrixChanged.send(matrix, sender);
138 :
139 0 : int newZoomPercentage = configuration()->zoomPercentageFromScaling(matrix.m11());
140 0 : if (m_zoomPercentage.val != newZoomPercentage) {
141 0 : m_zoomPercentage.set(newZoomPercentage);
142 : }
143 : }
144 :
145 0 : ValCh<int> NotationViewState::zoomPercentage() const
146 : {
147 0 : return m_zoomPercentage;
148 : }
149 :
150 0 : ValCh<ZoomType> NotationViewState::zoomType() const
151 : {
152 0 : return m_zoomType;
153 : }
154 :
155 0 : void NotationViewState::setZoomType(ZoomType type)
156 : {
157 0 : if (m_zoomType.val != type) {
158 0 : m_zoomType.set(type);
159 : }
160 0 : }
161 :
162 0 : ViewMode NotationViewState::viewMode() const
163 : {
164 0 : return m_viewMode;
165 : }
166 :
167 0 : void NotationViewState::setViewMode(const ViewMode& mode)
168 : {
169 0 : if (m_viewMode == mode) {
170 0 : return;
171 : }
172 :
173 0 : m_viewMode = mode;
174 0 : m_stateChanged.notify();
175 : }
176 :
177 0 : int NotationViewState::styleDialogLastPageIndex() const
178 : {
179 0 : return m_styleDialogLastPageIndex;
180 : }
181 :
182 0 : void NotationViewState::setStyleDialogLastPageIndex(int value)
183 : {
184 0 : if (m_styleDialogLastPageIndex == value) {
185 0 : return;
186 : }
187 0 : m_styleDialogLastPageIndex = value;
188 0 : m_stateChanged.notify();
189 : }
190 :
191 0 : int NotationViewState::styleDialogLastSubPageIndex() const
192 : {
193 0 : return m_styleDialogLastSubPageIndex;
194 : }
195 :
196 0 : void NotationViewState::setStyleDialogLastSubPageIndex(int value)
197 : {
198 0 : if (m_styleDialogLastSubPageIndex == value) {
199 0 : return;
200 : }
201 0 : m_styleDialogLastSubPageIndex = value;
202 0 : m_stateChanged.notify();
203 : }
204 :
205 0 : void NotationViewState::makeDefault()
206 : {
207 0 : m_viewMode = ViewMode::PAGE;
208 0 : m_styleDialogLastPageIndex = 0;
209 0 : m_styleDialogLastSubPageIndex = 0;
210 0 : m_stateChanged.notify();
211 0 : }
212 :
213 0 : muse::async::Notification NotationViewState::stateChanged() const
214 : {
215 0 : return m_stateChanged;
216 : }
|