Skip to content

Commit f6b87e1

Browse files
dmitrio95XiaoMigros
authored andcommitted
Plugin API: partially restore readScore, closeScore and writeScore
1 parent dbefa5f commit f6b87e1

7 files changed

Lines changed: 93 additions & 16 deletions

File tree

src/engraving/api/v1/qmlpluginapi.cpp

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,14 @@
2626
#include <QJSValueIterator>
2727

2828
#include "engraving/compat/scoreaccess.h"
29+
#include "engraving/dom/masterscore.h"
2930
#include "engraving/dom/factory.h"
3031
#include "engraving/dom/interval.h"
3132
#include "engraving/types/types.h"
3233

34+
#include "notation/inotation.h"
35+
#include "project/inotationwriter.h"
36+
3337
// api
3438
#include "apitypes.h"
3539
#include "engravingapiv1.h"
@@ -237,14 +241,52 @@ QQmlListProperty<apiv1::Score> PluginAPI::scores()
237241
bool PluginAPI::writeScore(Score* s, const QString& name, const QString& ext)
238242
{
239243
if (!s || !s->score()) {
244+
LOGW("PluginAPI::writeScore: no score provided");
240245
return false;
241246
}
242247

243-
UNUSED(name);
244-
UNUSED(ext);
248+
if (s->score() != currentScore()) {
249+
LOGW("PluginAPI::writeScore: only writing the selected score is currently supported");
250+
return false;
251+
}
245252

246-
NOT_IMPLEMENTED;
247-
return false;
253+
const notation::INotationPtr notation = context()->currentNotation();
254+
255+
if (!notation) {
256+
LOGW("PluginAPI::writeScore: no notation found");
257+
return false;
258+
}
259+
260+
const auto unitType = determineWriterUnitType(ext.toStdString());
261+
262+
if (!unitType) {
263+
LOGW("PluginAPI::writeScore: '%s' format is not supported", ext.toUtf8().constData());
264+
return false;
265+
}
266+
267+
const QString outPath = name.endsWith(ext) ? name : (name + '.' + ext);
268+
return exportProjectScenario()->exportScores({ notation }, outPath, *unitType, /* openDestinationFolderOnExport */ false);
269+
}
270+
271+
std::optional<project::INotationWriter::UnitType> PluginAPI::determineWriterUnitType(const std::string& ext) const
272+
{
273+
const project::INotationWriterPtr writer = writers()->writer(ext);
274+
275+
if (!writer) {
276+
return std::nullopt;
277+
}
278+
279+
project::INotationWriter::UnitType unitType;
280+
281+
if (writer->supportsUnitType(project::INotationWriter::UnitType::PER_PAGE)) {
282+
unitType = project::INotationWriter::UnitType::PER_PAGE;
283+
} else if (writer->supportsUnitType(project::INotationWriter::UnitType::PER_PART)) {
284+
unitType = project::INotationWriter::UnitType::PER_PART;
285+
} else {
286+
unitType = project::INotationWriter::UnitType::MULTI_PART;
287+
}
288+
289+
return unitType;
248290
}
249291

250292
//---------------------------------------------------------
@@ -259,11 +301,21 @@ bool PluginAPI::writeScore(Score* s, const QString& name, const QString& ext)
259301

260302
apiv1::Score* PluginAPI::readScore(const QString& name, bool noninteractive)
261303
{
262-
UNUSED(name);
263-
UNUSED(noninteractive);
304+
const bool hadScoreOpened = currentScore();
264305

265-
NOT_IMPLEMENTED;
266-
return nullptr;
306+
if (hadScoreOpened) {
307+
LOGW("PluginAPI::readScore: will open a score in a new window");
308+
}
309+
310+
if (noninteractive) {
311+
LOGW("PluginAPI::readScore: noninteractive flag is not yet implemented");
312+
}
313+
314+
const muse::io::path_t path(name);
315+
const project::ProjectFile file(path);
316+
const muse::Ret ret = projectFilesController()->openProject(file);
317+
318+
return (ret.success() && !hadScoreOpened) ? curScore() : nullptr;
267319
}
268320

269321
//---------------------------------------------------------
@@ -272,9 +324,17 @@ apiv1::Score* PluginAPI::readScore(const QString& name, bool noninteractive)
272324

273325
void PluginAPI::closeScore(apiv1::Score* score)
274326
{
275-
UNUSED(score);
327+
if (!score || !score->score()) {
328+
LOGW("PluginAPI::closeScore: no score provided");
329+
return;
330+
}
276331

277-
NOT_IMPLEMENTED;
332+
if (score->score() != currentScore()) {
333+
LOGW("PluginAPI::closeScore: only closing the selected score is currently supported");
334+
return;
335+
}
336+
337+
projectFilesController()->closeOpenedProject();
278338
}
279339

280340
//---------------------------------------------------------

src/engraving/api/v1/qmlpluginapi.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@
3131
#include "global/iapplication.h"
3232

3333
#include "enums.h"
34+
#include "engraving/dom/score.h"
35+
#include "engraving/dom/types.h"
36+
#include "engraving/types/types.h"
37+
#include "project/iexportprojectscenario.h"
38+
#include "project/inotationwritersregister.h"
39+
#include "project/iprojectfilescontroller.h"
40+
3441
#include "apitypes.h"
3542
#include "cursor.h"
3643

