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