1 | // Copyright (C) 2021 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #include "qquickfilenamefilter_p.h" |
5 | |
6 | #include <QtCore/qloggingcategory.h> |
7 | |
8 | QT_BEGIN_NAMESPACE |
9 | |
10 | Q_LOGGING_CATEGORY(lcFileNameFilter, "qt.quick.dialogs.qquickfilenamefilter" ) |
11 | |
12 | QQuickFileNameFilter::QQuickFileNameFilter(QObject *parent) |
13 | : QObject(parent), m_index(-1) |
14 | { |
15 | } |
16 | |
17 | int QQuickFileNameFilter::index() const |
18 | { |
19 | return m_index; |
20 | } |
21 | |
22 | void QQuickFileNameFilter::setIndex(int index) |
23 | { |
24 | if (m_index == index) |
25 | return; |
26 | |
27 | m_index = index; |
28 | emit indexChanged(index); |
29 | } |
30 | |
31 | QString QQuickFileNameFilter::name() const |
32 | { |
33 | return m_name; |
34 | } |
35 | |
36 | QStringList QQuickFileNameFilter::extensions() const |
37 | { |
38 | return m_extensions; |
39 | } |
40 | |
41 | QStringList QQuickFileNameFilter::globs() const |
42 | { |
43 | return m_globs; |
44 | } |
45 | |
46 | QSharedPointer<QFileDialogOptions> QQuickFileNameFilter::options() const |
47 | { |
48 | return m_options; |
49 | } |
50 | |
51 | void QQuickFileNameFilter::setOptions(const QSharedPointer<QFileDialogOptions> &options) |
52 | { |
53 | m_options = options; |
54 | } |
55 | |
56 | static QString (const QString &filter) |
57 | { |
58 | return filter.left(n: filter.indexOf(c: QLatin1Char('(')) - 1); |
59 | } |
60 | |
61 | static QString (QStringView filter) |
62 | { |
63 | return filter.mid(pos: filter.indexOf(c: QLatin1Char('.')) + 1).toString(); |
64 | } |
65 | |
66 | static void extractExtensionsAndGlobs(QStringView filter, QStringList &extensions, QStringList &globs) |
67 | { |
68 | extensions.clear(); |
69 | globs.clear(); |
70 | |
71 | const int from = filter.indexOf(c: QLatin1Char('(')); |
72 | const int to = filter.lastIndexOf(c: QLatin1Char(')')) - 1; |
73 | if (from >= 0 && from < to) { |
74 | const QStringView ref = filter.mid(pos: from + 1, n: to - from); |
75 | const QList<QStringView> exts = ref.split(sep: QLatin1Char(' '), behavior: Qt::SkipEmptyParts); |
76 | // For example, given the filter "HTML files (*.html *.htm)", |
77 | // "ref" would be "*.html" and "*.htm". |
78 | for (const QStringView &ref : exts) { |
79 | extensions += extractExtension(filter: ref); |
80 | globs += ref.toString(); |
81 | } |
82 | } |
83 | } |
84 | |
85 | void QQuickFileNameFilter::update(const QString &filter) |
86 | { |
87 | const QStringList filters = nameFilters(); |
88 | |
89 | const int oldIndex = m_index; |
90 | const QString oldName = m_name; |
91 | const QStringList oldExtensions = m_extensions; |
92 | const QStringList oldGlobs = m_globs; |
93 | |
94 | m_index = filters.indexOf(str: filter); |
95 | m_name = extractName(filter); |
96 | extractExtensionsAndGlobs(filter, extensions&: m_extensions, globs&: m_globs); |
97 | |
98 | if (oldIndex != m_index) |
99 | emit indexChanged(index: m_index); |
100 | if (oldName != m_name) |
101 | emit nameChanged(name: m_name); |
102 | if (oldExtensions != m_extensions) |
103 | emit extensionsChanged(extensions: m_extensions); |
104 | if (oldGlobs != m_globs) |
105 | emit globsChanged(globs: m_globs); |
106 | |
107 | qCDebug(lcFileNameFilter).nospace() << "update called on " << this << " of " << parent() |
108 | << " with filter " << filter << " (current filters are " << filters << "):" |
109 | << "\n old index=" << oldIndex << "new index=" << m_index |
110 | << "\n old name=" << oldName << "new name=" << m_name |
111 | << "\n old extensions=" << oldExtensions << "new extensions=" << m_extensions |
112 | << "\n old glob=s" << oldGlobs << "new globs=" << m_globs; |
113 | } |
114 | |
115 | QStringList QQuickFileNameFilter::nameFilters() const |
116 | { |
117 | return m_options ? m_options->nameFilters() : QStringList(); |
118 | } |
119 | |
120 | QString QQuickFileNameFilter::nameFilter(int index) const |
121 | { |
122 | return m_options ? m_options->nameFilters().value(i: index) : QString(); |
123 | } |
124 | |
125 | QT_END_NAMESPACE |
126 | |
127 | #include "moc_qquickfilenamefilter_p.cpp" |
128 | |