Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- Implemented natural sort for Variables tree.

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@22879 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
adeas31 committed Oct 22, 2014
1 parent e0f1456 commit f2ec8ee
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
7 changes: 7 additions & 0 deletions OMEdit/OMEditGUI/Plotting/VariablesWidget.cpp
Expand Up @@ -687,6 +687,13 @@ bool VariableTreeProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &
return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
}

bool VariableTreeProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
{
QVariant l = (left.model() ? left.model()->data(left) : QVariant());
QVariant r = (right.model() ? right.model()->data(right) : QVariant());
return StringHandler::naturalSort(l.toString(), r.toString());
}

VariablesTreeView::VariablesTreeView(VariablesWidget *pVariablesWidget)
: QTreeView(pVariablesWidget)
{
Expand Down
3 changes: 2 additions & 1 deletion OMEdit/OMEditGUI/Plotting/VariablesWidget.h
Expand Up @@ -137,7 +137,8 @@ class VariableTreeProxyModel : public QSortFilterProxyModel
public:
VariableTreeProxyModel(QObject *parent = 0);
protected:
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const;
virtual bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const;
virtual bool lessThan(const QModelIndex &left, const QModelIndex &right) const;
};

class VariablesWidget;
Expand Down
46 changes: 46 additions & 0 deletions OMEdit/OMEditGUI/Util/StringHandler.cpp
Expand Up @@ -1284,3 +1284,49 @@ bool StringHandler::isModelicaFile(QString extension)
else
return false;
}

bool StringHandler::naturalSort(const QString &s1, const QString &s2) {
int i1 = 0; // index in string
int i2 = 0;
while (true) {
if (s2.length() == i2) // both strings identical or s1 larger than s2
return s1.length() == i1 ? true : false;
if (s1.length() == i1) return true; // s1 smaller than s2

unsigned short u1 = s1[i1].unicode();
unsigned short u2 = s2[i2].unicode();

if (u1 >= '0' && u1 <= '9' && u2 >= '0' && u2 <= '9') {
// parse both numbers completely and compare them
quint64 n1 = 0; // the parsed number
quint64 n2 = 0;
int l1 = 0; // length of the number
int l2 = 0;
do {
++l1;
n1 = n1 * 10 + u1 - '0';
if (++i1 == s1.length()) break;
u1 = s1[i1].unicode();
} while (u1 >= '0' && u1 <= '9');
do {
++l2;
n2 = n2 * 10 + u2 - '0';
if (++i2 == s2.length()) break;
u2 = s2[i2].unicode();
} while (u2 >= '0' && u2 <= '9');
// compare two numbers
if (n1 < n2) return true;
if (n1 > n2) return false;
// only accept identical numbers if they also have the same length
// (same number of leading zeros)
if (l1 < l2) return true;
if (l1 > l2) return false;
} else {
// compare digit with non-digit or two non-digits
if (u1 < u2) return true;
if (u1 > u2) return false;
++i1;
++i2;
}
}
}
1 change: 1 addition & 0 deletions OMEdit/OMEditGUI/Util/StringHandler.h
Expand Up @@ -131,6 +131,7 @@ class StringHandler : public QObject
static QStringList makeVariableParts(QString variable);
static bool isCFile(QString extension);
static bool isModelicaFile(QString extension);
static bool naturalSort(const QString &s1, const QString &s2);
protected:
static QString mLastOpenDir;
};
Expand Down

0 comments on commit f2ec8ee

Please sign in to comment.