bool UiExtractor::checkProperty(QObject *obj, const QString &prop) const { const QMetaObject *mo = obj->metaObject(); const QMetaProperty mp = mo->property(mo->indexOfProperty(prop.toLatin1())); // TODO come up with some more aggressive filtering if (mp.isValid() && mp.isDesignable(obj) && mp.isStored(obj) && mp.isWritable()) { const QVariant value = mp.read(obj); // try to figure out the default by resetting to it if (mp.isResettable()) { mp.reset(obj); if (mp.read(obj) == value) { return false; } mp.write(obj, value); return true; } // some guessing for non-resettable properties if (value.isNull() || !value.isValid()) { return false; } if (value.type() == QVariant::String) { return !value.toString().isEmpty(); } else if (value.type() == QVariant::Locale) { return value.value<QLocale>() != QLocale::system(); } return true; } return false; }
QVariant ObjectStaticPropertyModel::data(const QModelIndex &index, int role) const { if (!index.isValid() || !m_obj || index.row() < 0 || index.row() >= m_obj.data()->metaObject()->propertyCount()) { return QVariant(); } const QMetaProperty prop = m_obj.data()->metaObject()->property(index.row()); if (role == Qt::DisplayRole) { if (index.column() == 0) { return prop.name(); } else if (index.column() == 1) { // QMetaProperty::read sets QVariant::typeName to int for enums, // so we need to handle that separately here const QVariant value = prop.read(m_obj.data()); const QString enumStr = Util::enumToString(value, prop.typeName(), m_obj.data()); if (!enumStr.isEmpty()) { return enumStr; } return VariantHandler::displayString(value); } else if (index.column() == 2) { return prop.typeName(); } else if (index.column() == 3) { const QMetaObject *mo = m_obj.data()->metaObject(); while (mo->propertyOffset() > index.row()) { mo = mo->superClass(); } return mo->className(); } } else if (role == Qt::DecorationRole) { if (index.column() == 1) { return VariantHandler::decoration(prop.read(m_obj.data())); } } else if (role == Qt::EditRole) { if (index.column() == 1) { return prop.read(m_obj.data()); } } else if (role == Qt::ToolTipRole) { const QString toolTip = tr("Constant: %1\nDesignable: %2\nFinal: %3\nResetable: %4\n" "Has notification: %5\nScriptable: %6\nStored: %7\nUser: %8\nWritable: %9"). arg(translateBool(prop.isConstant())). arg(translateBool(prop.isDesignable(m_obj.data()))). arg(translateBool(prop.isFinal())). arg(translateBool(prop.isResettable())). arg(translateBool(prop.hasNotifySignal())). arg(translateBool(prop.isScriptable(m_obj.data()))). arg(translateBool(prop.isStored(m_obj.data()))). arg(translateBool(prop.isUser(m_obj.data()))). arg(translateBool(prop.isWritable())); return toolTip; } return QVariant(); }
QVariant ObjectStaticPropertyModel::data(const QModelIndex &index, int role) const { if (!index.isValid() || !m_obj || index.row() < 0 || index.row() >= m_obj.data()->metaObject()->propertyCount()) { return QVariant(); } const QMetaProperty prop = m_obj.data()->metaObject()->property(index.row()); if (role == Qt::DisplayRole) { if (index.column() == 0) { return prop.name(); } else if (index.column() == 1) { return Util::variantToString(prop.read(m_obj.data())); } else if (index.column() == 2) { return prop.typeName(); } else if (index.column() == 3) { const QMetaObject *mo = m_obj.data()->metaObject(); while (mo->propertyOffset() > index.row()) { mo = mo->superClass(); } return mo->className(); } } else if (role == Qt::EditRole) { if (index.column() == 1) { return prop.read(m_obj.data()); } } else if (role == Qt::ToolTipRole) { const QString toolTip = tr("Constant: %1\nDesignable: %2\nFinal: %3\nResetable: %4\n" "Has notification: %5\nScriptable: %6\nStored: %7\nUser: %8\nWritable: %9"). arg(translateBool(prop.isConstant())). arg(translateBool(prop.isDesignable(m_obj.data()))). arg(translateBool(prop.isFinal())). arg(translateBool(prop.isResettable())). arg(translateBool(prop.hasNotifySignal())). arg(translateBool(prop.isScriptable(m_obj.data()))). arg(translateBool(prop.isStored(m_obj.data()))). arg(translateBool(prop.isUser(m_obj.data()))). arg(translateBool(prop.isWritable())); return toolTip; } return QVariant(); }
void Serializer::SerializeObjectProperties( const QObject *pObject ) { if (pObject != NULL) { const QMetaObject *pMetaObject = pObject->metaObject(); int nCount = pMetaObject->propertyCount(); for (int nIdx=0; nIdx < nCount; ++nIdx ) { QMetaProperty metaProperty = pMetaObject->property( nIdx ); if (metaProperty.isDesignable( pObject )) { const char *pszPropName = metaProperty.name(); QString sPropName( pszPropName ); if ( sPropName.compare( "objectName" ) == 0) continue; bool bHash = false; if (ReadPropertyMetadata( pObject, sPropName, "transient").toLower() != "true" ) { bHash = true; m_hash.addData( sPropName.toUtf8() ); } QVariant value( pObject->property( pszPropName ) ); if (bHash && !value.canConvert< QObject* >()) { m_hash.addData( value.toString().toUtf8() ); } AddProperty( sPropName, value, pMetaObject, &metaProperty ); } } } }
QString ObjectStaticPropertyModel::detailString(const QMetaProperty& prop) const { QStringList s; s << tr("Constant: %1").arg(translateBool(prop.isConstant())); s << tr("Designable: %1").arg(translateBool(prop.isDesignable(m_obj.data()))); s << tr("Final: %1").arg(translateBool(prop.isFinal())); if (prop.hasNotifySignal()) { s << tr("Notification: %1").arg(Util::prettyMethodSignature(prop.notifySignal())); } else { s << tr("Notification: no"); } s << tr("Resetable: %1").arg(translateBool(prop.isResettable())); #if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0) s << tr("Revision: %1").arg(prop.revision()); #endif s << tr("Scriptable: %1").arg(translateBool(prop.isScriptable(m_obj.data()))); s << tr("Stored: %1").arg(translateBool(prop.isStored(m_obj.data()))); s << tr("User: %1").arg(translateBool(prop.isUser(m_obj.data()))); s << tr("Writable: %1").arg(translateBool(prop.isWritable())); return s.join("\n"); }
QString QMetaPropertyAdaptor::detailString(const QMetaProperty &prop) const { QObject *obj = object().qtObject(); QStringList s; s << tr("Constant: %1").arg(translateBool(prop.isConstant())); s << tr("Designable: %1").arg(translateBool(prop.isDesignable(obj))); s << tr("Final: %1").arg(translateBool(prop.isFinal())); if (prop.hasNotifySignal()) s << tr("Notification: %1").arg(Util::prettyMethodSignature(prop.notifySignal())); else s << tr("Notification: no"); s << tr("Resetable: %1").arg(translateBool(prop.isResettable())); #if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0) s << tr("Revision: %1").arg(prop.revision()); #endif s << tr("Scriptable: %1").arg(translateBool(prop.isScriptable(obj))); s << tr("Stored: %1").arg(translateBool(prop.isStored(obj))); s << tr("User: %1").arg(translateBool(prop.isUser(obj))); s << tr("Writable: %1").arg(translateBool(prop.isWritable())); return s.join(QStringLiteral("\n")); }
void XmlPListSerializer::SerializePListObjectProperties(const QString &sName, const QObject *pObject, bool needKey ) { if (!pObject) return; if (needKey) { QString sItemName = GetItemName(sName); m_pXmlWriter->writeTextElement("key", sItemName); } m_pXmlWriter->writeStartElement("dict"); const QMetaObject *pMetaObject = pObject->metaObject(); int nCount = pMetaObject->propertyCount(); for (int nIdx=0; nIdx < nCount; ++nIdx) { QMetaProperty metaProperty = pMetaObject->property(nIdx); if (metaProperty.isDesignable(pObject)) { const char *pszPropName = metaProperty.name(); QString sPropName(pszPropName); if (sPropName.compare("objectName") == 0) continue; QVariant value(pObject->property(pszPropName)); AddProperty(sPropName, value, pMetaObject, &metaProperty); } } m_pXmlWriter->writeEndElement(); }
void ControlInfo::setControl(QWidget *activex) { listInfo->clear(); const QMetaObject *mo = activex->metaObject(); QTreeWidgetItem *group = new QTreeWidgetItem(listInfo); group->setText(0, tr("Class Info")); group->setText(1, QString::number(mo->classInfoCount())); QTreeWidgetItem *item = 0; int i; int count; for (i = mo->classInfoOffset(); i < mo->classInfoCount(); ++i) { const QMetaClassInfo info = mo->classInfo(i); item = new QTreeWidgetItem(group); item->setText(0, QString::fromLatin1(info.name())); item->setText(1, QString::fromLatin1(info.value())); } group = new QTreeWidgetItem(listInfo); group->setText(0, tr("Signals")); count = 0; for (i = mo->methodOffset(); i < mo->methodCount(); ++i) { const QMetaMethod method = mo->method(i); if (method.methodType() == QMetaMethod::Signal) { ++count; item = new QTreeWidgetItem(group); item->setText(0, QString::fromLatin1(method.signature())); } } group->setText(1, QString::number(count)); group = new QTreeWidgetItem(listInfo); group->setText(0, tr("Slots")); count = 0; for (i = mo->methodOffset(); i < mo->methodCount(); ++i) { const QMetaMethod method = mo->method(i); if (method.methodType() == QMetaMethod::Slot) { ++count; item = new QTreeWidgetItem(group); item->setText(0, QString::fromLatin1(method.signature())); } } group->setText(1, QString::number(count)); group = new QTreeWidgetItem(listInfo); group->setText(0, tr("Properties")); count = 0; for (i = mo->propertyOffset(); i < mo->propertyCount(); ++i) { ++count; const QMetaProperty property = mo->property(i); item = new QTreeWidgetItem(group); item->setText(0, QString::fromLatin1(property.name())); item->setText(1, QString::fromLatin1(property.typeName())); if (!property.isDesignable()) { item->setTextColor(0, Qt::gray); item->setTextColor(1, Qt::gray); } } group->setText(1, QString::number(count)); }
void ChangeProperties::updateProperties() { bool hasControl = activex && !activex->isNull(); tabWidget->setEnabled(hasControl); listProperties->clear(); listEditRequests->clear(); if (hasControl) { const QMetaObject *mo = activex->metaObject(); const int numprops = mo->propertyCount(); for (int i = mo->propertyOffset(); i < numprops; ++i) { const QMetaProperty property = mo->property(i); QTreeWidgetItem *item = new QTreeWidgetItem(listProperties); item->setText(0, QString::fromLatin1(property.name())); item->setText(1, QString::fromLatin1(property.typeName())); if (!property.isDesignable()) { item->setTextColor(0, Qt::gray); item->setTextColor(1, Qt::gray); item->setTextColor(2, Qt::gray); } QVariant var = activex->property(property.name()); switch (var.type()) { case QVariant::Color: { QColor col = qvariant_cast<QColor>(var); item->setText(2, col.name()); } break; case QVariant::Font: { QFont fnt = qvariant_cast<QFont>(var); item->setText(2, fnt.toString()); } break; case QVariant::Bool: { item->setText(2, var.toBool() ? QLatin1String("true") : QLatin1String("false")); } break; case QVariant::Pixmap: { QPixmap pm = qvariant_cast<QPixmap>(var); item->setIcon(2, pm); } break; case QVariant::List: { QList<QVariant> varList = var.toList(); QStringList strList; for (int i = 0; i < varList.count(); ++i) { QVariant var = varList.at(i); strList << var.toString(); } item->setText(2, strList.join(QLatin1String(", "))); } break; case QVariant::Int: if (property.isEnumType()) { const QMetaEnum enumerator = mo->enumerator(mo->indexOfEnumerator(property.typeName())); item->setText(2, QString::fromLatin1(enumerator.valueToKey(var.toInt()))); break; } //FALLTHROUGH default: item->setText(2, var.toString()); break; } bool requesting = false; #if 0 { void *argv[] = { &requesting }; activex->qt_metacall(QMetaObject::Call(0x10000000) /*RequestingEdit*/, i, argv); } #endif if (requesting) { QTreeWidgetItem *check = new QTreeWidgetItem(listEditRequests); check->setText(0, QString::fromLatin1(property.name())); check->setCheckState(0, activex->propertyWritable(property.name()) ? Qt::Checked : Qt::Unchecked); } } listProperties->setCurrentItem(listProperties->topLevelItem(0)); } else { editValue->clear(); } }
void ObjectControllerPrivate::addClassProperties(const QMetaObject *metaObject) { if (!metaObject) return; addClassProperties(metaObject->superClass()); QtProperty *classProperty = m_classToProperty.value(metaObject); if (!classProperty) { QString className = QLatin1String(metaObject->className()); classProperty = m_manager->addProperty(QtVariantPropertyManager::groupTypeId(), className); m_classToProperty[metaObject] = classProperty; m_propertyToClass[classProperty] = metaObject; for (int idx = metaObject->propertyOffset(); idx < metaObject->propertyCount(); idx++) { QMetaProperty metaProperty = metaObject->property(idx); int type = metaProperty.userType(); QtVariantProperty *subProperty = 0; if (!metaProperty.isReadable()) { subProperty = m_readOnlyManager->addProperty(QVariant::String, QLatin1String(metaProperty.name())); subProperty->setValue(QLatin1String("< Non Readable >")); } else if (metaProperty.isEnumType()) { if (metaProperty.isFlagType()) { subProperty = m_manager->addProperty(QtVariantPropertyManager::flagTypeId(), QLatin1String(metaProperty.name())); QMetaEnum metaEnum = metaProperty.enumerator(); QMap<int, bool> valueMap; QStringList flagNames; for (int i = 0; i < metaEnum.keyCount(); i++) { int value = metaEnum.value(i); if (!valueMap.contains(value) && isPowerOf2(value)) { valueMap[value] = true; flagNames.append(QLatin1String(metaEnum.key(i))); } subProperty->setAttribute(QLatin1String("flagNames"), flagNames); subProperty->setValue(flagToInt(metaEnum, metaProperty.read(m_object).toInt())); } } else { subProperty = m_manager->addProperty(QtVariantPropertyManager::enumTypeId(), QLatin1String(metaProperty.name())); QMetaEnum metaEnum = metaProperty.enumerator(); QMap<int, bool> valueMap; // dont show multiple enum values which have the same values QStringList enumNames; for (int i = 0; i < metaEnum.keyCount(); i++) { int value = metaEnum.value(i); if (!valueMap.contains(value)) { valueMap[value] = true; enumNames.append(QLatin1String(metaEnum.key(i))); } } subProperty->setAttribute(QLatin1String("enumNames"), enumNames); subProperty->setValue(enumToInt(metaEnum, metaProperty.read(m_object).toInt())); } } else if (m_manager->isPropertyTypeSupported(type)) { if (!metaProperty.isWritable()) subProperty = m_readOnlyManager->addProperty(type, QLatin1String(metaProperty.name()) + QLatin1String(" (Non Writable)")); if (!metaProperty.isDesignable()) subProperty = m_readOnlyManager->addProperty(type, QLatin1String(metaProperty.name()) + QLatin1String(" (Non Designable)")); else subProperty = m_manager->addProperty(type, QLatin1String(metaProperty.name())); subProperty->setValue(metaProperty.read(m_object)); } else { subProperty = m_readOnlyManager->addProperty(QVariant::String, QLatin1String(metaProperty.name())); subProperty->setValue(QLatin1String("< Unknown Type >")); subProperty->setEnabled(false); } classProperty->addSubProperty(subProperty); m_propertyToIndex[subProperty] = idx; m_classToIndexToProperty[metaObject][idx] = subProperty; } } else { updateClassProperties(metaObject, false); } m_topLevelProperties.append(classProperty); m_browser->addProperty(classProperty); }
bool Xsd::RenderXSD( HTTPRequest *pRequest, QObject *pClass ) { const QMetaObject *pMetaObject = pClass->metaObject(); QString sClassName = ConvertTypeToXSD( pMetaObject->className(), true); QDomElement oRoot = CreateSchemaRoot(); QMap<QString, TypeInfo> typesToInclude; // ------------------------------------------------------------------ // Create xs:complexType structure // ------------------------------------------------------------------ QDomElement oTypeNode = createElement( "xs:complexType" ); QDomElement oSeqNode = createElement( "xs:sequence" ); oTypeNode.setAttribute( "name", sClassName ); oTypeNode.appendChild( oSeqNode ); // -=>TODO: Add an xs:annotation node with class descriptions // ------------------------------------------------------------------ // Add all properties for this type // // <xs:element minOccurs="0" name="<propName>" type="<propType>"/> // <xs:element minOccurs="0" name="<childName>" nillable="true" // type="tns:<ChildType>"/> // ------------------------------------------------------------------ int nCount = pMetaObject->propertyCount(); for (int nIdx=0; nIdx < nCount; ++nIdx ) { QMetaProperty metaProperty = pMetaObject->property( nIdx ); if (metaProperty.isDesignable( pClass )) { const char *pszPropName = metaProperty.name(); QString sPropName( pszPropName ); if ( sPropName.compare( "objectName" ) == 0) continue; // ---------------------------------------------------------- // Create xs:element for this property // ---------------------------------------------------------- QDomElement oNode = createElement( "xs:element" ); QString sType = metaProperty.typeName(); bool bCustomType = false; QString sCustomAttr = "type"; QString sContentName = QString(); QString sContentType = QString(); // if this is a child object, sType will be QObject* // which we can't use, so we need to read the // properties value, and read it's metaObject data if (sType == "QObject*") { QVariant val = metaProperty.read( pClass ); const QObject *pObject = val.value< QObject* >(); sType = pObject->metaObject()->className(); bCustomType = true; } else if ((sType == "QVariantList" ) || (sType == "QVariantMap")) { sContentType = ReadPropertyMetadata( pClass, sPropName, "type" ); if (sContentType.at(0) == 'Q') sContentType = sContentType.mid( 1 ); sContentType.remove( "DTC::" ); sContentType.remove( QChar('*') ); if (sType == "QVariantMap") { sContentName = ReadPropertyMetadata( pClass, sPropName, "name" ); if (sContentName.isEmpty()) sContentName = sContentType; sType = "MapOfString" + sContentName; } else sType = "ArrayOf" + sContentType; bCustomType = true; } else if (sType == "QStringList") { sType = "ArrayOfString"; bCustomType = true; } else if (metaProperty.isEnumType() || metaProperty.isFlagType() ) { sCustomAttr = "enum"; sType = sClassName + "." + sType; bCustomType = true; } QString sNewPropName( metaProperty.name() ); if (IsNillable( sType )) oNode.setAttribute( "nillable" , true ); if (bCustomType) { TypeInfo info = { sCustomAttr, sContentType }; typesToInclude.insert( sType, info ); } oNode.setAttribute( "type" , (bCustomType ? "tns:" : "xs:") + ConvertTypeToXSD( sType, bCustomType )); oNode.setAttribute( "name" , sNewPropName ); oNode.setAttribute( "minOccurs", 0 ); oSeqNode.appendChild( oNode ); } } // ------------------------------------------------------------------ // Create element for class // // <xs:element name="<className>" nillable="true" type="tns:<className>"/> // ------------------------------------------------------------------ QDomElement oElementNode = createElement( "xs:element" ); oElementNode.setAttribute( "type" , "tns:" + sClassName ); oElementNode.setAttribute( "nillable", "true" ); oElementNode.setAttribute( "name" , sClassName ); // ---------------------------------------------------------------------- // Build xml tree... // ---------------------------------------------------------------------- appendChild( oRoot ); if (typesToInclude.count() > 0) { // ------------------------------------------------------------------ // Create all needed includes // // <xs:include schemaLocation="<path to dependant schema"/> // ------------------------------------------------------------------ QString sBaseUri = "http://" + pRequest->m_mapHeaders[ "host" ] + pRequest->m_sResourceUrl; QMap<QString, TypeInfo >::const_iterator it = typesToInclude.constBegin(); while( it != typesToInclude.constEnd()) { QDomElement oIncNode = createElement( "xs:include" ); QString sType = it.key(); sType.remove( "DTC::" ); TypeInfo info = it.value(); QString sValue = QString( "%1?%2=%3" ).arg( sBaseUri ) .arg( info.sAttrName ) .arg( sType ); if (!info.sContentType.isEmpty()) sValue += "&name=" + info.sContentType; oIncNode.setAttribute( "schemaLocation", sValue ); oRoot.appendChild( oIncNode ); ++it; } } oRoot.appendChild( oTypeNode ); oRoot.appendChild( oElementNode ); // ---------------------------------------------------------------------- // Return xsd doc to caller // ---------------------------------------------------------------------- QTextStream os( &(pRequest->m_response) ); pRequest->m_eResponseType = ResponseTypeXML; save( os, 0 ); return true; }
bool Wsdl::CreateType( QObject *pParent, QString &sTypeName ) { if ( m_typesCreated.contains( sTypeName )) return true; // sTypeName.remove( "DTC::" ); sTypeName.remove( QChar('*') ); int id = QMetaType::type( sTypeName.toUtf8() ); switch( id ) { case QMetaType::QStringList: return CreateArrayType( pParent, sTypeName, "Value", "string", true ); case QMetaType::QVariantMap: return CreateMapType( pParent, sTypeName, "Settings", "string", true ); default: // for not, treat QFileInfo as a string. Need to turn into MTOM later. if (id == QMetaType::type( "QFileInfo" )) { sTypeName = "QString"; return false; } break; } if ((id == -1) || (id < QMetaType::User)) return false; // ------------------------------------------------------------------ // Need to create an instance of the class to access it's metadata. // ------------------------------------------------------------------ QObject *pClass = (QObject *)QMetaType::construct( id ); if (pClass != NULL) { const QMetaObject *pMetaObject = pClass->metaObject(); // ------------------------------------------------------------------ // Create xsd element structure // ------------------------------------------------------------------ QDomElement oTypeNode = createElement( "xs:complexType" ); QDomElement oSeqNode = createElement( "xs:sequence" ); oTypeNode.setAttribute( "name", ConvertTypeToXSD( sTypeName, true)); // oElementNode.appendChild( oTypeNode ); oTypeNode .appendChild( oSeqNode ); // ------------------------------------------------------------------ // Add all properties for this type // ------------------------------------------------------------------ int nCount = pMetaObject->propertyCount(); for (int nIdx=0; nIdx < nCount; ++nIdx ) { QMetaProperty metaProperty = pMetaObject->property( nIdx ); if (metaProperty.isDesignable( pClass )) { const char *pszPropName = metaProperty.name(); QString sPropName( pszPropName ); if ( sPropName.compare( "objectName" ) == 0) continue; QDomElement oNode = createElement( "xs:element" ); QString sType = metaProperty.typeName(); // if this is a child object, sType will be QObject* // which we can't use, so we need to read the // properties value, and read it's metaObject data if (sType == "QObject*") { QVariant val = metaProperty.read( pClass ); const QObject *pObject = val.value< QObject* >(); sType = pObject->metaObject()->className(); // sType.remove( "DTC::" ); } oNode.setAttribute( "minOccurs", 0 ); oNode.setAttribute( "name" , metaProperty.name() ); oNode.setAttribute( "nillable" , true ); //-=>TODO: This may need to be determined by sParamType bool bCustomType = CreateType( pClass, sType ); oNode.setAttribute( "type" , ((bCustomType) ? "tns:" : "") + ConvertTypeToXSD( sType, bCustomType )); oSeqNode.appendChild( oNode ); } } QDomElement oElementNode = createElement( "xs:element" ); oElementNode.setAttribute( "name" , ConvertTypeToXSD( sTypeName, true)); oElementNode.setAttribute( "nillable", "true" ); oElementNode.setAttribute( "type" , "tns:" + ConvertTypeToXSD( sTypeName, true)); m_oTypes.appendChild( oTypeNode ); m_oTypes.appendChild( oElementNode ); QMetaType::destroy( id, pClass ); } m_typesCreated.insert( sTypeName, true ); return true; }
void ObjectControllerPrivate::addClassProperties(const QMetaObject *inmetaObject, bool subGroup) { if (!inmetaObject) return; // Collect a list of all sub classes in the object QList< const QMetaObject *> metaObjectsList; metaObjectsList.clear(); metaObjectsList << inmetaObject; const QMetaObject *tmpObj = inmetaObject->superClass(); metaObjectsList << tmpObj; while (tmpObj) { tmpObj = tmpObj->superClass(); if (tmpObj) metaObjectsList << tmpObj; } const QMetaObject *metaObject; for (int i = 0; i < metaObjectsList.count(); i++) { metaObject = metaObjectsList[i]; QtProperty *classProperty = m_classToProperty.value(metaObject); if (!classProperty) { QString className = QLatin1String(metaObject->className()); // Note: Skip class QObject from the property views if (className == QLatin1String("QObject")) return; // Process Class name into a user friendly view // Strip prefix C_ and process all _ to spaces QString prefix("C_"); // String to replace. QString replaceprefix(""); // Replacement string. className.replace(className.indexOf(prefix), prefix.size(), replaceprefix); className.replace(QString("_"), QString(" ")); classProperty = m_manager->addProperty(QtVariantPropertyManager::groupTypeId(), className); m_classToProperty[metaObject] = classProperty; m_propertyToClass[classProperty] = metaObject; for (int idx = metaObject->propertyOffset(); idx < metaObject->propertyCount(); idx++) { QMetaProperty metaProperty = metaObject->property(idx); int type = metaProperty.userType(); QtVariantProperty *subProperty = 0; // Note: Get the var member name and check if we want it to be writable (Enabled) QString memberVarName = QLatin1String(metaProperty.name()); bool b_SetEnabled = true; // Special case for enabling or disabling editing QString ememberVarName = "e" + QLatin1String(metaProperty.name()); QByteArray array = ememberVarName.toLocal8Bit(); char* buffer = array.data(); QVariant set = m_object->property(buffer); if (set.type() == QVariant::Bool) { b_SetEnabled = (bool &)set; } // qDebug() << "Member Name :" << memberVarName; // Note: process the first char if it contains _ then the var is read only and remove the _ char if (memberVarName.at(0) == "_") { b_SetEnabled = false; memberVarName.remove(0, 1); } // after that replace all occurance of _ with space char for display memberVarName.replace(QString("_"), QString(" ")); if (!metaProperty.isReadable()) { subProperty = m_readOnlyManager->addProperty(QVariant::String, memberVarName); subProperty->setValue(QLatin1String("< Non Readable >")); } else if (metaProperty.isEnumType()) { if (metaProperty.isFlagType()) { subProperty = m_manager->addProperty(QtVariantPropertyManager::flagTypeId(), memberVarName); QMetaEnum metaEnum = metaProperty.enumerator(); QMap<int, bool> valueMap; QStringList flagNames; for (int i = 0; i < metaEnum.keyCount(); i++) { int value = metaEnum.value(i); if (!valueMap.contains(value) && isPowerOf2(value)) { valueMap[value] = true; flagNames.append(QLatin1String(metaEnum.key(i))); } subProperty->setAttribute(QLatin1String("flagNames"), flagNames); subProperty->setValue(flagToInt(metaEnum, metaProperty.read(m_object).toInt())); } } else { subProperty = m_manager->addProperty(QtVariantPropertyManager::enumTypeId(), memberVarName); QMetaEnum metaEnum = metaProperty.enumerator(); QMap<int, bool> valueMap; // dont show multiple enum values which have the same values QStringList enumNames; for (int i = 0; i < metaEnum.keyCount(); i++) { int value = metaEnum.value(i); if (!valueMap.contains(value)) { valueMap[value] = true; enumNames.append(QLatin1String(metaEnum.key(i))); } } subProperty->setAttribute(QLatin1String("enumNames"), enumNames); subProperty->setValue(enumToInt(metaEnum, metaProperty.read(m_object).toInt())); } } else if (m_manager->isPropertyTypeSupported(type)) { if (!metaProperty.isWritable()) subProperty = m_readOnlyManager->addProperty(type, memberVarName + QLatin1String(" (Non Writable)")); if (!metaProperty.isDesignable()) subProperty = m_readOnlyManager->addProperty(type, memberVarName + QLatin1String(" (Non Designable)")); else subProperty = m_manager->addProperty(type, memberVarName); subProperty->setValue(metaProperty.read(m_object)); } else { subProperty = m_readOnlyManager->addProperty(QVariant::String, memberVarName); subProperty->setValue(QLatin1String("< Unknown Type >")); b_SetEnabled = false; } // Notes: QtVariantProperty *priority = variantManager->addProperty(QVariant::Int, "Priority"); if (subProperty) subProperty->setEnabled(b_SetEnabled); m_propertyToIndex[subProperty] = idx; m_classToIndexToProperty[metaObject][idx] = subProperty; if (subGroup) classProperty->addSubProperty(subProperty); else m_browser->addProperty(subProperty); } } else { updateClassProperties(metaObject, false); } m_topLevelProperties.append(classProperty); if (subGroup) m_browser->addProperty(classProperty); } // Loop i }
void tst_QQmlMetaObject::property() { QFETCH(QString, testFile); QFETCH(QByteArray, cppTypeName); QFETCH(int, cppType); QFETCH(bool, isDefault); QFETCH(QVariant, expectedValue); QFETCH(bool, isWritable); QFETCH(QVariant, newValue); QQmlEngine engine; QQmlComponent component(&engine, testFileUrl(testFile)); QObject *object = component.create(); QVERIFY(object != 0); const QMetaObject *mo = object->metaObject(); QVERIFY(mo->superClass() != 0); QVERIFY(QByteArray(mo->className()).contains("_QML_")); QCOMPARE(mo->propertyOffset(), mo->superClass()->propertyCount()); QCOMPARE(mo->propertyCount(), mo->superClass()->propertyCount() + 1); QMetaProperty prop = mo->property(mo->propertyOffset()); QCOMPARE(prop.name(), "test"); QCOMPARE(QByteArray(prop.typeName()), cppTypeName); if (prop.userType() < QMetaType::User) QCOMPARE(prop.type(), QVariant::Type(cppType)); else QCOMPARE(prop.type(), QVariant::UserType); QCOMPARE(prop.userType(), cppType); QVERIFY(!prop.isConstant()); QVERIFY(!prop.isDesignable()); QVERIFY(!prop.isEnumType()); QVERIFY(!prop.isFinal()); QVERIFY(!prop.isFlagType()); QVERIFY(prop.isReadable()); QVERIFY(!prop.isResettable()); QVERIFY(prop.isScriptable()); QVERIFY(!prop.isStored()); QVERIFY(!prop.isUser()); QVERIFY(prop.isValid()); QCOMPARE(prop.isWritable(), isWritable); QCOMPARE(mo->classInfoOffset(), mo->superClass()->classInfoCount()); QCOMPARE(mo->classInfoCount(), mo->superClass()->classInfoCount() + (isDefault ? 1 : 0)); if (isDefault) { QMetaClassInfo info = mo->classInfo(mo->classInfoOffset()); QCOMPARE(info.name(), "DefaultProperty"); QCOMPARE(info.value(), "test"); } QCOMPARE(mo->methodOffset(), mo->superClass()->methodCount()); QCOMPARE(mo->methodCount(), mo->superClass()->methodCount() + 1); // the signal QVERIFY(prop.notifySignalIndex() != -1); QMetaMethod signal = prop.notifySignal(); QCOMPARE(signal.methodType(), QMetaMethod::Signal); QCOMPARE(signal.name(), QByteArray("testChanged")); QCOMPARE(signal.methodSignature(), QByteArray("testChanged()")); QCOMPARE(signal.access(), QMetaMethod::Public); QCOMPARE(signal.parameterCount(), 0); QCOMPARE(signal.parameterTypes(), QList<QByteArray>()); QCOMPARE(signal.parameterNames(), QList<QByteArray>()); QCOMPARE(signal.tag(), ""); QCOMPARE(signal.typeName(), "void"); QCOMPARE(signal.returnType(), int(QMetaType::Void)); QSignalSpy changedSpy(object, SIGNAL(testChanged())); QObject::connect(object, SIGNAL(testChanged()), object, SLOT(deleteLater())); if (expectedValue.isValid()) QCOMPARE(prop.read(object), expectedValue); else QVERIFY(prop.read(object).isValid()); QCOMPARE(changedSpy.count(), 0); if (isWritable) { QVERIFY(prop.write(object, newValue)); QCOMPARE(changedSpy.count(), 1); QCOMPARE(prop.read(object), newValue); } else { QVERIFY(!prop.write(object, prop.read(object))); QCOMPARE(changedSpy.count(), 0); } delete object; }
static HRESULT classIDL(QObject *o, const QMetaObject *mo, const QString &className, bool isBindable, QTextStream &out) { int id = 1; int i = 0; if (!mo) return 3; QString topclass = qAxFactory()->exposeToSuperClass(className); if (topclass.isEmpty()) topclass = QLatin1String("QObject"); bool hasStockEvents = qAxFactory()->hasStockEvents(className); const QMetaObject *pmo = mo; do { pmo = pmo->superClass(); } while (pmo && topclass != QString::fromLatin1(pmo->className())); int enumoff = pmo ? pmo->enumeratorOffset() : mo->enumeratorOffset(); int methodoff = pmo ? pmo->methodOffset() : mo->methodOffset(); int propoff = pmo ? pmo->propertyOffset() : mo->propertyOffset(); int qtProps = 0; int qtSlots = 0; bool control = false; if (o && o->isWidgetType()) { qtProps = QWidget::staticMetaObject.propertyCount(); qtSlots = QWidget::staticMetaObject.methodCount(); control = true; } const QString classID = stripCurlyBraces(qAxFactory()->classID(className)); if (classID.isEmpty()) return 4; const QString interfaceID = stripCurlyBraces(qAxFactory()->interfaceID(className)); if (interfaceID.isEmpty()) return 5; const QString eventsID = stripCurlyBraces(qAxFactory()->eventsID(className)); const bool hasEvents = !eventsID.isEmpty(); QString cleanClassName = qax_clean_type(className, mo); QString defProp(QLatin1String(mo->classInfo(mo->indexOfClassInfo("DefaultProperty")).value())); QString defSignal(QLatin1String(mo->classInfo(mo->indexOfClassInfo("DefaultSignal")).value())); for (i = enumoff; i < mo->enumeratorCount(); ++i) { const QMetaEnum enumerator = mo->enumerator(i); if (enums.contains(enumerator.name())) continue; enums.append(enumerator.name()); out << "\tenum " << enumerator.name() << " {" << endl; for (int j = 0; j < enumerator.keyCount(); ++j) { QByteArray key(enumerator.key(j)); while (enumValues.contains(key)) { key += '_'; } enumValues.append(key); const uint value = uint(enumerator.value(j)); key = key.leftJustified(20); out << "\t\t" << key << "\t= "; if (enumerator.isFlag()) out << "0x" << QByteArray::number(value, 16).rightJustified(8, '0'); else out << value; if (j < enumerator.keyCount()-1) out << ", "; out << endl; } out << "\t};" << endl << endl; } // mouse cursor enum for QCursor support if (!enums.contains("MousePointer")) { enums.append("MousePointer"); out << "\tenum MousePointer {" << endl; out << "\t\tArrowCursor = " << Qt::ArrowCursor << ',' << endl; out << "\t\tUpArrowCursor = " << Qt::UpArrowCursor << ',' << endl; out << "\t\tCrossCursor = " << Qt::CrossCursor << ',' << endl; out << "\t\tWaitCursor = " << Qt::WaitCursor << ',' << endl; out << "\t\tIBeamCursor = " << Qt::IBeamCursor << ',' << endl; out << "\t\tSizeVerCursor = " << Qt::SizeVerCursor << ',' << endl; out << "\t\tSizeHorCursor = " << Qt::SizeHorCursor << ',' << endl; out << "\t\tSizeBDiagCursor = " << Qt::SizeBDiagCursor << ',' << endl; out << "\t\tSizeFDiagCursor = " << Qt::SizeFDiagCursor << ',' << endl; out << "\t\tSizeAllCursor = " << Qt::SizeAllCursor << ',' << endl; out << "\t\tBlankCursor = " << Qt::BlankCursor << ',' << endl; out << "\t\tSplitVCursor = " << Qt::SplitVCursor << ',' << endl; out << "\t\tSplitHCursor = " << Qt::SplitHCursor << ',' << endl; out << "\t\tPointingHandCursor = " << Qt::PointingHandCursor << ',' << endl; out << "\t\tForbiddenCursor = " << Qt::ForbiddenCursor << ',' << endl; out << "\t\tWhatsThisCursor = " << Qt::WhatsThisCursor << ',' << endl; out << "\t\tBusyCursor\t= " << Qt::BusyCursor << endl; out << "\t};" << endl << endl; } if (!enums.contains("FocusPolicy")) { enums.append("FocusPolicy"); out << "\tenum FocusPolicy {" << endl; out << "\t\tNoFocus = " << Qt::NoFocus << ',' << endl; out << "\t\tTabFocus = " << Qt::TabFocus << ',' << endl; out << "\t\tClickFocus = " << Qt::ClickFocus << ',' << endl; out << "\t\tStrongFocus = " << Qt::StrongFocus << ',' << endl; out << "\t\tWheelFocus = " << Qt::WheelFocus << endl; out << "\t};" << endl << endl; } out << endl; out << "\t[" << endl; out << "\t\tuuid(" << interfaceID << ")," << endl; out << "\t\thelpstring(\"" << cleanClassName << " Interface\")" << endl; out << "\t]" << endl; out << "\tdispinterface I" << cleanClassName << endl; out << "\t{" << endl; out << "\tproperties:" << endl; for (i = propoff; i < mo->propertyCount(); ++i) { const QMetaProperty property = mo->property(i); /* if (property.testFlags(QMetaProperty::Override)) continue;*/ if (i <= qtProps && ignoreProps(property.name())) continue; if (!property.name() || mo->indexOfProperty(property.name()) > i) continue; bool ok = true; QByteArray type(convertTypes(property.typeName(), &ok)); QByteArray name(replaceKeyword(property.name())); if (!ok) out << "\t/****** Property is of unsupported datatype" << endl; out << "\t\t[id(" << id << ')'; if (!property.isWritable()) out << ", readonly"; if (isBindable && property.isScriptable(o)) out << ", bindable"; if (!property.isDesignable(o)) out << ", nonbrowsable"; if (isBindable) out << ", requestedit"; if (defProp == QLatin1String(name)) out << ", uidefault"; out << "] " << type << ' ' << name << ';' << endl; if (!ok) out << "\t******/" << endl; ++id; } out << endl; out << "\tmethods:" << endl; int numDefArgs = 0; QByteArray outBuffer; for (i = methodoff; i < mo->methodCount(); ++i) { const QMetaMethod slot = mo->method(i); if (slot.access() != QMetaMethod::Public || slot.methodType() == QMetaMethod::Signal) continue; if (slot.attributes() & QMetaMethod::Cloned) { ++numDefArgs; continue; } if (!outBuffer.isEmpty()) { outBuffer = addDefaultArguments(outBuffer, numDefArgs); numDefArgs = 0; out << outBuffer; outBuffer = QByteArray(); } QByteArray signature(slot.methodSignature()); QByteArray name(signature.left(signature.indexOf('('))); if (i <= qtSlots && ignoreSlots(name)) continue; signature.remove(0, name.length() + 1); signature.truncate(signature.length() - 1); name = renameOverloads(replaceKeyword(name)); if (ignoreSlots(name)) continue; QList<QByteArray> parameterTypes(slot.parameterTypes()); QList<QByteArray> parameterNames(slot.parameterNames()); bool ok = true; QByteArray type = slot.typeName(); if (type.isEmpty()) type = "void"; else type = convertTypes(type, &ok); QByteArray ptype(prototype(parameterTypes, parameterNames, &ok)); if (!ok) outBuffer += "\t/****** Slot parameter uses unsupported datatype\n"; outBuffer += "\t\t[id(" + QByteArray::number(id) + ")] " + type + ' ' + name + '(' + ptype + ");\n"; if (!ok) outBuffer += "\t******/\n"; ++id; } if (!outBuffer.isEmpty()) { outBuffer = addDefaultArguments(outBuffer, numDefArgs); numDefArgs = 0; out << outBuffer; outBuffer = QByteArray(); } out << "\t};" << endl << endl; mapping.clear(); id = 1; if (hasEvents) { out << "\t[" << endl; out << "\t\tuuid(" << eventsID << ")," << endl; out << "\t\thelpstring(\"" << cleanClassName << " Events Interface\")" << endl; out << "\t]" << endl; out << "\tdispinterface I" << cleanClassName << "Events" << endl; out << "\t{" << endl; out << "\tproperties:" << endl; out << "\tmethods:" << endl; if (hasStockEvents) { out << "\t/****** Stock events ******/" << endl; out << "\t\t[id(DISPID_CLICK)] void Click();" << endl; out << "\t\t[id(DISPID_DBLCLICK)] void DblClick();" << endl; out << "\t\t[id(DISPID_KEYDOWN)] void KeyDown(short* KeyCode, short Shift);" << endl; out << "\t\t[id(DISPID_KEYPRESS)] void KeyPress(short* KeyAscii);" << endl; out << "\t\t[id(DISPID_KEYUP)] void KeyUp(short* KeyCode, short Shift);" << endl; out << "\t\t[id(DISPID_MOUSEDOWN)] void MouseDown(short Button, short Shift, OLE_XPOS_PIXELS x, OLE_YPOS_PIXELS y);" << endl; out << "\t\t[id(DISPID_MOUSEMOVE)] void MouseMove(short Button, short Shift, OLE_XPOS_PIXELS x, OLE_YPOS_PIXELS y);" << endl; out << "\t\t[id(DISPID_MOUSEUP)] void MouseUp(short Button, short Shift, OLE_XPOS_PIXELS x, OLE_YPOS_PIXELS y);" << endl << endl; } for (i = methodoff; i < mo->methodCount(); ++i) { const QMetaMethod signal = mo->method(i); if (signal.methodType() != QMetaMethod::Signal) continue; QByteArray signature(signal.methodSignature()); QByteArray name(signature.left(signature.indexOf('('))); signature.remove(0, name.length() + 1); signature.truncate(signature.length() - 1); QList<QByteArray> parameterTypes(signal.parameterTypes()); QList<QByteArray> parameterNames(signal.parameterNames()); bool isDefault = defSignal == QLatin1String(name); name = renameOverloads(replaceKeyword(name)); bool ok = true; QByteArray type = signal.typeName(); if (!type.isEmpty() && type != "void") // signals with return value not supported continue; QByteArray ptype(prototype(parameterTypes, parameterNames, &ok)); if (!ok) out << "\t/****** Signal parameter uses unsupported datatype" << endl; out << "\t\t[id(" << id << ')'; if (isDefault) out << ", uidefault"; out << "] void " << name << '(' << ptype << ");" << endl; if (!ok) out << "\t******/" << endl; ++id; } out << "\t};" << endl << endl; } out << "\t[" << endl; if (qstricmp(mo->classInfo(mo->indexOfClassInfo("Aggregatable")).value(), "no")) out << "\t\taggregatable," << endl; if (!qstricmp(mo->classInfo(mo->indexOfClassInfo("RegisterObject")).value(), "yes")) out << "\t\tappobject," << endl; if (mo->classInfo(mo->indexOfClassInfo("LicenseKey")).value()) out << "\t\tlicensed," << endl; const char *helpString = mo->classInfo(mo->indexOfClassInfo("Description")).value(); if (helpString) out << "\t\thelpstring(\"" << helpString << "\")," << endl; else out << "\t\thelpstring(\"" << cleanClassName << " Class\")," << endl; const char *classVersion = mo->classInfo(mo->indexOfClassInfo("Version")).value(); if (classVersion) out << "\t\tversion(" << classVersion << ")," << endl; out << "\t\tuuid(" << classID << ')'; if (control) { out << ", " << endl; out << "\t\tcontrol"; } else if (!o) { out << ", " << endl; out << "\t\tnoncreatable"; } out << endl; out << "\t]" << endl; out << "\tcoclass " << cleanClassName << endl; out << "\t{" << endl; out << "\t\t[default] dispinterface I" << cleanClassName << ';' << endl; if (hasEvents) out << "\t\t[default, source] dispinterface I" << cleanClassName << "Events;" << endl; out << "\t};" << endl; return S_OK; }