26 #include <QDataStream> 27 #include <QTextStream> 55 foreach (
QString pname, pnames) {
66 for (QVariantMap::const_iterator it = data.
begin(); it != data.
end(); ++it) {
70 qWarning()<<
KLF_FUNC_NAME<<
": Can't set property "<<it.key()<<
" to "<<it.value();
100 : pPropNameSpace(propNameSpace)
103 if (!pRegisteredProperties.
contains(propNameSpace))
105 if (!pRegisteredPropertiesMaxId.
contains(propNameSpace))
106 pRegisteredPropertiesMaxId[propNameSpace] = -1;
118 qWarning(
"%s[%s](): Property `%s' not registered.",
KLF_FUNC_NAME, qPrintable(pPropNameSpace),
119 qPrintable(propname));
126 if (propId >= 0 && propId < pProperties.
size()) {
128 return pProperties[
propId];
131 qWarning(
"%s[%s](%d): invalid property ID.",
KLF_FUNC_NAME, qPrintable(pPropNameSpace),
174 qWarning(
"%s[%s](): Property `%s' not registered.",
KLF_FUNC_NAME, qPrintable(pPropNameSpace),
175 qPrintable(propname));
182 if (propId >= 0 && propId < pProperties.
size()) {
185 pProperties[
propId] = value;
190 qWarning(
"%s[%s](id=%d): invalid property ID.",
KLF_FUNC_NAME, qPrintable(pPropNameSpace),
197 if (propId <= maxId) {
198 pProperties.
resize(maxId + 1);
200 if (propId < 0 || propId >= pProperties.
size() ||
202 qWarning(
"%s[%s](id=%d): invalid property id.",
KLF_FUNC_NAME, qPrintable(pPropNameSpace),
207 pProperties[
propId] = value;
213 klfDbg(
"propname="<<propname<<
" value="<<value) ;
230 for (k = 0; k < pProperties.
size(); ++k) {
231 if (pProperties[k].isValid())
242 for (k = 0; k < pProperties.
size(); ++k) {
243 if (pProperties[k].isValid())
258 for (k = 0; k < propertyList.
size(); ++k) {
281 klfDbg(
"propValues="<<propValues) ;
286 for (k = 0; k < propKeys.
size(); ++k) {
288 bool ok = (
doLoadProperty(propKeys[k], propValues[propKeys[k]]) >= 0);
291 qWarning()<<
KLF_FUNC_NAME<<
": Failed to load property "<<propKeys[k]<<
" with value "<<propValues[propKeys[k]];
352 s =
QString(
"<div class=\"klfpobj_entry\">\n<div class=\"klfpobj_name\">%2</div>\n")
355 s =
QString(
"<table class=\"klfpobj_tentry\">\n" 356 "<tr colspan=\"2\" class=\"klfpobj_tname\"><th>%1</th></tr>\n")
363 for (k = 0; k < props.
size(); ++k) {
367 bool canstring = vval.
canConvert(QVariant::String);
371 s +=
QString(
"<div class=\"klfpobj_prop_%1\"><div class=\"klfpobj_propname\">%2</div>: " 372 "<div class=\"klfpobj_propvalue\">%3</div></div>\n")
375 s +=
QString(
" <tr class=\"klfpobj_tprop_%1\"><td class=\"klfpobj_tpropname\">%2</td>" 376 "<td class=\"klfpobj_tpropvalue\">%3</td></tr>\n")
380 if (!isnull && canstring) {
383 value =
'"' + value +
'"';
384 }
else if (!isnull) {
457 internalRegisterProperty(pnamespace, name, propId);
461 return internalRegisterProperty(propNameSpace, propName, -1);
465 if ( ! pRegisteredPropertiesMaxId.
contains(propNameSpace) ) {
466 qWarning(
"%s(): property name space `%s' does not exist!",
KLF_FUNC_NAME,
467 qPrintable(propNameSpace));
470 return pRegisteredPropertiesMaxId[propNameSpace];
483 if ( ! pRegisteredProperties.
contains(propNameSpace) ) {
484 qWarning(
"%s: property name space `%s' does not exist!",
KLF_FUNC_NAME,
485 qPrintable(propNameSpace));
491 return propList.
value(name);
495 if ( ! pRegisteredProperties.
contains(propNameSpace) ) {
496 qWarning(
"%s: property name space `%s' does not exist!",
KLF_FUNC_NAME,
497 qPrintable(propNameSpace));
504 if (keyList.
size() > 1) {
505 qWarning(
"%s: What's going on?? property Id=%d not unique in prop name space `%s'.",
512 if ( ! pRegisteredProperties.
contains(propNameSpace) ) {
513 qWarning(
"%s: property name space `%s' does not exist!",
KLF_FUNC_NAME,
514 qPrintable(propNameSpace));
518 return pRegisteredProperties[propNameSpace].
keys();
522 if ( ! pRegisteredProperties.
contains(propNameSpace) ) {
523 qWarning(
"%s: property name space `%s' does not exist!",
KLF_FUNC_NAME,
524 qPrintable(propNameSpace));
528 return pRegisteredProperties[propNameSpace].
values();
533 if ( ! pRegisteredProperties.
contains(propNameSpace) ) {
534 qWarning(
"%s: property name space `%s' does not exist!",
KLF_FUNC_NAME,
535 qPrintable(propNameSpace));
538 return pRegisteredProperties[propNameSpace];
550 int KLFPropertizedObject::internalRegisterProperty(
const QString& propNameSpace,
555 klfDbg(
"propNameSpace = " << propNameSpace <<
", propName = " << propName <<
", propId = " << propId) ;
559 if (pRegisteredPropertiesMaxId.
contains(propNameSpace)) {
560 propMaxId = pRegisteredPropertiesMaxId[propNameSpace];
564 propId = propMaxId + 1;
569 if (propId > propMaxId) {
573 if ( propList.
keys(propId).size() > 0 ) {
575 if (propName == oldPropName) {
578 qWarning(
"%s[%s]: Property ID `%d' is already registered with conflicting names!\n" 579 "\told name is `%s', new is `%s'",
580 KLF_FUNC_NAME, qPrintable(propNameSpace), propId, qPrintable(oldPropName),
581 qPrintable(propName));
586 qWarning(
"%s[%s]: Cannot Register a property with empty name!",
KLF_FUNC_NAME,
587 qPrintable(propNameSpace));
591 qWarning(
"%s[%s]: Property `%s' already registered.",
KLF_FUNC_NAME, qPrintable(propNameSpace),
592 qPrintable(propName));
597 pRegisteredProperties[propNameSpace][propName] =
propId;
599 pRegisteredPropertiesMaxId[propNameSpace] = propMaxId;
606 if (a.pPropNameSpace != b.pPropNameSpace)
610 for (k = 0; k < propIds.
size(); ++k)
bool canConvert(int targetTypeId) const
virtual bool setAllProperties(const QMap< QString, QVariant > &propValues)
Initializes properties to given values.
virtual ~KLFPropertizedObject()
bool contains(const Key &key) const
KLF_EXPORT QDataStream & operator<<(QDataStream &stream, const KLFEnumType &e)
QList< T > values() const
Encapsulates output in an HTML <table> and escapes strings.
KLF_EXPORT QDataStream & operator>>(QDataStream &stream, KLFEnumType &e)
QStringList propertyNameList() const
A list of properties that have been set.
QStringList registeredPropertyNameList() const
See the corresponding protected static method.
QDataStream & streamFrom(QDataStream &stream)
Explicit function name for the simple "operator>>".
KLFAbstractPropertizedObject()
#define klfDbg(streamableItems)
print debug stream items
KLFPropertizedObject(const QString &propertyNameSpace)
#define KLF_DEBUG_BLOCK(msg)
Utility to debug the execution of a block.
void setAllPropertiesFromByteArray(const QByteArray &data)
Loads all properties saved by allPropertiesToByteArray()
QByteArray allPropertiesToByteArray() const
Saves all the properties in binary form.
virtual bool setProperty(const QString &pname, const QVariant &value)=0
Assign a value to a property.
void registerBuiltInProperty(int propId, const QString &propName) const
virtual QVariant property(const QString &propName) const
QDataStream & streamInto(QDataStream &stream) const
Explicit function name for the simple "operator<<".
virtual void propertyValueChanged(int propId, const QVariant &oldValue, const QVariant &newValue)
A class that holds properties.
virtual QMap< QString, QVariant > allProperties() const
Convenience function to retrieve all properties.
QList< Key > keys() const
Ensures that non-html output is machine parsable.
QMap< QString, QVariant > allProperties() const
Returns all properties that have been set.
friend bool operator==(const KLFPropertizedObject &a, const KLFPropertizedObject &b)
virtual QStringList propertyNameList() const =0
Queries what property are (or can be) set.
virtual QVariant property(const QString &propName) const =0
get a property's value
virtual ~KLFSpecifyableType()
QList< int > propertyIdList() const
A list of properties that have been set.
int registerProperty(const QString &propertyName) const
virtual bool setProperty(const QString &propname, const QVariant &value)
Sets the given property to the given value.
virtual bool doSetProperty(const QString &propname, const QVariant &value)
QString propertyNameForId(int propId) const
See the corresponding protected static method.
QList< int > registeredPropertyIdList() const
See the corresponding protected static method.
QString toHtmlEscaped() const
virtual bool hasPropertyValue(const QString &propName) const
Tests if a property was set.
bool propertyNameRegistered(const QString &propertyName) const
See the corresponding protected static method.
QString & replace(int position, int n, QChar after)
virtual int doLoadProperty(const QString &propname, const QVariant &value)
int propertyMaxId() const
See the corresponding protected static method.
const char * typeName() const
bool setSpecification(const QByteArray &data)
virtual bool setAllProperties(const QMap< QString, QVariant > &data)
Convenience function to load a set of property values.
int propertyIdForName(const QString &propertyName) const
See the corresponding protected static method.
#define KLF_ASSERT_CONDITION(expr, msg, failaction)
Asserting Conditions (NON-FATAL)
QMap< QString, int > registeredProperties() const
See the corresponding protected static method.
Include also all non-explicitely-set properties.
virtual QString toString(uint toStringFlags=0) const
Formats the property contents in a (human and/or parsable) string.
virtual ~KLFAbstractPropertizedObject()
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const
bool propertyIdRegistered(int propId) const
See the corresponding protected static method.
Uses <div> with CSS classes instead of a table (HTML only)
QByteArray specification() const
const T value(const Key &key, const T &defaultValue) const