From 487415a313a6d705a6f681c52213d5d34c07bf9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20M=C3=B6bius?= Date: Wed, 10 Feb 2010 11:50:43 +0000 Subject: [PATCH] Reimplement DataType to support Scripting calls with DataType git-svn-id: http://www.openflipper.org/svnrepo/OpenFlipper/branches/Free@8513 383ad7c9-94d9-4d36-a494-682f7c89f535 --- Core/Core.cc | 49 ++++---- Core/openFunctions.cc | 8 +- .../scriptPrototypes/prototypeDataType.cc | 4 - Scripting/scriptWrappers/DataTypeWrapper.cc | 13 +-- common/DataTypes.hh | 57 ++++++++- common/Types.cc | 110 ++++++++++++++++-- widgets/addEmptyWidget/addEmptyWidget.cc | 2 +- widgets/coreWidget/About.cc | 2 +- 8 files changed, 186 insertions(+), 59 deletions(-) diff --git a/Core/Core.cc b/Core/Core.cc index 2f4c8c66..78c7b51f 100644 --- a/Core/Core.cc +++ b/Core/Core.cc @@ -413,41 +413,43 @@ Core::init() { printFunction.setProperty("textedit",scriptEngine_.newQObject(this)); scriptEngine_.globalObject().setProperty("print", printFunction); - // Register Vector Type to ScriptEngine ( is Vec3d ) + + + // Register IdList Type to scripting Engine + qScriptRegisterSequenceMetaType< IdList >(&scriptEngine_); + + // Register Vector of ints Type to scripting Engine + qScriptRegisterSequenceMetaType< QVector< int > >(&scriptEngine_); + + //========================================================================== + // Register the 3d Vector Type to the core ( is Vec3d ) + //========================================================================== qScriptRegisterMetaType(&scriptEngine_, toScriptValueVector, fromScriptValueVector, scriptEngine_.newQObject(&vec3dPrototype_)); - + // set a constructor to allow creation via Vector(x,y,z) QScriptValue ctor = scriptEngine_.newFunction(createVector); scriptEngine_.globalObject().setProperty("Vector", ctor); - - -// // Register ObjectId Type to ScriptEngine ( is int ) -// qScriptRegisterMetaType(&scriptEngine_, -// toScriptValueObjectId, -// fromScriptValueObjectId); -// -// // set a constructor to allow creation via Vector(x,y,z) -// ctor = scriptEngine_.newFunction(createObjectId); -// scriptEngine_.globalObject().setProperty("ObjectId", ctor); - - - // Register IdList Type to scripting Engine - qScriptRegisterSequenceMetaType< IdList >(&scriptEngine_); - - qScriptRegisterSequenceMetaType< QVector< int > >(&scriptEngine_); + + //========================================================================== + // Register the DataType Class to the core + //========================================================================== // Register DataType in QScriptEngine - qScriptRegisterMetaType(&scriptEngine_, + qScriptRegisterMetaType(&scriptEngine_, toScriptValueDataType, fromScriptValueDataType, scriptEngine_.newQObject(&DataTypePrototype_)); // set a constructor to allow creation via DataType(uint) - QScriptValue dataType = scriptEngine_.newFunction(createDataType); - scriptEngine_.globalObject().setProperty("DataType", dataType); + QScriptValue dataTypector = scriptEngine_.newFunction(createDataType); + scriptEngine_.globalObject().setProperty("DataType", dataTypector); + + //========================================================================== + // Register the Matrix Class to the core + //========================================================================== // Register Matrix Type to scripting Engine ( is ACG::Matrix4x4d ) qScriptRegisterMetaType(&scriptEngine_, @@ -459,7 +461,10 @@ Core::init() { QScriptValue matrix4x4ctor = scriptEngine_.newFunction(createMatrix4x4); scriptEngine_.globalObject().setProperty("Matrix4x4", matrix4x4ctor); - // Collect Core scripting information + //========================================================================== + // Collect Core scripting information + //========================================================================== + QScriptValue scriptInstance = scriptEngine_.newQObject(this, QScriptEngine::QtOwnership, QScriptEngine::ExcludeChildObjects | diff --git a/Core/openFunctions.cc b/Core/openFunctions.cc index 82338e78..6313a018 100644 --- a/Core/openFunctions.cc +++ b/Core/openFunctions.cc @@ -89,7 +89,7 @@ void Core::slotGetAllFilters ( QStringList& _list){ for (int i=0; i < (int)supportedTypes_.size(); i++){ QString f = supportedTypes_[i].plugin->getLoadFilters(); f = f.section(")",0,0).section("(",1,1).trimmed(); - _list << (QString::number(supportedTypes_[i].plugin->supportedType()) + " " + f); + _list << (QString::number(supportedTypes_[i].plugin->supportedType().value()) + " " + f); } } @@ -326,11 +326,11 @@ void Core::slotAddEmptyObject( DataType _type , int& _id ) { if ( OpenFlipper::Options::doSlotDebugging() ) { if ( sender() != 0 ) { if ( sender()->metaObject() != 0 ) { - emit log(LOGINFO,"slotAddEmptyObject( " + QString::number(_type) + "," + QString::number(_id) + tr(" ) called by ") + + emit log(LOGINFO,"slotAddEmptyObject( " + _type.name() + "," + QString::number(_id) + tr(" ) called by ") + QString( sender()->metaObject()->className() ) ); } } else { - emit log(LOGINFO,"slotAddEmptyObject( " + QString::number(_type) + "," + QString::number(_id) + tr(" ) called by Core") ); + emit log(LOGINFO,"slotAddEmptyObject( " + _type.name() + "," + QString::number(_id) + tr(" ) called by Core") ); } } } @@ -649,7 +649,7 @@ void Core::slotAddEmptyObjectMenu() { } // Advance to next type ( Indices are bits so multiply by to to get next bit) - currentType *= 2; + currentType++; } static addEmptyWidget* widget = 0; diff --git a/Scripting/scriptPrototypes/prototypeDataType.cc b/Scripting/scriptPrototypes/prototypeDataType.cc index 3004231c..56f69263 100644 --- a/Scripting/scriptPrototypes/prototypeDataType.cc +++ b/Scripting/scriptPrototypes/prototypeDataType.cc @@ -47,15 +47,11 @@ prototypeDataType::prototypeDataType(QObject *parent ) : QObject(parent) { - std::cerr << "Prototype cnstructor" << std::endl; } QString prototypeDataType::toString() const { DataType s = thisObject().property("type").toNumber(); - - std::cerr << "Prototype to String " << s << std::endl; - std::cerr << "String is : " << typeName(s).toStdString() << std::endl; return typeName(s); } diff --git a/Scripting/scriptWrappers/DataTypeWrapper.cc b/Scripting/scriptWrappers/DataTypeWrapper.cc index 95c2bcd4..6437736d 100644 --- a/Scripting/scriptWrappers/DataTypeWrapper.cc +++ b/Scripting/scriptWrappers/DataTypeWrapper.cc @@ -58,22 +58,17 @@ QScriptValue toScriptValueDataType(QScriptEngine *engine, const DataType &s) { QScriptValue obj = engine->newObject(); - obj.setProperty("type", QScriptValue(engine, s )); - std::cerr << "toScriptValueDataType " << s << std::endl; + obj.setProperty("type", QScriptValue(engine, s.value() )); return obj; } void fromScriptValueDataType(const QScriptValue &obj, DataType &s) { s = obj.property("type").toNumber(); - - std::cerr << "fromScriptValueDataType " << s << std::endl; } QScriptValue createDataType(QScriptContext *context, QScriptEngine *engine) { - std::cerr << "createDataType " << std::endl; - DataType s; QScriptValue callee = context->callee(); @@ -86,18 +81,12 @@ QScriptValue createDataType(QScriptContext *context, QScriptEngine *engine) s = DATA_UNKNOWN; } - - std::cerr << "s set to " << s << std::endl; - return engine->toScriptValue( s ); } QScriptValue DataTypeToString(QScriptContext *context, QScriptEngine *engine) { DataType s = context->thisObject().property("type").toNumber(); - - std::cerr << "DataTypeToString " << s << std::endl; - std::cerr << "DataTypeToString " << typeName(s).toStdString() << std::endl; return QScriptValue(engine, typeName(s) ); } diff --git a/common/DataTypes.hh b/common/DataTypes.hh index 31c0dfca..3bedae21 100644 --- a/common/DataTypes.hh +++ b/common/DataTypes.hh @@ -77,16 +77,61 @@ * datatypes in the future. */ +/** \brief the internal DataType class +* +* Normally we could use an unsigned int here. But QT cant register an +* typedef unsigned int DataType as DataType and will recognize it as unsigned int and +* therefore DataType will still be unknown to QtScript. +* To overcome this Problem, we reimplement a wrapper arround the int and provide additional +* functionality such as returning the name of the type directly +*/ +class DataType { + public: + DataType(); + DataType(const unsigned int& _i); + + bool operator!=( const unsigned int& _i ); + bool operator!=( const DataType& _i ); + + bool operator==( const unsigned int& _i ); + bool operator==( const DataType& _i ); + + bool operator=( const unsigned int& _i ); + bool operator=( const DataType& _i ); + + bool operator<( const unsigned int& _i ); + bool operator<( const DataType& _i ) const; + + bool operator&( const unsigned int& _i ); + bool operator&( const DataType& _i ) const; + + DataType operator|( const DataType& _i ) const; + + bool operator++(int _unused); + + /** return the internal representation of the type which is an unsigned int at the moment. + * + * You should avoid using this directly as the internal representation might change in the future + */ + unsigned int value() const; + + /// Return the name of this type as text + QString name(); + + private: + unsigned int field; +}; + +/// Identifier for all available objects +const DataType DATA_ALL(UINT_MAX); + /// None of the other Objects -#define DATA_UNKNOWN 0 +const DataType DATA_UNKNOWN(0); /// Items used for Grouping -#define DATA_GROUP 1 - -/// Identifier for all available objects -const unsigned int DATA_ALL = UINT_MAX; +const DataType DATA_GROUP(1); -typedef unsigned int DataType; +std::ostream &operator<<(std::ostream &stream, DataType type); //== TYPEDEFS ================================================================= diff --git a/common/Types.cc b/common/Types.cc index 29b454f2..5096a75b 100644 --- a/common/Types.cc +++ b/common/Types.cc @@ -71,7 +71,7 @@ static int nextTypeId_ = 2; /** This map maps an dataType id to an typeName */ -static std::map< unsigned int, QString > typeToString; +static std::map< DataType, QString > typeToString; /** This map maps an dataType name to its id in the types vector */ @@ -79,7 +79,7 @@ static std::map< QString , unsigned int > stringToTypeInfo; /** This map maps an dataType id to its id in the types vector */ -static std::map< unsigned int , unsigned int > typeToTypeInfo; +static std::map< DataType , unsigned int > typeToTypeInfo; class TypeInfo { @@ -117,9 +117,17 @@ static std::vector< TypeInfo > types; //== Functions ========================================================= +std::ostream &operator<<(std::ostream &stream, DataType type) +{ + stream << type.value() ; + + return stream; +} + void initializeTypes() { stringToTypeInfo["Unknown"] = types.size(); - typeToTypeInfo[DATA_UNKNOWN] = types.size(); + DataType test(DATA_UNKNOWN); + typeToTypeInfo[test] = types.size(); types.push_back( TypeInfo(DATA_UNKNOWN ,"Unknown" ,"Unknown.png", QCoreApplication::translate("Types","Unknown")) ); stringToTypeInfo["Group"] = types.size(); @@ -171,7 +179,7 @@ DataType typeId(QString _name) { /// Get the name of a type with given id QString typeName(DataType _id) { - std::map::iterator name = typeToString.find(_id); + std::map::iterator name = typeToString.find(_id); if ( name != typeToString.end() ) return name->second; @@ -203,7 +211,7 @@ QString typeIconName(QString _name) { /// get the icon of a given dataType QString typeIconName(DataType _id) { - std::map::iterator index = typeToTypeInfo.find(_id); + std::map::iterator index = typeToTypeInfo.find(_id); if ( index != typeToTypeInfo.end() ) return types[ index->second ].iconName; @@ -214,7 +222,7 @@ QString typeIconName(DataType _id) { /// get the icon of a given dataType QIcon& typeIcon(DataType _id) { - std::map::iterator index = typeToTypeInfo.find(_id); + std::map::iterator index = typeToTypeInfo.find(_id); if ( index != typeToTypeInfo.end() ) return types[ index->second ].icon; @@ -225,7 +233,7 @@ QIcon& typeIcon(DataType _id) { /// Set the icon for a given dataType void setTypeIcon( DataType _id , QString _icon ) { - std::map::iterator index = typeToTypeInfo.find(_id); + std::map::iterator index = typeToTypeInfo.find(_id); if ( index != typeToTypeInfo.end() ) { types[ index->second ].iconName = _icon; @@ -252,7 +260,7 @@ void setTypeIcon( QString _name , QString _icon ) { /// Get DataType Human readable name ( this name might change. Use the typeName insted! ) QString dataTypeName( DataType _id ) { - std::map::iterator index = typeToTypeInfo.find(_id); + std::map::iterator index = typeToTypeInfo.find(_id); if ( index != typeToTypeInfo.end() ) return types[ index->second ].readableName ; @@ -280,7 +288,7 @@ QString dataTypeName( QString _typeName ) { /// Set the icon for a given dataType void setDataTypeName( DataType _id , QString _name ) { - std::map::iterator index = typeToTypeInfo.find(_id); + std::map::iterator index = typeToTypeInfo.find(_id); if ( index != typeToTypeInfo.end() ) types[ index->second ].readableName = _name; @@ -299,5 +307,89 @@ void setDataTypeName( QString _typeName , QString _name ) { std::cerr << "Could not set human name for DataType. Type not found!" << std::endl; } + +DataType::DataType(): + field(0) +{ +}; + +DataType::DataType(const unsigned int& _i): + field(_i) +{ +}; + +//=========================================== + +bool DataType::operator!=( const unsigned int& _i ) { + return (_i != field); +} + +bool DataType::operator!=( const DataType& _i ) { + return (field != _i.field); +} + +//=========================================== + +bool DataType::operator==( const unsigned int& _i ) { + return (_i == field); +} + +bool DataType::operator==( const DataType& _i ) { + return (_i.field == field); +} + +//=========================================== + +bool DataType::operator=( const unsigned int& _i ) { + return (field = _i); +} + +bool DataType::operator=( const DataType& _i ) { + return (field = _i.field); +} + +//=========================================== + +bool DataType::operator<( const unsigned int& _i ) { + return (field < _i); +} + +bool DataType::operator<( const DataType& _i ) const { + return (field < _i.field); +} + +//=========================================== + +bool DataType::operator&( const unsigned int& _i ) { + return (field & _i); +} + +bool DataType::operator&( const DataType& _i ) const { + return (field & _i.field); +} + +//=========================================== + +DataType DataType::operator|( const DataType& _i ) const { + return (field | _i.field); +} + +//=========================================== + +bool DataType::operator++(int _unused) { + return (field *= 2); +} + +//=========================================== + +unsigned int DataType::value() const { + return( field ); +} + +QString DataType::name() { + return typeName(field); +} + + //============================================================================= //============================================================================= diff --git a/widgets/addEmptyWidget/addEmptyWidget.cc b/widgets/addEmptyWidget/addEmptyWidget.cc index f795ebd1..e457f73e 100644 --- a/widgets/addEmptyWidget/addEmptyWidget.cc +++ b/widgets/addEmptyWidget/addEmptyWidget.cc @@ -56,7 +56,7 @@ addEmptyWidget::addEmptyWidget(std::vector< DataType > _types , QStringList _typ typeBox->setDuplicatesEnabled(false); for (int i=0; i < (int)types_.size(); i++) - typeBox->addItem(typeNames_[i],QVariant(types_[i])); + typeBox->addItem(typeNames_[i],QVariant(types_[i].value())); //set last used DataType as currentItem for (int i=0; i < typeBox->count(); i++) diff --git a/widgets/coreWidget/About.cc b/widgets/coreWidget/About.cc index 425b24d9..b449a020 100644 --- a/widgets/coreWidget/About.cc +++ b/widgets/coreWidget/About.cc @@ -166,7 +166,7 @@ void CoreWidget::showAboutWidget( ) { types += typeName( currentType ) + " "; // Advance to next type ( Indices are bits so multiply by to to get next bit) - currentType *= 2; + currentType++; } aboutWidget_->OpenFlipperAbout->append( types ); -- GitLab