Commit 487415a3 authored by Jan Möbius's avatar Jan Möbius
Browse files

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
parent 97fed783
...@@ -413,7 +413,17 @@ Core::init() { ...@@ -413,7 +413,17 @@ Core::init() {
printFunction.setProperty("textedit",scriptEngine_.newQObject(this)); printFunction.setProperty("textedit",scriptEngine_.newQObject(this));
scriptEngine_.globalObject().setProperty("print", printFunction); 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_, qScriptRegisterMetaType(&scriptEngine_,
toScriptValueVector, toScriptValueVector,
fromScriptValueVector, fromScriptValueVector,
...@@ -423,31 +433,23 @@ Core::init() { ...@@ -423,31 +433,23 @@ Core::init() {
QScriptValue ctor = scriptEngine_.newFunction(createVector); QScriptValue ctor = scriptEngine_.newFunction(createVector);
scriptEngine_.globalObject().setProperty("Vector", ctor); scriptEngine_.globalObject().setProperty("Vector", ctor);
//==========================================================================
// // Register ObjectId Type to ScriptEngine ( is int ) // Register the DataType Class to the core
// 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 DataType in QScriptEngine // Register DataType in QScriptEngine
qScriptRegisterMetaType(&scriptEngine_, qScriptRegisterMetaType<DataType>(&scriptEngine_,
toScriptValueDataType, toScriptValueDataType,
fromScriptValueDataType, fromScriptValueDataType,
scriptEngine_.newQObject(&DataTypePrototype_)); scriptEngine_.newQObject(&DataTypePrototype_));
// set a constructor to allow creation via DataType(uint) // set a constructor to allow creation via DataType(uint)
QScriptValue dataType = scriptEngine_.newFunction(createDataType); QScriptValue dataTypector = scriptEngine_.newFunction(createDataType);
scriptEngine_.globalObject().setProperty("DataType", dataType); scriptEngine_.globalObject().setProperty("DataType", dataTypector);
//==========================================================================
// Register the Matrix Class to the core
//==========================================================================
// Register Matrix Type to scripting Engine ( is ACG::Matrix4x4d ) // Register Matrix Type to scripting Engine ( is ACG::Matrix4x4d )
qScriptRegisterMetaType(&scriptEngine_, qScriptRegisterMetaType(&scriptEngine_,
...@@ -459,7 +461,10 @@ Core::init() { ...@@ -459,7 +461,10 @@ Core::init() {
QScriptValue matrix4x4ctor = scriptEngine_.newFunction(createMatrix4x4); QScriptValue matrix4x4ctor = scriptEngine_.newFunction(createMatrix4x4);
scriptEngine_.globalObject().setProperty("Matrix4x4", matrix4x4ctor); scriptEngine_.globalObject().setProperty("Matrix4x4", matrix4x4ctor);
//==========================================================================
// Collect Core scripting information // Collect Core scripting information
//==========================================================================
QScriptValue scriptInstance = scriptEngine_.newQObject(this, QScriptValue scriptInstance = scriptEngine_.newQObject(this,
QScriptEngine::QtOwnership, QScriptEngine::QtOwnership,
QScriptEngine::ExcludeChildObjects | QScriptEngine::ExcludeChildObjects |
......
...@@ -89,7 +89,7 @@ void Core::slotGetAllFilters ( QStringList& _list){ ...@@ -89,7 +89,7 @@ void Core::slotGetAllFilters ( QStringList& _list){
for (int i=0; i < (int)supportedTypes_.size(); i++){ for (int i=0; i < (int)supportedTypes_.size(); i++){
QString f = supportedTypes_[i].plugin->getLoadFilters(); QString f = supportedTypes_[i].plugin->getLoadFilters();
f = f.section(")",0,0).section("(",1,1).trimmed(); 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 ) { ...@@ -326,11 +326,11 @@ void Core::slotAddEmptyObject( DataType _type , int& _id ) {
if ( OpenFlipper::Options::doSlotDebugging() ) { if ( OpenFlipper::Options::doSlotDebugging() ) {
if ( sender() != 0 ) { if ( sender() != 0 ) {
if ( sender()->metaObject() != 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() ) ); QString( sender()->metaObject()->className() ) );
} }
} else { } 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() { ...@@ -649,7 +649,7 @@ void Core::slotAddEmptyObjectMenu() {
} }
// Advance to next type ( Indices are bits so multiply by to to get next bit) // Advance to next type ( Indices are bits so multiply by to to get next bit)
currentType *= 2; currentType++;
} }
static addEmptyWidget* widget = 0; static addEmptyWidget* widget = 0;
......
...@@ -47,16 +47,12 @@ ...@@ -47,16 +47,12 @@
prototypeDataType::prototypeDataType(QObject *parent ) : prototypeDataType::prototypeDataType(QObject *parent ) :
QObject(parent) QObject(parent)
{ {
std::cerr << "Prototype cnstructor" << std::endl;
} }
QString prototypeDataType::toString() const { QString prototypeDataType::toString() const {
DataType s = thisObject().property("type").toNumber(); 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); return typeName(s);
} }
......
...@@ -58,22 +58,17 @@ ...@@ -58,22 +58,17 @@
QScriptValue toScriptValueDataType(QScriptEngine *engine, const DataType &s) QScriptValue toScriptValueDataType(QScriptEngine *engine, const DataType &s)
{ {
QScriptValue obj = engine->newObject(); QScriptValue obj = engine->newObject();
obj.setProperty("type", QScriptValue(engine, s )); obj.setProperty("type", QScriptValue(engine, s.value() ));
std::cerr << "toScriptValueDataType " << s << std::endl;
return obj; return obj;
} }
void fromScriptValueDataType(const QScriptValue &obj, DataType &s) void fromScriptValueDataType(const QScriptValue &obj, DataType &s)
{ {
s = obj.property("type").toNumber(); s = obj.property("type").toNumber();
std::cerr << "fromScriptValueDataType " << s << std::endl;
} }
QScriptValue createDataType(QScriptContext *context, QScriptEngine *engine) QScriptValue createDataType(QScriptContext *context, QScriptEngine *engine)
{ {
std::cerr << "createDataType " << std::endl;
DataType s; DataType s;
QScriptValue callee = context->callee(); QScriptValue callee = context->callee();
...@@ -86,9 +81,6 @@ QScriptValue createDataType(QScriptContext *context, QScriptEngine *engine) ...@@ -86,9 +81,6 @@ QScriptValue createDataType(QScriptContext *context, QScriptEngine *engine)
s = DATA_UNKNOWN; s = DATA_UNKNOWN;
} }
std::cerr << "s set to " << s << std::endl;
return engine->toScriptValue( s ); return engine->toScriptValue( s );
} }
...@@ -96,9 +88,6 @@ QScriptValue DataTypeToString(QScriptContext *context, QScriptEngine *engine) ...@@ -96,9 +88,6 @@ QScriptValue DataTypeToString(QScriptContext *context, QScriptEngine *engine)
{ {
DataType s = context->thisObject().property("type").toNumber(); 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) ); return QScriptValue(engine, typeName(s) );
} }
......
...@@ -77,16 +77,61 @@ ...@@ -77,16 +77,61 @@
* datatypes in the future. * datatypes in the future.
*/ */
/// None of the other Objects /** \brief the internal DataType class
#define DATA_UNKNOWN 0 *
* 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);
/// Items used for Grouping bool operator!=( const unsigned int& _i );
#define DATA_GROUP 1 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 /// Identifier for all available objects
const unsigned int DATA_ALL = UINT_MAX; const DataType DATA_ALL(UINT_MAX);
/// None of the other Objects
const DataType DATA_UNKNOWN(0);
/// Items used for Grouping
const DataType DATA_GROUP(1);
typedef unsigned int DataType; std::ostream &operator<<(std::ostream &stream, DataType type);
//== TYPEDEFS ================================================================= //== TYPEDEFS =================================================================
......
...@@ -71,7 +71,7 @@ static int nextTypeId_ = 2; ...@@ -71,7 +71,7 @@ static int nextTypeId_ = 2;
/** This map maps an dataType id to an typeName /** 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 /** This map maps an dataType name to its id in the types vector
*/ */
...@@ -79,7 +79,7 @@ static std::map< QString , unsigned int > stringToTypeInfo; ...@@ -79,7 +79,7 @@ static std::map< QString , unsigned int > stringToTypeInfo;
/** This map maps an dataType id to its id in the types vector /** 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 { class TypeInfo {
...@@ -117,9 +117,17 @@ static std::vector< TypeInfo > types; ...@@ -117,9 +117,17 @@ static std::vector< TypeInfo > types;
//== Functions ========================================================= //== Functions =========================================================
std::ostream &operator<<(std::ostream &stream, DataType type)
{
stream << type.value() ;
return stream;
}
void initializeTypes() { void initializeTypes() {
stringToTypeInfo["Unknown"] = types.size(); 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")) ); types.push_back( TypeInfo(DATA_UNKNOWN ,"Unknown" ,"Unknown.png", QCoreApplication::translate("Types","Unknown")) );
stringToTypeInfo["Group"] = types.size(); stringToTypeInfo["Group"] = types.size();
...@@ -171,7 +179,7 @@ DataType typeId(QString _name) { ...@@ -171,7 +179,7 @@ DataType typeId(QString _name) {
/// Get the name of a type with given id /// Get the name of a type with given id
QString typeName(DataType _id) { QString typeName(DataType _id) {
std::map<unsigned int, QString>::iterator name = typeToString.find(_id); std::map<DataType, QString>::iterator name = typeToString.find(_id);
if ( name != typeToString.end() ) if ( name != typeToString.end() )
return name->second; return name->second;
...@@ -203,7 +211,7 @@ QString typeIconName(QString _name) { ...@@ -203,7 +211,7 @@ QString typeIconName(QString _name) {
/// get the icon of a given dataType /// get the icon of a given dataType
QString typeIconName(DataType _id) { QString typeIconName(DataType _id) {
std::map<unsigned int, unsigned int>::iterator index = typeToTypeInfo.find(_id); std::map<DataType, unsigned int>::iterator index = typeToTypeInfo.find(_id);
if ( index != typeToTypeInfo.end() ) if ( index != typeToTypeInfo.end() )
return types[ index->second ].iconName; return types[ index->second ].iconName;
...@@ -214,7 +222,7 @@ QString typeIconName(DataType _id) { ...@@ -214,7 +222,7 @@ QString typeIconName(DataType _id) {
/// get the icon of a given dataType /// get the icon of a given dataType
QIcon& typeIcon(DataType _id) { QIcon& typeIcon(DataType _id) {
std::map<unsigned int, unsigned int>::iterator index = typeToTypeInfo.find(_id); std::map<DataType, unsigned int>::iterator index = typeToTypeInfo.find(_id);
if ( index != typeToTypeInfo.end() ) if ( index != typeToTypeInfo.end() )
return types[ index->second ].icon; return types[ index->second ].icon;
...@@ -225,7 +233,7 @@ QIcon& typeIcon(DataType _id) { ...@@ -225,7 +233,7 @@ QIcon& typeIcon(DataType _id) {
/// Set the icon for a given dataType /// Set the icon for a given dataType
void setTypeIcon( DataType _id , QString _icon ) { void setTypeIcon( DataType _id , QString _icon ) {
std::map<unsigned int, unsigned int>::iterator index = typeToTypeInfo.find(_id); std::map<DataType, unsigned int>::iterator index = typeToTypeInfo.find(_id);
if ( index != typeToTypeInfo.end() ) { if ( index != typeToTypeInfo.end() ) {
types[ index->second ].iconName = _icon; types[ index->second ].iconName = _icon;
...@@ -252,7 +260,7 @@ void setTypeIcon( QString _name , QString _icon ) { ...@@ -252,7 +260,7 @@ void setTypeIcon( QString _name , QString _icon ) {
/// Get DataType Human readable name ( this name might change. Use the typeName insted! ) /// Get DataType Human readable name ( this name might change. Use the typeName insted! )
QString dataTypeName( DataType _id ) { QString dataTypeName( DataType _id ) {
std::map<unsigned int, unsigned int>::iterator index = typeToTypeInfo.find(_id); std::map<DataType, unsigned int>::iterator index = typeToTypeInfo.find(_id);
if ( index != typeToTypeInfo.end() ) if ( index != typeToTypeInfo.end() )
return types[ index->second ].readableName ; return types[ index->second ].readableName ;
...@@ -280,7 +288,7 @@ QString dataTypeName( QString _typeName ) { ...@@ -280,7 +288,7 @@ QString dataTypeName( QString _typeName ) {
/// Set the icon for a given dataType /// Set the icon for a given dataType
void setDataTypeName( DataType _id , QString _name ) { void setDataTypeName( DataType _id , QString _name ) {
std::map<unsigned int, unsigned int>::iterator index = typeToTypeInfo.find(_id); std::map<DataType, unsigned int>::iterator index = typeToTypeInfo.find(_id);
if ( index != typeToTypeInfo.end() ) if ( index != typeToTypeInfo.end() )
types[ index->second ].readableName = _name; types[ index->second ].readableName = _name;
...@@ -299,5 +307,89 @@ void setDataTypeName( QString _typeName , QString _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; 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);
}
//============================================================================= //=============================================================================
//============================================================================= //=============================================================================
...@@ -56,7 +56,7 @@ addEmptyWidget::addEmptyWidget(std::vector< DataType > _types , QStringList _typ ...@@ -56,7 +56,7 @@ addEmptyWidget::addEmptyWidget(std::vector< DataType > _types , QStringList _typ
typeBox->setDuplicatesEnabled(false); typeBox->setDuplicatesEnabled(false);
for (int i=0; i < (int)types_.size(); i++) 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 //set last used DataType as currentItem
for (int i=0; i < typeBox->count(); i++) for (int i=0; i < typeBox->count(); i++)
......
...@@ -166,7 +166,7 @@ void CoreWidget::showAboutWidget( ) { ...@@ -166,7 +166,7 @@ void CoreWidget::showAboutWidget( ) {
types += typeName( currentType ) + " "; types += typeName( currentType ) + " ";
// Advance to next type ( Indices are bits so multiply by to to get next bit) // Advance to next type ( Indices are bits so multiply by to to get next bit)
currentType *= 2; currentType++;
} }
aboutWidget_->OpenFlipperAbout->append( types ); aboutWidget_->OpenFlipperAbout->append( types );
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment