Skip to content

Commit

Permalink
fixed crashing when element track is invalid (implemented handling of…
Browse files Browse the repository at this point in the history
… invalid indexes)
  • Loading branch information
RomanPudashkin authored and igorkorsukov committed Feb 17, 2021
1 parent c723ee6 commit 46f25fa
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 15 deletions.
49 changes: 47 additions & 2 deletions src/libmscore/element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,13 +290,18 @@ void Element::change(Element* o, Element* n)

Staff* Element::staff() const
{
if (_track == -1 || score()->staves().empty()) {
return 0;
if (!hasStaff() || score()->staves().empty()) {
return nullptr;
}

return score()->staff(staffIdx());
}

bool Element::hasStaff() const
{
return _track != INVALID_TRACK_INDEX;
}

//---------------------------------------------------------
// staffType
//---------------------------------------------------------
Expand All @@ -317,6 +322,16 @@ bool Element::onTabStaff() const
return stt ? stt->isTabStaff() : false;
}

int Element::track() const
{
return _track;
}

void Element::setTrack(int val)
{
_track = val;
}

//---------------------------------------------------------
// z
//---------------------------------------------------------
Expand All @@ -329,6 +344,36 @@ int Element::z() const
return _z;
}

void Element::setZ(int val)
{
_z = val;
}

int Element::staffIdx() const
{
return track2staff(_track);
}

void Element::setStaffIdx(int val)
{
_track = staff2track(val);
}

int Element::vStaffIdx() const
{
return staffIdx();
}

int Element::voice() const
{
return track2voice(_track);
}

void Element::setVoice(int v)
{
_track = (_track / VOICES) * VOICES + v;
}

//---------------------------------------------------------
// tick
//---------------------------------------------------------
Expand Down
17 changes: 9 additions & 8 deletions src/libmscore/element.h
Original file line number Diff line number Diff line change
Expand Up @@ -380,18 +380,19 @@ class Element : public ScoreElement
/** Returns grips positions in page coordinates. */
virtual std::vector<QPointF> gripsPositions(const EditData& = EditData()) const { return std::vector<QPointF>(); }

int track() const { return _track; }
virtual void setTrack(int val) { _track = val; }
int track() const;
virtual void setTrack(int val);

int z() const;
void setZ(int val) { _z = val; }
void setZ(int val);

int staffIdx() const { return _track / VOICES; }
void setStaffIdx(int val) { _track = val * VOICES + voice(); }
virtual int vStaffIdx() const { return staffIdx(); }
int voice() const { return _track % VOICES; }
void setVoice(int v) { _track = (_track / VOICES) * VOICES + v; }
int staffIdx() const;
void setStaffIdx(int val);
virtual int vStaffIdx() const;
int voice() const;
void setVoice(int v);
Staff* staff() const;
bool hasStaff() const;
const StaffType* staffType() const;
bool onTabStaff() const;
Part* part() const;
Expand Down
26 changes: 22 additions & 4 deletions src/libmscore/mscore.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,28 @@ enum class HairpinType : signed char;
#define VOICES 4
#endif

inline int staff2track(int staffIdx) { return staffIdx * VOICES; }
inline int track2staff(int track) { return track / VOICES; }
inline int track2voice(int track) { return track % VOICES; }
inline int trackZeroVoice(int track) { return (track / VOICES) * VOICES; }
static constexpr int INVALID_TRACK_INDEX = -1;
static constexpr int INVALID_VOICE_INDEX = -1;

inline int staff2track(int staffIdx)
{
return staffIdx >= 0 ? staffIdx * VOICES : INVALID_TRACK_INDEX;
}

inline int track2staff(int track)
{
return track >= 0 ? track / VOICES : INVALID_TRACK_INDEX;
}

inline int track2voice(int track)
{
return track >= 0 ? track % VOICES : INVALID_VOICE_INDEX;
}

inline int trackZeroVoice(int track)
{
return track >= 0 ? (track / VOICES) * VOICES : INVALID_VOICE_INDEX;
}

static const int MAX_TAGS = 32;

Expand Down
2 changes: 1 addition & 1 deletion src/notation/internal/notationaccessibility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ QString NotationAccessibility::singleElementAccessibilityInfo() const
accessibilityInfo += "; " + barsAndBeats;
}

if (element->staffIdx() + 1) {
if (element->hasStaff()) {
QString staff = qtrc("notation", "Staff %1").arg(QString::number(element->staffIdx() + 1));

QString staffName = element->staff()->part()->longName(element->tick());
Expand Down

0 comments on commit 46f25fa

Please sign in to comment.