From 8a3e604dda9690f04f9e67759b9a9635c3fb8ac5 Mon Sep 17 00:00:00 2001 From: szeli1 <143485814+szeli1@users.noreply.github.com> Date: Tue, 22 Oct 2024 21:06:00 +0200 Subject: [PATCH 1/8] Track_adding_renaming_methods --- include/Track.h | 5 +++++ src/core/Track.cpp | 52 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/include/Track.h b/include/Track.h index 52d43f6e115..d78d28af948 100644 --- a/include/Track.h +++ b/include/Track.h @@ -197,6 +197,8 @@ class LMMS_EXPORT Track : public Model, public JournallingObject BoolModel* getMutedModel(); + //! returns a name that isn't used by any track and conatins `sourceName` + QString findUniqueName(const QString& sourceName) const; public slots: virtual void setName(const QString& newName); @@ -211,6 +213,9 @@ public slots: void saveTrack(QDomDocument& doc, QDomElement& element, bool presetMode); void loadTrack(const QDomElement& element, bool presetMode); + //! returns the number characters at the end of a string + static QString getNameNumberEnding(const QString& name); + private: TrackContainer* m_trackContainer; Type m_type; diff --git a/src/core/Track.cpp b/src/core/Track.cpp index e44475d9374..aceb78af2cf 100644 --- a/src/core/Track.cpp +++ b/src/core/Track.cpp @@ -167,6 +167,8 @@ Track* Track::clone() QDomElement parent = doc.createElement("clonedtrack"); saveState(doc, parent); Track* t = create(parent.firstChild().toElement(), m_trackContainer); + // giving different name to cloned track + t->setName(findUniqueName(name())); AutomationClip::resolveAllIDs(); return t; @@ -652,6 +654,56 @@ BoolModel *Track::getMutedModel() return &m_mutedModel; } +QString Track::findUniqueName(const QString& sourceName) const +{ + QString output = sourceName; + // removing number from `sourceName` + size_t sourceNumberLength = Track::getNameNumberEnding(sourceName).size(); + if (sourceNumberLength > 0) + { + // whitespace needs to be removed so we add + 1 to `sourceNumberLength` + sourceNumberLength++; + output.remove(output.size() - sourceNumberLength, sourceNumberLength); + } + + const TrackContainer::TrackList& trackList = m_trackContainer->tracks(); + + size_t maxNameCounter = 0; + bool found = false; + + for (const Track* it : trackList) + { + if (it->name().startsWith(sourceName)) + { + size_t nameCount = Track::getNameNumberEnding(it->name()).toInt(); + maxNameCounter = maxNameCounter < nameCount ? nameCount : maxNameCounter; + found = true; + } + } + + if (found) + { + output = output + " " + QString::number(maxNameCounter + 1); + } + + return output; +} + +QString Track::getNameNumberEnding(const QString& name) +{ + QString numberString = ""; + + // this should be safe with unsigned types + for (int i = name.size(); i-- > 0;) + { + if (name[i].isDigit() == true) + { + numberString = name[i] + numberString; + } + } + return numberString; +} + void Track::setName(const QString& newName) { if (m_name != newName) From 27a1b5dd61aa082b1aea8476ca04793886e5f30a Mon Sep 17 00:00:00 2001 From: szeli1 <143485814+szeli1@users.noreply.github.com> Date: Tue, 22 Oct 2024 21:06:17 +0200 Subject: [PATCH 2/8] AutomationTrack_implementing_renaming_method --- src/tracks/AutomationTrack.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tracks/AutomationTrack.cpp b/src/tracks/AutomationTrack.cpp index 1ff30ac7680..8d9eb9d24ee 100644 --- a/src/tracks/AutomationTrack.cpp +++ b/src/tracks/AutomationTrack.cpp @@ -36,7 +36,7 @@ namespace lmms AutomationTrack::AutomationTrack( TrackContainer* tc, bool _hidden ) : Track( _hidden ? Type::HiddenAutomation : Type::Automation, tc ) { - setName( tr( "Automation track" ) ); + setName(findUniqueName(tr("Automation track"))); } bool AutomationTrack::play( const TimePos & time_start, const fpp_t _frames, From 0c030ac8bee0e7e762fd58d0f059af38c5afad72 Mon Sep 17 00:00:00 2001 From: szeli1 <143485814+szeli1@users.noreply.github.com> Date: Tue, 22 Oct 2024 21:06:38 +0200 Subject: [PATCH 3/8] InstrumentTrack_implementing_renaming_method --- src/tracks/InstrumentTrack.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/tracks/InstrumentTrack.cpp b/src/tracks/InstrumentTrack.cpp index 66992bc4f6d..5e56cef0b5e 100644 --- a/src/tracks/InstrumentTrack.cpp +++ b/src/tracks/InstrumentTrack.cpp @@ -104,7 +104,7 @@ InstrumentTrack::InstrumentTrack( TrackContainer* tc ) : this, [this, i]{ processCCEvent(i); }, Qt::DirectConnection); } - setName( tr( "Default preset" ) ); + setName(findUniqueName(tr("Default preset"))); connect(&m_baseNoteModel, SIGNAL(dataChanged()), this, SLOT(updateBaseNote()), Qt::DirectConnection); connect(&m_pitchModel, SIGNAL(dataChanged()), this, SLOT(updatePitch()), Qt::DirectConnection); @@ -1056,7 +1056,7 @@ Instrument * InstrumentTrack::loadInstrument(const QString & _plugin_name, m_instrument = Instrument::instantiate(_plugin_name, this, key, keyFromDnd); unlock(); - setName(m_instrument->displayName()); + setName(findUniqueName(m_instrument->displayName())); emit instrumentChanged(); From bf72dd9028d94650e40f160ab94b3733278de245 Mon Sep 17 00:00:00 2001 From: szeli1 <143485814+szeli1@users.noreply.github.com> Date: Tue, 22 Oct 2024 21:06:59 +0200 Subject: [PATCH 4/8] PatternTrack_implementing_renaming_method --- src/tracks/PatternTrack.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/tracks/PatternTrack.cpp b/src/tracks/PatternTrack.cpp index 697a7c2a8fb..ede04948aa5 100644 --- a/src/tracks/PatternTrack.cpp +++ b/src/tracks/PatternTrack.cpp @@ -46,7 +46,7 @@ PatternTrack::PatternTrack(TrackContainer* tc) : int patternNum = s_infoMap.size(); s_infoMap[this] = patternNum; - setName(tr("Pattern %1").arg(patternNum)); + setName(findUniqueName(tr("Pattern"))); Engine::patternStore()->createClipsForPattern(patternNum); Engine::patternStore()->setCurrentPattern(patternNum); Engine::patternStore()->updateComboBox(); @@ -194,8 +194,6 @@ void PatternTrack::loadTrackSpecificSettings(const QDomElement& _this) { Clip::copyStateTo(track->getClip(src), track->getClip(dst)); } - setName( tr( "Clone of %1" ).arg( - _this.parentNode().toElement().attribute( "name" ) ) ); } else { From f13a169bed668df6353aa8e3a9673fed121c860d Mon Sep 17 00:00:00 2001 From: szeli1 <143485814+szeli1@users.noreply.github.com> Date: Tue, 22 Oct 2024 21:07:21 +0200 Subject: [PATCH 5/8] SampleTrack_implementing_renaming_method --- src/tracks/SampleTrack.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tracks/SampleTrack.cpp b/src/tracks/SampleTrack.cpp index 8ad75799dd0..c62b8bb9715 100644 --- a/src/tracks/SampleTrack.cpp +++ b/src/tracks/SampleTrack.cpp @@ -52,7 +52,7 @@ SampleTrack::SampleTrack(TrackContainer* tc) : m_audioPort(tr("Sample track"), true, &m_volumeModel, &m_panningModel, &m_mutedModel), m_isPlaying(false) { - setName(tr("Sample track")); + setName(findUniqueName(tr("Sample track"))); m_panningModel.setCenterValue(DefaultPanning); m_mixerChannelModel.setRange(0, Engine::mixer()->numChannels()-1, 1); From d519e8d03820c4ac60f134591dc42877f0ad8d74 Mon Sep 17 00:00:00 2001 From: szeli1 <143485814+szeli1@users.noreply.github.com> Date: Wed, 23 Oct 2024 08:13:23 +0200 Subject: [PATCH 6/8] Track_changing_for_to_use_unsigned_int --- src/core/Track.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/Track.cpp b/src/core/Track.cpp index aceb78af2cf..3eab0223684 100644 --- a/src/core/Track.cpp +++ b/src/core/Track.cpp @@ -694,7 +694,7 @@ QString Track::getNameNumberEnding(const QString& name) QString numberString = ""; // this should be safe with unsigned types - for (int i = name.size(); i-- > 0;) + for (unsigned int i = name.size(); i-- > 0;) { if (name[i].isDigit() == true) { From d55389a91a370fc541227f66eac5d680625e9c31 Mon Sep 17 00:00:00 2001 From: szeli1 <143485814+szeli1@users.noreply.github.com> Date: Wed, 23 Oct 2024 08:54:27 +0200 Subject: [PATCH 7/8] Track_optimizing_getNameNumberEnding_method --- src/core/Track.cpp | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/core/Track.cpp b/src/core/Track.cpp index 3eab0223684..c6aa1328129 100644 --- a/src/core/Track.cpp +++ b/src/core/Track.cpp @@ -661,6 +661,7 @@ QString Track::findUniqueName(const QString& sourceName) const size_t sourceNumberLength = Track::getNameNumberEnding(sourceName).size(); if (sourceNumberLength > 0) { + qDebug("source name length"); // whitespace needs to be removed so we add + 1 to `sourceNumberLength` sourceNumberLength++; output.remove(output.size() - sourceNumberLength, sourceNumberLength); @@ -683,6 +684,7 @@ QString Track::findUniqueName(const QString& sourceName) const if (found) { + qDebug("found"); output = output + " " + QString::number(maxNameCounter + 1); } @@ -693,14 +695,28 @@ QString Track::getNameNumberEnding(const QString& name) { QString numberString = ""; - // this should be safe with unsigned types - for (unsigned int i = name.size(); i-- > 0;) + //! `it` will point to where the numbers start in `name` + auto it = name.end(); + size_t digitCount = 0; + while (it != name.begin()) { - if (name[i].isDigit() == true) + it--; + if (it->isDigit() == false) { - numberString = name[i] + numberString; + // the last character was not a number + // increase `it` to account for this (and make it point to a digit) + it++; + break; } + digitCount++; } + + if (digitCount > 0) + { + numberString.resize(digitCount); + std::copy(it, name.end(), numberString.begin()); + } + return numberString; } From 8ecd4df84b5cf2065c3c046aff46a0ae30e81beb Mon Sep 17 00:00:00 2001 From: szeli1 <143485814+szeli1@users.noreply.github.com> Date: Wed, 23 Oct 2024 09:09:51 +0200 Subject: [PATCH 8/8] Track_name_fixing_whitespace_issues --- include/Track.h | 2 +- src/core/Track.cpp | 15 +++++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/include/Track.h b/include/Track.h index d78d28af948..f4268da922d 100644 --- a/include/Track.h +++ b/include/Track.h @@ -214,7 +214,7 @@ public slots: void loadTrack(const QDomElement& element, bool presetMode); //! returns the number characters at the end of a string - static QString getNameNumberEnding(const QString& name); + static QString getNameNumberEnding(const QString& name, bool* isSeparatedWithWhiteSpace = nullptr); private: TrackContainer* m_trackContainer; diff --git a/src/core/Track.cpp b/src/core/Track.cpp index c6aa1328129..cb8449fe633 100644 --- a/src/core/Track.cpp +++ b/src/core/Track.cpp @@ -658,12 +658,12 @@ QString Track::findUniqueName(const QString& sourceName) const { QString output = sourceName; // removing number from `sourceName` - size_t sourceNumberLength = Track::getNameNumberEnding(sourceName).size(); + bool isSeparatedWithWhiteSpace = false; + size_t sourceNumberLength = Track::getNameNumberEnding(sourceName, &isSeparatedWithWhiteSpace).size(); if (sourceNumberLength > 0) { - qDebug("source name length"); // whitespace needs to be removed so we add + 1 to `sourceNumberLength` - sourceNumberLength++; + sourceNumberLength = isSeparatedWithWhiteSpace ? sourceNumberLength + 1 : sourceNumberLength; output.remove(output.size() - sourceNumberLength, sourceNumberLength); } @@ -674,7 +674,7 @@ QString Track::findUniqueName(const QString& sourceName) const for (const Track* it : trackList) { - if (it->name().startsWith(sourceName)) + if (it->name().startsWith(output)) { size_t nameCount = Track::getNameNumberEnding(it->name()).toInt(); maxNameCounter = maxNameCounter < nameCount ? nameCount : maxNameCounter; @@ -684,14 +684,13 @@ QString Track::findUniqueName(const QString& sourceName) const if (found) { - qDebug("found"); output = output + " " + QString::number(maxNameCounter + 1); } return output; } -QString Track::getNameNumberEnding(const QString& name) +QString Track::getNameNumberEnding(const QString& name, bool* isSeparatedWithWhiteSpace) { QString numberString = ""; @@ -703,6 +702,10 @@ QString Track::getNameNumberEnding(const QString& name) it--; if (it->isDigit() == false) { + if (isSeparatedWithWhiteSpace != nullptr) + { + *isSeparatedWithWhiteSpace = *it == ' '; + } // the last character was not a number // increase `it` to account for this (and make it point to a digit) it++;