diff options
Diffstat (limited to 'src/quickdialogs/quickdialogsutils')
5 files changed, 281 insertions, 0 deletions
diff --git a/src/quickdialogs/quickdialogsutils/CMakeLists.txt b/src/quickdialogs/quickdialogsutils/CMakeLists.txt new file mode 100644 index 0000000000..32c175241b --- /dev/null +++ b/src/quickdialogs/quickdialogsutils/CMakeLists.txt @@ -0,0 +1,29 @@ +# Copyright (C) 2022 The Qt Company Ltd. +# SPDX-License-Identifier: BSD-3-Clause + +# This library exists because QuickDialogs2 and QuickDialogs2QuickImpl +# both need QQuickNameFilter. QQuickNameFilter was originally in +# QuickDialogs2. Since QuickDialogs2 already links to +# QuickDialogs2QuickImpl, making the latter link to the former (to get +# access to QQuickNameFilter) would result in a circular dependency, +# so we have this library as a result. + +qt_internal_add_module(QuickDialogs2Utils + SOURCES + qquickdialogtype_p.h + qquickfilenamefilter.cpp + qquickfilenamefilter_p.h + qtquickdialogs2utilsglobal_p.h + DEFINES + QT_BUILD_QUICKDIALOGS2UTILS_LIB + QT_NO_CAST_FROM_ASCII + QT_NO_CAST_TO_ASCII + INCLUDE_DIRECTORIES + ${CMAKE_CURRENT_SOURCE_DIR} + LIBRARIES + Qt::GuiPrivate + PUBLIC_LIBRARIES + Qt::Core + GENERATE_CPP_EXPORTS + GENERATE_PRIVATE_CPP_EXPORTS +) diff --git a/src/quickdialogs/quickdialogsutils/qquickdialogtype_p.h b/src/quickdialogs/quickdialogsutils/qquickdialogtype_p.h new file mode 100644 index 0000000000..1778ef3a14 --- /dev/null +++ b/src/quickdialogs/quickdialogsutils/qquickdialogtype_p.h @@ -0,0 +1,35 @@ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +#ifndef QQUICKDIALOGTYPE_P_H +#define QQUICKDIALOGTYPE_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include <QtCore/qnamespace.h> +#include <QtCore/private/qglobal_p.h> + +QT_BEGIN_NAMESPACE + +// We need our own type for the extra FolderDialog value, so that we can load FolderDialog.qml, +// otherwise we would just use QPlatformTheme::DialogType and then we wouldn't need this. +enum class QQuickDialogType { + FileDialog, + ColorDialog, + FontDialog, + MessageDialog, + FolderDialog +}; + +QT_END_NAMESPACE + +#endif // QQUICKDIALOGTYPE_P_H diff --git a/src/quickdialogs/quickdialogsutils/qquickfilenamefilter.cpp b/src/quickdialogs/quickdialogsutils/qquickfilenamefilter.cpp new file mode 100644 index 0000000000..537ca1f058 --- /dev/null +++ b/src/quickdialogs/quickdialogsutils/qquickfilenamefilter.cpp @@ -0,0 +1,127 @@ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +#include "qquickfilenamefilter_p.h" + +#include <QtCore/qloggingcategory.h> + +QT_BEGIN_NAMESPACE + +Q_LOGGING_CATEGORY(lcFileNameFilter, "qt.quick.dialogs.qquickfilenamefilter") + +QQuickFileNameFilter::QQuickFileNameFilter(QObject *parent) + : QObject(parent), m_index(-1) +{ +} + +int QQuickFileNameFilter::index() const +{ + return m_index; +} + +void QQuickFileNameFilter::setIndex(int index) +{ + if (m_index == index) + return; + + m_index = index; + emit indexChanged(index); +} + +QString QQuickFileNameFilter::name() const +{ + return m_name; +} + +QStringList QQuickFileNameFilter::extensions() const +{ + return m_extensions; +} + +QStringList QQuickFileNameFilter::globs() const +{ + return m_globs; +} + +QSharedPointer<QFileDialogOptions> QQuickFileNameFilter::options() const +{ + return m_options; +} + +void QQuickFileNameFilter::setOptions(const QSharedPointer<QFileDialogOptions> &options) +{ + m_options = options; +} + +static QString extractName(const QString &filter) +{ + return filter.left(filter.indexOf(QLatin1Char('(')) - 1); +} + +static QString extractExtension(QStringView filter) +{ + return filter.mid(filter.indexOf(QLatin1Char('.')) + 1).toString(); +} + +static void extractExtensionsAndGlobs(QStringView filter, QStringList &extensions, QStringList &globs) +{ + extensions.clear(); + globs.clear(); + + const int from = filter.indexOf(QLatin1Char('(')); + const int to = filter.lastIndexOf(QLatin1Char(')')) - 1; + if (from >= 0 && from < to) { + const QStringView ref = filter.mid(from + 1, to - from); + const QList<QStringView> exts = ref.split(QLatin1Char(' '), Qt::SkipEmptyParts); + // For example, given the filter "HTML files (*.html *.htm)", + // "ref" would be "*.html" and "*.htm". + for (const QStringView &ref : exts) { + extensions += extractExtension(ref); + globs += ref.toString(); + } + } +} + +void QQuickFileNameFilter::update(const QString &filter) +{ + const QStringList filters = nameFilters(); + + const int oldIndex = m_index; + const QString oldName = m_name; + const QStringList oldExtensions = m_extensions; + const QStringList oldGlobs = m_globs; + + m_index = filters.indexOf(filter); + m_name = extractName(filter); + extractExtensionsAndGlobs(filter, m_extensions, m_globs); + + if (oldIndex != m_index) + emit indexChanged(m_index); + if (oldName != m_name) + emit nameChanged(m_name); + if (oldExtensions != m_extensions) + emit extensionsChanged(m_extensions); + if (oldGlobs != m_globs) + emit globsChanged(m_globs); + + qCDebug(lcFileNameFilter).nospace() << "update called on " << this << " of " << parent() + << " with filter " << filter << " (current filters are " << filters << "):" + << "\n old index=" << oldIndex << "new index=" << m_index + << "\n old name=" << oldName << "new name=" << m_name + << "\n old extensions=" << oldExtensions << "new extensions=" << m_extensions + << "\n old glob=s" << oldGlobs << "new globs=" << m_globs; +} + +QStringList QQuickFileNameFilter::nameFilters() const +{ + return m_options ? m_options->nameFilters() : QStringList(); +} + +QString QQuickFileNameFilter::nameFilter(int index) const +{ + return m_options ? m_options->nameFilters().value(index) : QString(); +} + +QT_END_NAMESPACE + +#include "moc_qquickfilenamefilter_p.cpp" diff --git a/src/quickdialogs/quickdialogsutils/qquickfilenamefilter_p.h b/src/quickdialogs/quickdialogsutils/qquickfilenamefilter_p.h new file mode 100644 index 0000000000..20722f01fa --- /dev/null +++ b/src/quickdialogs/quickdialogsutils/qquickfilenamefilter_p.h @@ -0,0 +1,69 @@ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +#ifndef QQUICKFILENAMEFILTER_P_H +#define QQUICKFILENAMEFILTER_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include <QtCore/qobject.h> +#include <QtCore/qsharedpointer.h> +#include <QtCore/qstringlist.h> +#include <QtGui/qpa/qplatformdialoghelper.h> + +#include "qtquickdialogs2utilsglobal_p.h" + +QT_BEGIN_NAMESPACE + +class Q_QUICKDIALOGS2UTILS_PRIVATE_EXPORT QQuickFileNameFilter : public QObject +{ + Q_OBJECT + Q_PROPERTY(int index READ index WRITE setIndex NOTIFY indexChanged FINAL) + Q_PROPERTY(QString name READ name NOTIFY nameChanged FINAL) + Q_PROPERTY(QStringList extensions READ extensions NOTIFY extensionsChanged FINAL) + Q_PROPERTY(QStringList globs READ globs NOTIFY globsChanged FINAL) + +public: + explicit QQuickFileNameFilter(QObject *parent = nullptr); + + int index() const; + void setIndex(int index); + + QString name() const; + QStringList extensions() const; + QStringList globs() const; + + QSharedPointer<QFileDialogOptions> options() const; + void setOptions(const QSharedPointer<QFileDialogOptions> &options); + + void update(const QString &filter); + +Q_SIGNALS: + void indexChanged(int index); + void nameChanged(const QString &name); + void extensionsChanged(const QStringList &extensions); + void globsChanged(const QStringList &globs); + +private: + QStringList nameFilters() const; + QString nameFilter(int index) const; + + int m_index; + QString m_name; + QStringList m_extensions; + QStringList m_globs; + QSharedPointer<QFileDialogOptions> m_options; +}; + +QT_END_NAMESPACE + +#endif // QQUICKFILENAMEFILTER_P_H diff --git a/src/quickdialogs/quickdialogsutils/qtquickdialogs2utilsglobal_p.h b/src/quickdialogs/quickdialogsutils/qtquickdialogs2utilsglobal_p.h new file mode 100644 index 0000000000..6a59111d14 --- /dev/null +++ b/src/quickdialogs/quickdialogsutils/qtquickdialogs2utilsglobal_p.h @@ -0,0 +1,21 @@ +// Copyright (C) 2021 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only + +#ifndef QTQUICKDIALOGS2UTILSGLOBAL_P_H +#define QTQUICKDIALOGS2UTILSGLOBAL_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include <QtCore/qglobal.h> +#include <QtQuickDialogs2Utils/private/qtquickdialogs2utilsexports_p.h> + +#endif // QTQUICKDIALOGS2UTILSUTILSGLOBAL_P_H |