Commit 95e26ee7 authored by Volker Krause's avatar Volker Krause
Browse files

Add JS API to retrieve child nodes matching a given filter

This will be the replacement for the few remaining extractor scripts still
relying on the unintended behavior of being iterated over all detected
barcodes even when that isn't asked for in their filter rules. In a few
cases we cannot change that (which would generally be better), as there
is nothing strongly identifying those barcodes (e.g. just containing
random ids).
parent fa1f8652
Pipeline #55900 passed with stages
in 12 minutes and 55 seconds
......@@ -6,6 +6,7 @@
#include "extractordocumentnode.h"
#include "extractordocumentprocessor.h"
#include "extractorfilter.h"
#include "extractorresult.h"
#include <QJSEngine>
......@@ -214,4 +215,17 @@ void ExtractorDocumentNode::setScriptEngine(QJSEngine* jsEngine) const
}
}
QVariantList ExtractorDocumentNode::findChildNodes(const QJSValue &jsFilter) const
{
const auto filter = ExtractorFilter::fromJSValue(jsFilter);
std::vector<ExtractorDocumentNode> matches;
filter.allMatches(*this, matches);
qDebug() << matches.size() << filter.fieldName() << filter.pattern() << filter.mimeType();
QVariantList l;
l.reserve(matches.size());
std::transform(matches.begin(), matches.end(), std::back_inserter(l), [](const auto &c) { return QVariant::fromValue(c); });
return l;
}
#include "moc_extractordocumentnode.cpp"
......@@ -166,6 +166,9 @@ public:
*/
void appendChild(ExtractorDocumentNode &child);
/** JS API for finding child nodes given an KItinerary::ExtractorFilter. */
Q_INVOKABLE QVariantList findChildNodes(const QJSValue &jsFilter) const;
/** Returns the results that have accumulated so far from this node or its children. */
ExtractorResult result() const;
/** Add additional results from an extraction step. */
......
......@@ -11,6 +11,7 @@
#include "logging.h"
#include <QJsonObject>
#include <QJSValue>
#include <QMetaEnum>
#include <QRegularExpression>
......@@ -256,3 +257,13 @@ void ExtractorFilter::allMatches(const ExtractorDocumentNode &node, std::vector<
return;
}
}
ExtractorFilter ExtractorFilter::fromJSValue(const QJSValue &js)
{
ExtractorFilter f;
f.setMimeType(js.property(QLatin1String("mimeType")).toString());
f.setFieldName(js.property(QLatin1String("field")).toString());
f.setPattern(js.property(QLatin1String("match")).toString());
f.setScope(readEnum<ExtractorFilter::Scope>(js.property(QLatin1String("scope")).toString(), ExtractorFilter::Current));
return f;
}
......@@ -15,6 +15,7 @@
#include <qobjectdefs.h>
class QJsonObject;
class QJSValue;
namespace KItinerary {
......@@ -71,6 +72,8 @@ public:
bool load(const QJsonObject &obj);
/** Serialize filter to a JSON object. */
QJsonObject toJson() const;
/** Create a filter from a JS object value. */
static ExtractorFilter fromJSValue(const QJSValue &js);
[[deprecated("use setMimeType()")]] void setType(ExtractorInput::Type type);
void setMimeType(const QString &mimeType);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment