- class QNdefFilter¶
The
QNdefFilter
class provides a filter for matching NDEF messages. More…Synopsis¶
Methods¶
def
__init__()
def
appendRecord()
def
clear()
def
match()
def
orderMatch()
def
recordAt()
def
recordCount()
def
setOrderMatch()
Note
This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE
Detailed Description¶
The
QNdefFilter
encapsulates the structure of an NDEF message and is used for matching messages that have a particular structure.The following filter matches NDEF messages that contain a single smart poster record:
QNdefFilter filter; filter.append(QNdefRecord::NfcRtd, "Sp");
The following filter matches NDEF messages that contain a URI, a localized piece of text and an optional JPEG image. The order of the records must be in the order specified:
QNdefFilter filter; filter.setOrderMatch(true); filter.appendRecord(QNdefRecord::NfcRtd, "U"); filter.appendRecord<QNdefNfcTextRecord>(); filter.appendRecord(QNdefRecord::Mime, "image/jpeg", 0, 1);
The
match()
method can be used to check if a message matches the filter.Matching Algorithms¶
The filter behavior depends on the value of
orderMatch()
parameter.Note
In the discussion below we will consider the filter records to be equal if their
typeNameFormat
andtype
parameters match. Joining two records means adding theirminimum
andmaximum
values, respectively.Unordered Matching¶
If the record order is not taken into account, all the equal records in the filter can be joined. The resulting filter will contain only unique records, each with the updated
minimum
andmaximum
value.Consider the following example:
QNdefFilter filter; filter.appendRecord<QNdefNfcTextRecord>(0, 1); filter.appendRecord<QNdefNfcTextRecord>(0, 1); filter.appendRecord(QNdefRecord::Mime, "", 1, 1); filter.appendRecord<QNdefNfcTextRecord>(1, 1); filter.setOrderMatch(false);
With the unordered matching, the filter will be simplified to the following:
QNdefFilter filter; filter.appendRecord<QNdefNfcTextRecord>(1, 3); filter.appendRecord(QNdefRecord::Mime, "", 1, 1); filter.setOrderMatch(false);
Once the filter contains only the unique records, the matching algorithm iterates through the message and calculates the actual amount of records of each type. If all the actual amounts fit in the corresponding [minimum, maximum] ranges, the matching algorithm returns
true
.Ordered Matching¶
If the record order is important, a different approach is applied. In this case the equal records can’t be simply joined together. However, the consecutive equal records can still be joined. Then, the matching algorithm iterates through the message, this time also taking the positions of the records into account.
Handling Empty Type in Filter Record¶
It’s possible to add a filter record with an empty
type
. In this case the empty type will act as a wildcard for any type.For example, the filter can be defined as follows:
QNdefFilter filter; filter.addRecord(QNdefRecord::Mime, "", 1, 1);
This filter specifies that the message must contain exactly one NDEF record with
Mime
typeNameFormat
(), and anytype
().Handling Extra Records in the Message¶
If the message contains some records that do not match any record in the filter, the matching algorithm will return
false
.Filter Examples¶
In the table below, each filter record is specified by the following parameters (in the given order):
typeNameFormat
- contains thetypeNameFormat
() of the record.type
- contains thetype
() of the record.minimum
- contains the minimum amount of occurrences of the record in the message.maximum
- contains the maximum amount of occurrences of the record in the message.
The filter contains multiple records.
The message consists of multiple
QNdefRecord
s. In the table below, only thetypeNameFormat
() andtype
() of each record will be shown, because the other parameters do not matter for filtering.Filter
Message
Match Result
Comment
Empty filter
Empty message
Match
Empty filter
Non-empty message
No match
Non-empty filter
Empty message
No match
[(
NfcRtd
, “T”, 1, 2), (Mime
, “”, 1, 1), (Empty
, “”, 0, 100)][(
Mime
, “image/jpeg”), (Empty
, “”), (NfcRtd
, “T”), (Empty
, “”), (NfcRtd
, “T”)]Unordered: match
Ordered filter does not match because the message must start with a
NfcRtd
record, but it starts withMime
.Ordered: no match
[(
NfcRtd
, “T”, 0, 2), (Mime
, “”, 1, 1), (NfcRtd
, “T”, 1, 1)]Unordered: match
Ordered filter does not match because an
NfcRtd
record is expected afterMime
, but the message does not have it.Ordered: no match
[(
NfcRtd
, “T”, 0, 2), (NfcRtd
, “T”, 1, 1), (Mime
, “”, 1, 1)]Unordered: match
Both cases match because the message contains the required minimum of records in the correct order.
Ordered: match
- __init__()¶
Constructs a new NDEF filter.
- __init__(other)
- Parameters:
other –
QNdefFilter
Constructs a new NDEF filter that is a copy of
other
.- appendRecord(record)¶
- Parameters:
record –
Record
- Return type:
bool
Verifies the
record
and appends it to the NDEF filter.Returns
true
if the record was appended successfully. Otherwise returnsfalse
.- appendRecord(typeNameFormat, type[, min=1[, max=1]])
- Parameters:
typeNameFormat –
TypeNameFormat
type –
QByteArray
min – int
max – int
- Return type:
bool
Appends a record with type name format
typeNameFormat
and typetype
to the NDEF filter. The record must occur betweenmin
andmax
times in the NDEF message.Returns
true
if the record was appended successfully. Otherwise returnsfalse
.- clear()¶
Clears the filter.
- match(message)¶
- Parameters:
message –
QNdefMessage
- Return type:
bool
Returns
true
if themessage
matches the given filter. Otherwise returnsfalse
.See
Matching Algorithms
for more detailed explanation of matching.- orderMatch()¶
- Return type:
bool
Returns
true
if the filter takes NDEF record order into account when matching. Otherwise returnsfalse
.See also
- recordAt(i)¶
- Parameters:
i – int
- Return type:
Record
Returns the NDEF record at index
i
.i
must be a valid index (i.e. 0 <= i <recordCount()
).See also
- recordCount()¶
- Return type:
int
Returns the number of NDEF records in the filter.
- setOrderMatch(on)¶
- Parameters:
on – bool
Sets the ordering requirements of the filter. If
on
istrue
, the filter will only match if the order of records in the filter matches the order of the records in the NDEF message. Ifon
isfalse
, the order of the records is not taken into account when matching.By default record order is not taken into account.
See also