@@ -89,6 +96,12 @@ class PluginAPI : public QQuickItem, public muse::extensions::apiv1::IPluginApiV
8996
{
9097
Q_OBJECT
9198

99+
INJECT(muse::actions::IActionsDispatcher, actionsDispatcher)
100+
INJECT(mu::context::IGlobalContext, context)
101+
INJECT(mu::project::INotationWritersRegister, writers)
102+
INJECT(mu::project::IExportProjectScenario, exportProjectScenario)
103+
INJECT(mu::project::IProjectFilesController, projectFilesController)
104+
92105
/** Path where the plugin is placed in menu */
93106
Q_PROPERTY(QString menuPath READ menuPath WRITE setMenuPath)
94107
/** Title of this plugin */
@@ -129,9 +142,9 @@ class PluginAPI : public QQuickItem, public muse::extensions::apiv1::IPluginApiV
129142
/** List of currently open scores (read only).\n \since MuseScore 3.2 */
130143
Q_PROPERTY(QQmlListProperty<mu::engraving::apiv1::Score> scores READ scores)
131144

132-
public:
133-
muse::Inject<muse::actions::IActionsDispatcher> actionsDispatcher = { this };
134145
muse::Inject<mu::context::IGlobalContext> context = { this };
146+
147+
public:
135148
muse::Inject<muse::IApplication> application = { this };
136149

137150
public:
@@ -537,6 +550,7 @@ class PluginAPI : public QQuickItem, public muse::extensions::apiv1::IPluginApiV
537550
QString m_thumbnailName;
538551
QString m_categoryCode;
539552
muse::async::Notification m_closeRequested;
553+
std::optional<project::INotationWriter::UnitType> determineWriterUnitType(const std::string& ext) const;
540554
};
541555

542556
#undef DECLARE_API_ENUM2

src/project/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ set(MODULE_SRC
5050
${CMAKE_CURRENT_LIST_DIR}/projectmodule.h
5151
${CMAKE_CURRENT_LIST_DIR}/projecterrors.h
5252
${CMAKE_CURRENT_LIST_DIR}/projectextensionpoints.h
53+
${CMAKE_CURRENT_LIST_DIR}/iexportprojectscenario.h
5354
${CMAKE_CURRENT_LIST_DIR}/inotationproject.h
5455
${CMAKE_CURRENT_LIST_DIR}/iprojectcreator.h
5556
${CMAKE_CURRENT_LIST_DIR}/inotationreader.h
@@ -89,7 +90,6 @@ set(MODULE_SRC
8990
${CMAKE_CURRENT_LIST_DIR}/internal/projectconfiguration.h
9091
${CMAKE_CURRENT_LIST_DIR}/internal/exporttype.cpp
9192
${CMAKE_CURRENT_LIST_DIR}/internal/exporttype.h
92-
${CMAKE_CURRENT_LIST_DIR}/internal/iexportprojectscenario.h
9393
${CMAKE_CURRENT_LIST_DIR}/internal/exportprojectscenario.cpp
9494
${CMAKE_CURRENT_LIST_DIR}/internal/exportprojectscenario.h
9595
${CMAKE_CURRENT_LIST_DIR}/internal/iopensaveprojectscenario.h

src/project/internal/iexportprojectscenario.h renamed to src/project/iexportprojectscenario.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#include "modularity/imoduleinterface.h"
2626
#include "notation/inotation.h"
2727
#include "inotationwriter.h"
28-
#include "exporttype.h"
28+
#include "internal/exporttype.h"
2929
#include "types/projecttypes.h"
3030

3131
namespace mu::project {

src/project/internal/exportprojectscenario.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,9 @@ bool ExportProjectScenario::exportScores(const notation::INotationPtrList& notat
228228
} break;
229229
case INotationWriter::UnitType::MULTI_PART: {
230230
auto exportFunction = [writer, notations, options](IODevice& destinationDevice) {
231+
if (notations.size() == 1) {
232+
return writer->write(notations.front(), destinationDevice, options);
233+
}
231234
return writer->writeList(notations, destinationDevice, options);
232235
};
233236

src/project/internal/projectactionscontroller.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@
3737
#include "cloud/audiocom/iaudiocomservice.h"
3838
#include "playback/iplaybackcontroller.h"
3939
#include "print/iprintprovider.h"
40+
#include "iexportprojectscenario.h"
4041
#include "inotationreadersregister.h"
4142
#include "iopensaveprojectscenario.h"
4243
#include "imscmetareader.h"
4344
#include "io/ifilesystem.h"
44-
#include "internal/iexportprojectscenario.h"
4545
#include "notation/inotationconfiguration.h"
4646
#include "musesounds/imusesoundscheckupdatescenario.h"
4747
#include "extensions/iextensionsprovider.h"

src/project/view/exportdialogmodel.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@
3636
#include "importexport/audioexport/iaudioexportconfiguration.h"
3737
#include "importexport/mei/imeiconfiguration.h"
3838

39+
#include "iexportprojectscenario.h"
3940
#include "inotationwritersregister.h"
4041
#include "iprojectconfiguration.h"
41-
#include "internal/iexportprojectscenario.h"
4242
#include "types/projecttypes.h"
4343

4444
class QItemSelectionModel;

0 commit comments

Comments
 (0)