oopapidocs
2.0
|
00001 #include <qglobal.h> 00002 #include <QTextDocument> 00003 #include <QTime> 00004 #include <QMetaObject> 00005 #include <QMetaProperty> 00006 #include <QList> 00007 #include <QDebug> 00008 #include "qobjectwriter.h" 00009 00010 QObjectWriter:: 00011 QObjectWriter(const VariantWriter* vwriter, bool children) 00012 : m_vwriter (vwriter), m_children(children) 00013 { } 00014 00015 QStringList QObjectWriter:: 00016 propertyNames(const QObject* obj) const { 00017 const QMetaObject* meta = obj->metaObject(); 00018 QStringList retval; 00019 00020 QList<QByteArray> dynamicProps = obj->dynamicPropertyNames(); 00021 00022 foreach (const QByteArray& qba, dynamicProps) 00023 retval << QString(qba); 00024 00025 for (int i=0; i<meta->propertyCount(); ++i) 00026 retval << meta->property(i).name(); 00027 00028 return retval; 00029 } 00030 00031 void QObjectWriter:: 00032 mapType(QVariant::Type t, VariantWriter* vw) { 00033 m_map.insert(t, vw); 00034 } 00035 00036 QString QObjectWriter:: 00037 toString(const QVariant& val, const QMetaProperty& mprop) const { 00038 QString result; 00039 QVariant::Type t = mprop.type(); 00040 if (t == QVariant::Time) { 00041 QTime t = qVariantValue<QTime>(val); 00042 if (t.hour() > 0) { 00043 return t.toString("hh:mm:ss"); 00044 } 00045 else { 00046 return t.toString("m:ss"); 00047 } 00048 } 00049 if (mprop.isEnumType()) { 00050 int value = val.toInt(); 00051 QMetaEnum menum = mprop.enumerator(); 00052 if (mprop.isFlagType()) { 00053 QStringList selectedFlags; 00054 int kc = menum.keyCount(); 00055 for (int j=0; j<kc; ++j) { 00056 if (menum.value(j) == 0) continue; 00057 if ((value & menum.value(j)) == menum.value(j)) { 00058 selectedFlags << menum.key(j); 00059 } 00060 } 00061 result = selectedFlags.join("|") + QString(" (%1)").arg(val.toInt()); 00062 } 00063 else result = QString("%1 (%2)").arg(menum.valueToKey(value)).arg(val.toInt()); 00064 return result; 00065 } 00066 if (m_map.contains(t)) { 00067 VariantWriter* vw = m_map[t]; 00068 result = vw->toString(val, mprop); 00069 } 00070 else if (m_vwriter != 0 && result == QString()) 00071 result = m_vwriter->toString(val, mprop); 00072 if (result == QString()) 00073 result = Qt::escape(val.toString()); 00074 else 00075 result = Qt::escape(result); 00076 return result; 00077 } 00078 //start 00079 QString QObjectWriter:: 00080 toString(const QObject* obj, int indentLevel) const { 00081 QStringList result; 00082 QString indentspace; 00083 indentspace.fill(' ', indentLevel * 3 ); 00084 QString className = obj->metaObject()->className(); 00085 QString objectName = obj->objectName(); 00086 QStringList propnames = propertyNames(obj); 00087 foreach (const QString &propName, propnames) { 00088 if (propName == "objectName") continue; 00089 QVariant qv = obj->property(propName.toAscii()); 00090 00091 if (propName == "className") { 00092 className = qv.toString(); 00093 continue; 00094 } 00095 const QMetaObject* meta = obj->metaObject(); 00096 int idx = meta->indexOfProperty(propName.toAscii()); 00097 QMetaProperty mprop = meta->property(idx); 00098 00099 result << 00100 QString("%1 <property name=\"%2\" type=\"%3\" value=\"%4\" />") 00101 .arg(indentspace).arg(propName). 00102 arg(qv.typeName()).arg(toString(qv, mprop)); 00103 } 00104 /* Query over QObjects */ 00105 if (m_children) { 00106 QList<QObject*> childlist = 00107 qFindChildren<QObject*>(obj, QString()); 00108 00109 foreach (const QObject* child, childlist) { 00110 if (child->parent() != obj) { 00111 // qDebug() << "This is not my child!!"; 00112 continue; 00113 } 00114 if (child != 0) { 00115 result << toString(child, indentLevel+1); 00116 } 00117 } 00118 } 00119 00120 result.insert(0, QString("\n%1<object class=\"%2\" name=\"%3\" >") 00121 .arg(indentspace).arg(className).arg(objectName)); 00122 result << QString("%1</object>\n").arg(indentspace); 00123 return result.join("\n"); 00124 } 00125 //end