25 #include <QtCore/QStringList> 26 #include <QtCore/QMetaEnum> 30 class Predicate::Private
36 operand1(0), operand2(0) {}
64 const QString &property,
const QVariant &value,
69 d->ifaceType = ifaceType;
70 d->property = property;
72 d->compOperator = compOperator;
76 const QString &property,
const QVariant &value,
82 if (((
int)ifaceType)!=-1)
85 d->ifaceType = ifaceType;
86 d->property = property;
88 d->compOperator = compOperator;
97 d->ifaceType = ifaceType;
105 if (((
int)ifaceType)!=-1)
109 d->ifaceType = ifaceType;
115 if (d->type!=PropertyCheck && d->type!=InterfaceCheck) {
126 d->type = other.d->type;
128 if (d->type!=PropertyCheck && d->type!=InterfaceCheck)
132 d->operand1 = operand1;
135 d->operand2 = operand2;
139 d->ifaceType = other.d->ifaceType;
140 d->property = other.d->property;
141 d->value = other.d->value;
142 d->compOperator = other.d->compOperator;
152 result.d->isValid =
true;
153 result.d->type = Conjunction;
154 result.d->operand1 =
new Predicate(*
this);
155 result.d->operand2 =
new Predicate(other);
162 *
this = *
this & other;
170 result.d->isValid =
true;
171 result.d->type = Disjunction;
172 result.d->operand1 =
new Predicate(*
this);
173 result.d->operand2 =
new Predicate(other);
180 *
this = *
this | other;
191 if (!d->isValid)
return false;
196 return d->operand1->matches(device)
197 || d->operand2->matches(device);
199 return d->operand1->matches(device)
200 && d->operand2->matches(device);
207 const int index = iface->metaObject()->indexOfProperty(d->property.toLatin1());
208 QMetaProperty metaProp = iface->metaObject()->property(index);
209 QVariant value = metaProp.isReadable() ? metaProp.read(iface) : QVariant();
210 QVariant expected = d->value;
212 if (metaProp.isEnumType() && expected.type()==QVariant::String) {
213 QMetaEnum metaEnum = metaProp.enumerator();
214 int value = metaEnum.keysToValue(d->value.toString().toLatin1());
218 expected = QVariant();
222 if (d->compOperator==Mask) {
224 int v = value.toInt(&v_ok);
226 int e = expected.toInt(&e_ok);
228 return (e_ok && v_ok && (v &e));
230 return (value == expected);
244 QSet<DeviceInterface::Type> res;
252 res+= d->operand1->usedTypes();
253 res+= d->operand2->usedTypes();
269 if (!d->isValid)
return "False";
271 if (d->type!=PropertyCheck && d->type!=InterfaceCheck)
273 QString op =
" AND ";
274 if (d->type==Disjunction) op =
" OR ";
276 return '['+d->operand1->toString()+op+d->operand2->toString()+
']';
282 if (ifaceName.isEmpty()) ifaceName =
"Unknown";
284 if (d->type==InterfaceCheck) {
285 return "IS "+ifaceName;
290 switch (d->value.type())
292 case QVariant::StringList:
296 const QStringList list = d->value.toStringList();
298 QStringList::ConstIterator it = list.begin();
299 QStringList::ConstIterator end = list.end();
301 for (; it!=end; ++it)
303 value+=
'\''+ *it+
'\'';
315 value = (d->value.toBool()?
"true":
"false");
319 case QVariant::LongLong:
320 case QVariant::ULongLong:
321 value = d->value.toString();
324 value =
'\''+d->value.toString()+
'\'';
328 QString str_operator =
"==";
329 if (d->compOperator!=Equals) str_operator =
" &";
332 return ifaceName+
'.'+d->property+
' '+str_operator+
' '+value;
358 return d->compOperator;
Predicate firstOperand() const
A smaller, inner predicate which is the first to appear and is compared with the second one.
Predicate & operator=(const Predicate &other)
Assignement operator.
Predicate operator|(const Predicate &other)
'Or' operator.
Predicate operator&(const Predicate &other)
'And' operator.
static QString typeToString(Type type)
ComparisonOperator
The comparison operator which can be used for matching within the predicate.
bool isValid() const
Indicates if the predicate is valid.
This class allows applications to deal with devices available in the underlying system.
Base class of all the device interfaces.
Type
This enum type defines the type of device interface that a Device can have.
bool matches(const Device &device) const
Checks if a device matches the predicate.
Predicate secondOperand() const
A smaller, inner predicate which is the second to appear and is compared with the first one.
Predicate & operator&=(const Predicate &other)
'AndEquals' operator.
QVariant matchingValue() const
Retrieves the value used when comparing a devices property to see if it matches the predicate.
QString toString() const
Converts the predicate to its string form.
Type
The predicate type which controls how the predicate is handled.
~Predicate()
Destroys a Predicate object.
static Type stringToType(const QString &type)
bool isDeviceInterface(const DeviceInterface::Type &type) const
Tests if a device interface is available from the device.
Type type() const
Retrieves the predicate type, used to determine how to handle the predicate.
QSet< DeviceInterface::Type > usedTypes() const
Retrieves the device interface types used in this predicate.
DeviceInterface * asDeviceInterface(const DeviceInterface::Type &type)
Retrieves a specialized interface to interact with the device corresponding to a particular device in...
This class implements predicates for devices.
QString propertyName() const
Retrieves the property name used when retrieving the value to compare against.
DeviceInterface::Type interfaceType() const
Retrieves the interface type.
Predicate()
Constructs an invalid predicate.
ComparisonOperator comparisonOperator() const
Retrieves the comparison operator used to compare a property's value.
Predicate & operator|=(const Predicate &other)
'OrEquals' operator.