5#include <private/qstdweb_p.h>
6#include <QtCore/QRegularExpression>
11std::string qtFilterListToFileInputAccept(
const QStringList &filterList)
14 for (
const auto &
filter : filterList) {
16 emscripten::val::global(
"console").call<
void>(
"log",
filter.toStdString());
20 const auto &extensions =
type->accept()->mimeType().extensions();
21 for (
const auto &
ext : extensions) {
22 emscripten::val::global(
"console").call<
void>(
"log",
26 std::transform(extensions.begin(), extensions.end(), std::back_inserter(transformed),
27 [](
const Type::Accept::MimeType::Extension &
extension) {
28 return extension.value().toString();
32 for (
const QString &tran : transformed) {
33 emscripten::val::global(
"console").call<
void>(
"log", tran.toStdString());
38std::optional<emscripten::val> qtFilterListToTypes(
const QStringList &filterList)
43 auto types = emscripten::val::array();
45 for (
const auto &fileFilter : filterList) {
46 auto type = Type::fromQt(fileFilter);
48 auto jsType = val::object();
49 jsType.set(
"description",
type->description().toString().toStdString());
51 jsType.set(
"accept", ([&
mimeType =
type->accept()->mimeType()]() {
52 val acceptDict = val::object();
54 QList<emscripten::val> extensions;
55 extensions.reserve(mimeType.extensions().size());
57 mimeType.extensions().begin(), mimeType.extensions().end(),
58 std::back_inserter(extensions),
59 [](const Type::Accept::MimeType::Extension &extension) {
60 return val(extension.value().toString().toStdString());
62 acceptDict.set(
"application/octet-stream",
63 emscripten::val::array(extensions.begin(),
68 types.call<
void>(
"push", std::move(jsType));
72 return types[
"length"].as<
int>() == 0 ? std::optional<emscripten::val>() :
types;
76Type::Type(
QStringView description, std::optional<Accept> accept)
77 : m_description(description.trimmed()), m_accept(
std::move(accept))
81Type::~Type() =
default;
96 if (!
match.hasMatch())
99 constexpr size_t DescriptionIndex = 1;
100 constexpr size_t FilterListFromParensIndex = 2;
101 constexpr size_t PlainFilterListIndex = 3;
104 ?
match.capturedView(DescriptionIndex)
106 const auto filterList =
match.capturedView(
match.hasCaptured(FilterListFromParensIndex)
107 ? FilterListFromParensIndex
108 : PlainFilterListIndex);
110 auto accept = Type::Accept::fromQt(filterList);
117Type::Accept::Accept() =
default;
119Type::Accept::~Accept() =
default;
121std::optional<Type::Accept> Type::Accept::fromQt(
QStringView qtRepresentation)
129 auto internalMatch = internalRegex.
matchView(qtRepresentation,
offset);
132 while (internalMatch.hasMatch()) {
133 auto webExtension = MimeType::Extension::fromQt(internalMatch.capturedView(1));
138 mimeType.addExtension(*webExtension);
140 internalMatch = internalRegex.
matchView(qtRepresentation, internalMatch.capturedEnd());
152Type::Accept::MimeType::MimeType() =
default;
154Type::Accept::MimeType::~MimeType() =
default;
158 m_extensions.push_back(std::move(
extension));
163Type::Accept::MimeType::Extension::~Extension() =
default;
165std::optional<Type::Accept::MimeType::Extension>
166Type::Accept::MimeType::Extension::fromQt(
QStringView qtRepresentation)
182 auto extensionMatch = qtFilenameMatcherRegex.
matchView(qtRepresentation);
183 if (extensionMatch.hasMatch())
184 return Extension(extensionMatch.capturedView(2));
192 auto options = emscripten::val::object();
193 if (
auto typeList = qtFilterListToTypes(filterList)) {
194 options.set(
"types", std::move(*
typeList));
195 options.set(
"excludeAcceptAllOption",
true);
198 options.set(
"multiple", acceptMultiple);
205 auto options = emscripten::val::object();
207 if (!suggestedName.empty())
208 options.set(
"suggestedName", emscripten::val(suggestedName));
210 if (
auto typeList = qtFilterListToTypes(filterList))
211 options.set(
"types", emscripten::val(std::move(*
typeList)));
218 return qtFilterListToFileInputAccept(filterList);
void setMimeType(MimeType mimeType)
const QStringView & description() const
const std::optional< Accept > & accept() const
bool hasMatch() const
Returns true if the regular expression matched against the subject string, or false otherwise.
\inmodule QtCore \reentrant
QRegularExpressionMatch matchView(QStringView subjectView, qsizetype offset=0, MatchType matchType=NormalMatch, MatchOptions matchOptions=NoMatchOption) const
static QString anchoredPattern(const QString &expression)
\macro QT_RESTRICTED_CAST_FROM_ASCII
std::string toStdString() const
Returns a std::string object with the data contained in this QString.
std::string makeFileInputAccept(const QStringList &filterList)
emscripten::val makeOpenFileOptions(const QStringList &filterList, bool acceptMultiple)
emscripten::val makeSaveFileOptions(const QStringList &filterList, const std::string &suggestedName)
Combined button and popup list for selecting options.
GLsizei GLenum GLenum * types
GLint GLint GLint GLint GLint GLint GLint GLbitfield GLenum filter
GLenum GLuint GLintptr offset
static QT_BEGIN_NAMESPACE const char * typeList[]
#define QStringLiteral(str)
static bool match(const uchar *found, uint foundLen, const char *target, uint targetLen)