changeset 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 11aaa9214cfb
children 6fa321b3dfe7
files src/folderselectdialog.cpp src/folderselectdialog.h src/l10n/main_de_DE.ts
diffstat 3 files changed, 57 insertions(+), 20 deletions(-) [+]
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();
--- a/src/folderselectdialog.h	Thu Jun 18 18:55:00 2015 +0200
+++ b/src/folderselectdialog.h	Thu Jun 18 19:22:21 2015 +0200
@@ -14,7 +14,7 @@
 #include "filterwidget.h"
 
 class QTableView;
-class QSortFilterProxyModel;
+class IPAwareSortModel;
 class FilterWidget;
 class QStandardItemModel;
 class QLabel;
@@ -70,7 +70,7 @@
 private:
     QTableView *mView;
     FilterWidget *mFilterWidget;
-    QSortFilterProxyModel *mSortModel;
+    IPAwareSortModel *mSortModel;
     QString mCurFolder,
             mFolderPattern;
     QString mPathLabelString;
--- a/src/l10n/main_de_DE.ts	Thu Jun 18 18:55:00 2015 +0200
+++ b/src/l10n/main_de_DE.ts	Thu Jun 18 19:22:21 2015 +0200
@@ -17,42 +17,42 @@
 <context>
     <name>FolderSelectDialog</name>
     <message>
-        <location filename="../folderselectdialog.cpp" line="65"/>
+        <location filename="../folderselectdialog.cpp" line="99"/>
         <source>Select folder</source>
         <translation>Ordner auswählen</translation>
     </message>
     <message>
-        <location filename="../folderselectdialog.cpp" line="77"/>
+        <location filename="../folderselectdialog.cpp" line="111"/>
         <source>Go</source>
         <translation></translation>
     </message>
     <message>
-        <location filename="../folderselectdialog.cpp" line="107"/>
+        <location filename="../folderselectdialog.cpp" line="141"/>
         <source>Ok</source>
         <translation>Ok</translation>
     </message>
     <message>
-        <location filename="../folderselectdialog.cpp" line="135"/>
+        <location filename="../folderselectdialog.cpp" line="169"/>
         <source>Error!</source>
         <translation>Fehler!</translation>
     </message>
     <message>
-        <location filename="../folderselectdialog.cpp" line="135"/>
+        <location filename="../folderselectdialog.cpp" line="169"/>
         <source>Failed to access directory: &apos;%1&apos;</source>
         <translation>Auf das Verzeichnis &apos;%1&apos; konnte nicht zugegriffen werden.</translation>
     </message>
     <message>
-        <location filename="../folderselectdialog.cpp" line="179"/>
+        <location filename="../folderselectdialog.cpp" line="213"/>
         <source>The following folders did not match the pattern: %1</source>
         <translation>Die folgenden Order entsprechen nicht dem Schema: %1</translation>
     </message>
     <message>
-        <location filename="../folderselectdialog.cpp" line="181"/>
+        <location filename="../folderselectdialog.cpp" line="215"/>
         <source>Failed to parse some folders.</source>
         <translation>Einige Ordnernamen konnten nicht verarbeitet werden.</translation>
     </message>
     <message>
-        <location filename="../folderselectdialog.cpp" line="205"/>
+        <location filename="../folderselectdialog.cpp" line="239"/>
         <source>Select %1</source>
         <translation>Bitte das %1 auswählen</translation>
     </message>
@@ -70,42 +70,47 @@
         <translation>Filter/Details</translation>
     </message>
     <message>
-        <location filename="../mainwindow.cpp" line="76"/>
+        <location filename="../mainwindow.cpp" line="67"/>
+        <source>Back to exam selection.</source>
+        <translation type="unfinished"></translation>
+    </message>
+    <message>
+        <location filename="../mainwindow.cpp" line="82"/>
         <source>Error!</source>
         <translation>Fehler!</translation>
     </message>
     <message>
-        <location filename="../mainwindow.cpp" line="142"/>
+        <location filename="../mainwindow.cpp" line="148"/>
         <source>Failed to access directory: &apos;%1&apos;</source>
         <translation>Auf das Verzeichnis &apos;%1&apos; konnte nicht zugegriffen werden.</translation>
     </message>
     <message>
-        <location filename="../mainwindow.cpp" line="147"/>
+        <location filename="../mainwindow.cpp" line="153"/>
         <source>Failed to access meta data file: &apos;%1&apos;</source>
         <translation>Auf die Metadaten-Datei: &apos;%1&apos; konnte nicht zugegriffen werden.</translation>
     </message>
     <message>
-        <location filename="../mainwindow.cpp" line="155"/>
+        <location filename="../mainwindow.cpp" line="161"/>
         <source>Parsed: &apos;%1&apos;</source>
         <translation>&apos;%1&apos; eingelesen</translation>
     </message>
     <message>
-        <location filename="../mainwindow.cpp" line="172"/>
+        <location filename="../mainwindow.cpp" line="178"/>
         <source>Showing: &apos;%1&apos;</source>
         <translation>Zeige: &apos;%1&apos;</translation>
     </message>
     <message>
-        <location filename="../mainwindow.cpp" line="191"/>
+        <location filename="../mainwindow.cpp" line="197"/>
         <source>Persons</source>
         <translation>Personen</translation>
     </message>
     <message>
-        <location filename="../mainwindow.cpp" line="208"/>
+        <location filename="../mainwindow.cpp" line="214"/>
         <source>Exams</source>
         <translation>Prüfungen</translation>
     </message>
     <message>
-        <location filename="../mainwindow.cpp" line="207"/>
+        <location filename="../mainwindow.cpp" line="213"/>
         <source>Root-Path</source>
         <translation>Wurzelverzeichnis</translation>
     </message>
@@ -113,7 +118,7 @@
 <context>
     <name>MetaDataView</name>
     <message>
-        <location filename="../metadataview.cpp" line="91"/>
+        <location filename="../metadataview.cpp" line="90"/>
         <source>Failed to parse file: &apos;%1&apos;</source>
         <translation>Die Datei &apos;%1&apos; konnte nicht eingelesen werden.</translation>
     </message>
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)