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

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,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<DataType>(&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 |
......
......@@ -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;
......
......@@ -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);
}
......
......@@ -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) );
}
......
......@@ -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 =================================================================
......
......@@ -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<unsigned int, QString>::iterator name = typeToString.find(_id);
std::map<DataType, QString>::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<unsigned int, unsigned int>::iterator index = typeToTypeInfo.find(_id);
std::map<DataType, unsigned int>::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<unsigned int, unsigned int>::iterator index = typeToTypeInfo.find(_id);
std::map<DataType, unsigned int>::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<unsigned int, unsigned int>::iterator index = typeToTypeInfo.find(_id);
std::map<DataType, unsigned int>::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<unsigned int, unsigned int>::iterator index = typeToTypeInfo.find(_id);
std::map<DataType, unsigned int>::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<unsigned int, unsigned int>::iterator index = typeToTypeInfo.find(_id);
std::map<DataType, unsigned int>::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);
}
//=============================================================================
//=============================================================================
......@@ -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++)
......
......@@ -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 );
......
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