1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. |
2 | // |
3 | // SPDX-License-Identifier: GPL-3.0-or-later |
4 | |
5 | #include "jsondispalysfmodel.h" |
6 | |
7 | #include <QRegularExpression> |
8 | |
9 | class JsonDispalySFModelPrivate |
10 | { |
11 | friend class JsonDispalySFModel; |
12 | QStringList regExpStrs |
13 | { |
14 | {"(?<float>[0-9]+.[0-9]+)%$" }, |
15 | {"(?<llong>[0-9]+)" }, |
16 | {"(?<path>[/\\S]+)" } |
17 | }; |
18 | |
19 | void getPathName(const QString &path, QString &perfix, QString &name) |
20 | { |
21 | QString temp = path; |
22 | int endIdx = temp.size() - 1; |
23 | while (endIdx >= 0) { |
24 | if (temp[endIdx] == "/" ) |
25 | break; |
26 | endIdx --; |
27 | } |
28 | if (endIdx >= 0) { |
29 | name = temp.mid(endIdx + 1, temp.size()); |
30 | perfix = temp.mid(0, endIdx); |
31 | } |
32 | } |
33 | }; |
34 | |
35 | JsonDispalySFModel::JsonDispalySFModel(QObject *parent) |
36 | : QSortFilterProxyModel (parent) |
37 | , d (new JsonDispalySFModelPrivate) |
38 | { |
39 | |
40 | } |
41 | |
42 | JsonDispalySFModel::~JsonDispalySFModel() |
43 | { |
44 | if (d) |
45 | delete d; |
46 | } |
47 | |
48 | bool JsonDispalySFModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const |
49 | { |
50 | QString leftStr = source_left.data(Qt::DisplayRole).toString(); |
51 | QString rightStr = source_right.data(Qt::DisplayRole).toString(); |
52 | for (auto regExpStr : d->regExpStrs) { |
53 | QRegularExpression regExp(regExpStr); |
54 | auto leftMatchs = regExp.match(leftStr); |
55 | auto rightMatchs = regExp.match(rightStr); |
56 | if (leftMatchs.isValid() && rightMatchs.isValid()) { |
57 | QString left_fMatch = leftMatchs.captured("float" ); |
58 | QString right_fMatch = rightMatchs.captured("float" ); |
59 | if (!left_fMatch.isEmpty() && !right_fMatch.isEmpty()) { |
60 | return left_fMatch.toFloat() > right_fMatch.toFloat(); |
61 | } |
62 | QString left_llMatch = leftMatchs.captured("llong" ); |
63 | QString right_llMatch = rightMatchs.captured("llong" ); |
64 | if (!left_llMatch.isEmpty() && !right_llMatch.isEmpty()) { |
65 | return left_llMatch.toLongLong() > right_llMatch.toLongLong(); |
66 | } |
67 | QString left_pMatch = leftMatchs.captured("path" ).toLower(); |
68 | QString right_pMatch = rightMatchs.captured("path" ).toLower(); |
69 | if (!left_pMatch.isEmpty() && !right_pMatch.isEmpty()) { |
70 | return left_pMatch > right_pMatch; |
71 | } |
72 | } |
73 | } |
74 | return QSortFilterProxyModel::lessThan(source_left, source_right); |
75 | } |
76 | |