From 98c56a96cf1538dcdce481c0a1c045b2660a59ee Mon Sep 17 00:00:00 2001 From: allejok96 Date: Sun, 3 Jul 2022 17:21:10 +0200 Subject: [PATCH] bool operator instead of optional + qobject_cast --- include/Note.h | 8 +++++++- src/core/Note.cpp | 6 ++---- src/gui/clips/MidiClipView.cpp | 8 ++++---- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/include/Note.h b/include/Note.h index 01338be49..fc4058c1b 100644 --- a/include/Note.h +++ b/include/Note.h @@ -253,16 +253,22 @@ private: typedef QVector NoteVector; +/*! \brief Bounding box of notes + * + * Defaults to all zero, and will in that case evaluate to false if converted to bool. + */ struct NoteBounds { TimePos start; TimePos end; int lowest; int highest; + + operator bool() { return start.getTicks() || end.getTicks() || lowest || highest; } }; -std::optional boundsForNotes(const NoteVector& notes); +NoteBounds boundsForNotes(const NoteVector& notes); } // namespace lmms diff --git a/src/core/Note.cpp b/src/core/Note.cpp index 635dae000..2a50efad1 100644 --- a/src/core/Note.cpp +++ b/src/core/Note.cpp @@ -240,12 +240,10 @@ bool Note::withinRange(int tickStart, int tickEnd) const /*! \brief Get the start/end/bottom/top positions of notes in a vector - * - * Returns no value if there are no notes */ -std::optional boundsForNotes(const NoteVector& notes) +NoteBounds boundsForNotes(const NoteVector& notes) { - if (notes.empty()) { return {}; } + if (notes.empty()) { return NoteBounds(); } TimePos start = notes.front()->pos(); TimePos end = start; diff --git a/src/gui/clips/MidiClipView.cpp b/src/gui/clips/MidiClipView.cpp index 07102e818..fa24d2830 100644 --- a/src/gui/clips/MidiClipView.cpp +++ b/src/gui/clips/MidiClipView.cpp @@ -155,12 +155,12 @@ void MidiClipView::transposeSelection() int lowest = NumKeys - 1; for (ClipView* clipview: selection) { - if (auto mcv = dynamic_cast(clipview)) + if (auto mcv = qobject_cast(clipview)) { if (auto bounds = boundsForNotes(mcv->getMidiClip()->notes())) { - lowest = std::min(bounds->lowest, lowest); - highest = std::max(bounds->highest, highest); + lowest = std::min(bounds.lowest, lowest); + highest = std::max(bounds.highest, highest); } } } @@ -176,7 +176,7 @@ void MidiClipView::transposeSelection() QSet m_changedTracks; for (ClipView* clipview: selection) { - auto mcv = dynamic_cast(clipview); + auto mcv = qobject_cast(clipview); if (!mcv) { continue; } auto clip = mcv->getMidiClip();