diff src/folderselectdialog.cpp @ 84:90c297a2a3dd

Sort IP Addresses by their actual number in folder selection
author Andre Heinecke <andre.heinecke@intevation.de>
date Thu, 18 Jun 2015 19:22:21 +0200
parents ca8d3cfe8ba1
children a32406f8832f
line wrap: on
line diff
--- a/src/folderselectdialog.cpp	Thu Jun 18 18:55:00 2015 +0200
+++ b/src/folderselectdialog.cpp	Thu Jun 18 19:22:21 2015 +0200
@@ -26,6 +26,38 @@
 #include <QStandardItem>
 #include <QMessageBox>
 
+static ulong ipStringToLong(const QString str) {
+    QStringList octets = str.split(".");
+    if (!octets.size() > 3) {
+        qWarning() << "invalid call to str to long";
+        return 0;
+    }
+    ulong s1 = octets.at(0).toLong();
+    ulong s2 = octets.at(1).toLong();
+    ulong s3 = octets.at(2).toLong();
+    ulong s4 = octets.at(3).toLong();
+
+    return (s1 << 24) | (s2 << 16) | (s3 << 8) | s4;
+}
+
+class IPAwareSortModel : public QSortFilterProxyModel
+{
+public:
+    bool lessThan(const QModelIndex &left,
+                  const QModelIndex &right) const
+    {
+        QString leftString = sourceModel()->data(left).toString();
+        QString rightString = sourceModel()->data(right).toString();
+
+        static QRegExp ipPattern("\\b\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\b");
+        if (ipPattern.indexIn(leftString) != -1 && ipPattern.indexIn(rightString) != -1) {
+            return ipStringToLong(leftString) > ipStringToLong(rightString);
+        } else {
+            return QSortFilterProxyModel::lessThan(left, right);
+        }
+    }
+};
+
 FolderSelectDialog::FolderSelectDialog(const QString& startFolder,
                                        const QString& folderPattern,
                                        const QString& pathLabel,
@@ -38,7 +70,7 @@
     mPathLabel(NULL),
     mPathLineEdit(NULL)
 {
-    mSortModel = new QSortFilterProxyModel();
+    mSortModel = new IPAwareSortModel();
     mModel = new QStandardItemModel();
     mShowPathSelection = !pathLabel.isEmpty();
     setupGUI();
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)