From e206ce6108536040158407a6a0d0fa67adfacf08 Mon Sep 17 00:00:00 2001 From: Tats Date: Sun, 8 Dec 2013 14:45:41 -0500 Subject: [PATCH] Code refactoring to add output shape to property editor --- Mapper.cpp | 81 +++++++++++++++++++++++++++++++++++------------------- Mapper.h | 12 ++++++++ 2 files changed, 64 insertions(+), 29 deletions(-) diff --git a/Mapper.cpp b/Mapper.cpp index 69be963..1a730fc 100644 --- a/Mapper.cpp +++ b/Mapper.cpp @@ -34,31 +34,28 @@ TextureMapper::TextureMapper(std::tr1::shared_ptr mapping) std::tr1::shared_ptr texture = std::tr1::static_pointer_cast(textureMapping->getPaint()); Q_CHECK_PTR(texture); - QtProperty *topItem = _variantManager->addProperty(QtVariantPropertyManager::groupTypeId(), - QObject::tr("Texture mapping")); - // Input shape - QtProperty *inputItem = _variantManager->addProperty(QtVariantPropertyManager::groupTypeId(), - QObject::tr("Input shape")); - topItem->addSubProperty(inputItem); + _topItem = _variantManager->addProperty(QtVariantPropertyManager::groupTypeId(), + QObject::tr("Texture mapping")); - Shape::ptr inputShape = textureMapping->getInputShape(); - for (int i=0; inVertices(); i++) - { - // Add point. - QtVariantProperty* pointItem = _variantManager->addProperty(QVariant::PointF, - QString("Point %1").arg(i)); + // Input shape. + _inputItem = _variantManager->addProperty(QtVariantPropertyManager::groupTypeId(), + QObject::tr("Input shape")); - Point p = inputShape->getVertex(i); - pointItem->setValue(QPointF(p.x, p.y)); + _buildShapeProperty(_inputItem, textureMapping->getInputShape().get()); + _topItem->addSubProperty(_inputItem); - inputItem->addSubProperty(pointItem); - _propertyToVertex[pointItem] = std::make_pair(inputShape.get(), i); - } + // Output shape. + // Input shape. + _outputItem = _variantManager->addProperty(QtVariantPropertyManager::groupTypeId(), + QObject::tr("Output shape")); + + _buildShapeProperty(_outputItem, textureMapping->getShape().get()); + _topItem->addSubProperty(_outputItem); connect(_variantManager, SIGNAL(valueChanged(QtProperty*, const QVariant&)), this, SLOT(setValue(QtProperty*, const QVariant&))); - _propertyBrowser->addProperty(topItem); + _propertyBrowser->addProperty(_topItem); qDebug() << "Creating mapper" << endl; } @@ -85,19 +82,17 @@ void TextureMapper::updateShape(Shape* shape) std::tr1::shared_ptr texture = std::tr1::static_pointer_cast(textureMapping->getPaint()); Q_CHECK_PTR(texture); - Shape::ptr inputShape = textureMapping->getInputShape(); - if (shape == inputShape.get()) + Shape* inputShape = textureMapping->getInputShape().get(); + Shape* outputShape = textureMapping->getShape().get(); + if (shape == inputShape) { - QtProperty* topItem = _propertyBrowser->properties().first(); - QtProperty* inputItem = topItem->subProperties()[0]; - QList pointItems = inputItem->subProperties(); - for (int i=0; inVertices(); i++) - { - QtVariantProperty* pointItem = (QtVariantProperty*)pointItems[i]; - Point p = inputShape->getVertex(i); - pointItem->setValue(QPointF(p.x, p.y)); - } + _updateShapeProperty(_inputItem, inputShape); } + else if (shape == outputShape) + { + _updateShapeProperty(_outputItem, outputShape); + } + } QWidget* TextureMapper::getPropertiesEditor() @@ -163,3 +158,31 @@ void TextureMapper::draw() glDisable(GL_TEXTURE_2D); } +void TextureMapper::_buildShapeProperty(QtProperty* shapeItem, Shape* shape) +{ + for (int i=0; inVertices(); i++) + { + // Add point. + QtVariantProperty* pointItem = _variantManager->addProperty(QVariant::PointF, + QObject::tr("Point %1").arg(i)); + + Point p = shape->getVertex(i); + pointItem->setValue(QPointF(p.x, p.y)); + + shapeItem->addSubProperty(pointItem); + _propertyToVertex[pointItem] = std::make_pair(shape, i); + } + +} + +void TextureMapper::_updateShapeProperty(QtProperty* shapeItem, Shape* shape) +{ + QList pointItems = shapeItem->subProperties(); + for (int i=0; inVertices(); i++) + { + QtVariantProperty* pointItem = (QtVariantProperty*)pointItems[i]; + Point p = shape->getVertex(i); + pointItem->setValue(QPointF(p.x, p.y)); + } +} + diff --git a/Mapper.h b/Mapper.h index 43219b8..1928df2 100644 --- a/Mapper.h +++ b/Mapper.h @@ -71,6 +71,12 @@ public: virtual QWidget* getPropertiesEditor() = 0; virtual void draw() = 0; + +public slots: + virtual void updateShape(Shape* shape) + { + Q_UNUSED(shape); + } }; //class ShapeDrawer @@ -136,8 +142,14 @@ private: QtAbstractPropertyBrowser* _propertyBrowser; QtVariantEditorFactory* _variantFactory; QtVariantPropertyManager* _variantManager; + QtProperty* _topItem; + QtProperty* _inputItem; + QtProperty* _outputItem; std::map > _propertyToVertex; + + void _buildShapeProperty(QtProperty* shapeItem, Shape* shape); + void _updateShapeProperty(QtProperty* shapeItem, Shape* shape); };