forked from musescore/MuseScore
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bsymbol.cpp
205 lines (174 loc) · 5.81 KB
/
bsymbol.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//=============================================================================
// MuseScore
// Music Composition & Notation
//
// Copyright (C) 2002-2011 Werner Schweer
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2
// as published by the Free Software Foundation and appearing in
// the file LICENCE.GPL
//=============================================================================
#include "score.h"
#include "image.h"
#include "xml.h"
#include "staff.h"
namespace Ms {
//---------------------------------------------------------
// BSymbol
//---------------------------------------------------------
BSymbol::BSymbol(Score* s)
: Element(s)
{
_z = int(Element::Type::SYMBOL) * 100;
setFlags(ElementFlag::MOVABLE | ElementFlag::SELECTABLE);
_systemFlag = false;
}
BSymbol::BSymbol(const BSymbol& s)
: Element(s), ElementLayout(s)
{
_z = s._z;
_systemFlag = s._systemFlag;
foreach(Element* e, s._leafs) {
Element* ee = e->clone();
ee->setParent(this);
_leafs.append(ee);
}
}
//---------------------------------------------------------
// writeProperties
//---------------------------------------------------------
void BSymbol::writeProperties(Xml& xml) const
{
if (_systemFlag)
xml.tag("systemFlag", _systemFlag);
foreach(const Element* e, leafs())
e->write(xml);
Element::writeProperties(xml);
}
//---------------------------------------------------------
// readProperties
//---------------------------------------------------------
bool BSymbol::readProperties(XmlReader& e)
{
const QStringRef& tag = e.name();
if (Element::readProperties(e))
return true;
else if (tag == "systemFlag")
_systemFlag = e.readInt();
else if (tag == "Symbol" || tag == "FSymbol") {
Element* element = name2Element(tag, score());
element->read(e);
add(element);
}
else if ( tag == "Image") {
if (MScore::noImages)
e.skipCurrentElement();
else {
Element* element = name2Element(tag, score());
element->read(e);
add(element);
}
}
else
return false;
return true;
}
//---------------------------------------------------------
// add
//---------------------------------------------------------
void BSymbol::add(Element* e)
{
if (e->type() == Element::Type::SYMBOL || e->type() == Element::Type::IMAGE) {
e->setParent(this);
_leafs.append(e);
static_cast<BSymbol*>(e)->setZ(z() - 1); // draw on top of parent
}
else
qDebug("BSymbol::add: unsupported type %s", e->name());
}
//---------------------------------------------------------
// remove
//---------------------------------------------------------
void BSymbol::remove(Element* e)
{
if (e->type() == Element::Type::SYMBOL || e->type() == Element::Type::IMAGE) {
if (!_leafs.removeOne(e))
qDebug("BSymbol::remove: element <%s> not found", e->name());
}
else
qDebug("BSymbol::remove: unsupported type %s", e->name());
}
//---------------------------------------------------------
// scanElements
//---------------------------------------------------------
void BSymbol::scanElements(void* data, void (*func)(void*, Element*), bool all)
{
func(data, this);
foreach (Element* e, _leafs)
e->scanElements(data, func, all);
}
//---------------------------------------------------------
// acceptDrop
//---------------------------------------------------------
bool BSymbol::acceptDrop(const DropData& data) const
{
Element::Type type = data.element->type();
return type == Element::Type::SYMBOL || type == Element::Type::IMAGE;
}
//---------------------------------------------------------
// drop
//---------------------------------------------------------
Element* BSymbol::drop(const DropData& data)
{
Element* el = data.element;
if (el->type() == Element::Type::SYMBOL || el->type() == Element::Type::IMAGE) {
el->setParent(this);
QPointF p = data.pos - pagePos() - data.dragOffset;
el->setUserOff(p);
score()->undoAddElement(el);
return el;
}
else
delete el;
return 0;
}
//---------------------------------------------------------
// layout
//---------------------------------------------------------
void BSymbol::layout()
{
if (staff())
setMag(staff()->mag());
for (Element* e : _leafs)
e->layout();
adjustReadPos();
}
//---------------------------------------------------------
// drag
//---------------------------------------------------------
QRectF BSymbol::drag(EditData* data)
{
QRectF r(canvasBoundingRect());
foreach(const Element* e, _leafs)
r |= e->canvasBoundingRect();
qreal x = data->delta.x();
qreal y = data->delta.y();
qreal _spatium = spatium();
if (data->hRaster) {
qreal hRaster = _spatium / MScore::hRaster();
int n = lrint(x / hRaster);
x = hRaster * n;
}
if (data->vRaster) {
qreal vRaster = _spatium / MScore::vRaster();
int n = lrint(y / vRaster);
y = vRaster * n;
}
setUserOff(QPointF(x, y));
r |= canvasBoundingRect();
foreach(const Element* e, _leafs)
r |= e->canvasBoundingRect();
return r;
}
}