comparison 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
comparison
equal deleted inserted replaced
83:11aaa9214cfb 84:90c297a2a3dd
24 #include <QSettings> 24 #include <QSettings>
25 #include <QStringList> 25 #include <QStringList>
26 #include <QStandardItem> 26 #include <QStandardItem>
27 #include <QMessageBox> 27 #include <QMessageBox>
28 28
29 static ulong ipStringToLong(const QString str) {
30 QStringList octets = str.split(".");
31 if (!octets.size() > 3) {
32 qWarning() << "invalid call to str to long";
33 return 0;
34 }
35 ulong s1 = octets.at(0).toLong();
36 ulong s2 = octets.at(1).toLong();
37 ulong s3 = octets.at(2).toLong();
38 ulong s4 = octets.at(3).toLong();
39
40 return (s1 << 24) | (s2 << 16) | (s3 << 8) | s4;
41 }
42
43 class IPAwareSortModel : public QSortFilterProxyModel
44 {
45 public:
46 bool lessThan(const QModelIndex &left,
47 const QModelIndex &right) const
48 {
49 QString leftString = sourceModel()->data(left).toString();
50 QString rightString = sourceModel()->data(right).toString();
51
52 static QRegExp ipPattern("\\b\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\b");
53 if (ipPattern.indexIn(leftString) != -1 && ipPattern.indexIn(rightString) != -1) {
54 return ipStringToLong(leftString) > ipStringToLong(rightString);
55 } else {
56 return QSortFilterProxyModel::lessThan(left, right);
57 }
58 }
59 };
60
29 FolderSelectDialog::FolderSelectDialog(const QString& startFolder, 61 FolderSelectDialog::FolderSelectDialog(const QString& startFolder,
30 const QString& folderPattern, 62 const QString& folderPattern,
31 const QString& pathLabel, 63 const QString& pathLabel,
32 QWidget * parent, 64 QWidget * parent,
33 Qt::WindowFlags f) : 65 Qt::WindowFlags f) :
36 mFolderPattern(folderPattern), 68 mFolderPattern(folderPattern),
37 mPathLabelString(pathLabel), 69 mPathLabelString(pathLabel),
38 mPathLabel(NULL), 70 mPathLabel(NULL),
39 mPathLineEdit(NULL) 71 mPathLineEdit(NULL)
40 { 72 {
41 mSortModel = new QSortFilterProxyModel(); 73 mSortModel = new IPAwareSortModel();
42 mModel = new QStandardItemModel(); 74 mModel = new QStandardItemModel();
43 mShowPathSelection = !pathLabel.isEmpty(); 75 mShowPathSelection = !pathLabel.isEmpty();
44 setupGUI(); 76 setupGUI();
45 if (mShowPathSelection) { 77 if (mShowPathSelection) {
46 mPathLabel->setText("<b>" + pathLabel + ":</b> "); 78 mPathLabel->setText("<b>" + pathLabel + ":</b> ");
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)