MuseScore  3.4
Music composition and notation
palettetree.h
Go to the documentation of this file.
1 //=============================================================================
2 // MuseScore
3 // Music Composition & Notation
4 //
5 // Copyright (C) 2019 Werner Schweer and others
6 //
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License version 2.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 //=============================================================================
19 
20 #ifndef __PALETTETREE_H__
21 #define __PALETTETREE_H__
22 
23 #include "libmscore/element.h"
24 #include "libmscore/xml.h"
25 
26 namespace Ms {
27 
28 struct PaletteCell;
29 using PaletteCellPtr = std::shared_ptr<PaletteCell>;
30 using PaletteCellConstPtr = std::shared_ptr<const PaletteCell>;
31 
32 //---------------------------------------------------------
33 // PaletteCell
34 //---------------------------------------------------------
35 
36 struct PaletteCell {
37  std::unique_ptr<Element> element;
38  std::unique_ptr<Element> untranslatedElement;
39  QString name; // used for tool tip
40  QString tag;
41 
42  bool drawStaff { false };
43  double x { 0.0 }; // TODO: remove?
44  double y { 0.0 }; // TODO: remove?
45  double xoffset { 0.0 };
46  double yoffset { 0.0 }; // in spatium units of "gscore"
47  qreal mag { 1.0 };
48  bool readOnly { false };
49 
50  bool visible { true };
51  bool custom { false };
52  bool active { false };
53 
54  PaletteCell() = default;
55  PaletteCell(std::unique_ptr<Element> e, const QString& _name, QString _tag = QString(), qreal _mag = 1.0);
56 
57  static constexpr const char* mimeDataFormat = "application/musescore/palette/cell";
58 
59  const char* translationContext() const;
60  QString translatedName() const;
61 
62  void retranslate();
63  void setElementTranslated(bool translate);
64 
65  void write(XmlWriter& xml) const;
66  bool read(XmlReader&);
67  QByteArray mimeData() const;
68  static PaletteCellPtr readMimeData(const QByteArray& data);
69  static PaletteCellPtr readElementMimeData(const QByteArray& data);
70  };
71 
72 //---------------------------------------------------------
73 // PaletteCellIconEngine
74 //---------------------------------------------------------
75 
76 class PaletteCellIconEngine : public QIconEngine {
78  qreal _extraMag = 1.0;
79 
80  PaletteCellConstPtr cell() const { return _cell; }
81 
82  public:
83  PaletteCellIconEngine(PaletteCellConstPtr cell, qreal extraMag = 1.0)
84  : _cell(cell), _extraMag(extraMag) {}
85 
86  QIconEngine* clone() const override { return new PaletteCellIconEngine(cell(), _extraMag); }
87 
88  void paint(QPainter* painter, const QRect& rect, QIcon::Mode mode, QIcon::State state) override;
89  };
90 
91 //---------------------------------------------------------
92 // PalettePanel
93 //---------------------------------------------------------
94 
95 class PalettePanel {
96  Q_GADGET
97  public:
98  enum class Type {
99  Unknown = 0,
100  Clef,
101  KeySig,
102  TimeSig,
103  Bracket,
104  Accidental,
105  Articulation,
106  Ornament,
107  Breath,
108  GraceNote,
109  NoteHead,
110  Line,
111  BarLine,
112  Arpeggio,
113  Tremolo,
114  Text,
115  Tempo,
116  Dynamic,
117  Fingering,
118  Repeat,
119  FretboardDiagram,
120  Accordion,
122  Break,
123  Frame,
124  Beam,
125  Custom
126  };
127  Q_ENUM(Type);
128 
129  private:
130  QString _name;
132 
133  std::vector<PaletteCellPtr> cells;
134 
135  QSize _gridSize = QSize(64, 64);
136 // int hgrid;
137 // int vgrid;
138 
139  qreal _mag = 1.0;
140  bool _drawGrid = false;
141  bool _editable = true;
142 // bool _systemPalette;
143  qreal _yOffset = 0.0; // in spatium units of "gscore"
144 
145  bool _moreElements = false; // not used by QML palettes, default is false for compatibility with Palette class. TODO: remove?
146 
147  bool _visible = true;
148  bool _expanded = false;
149 
150  Type guessType() const;
151 
152  public:
153  PalettePanel(Type t = Type::Custom) : _type(t) {}
154 
155  PaletteCell* insert(int idx, Element* e, const QString& name, QString tag = QString(), qreal mag = 1.0);
156  PaletteCell* append(Element* e, const QString& name, QString tag = QString(), qreal mag = 1.0);
157 
158  const QString& name() const { return _name; }
159  void setName(const QString& str) { _name = str; }
160 
161  QString translatedName() const { return qApp->translate("Palette", name().toUtf8()); }
162 
163  QSize gridSize() const { return _gridSize; }
164  void setGrid(QSize s) { _gridSize = s; }
165  void setGrid(int w, int h) { _gridSize = QSize(w, h); }
166 
167  qreal mag() const { return _mag; }
168  void setMag(qreal val) { _mag = val; }
169 
170  bool drawGrid() const { return _drawGrid; }
171  void setDrawGrid(bool val) { _drawGrid = val; }
172 
173  qreal yOffset() const { return _yOffset; }
174  void setYOffset(qreal val) { _yOffset = val; }
175 
176  bool moreElements() const { return _moreElements; }
177  void setMoreElements(bool val) { _moreElements = val; }
178 
179  bool visible() const { return _visible; }
180  void setVisible(bool val) { _visible = val; }
181  bool custom() const { return _type == Type::Custom; }
182 
183  static constexpr const char* mimeDataFormat = "application/musescore/palette/panel";
184 
185  void write(XmlWriter&) const;
186  bool read(XmlReader&);
187  QByteArray mimeData() const;
188  static std::unique_ptr<PalettePanel> readMimeData(const QByteArray& data);
189 
190  bool readFromFile(const QString& path);
191  bool writeToFile(const QString& path) const;
192 
193  int ncells() const { return int(cells.size()); }
194  bool empty() const { return cells.empty(); }
195  PaletteCellPtr cell(int idx) { return cells[idx]; }
196  PaletteCellConstPtr cell(int idx) const { return cells[idx]; }
197 
198  std::vector<PaletteCellPtr> takeCells(int idx, int count);
199  bool insertCells(int idx, std::vector<PaletteCellPtr> cells);
200  bool insertCell(int idx, PaletteCellPtr cell);
201 
202  int findPaletteCell(const PaletteCell& cell, bool matchName = true) const;
203 
204  bool expanded() const { return _expanded; }
205  void setExpanded(bool val) { _expanded = val; }
206 
207  bool editable() const { return _editable; }
208  void setEditable(bool val) { _editable = val; }
209 
210  Type type() const { return _type; }
211  void setType(Type t) { _type = t; }
212 
213  Type contentType() const;
214 
215  void retranslate();
216  };
217 
218 //---------------------------------------------------------
219 // PaletteTree
220 //---------------------------------------------------------
221 
222 struct PaletteTree {
223  std::vector<std::unique_ptr<PalettePanel>> palettes;
224 
225  void write(XmlWriter&) const;
226  bool read(XmlReader&);
227 
228  void insert(int idx, PalettePanel*);
229  void append(PalettePanel*);
230 
231  void retranslate();
232  };
233 
234 } // namespace Ms
235 
236 Q_DECLARE_METATYPE(const Ms::PaletteCell*);
237 Q_DECLARE_METATYPE(Ms::PaletteCell*);
238 
239 #endif
void setExpanded(bool val)
Definition: palettetree.h:205
bool visible
Definition: palettetree.h:50
bool moreElements() const
Definition: palettetree.h:176
QString translatedName() const
Definition: palettetree.h:161
Definition: beam.h:37
double y
Definition: palettemodel_list.h:38
Definition: bagpembell.h:51
Definition: xml.h:67
qreal mag() const
Definition: palettetree.h:167
std::shared_ptr< const PaletteCell > PaletteCellConstPtr
Definition: palettetree.h:30
qreal mag
Definition: palettemodel_list.h:41
void setMoreElements(bool val)
Definition: palettetree.h:177
void setGrid(QSize s)
Definition: palettetree.h:164
static PaletteCellPtr readMimeData(const QByteArray &data)
Definition: palettetree.cpp:307
bool read(XmlReader &)
Definition: palettetree.cpp:244
QString _name
Definition: palettetree.h:127
Definition: tremolo.h:39
PalettePanel(Type t=Type::Custom)
Definition: palettetree.h:153
PaletteCellConstPtr cell() const
Definition: palettetree.h:80
void setEditable(bool val)
Definition: palettetree.h:208
void setVisible(bool val)
Definition: palettetree.h:180
QString name
Definition: palettemodel_list.h:33
std::unique_ptr< Element > untranslatedElement
Definition: palettetree.h:38
bool active
Definition: palettetree.h:52
Base class of score layout elements.
Definition: element.h:158
Definition: palettetree.h:95
double x
Definition: palettemodel_list.h:37
bool expanded() const
Definition: palettetree.h:204
bool drawGrid() const
Definition: palettetree.h:170
QString tag
Definition: palettemodel_list.h:34
std::unique_ptr< Element > element
Definition: palettemodel_list.h:32
void write(XmlWriter &xml) const
Definition: palettetree.cpp:205
Definition: accidental.h:65
Type
Definition: palettetree.h:98
void setMag(qreal val)
Definition: palettetree.h:168
bool editable() const
Definition: palettetree.h:207
bool drawStaff
Definition: palettemodel_list.h:36
void retranslate()
Retranslates cell content, e.g.
Definition: palettetree.cpp:176
Definition: arpeggio.h:30
void setYOffset(qreal val)
Definition: palettetree.h:174
std::vector< std::unique_ptr< PalettePanel > > palettes
Definition: palettetree.h:223
Definition: bracket.h:29
const QString & name() const
Definition: palettetree.h:158
static constexpr const char * mimeDataFormat
Definition: palettetree.h:57
const char * translationContext() const
Definition: palettetree.cpp:119
std::vector< PaletteCellPtr > cells
Definition: palettetree.h:133
qreal yOffset() const
Definition: palettetree.h:173
Definition: palettemodel_list.h:31
double yoffset
Definition: palettemodel_list.h:40
double xoffset
Definition: palettemodel_list.h:39
articulation marks
Definition: articulation.h:57
Type type() const
Definition: palettetree.h:210
dynamics marker; determines midi velocity
Definition: dynamic.h:31
Definition: palettemodel_list.h:55
void setGrid(int w, int h)
Definition: palettetree.h:165
Definition: aeolus.cpp:26
Definition: note.h:53
PaletteCellConstPtr cell(int idx) const
Definition: palettetree.h:196
bool custom
Definition: palettetree.h:51
Definition: xml.h:218
int ncells() const
Definition: palettetree.h:193
bool empty() const
Definition: palettetree.h:194
bool readOnly
Definition: palettemodel_list.h:42
QIconEngine * clone() const override
Definition: palettetree.h:86
bool custom() const
Definition: palettetree.h:181
Definition: fingering.h:24
QByteArray mimeData() const
Definition: palettetree.cpp:350
Type _type
Definition: palettetree.h:131
Repeat
Definition: measurebase.h:34
void setName(const QString &str)
Definition: palettetree.h:159
bool visible() const
Definition: palettetree.h:179
PaletteCellPtr cell(int idx)
Definition: palettetree.h:195
std::shared_ptr< PaletteCell > PaletteCellPtr
Definition: palettetree.h:29
void setType(Type t)
Definition: palettetree.h:211
PaletteCellIconEngine(PaletteCellConstPtr cell, qreal extraMag=1.0)
Definition: palettetree.h:83
Definition: text.h:24
PaletteCellConstPtr _cell
Definition: palettetree.h:77
QSize gridSize() const
Definition: palettetree.h:163
void setDrawGrid(bool val)
Definition: palettetree.h:171
static PaletteCellPtr readElementMimeData(const QByteArray &data)
Definition: palettetree.cpp:316
PaletteCell()=default
Definition: palettetree.h:222
void setElementTranslated(bool translate)
Definition: palettetree.cpp:190
QString translatedName() const
Definition: palettetree.cpp:161