From 64df9718f20a7371c0572ea024987eac5eb26b75 Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Wed, 29 Jan 2014 16:36:44 -0800 Subject: [PATCH] Working on object attributes. --- interface/src/ui/MetavoxelEditor.cpp | 6 +- .../metavoxels/src/AttributeRegistry.cpp | 59 ++++++++++++++++++- libraries/metavoxels/src/AttributeRegistry.h | 54 +++++++++++++++++ libraries/metavoxels/src/MetavoxelData.cpp | 7 +-- libraries/metavoxels/src/MetavoxelData.h | 14 +++-- 5 files changed, 127 insertions(+), 13 deletions(-) diff --git a/interface/src/ui/MetavoxelEditor.cpp b/interface/src/ui/MetavoxelEditor.cpp index d35b44ffef..0a168ff251 100644 --- a/interface/src/ui/MetavoxelEditor.cpp +++ b/interface/src/ui/MetavoxelEditor.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -152,7 +153,10 @@ void MetavoxelEditor::updateValueEditor() { AttributePointer attribute = AttributeRegistry::getInstance()->getAttribute(selected); QWidget* editor = attribute->createEditor(); if (editor) { - _value->layout()->addWidget(editor); + QScrollArea* area = new QScrollArea(); + area->setWidgetResizable(true); + area->setWidget(editor); + _value->layout()->addWidget(area); } } diff --git a/libraries/metavoxels/src/AttributeRegistry.cpp b/libraries/metavoxels/src/AttributeRegistry.cpp index 5b7a8859ca..f57e3ca925 100644 --- a/libraries/metavoxels/src/AttributeRegistry.cpp +++ b/libraries/metavoxels/src/AttributeRegistry.cpp @@ -7,6 +7,9 @@ // #include +#include +#include +#include #include #include #include @@ -22,7 +25,8 @@ AttributeRegistry* AttributeRegistry::getInstance() { } AttributeRegistry::AttributeRegistry() : - _guideAttribute(registerAttribute(new PolymorphicAttribute("guide", PolymorphicDataPointer(new DefaultMetavoxelGuide())))), + _guideAttribute(registerAttribute(new SharedObjectAttribute("guide", &MetavoxelGuide::staticMetaObject, + PolymorphicDataPointer(new DefaultMetavoxelGuide())))), _colorAttribute(registerAttribute(new QRgbAttribute("color"))), _normalAttribute(registerAttribute(new QRgbAttribute("normal", qRgb(0, 127, 0)))) { } @@ -181,6 +185,59 @@ PolymorphicAttribute::PolymorphicAttribute(const QString& name, const Polymorphi InlineAttribute(name, defaultValue) { } +void* PolymorphicAttribute::createFromVariant(const QVariant& value) const { + return create(encodeInline(value.value())); +} + bool PolymorphicAttribute::merge(void*& parent, void* children[]) const { return false; } + +PolymorphicData* SharedObject::clone() const { + // default behavior is to make a copy using the no-arg constructor and copy the stored properties + const QMetaObject* metaObject = this->metaObject(); + QObject* newObject = metaObject->newInstance(); + for (int i = 0; i < metaObject->propertyCount(); i++) { + QMetaProperty property = metaObject->property(i); + if (property.isStored()) { + property.write(newObject, property.read(this)); + } + } + foreach (const QByteArray& propertyName, dynamicPropertyNames()) { + newObject->setProperty(propertyName, property(propertyName)); + } + return static_cast(newObject); +} + +SharedObjectAttribute::SharedObjectAttribute(const QString& name, const QMetaObject* metaObject, + const PolymorphicDataPointer& defaultValue) : + PolymorphicAttribute(name, defaultValue), + _metaObject(metaObject) { + +} + +QWidget* SharedObjectAttribute::createEditor(QWidget* parent) const { + SharedObjectEditor* editor = new SharedObjectEditor(parent); + editor->setObject(_defaultValue); + return editor; +} + +SharedObjectEditor::SharedObjectEditor(QWidget* parent) : QWidget(parent) { + QVBoxLayout* layout = new QVBoxLayout(); + setLayout(layout); + + QFormLayout* form = new QFormLayout(); + layout->addLayout(form); + + form->addRow("Type:", _type = new QComboBox()); + _type->addItem("(none)"); + connect(_type, SIGNAL(currentIndexChanged(int)), SLOT(updateType())); +} + +void SharedObjectEditor::setObject(const PolymorphicDataPointer& object) { + _object = object; +} + +void SharedObjectEditor::updateType() { + +} diff --git a/libraries/metavoxels/src/AttributeRegistry.h b/libraries/metavoxels/src/AttributeRegistry.h index 058b02d78f..c27e24395c 100644 --- a/libraries/metavoxels/src/AttributeRegistry.h +++ b/libraries/metavoxels/src/AttributeRegistry.h @@ -20,6 +20,7 @@ #include "Bitstream.h" +class QComboBox; class QPushButton; class QScriptContext; class QScriptEngine; @@ -338,13 +339,66 @@ template<> PolymorphicData* QExplicitlySharedDataPointer::clone typedef QExplicitlySharedDataPointer PolymorphicDataPointer; +Q_DECLARE_METATYPE(PolymorphicDataPointer) + /// Provides polymorphic streaming and averaging. class PolymorphicAttribute : public InlineAttribute { public: PolymorphicAttribute(const QString& name, const PolymorphicDataPointer& defaultValue = PolymorphicDataPointer()); + virtual void* createFromVariant(const QVariant& value) const; + virtual bool merge(void*& parent, void* children[]) const; }; +class SharedObject : public QObject, public PolymorphicData { + Q_OBJECT + +public: + + /// Creates a new clone of this object. + virtual PolymorphicData* clone() const; +}; + +/// An attribute that takes the form of QObjects of a given meta-type (a subclass of SharedObject). +class SharedObjectAttribute : public PolymorphicAttribute { + Q_OBJECT + Q_PROPERTY(const QMetaObject* metaObject MEMBER _metaObject) + +public: + + Q_INVOKABLE SharedObjectAttribute(const QString& name = QString(), const QMetaObject* metaObject = NULL, + const PolymorphicDataPointer& defaultValue = PolymorphicDataPointer()); + + virtual QWidget* createEditor(QWidget* parent = NULL) const; + +private: + + const QMetaObject* _metaObject; +}; + +/// Allows editing shared object instances. +class SharedObjectEditor : public QWidget { + Q_OBJECT + Q_PROPERTY(PolymorphicDataPointer object MEMBER _object WRITE setObject USER true) + +public: + + SharedObjectEditor(QWidget* parent); + +public slots: + + void setObject(const PolymorphicDataPointer& object); + +private slots: + + void updateType(); + +private: + + QComboBox* _type; + PolymorphicDataPointer _object; +}; + #endif /* defined(__interface__AttributeRegistry__) */ diff --git a/libraries/metavoxels/src/MetavoxelData.cpp b/libraries/metavoxels/src/MetavoxelData.cpp index 2f99684a42..8f3215bc84 100644 --- a/libraries/metavoxels/src/MetavoxelData.cpp +++ b/libraries/metavoxels/src/MetavoxelData.cpp @@ -425,8 +425,7 @@ MetavoxelVisitor::MetavoxelVisitor(const QVector& inputs, cons MetavoxelVisitor::~MetavoxelVisitor() { } -PolymorphicData* DefaultMetavoxelGuide::clone() const { - return new DefaultMetavoxelGuide(); +DefaultMetavoxelGuide::DefaultMetavoxelGuide() { } void DefaultMetavoxelGuide::guide(MetavoxelVisitation& visitation) { @@ -592,10 +591,6 @@ ScriptedMetavoxelGuide::ScriptedMetavoxelGuide(const QScriptValue& guideFunction _info.setProperty(_minimumHandle, _minimum); } -PolymorphicData* ScriptedMetavoxelGuide::clone() const { - return new ScriptedMetavoxelGuide(_guideFunction); -} - void ScriptedMetavoxelGuide::guide(MetavoxelVisitation& visitation) { QScriptValue data = _guideFunction.engine()->newVariant(QVariant::fromValue(this)); _getInputsFunction.setData(data); diff --git a/libraries/metavoxels/src/MetavoxelData.h b/libraries/metavoxels/src/MetavoxelData.h index 0114f1b4d6..06ecec0001 100644 --- a/libraries/metavoxels/src/MetavoxelData.h +++ b/libraries/metavoxels/src/MetavoxelData.h @@ -150,7 +150,9 @@ protected: typedef QSharedPointer MetavoxelVisitorPointer; /// Interface for objects that guide metavoxel visitors. -class MetavoxelGuide : public PolymorphicData { +class MetavoxelGuide : public SharedObject { + Q_OBJECT + public: /// Guides the specified visitor to the contained voxels. @@ -159,21 +161,23 @@ public: /// Guides visitors through the explicit content of the system. class DefaultMetavoxelGuide : public MetavoxelGuide { + Q_OBJECT + public: - virtual PolymorphicData* clone() const; + Q_INVOKABLE DefaultMetavoxelGuide(); virtual void guide(MetavoxelVisitation& visitation); }; /// Represents a guide implemented in Javascript. class ScriptedMetavoxelGuide : public MetavoxelGuide { + Q_OBJECT + public: - ScriptedMetavoxelGuide(const QScriptValue& guideFunction); + Q_INVOKABLE ScriptedMetavoxelGuide(const QScriptValue& guideFunction = QScriptValue()); - virtual PolymorphicData* clone() const; - virtual void guide(MetavoxelVisitation& visitation); private: