From 7d5d56be5761592c07cc11a26f976ef250d04900 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Michal=20Mal=C3=BD?= Date: Tue, 24 Mar 2015 00:18:33 +0100 Subject: [PATCH] Simple implementation of opened sequences list alphabetical sorting. --- sequenceselectormodel.cpp | 33 +++++++++++++++++++++++++++++++-- sequenceselectormodel.h | 1 + 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/sequenceselectormodel.cpp b/sequenceselectormodel.cpp index d90aae1..0338abe 100644 --- a/sequenceselectormodel.cpp +++ b/sequenceselectormodel.cpp @@ -20,7 +20,9 @@ THE SOFTWARE. */ +#include "logger.h" #include "sequenceselectormodel.h" +#include SequenceSelectorModel::SequenceSelectorModel(QObject* parent) : QAbstractListModel(parent) @@ -29,8 +31,16 @@ SequenceSelectorModel::SequenceSelectorModel(QObject* parent) : void SequenceSelectorModel::addSequence(const std::string& key) { - beginInsertRows(QModelIndex(), m_sequenceKeys.size(), m_sequenceKeys.size()); - m_sequenceKeys.push_back(key); + size_t idx; + /* Very naive lookup of the index where to put the new key */ + for (idx = 1; idx < m_sequenceKeys.size(); idx++) { + if (key.compare(m_sequenceKeys[idx]) < 0) + break; + } + + Logger::log(Logger::Level::DEBUG, ME_SENDER_STR, QString("Inserting new key at %1, total items: %2").arg(idx).arg(m_sequenceKeys.size()+1)); + beginInsertRows(QModelIndex(), idx, idx); + m_sequenceKeys.insert(m_sequenceKeys.begin()+idx, key); endInsertRows(); } @@ -73,5 +83,24 @@ void SequenceSelectorModel::setSequences(const std::vector sequence { beginResetModel(); m_sequenceKeys = sequenceKeys; + sortKeys(); endResetModel(); } + +/** Private methods */ +void SequenceSelectorModel::sortKeys() +{ + std::sort(m_sequenceKeys.begin(), m_sequenceKeys.end(), + [](const std::string& s1, const std::string s2) { + static const std::string ALWAYS_FIRST = "Single runs"; + + if (s1.compare(ALWAYS_FIRST) == 0) + return true; + if (s2.compare(ALWAYS_FIRST) == 0) + return false; + + if (s1.compare(s2) <= 0) + return true; + return false;} + ); +} diff --git a/sequenceselectormodel.h b/sequenceselectormodel.h index 5a29473..1353e5f 100644 --- a/sequenceselectormodel.h +++ b/sequenceselectormodel.h @@ -38,6 +38,7 @@ public: void setSequences(const std::vector sequenceKeys); private: + void sortKeys(); std::vector m_sequenceKeys; }; -- 2.43.5