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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
|
//=============================================================================
// MuseScore
// Music Composition & Notation
//
// Copyright (C) 2018 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 "connector.h"
#include "element.h"
#include "score.h"
#include "scoreElement.h"
#include "xml.h"
namespace Ms {
//---------------------------------------------------------
// ConnectorInfo
//---------------------------------------------------------
ConnectorInfo::ConnectorInfo(const Element* current, int track, Fraction frac)
: _current(current), _score(current->score()), _currentLoc(Location::absolute())
{
if (!current)
qFatal("ConnectorInfo::ConnectorInfo(): invalid argument: %p", current);
// It is not always possible to determine the track number correctly from
// the current element (for example, in case of a Segment).
// If the caller does not know the track number and passes -1
// it may be corrected later.
if (track >= 0)
_currentLoc.setTrack(track);
if (frac >= Fraction(0,1))
_currentLoc.setFrac(frac);
}
//---------------------------------------------------------
// ConnectorInfo
//---------------------------------------------------------
ConnectorInfo::ConnectorInfo(const Score* score, const Location& currentLocation)
: _score(score), _currentLoc(currentLocation)
{}
//---------------------------------------------------------
// ConnectorInfo::updateLocation
//---------------------------------------------------------
void ConnectorInfo::updateLocation(const Element* e, Location& l, bool clipboardmode)
{
l.fillForElement(e, clipboardmode);
}
//---------------------------------------------------------
// ConnectorInfo::updateCurrentInfo
//---------------------------------------------------------
void ConnectorInfo::updateCurrentInfo(bool clipboardmode)
{
if (!currentUpdated() && _current)
updateLocation(_current, _currentLoc, clipboardmode);
setCurrentUpdated(true);
}
//---------------------------------------------------------
// ConnectorInfo::connect
//---------------------------------------------------------
bool ConnectorInfo::connect(ConnectorInfo* other)
{
if (!other || (this == other))
return false;
if (_type != other->_type || _score != other->_score)
return false;
if (hasPrevious() && _prev == nullptr
&& other->hasNext() && other->_next == nullptr
) {
if ((_prevLoc == other->_currentLoc)
&& (_currentLoc == other->_nextLoc)
) {
_prev = other;
other->_next = this;
return true;
}
}
if (hasNext() && _next == nullptr
&& other->hasPrevious() && other->_prev == nullptr
) {
if ((_nextLoc == other->_currentLoc)
&& (_currentLoc == other->_prevLoc)
) {
_next = other;
other->_prev = this;
return true;
}
}
return false;
}
//---------------------------------------------------------
// ConnectorInfo::forceConnect
//---------------------------------------------------------
void ConnectorInfo::forceConnect(ConnectorInfo* other)
{
if (!other || (this == other))
return;
_next = other;
other->_prev = this;
}
//---------------------------------------------------------
// distance
//---------------------------------------------------------
static int distance(const Location& l1, const Location& l2)
{
constexpr int commonDenominator = 1000;
Fraction dfrac = (l2.frac() - l1.frac()).absValue();
int dpos = dfrac.numerator() * commonDenominator / dfrac.denominator();
dpos += 10000 * qAbs(l2.measure() - l1.measure());
return 1000 * dpos + 100 * qAbs(l2.track() - l1.track()) + 10 * qAbs(l2.note() - l1.note()) + qAbs(l2.graceIndex() - l1.graceIndex());
}
//---------------------------------------------------------
// ConnectorInfo::orderedConnectionDistance
//---------------------------------------------------------
int ConnectorInfo::orderedConnectionDistance(const ConnectorInfo& c1, const ConnectorInfo& c2)
{
Location c1Next = c1._nextLoc;
c1Next.toRelative(c1._currentLoc);
Location c2Prev = c2._currentLoc; // inversed order to get equal signs
c2Prev.toRelative(c2._prevLoc);
if (c1Next == c2Prev)
return distance(c1._nextLoc, c2._currentLoc);
return INT_MAX;
}
//---------------------------------------------------------
// ConnectorInfo::connectionDistance
// Returns a "distance" representing a likelihood of
// that the checked connectors should be connected.
// Returns 0 if can be readily connected via connect(),
// < 0 if other is likely to be the first,
// INT_MAX if cannot be connected
//---------------------------------------------------------
int ConnectorInfo::connectionDistance(const ConnectorInfo& other) const
{
if (_type != other._type || _score != other._score)
return INT_MAX;
int distThisOther = INT_MAX;
int distOtherThis = INT_MAX;
if (hasNext() && _next == nullptr
&& other.hasPrevious() && other._prev == nullptr)
distThisOther = orderedConnectionDistance(*this, other);
if (hasPrevious() && _prev == nullptr
&& other.hasNext() && other._next == nullptr)
distOtherThis = orderedConnectionDistance(other, *this);
if (distOtherThis < distThisOther)
return -distOtherThis;
return distThisOther;
}
//---------------------------------------------------------
// ConnectorInfo::findFirst
//---------------------------------------------------------
ConnectorInfo* ConnectorInfo::findFirst()
{
ConnectorInfo* i = this;
while (i->_prev) {
i = i->_prev;
if (i == this) {
qWarning("ConnectorInfo::findFirst: circular connector %p", this);
return nullptr;
}
}
return i;
}
//---------------------------------------------------------
// ConnectorInfo::findFirst
//---------------------------------------------------------
const ConnectorInfo* ConnectorInfo::findFirst() const
{
return const_cast<ConnectorInfo*>(this)->findFirst();
}
//---------------------------------------------------------
// ConnectorInfo::findLast
//---------------------------------------------------------
ConnectorInfo* ConnectorInfo::findLast()
{
ConnectorInfo* i = this;
while (i->_next) {
i = i->_next;
if (i == this) {
qWarning("ConnectorInfo::findLast: circular connector %p", this);
return nullptr;
}
}
return i;
}
//---------------------------------------------------------
// ConnectorInfo::findLast
//---------------------------------------------------------
const ConnectorInfo* ConnectorInfo::findLast() const
{
return const_cast<ConnectorInfo*>(this)->findLast();
}
//---------------------------------------------------------
// ConnectorInfo::finished
//---------------------------------------------------------
bool ConnectorInfo::finished() const
{
return (finishedLeft() && finishedRight());
}
//---------------------------------------------------------
// ConnectorInfo::finishedLeft
//---------------------------------------------------------
bool ConnectorInfo::finishedLeft() const
{
const ConnectorInfo* i = findFirst();
return (i && !i->hasPrevious());
}
//---------------------------------------------------------
// ConnectorInfo::finishedRight
//---------------------------------------------------------
bool ConnectorInfo::finishedRight() const
{
const ConnectorInfo* i = findLast();
return (i && !i->hasNext());
}
//---------------------------------------------------------
// ConnectorInfo::start
//---------------------------------------------------------
ConnectorInfo* ConnectorInfo::start()
{
ConnectorInfo* i = findFirst();
if (i && i->hasPrevious())
return nullptr;
return i;
}
//---------------------------------------------------------
// ConnectorInfo::end
//---------------------------------------------------------
ConnectorInfo* ConnectorInfo::end()
{
ConnectorInfo* i = findLast();
if (i && i->hasNext())
return nullptr;
return i;
}
//---------------------------------------------------------
// ConnectorInfoReader
//---------------------------------------------------------
ConnectorInfoReader::ConnectorInfoReader(XmlReader& e, Element* current, int track)
: ConnectorInfo(current, track), _reader(&e), _connector(nullptr), _currentElement(current), _connectorReceiver(current)
{}
//---------------------------------------------------------
// readPositionInfo
//---------------------------------------------------------
static Location readPositionInfo(const XmlReader& e, int track) {
Location info = e.location();
info.setTrack(track);
return info;
}
//---------------------------------------------------------
// ConnectorInfoReader
//---------------------------------------------------------
ConnectorInfoReader::ConnectorInfoReader(XmlReader& e, Score* current, int track)
: ConnectorInfo(current, readPositionInfo(e, track)), _reader(&e), _connector(nullptr), _currentElement(nullptr), _connectorReceiver(current)
{
setCurrentUpdated(true);
}
//---------------------------------------------------------
// ConnectorInfoWriter
//---------------------------------------------------------
ConnectorInfoWriter::ConnectorInfoWriter(XmlWriter& xml, const Element* current, const Element* connector, int track, Fraction frac)
: ConnectorInfo(current, track, frac), _xml(&xml), _connector(connector)
{
if (!connector) {
qFatal("ConnectorInfoWriter::ConnectorInfoWriter(): invalid arguments: %p, %p", connector, current);
return;
}
_type = connector->type();
updateCurrentInfo(xml.clipboardmode());
}
//---------------------------------------------------------
// ConnectorInfoWriter::write
//---------------------------------------------------------
void ConnectorInfoWriter::write()
{
XmlWriter& xml = *_xml;
if (!xml.canWrite(_connector))
return;
xml.stag(QString("%1 type=\"%2\"").arg(tagName()).arg(_connector->name()));
if (isStart())
_connector->write(xml);
if (hasPrevious()) {
xml.stag("prev");
_prevLoc.toRelative(_currentLoc);
_prevLoc.write(xml);
xml.etag();
}
if (hasNext()) {
xml.stag("next");
_nextLoc.toRelative(_currentLoc);
_nextLoc.write(xml);
xml.etag();
}
xml.etag();
}
//---------------------------------------------------------
// ConnectorInfoReader::read
//---------------------------------------------------------
bool ConnectorInfoReader::read()
{
XmlReader& e = *_reader;
const QString name(e.attribute("type"));
_type = ScoreElement::name2type(&name);
e.fillLocation(_currentLoc);
while (e.readNextStartElement()) {
const QStringRef& tag(e.name());
if (tag == "prev")
readEndpointLocation(_prevLoc);
else if (tag == "next")
readEndpointLocation(_nextLoc);
else {
if (tag == name)
_connector = Element::name2Element(tag, _connectorReceiver->score());
else
qWarning("ConnectorInfoReader::read: element tag (%s) does not match connector type (%s). Is the file corrupted?", tag.toLatin1().constData(), name.toLatin1().constData());
if (!_connector) {
e.unknown();
return false;
}
_connector->setTrack(_currentLoc.track());
_connector->read(e);
}
}
return true;
}
//---------------------------------------------------------
// ConnectorInfoReader::readEndpointLocation
//---------------------------------------------------------
void ConnectorInfoReader::readEndpointLocation(Location& l)
{
XmlReader& e = *_reader;
while (e.readNextStartElement()) {
const QStringRef& tag(e.name());
if (tag == "location") {
l = Location::relative();
l.read(e);
}
else
e.unknown();
}
}
//---------------------------------------------------------
// ConnectorInfoReader::update
//---------------------------------------------------------
void ConnectorInfoReader::update()
{
if (!currentUpdated())
updateCurrentInfo(_reader->pasteMode());
if (hasPrevious())
_prevLoc.toAbsolute(_currentLoc);
if (hasNext())
_nextLoc.toAbsolute(_currentLoc);
}
//---------------------------------------------------------
// ConnectorInfoReader::addToScore
//---------------------------------------------------------
void ConnectorInfoReader::addToScore(bool pasteMode)
{
ConnectorInfoReader* r = this;
while (r->prev())
r = r->prev();
while (r) {
r->_connectorReceiver->readAddConnector(r, pasteMode);
r = r->next();
}
}
//---------------------------------------------------------
// ConnectorInfoReader::readConnector
//---------------------------------------------------------
void ConnectorInfoReader::readConnector(std::unique_ptr<ConnectorInfoReader> info, XmlReader& e)
{
if (!info->read()) {
e.skipCurrentElement();
return;
}
e.addConnectorInfoLater(std::move(info));
}
//---------------------------------------------------------
// ConnectorInfoReader::connector
//---------------------------------------------------------
Element* ConnectorInfoReader::connector()
{
// connector should be contained in the first node normally.
ConnectorInfo* i = findFirst();
if (i)
return static_cast<ConnectorInfoReader*>(i)->_connector;
return nullptr;
}
//---------------------------------------------------------
// ConnectorInfoReader::connector
//---------------------------------------------------------
const Element* ConnectorInfoReader::connector() const
{
return const_cast<ConnectorInfoReader*>(this)->connector();
}
//---------------------------------------------------------
// ConnectorInfoReader::releaseConnector
//---------------------------------------------------------
Element* ConnectorInfoReader::releaseConnector()
{
ConnectorInfoReader* i = static_cast<ConnectorInfoReader*>(findFirst());
if (!i) {
// circular connector?
ConnectorInfoReader* ii = this;
Element* c = nullptr;
while (ii->prev()) {
if (ii->_connector) {
c = ii->_connector;
ii->_connector = nullptr;
}
ii = ii->prev();
if (ii == this)
break;
}
return c;
}
Element* c = i->_connector;
i->_connector = nullptr;
return c;
}
}
|