Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
5
6#include <QtCore/qloggingcategory.h>
7
9
10Q_LOGGING_CATEGORY(lcFileNameFilter, "qt.quick.dialogs.qquickfilenamefilter")
11
13 : QObject(parent), m_index(-1)
14{
15}
16
18{
19 return m_index;
20}
21
23{
24 if (m_index == index)
25 return;
26
27 m_index = index;
29}
30
32{
33 return m_name;
34}
35
37{
38 return m_extensions;
39}
40
42{
43 return m_globs;
44}
45
46QSharedPointer<QFileDialogOptions> QQuickFileNameFilter::options() const
47{
48 return m_options;
49}
50
51void QQuickFileNameFilter::setOptions(const QSharedPointer<QFileDialogOptions> &options)
52{
53 m_options = options;
54}
55
57{
58 return filter.left(filter.indexOf(QLatin1Char('(')) - 1);
59}
60
62{
63 return filter.mid(filter.indexOf(QLatin1Char('.')) + 1).toString();
64}
65
67{
68 extensions.clear();
69 globs.clear();
70
71 const int from = filter.indexOf(QLatin1Char('('));
72 const int to = filter.lastIndexOf(QLatin1Char(')')) - 1;
73 if (from >= 0 && from < to) {
74 const QStringView ref = filter.mid(from + 1, to - from);
75 const QList<QStringView> exts = ref.split(QLatin1Char(' '), 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(ref);
80 globs += ref.toString();
81 }
82 }
83}
84
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(filter);
95 m_name = extractName(filter);
96 extractExtensionsAndGlobs(filter, m_extensions, m_globs);
97
98 if (oldIndex != m_index)
99 emit indexChanged(m_index);
100 if (oldName != m_name)
101 emit nameChanged(m_name);
102 if (oldExtensions != m_extensions)
103 emit extensionsChanged(m_extensions);
104 if (oldGlobs != m_globs)
105 emit globsChanged(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
115QStringList QQuickFileNameFilter::nameFilters() const
116{
117 return m_options ? m_options->nameFilters() : QStringList();
118}
119
120QString QQuickFileNameFilter::nameFilter(int index) const
121{
122 return m_options ? m_options->nameFilters().value(index) : QString();
123}
124
126
127#include "moc_qquickfilenamefilter_p.cpp"
QStringList nameFilters() const
\inmodule QtCore
Definition qobject.h:103
QObject * parent() const
Returns a pointer to the parent object.
Definition qobject.h:346
void setOptions(const QSharedPointer< QFileDialogOptions > &options)
void update(const QString &filter)
void globsChanged(const QStringList &globs)
void nameChanged(const QString &name)
QSharedPointer< QFileDialogOptions > options() const
void indexChanged(int index)
void extensionsChanged(const QStringList &extensions)
\inmodule QtCore
\inmodule QtCore
Definition qstringview.h:78
constexpr QStringView mid(qsizetype pos, qsizetype n=-1) const noexcept
Returns the substring of length length starting at position start in this object.
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
Combined button and popup list for selecting options.
@ SkipEmptyParts
Definition qnamespace.h:128
QList< QString > QStringList
Constructs a string list that contains the given string, str.
#define Q_LOGGING_CATEGORY(name,...)
#define qCDebug(category,...)
GLuint index
[2]
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
GLint ref
static QString extractName(const QString &filter)
static QString extractExtension(QStringView filter)
static void extractExtensionsAndGlobs(QStringView filter, QStringList &extensions, QStringList &globs)
static QString extractName(const QString &filter)
#define emit
const QStringList filters({"Image files (*.png *.xpm *.jpg)", "Text files (*.txt)", "Any files (*)" })
[6]
\inmodule QtCore \reentrant
Definition qchar.h:18