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 : #ifndef MU_PALETTE_MIMEDATAUTILS_H
23 : #define MU_PALETTE_MIMEDATAUTILS_H
24 :
25 : #include "io/buffer.h"
26 :
27 : #include "global/modularity/ioc.h"
28 :
29 : #include "engraving/rw/xmlreader.h"
30 : #include "engraving/rw/xmlwriter.h"
31 :
32 : namespace mu::palette {
33 : template<class T>
34 0 : QByteArray toMimeData(T* t)
35 : {
36 0 : auto buffer = muse::io::Buffer::opened(muse::io::IODevice::WriteOnly);
37 0 : engraving::XmlWriter xml(&buffer);
38 0 : t->write(xml, true);
39 0 : xml.flush();
40 0 : buffer.close();
41 0 : return buffer.data().toQByteArray();
42 0 : }
43 :
44 : template<class T>
45 0 : std::shared_ptr<T> fromMimeData(const QByteArray& data, const muse::AsciiStringView& tagName, const muse::modularity::ContextPtr& iocCtx)
46 : {
47 0 : engraving::XmlReader e(data);
48 0 : while (e.readNextStartElement()) {
49 0 : const muse::AsciiStringView tag(e.name());
50 0 : if (tag == tagName) {
51 0 : std::shared_ptr<T> t(new T(iocCtx));
52 0 : if (!t->read(e, true)) {
53 0 : return nullptr;
54 : }
55 0 : return t;
56 0 : } else {
57 0 : return nullptr;
58 : }
59 : }
60 0 : return nullptr;
61 0 : }
62 : }
63 :
64 : #endif // MU_PALETTE_MIMEDATAUTILS_H
|