]> Devoid-pointer.net GitWeb - anyanka.git/commitdiff
Simple implementation of opened sequences list alphabetical sorting.
authorMichal Malý <madcatxster@devoid-pointer.net>
Mon, 23 Mar 2015 23:18:33 +0000 (00:18 +0100)
committerMichal Malý <madcatxster@devoid-pointer.net>
Mon, 23 Mar 2015 23:18:33 +0000 (00:18 +0100)
sequenceselectormodel.cpp
sequenceselectormodel.h

index d90aae1881d33ce3a0dbf3c782d3cba668463ae3..0338abe0e57c6cb61cb28c3375579047d205d4a7 100644 (file)
@@ -20,7 +20,9 @@
   THE SOFTWARE.
 */
 
+#include "logger.h"
 #include "sequenceselectormodel.h"
+#include <algorithm>
 
 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<std::string> 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;}
+    );
+}
index 5a29473f3d4b310ecad65ec68841e4ef9265611a..1353e5f223ab49f2d0a40920db35c360b0baa969 100644 (file)
@@ -38,6 +38,7 @@ public:
   void setSequences(const std::vector<std::string> sequenceKeys);
 
 private:
+  void sortKeys();
   std::vector<std::string> m_sequenceKeys;
 };