From e8870db92aa606912384ee72cd8d214b2feee204 Mon Sep 17 00:00:00 2001 From: Tats Date: Fri, 31 Oct 2014 17:03:18 -0400 Subject: [PATCH 01/32] Replaced Shape by MShape to avoid name conflicts. --- DestinationGLCanvas.cpp | 2 +- DestinationGLCanvas.h | 2 +- MainWindow.cpp | 36 ++++++++++++++++++------------------ Mapper.cpp | 16 ++++++++-------- MapperGLCanvas.cpp | 4 ++-- MapperGLCanvas.h | 8 ++++---- Mapping.cpp | 2 +- Mapping.h | 18 +++++++++--------- ProjectWriter.cpp | 4 ++-- ProjectWriter.h | 2 +- Shape.cpp | 8 ++++---- Shape.h | 22 +++++++++++----------- SourceGLCanvas.cpp | 2 +- SourceGLCanvas.h | 2 +- Util.cpp | 2 +- Util.h | 2 +- 16 files changed, 66 insertions(+), 66 deletions(-) diff --git a/DestinationGLCanvas.cpp b/DestinationGLCanvas.cpp index 1116d5f..43b1e96 100644 --- a/DestinationGLCanvas.cpp +++ b/DestinationGLCanvas.cpp @@ -27,7 +27,7 @@ DestinationGLCanvas::DestinationGLCanvas(MainWindow* mainWindow, QWidget* parent { } -Shape* DestinationGLCanvas::getShapeFromMappingId(uid mappingId) +MShape* DestinationGLCanvas::getShapeFromMappingId(uid mappingId) { if (mappingId == NULL_UID) return NULL; diff --git a/DestinationGLCanvas.h b/DestinationGLCanvas.h index bbd5d45..4d99c09 100644 --- a/DestinationGLCanvas.h +++ b/DestinationGLCanvas.h @@ -34,7 +34,7 @@ public: DestinationGLCanvas(MainWindow* mainWindow, QWidget* parent = 0, const QGLWidget * shareWidget = 0); // virtual ~DestinationGLCanvas(); - virtual Shape* getShapeFromMappingId(uid mappingId); + virtual MShape* getShapeFromMappingId(uid mappingId); void setDisplayCrosshair(bool displayCrosshair) { _displayCrosshair = displayCrosshair; diff --git a/MainWindow.cpp b/MainWindow.cpp index de0d506..d405ddc 100644 --- a/MainWindow.cpp +++ b/MainWindow.cpp @@ -409,7 +409,7 @@ void MainWindow::addMesh() Mapping* mappingPtr; if (paint->getType() == "color") { - Shape::ptr outputQuad = Shape::ptr(Util::createQuadForColor(sourceCanvas->width(), sourceCanvas->height())); + MShape::ptr outputQuad = MShape::ptr(Util::createQuadForColor(sourceCanvas->width(), sourceCanvas->height())); mappingPtr = new ColorMapping(paint, outputQuad); } else @@ -417,8 +417,8 @@ void MainWindow::addMesh() std::tr1::shared_ptr texture = std::tr1::static_pointer_cast(paint); Q_CHECK_PTR(texture); - Shape::ptr outputQuad = Shape::ptr(Util::createMeshForTexture(texture.get(), sourceCanvas->width(), sourceCanvas->height())); - Shape::ptr inputQuad = Shape::ptr(Util::createMeshForTexture(texture.get(), sourceCanvas->width(), sourceCanvas->height())); + MShape::ptr outputQuad = MShape::ptr(Util::createMeshForTexture(texture.get(), sourceCanvas->width(), sourceCanvas->height())); + MShape::ptr inputQuad = MShape::ptr(Util::createMeshForTexture(texture.get(), sourceCanvas->width(), sourceCanvas->height())); mappingPtr = new TextureMapping(paint, outputQuad, inputQuad); } @@ -442,7 +442,7 @@ void MainWindow::addTriangle() Mapping* mappingPtr; if (paint->getType() == "color") { - Shape::ptr outputTriangle = Shape::ptr(Util::createTriangleForColor(sourceCanvas->width(), sourceCanvas->height())); + MShape::ptr outputTriangle = MShape::ptr(Util::createTriangleForColor(sourceCanvas->width(), sourceCanvas->height())); mappingPtr = new ColorMapping(paint, outputTriangle); } else @@ -450,8 +450,8 @@ void MainWindow::addTriangle() std::tr1::shared_ptr texture = std::tr1::static_pointer_cast(paint); Q_CHECK_PTR(texture); - Shape::ptr outputTriangle = Shape::ptr(Util::createTriangleForTexture(texture.get(), sourceCanvas->width(), sourceCanvas->height())); - Shape::ptr inputTriangle = Shape::ptr(Util::createTriangleForTexture(texture.get(), sourceCanvas->width(), sourceCanvas->height())); + MShape::ptr outputTriangle = MShape::ptr(Util::createTriangleForTexture(texture.get(), sourceCanvas->width(), sourceCanvas->height())); + MShape::ptr inputTriangle = MShape::ptr(Util::createTriangleForTexture(texture.get(), sourceCanvas->width(), sourceCanvas->height())); mappingPtr = new TextureMapping(paint, inputTriangle, outputTriangle); } @@ -475,7 +475,7 @@ void MainWindow::addEllipse() Mapping* mappingPtr; if (paint->getType() == "color") { - Shape::ptr outputEllipse = Shape::ptr(Util::createEllipseForColor(sourceCanvas->width(), sourceCanvas->height())); + MShape::ptr outputEllipse = MShape::ptr(Util::createEllipseForColor(sourceCanvas->width(), sourceCanvas->height())); mappingPtr = new ColorMapping(paint, outputEllipse); } else @@ -483,8 +483,8 @@ void MainWindow::addEllipse() std::tr1::shared_ptr texture = std::tr1::static_pointer_cast(paint); Q_CHECK_PTR(texture); - Shape::ptr outputEllipse = Shape::ptr(Util::createEllipseForTexture(texture.get(), sourceCanvas->width(), sourceCanvas->height())); - Shape::ptr inputEllipse = Shape::ptr(Util::createEllipseForTexture(texture.get(), sourceCanvas->width(), sourceCanvas->height())); + MShape::ptr outputEllipse = MShape::ptr(Util::createEllipseForTexture(texture.get(), sourceCanvas->width(), sourceCanvas->height())); + MShape::ptr inputEllipse = MShape::ptr(Util::createEllipseForTexture(texture.get(), sourceCanvas->width(), sourceCanvas->height())); mappingPtr = new TextureMapping(paint, inputEllipse, outputEllipse); } @@ -722,8 +722,8 @@ uid MainWindow::createMeshTextureMapping(uid mappingId, qDebug() << nVertices << " vs " << nColumns << "x" << nRows << " vs " << src.size() << " " << dst.size() << endl; Q_ASSERT(src.size() == nVertices && dst.size() == nVertices); - Shape::ptr inputMesh( new Mesh(src, nColumns, nRows)); - Shape::ptr outputMesh(new Mesh(dst, nColumns, nRows)); + MShape::ptr inputMesh( new Mesh(src, nColumns, nRows)); + MShape::ptr outputMesh(new Mesh(dst, nColumns, nRows)); // Add it to the manager. Mapping::ptr mapping(new TextureMapping(paint, outputMesh, inputMesh, mappingId)); @@ -752,8 +752,8 @@ uid MainWindow::createTriangleTextureMapping(uid mappingId, Paint::ptr paint = mappingManager->getPaintById(paintId); Q_ASSERT(src.size() == 3 && dst.size() == 3); - Shape::ptr inputTriangle( new Triangle(src[0], src[1], src[2])); - Shape::ptr outputTriangle(new Triangle(dst[0], dst[1], dst[2])); + MShape::ptr inputTriangle( new Triangle(src[0], src[1], src[2])); + MShape::ptr outputTriangle(new Triangle(dst[0], dst[1], dst[2])); // Add it to the manager. Mapping::ptr mapping(new TextureMapping(paint, outputTriangle, inputTriangle, mappingId)); @@ -782,8 +782,8 @@ uid MainWindow::createEllipseTextureMapping(uid mappingId, Paint::ptr paint = mappingManager->getPaintById(paintId); Q_ASSERT(src.size() == 5 && dst.size() == 5); - Shape::ptr inputEllipse( new Ellipse(src[0], src[1], src[2], src[3], src[4])); - Shape::ptr outputEllipse(new Ellipse(dst[0], dst[1], dst[2], dst[3], dst[4])); + MShape::ptr inputEllipse( new Ellipse(src[0], src[1], src[2], src[3], src[4])); + MShape::ptr outputEllipse(new Ellipse(dst[0], dst[1], dst[2], dst[3], dst[4])); // Add it to the manager. Mapping::ptr mapping(new TextureMapping(paint, outputEllipse, inputEllipse, mappingId)); @@ -812,7 +812,7 @@ uid MainWindow::createQuadColorMapping(uid mappingId, Paint::ptr paint = mappingManager->getPaintById(paintId); Q_ASSERT(dst.size() == 4); - Shape::ptr outputQuad(new Quad(dst[0], dst[1], dst[2], dst[3])); + MShape::ptr outputQuad(new Quad(dst[0], dst[1], dst[2], dst[3])); // Add it to the manager. Mapping::ptr mapping(new ColorMapping(paint, outputQuad, mappingId)); @@ -841,7 +841,7 @@ uid MainWindow::createTriangleColorMapping(uid mappingId, Paint::ptr paint = mappingManager->getPaintById(paintId); Q_ASSERT(dst.size() == 3); - Shape::ptr outputTriangle(new Triangle(dst[0], dst[1], dst[2])); + MShape::ptr outputTriangle(new Triangle(dst[0], dst[1], dst[2])); // Add it to the manager. Mapping::ptr mapping(new ColorMapping(paint, outputTriangle, mappingId)); @@ -870,7 +870,7 @@ uid MainWindow::createEllipseColorMapping(uid mappingId, Paint::ptr paint = mappingManager->getPaintById(paintId); Q_ASSERT(dst.size() == 4); - Shape::ptr outputEllipse(new Ellipse(dst[0], dst[1], dst[2], dst[3])); + MShape::ptr outputEllipse(new Ellipse(dst[0], dst[1], dst[2], dst[3])); // Add it to the manager. Mapping::ptr mapping(new ColorMapping(paint, outputEllipse, mappingId)); diff --git a/Mapper.cpp b/Mapper.cpp index 8713fd9..c9766c5 100644 --- a/Mapper.cpp +++ b/Mapper.cpp @@ -65,11 +65,11 @@ QWidget* Mapper::getPropertiesEditor() void Mapper::setValue(QtProperty* property, const QVariant& value) { - std::map >::iterator it = _propertyToVertex.find(property); + std::map >::iterator it = _propertyToVertex.find(property); if (it != _propertyToVertex.end()) { const QPointF& p = value.toPointF(); - Shape* shape = it->second.first; + MShape* shape = it->second.first; int v = it->second.second; if (shape->getVertex(v) != p) { @@ -79,7 +79,7 @@ void Mapper::setValue(QtProperty* property, const QVariant& value) } } -void Mapper::_buildShapeProperty(QtProperty* shapeItem, Shape* shape) +void Mapper::_buildShapeProperty(QtProperty* shapeItem, MShape* shape) { for (int i=0; inVertices(); i++) { @@ -96,7 +96,7 @@ void Mapper::_buildShapeProperty(QtProperty* shapeItem, Shape* shape) } -void Mapper::_updateShapeProperty(QtProperty* shapeItem, Shape* shape) +void Mapper::_updateShapeProperty(QtProperty* shapeItem, MShape* shape) { QList pointItems = shapeItem->subProperties(); for (int i=0; inVertices(); i++) @@ -233,7 +233,7 @@ TextureMapper::TextureMapper(std::tr1::shared_ptr mapping) texture = std::tr1::static_pointer_cast(textureMapping->getPaint()); Q_CHECK_PTR(texture); - inputShape = std::tr1::static_pointer_cast(textureMapping->getInputShape()); + inputShape = std::tr1::static_pointer_cast(textureMapping->getInputShape()); Q_CHECK_PTR(inputShape); // Input shape. @@ -282,7 +282,7 @@ void TextureMapper::drawInput(QPainter* painter) _postDraw(painter); } -void TextureMapper::updateShape(Shape* shape) +void TextureMapper::updateShape(MShape* shape) { std::tr1::shared_ptr textureMapping = std::tr1::static_pointer_cast(_mapping); Q_CHECK_PTR(textureMapping); @@ -290,8 +290,8 @@ void TextureMapper::updateShape(Shape* shape) std::tr1::shared_ptr texture = std::tr1::static_pointer_cast(textureMapping->getPaint()); Q_CHECK_PTR(texture); - Shape* inputShape = textureMapping->getInputShape().get(); - Shape* outputShape = textureMapping->getShape().get(); + MShape* inputShape = textureMapping->getInputShape().get(); + MShape* outputShape = textureMapping->getShape().get(); if (shape == inputShape) { _updateShapeProperty(_inputItem, inputShape); diff --git a/MapperGLCanvas.cpp b/MapperGLCanvas.cpp index e9d7d84..b7ee0e3 100644 --- a/MapperGLCanvas.cpp +++ b/MapperGLCanvas.cpp @@ -351,12 +351,12 @@ void MapperGLCanvas::enableStickyVertices(bool value) /* Stick vertex p of Shape orig to another Shape's vertex, if the 2 vertices are * close enough. The distance per coordinate is currently set in dist_stick * variable. Perhaps the sticky-sensitivity should be configurable through GUI */ -void MapperGLCanvas::glueVertex(Shape *orig, QPointF *p) +void MapperGLCanvas::glueVertex(MShape *orig, QPointF *p) { MappingManager manager = getMainWindow()->getMappingManager(); for (int i = 0; i < manager.nMappings(); i++) { - Shape *shape = getShapeFromMappingId(manager.getMapping(i)->getId()); + MShape *shape = getShapeFromMappingId(manager.getMapping(i)->getId()); if (shape && shape != orig) { for (int vertex = 0; vertex < shape->nVertices(); vertex++) diff --git a/MapperGLCanvas.h b/MapperGLCanvas.h index a1a1704..93a3191 100644 --- a/MapperGLCanvas.h +++ b/MapperGLCanvas.h @@ -45,13 +45,13 @@ public: virtual ~MapperGLCanvas() {} /// Returns shape associated with mapping id. - virtual Shape* getShapeFromMappingId(uid mappingId) = 0; + virtual MShape* getShapeFromMappingId(uid mappingId) = 0; // QSize sizeHint() const; // QSize minimumSizeHint() const; /// Returns current shape. - Shape* getCurrentShape(); +// Shape* getCurrentShape(); /** * Stick vertex p of Shape orig to another Shape's vertex, if the 2 vertices are @@ -59,7 +59,7 @@ public: * variable. */ // TODO: Perhaps the sticky-sensitivity should be configurable through GUI - void glueVertex(Shape *, QPointF *); + void glueVertex(MShape *, QPointF *); /// Returns pointer to main window. MainWindow* getMainWindow() const { return _mainWindow; } @@ -133,7 +133,7 @@ private: bool _stickyVertices; signals: - void shapeChanged(Shape*); + void shapeChanged(MShape*); void imageChanged(); public slots: diff --git a/Mapping.cpp b/Mapping.cpp index c4d1051..e1c6301 100644 --- a/Mapping.cpp +++ b/Mapping.cpp @@ -22,7 +22,7 @@ UidAllocator Mapping::allocator; -Mapping::Mapping(Paint::ptr paint, Shape::ptr shape, uid id) +Mapping::Mapping(Paint::ptr paint, MShape::ptr shape, uid id) : _paint(paint), _shape(shape), _isLocked(false), _isSolo(false), _isVisible(true), _opacity(1.0f) { diff --git a/Mapping.h b/Mapping.h index 25c2cd0..8a0d013 100644 --- a/Mapping.h +++ b/Mapping.h @@ -46,7 +46,7 @@ protected: Paint::ptr _paint; /// The output Shape instance. - Shape::ptr _shape; + MShape::ptr _shape; private: static UidAllocator allocator; @@ -60,7 +60,7 @@ private: protected: /// Constructor. - Mapping(Paint::ptr paint, Shape::ptr shape, uid id=NULL_UID); + Mapping(Paint::ptr paint, MShape::ptr shape, uid id=NULL_UID); public: typedef std::tr1::shared_ptr ptr; @@ -85,13 +85,13 @@ public: Paint::ptr getPaint() const { return _paint; } /// Returns the (output) shape. - Shape::ptr getShape() const { return _shape; } + MShape::ptr getShape() const { return _shape; } /// Returns true iff the mapping possesses an input (source) shape. virtual bool hasInputShape() const { return false; } /// Returns the input (source) shape (if this mapping has one) or a null pointer if not. - virtual Shape::ptr getInputShape() const { return Shape::ptr(); } + virtual MShape::ptr getInputShape() const { return MShape::ptr(); } uid getId() const { return _id; } @@ -121,7 +121,7 @@ public: class ColorMapping : public Mapping { public: - ColorMapping(Paint::ptr paint, Shape::ptr shape, + ColorMapping(Paint::ptr paint, MShape::ptr shape, uid id=NULL_UID) : Mapping(paint, shape, id) {} @@ -138,12 +138,12 @@ public: class TextureMapping : public Mapping { private: - Shape::ptr _inputShape; + MShape::ptr _inputShape; public: TextureMapping(Paint::ptr paint, - Shape::ptr shape, - Shape::ptr inputShape, uid id=NULL_UID) + MShape::ptr shape, + MShape::ptr inputShape, uid id=NULL_UID) : Mapping(paint, shape, id), _inputShape(inputShape) { @@ -162,7 +162,7 @@ public: public: virtual bool hasInputShape() const { return true; } - virtual Shape::ptr getInputShape() const { return _inputShape; } + virtual MShape::ptr getInputShape() const { return _inputShape; } }; #endif /* MAPPING_H_ */ diff --git a/ProjectWriter.cpp b/ProjectWriter.cpp index 5dcaeca..b3a06ab 100644 --- a/ProjectWriter.cpp +++ b/ProjectWriter.cpp @@ -117,7 +117,7 @@ void ProjectWriter::writeItem(Paint *item) qDebug() << "Unknown type, cannot save: " << item->getType() << endl; } -void ProjectWriter::writeShapeVertices(Shape *shape) +void ProjectWriter::writeShapeVertices(MShape *shape) { if (shape->getType() == "mesh") { Mesh* mesh = (Mesh*) shape; @@ -150,7 +150,7 @@ void ProjectWriter::writeItem(Mapping *item) _xml.writeAttribute("solo", QString::number((int) item->isSolo() ? 1 : 0)); _xml.writeAttribute("visible", QString::number((int) item->isVisible() ? 1 : 0)); - Shape *shape = item->getShape().get(); + MShape *shape = item->getShape().get(); _xml.writeStartElement("destination"); _xml.writeAttribute("shape", shape->getType()); writeShapeVertices(shape); diff --git a/ProjectWriter.h b/ProjectWriter.h index 1f5d99b..737e71b 100644 --- a/ProjectWriter.h +++ b/ProjectWriter.h @@ -32,7 +32,7 @@ class ProjectWriter private: void writeItem (Paint *item); void writeItem (Mapping *item); - void writeShapeVertices (Shape *shape); + void writeShapeVertices (MShape *shape); QXmlStreamWriter _xml; MappingManager *_manager; }; diff --git a/Shape.cpp b/Shape.cpp index fbfa349..17c67c0 100644 --- a/Shape.cpp +++ b/Shape.cpp @@ -19,7 +19,7 @@ #include "Shape.h" -void Shape::translate(int x, int y) +void MShape::translate(int x, int y) { for (QVector::iterator it = vertices.begin() ; it != vertices.end(); ++it) @@ -507,10 +507,10 @@ void Ellipse::setVertex(int i, const QPointF& v) transform *= fromUnitCircle(); // Set vertices. - Shape::setVertex(1, transform.map( getVertex(1) )); - Shape::setVertex(3, transform.map( getVertex(3) )); + MShape::setVertex(1, transform.map( getVertex(1) )); + MShape::setVertex(3, transform.map( getVertex(3) )); if (hasCenterControl()) - Shape::setVertex(4, transform.map( getVertex(4) )); + MShape::setVertex(4, transform.map( getVertex(4) )); } // If changed one of the two other points, just change the vertical axis. diff --git a/Shape.h b/Shape.h index 10b19d5..366a588 100644 --- a/Shape.h +++ b/Shape.h @@ -42,16 +42,16 @@ /** * Shape represented by a series of control points. */ -class Shape +class MShape { public: - typedef std::tr1::shared_ptr ptr; + typedef std::tr1::shared_ptr ptr; - Shape() {} - Shape(QVector vertices_) : + MShape() {} + MShape(QVector vertices_) : vertices(vertices_) {} - virtual ~Shape() {} + virtual ~MShape() {} virtual void build() {} @@ -105,10 +105,10 @@ protected: /** * This class represents a simple polygon (ie. the control points are vertices). */ -class Polygon : public Shape { +class Polygon : public MShape { public: Polygon() {} - Polygon(QVector vertices_) : Shape(vertices_) {} + Polygon(QVector vertices_) : MShape(vertices_) {} virtual ~Polygon() {} virtual QPolygonF toPolygon() const; @@ -246,7 +246,7 @@ protected: void _reorderVertices(); }; -class Ellipse : public Shape { +class Ellipse : public MShape { public: Ellipse() {} Ellipse(QPointF p1, QPointF p2, QPointF p3, QPointF p4, QPointF p5) @@ -284,13 +284,13 @@ public: QVector2D vFromCenter = QVector2D(getVertex(1)) - center; const QVector2D& projection = QVector2D::dotProduct( vFromCenter, vAxisNormalized ) * vAxisNormalized; - Shape::setVertex(1, (center + projection).toPointF()); - Shape::setVertex(3, (center - projection).toPointF()); + MShape::setVertex(1, (center + projection).toPointF()); + MShape::setVertex(3, (center - projection).toPointF()); if (hasCenterControl()) { // Clip control point. - Shape::setVertex(4, clipInside(getVertex(4))); + MShape::setVertex(4, clipInside(getVertex(4))); } } diff --git a/SourceGLCanvas.cpp b/SourceGLCanvas.cpp index 5a2d127..a567c0c 100644 --- a/SourceGLCanvas.cpp +++ b/SourceGLCanvas.cpp @@ -28,7 +28,7 @@ SourceGLCanvas::SourceGLCanvas(MainWindow* mainWindow, QWidget* parent) { } -Shape* SourceGLCanvas::getShapeFromMappingId(uid mappingId) +MShape* SourceGLCanvas::getShapeFromMappingId(uid mappingId) { if (mappingId == NULL_UID) return NULL; diff --git a/SourceGLCanvas.h b/SourceGLCanvas.h index 60161f1..88b08da 100644 --- a/SourceGLCanvas.h +++ b/SourceGLCanvas.h @@ -35,7 +35,7 @@ public: SourceGLCanvas(MainWindow* mainWindow, QWidget* parent = 0); // virtual ~SourceGLCanvas() {} - virtual Shape* getShapeFromMappingId(uid mappingId); + virtual MShape* getShapeFromMappingId(uid mappingId); private: virtual void doDraw(QPainter* painter); diff --git a/Util.cpp b/Util.cpp index c459b77..cbcfd95 100644 --- a/Util.cpp +++ b/Util.cpp @@ -165,7 +165,7 @@ void drawControlsVertex(QPainter* painter, const QPointF& vertex, bool selected, painter->drawLine( vertex + QPointF(offset, -offset), vertex + QPointF(-offset, offset) ); } -void drawControlsVertices(QPainter* painter, const QList* selectedVertices, const Shape& shape) +void drawControlsVertices(QPainter* painter, const QList* selectedVertices, const MShape& shape) { if (!selectedVertices) for (int i=0; i* selectedVertices, const Shape& shape); +void drawControlsVertices(QPainter* painter, const QList* selectedVertices, const MShape& shape); void drawControlsEllipse(QPainter* painter, const QList* selectedVertices, const Ellipse& ellipse); void drawControlsQuad(QPainter* painter, const QList* selectedVertices, const Quad& quad); void drawControlsMesh(QPainter* painter, const QList* selectedVertices, const Mesh& mesh); From cbfe3981c0a9e822e144aad2c5505654fe6aacad Mon Sep 17 00:00:00 2001 From: Tats Date: Fri, 31 Oct 2014 17:04:28 -0400 Subject: [PATCH 02/32] Started using QGraphicsView in canvases instead of QGLWidget. --- DestinationGLCanvas.cpp | 92 +++--- DestinationGLCanvas.h | 4 +- MapperGLCanvas.cpp | 600 +++++++++++++++++++++------------------- MapperGLCanvas.h | 62 +++-- SourceGLCanvas.cpp | 48 ++-- SourceGLCanvas.h | 4 +- 6 files changed, 420 insertions(+), 390 deletions(-) diff --git a/DestinationGLCanvas.cpp b/DestinationGLCanvas.cpp index 43b1e96..a9894e5 100644 --- a/DestinationGLCanvas.cpp +++ b/DestinationGLCanvas.cpp @@ -35,50 +35,50 @@ MShape* DestinationGLCanvas::getShapeFromMappingId(uid mappingId) return getMainWindow()->getMappingManager().getMappingById(mappingId)->getShape().get(); } -void DestinationGLCanvas::doDraw(QPainter* painter) -{ - glPushMatrix(); - - // Draw the mappings. - QVector mappings = getMainWindow()->getMappingManager().getVisibleMappings(); - for (QVector::const_iterator it = mappings.begin(); it != mappings.end(); ++it) - { - painter->save(); - getMainWindow()->getMapperByMappingId((*it)->getId())->draw(painter); - painter->restore(); - } - - // Draw the controls of current mapping. - if (displayControls() && - getMainWindow()->hasCurrentMapping() && - getCurrentShape() != NULL) - { - painter->save(); - const Mapper::ptr& mapper = getMainWindow()->getMapperByMappingId(getMainWindow()->getCurrentMappingId()); - if (hasActiveVertex()) { - QList selectedVertices; - selectedVertices.append(getActiveVertexIndex()); - mapper->drawControls(painter, &selectedVertices); - } - else - mapper->drawControls(painter); - painter->restore(); - } - - glPopMatrix(); - - // Display crosshair cursor. - if (_displayCrosshair) - { - const QPoint& cursorPosition = this->mapFromGlobal(QCursor::pos()); - const QRect& geo = geometry(); - if (geo.contains(cursorPosition)) - { - painter->setPen(MM::CONTROL_COLOR); - painter->drawLine(cursorPosition.x(), 0, cursorPosition.x(), geo.height()); - painter->drawLine(0, cursorPosition.y(), geo.width(), cursorPosition.y()); - } - } - -} +//void DestinationGLCanvas::doDraw(QPainter* painter) +//{ +// glPushMatrix(); +// +// // Draw the mappings. +// QVector mappings = getMainWindow()->getMappingManager().getVisibleMappings(); +// for (QVector::const_iterator it = mappings.begin(); it != mappings.end(); ++it) +// { +// painter->save(); +// getMainWindow()->getMapperByMappingId((*it)->getId())->draw(painter); +// painter->restore(); +// } +// +// // Draw the controls of current mapping. +// if (displayControls() && +// getMainWindow()->hasCurrentMapping() && +// getCurrentShape() != NULL) +// { +// painter->save(); +// const Mapper::ptr& mapper = getMainWindow()->getMapperByMappingId(getMainWindow()->getCurrentMappingId()); +// if (hasActiveVertex()) { +// QList selectedVertices; +// selectedVertices.append(getActiveVertexIndex()); +// mapper->drawControls(painter, &selectedVertices); +// } +// else +// mapper->drawControls(painter); +// painter->restore(); +// } +// +// glPopMatrix(); +// +// // Display crosshair cursor. +// if (_displayCrosshair) +// { +// const QPoint& cursorPosition = this->mapFromGlobal(QCursor::pos()); +// const QRect& geo = geometry(); +// if (geo.contains(cursorPosition)) +// { +// painter->setPen(MM::CONTROL_COLOR); +// painter->drawLine(cursorPosition.x(), 0, cursorPosition.x(), geo.height()); +// painter->drawLine(0, cursorPosition.y(), geo.width(), cursorPosition.y()); +// } +// } +// +//} diff --git a/DestinationGLCanvas.h b/DestinationGLCanvas.h index 4d99c09..8057480 100644 --- a/DestinationGLCanvas.h +++ b/DestinationGLCanvas.h @@ -32,7 +32,7 @@ class DestinationGLCanvas: public MapperGLCanvas public: DestinationGLCanvas(MainWindow* mainWindow, QWidget* parent = 0, const QGLWidget * shareWidget = 0); -// virtual ~DestinationGLCanvas(); + virtual ~DestinationGLCanvas() {} virtual MShape* getShapeFromMappingId(uid mappingId); @@ -41,7 +41,7 @@ public: } private: - virtual void doDraw(QPainter* painter); +// virtual void doDraw(QPainter* painter); bool _displayCrosshair; }; diff --git a/MapperGLCanvas.cpp b/MapperGLCanvas.cpp index b7ee0e3..dbef742 100644 --- a/MapperGLCanvas.cpp +++ b/MapperGLCanvas.cpp @@ -23,7 +23,7 @@ #include "MainWindow.h" MapperGLCanvas::MapperGLCanvas(MainWindow* mainWindow, QWidget* parent, const QGLWidget * shareWidget) - : QGLWidget(QGLFormat(QGL::SampleBuffers), parent, shareWidget), + : QGraphicsView(parent), _mainWindow(mainWindow), _mousePressedOnVertex(false), _activeVertex(NO_VERTEX), @@ -32,305 +32,327 @@ MapperGLCanvas::MapperGLCanvas(MainWindow* mainWindow, QWidget* parent, const QG _displayControls(true), _stickyVertices(true) { + setDragMode(QGraphicsView::RubberBandDrag); + setRenderHint(QPainter::Antialiasing, true); + setRenderHint(QPainter::TextAntialiasing, true); + + //setFrameStyle(Sunken | StyledPanel); + // TODO: check this + // setOptimizationFlags(QGraphicsView::DontSavePainterState); + //setViewportUpdateMode(QGraphicsView::FullViewportUpdate); + setTransformationAnchor(QGraphicsView::AnchorUnderMouse); + + resetTransform(); + // setAcceptDrops(true); + + // Render with OpenGL. + setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers), this, shareWidget)); + + // TODO: do we need to delete scene (or call new QGraphicsScene(this)?) +// _scene = new QGraphicsScene; + setScene(new QGraphicsScene); + + // Black background. + scene()->setBackgroundBrush(Qt::black); } -void MapperGLCanvas::initializeGL() -{ - //qDebug() << "initializeGL" << endl; - // Clear to black. - qglClearColor(Qt::black); - // Antialiasing options. - //QGLWidget::setFormat(QGLFormat(QGL::SampleBuffers)); -} +//void MapperGLCanvas::initializeGL() +//{ +// //qDebug() << "initializeGL" << endl; +// // Clear to black. +// qglClearColor(Qt::black); +// // Antialiasing options. +// //QGLWidget::setFormat(QGLFormat(QGL::SampleBuffers)); +//} +// +//void MapperGLCanvas::resizeGL(int width, int height) +//{ +// glViewport(0, 0, width, height); +// glMatrixMode (GL_PROJECTION); +// glLoadIdentity (); +// glOrtho ( +// 0.0f, (GLfloat) width, // left, right +// (GLfloat) height, 0.0f, // bottom, top +// -1.0, 1.0f); +// glMatrixMode (GL_MODELVIEW); +// //qDebug() << "done resizing" << endl; +//} +// +//void MapperGLCanvas::paintGL() +//{ +//} +// +//void MapperGLCanvas::draw(QPainter* painter) +//{ +// //qDebug() << "draw mappergl" << endl; +// enterDraw(painter); +// doDraw(painter); +// exitDraw(painter); +// // qDebug() << "done." << endl; +//} -void MapperGLCanvas::resizeGL(int width, int height) -{ - glViewport(0, 0, width, height); - glMatrixMode (GL_PROJECTION); - glLoadIdentity (); - glOrtho ( - 0.0f, (GLfloat) width, // left, right - (GLfloat) height, 0.0f, // bottom, top - -1.0, 1.0f); - glMatrixMode (GL_MODELVIEW); - //qDebug() << "done resizing" << endl; -} +//void MapperGLCanvas::enterDraw(QPainter* painter) +//{ +// painter->beginNativePainting(); +// +// // Clear to black. +// glClearColor(0.0, 0.0, 0.0, 1.0); +// +// // Clear buffer. +// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); +// +// painter->endNativePainting(); +// +// // Antialiasing. +// painter->setRenderHint(QPainter::Antialiasing); +// //painter->setBackground(Qt::black); +// painter->setPen(Qt::NoPen); +// painter->setBrush(Qt::NoBrush); +//} +// +//void MapperGLCanvas::exitDraw(QPainter* painter) +//{ +// Q_UNUSED(painter); +//} -void MapperGLCanvas::paintGL() -{ -} - -void MapperGLCanvas::draw(QPainter* painter) -{ - //qDebug() << "draw mappergl" << endl; - enterDraw(painter); - doDraw(painter); - exitDraw(painter); - // qDebug() << "done." << endl; -} - -void MapperGLCanvas::enterDraw(QPainter* painter) -{ - painter->beginNativePainting(); - - // Clear to black. - glClearColor(0.0, 0.0, 0.0, 1.0); - - // Clear buffer. - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - painter->endNativePainting(); - - // Antialiasing. - painter->setRenderHint(QPainter::Antialiasing); - //painter->setBackground(Qt::black); - painter->setPen(Qt::NoPen); - painter->setBrush(Qt::NoBrush); -} - -void MapperGLCanvas::exitDraw(QPainter* painter) -{ - Q_UNUSED(painter); -} - -Shape* MapperGLCanvas::getCurrentShape() -{ - return getShapeFromMappingId(getMainWindow()->getCurrentMappingId()); -} - -void MapperGLCanvas::mousePressEvent(QMouseEvent* event) -{ - int i; - int dist; - int minDistance; - - bool mousePressedOnSomething = false; - - _mousePressedPosition = event->pos(); - - // Note: we compare with the square value for fastest computation of the distance - minDistance = MM::VERTEX_SELECT_RADIUS * MM::VERTEX_SELECT_RADIUS; - - // Drag the closest vertex - if (event->buttons() & Qt::LeftButton) - { - Shape* shape = getCurrentShape(); - if (shape) - { - // find the ID of the nearest vertex: (from the selected shape) - for (i = 0; i < shape->nVertices(); i++) - { - dist = distSq(_mousePressedPosition, shape->getVertex(i)); // squared distance - if (dist < minDistance) - { - _activeVertex = i; - minDistance = dist; - _mousePressedOnVertex = true; - mousePressedOnSomething = true; - } - } - } - } - - if (mousePressedOnSomething) - return; - - // Select a shape with a click. - if (event->buttons() & Qt::LeftButton) - { - Shape* orig = getCurrentShape(); - MappingManager manager = getMainWindow()->getMappingManager(); - QVector mappings = manager.getVisibleMappings(); - for (QVector::const_iterator it = mappings.end() - 1; it >= mappings.begin(); --it) - { - Shape *shape = getShapeFromMappingId((*it)->getId()); - // Mouse pressed on a shape. - if (shape && shape->includesPoint(_mousePressedPosition)) - { - mousePressedOnSomething = true; - // Deselect vertices. - deselectVertices(); - // Change mapping. - if (shape != orig) - { - getMainWindow()->setCurrentMapping((*it)->getId()); - } - break; - } - } - - // Grab the shape. - if (orig && orig->includesPoint(_mousePressedPosition)) - { - _shapeGrabbed = true; - _shapeFirstGrab = true; - } - } - - if (mousePressedOnSomething) - return; - - // Deactivate. - deselectAll(); -} - -void MapperGLCanvas::mouseReleaseEvent(QMouseEvent* event) -{ - Q_UNUSED(event); -// // Click on vertex ==> select the vertex. -// if ((event->buttons() & Qt::LeftButton) && _mousePressedOnVertex) +//Shape* MapperGLCanvas::getCurrentShape() +//{ +// return getShapeFromMappingId(getMainWindow()->getCurrentMappingId()); +//} +// +//void MapperGLCanvas::mousePressEvent(QMouseEvent* event) +//{ +// int i; +// int dist; +// int minDistance; +// +// bool mousePressedOnSomething = false; +// +// _mousePressedPosition = event->pos(); +// +// // Note: we compare with the square value for fastest computation of the distance +// minDistance = MM::VERTEX_SELECT_RADIUS * MM::VERTEX_SELECT_RADIUS; +// +// // Drag the closest vertex +// if (event->buttons() & Qt::LeftButton) // { -// } - _mousePressedOnVertex = false; - _shapeGrabbed = false; -} - -void MapperGLCanvas::mouseMoveEvent(QMouseEvent* event) -{ - if (_mousePressedOnVertex) - { - // std::cout << "Move event " << std::endl; - Shape* shape = getCurrentShape(); - if (shape && _activeVertex != NO_VERTEX) - { - QPointF p = shape->getVertex(_activeVertex); - // Set point to mouse coordinates. - p.setX(event->x()); - p.setY(event->y()); - - // Stick to vertices. - if (stickyVertices()) - glueVertex(shape, &p); - shape->setVertex(_activeVertex, p); - - update(); - emit shapeChanged(getCurrentShape()); - } - } - else if (_shapeGrabbed) - { - // std::cout << "Move event " << std::endl; - Shape* shape = getCurrentShape(); - static QPointF prevMousePosition(0,0); // point that keeps track of last position of the mouse - if (shape) - { - if (!_shapeFirstGrab) - { - shape->translate(event->x() - prevMousePosition.x(), event->y() - prevMousePosition.y()); - update(); - emit shapeChanged(getCurrentShape()); - } - else - _shapeFirstGrab = false; - } - // Update previous mouse position. - prevMousePosition.setX( event->x() ); - prevMousePosition.setY( event->y() ); - } -} - -void MapperGLCanvas::keyPressEvent(QKeyEvent* event) -{ - // Checks if the key has been handled by this function or needs to be deferred to superclass. - bool handledKey = false; - - // Active vertex selected. - if (hasActiveVertex()) - { - Shape* shape = getCurrentShape(); - QPointF p = shape->getVertex(_activeVertex); - handledKey = true; - switch (event->key()) { - // TODO: key tab should switch to next vertex: not working because somehow caught at a higher level - // to switch between frames of the layout -// case Qt::Key_Tab: -// if (shape) -// _activeVertex = (_activeVertex + 1) % shape->nVertices(); -// p = shape->getVertex(_activeVertex); // reset to new vertex -// qDebug() << "New active vertex : " << _activeVertex << endl; -// break; - // Handle pixel-wise adjustments of vertex. - case Qt::Key_Up: - p.ry()--; - break; - case Qt::Key_Down: - p.ry()++; - break; - case Qt::Key_Right: - p.rx()++; - break; - case Qt::Key_Left: - p.rx()--; - break; - default: - handledKey = false; - break; - } - // TODO: this will always be called even if no arrow key has been pressed (small performance issue). - shape->setVertex(_activeVertex, p); - update(); - emit shapeChanged(getCurrentShape()); - } - - // Defer unhandled keys to parent. - if (!handledKey) - { - QWidget::keyPressEvent(event); - } - -// std::cout << "Key pressed" << std::endl; -// int xMove = 0; -// int yMove = 0; -// switch (event->key()) { -// case Qt::Key_Tab: -// if (event->modifiers() & Qt::ControlModifier) -// switchImage( (Common::getCurrentSourceId() + 1) % Common::nImages()); -// else +// Shape* shape = getCurrentShape(); +// if (shape) // { -// Quad& quad = getQuad(); -// _active_vertex = (_active_vertex + 1 ) % 4; +// // find the ID of the nearest vertex: (from the selected shape) +// for (i = 0; i < shape->nVertices(); i++) +// { +// dist = distSq(_mousePressedPosition, shape->getVertex(i)); // squared distance +// if (dist < minDistance) +// { +// _activeVertex = i; +// minDistance = dist; +// _mousePressedOnVertex = true; +// mousePressedOnSomething = true; +// } +// } // } -// break; -// case Qt::Key_Up: -// yMove = -1; -// break; -// case Qt::Key_Down: -// yMove = +1; -// break; -// case Qt::Key_Left: -// xMove = -1; -// break; -// case Qt::Key_Right: -// xMove = +1; -// break; -// default: -// std::cerr << "Unhandled key" << std::endl; -// QWidget::keyPressEvent(event); -// break; // } // -// Quad& quad = getQuad(); -// Point *p = quad.getVertex(_active_vertex); -// p->x += xMove; -// p->y += yMove; -// quad.setVertex(_active_vertex, p); +// if (mousePressedOnSomething) +// return; // -// update(); +// // Select a shape with a click. +// if (event->buttons() & Qt::LeftButton) +// { +// Shape* orig = getCurrentShape(); +// MappingManager manager = getMainWindow()->getMappingManager(); +// QVector mappings = manager.getVisibleMappings(); +// for (QVector::const_iterator it = mappings.end() - 1; it >= mappings.begin(); --it) +// { +// Shape *shape = getShapeFromMappingId((*it)->getId()); +// // Mouse pressed on a shape. +// if (shape && shape->includesPoint(_mousePressedPosition)) +// { +// mousePressedOnSomething = true; +// // Deselect vertices. +// deselectVertices(); +// // Change mapping. +// if (shape != orig) +// { +// getMainWindow()->setCurrentMapping((*it)->getId()); +// } +// break; +// } +// } // -// emit quadChanged(); -} - -void MapperGLCanvas::paintEvent(QPaintEvent* /* event */) -{ - makeCurrent(); - - QPainter painter(this); - painter.setRenderHint(QPainter::Antialiasing); - - draw(&painter); - - painter.end(); -} +// // Grab the shape. +// if (orig && orig->includesPoint(_mousePressedPosition)) +// { +// _shapeGrabbed = true; +// _shapeFirstGrab = true; +// } +// } +// +// if (mousePressedOnSomething) +// return; +// +// // Deactivate. +// deselectAll(); +//} +// +//void MapperGLCanvas::mouseReleaseEvent(QMouseEvent* event) +//{ +// Q_UNUSED(event); +//// // Click on vertex ==> select the vertex. +//// if ((event->buttons() & Qt::LeftButton) && _mousePressedOnVertex) +//// { +//// } +// _mousePressedOnVertex = false; +// _shapeGrabbed = false; +//} +// +//void MapperGLCanvas::mouseMoveEvent(QMouseEvent* event) +//{ +// if (_mousePressedOnVertex) +// { +// // std::cout << "Move event " << std::endl; +// Shape* shape = getCurrentShape(); +// if (shape && _activeVertex != NO_VERTEX) +// { +// QPointF p = shape->getVertex(_activeVertex); +// // Set point to mouse coordinates. +// p.setX(event->x()); +// p.setY(event->y()); +// +// // Stick to vertices. +// if (stickyVertices()) +// glueVertex(shape, &p); +// shape->setVertex(_activeVertex, p); +// +// update(); +// emit shapeChanged(getCurrentShape()); +// } +// } +// else if (_shapeGrabbed) +// { +// // std::cout << "Move event " << std::endl; +// Shape* shape = getCurrentShape(); +// static QPointF prevMousePosition(0,0); // point that keeps track of last position of the mouse +// if (shape) +// { +// if (!_shapeFirstGrab) +// { +// shape->translate(event->x() - prevMousePosition.x(), event->y() - prevMousePosition.y()); +// update(); +// emit shapeChanged(getCurrentShape()); +// } +// else +// _shapeFirstGrab = false; +// } +// // Update previous mouse position. +// prevMousePosition.setX( event->x() ); +// prevMousePosition.setY( event->y() ); +// } +//} +// +//void MapperGLCanvas::keyPressEvent(QKeyEvent* event) +//{ +// // Checks if the key has been handled by this function or needs to be deferred to superclass. +// bool handledKey = false; +// +// // Active vertex selected. +// if (hasActiveVertex()) +// { +// Shape* shape = getCurrentShape(); +// QPointF p = shape->getVertex(_activeVertex); +// handledKey = true; +// switch (event->key()) { +// // TODO: key tab should switch to next vertex: not working because somehow caught at a higher level +// // to switch between frames of the layout +//// case Qt::Key_Tab: +//// if (shape) +//// _activeVertex = (_activeVertex + 1) % shape->nVertices(); +//// p = shape->getVertex(_activeVertex); // reset to new vertex +//// qDebug() << "New active vertex : " << _activeVertex << endl; +//// break; +// // Handle pixel-wise adjustments of vertex. +// case Qt::Key_Up: +// p.ry()--; +// break; +// case Qt::Key_Down: +// p.ry()++; +// break; +// case Qt::Key_Right: +// p.rx()++; +// break; +// case Qt::Key_Left: +// p.rx()--; +// break; +// default: +// handledKey = false; +// break; +// } +// // TODO: this will always be called even if no arrow key has been pressed (small performance issue). +// shape->setVertex(_activeVertex, p); +// update(); +// emit shapeChanged(getCurrentShape()); +// } +// +// // Defer unhandled keys to parent. +// if (!handledKey) +// { +// QWidget::keyPressEvent(event); +// } +// +//// std::cout << "Key pressed" << std::endl; +//// int xMove = 0; +//// int yMove = 0; +//// switch (event->key()) { +//// case Qt::Key_Tab: +//// if (event->modifiers() & Qt::ControlModifier) +//// switchImage( (Common::getCurrentSourceId() + 1) % Common::nImages()); +//// else +//// { +//// Quad& quad = getQuad(); +//// _active_vertex = (_active_vertex + 1 ) % 4; +//// } +//// break; +//// case Qt::Key_Up: +//// yMove = -1; +//// break; +//// case Qt::Key_Down: +//// yMove = +1; +//// break; +//// case Qt::Key_Left: +//// xMove = -1; +//// break; +//// case Qt::Key_Right: +//// xMove = +1; +//// break; +//// default: +//// std::cerr << "Unhandled key" << std::endl; +//// QWidget::keyPressEvent(event); +//// break; +//// } +//// +//// Quad& quad = getQuad(); +//// Point *p = quad.getVertex(_active_vertex); +//// p->x += xMove; +//// p->y += yMove; +//// quad.setVertex(_active_vertex, p); +//// +//// update(); +//// +//// emit quadChanged(); +//} +// +//void MapperGLCanvas::paintEvent(QPaintEvent* /* event */) +//{ +// makeCurrent(); +// +// QPainter painter(this); +// painter.setRenderHint(QPainter::Antialiasing); +// +// draw(&painter); +// +// painter.end(); +//} void MapperGLCanvas::updateCanvas() { diff --git a/MapperGLCanvas.h b/MapperGLCanvas.h index 93a3191..f8920ef 100644 --- a/MapperGLCanvas.h +++ b/MapperGLCanvas.h @@ -21,6 +21,8 @@ #define MAPPERGLCANVAS_H_ #include +#include +#include #include #include @@ -35,7 +37,7 @@ class MainWindow; * Mother class for OpenGL canvases that allow the display and controls of shapes and vertices. * Provides common functionality to both main sublasses: SourceGLCanvas and DestinationGLCanvas. */ -class MapperGLCanvas: public QGLWidget +class MapperGLCanvas: public QGraphicsView { Q_OBJECT @@ -64,6 +66,8 @@ public: /// Returns pointer to main window. MainWindow* getMainWindow() const { return _mainWindow; } +// QGLWidget* getViewport() const { return _viewport; } + /// Returns true iff we should display the controls. bool displayControls() const { return _displayControls; } @@ -77,40 +81,44 @@ public: int getActiveVertexIndex() const { return _activeVertex; } protected: - void initializeGL(); - void resizeGL(int width, int height); - void paintGL(); +// void initializeGL(); +// void resizeGL(int width, int height); +// void paintGL(); - void keyPressEvent(QKeyEvent* event); - void mousePressEvent(QMouseEvent* event); - void mouseMoveEvent(QMouseEvent* event); - void mouseReleaseEvent(QMouseEvent* event); - void paintEvent(QPaintEvent* event); +// void keyPressEvent(QKeyEvent* event); +// void mousePressEvent(QMouseEvent* event); +// void mouseMoveEvent(QMouseEvent* event); +// void mouseReleaseEvent(QMouseEvent* event); +// void paintEvent(QPaintEvent* event); protected: - /** - * Draws the shapes and controls over the canvas. This method calls: - * - * enterDraw(painter); - * doDraw(painter); - * exitDraw(painter); - * - */ - void draw(QPainter* painter); - - /// Performs initalizations before drawing. - void enterDraw(QPainter* painter); - - /// Performs the drawing (implemented by subclasses). - virtual void doDraw(QPainter* painter) = 0; - - /// Performs last drawing actions before exiting draw(QPainter*). - void exitDraw(QPainter* painter); +// /** +// * Draws the shapes and controls over the canvas. This method calls: +// * +// * enterDraw(painter); +// * doDraw(painter); +// * exitDraw(painter); +// * +// */ +// void draw(QPainter* painter); +// +// /// Performs initalizations before drawing. +// void enterDraw(QPainter* painter); +// +// /// Performs the drawing (implemented by subclasses). +// virtual void doDraw(QPainter* painter) = 0; +// +// /// Performs last drawing actions before exiting draw(QPainter*). +// void exitDraw(QPainter* painter); private: // Pointer to main window. MainWindow* _mainWindow; +// QGraphicsScene* _scene; + +// QGLWidget* _viewport; + // Last point pressed. QPoint _mousePressedPosition; diff --git a/SourceGLCanvas.cpp b/SourceGLCanvas.cpp index a567c0c..66ddbdc 100644 --- a/SourceGLCanvas.cpp +++ b/SourceGLCanvas.cpp @@ -40,29 +40,29 @@ MShape* SourceGLCanvas::getShapeFromMappingId(uid mappingId) return mapping->getInputShape().get(); } } +// - -void SourceGLCanvas::doDraw(QPainter* painter) -{ - if (getMainWindow()->hasCurrentMapping()) - { - uint mappingId = getMainWindow()->getCurrentMappingId(); - const Mapper::ptr& mapper = getMainWindow()->getMapperByMappingId(mappingId); - painter->save(); - mapper->drawInput(painter); - painter->restore(); - if (displayControls()) - { - painter->save(); - if (hasActiveVertex()) { - QList selectedVertices; - selectedVertices.append(getActiveVertexIndex()); - mapper->drawInputControls(painter, &selectedVertices); - } - else - mapper->drawInputControls(painter); - painter->restore(); - } - } -} +//void SourceGLCanvas::doDraw(QPainter* painter) +//{ +// if (getMainWindow()->hasCurrentMapping()) +// { +// uint mappingId = getMainWindow()->getCurrentMappingId(); +// const Mapper::ptr& mapper = getMainWindow()->getMapperByMappingId(mappingId); +// painter->save(); +// mapper->drawInput(painter); +// painter->restore(); +// if (displayControls()) +// { +// painter->save(); +// if (hasActiveVertex()) { +// QList selectedVertices; +// selectedVertices.append(getActiveVertexIndex()); +// mapper->drawInputControls(painter, &selectedVertices); +// } +// else +// mapper->drawInputControls(painter); +// painter->restore(); +// } +// } +//} diff --git a/SourceGLCanvas.h b/SourceGLCanvas.h index 88b08da..ec92045 100644 --- a/SourceGLCanvas.h +++ b/SourceGLCanvas.h @@ -33,12 +33,12 @@ class SourceGLCanvas: public MapperGLCanvas public: SourceGLCanvas(MainWindow* mainWindow, QWidget* parent = 0); -// virtual ~SourceGLCanvas() {} + virtual ~SourceGLCanvas() {} virtual MShape* getShapeFromMappingId(uid mappingId); private: - virtual void doDraw(QPainter* painter); +// virtual void doDraw(QPainter* painter); }; From 6843ecd8824204ce5a14671b1d0fa499f5acafcb Mon Sep 17 00:00:00 2001 From: Tats Date: Fri, 31 Oct 2014 17:05:32 -0400 Subject: [PATCH 03/32] Integrated view items for selecing and dragging shapes in the QGraphicsView; not completely tied to model yet. --- MainWindow.cpp | 10 +- Mapper.cpp | 42 ++++---- Mapper.h | 271 ++++++++++++++++++++++++++++++++++++++++++++----- Util.cpp | 2 +- 4 files changed, 278 insertions(+), 47 deletions(-) diff --git a/MainWindow.cpp b/MainWindow.cpp index d405ddc..18f638e 100644 --- a/MainWindow.cpp +++ b/MainWindow.cpp @@ -976,12 +976,12 @@ void MainWindow::createLayout() sourceCanvas->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); sourceCanvas->setMinimumSize(CANVAS_MINIMUM_WIDTH, CANVAS_MINIMUM_HEIGHT); - destinationCanvas = new DestinationGLCanvas(this, 0, sourceCanvas); + destinationCanvas = new DestinationGLCanvas(this, 0, (QGLWidget*)sourceCanvas->viewport()); destinationCanvas->setFocusPolicy(Qt::ClickFocus); destinationCanvas->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); destinationCanvas->setMinimumSize(CANVAS_MINIMUM_WIDTH, CANVAS_MINIMUM_HEIGHT); - outputWindow = new OutputGLWindow(this, this, sourceCanvas); + outputWindow = new OutputGLWindow(this, this, (QGLWidget*)sourceCanvas->viewport()); outputWindow->setVisible(true); // Source changed -> change destination @@ -1890,6 +1890,12 @@ void MainWindow::addMappingItem(uid mappingId) mappingList->insertItem(0, item); mappingList->setCurrentItem(item); + // Add items to scenes. + if (mapper->getInputGraphicsItem()) + sourceCanvas->scene()->addItem(mapper->getInputGraphicsItem()); + if (mapper->getGraphicsItem()) + destinationCanvas->scene()->addItem(mapper->getGraphicsItem()); + // Window was modified. windowModified(); } diff --git a/Mapper.cpp b/Mapper.cpp index c9766c5..2f3e1cd 100644 --- a/Mapper.cpp +++ b/Mapper.cpp @@ -21,8 +21,24 @@ #include "Mapper.h" #include "MainWindow.h" +void ShapeGraphicsItem::_createVertices() +{ + // rect offset + qreal offset = MM::VERTEX_SELECT_RADIUS / 2.0; + for (int i=0; i<_shape->nVertices(); i++) + { + // XXX is this freed by parent? + VertexGraphicsItem* child = new VertexGraphicsItem(i); + QPointF pos = _shape->getVertex(i); + child->setRect(pos.x()-offset, pos.y()-offset, MM::VERTEX_SELECT_RADIUS, MM::VERTEX_SELECT_RADIUS); + child->setParentItem(this); + } +} + Mapper::Mapper(Mapping::ptr mapping) - : _mapping(mapping) + : _mapping(mapping), + _graphicsItem(NULL), + _inputGraphicsItem(NULL) { outputShape = mapping->getShape(); Q_CHECK_PTR(outputShape); @@ -55,7 +71,10 @@ Mapper::Mapper(Mapping::ptr mapping) Mapper::~Mapper() { - delete _propertyBrowser; + if (_propertyBrowser) + delete _propertyBrowser; + if (_graphicsItem) + delete _graphicsItem; } QWidget* Mapper::getPropertiesEditor() @@ -125,23 +144,6 @@ void ColorMapper::updatePaint() Q_CHECK_PTR(color); } -void PolygonColorMapper::draw(QPainter* painter) -{ - painter->setRenderHint(QPainter::Antialiasing); - painter->setPen(Qt::NoPen); - painter->setBrush(color->getColor()); - - // Draw shape as polygon. - Polygon* poly = (Polygon*)_mapping->getShape().get(); - painter->drawPolygon(poly->toPolygon()); -} - -void PolygonColorMapper::drawControls(QPainter* painter, const QList* selectedVertices) -{ - Polygon* poly = (Polygon*)_mapping->getShape().get(); - Util::drawControlsPolygon(painter, selectedVertices, *poly); -} - MeshColorMapper::MeshColorMapper(Mapping::ptr mapping) : ColorMapper(mapping) { // Add mesh sub property. @@ -363,6 +365,8 @@ void PolygonTextureMapper::drawInputControls(QPainter* painter, const QList TriangleTextureMapper::TriangleTextureMapper(std::tr1::shared_ptr mapping) : PolygonTextureMapper(mapping) { + _graphicsItem = new TriangleTextureGraphicsItem(_mapping, true); + _inputGraphicsItem = new TriangleTextureGraphicsItem(_mapping, false); } void TriangleTextureMapper::_doDraw(QPainter* painter) diff --git a/Mapper.h b/Mapper.h index e25de47..402261f 100644 --- a/Mapper.h +++ b/Mapper.h @@ -76,25 +76,16 @@ public: /// Returns a pointer to the properties editor for that mapper. virtual QWidget* getPropertiesEditor(); - /// Draws the output. - virtual void draw(QPainter* painter) = 0; - - /// Draws the output controls. - virtual void drawControls(QPainter* painter, const QList* selectedVertices=0) = 0; - - /// Draws the input (source) (if applicable). - virtual void drawInput(QPainter* painter) { Q_UNUSED(painter); } - - /// Draws the input (source) controls (if applicable). - virtual void drawInputControls(QPainter* painter, const QList* selectedVertices=0) - { - Q_UNUSED(painter); - Q_UNUSED(selectedVertices); + virtual QGraphicsItem* getGraphicsItem() { + return _graphicsItem; + } + virtual QGraphicsItem* getInputGraphicsItem() { + return _inputGraphicsItem; } public slots: virtual void setValue(QtProperty* property, const QVariant& value); - virtual void updateShape(Shape* shape) { Q_UNUSED(shape); } + virtual void updateShape(MShape* shape) { Q_UNUSED(shape); } virtual void updatePaint() {} signals: @@ -108,15 +99,246 @@ protected: QtProperty* _topItem; QtProperty* _outputItem; - std::map > _propertyToVertex; + std::map > _propertyToVertex; + + QGraphicsItem* _graphicsItem; + QGraphicsItem* _inputGraphicsItem; // FIXME: use typedefs, member of the class for type names that are too long to type: - std::tr1::shared_ptr outputShape; + MShape::ptr outputShape; - virtual void _buildShapeProperty(QtProperty* shapeItem, Shape* shape); - virtual void _updateShapeProperty(QtProperty* shapeItem, Shape* shape); + virtual void _buildShapeProperty(QtProperty* shapeItem, MShape* shape); + virtual void _updateShapeProperty(QtProperty* shapeItem, MShape* shape); }; +class VertexGraphicsItem; + +class ShapeGraphicsItem : public QGraphicsItem +{ + Q_DECLARE_TR_FUNCTIONS(ShapeGraphicsItem) + +public: + ShapeGraphicsItem(Mapping::ptr mapping, bool output=true) + : _mapping(mapping), _output(output) + { + _shape = output ? _mapping->getShape() : _mapping->getInputShape(); + + setFlags(ItemIsMovable | ItemIsSelectable); + + _createVertices(); + } + virtual ~ShapeGraphicsItem() {} + + // TODO: dangereux: confusion possible entre shape() et getShape()... + MShape::ptr getShape() const { return _shape; } + + Mapping::ptr getMapping() const { return _mapping; } + + bool isOutput() const { return _output; } + + virtual QRectF boundingRect() const = 0; +// virtual QPainterPath shape() const; +// virtual void paint(QPainter *painter, +// const QStyleOptionGraphicsItem *option, QWidget *widget); + +protected: + virtual void _createVertices(); + + Mapping::ptr _mapping; + MShape::ptr _shape; + bool _output; +}; + +class VertexGraphicsItem : public QGraphicsEllipseItem +{ + Q_DECLARE_TR_FUNCTIONS(VertexGraphicsItem) + +public: + VertexGraphicsItem(int index) : _index(index) { + setFlags(ItemIsMovable | ItemIsSelectable); + } + virtual ~VertexGraphicsItem() {} + + virtual void paint(QPainter *painter, + const QStyleOptionGraphicsItem *option, QWidget *widget) + { + const QPointF& vertex = ((ShapeGraphicsItem*)parentItem())->getShape()->getVertex(_index); + Util::drawControlsVertex(painter, vertex, (option->state & QStyle::State_Selected)); + } + + +protected: + int _index; +}; + +class PolygonColorGraphicsItem : public ShapeGraphicsItem +{ +public: + PolygonColorGraphicsItem(Mapping::ptr mapping, bool output=true) : ShapeGraphicsItem(mapping, output) {} + virtual ~PolygonColorGraphicsItem() {} + + + virtual QRectF boundingRect() const { + return ((Polygon*)_shape.get())->toPolygon().boundingRect(); + } + + virtual QPainterPath shape() const { + QPainterPath path; + path.addPolygon(((Polygon*)_shape.get())->toPolygon()); + return path; + } + + virtual void paint(QPainter *painter, + const QStyleOptionGraphicsItem *option, QWidget *widget) + { + painter->setPen(Qt::NoPen); + painter->setBrush(Qt::red); + painter->setBrush(((Color*)_mapping->getPaint().get())->getColor()); + + // Draw shape as polygon. + Polygon* poly = (Polygon*)_shape.get(); + painter->drawPolygon(poly->toPolygon()); + + if (option->state & QStyle::State_Selected) + { + QList dummySelectedVertices; + Util::drawControlsPolygon(painter, &dummySelectedVertices, *poly); + } + } +}; + +class TextureGraphicsItem : public ShapeGraphicsItem +{ +public: + TextureGraphicsItem(Mapping::ptr mapping, bool output=true) : ShapeGraphicsItem(mapping, output) { + _textureMapping = std::tr1::static_pointer_cast(mapping); + Q_CHECK_PTR(_textureMapping); + + _texture = std::tr1::static_pointer_cast(_textureMapping->getPaint()); + Q_CHECK_PTR(_texture); + + _inputShape = std::tr1::static_pointer_cast(_textureMapping->getInputShape()); + Q_CHECK_PTR(_inputShape); + } + + virtual ~TextureGraphicsItem() {} + + virtual void paint(QPainter *painter, + const QStyleOptionGraphicsItem *option, QWidget *widget) + { + if (_output) + { + // Prepare drawing. + _preDraw(painter); + + // Perform the actual mapping (done by subclasses). + _doDraw(painter, option->state & QStyle::State_Selected); + + // End drawing. + _postDraw(painter); + } + + if (option->state & QStyle::State_Selected) + _drawControls(painter); + } + + virtual void _doDraw(QPainter* painter, bool selected) = 0; + void _preDraw(QPainter* painter) + { + painter->beginNativePainting(); + + // Only works for similar shapes. + // TODO:remettre + //Q_ASSERT( _inputShape->nVertices() == outputShape->nVertices()); + + // Project source texture and sent it to destination. + _texture->update(); + + glEnable (GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, _texture->getTextureId()); + + // Copy bits to texture iff necessary. + _texture->lockMutex(); + if (_texture->bitsHaveChanged()) + { + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, + _texture->getWidth(), _texture->getHeight(), 0, GL_RGBA, + GL_UNSIGNED_BYTE, _texture->getBits()); + } + _texture->unlockMutex(); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + + glColor4f(1.0f, 1.0f, 1.0f, 1.0f); + } + + void _postDraw(QPainter* painter) + { + glDisable(GL_TEXTURE_2D); + + painter->endNativePainting(); + } + + virtual void _drawControls(QPainter *painter) + {} + +protected: + std::tr1::shared_ptr _textureMapping; + std::tr1::shared_ptr _texture; + std::tr1::shared_ptr _inputShape; +}; + +class PolygonTextureGraphicsItem : public TextureGraphicsItem +{ +public: + PolygonTextureGraphicsItem(Mapping::ptr mapping, bool output=true) : TextureGraphicsItem(mapping, output) {} + virtual ~PolygonTextureGraphicsItem(){} + + virtual void _drawControls(QPainter* painter) + { + std::tr1::shared_ptr outputPoly = std::tr1::static_pointer_cast(_shape); + QList dummySelectedVertices; + Util::drawControlsPolygon(painter, &dummySelectedVertices, *outputPoly); + } +}; + +class TriangleTextureGraphicsItem : public PolygonTextureGraphicsItem +{ +public: + TriangleTextureGraphicsItem(Mapping::ptr mapping, bool output=true) : PolygonTextureGraphicsItem(mapping, output) {} + virtual ~TriangleTextureGraphicsItem(){} + + virtual QRectF boundingRect() const { + return ((Polygon*)_shape.get())->toPolygon().boundingRect(); + } + + virtual QPainterPath shape() const { + QPainterPath path; + path.addPolygon(((Polygon*)_shape.get())->toPolygon()); + return path; + } + + virtual void _doDraw(QPainter* painter, bool selected) + { + if (_output) + { + glBegin(GL_TRIANGLES); + { + for (int i = 0; i < _inputShape->nVertices(); i++) + { + Util::setGlTexPoint(*_texture, _inputShape->getVertex(i), _shape->getVertex(i)); + } + } + glEnd(); + } + } +}; + + + /** * Draws a color (abstract class). */ @@ -140,11 +362,10 @@ class PolygonColorMapper : public ColorMapper Q_OBJECT public: - PolygonColorMapper(Mapping::ptr mapping) : ColorMapper(mapping) {} + PolygonColorMapper(Mapping::ptr mapping) : ColorMapper(mapping) { + _graphicsItem = new PolygonColorGraphicsItem(mapping, true); + } virtual ~PolygonColorMapper() {} - - virtual void draw(QPainter* painter); - virtual void drawControls(QPainter* painter, const QList* selectedVertices=0); }; class MeshColorMapper : public ColorMapper @@ -199,7 +420,7 @@ public: virtual void drawInputControls(QPainter* painter, const QList* selectedVertices=0) = 0; public slots: - virtual void updateShape(Shape* shape); + virtual void updateShape(MShape* shape); virtual void updatePaint(); protected: @@ -214,7 +435,7 @@ protected: // FIXME: use typedefs, member of the class for type names that are too long to type: std::tr1::shared_ptr textureMapping; std::tr1::shared_ptr texture; - std::tr1::shared_ptr inputShape; + std::tr1::shared_ptr inputShape; }; class PolygonTextureMapper : public TextureMapper diff --git a/Util.cpp b/Util.cpp index cbcfd95..6b4145c 100644 --- a/Util.cpp +++ b/Util.cpp @@ -239,7 +239,7 @@ void drawControlsPolygon(QPainter* painter, const QList* selectedVertices, painter->drawPolygon(polygon.toPolygon()); // Draw control points. - drawControlsVertices(painter, selectedVertices, polygon); + //drawControlsVertices(painter, selectedVertices, polygon); } bool fileExists(const QString& filename) From 965a26edd6587a0caef250d070a683e53edd6e1b Mon Sep 17 00:00:00 2001 From: Tats Date: Sun, 28 Dec 2014 15:58:06 -0500 Subject: [PATCH 04/32] Extremely rough patch for manipulating mapping texture shapes (works only for triangles). --- Mapper.cpp | 40 ++++++++++++++++++++++++++++++++++++++-- Mapper.h | 51 +++++++++++++++++++++++++++++++++++---------------- 2 files changed, 73 insertions(+), 18 deletions(-) diff --git a/Mapper.cpp b/Mapper.cpp index 2f3e1cd..0d0715d 100644 --- a/Mapper.cpp +++ b/Mapper.cpp @@ -21,6 +21,27 @@ #include "Mapper.h" #include "MainWindow.h" +bool ShapeGraphicsItem::sceneEventFilter(QGraphicsItem * watched, QEvent * event) +{ + if (watched == this) + { + _syncShape(); + } + else { + VertexGraphicsItem* child = static_cast(watched); + Q_ASSERT(child); + qreal offset = MM::VERTEX_SELECT_RADIUS / 2.0; + int idx = childItems().indexOf(watched); + Q_ASSERT(idx != -1); + QPointF pos = child->pos(); + qDebug() << "child(" << idx << ") position = " << watched->pos() << " map from " << mapFromItem(watched, watched->pos()) << endl; + _shape->setVertex(idx, pos); +// child->setRect(pos.x()-offset, pos.y()-offset, MM::VERTEX_SELECT_RADIUS, MM::VERTEX_SELECT_RADIUS); + this->update(); + } + return false; +} + void ShapeGraphicsItem::_createVertices() { // rect offset @@ -28,10 +49,25 @@ void ShapeGraphicsItem::_createVertices() for (int i=0; i<_shape->nVertices(); i++) { // XXX is this freed by parent? + QPointF pos = _shape->getVertex(i) - this->pos(); VertexGraphicsItem* child = new VertexGraphicsItem(i); - QPointF pos = _shape->getVertex(i); - child->setRect(pos.x()-offset, pos.y()-offset, MM::VERTEX_SELECT_RADIUS, MM::VERTEX_SELECT_RADIUS); +// child->setPos( pos ); child->setParentItem(this); + child->setPos( pos ); + child->setRect( -MM::VERTEX_SELECT_RADIUS, -MM::VERTEX_SELECT_RADIUS, MM::VERTEX_SELECT_RADIUS*2, MM::VERTEX_SELECT_RADIUS*2); +// child->setRect(pos.x()-offset, pos.y()-offset, MM::VERTEX_SELECT_RADIUS, MM::VERTEX_SELECT_RADIUS); +// qDebug() << "Adding child at " << pos << " " << child->pos(); +// qDebug() << ", after add " << child->pos() << endl; + } +} + +void ShapeGraphicsItem::_syncShape() +{ + QList children = childItems(); + for (int i=0; i<_shape->nVertices(); i++) + { + QGraphicsItem* child = children.at(i); + _shape->setVertex(i, mapFromItem(child, child->pos())); } } diff --git a/Mapper.h b/Mapper.h index 402261f..02a04b6 100644 --- a/Mapper.h +++ b/Mapper.h @@ -124,6 +124,7 @@ public: _shape = output ? _mapping->getShape() : _mapping->getInputShape(); setFlags(ItemIsMovable | ItemIsSelectable); + setFiltersChildEvents(true); _createVertices(); } @@ -141,14 +142,18 @@ public: // virtual void paint(QPainter *painter, // const QStyleOptionGraphicsItem *option, QWidget *widget); + virtual bool sceneEventFilter(QGraphicsItem * watched, QEvent * event); + protected: virtual void _createVertices(); + virtual void _syncShape(); Mapping::ptr _mapping; MShape::ptr _shape; bool _output; }; +/// Graphics item for vertices / control points. class VertexGraphicsItem : public QGraphicsEllipseItem { Q_DECLARE_TR_FUNCTIONS(VertexGraphicsItem) @@ -162,7 +167,9 @@ public: virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { - const QPointF& vertex = ((ShapeGraphicsItem*)parentItem())->getShape()->getVertex(_index); + Q_UNUSED(widget); +// const QPointF& vertex = ((ShapeGraphicsItem*)parentItem())->getShape()->getVertex(_index); + const QPointF vertex(0,0); Util::drawControlsVertex(painter, vertex, (option->state & QStyle::State_Selected)); } @@ -191,6 +198,8 @@ public: virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { + Q_UNUSED(widget); + painter->setPen(Qt::NoPen); painter->setBrush(Qt::red); painter->setBrush(((Color*)_mapping->getPaint().get())->getColor()); @@ -199,11 +208,13 @@ public: Polygon* poly = (Polygon*)_shape.get(); painter->drawPolygon(poly->toPolygon()); - if (option->state & QStyle::State_Selected) - { - QList dummySelectedVertices; - Util::drawControlsPolygon(painter, &dummySelectedVertices, *poly); - } +// if (option->state & QStyle::State_Selected) +// { +// // TODO: drawControlsPolygon()'s selected vertices should be optional +// // ie. disabled if NULL +// QList dummySelectedVertices; +// Util::drawControlsPolygon(painter, &dummySelectedVertices, *poly); +// } } }; @@ -226,6 +237,8 @@ public: virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { + Q_UNUSED(widget); + if (_output) { // Prepare drawing. @@ -238,8 +251,8 @@ public: _postDraw(painter); } - if (option->state & QStyle::State_Selected) - _drawControls(painter); +// if (option->state & QStyle::State_Selected) +// _drawControls(painter); } virtual void _doDraw(QPainter* painter, bool selected) = 0; @@ -283,7 +296,9 @@ public: } virtual void _drawControls(QPainter *painter) - {} + { + Q_UNUSED(painter); + } protected: std::tr1::shared_ptr _textureMapping; @@ -297,12 +312,12 @@ public: PolygonTextureGraphicsItem(Mapping::ptr mapping, bool output=true) : TextureGraphicsItem(mapping, output) {} virtual ~PolygonTextureGraphicsItem(){} - virtual void _drawControls(QPainter* painter) - { - std::tr1::shared_ptr outputPoly = std::tr1::static_pointer_cast(_shape); - QList dummySelectedVertices; - Util::drawControlsPolygon(painter, &dummySelectedVertices, *outputPoly); - } +// virtual void _drawControls(QPainter* painter) +// { +// std::tr1::shared_ptr outputPoly = std::tr1::static_pointer_cast(_shape); +// QList dummySelectedVertices; +// Util::drawControlsPolygon(painter, &dummySelectedVertices, *outputPoly); +// } }; class TriangleTextureGraphicsItem : public PolygonTextureGraphicsItem @@ -317,12 +332,16 @@ public: virtual QPainterPath shape() const { QPainterPath path; - path.addPolygon(((Polygon*)_shape.get())->toPolygon()); + Polygon* poly = static_cast(_shape.get()); + Q_ASSERT(poly); + path.addPolygon(poly->toPolygon()); return path; } virtual void _doDraw(QPainter* painter, bool selected) { + Q_UNUSED(painter); + Q_UNUSED(selected); if (_output) { glBegin(GL_TRIANGLES); From 5cf9294dc7afbdd21b910eca5e6a3fd456b9ade3 Mon Sep 17 00:00:00 2001 From: Tats Date: Sun, 28 Dec 2014 16:16:21 -0500 Subject: [PATCH 05/32] Only update vertex coordinates when control point is actually moved (as opposed to on any event). --- Mapper.cpp | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/Mapper.cpp b/Mapper.cpp index 0d0715d..32a0d5f 100644 --- a/Mapper.cpp +++ b/Mapper.cpp @@ -21,24 +21,25 @@ #include "Mapper.h" #include "MainWindow.h" + bool ShapeGraphicsItem::sceneEventFilter(QGraphicsItem * watched, QEvent * event) { - if (watched == this) + VertexGraphicsItem* child = static_cast(watched); + Q_ASSERT(child); + + // Change vertex in model according to moved item. + if (event->type() == QEvent::GraphicsSceneMouseMove) { - _syncShape(); - } - else { - VertexGraphicsItem* child = static_cast(watched); - Q_ASSERT(child); - qreal offset = MM::VERTEX_SELECT_RADIUS / 2.0; int idx = childItems().indexOf(watched); Q_ASSERT(idx != -1); QPointF pos = child->pos(); - qDebug() << "child(" << idx << ") position = " << watched->pos() << " map from " << mapFromItem(watched, watched->pos()) << endl; _shape->setVertex(idx, pos); -// child->setRect(pos.x()-offset, pos.y()-offset, MM::VERTEX_SELECT_RADIUS, MM::VERTEX_SELECT_RADIUS); - this->update(); } + + // Refresh this shape. + update(); + + // Returns false to allow the child item to process its event. return false; } From 31e315363e664374ec990512e254caa593db9453 Mon Sep 17 00:00:00 2001 From: Tats Date: Sun, 28 Dec 2014 18:01:02 -0500 Subject: [PATCH 06/32] Major refactoring: moved too long methods from Mapper.h to Mapper.cpp and removed unused drawing code from Mapper classes. --- Mapper.cpp | 420 ++++++++++++++++++++++++++++++++++++----------------- Mapper.h | 416 ++++++++++++++++++---------------------------------- 2 files changed, 431 insertions(+), 405 deletions(-) diff --git a/Mapper.cpp b/Mapper.cpp index 32a0d5f..6059405 100644 --- a/Mapper.cpp +++ b/Mapper.cpp @@ -21,6 +21,19 @@ #include "Mapper.h" #include "MainWindow.h" +ShapeGraphicsItem::ShapeGraphicsItem(Mapping::ptr mapping, bool output) + : _mapping(mapping), _output(output) +{ + _shape = output ? _mapping->getShape() : _mapping->getInputShape(); + + setFlags(ItemIsMovable | ItemIsSelectable); + + // Shape filters child (control point) events. + setFiltersChildEvents(true); + + // Create control point graphics items. + _createVertices(); +} bool ShapeGraphicsItem::sceneEventFilter(QGraphicsItem * watched, QEvent * event) { @@ -72,6 +85,190 @@ void ShapeGraphicsItem::_syncShape() } } +QPainterPath PolygonColorGraphicsItem::shape() const +{ + QPainterPath path; + Polygon* poly = static_cast(_shape.get()); + Q_ASSERT(poly); + path.addPolygon(poly->toPolygon()); + return path; +} + +QRectF PolygonColorGraphicsItem::boundingRect() const +{ + Polygon* poly = static_cast(_shape.get()); + Q_ASSERT(poly); + return poly->toPolygon().boundingRect(); +} + +QPainterPath EllipseColorGraphicsItem::shape() const +{ + QPainterPath path; + Ellipse* ellipse = static_cast(_shape.get()); + Q_ASSERT(ellipse); + QTransform transform; + transform.rotate(ellipse->getRotation()); + path.addEllipse(ellipse->getCenter(), ellipse->getHorizontalRadius(), ellipse->getVerticalRadius()); + return transform.map(path); +} + +QRectF EllipseColorGraphicsItem::boundingRect() const +{ + return shape().boundingRect(); +} + + +void PolygonColorGraphicsItem::paint(QPainter *painter, + const QStyleOptionGraphicsItem *option, QWidget *widget) +{ + Q_UNUSED(widget); + + // Setup pen and brush. + if (option->state & QStyle::State_Selected) + painter->setPen(MM::SHAPE_STROKE); + else + painter->setPen(Qt::NoPen); + + Color* color = static_cast(_mapping->getPaint().get()); + Q_ASSERT(color); + painter->setBrush(color->getColor()); + + // Draw shape as polygon. + Polygon* poly = static_cast(_shape.get()); + Q_ASSERT(poly); + painter->drawPolygon(poly->toPolygon()); +} + +void EllipseColorGraphicsItem::paint(QPainter* painter, + const QStyleOptionGraphicsItem* option, QWidget* widget) +{ + Color* color = static_cast(_mapping->getPaint().get()); + Q_ASSERT(color); + + // Setup pen and brush. + if (option->state & QStyle::State_Selected) + painter->setPen(MM::SHAPE_STROKE); + else + painter->setPen(Qt::NoPen); + + painter->setBrush(color->getColor()); + + // Draw shape as ellipse. + Ellipse* ellipse = static_cast(_shape.get()); + Q_ASSERT(ellipse); + + qreal rotation = ellipse->getRotation(); + + painter->setBrush(color->getColor()); + const QPointF& center = ellipse->getCenter(); + painter->translate(center); + painter->rotate(rotation); + painter->drawEllipse(QPointF(0,0), ellipse->getHorizontalRadius(), ellipse->getVerticalRadius()); +} + +TextureGraphicsItem::TextureGraphicsItem(Mapping::ptr mapping, bool output) + : ShapeGraphicsItem(mapping, output) +{ + _textureMapping = std::tr1::static_pointer_cast(mapping); + Q_CHECK_PTR(_textureMapping); + + _texture = std::tr1::static_pointer_cast(_textureMapping->getPaint()); + Q_CHECK_PTR(_texture); + + _inputShape = std::tr1::static_pointer_cast(_textureMapping->getInputShape()); + Q_CHECK_PTR(_inputShape); +} + +void TextureGraphicsItem::paint(QPainter *painter, + const QStyleOptionGraphicsItem *option, QWidget *widget) +{ + Q_UNUSED(widget); + + if (_output) + { + // Prepare drawing. + _preDraw(painter); + + // Perform the actual mapping (done by subclasses). + _doDraw(painter, option->state & QStyle::State_Selected); + + // End drawing. + _postDraw(painter); + } +} + +void TextureGraphicsItem::_preDraw(QPainter* painter) +{ + painter->beginNativePainting(); + + // Only works for similar shapes. + // TODO:remettre + //Q_ASSERT( _inputShape->nVertices() == outputShape->nVertices()); + + // Project source texture and sent it to destination. + _texture->update(); + + glEnable (GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, _texture->getTextureId()); + + // Copy bits to texture iff necessary. + _texture->lockMutex(); + if (_texture->bitsHaveChanged()) + { + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, + _texture->getWidth(), _texture->getHeight(), 0, GL_RGBA, + GL_UNSIGNED_BYTE, _texture->getBits()); + } + _texture->unlockMutex(); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + + glColor4f(1.0f, 1.0f, 1.0f, 1.0f); +} + +void TextureGraphicsItem::_postDraw(QPainter* painter) +{ + glDisable(GL_TEXTURE_2D); + + painter->endNativePainting(); +} + +QPainterPath PolygonTextureGraphicsItem::shape() const +{ + QPainterPath path; + Polygon* poly = static_cast(_shape.get()); + Q_ASSERT(poly); + path.addPolygon(poly->toPolygon()); + return path; +} + +QRectF PolygonTextureGraphicsItem::boundingRect() const { + Polygon* poly = static_cast(_shape.get()); + Q_ASSERT(poly); + return poly->toPolygon().boundingRect(); +} + + +void TriangleTextureGraphicsItem::_doDraw(QPainter* painter, bool selected) +{ + Q_UNUSED(painter); + Q_UNUSED(selected); + if (_output) + { + glBegin(GL_TRIANGLES); + { + for (int i=0; i<_inputShape->nVertices(); i++) + { + Util::setGlTexPoint(*_texture, _inputShape->getVertex(i), _shape->getVertex(i)); + } + } + glEnd(); + } +} + Mapper::Mapper(Mapping::ptr mapping) : _mapping(mapping), _graphicsItem(NULL), @@ -230,37 +427,6 @@ void MeshColorMapper::setValue(QtProperty* property, const QVariant& value) ColorMapper::setValue(property, value); } -EllipseColorMapper::EllipseColorMapper(Mapping::ptr mapping) - : ColorMapper(mapping) { -} - -void EllipseColorMapper::draw(QPainter* painter) -{ - painter->setPen(Qt::NoPen); - painter->setBrush(color->getColor()); - - std::tr1::shared_ptr outputEllipse = std::tr1::static_pointer_cast(outputShape); - qreal rotation = outputEllipse->getRotation(); - - painter->save(); // save painter state - - painter->resetTransform(); - painter->setBrush(color->getColor()); - const QPointF& center = outputEllipse->getCenter(); - painter->translate(center); - painter->rotate(rotation); - painter->drawEllipse(QPointF(0,0), outputEllipse->getHorizontalRadius(), outputEllipse->getVerticalRadius()); - - painter->restore(); // restore saved painter state -} - -void EllipseColorMapper::drawControls(QPainter* painter, const QList* selectedVertices) -{ - std::tr1::shared_ptr outputEllipse = std::tr1::static_pointer_cast(outputShape); - Util::drawControlsEllipse(painter, selectedVertices, *outputEllipse); -} - - TextureMapper::TextureMapper(std::tr1::shared_ptr mapping) : Mapper(mapping), _meshItem(NULL) @@ -282,44 +448,32 @@ TextureMapper::TextureMapper(std::tr1::shared_ptr mapping) _buildShapeProperty(_inputItem, textureMapping->getInputShape().get()); _topItem->insertSubProperty(_inputItem, 0); // insert before output item } - -void TextureMapper::draw(QPainter* painter) -{ - // Prepare drawing. - _preDraw(painter); - - // Perform the actual mapping (done by subclasses). - _doDraw(painter); - - // End drawing. - _postDraw(painter); -} - -void TextureMapper::drawInput(QPainter* painter) -{ - // Prepare drawing. - _preDraw(painter); - - // FIXME: Does this draw the quad counterclockwise? - glBegin (GL_QUADS); - { - Util::correctGlTexCoord(0, 0); - glVertex3f (texture->getX(), texture->getY(), 0); - - Util::correctGlTexCoord(1, 0); - glVertex3f (texture->getX()+texture->getWidth(), texture->getY(), 0); - - Util::correctGlTexCoord(1, 1); - glVertex3f (texture->getX()+texture->getWidth(), texture->getY() + texture->getHeight(), 0); - - Util::correctGlTexCoord(0, 1); - glVertex3f (texture->getX(), texture->getY() + texture->getHeight(), 0); - } - glEnd (); - - // End drawing. - _postDraw(painter); -} +// +//void TextureMapper::drawInput(QPainter* painter) +//{ +// // Prepare drawing. +// _preDraw(painter); +// +// // FIXME: Does this draw the quad counterclockwise? +// glBegin (GL_QUADS); +// { +// Util::correctGlTexCoord(0, 0); +// glVertex3f (texture->getX(), texture->getY(), 0); +// +// Util::correctGlTexCoord(1, 0); +// glVertex3f (texture->getX()+texture->getWidth(), texture->getY(), 0); +// +// Util::correctGlTexCoord(1, 1); +// glVertex3f (texture->getX()+texture->getWidth(), texture->getY() + texture->getHeight(), 0); +// +// Util::correctGlTexCoord(0, 1); +// glVertex3f (texture->getX(), texture->getY() + texture->getHeight(), 0); +// } +// glEnd (); +// +// // End drawing. +// _postDraw(painter); +//} void TextureMapper::updateShape(MShape* shape) { @@ -348,56 +502,56 @@ void TextureMapper::updatePaint() texture = std::tr1::static_pointer_cast(textureMapping->getPaint()); Q_CHECK_PTR(texture); } - -void TextureMapper::_preDraw(QPainter* painter) -{ - painter->beginNativePainting(); - - // Only works for similar shapes. - Q_ASSERT( inputShape->nVertices() == outputShape->nVertices()); - - // Project source texture and sent it to destination. - texture->update(); - - glEnable (GL_TEXTURE_2D); - glBindTexture(GL_TEXTURE_2D, texture->getTextureId()); - - // Copy bits to texture iff necessary. - texture->lockMutex(); - if (texture->bitsHaveChanged()) - { - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, - texture->getWidth(), texture->getHeight(), 0, GL_RGBA, - GL_UNSIGNED_BYTE, texture->getBits()); - } - texture->unlockMutex(); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - - glColor4f(1.0f, 1.0f, 1.0f, 1.0f); -} - -void TextureMapper::_postDraw(QPainter* painter) -{ - glDisable(GL_TEXTURE_2D); - - painter->endNativePainting(); -} - -void PolygonTextureMapper::drawControls(QPainter* painter, const QList* selectedVertices) -{ - std::tr1::shared_ptr outputPoly = std::tr1::static_pointer_cast(outputShape); - Util::drawControlsPolygon(painter, selectedVertices, *outputPoly); -} - -void PolygonTextureMapper::drawInputControls(QPainter* painter, const QList* selectedVertices) -{ - std::tr1::shared_ptr inputPoly = std::tr1::static_pointer_cast(inputShape); - Util::drawControlsPolygon(painter, selectedVertices, *inputPoly); -} +// +//void TextureMapper::_preDraw(QPainter* painter) +//{ +// painter->beginNativePainting(); +// +// // Only works for similar shapes. +// Q_ASSERT( inputShape->nVertices() == outputShape->nVertices()); +// +// // Project source texture and sent it to destination. +// texture->update(); +// +// glEnable (GL_TEXTURE_2D); +// glBindTexture(GL_TEXTURE_2D, texture->getTextureId()); +// +// // Copy bits to texture iff necessary. +// texture->lockMutex(); +// if (texture->bitsHaveChanged()) +// { +// glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, +// texture->getWidth(), texture->getHeight(), 0, GL_RGBA, +// GL_UNSIGNED_BYTE, texture->getBits()); +// } +// texture->unlockMutex(); +// +// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); +// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); +// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); +// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); +// +// glColor4f(1.0f, 1.0f, 1.0f, 1.0f); +//} +// +//void TextureMapper::_postDraw(QPainter* painter) +//{ +// glDisable(GL_TEXTURE_2D); +// +// painter->endNativePainting(); +//} +// +//void PolygonTextureMapper::drawControls(QPainter* painter, const QList* selectedVertices) +//{ +// std::tr1::shared_ptr outputPoly = std::tr1::static_pointer_cast(outputShape); +// Util::drawControlsPolygon(painter, selectedVertices, *outputPoly); +//} +// +//void PolygonTextureMapper::drawInputControls(QPainter* painter, const QList* selectedVertices) +//{ +// std::tr1::shared_ptr inputPoly = std::tr1::static_pointer_cast(inputShape); +// Util::drawControlsPolygon(painter, selectedVertices, *inputPoly); +//} TriangleTextureMapper::TriangleTextureMapper(std::tr1::shared_ptr mapping) : PolygonTextureMapper(mapping) @@ -405,19 +559,20 @@ TriangleTextureMapper::TriangleTextureMapper(std::tr1::shared_ptrnVertices(); i++) - { - Util::setGlTexPoint(*texture, inputShape->getVertex(i), outputShape->getVertex(i)); - } - } - glEnd(); -} +// +//void TriangleTextureMapper::_doDraw(QPainter* painter) +//{ +// qDebug() << "Is this really used!" << endl; +//// Q_UNUSED(painter); +//// glBegin(GL_TRIANGLES); +//// { +//// for (int i = 0; i < inputShape->nVertices(); i++) +//// { +//// Util::setGlTexPoint(*texture, inputShape->getVertex(i), outputShape->getVertex(i)); +//// } +//// } +//// glEnd(); +//} MeshTextureMapper::MeshTextureMapper(std::tr1::shared_ptr mapping) : PolygonTextureMapper(mapping) @@ -576,4 +731,3 @@ void EllipseTextureMapper::_setPointOfEllipseAtAngle(QPointF& point, const QPoin point.setX( sin(angle + rotation) * distance + center.x() ); point.setY( cos(angle + rotation) * distance + center.y() ); } - diff --git a/Mapper.h b/Mapper.h index 02a04b6..6d14143 100644 --- a/Mapper.h +++ b/Mapper.h @@ -50,12 +50,137 @@ #include "variantmanager.h" #include "variantfactory.h" +class VertexGraphicsItem; + +class ShapeGraphicsItem : public QGraphicsItem +{ + Q_DECLARE_TR_FUNCTIONS(ShapeGraphicsItem) + +public: + ShapeGraphicsItem(Mapping::ptr mapping, bool output=true); + virtual ~ShapeGraphicsItem() {} + + // TODO: dangereux: confusion possible entre shape() et getShape()... + MShape::ptr getShape() const { return _shape; } + + Mapping::ptr getMapping() const { return _mapping; } + + bool isOutput() const { return _output; } + + virtual QRectF boundingRect() const = 0; +// virtual QPainterPath shape() const; +// virtual void paint(QPainter *painter, +// const QStyleOptionGraphicsItem *option, QWidget *widget); + + virtual bool sceneEventFilter(QGraphicsItem * watched, QEvent * event); + +protected: + virtual void _createVertices(); + virtual void _syncShape(); + + Mapping::ptr _mapping; + MShape::ptr _shape; + bool _output; +}; + +/// Graphics item for vertices / control points. +class VertexGraphicsItem : public QGraphicsEllipseItem +{ + Q_DECLARE_TR_FUNCTIONS(VertexGraphicsItem) + +public: + VertexGraphicsItem(int index) : _index(index) { + setFlags(ItemIsMovable | ItemIsSelectable); + } + virtual ~VertexGraphicsItem() {} + + virtual void paint(QPainter *painter, + const QStyleOptionGraphicsItem *option, QWidget *widget) + { + Q_UNUSED(widget); + Util::drawControlsVertex(painter, QPointF(0,0), (option->state & QStyle::State_Selected)); + } + +protected: + int _index; +}; + +/// Graphics item for colored polygons (eg. quad, triangle). +class PolygonColorGraphicsItem : public ShapeGraphicsItem +{ +public: + PolygonColorGraphicsItem(Mapping::ptr mapping, bool output=true) : ShapeGraphicsItem(mapping, output) {} + virtual ~PolygonColorGraphicsItem() {} + + virtual QPainterPath shape() const; + virtual QRectF boundingRect() const; + + virtual void paint(QPainter *painter, + const QStyleOptionGraphicsItem *option, QWidget *widget); +}; + +/// Graphics item for colored polygons (eg. quad, triangle). +class EllipseColorGraphicsItem : public ShapeGraphicsItem +{ +public: + EllipseColorGraphicsItem(Mapping::ptr mapping, bool output=true) : ShapeGraphicsItem(mapping, output) {} + virtual ~EllipseColorGraphicsItem() {} + + virtual QPainterPath shape() const; + virtual QRectF boundingRect() const; + + virtual void paint(QPainter *painter, + const QStyleOptionGraphicsItem *option, QWidget *widget); +}; + +/// Abstract class for texture graphics items. +class TextureGraphicsItem : public ShapeGraphicsItem +{ +public: + TextureGraphicsItem(Mapping::ptr mapping, bool output=true); + + virtual ~TextureGraphicsItem() {} + + virtual void paint(QPainter *painter, + const QStyleOptionGraphicsItem *option, QWidget *widget); + + virtual void _doDraw(QPainter* painter, bool selected) = 0; + void _preDraw(QPainter* painter); + void _postDraw(QPainter* painter); + +protected: + std::tr1::shared_ptr _textureMapping; + std::tr1::shared_ptr _texture; + std::tr1::shared_ptr _inputShape; +}; + +/// Graphics item for textured polygons (eg. triangles). +class PolygonTextureGraphicsItem : public TextureGraphicsItem +{ +public: + PolygonTextureGraphicsItem(Mapping::ptr mapping, bool output=true) : TextureGraphicsItem(mapping, output) {} + virtual ~PolygonTextureGraphicsItem(){} + + virtual QPainterPath shape() const; + virtual QRectF boundingRect() const; +}; + +/// Graphics item for textured triangles. +class TriangleTextureGraphicsItem : public PolygonTextureGraphicsItem +{ +public: + TriangleTextureGraphicsItem(Mapping::ptr mapping, bool output=true) : PolygonTextureGraphicsItem(mapping, output) {} + virtual ~TriangleTextureGraphicsItem(){} + + virtual void _doDraw(QPainter* painter, bool selected); +}; + /** * A way to draw on some kind of shape. - * + * * This is an abstract class for specific ways to draw * a mapping ie. a shape applied to a paint. - * + * * Each mapping (ie. shape x paint combination) that is possible in * this software are implemented using a child of this * class. @@ -111,253 +236,6 @@ protected: virtual void _updateShapeProperty(QtProperty* shapeItem, MShape* shape); }; -class VertexGraphicsItem; - -class ShapeGraphicsItem : public QGraphicsItem -{ - Q_DECLARE_TR_FUNCTIONS(ShapeGraphicsItem) - -public: - ShapeGraphicsItem(Mapping::ptr mapping, bool output=true) - : _mapping(mapping), _output(output) - { - _shape = output ? _mapping->getShape() : _mapping->getInputShape(); - - setFlags(ItemIsMovable | ItemIsSelectable); - setFiltersChildEvents(true); - - _createVertices(); - } - virtual ~ShapeGraphicsItem() {} - - // TODO: dangereux: confusion possible entre shape() et getShape()... - MShape::ptr getShape() const { return _shape; } - - Mapping::ptr getMapping() const { return _mapping; } - - bool isOutput() const { return _output; } - - virtual QRectF boundingRect() const = 0; -// virtual QPainterPath shape() const; -// virtual void paint(QPainter *painter, -// const QStyleOptionGraphicsItem *option, QWidget *widget); - - virtual bool sceneEventFilter(QGraphicsItem * watched, QEvent * event); - -protected: - virtual void _createVertices(); - virtual void _syncShape(); - - Mapping::ptr _mapping; - MShape::ptr _shape; - bool _output; -}; - -/// Graphics item for vertices / control points. -class VertexGraphicsItem : public QGraphicsEllipseItem -{ - Q_DECLARE_TR_FUNCTIONS(VertexGraphicsItem) - -public: - VertexGraphicsItem(int index) : _index(index) { - setFlags(ItemIsMovable | ItemIsSelectable); - } - virtual ~VertexGraphicsItem() {} - - virtual void paint(QPainter *painter, - const QStyleOptionGraphicsItem *option, QWidget *widget) - { - Q_UNUSED(widget); -// const QPointF& vertex = ((ShapeGraphicsItem*)parentItem())->getShape()->getVertex(_index); - const QPointF vertex(0,0); - Util::drawControlsVertex(painter, vertex, (option->state & QStyle::State_Selected)); - } - - -protected: - int _index; -}; - -class PolygonColorGraphicsItem : public ShapeGraphicsItem -{ -public: - PolygonColorGraphicsItem(Mapping::ptr mapping, bool output=true) : ShapeGraphicsItem(mapping, output) {} - virtual ~PolygonColorGraphicsItem() {} - - - virtual QRectF boundingRect() const { - return ((Polygon*)_shape.get())->toPolygon().boundingRect(); - } - - virtual QPainterPath shape() const { - QPainterPath path; - path.addPolygon(((Polygon*)_shape.get())->toPolygon()); - return path; - } - - virtual void paint(QPainter *painter, - const QStyleOptionGraphicsItem *option, QWidget *widget) - { - Q_UNUSED(widget); - - painter->setPen(Qt::NoPen); - painter->setBrush(Qt::red); - painter->setBrush(((Color*)_mapping->getPaint().get())->getColor()); - - // Draw shape as polygon. - Polygon* poly = (Polygon*)_shape.get(); - painter->drawPolygon(poly->toPolygon()); - -// if (option->state & QStyle::State_Selected) -// { -// // TODO: drawControlsPolygon()'s selected vertices should be optional -// // ie. disabled if NULL -// QList dummySelectedVertices; -// Util::drawControlsPolygon(painter, &dummySelectedVertices, *poly); -// } - } -}; - -class TextureGraphicsItem : public ShapeGraphicsItem -{ -public: - TextureGraphicsItem(Mapping::ptr mapping, bool output=true) : ShapeGraphicsItem(mapping, output) { - _textureMapping = std::tr1::static_pointer_cast(mapping); - Q_CHECK_PTR(_textureMapping); - - _texture = std::tr1::static_pointer_cast(_textureMapping->getPaint()); - Q_CHECK_PTR(_texture); - - _inputShape = std::tr1::static_pointer_cast(_textureMapping->getInputShape()); - Q_CHECK_PTR(_inputShape); - } - - virtual ~TextureGraphicsItem() {} - - virtual void paint(QPainter *painter, - const QStyleOptionGraphicsItem *option, QWidget *widget) - { - Q_UNUSED(widget); - - if (_output) - { - // Prepare drawing. - _preDraw(painter); - - // Perform the actual mapping (done by subclasses). - _doDraw(painter, option->state & QStyle::State_Selected); - - // End drawing. - _postDraw(painter); - } - -// if (option->state & QStyle::State_Selected) -// _drawControls(painter); - } - - virtual void _doDraw(QPainter* painter, bool selected) = 0; - void _preDraw(QPainter* painter) - { - painter->beginNativePainting(); - - // Only works for similar shapes. - // TODO:remettre - //Q_ASSERT( _inputShape->nVertices() == outputShape->nVertices()); - - // Project source texture and sent it to destination. - _texture->update(); - - glEnable (GL_TEXTURE_2D); - glBindTexture(GL_TEXTURE_2D, _texture->getTextureId()); - - // Copy bits to texture iff necessary. - _texture->lockMutex(); - if (_texture->bitsHaveChanged()) - { - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, - _texture->getWidth(), _texture->getHeight(), 0, GL_RGBA, - GL_UNSIGNED_BYTE, _texture->getBits()); - } - _texture->unlockMutex(); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - - glColor4f(1.0f, 1.0f, 1.0f, 1.0f); - } - - void _postDraw(QPainter* painter) - { - glDisable(GL_TEXTURE_2D); - - painter->endNativePainting(); - } - - virtual void _drawControls(QPainter *painter) - { - Q_UNUSED(painter); - } - -protected: - std::tr1::shared_ptr _textureMapping; - std::tr1::shared_ptr _texture; - std::tr1::shared_ptr _inputShape; -}; - -class PolygonTextureGraphicsItem : public TextureGraphicsItem -{ -public: - PolygonTextureGraphicsItem(Mapping::ptr mapping, bool output=true) : TextureGraphicsItem(mapping, output) {} - virtual ~PolygonTextureGraphicsItem(){} - -// virtual void _drawControls(QPainter* painter) -// { -// std::tr1::shared_ptr outputPoly = std::tr1::static_pointer_cast(_shape); -// QList dummySelectedVertices; -// Util::drawControlsPolygon(painter, &dummySelectedVertices, *outputPoly); -// } -}; - -class TriangleTextureGraphicsItem : public PolygonTextureGraphicsItem -{ -public: - TriangleTextureGraphicsItem(Mapping::ptr mapping, bool output=true) : PolygonTextureGraphicsItem(mapping, output) {} - virtual ~TriangleTextureGraphicsItem(){} - - virtual QRectF boundingRect() const { - return ((Polygon*)_shape.get())->toPolygon().boundingRect(); - } - - virtual QPainterPath shape() const { - QPainterPath path; - Polygon* poly = static_cast(_shape.get()); - Q_ASSERT(poly); - path.addPolygon(poly->toPolygon()); - return path; - } - - virtual void _doDraw(QPainter* painter, bool selected) - { - Q_UNUSED(painter); - Q_UNUSED(selected); - if (_output) - { - glBegin(GL_TRIANGLES); - { - for (int i = 0; i < _inputShape->nVertices(); i++) - { - Util::setGlTexPoint(*_texture, _inputShape->getVertex(i), _shape->getVertex(i)); - } - } - glEnd(); - } - } -}; - - - /** * Draws a color (abstract class). */ @@ -410,11 +288,11 @@ class EllipseColorMapper : public ColorMapper Q_OBJECT public: - EllipseColorMapper(Mapping::ptr mapping); - virtual ~EllipseColorMapper() {} + EllipseColorMapper(Mapping::ptr mapping) : ColorMapper(mapping) { + _graphicsItem = new EllipseColorGraphicsItem(mapping, true); + } - virtual void draw(QPainter* painter); - virtual void drawControls(QPainter* painter, const QList* selectedVertices=0); + virtual ~EllipseColorMapper() {} }; /** @@ -428,24 +306,24 @@ public: TextureMapper(std::tr1::shared_ptr mapping); virtual ~TextureMapper() {} - /** - * This method will call the _doDraw(QPainter*) method to actually perform the drawing. - * of the mapping. - */ - virtual void draw(QPainter* painter); - virtual void drawInput(QPainter* painter); - - virtual void drawControls(QPainter* painter, const QList* selectedVertices=0) = 0; - virtual void drawInputControls(QPainter* painter, const QList* selectedVertices=0) = 0; +// /** +// * This method will call the _doDraw(QPainter*) method to actually perform the drawing. +// * of the mapping. +// */ +// virtual void draw(QPainter* painter); +// virtual void drawInput(QPainter* painter); +// +// virtual void drawControls(QPainter* painter, const QList* selectedVertices=0) = 0; +// virtual void drawInputControls(QPainter* painter, const QList* selectedVertices=0) = 0; public slots: virtual void updateShape(MShape* shape); virtual void updatePaint(); -protected: - virtual void _doDraw(QPainter* painter) = 0; - void _preDraw(QPainter* painter); - void _postDraw(QPainter* painter); +//protected: +// virtual void _doDraw(QPainter* painter) = 0; +// void _preDraw(QPainter* painter); +// void _postDraw(QPainter* painter); protected: QtProperty* _inputItem; @@ -464,9 +342,6 @@ class PolygonTextureMapper : public TextureMapper public: PolygonTextureMapper(std::tr1::shared_ptr mapping) : TextureMapper(mapping) {} virtual ~PolygonTextureMapper() {} - - virtual void drawControls(QPainter* painter, const QList* selectedVertices=0); - virtual void drawInputControls(QPainter* painter, const QList* selectedVertices=0); }; class TriangleTextureMapper : public PolygonTextureMapper @@ -476,9 +351,6 @@ class TriangleTextureMapper : public PolygonTextureMapper public: TriangleTextureMapper(std::tr1::shared_ptr mapping); virtual ~TriangleTextureMapper() {} - -protected: - virtual void _doDraw(QPainter* painter); }; class MeshTextureMapper : public PolygonTextureMapper From b35ce293bf028d9b3c3ff1e55ff8b6a47fb4511d Mon Sep 17 00:00:00 2001 From: Tats Date: Sun, 28 Dec 2014 18:01:39 -0500 Subject: [PATCH 07/32] Sync of vertices control points retroactive (to make ellipse works) (not fully working yet). --- Mapper.cpp | 38 ++++++++++++++++++++++++++++---------- Mapper.h | 1 + 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/Mapper.cpp b/Mapper.cpp index 6059405..fd8c73f 100644 --- a/Mapper.cpp +++ b/Mapper.cpp @@ -37,23 +37,31 @@ ShapeGraphicsItem::ShapeGraphicsItem(Mapping::ptr mapping, bool output) bool ShapeGraphicsItem::sceneEventFilter(QGraphicsItem * watched, QEvent * event) { - VertexGraphicsItem* child = static_cast(watched); - Q_ASSERT(child); - // Change vertex in model according to moved item. if (event->type() == QEvent::GraphicsSceneMouseMove) { + QGraphicsSceneMoveEvent* moveEvent = static_cast(event); + Q_ASSERT(moveEvent); + int idx = childItems().indexOf(watched); Q_ASSERT(idx != -1); - QPointF pos = child->pos(); + + QPointF pos = moveEvent->newPos(); _shape->setVertex(idx, pos); + + _syncVertices(); + + // Refresh this shape. + update(); + + // override default + return true; + } + else + { + // Returns false to allow the child item to process its event. + return false; } - - // Refresh this shape. - update(); - - // Returns false to allow the child item to process its event. - return false; } void ShapeGraphicsItem::_createVertices() @@ -85,6 +93,16 @@ void ShapeGraphicsItem::_syncShape() } } +void ShapeGraphicsItem::_syncVertices() +{ + for (int i=0; i<_shape->nVertices(); i++) + { + QPointF pos = _shape->getVertex(i);// - this->pos(); + childItems().at(i)->setPos(pos); + childItems().at(i)->update(); + } +} + QPainterPath PolygonColorGraphicsItem::shape() const { QPainterPath path; diff --git a/Mapper.h b/Mapper.h index 6d14143..40730e0 100644 --- a/Mapper.h +++ b/Mapper.h @@ -77,6 +77,7 @@ public: protected: virtual void _createVertices(); virtual void _syncShape(); + virtual void _syncVertices(); Mapping::ptr _mapping; MShape::ptr _shape; From 78294f2ef083e3b66de2c03ecebb1d71349f3740 Mon Sep 17 00:00:00 2001 From: Tats Date: Sun, 28 Dec 2014 19:44:46 -0500 Subject: [PATCH 08/32] Fixed correct translation of color shapes. --- Mapper.cpp | 98 ++++++++++++++++++++++++++++++------------------------ Mapper.h | 3 ++ 2 files changed, 57 insertions(+), 44 deletions(-) diff --git a/Mapper.cpp b/Mapper.cpp index fd8c73f..cf3f71b 100644 --- a/Mapper.cpp +++ b/Mapper.cpp @@ -41,18 +41,22 @@ bool ShapeGraphicsItem::sceneEventFilter(QGraphicsItem * watched, QEvent * event if (event->type() == QEvent::GraphicsSceneMouseMove) { QGraphicsSceneMoveEvent* moveEvent = static_cast(event); - Q_ASSERT(moveEvent); + QGraphicsSceneMouseEvent* mouseEvent = static_cast(event); + // Q_ASSERT(moveEvent); + Q_ASSERT(mouseEvent); int idx = childItems().indexOf(watched); Q_ASSERT(idx != -1); - QPointF pos = moveEvent->newPos(); +// QPointF pos = moveEvent->newPos();// + this->pos(); + QPointF pos = mouseEvent->scenePos(); + // qDebug() << moveEvent->oldPos() << " " << pos << " " << childItems().at(idx)->pos() << endl; _shape->setVertex(idx, pos); _syncVertices(); // Refresh this shape. - update(); + // update(); // override default return true; @@ -64,14 +68,30 @@ bool ShapeGraphicsItem::sceneEventFilter(QGraphicsItem * watched, QEvent * event } } +void ShapeGraphicsItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) +{ + QGraphicsItem::mouseMoveEvent(event); + _syncShape(); +} + +//QVariant ShapeGraphicsItem::itemChange(GraphicsItemChange change, const QVariant &value) +//{ +// if (change == ItemPositionChange) +// { +// qDebug() << "Item changed" << endl; +// return QPointF(pos().x(), value.toPointF().y()); +// } +// +// return QGraphicsItem::itemChange(change, value); +//} + void ShapeGraphicsItem::_createVertices() { // rect offset - qreal offset = MM::VERTEX_SELECT_RADIUS / 2.0; for (int i=0; i<_shape->nVertices(); i++) { // XXX is this freed by parent? - QPointF pos = _shape->getVertex(i) - this->pos(); + QPointF pos = mapFromScene(_shape->getVertex(i));// - this->pos(); VertexGraphicsItem* child = new VertexGraphicsItem(i); // child->setPos( pos ); child->setParentItem(this); @@ -89,7 +109,7 @@ void ShapeGraphicsItem::_syncShape() for (int i=0; i<_shape->nVertices(); i++) { QGraphicsItem* child = children.at(i); - _shape->setVertex(i, mapFromItem(child, child->pos())); + _shape->setVertex(i, child->scenePos()); } } @@ -97,8 +117,8 @@ void ShapeGraphicsItem::_syncVertices() { for (int i=0; i<_shape->nVertices(); i++) { - QPointF pos = _shape->getVertex(i);// - this->pos(); - childItems().at(i)->setPos(pos); + QPointF pos = _shape->getVertex(i) ;//- this->pos(); // this is in scene coordinates + childItems().at(i)->setPos(this->mapFromScene(pos)); childItems().at(i)->update(); } } @@ -109,52 +129,52 @@ QPainterPath PolygonColorGraphicsItem::shape() const Polygon* poly = static_cast(_shape.get()); Q_ASSERT(poly); path.addPolygon(poly->toPolygon()); - return path; + return mapFromScene(path); } QRectF PolygonColorGraphicsItem::boundingRect() const -{ - Polygon* poly = static_cast(_shape.get()); - Q_ASSERT(poly); - return poly->toPolygon().boundingRect(); -} - -QPainterPath EllipseColorGraphicsItem::shape() const -{ - QPainterPath path; - Ellipse* ellipse = static_cast(_shape.get()); - Q_ASSERT(ellipse); - QTransform transform; - transform.rotate(ellipse->getRotation()); - path.addEllipse(ellipse->getCenter(), ellipse->getHorizontalRadius(), ellipse->getVerticalRadius()); - return transform.map(path); -} - -QRectF EllipseColorGraphicsItem::boundingRect() const { return shape().boundingRect(); } - void PolygonColorGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { Q_UNUSED(widget); + Color* color = static_cast(_mapping->getPaint().get()); + Q_ASSERT(color); + // Setup pen and brush. if (option->state & QStyle::State_Selected) painter->setPen(MM::SHAPE_STROKE); else painter->setPen(Qt::NoPen); - Color* color = static_cast(_mapping->getPaint().get()); - Q_ASSERT(color); painter->setBrush(color->getColor()); - // Draw shape as polygon. + // TODO: polygon and ellipse are 99% similar (except for these 3 lines!). we should use that. Polygon* poly = static_cast(_shape.get()); Q_ASSERT(poly); - painter->drawPolygon(poly->toPolygon()); + painter->drawPolygon(mapFromScene(poly->toPolygon())); +} + +QPainterPath EllipseColorGraphicsItem::shape() const +{ + // Create path for ellipse. + QPainterPath path; + Ellipse* ellipse = static_cast(_shape.get()); + Q_ASSERT(ellipse); + QTransform transform; + transform.translate(ellipse->getCenter().x(), ellipse->getCenter().y()); + transform.rotate(ellipse->getRotation()); + path.addEllipse(QPoint(0,0), ellipse->getHorizontalRadius(), ellipse->getVerticalRadius()); + return mapFromScene(transform.map(path)); +} + +QRectF EllipseColorGraphicsItem::boundingRect() const +{ + return shape().boundingRect(); } void EllipseColorGraphicsItem::paint(QPainter* painter, @@ -170,18 +190,8 @@ void EllipseColorGraphicsItem::paint(QPainter* painter, painter->setPen(Qt::NoPen); painter->setBrush(color->getColor()); - - // Draw shape as ellipse. - Ellipse* ellipse = static_cast(_shape.get()); - Q_ASSERT(ellipse); - - qreal rotation = ellipse->getRotation(); - - painter->setBrush(color->getColor()); - const QPointF& center = ellipse->getCenter(); - painter->translate(center); - painter->rotate(rotation); - painter->drawEllipse(QPointF(0,0), ellipse->getHorizontalRadius(), ellipse->getVerticalRadius()); + // Just draw the path. + painter->drawPath(shape()); } TextureGraphicsItem::TextureGraphicsItem(Mapping::ptr mapping, bool output) diff --git a/Mapper.h b/Mapper.h index 40730e0..d68924a 100644 --- a/Mapper.h +++ b/Mapper.h @@ -74,6 +74,9 @@ public: virtual bool sceneEventFilter(QGraphicsItem * watched, QEvent * event); + void mouseMoveEvent(QGraphicsSceneMouseEvent *event); +// virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value); + protected: virtual void _createVertices(); virtual void _syncShape(); From 2b7a6e82a5fc40228c7757cd9f1ce5677c1bb1b1 Mon Sep 17 00:00:00 2001 From: Tats Date: Sun, 28 Dec 2014 19:53:36 -0500 Subject: [PATCH 09/32] Fixed translation and display of controls of triangle texture. --- Mapper.cpp | 15 +++++++++------ Mapper.h | 7 +++++++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/Mapper.cpp b/Mapper.cpp index cf3f71b..7a43055 100644 --- a/Mapper.cpp +++ b/Mapper.cpp @@ -214,14 +214,19 @@ void TextureGraphicsItem::paint(QPainter *painter, if (_output) { + bool selected = option->state & QStyle::State_Selected; + // Prepare drawing. _preDraw(painter); // Perform the actual mapping (done by subclasses). - _doDraw(painter, option->state & QStyle::State_Selected); + _doDraw(painter, selected); // End drawing. _postDraw(painter); + + if (selected) + _doDrawControls(painter); } } @@ -270,13 +275,11 @@ QPainterPath PolygonTextureGraphicsItem::shape() const Polygon* poly = static_cast(_shape.get()); Q_ASSERT(poly); path.addPolygon(poly->toPolygon()); - return path; + return mapFromScene(path); } QRectF PolygonTextureGraphicsItem::boundingRect() const { - Polygon* poly = static_cast(_shape.get()); - Q_ASSERT(poly); - return poly->toPolygon().boundingRect(); + return shape().boundingRect(); } @@ -290,7 +293,7 @@ void TriangleTextureGraphicsItem::_doDraw(QPainter* painter, bool selected) { for (int i=0; i<_inputShape->nVertices(); i++) { - Util::setGlTexPoint(*_texture, _inputShape->getVertex(i), _shape->getVertex(i)); + Util::setGlTexPoint(*_texture, _inputShape->getVertex(i), mapFromScene(_shape->getVertex(i))); } } glEnd(); diff --git a/Mapper.h b/Mapper.h index d68924a..31326ac 100644 --- a/Mapper.h +++ b/Mapper.h @@ -149,6 +149,7 @@ public: const QStyleOptionGraphicsItem *option, QWidget *widget); virtual void _doDraw(QPainter* painter, bool selected) = 0; + virtual void _doDrawControls(QPainter* painter) { Q_UNUSED(painter); } void _preDraw(QPainter* painter); void _postDraw(QPainter* painter); @@ -177,6 +178,12 @@ public: virtual ~TriangleTextureGraphicsItem(){} virtual void _doDraw(QPainter* painter, bool selected); + virtual void _doDrawControls(QPainter* painter) { + painter->setPen(MM::SHAPE_STROKE); + Polygon* poly = static_cast(_shape.get()); + Q_ASSERT(poly); + painter->drawPolygon(mapFromScene(poly->toPolygon())); + } }; /** From a84888467b7f46afd6ee7bf57349886e26a27b17 Mon Sep 17 00:00:00 2001 From: Tats Date: Sun, 28 Dec 2014 20:50:17 -0500 Subject: [PATCH 10/32] Fixed drawing of texture in input mapping. --- Mapper.cpp | 42 ++++++++++++++++++++++++++++++------------ Paint.h | 2 ++ 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/Mapper.cpp b/Mapper.cpp index 7a43055..6dff56f 100644 --- a/Mapper.cpp +++ b/Mapper.cpp @@ -212,22 +212,19 @@ void TextureGraphicsItem::paint(QPainter *painter, { Q_UNUSED(widget); - if (_output) - { - bool selected = option->state & QStyle::State_Selected; + bool selected = option->state & QStyle::State_Selected; - // Prepare drawing. - _preDraw(painter); + // Prepare drawing. + _preDraw(painter); - // Perform the actual mapping (done by subclasses). - _doDraw(painter, selected); + // Perform the actual mapping (done by subclasses). + _doDraw(painter, selected); - // End drawing. - _postDraw(painter); + // End drawing. + _postDraw(painter); - if (selected) - _doDrawControls(painter); - } +// if (selected) + _doDrawControls(painter); } void TextureGraphicsItem::_preDraw(QPainter* painter) @@ -298,6 +295,27 @@ void TriangleTextureGraphicsItem::_doDraw(QPainter* painter, bool selected) } glEnd(); } + else + { + // FIXME: Does this draw the quad counterclockwise? + glBegin (GL_QUADS); + { + QRectF rect = mapFromScene(_texture->getRect()).boundingRect(); + + Util::correctGlTexCoord(0, 0); + glVertex3f (rect.x(), rect.y(), 0); + + Util::correctGlTexCoord(1, 0); + glVertex3f (rect.x() + rect.width(), rect.y(), 0); + + Util::correctGlTexCoord(1, 1); + glVertex3f (rect.x()+rect.width(), rect.y()+rect.height(), 0); + + Util::correctGlTexCoord(0, 1); + glVertex3f (rect.x(), rect.y()+rect.height(), 0); + } + glEnd (); + } } Mapper::Mapper(Mapping::ptr mapping) diff --git a/Paint.h b/Paint.h index 19b69f6..c735730 100644 --- a/Paint.h +++ b/Paint.h @@ -152,6 +152,8 @@ public: } virtual GLfloat getX() const { return x; } virtual GLfloat getY() const { return y; } + + virtual QRectF getRect() const { return QRectF(getX(), getY(), getWidth(), getHeight()); } }; /** From 191f0f9abaf98139d650f3f6fd41a7d3961db0a8 Mon Sep 17 00:00:00 2001 From: Tats Date: Sun, 28 Dec 2014 21:05:28 -0500 Subject: [PATCH 11/32] Fixed wrongful use of static const floats in class MM. --- MM.cpp | 9 +++++++++ MM.h | 12 ++++++------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/MM.cpp b/MM.cpp index a93077c..a8ab559 100644 --- a/MM.cpp +++ b/MM.cpp @@ -35,6 +35,15 @@ const QColor MM::DARK_GRAY("#272a36"); const QColor MM::CONTROL_COLOR(WHITE); const QBrush MM::VERTEX_BACKGROUND(QColor(CONTROL_COLOR.red(), CONTROL_COLOR.green(), CONTROL_COLOR.blue(), 63)); const QBrush MM::VERTEX_SELECTED_BACKGROUND(QColor(CONTROL_COLOR.red(), CONTROL_COLOR.green(), CONTROL_COLOR.blue(), 127)); + +const qreal MM::SHAPE_STROKE_WIDTH = 1.5; +const qreal MM::SHAPE_INNER_STROKE_WIDTH = 0.5; const QPen MM::SHAPE_STROKE(QBrush(CONTROL_COLOR), SHAPE_STROKE_WIDTH); const QPen MM::SHAPE_INNER_STROKE(QBrush(CONTROL_COLOR), SHAPE_INNER_STROKE_WIDTH); +const qreal MM::VERTEX_STICK_RADIUS = 10; +const qreal MM::VERTEX_SELECT_RADIUS = 10; +const qreal MM::VERTEX_SELECT_STROKE_WIDTH = 1; + +// Time. +const qreal MM::FRAMES_PER_SECOND = 29.97f; diff --git a/MM.h b/MM.h index 0970c5f..ae6fa08 100644 --- a/MM.h +++ b/MM.h @@ -53,18 +53,18 @@ public: static const QBrush VERTEX_BACKGROUND; static const QBrush VERTEX_SELECTED_BACKGROUND; - static const qreal SHAPE_STROKE_WIDTH = 2; - static const qreal SHAPE_INNER_STROKE_WIDTH = 0.5; + static const qreal SHAPE_STROKE_WIDTH; + static const qreal SHAPE_INNER_STROKE_WIDTH; static const QPen SHAPE_STROKE; static const QPen SHAPE_INNER_STROKE; // Control. - static const qreal VERTEX_STICK_RADIUS = 10; - static const qreal VERTEX_SELECT_RADIUS = 10; - static const qreal VERTEX_SELECT_STROKE_WIDTH = 1; + static const qreal VERTEX_STICK_RADIUS; + static const qreal VERTEX_SELECT_RADIUS; + static const qreal VERTEX_SELECT_STROKE_WIDTH; // Time. - static const float FRAMES_PER_SECOND = 29.97f; + static const qreal FRAMES_PER_SECOND; }; #endif From c0b6bee45bfb529a68ef50426081de54f226c7f2 Mon Sep 17 00:00:00 2001 From: Tats Date: Sun, 28 Dec 2014 21:05:49 -0500 Subject: [PATCH 12/32] Implemented working zoom with wheel mouse. --- MM.cpp | 6 ++++++ MM.h | 5 +++++ MainWindow.cpp | 6 +++--- MapperGLCanvas.cpp | 17 ++++++++++++++++- MapperGLCanvas.h | 5 +++++ 5 files changed, 35 insertions(+), 4 deletions(-) diff --git a/MM.cpp b/MM.cpp index a8ab559..5a5e1aa 100644 --- a/MM.cpp +++ b/MM.cpp @@ -47,3 +47,9 @@ const qreal MM::VERTEX_SELECT_STROKE_WIDTH = 1; // Time. const qreal MM::FRAMES_PER_SECOND = 29.97f; + +// Zoom. +const qreal MM::ZOOM_FACTOR = 1.4f; +const qreal MM::ZOOM_MIN = 0.25f; +const qreal MM::ZOOM_MAX = 3.0f; + diff --git a/MM.h b/MM.h index ae6fa08..4a0d173 100644 --- a/MM.h +++ b/MM.h @@ -65,6 +65,11 @@ public: // Time. static const qreal FRAMES_PER_SECOND; + + // Zoom. + static const qreal ZOOM_FACTOR; + static const qreal ZOOM_MIN; + static const qreal ZOOM_MAX; }; #endif diff --git a/MainWindow.cpp b/MainWindow.cpp index 18f638e..b0962a0 100644 --- a/MainWindow.cpp +++ b/MainWindow.cpp @@ -1978,9 +1978,9 @@ void MainWindow::clearWindow() void MainWindow::updateCanvases() { - sourceCanvas->update(); - destinationCanvas->update(); - outputWindow->getCanvas()->update(); + sourceCanvas->updateCanvas(); + destinationCanvas->updateCanvas(); + outputWindow->getCanvas()->updateCanvas(); } QString MainWindow::strippedName(const QString &fullFileName) diff --git a/MapperGLCanvas.cpp b/MapperGLCanvas.cpp index dbef742..34269cf 100644 --- a/MapperGLCanvas.cpp +++ b/MapperGLCanvas.cpp @@ -30,7 +30,8 @@ MapperGLCanvas::MapperGLCanvas(MainWindow* mainWindow, QWidget* parent, const QG _shapeGrabbed(false), // comment out? _shapeFirstGrab(false), // comment out? _displayControls(true), - _stickyVertices(true) + _stickyVertices(true), + _zoomLevel(0) { setDragMode(QGraphicsView::RubberBandDrag); setRenderHint(QPainter::Antialiasing, true); @@ -357,6 +358,7 @@ MapperGLCanvas::MapperGLCanvas(MainWindow* mainWindow, QWidget* parent, const QG void MapperGLCanvas::updateCanvas() { update(); + scene()->update(); } void MapperGLCanvas::enableDisplayControls(bool display) @@ -407,3 +409,16 @@ void MapperGLCanvas::deselectAll() _shapeGrabbed = false; _shapeFirstGrab = false; } + +void MapperGLCanvas::wheelEvent(QWheelEvent *event) +{ + _zoomLevel += (event->delta() / 120); + float zoomFactor = qMin(MM::ZOOM_MAX, + qMax(MM::ZOOM_MIN, + qPow(MM::ZOOM_FACTOR, _zoomLevel))); + QGraphicsView* view = scene()->views().first(); + view->resetMatrix(); + view->scale(zoomFactor, zoomFactor); + view->update(); + event->accept(); +} diff --git a/MapperGLCanvas.h b/MapperGLCanvas.h index f8920ef..f7bdbf9 100644 --- a/MapperGLCanvas.h +++ b/MapperGLCanvas.h @@ -28,6 +28,7 @@ #include +#include "MM.h" #include "UidAllocator.h" #include "Shape.h" @@ -140,6 +141,8 @@ private: // True iff we want vertices to stick to each other. bool _stickyVertices; + int _zoomLevel; + signals: void shapeChanged(MShape*); void imageChanged(); @@ -151,6 +154,8 @@ public slots: void deselectVertices(); void deselectAll(); + void wheelEvent(QWheelEvent * event); + public: static const int NO_VERTEX = -1; }; From 3e7fd351cf9eff3f09744aa80ba57cbd79a0f44d Mon Sep 17 00:00:00 2001 From: Tats Date: Sun, 28 Dec 2014 21:51:34 -0500 Subject: [PATCH 13/32] Bug fix: once we reach max or min zoom we still record the scroll events which results in a weird behavior. --- MapperGLCanvas.cpp | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/MapperGLCanvas.cpp b/MapperGLCanvas.cpp index 34269cf..1460a78 100644 --- a/MapperGLCanvas.cpp +++ b/MapperGLCanvas.cpp @@ -412,10 +412,29 @@ void MapperGLCanvas::deselectAll() void MapperGLCanvas::wheelEvent(QWheelEvent *event) { - _zoomLevel += (event->delta() / 120); - float zoomFactor = qMin(MM::ZOOM_MAX, - qMax(MM::ZOOM_MIN, - qPow(MM::ZOOM_FACTOR, _zoomLevel))); + int deltaLevel = event->delta() / 120; + qreal zoomFactor = qPow(MM::ZOOM_FACTOR, _zoomLevel); + if (deltaLevel > 0) + { + // First check if we're already at max. + while (deltaLevel && zoomFactor < MM::ZOOM_MAX) { + _zoomLevel++; + deltaLevel--; + zoomFactor = qPow(MM::ZOOM_FACTOR, _zoomLevel); + } + zoomFactor = qMin(zoomFactor, MM::ZOOM_MAX); + } + else + { + // First check if we're already at min. + while (deltaLevel && zoomFactor > MM::ZOOM_MIN) { + _zoomLevel--; + deltaLevel++; + zoomFactor = qPow(MM::ZOOM_FACTOR, _zoomLevel); + } + zoomFactor = qMax(zoomFactor, MM::ZOOM_MIN); + } + QGraphicsView* view = scene()->views().first(); view->resetMatrix(); view->scale(zoomFactor, zoomFactor); From c6a3de501fc644bd8cbf32c47ca4e06194a7089d Mon Sep 17 00:00:00 2001 From: Tats Date: Sun, 28 Dec 2014 21:51:44 -0500 Subject: [PATCH 14/32] Added comment for zoom level. --- MapperGLCanvas.h | 1 + 1 file changed, 1 insertion(+) diff --git a/MapperGLCanvas.h b/MapperGLCanvas.h index f7bdbf9..f56ab6d 100644 --- a/MapperGLCanvas.h +++ b/MapperGLCanvas.h @@ -141,6 +141,7 @@ private: // True iff we want vertices to stick to each other. bool _stickyVertices; + // The zoom level (in number of steps). int _zoomLevel; signals: From 7c642e857045beb58d0caa581baeadcb8a83920f Mon Sep 17 00:00:00 2001 From: Tats Date: Sun, 28 Dec 2014 22:28:08 -0500 Subject: [PATCH 15/32] Commented out remaining Mapper code from old approach. --- Mapper.cpp | 96 +++++++++++++++++++++++++++--------------------------- Mapper.h | 34 +++++++++---------- 2 files changed, 65 insertions(+), 65 deletions(-) diff --git a/Mapper.cpp b/Mapper.cpp index 6dff56f..5918cde 100644 --- a/Mapper.cpp +++ b/Mapper.cpp @@ -427,54 +427,54 @@ void ColorMapper::updatePaint() Q_CHECK_PTR(color); } -MeshColorMapper::MeshColorMapper(Mapping::ptr mapping) - : ColorMapper(mapping) { - // Add mesh sub property. - Mesh* mesh = (Mesh*)mapping->getShape().get(); - _meshItem = _variantManager->addProperty(QVariant::Size, QObject::tr("Dimensions")); - _meshItem->setValue(QSize(mesh->nColumns(), mesh->nRows())); - _topItem->insertSubProperty(_meshItem, 0); // insert at the beginning -} - -void MeshColorMapper::draw(QPainter* painter) -{ - painter->setPen(Qt::NoPen); - painter->setBrush(color->getColor()); - - std::tr1::shared_ptr outputMesh = std::tr1::static_pointer_cast(outputShape); - QVector > outputQuads = outputMesh->getQuads2d(); - for (int x = 0; x < outputMesh->nHorizontalQuads(); x++) - { - for (int y = 0; y < outputMesh->nVerticalQuads(); y++) - { - Quad& outputQuad = outputQuads[x][y]; - painter->drawPolygon(outputQuad.toPolygon()); - } - } -} - -void MeshColorMapper::drawControls(QPainter* painter, const QList* selectedVertices) -{ - std::tr1::shared_ptr outputMesh = std::tr1::static_pointer_cast(outputShape); - Util::drawControlsMesh(painter, selectedVertices, *outputMesh); -} - -void MeshColorMapper::setValue(QtProperty* property, const QVariant& value) -{ - if (property == _meshItem) - { - Mesh* outputMesh = static_cast(_mapping->getShape().get()); - QSize size = (static_cast(property))->value().toSize(); - if (outputMesh->nColumns() != size.width() || outputMesh->nRows() != size.height()) - { - outputMesh->resize(size.width(), size.height()); - - emit valueChanged(); - } - } - else - ColorMapper::setValue(property, value); -} +//MeshColorMapper::MeshColorMapper(Mapping::ptr mapping) +// : ColorMapper(mapping) { +// // Add mesh sub property. +// Mesh* mesh = (Mesh*)mapping->getShape().get(); +// _meshItem = _variantManager->addProperty(QVariant::Size, QObject::tr("Dimensions")); +// _meshItem->setValue(QSize(mesh->nColumns(), mesh->nRows())); +// _topItem->insertSubProperty(_meshItem, 0); // insert at the beginning +//} +// +//void MeshColorMapper::draw(QPainter* painter) +//{ +// painter->setPen(Qt::NoPen); +// painter->setBrush(color->getColor()); +// +// std::tr1::shared_ptr outputMesh = std::tr1::static_pointer_cast(outputShape); +// QVector > outputQuads = outputMesh->getQuads2d(); +// for (int x = 0; x < outputMesh->nHorizontalQuads(); x++) +// { +// for (int y = 0; y < outputMesh->nVerticalQuads(); y++) +// { +// Quad& outputQuad = outputQuads[x][y]; +// painter->drawPolygon(outputQuad.toPolygon()); +// } +// } +//} +// +//void MeshColorMapper::drawControls(QPainter* painter, const QList* selectedVertices) +//{ +// std::tr1::shared_ptr outputMesh = std::tr1::static_pointer_cast(outputShape); +// Util::drawControlsMesh(painter, selectedVertices, *outputMesh); +//} +// +//void MeshColorMapper::setValue(QtProperty* property, const QVariant& value) +//{ +// if (property == _meshItem) +// { +// Mesh* outputMesh = static_cast(_mapping->getShape().get()); +// QSize size = (static_cast(property))->value().toSize(); +// if (outputMesh->nColumns() != size.width() || outputMesh->nRows() != size.height()) +// { +// outputMesh->resize(size.width(), size.height()); +// +// emit valueChanged(); +// } +// } +// else +// ColorMapper::setValue(property, value); +//} TextureMapper::TextureMapper(std::tr1::shared_ptr mapping) : Mapper(mapping), diff --git a/Mapper.h b/Mapper.h index 31326ac..afbb2cf 100644 --- a/Mapper.h +++ b/Mapper.h @@ -276,23 +276,23 @@ public: virtual ~PolygonColorMapper() {} }; -class MeshColorMapper : public ColorMapper -{ - Q_OBJECT - -public: - MeshColorMapper(Mapping::ptr mapping); - virtual ~MeshColorMapper() {} - - virtual void draw(QPainter* painter); - virtual void drawControls(QPainter* painter, const QList* selectedVertices=0); - -public slots: - virtual void setValue(QtProperty* property, const QVariant& value); - -private: - QtVariantProperty* _meshItem; -}; +//class MeshColorMapper : public ColorMapper +//{ +// Q_OBJECT +// +//public: +// MeshColorMapper(Mapping::ptr mapping); +// virtual ~MeshColorMapper() {} +// +// virtual void draw(QPainter* painter); +// virtual void drawControls(QPainter* painter, const QList* selectedVertices=0); +// +//public slots: +// virtual void setValue(QtProperty* property, const QVariant& value); +// +//private: +// QtVariantProperty* _meshItem; +//}; class EllipseColorMapper : public ColorMapper { From 067a84cf123e53195ab9fae599d980eaed172d29 Mon Sep 17 00:00:00 2001 From: Tats Date: Sun, 28 Dec 2014 22:29:47 -0500 Subject: [PATCH 16/32] Changing the ordering of mappings now changes the order in which they are actually displayed (layers). --- Mapper.cpp | 28 +++++++++++++++++----------- Mapper.h | 23 ++++++++++++++++------- Mapping.cpp | 3 +++ Mapping.h | 8 ++++++-- MappingManager.cpp | 7 ++++++- 5 files changed, 48 insertions(+), 21 deletions(-) diff --git a/Mapper.cpp b/Mapper.cpp index 5918cde..9c5d3bf 100644 --- a/Mapper.cpp +++ b/Mapper.cpp @@ -74,6 +74,17 @@ void ShapeGraphicsItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) _syncShape(); } +void ShapeGraphicsItem::paint(QPainter *painter, + const QStyleOptionGraphicsItem *option, QWidget *widget) +{ + // Sync depth of figure with that of mapping (for layered output). + setZValue(_mapping->getDepth()); + + // Paint if visible. + if (_mapping->isVisible()) + _doPaint(painter, option); +} + //QVariant ShapeGraphicsItem::itemChange(GraphicsItemChange change, const QVariant &value) //{ // if (change == ItemPositionChange) @@ -137,11 +148,9 @@ QRectF PolygonColorGraphicsItem::boundingRect() const return shape().boundingRect(); } -void PolygonColorGraphicsItem::paint(QPainter *painter, - const QStyleOptionGraphicsItem *option, QWidget *widget) +void PolygonColorGraphicsItem::_doPaint(QPainter *painter, + const QStyleOptionGraphicsItem *option) { - Q_UNUSED(widget); - Color* color = static_cast(_mapping->getPaint().get()); Q_ASSERT(color); @@ -177,8 +186,8 @@ QRectF EllipseColorGraphicsItem::boundingRect() const return shape().boundingRect(); } -void EllipseColorGraphicsItem::paint(QPainter* painter, - const QStyleOptionGraphicsItem* option, QWidget* widget) +void EllipseColorGraphicsItem::_doPaint(QPainter* painter, + const QStyleOptionGraphicsItem* option) { Color* color = static_cast(_mapping->getPaint().get()); Q_ASSERT(color); @@ -207,11 +216,9 @@ TextureGraphicsItem::TextureGraphicsItem(Mapping::ptr mapping, bool output) Q_CHECK_PTR(_inputShape); } -void TextureGraphicsItem::paint(QPainter *painter, - const QStyleOptionGraphicsItem *option, QWidget *widget) +void TextureGraphicsItem::_doPaint(QPainter *painter, + const QStyleOptionGraphicsItem *option) { - Q_UNUSED(widget); - bool selected = option->state & QStyle::State_Selected; // Prepare drawing. @@ -279,7 +286,6 @@ QRectF PolygonTextureGraphicsItem::boundingRect() const { return shape().boundingRect(); } - void TriangleTextureGraphicsItem::_doDraw(QPainter* painter, bool selected) { Q_UNUSED(painter); diff --git a/Mapper.h b/Mapper.h index afbb2cf..0ccff79 100644 --- a/Mapper.h +++ b/Mapper.h @@ -77,11 +77,16 @@ public: void mouseMoveEvent(QGraphicsSceneMouseEvent *event); // virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value); + virtual void paint(QPainter *painter, + const QStyleOptionGraphicsItem *option, QWidget *widget); + protected: virtual void _createVertices(); virtual void _syncShape(); virtual void _syncVertices(); + virtual void _doPaint(QPainter *painter, const QStyleOptionGraphicsItem *option) = 0; + Mapping::ptr _mapping; MShape::ptr _shape; bool _output; @@ -99,7 +104,8 @@ public: virtual ~VertexGraphicsItem() {} virtual void paint(QPainter *painter, - const QStyleOptionGraphicsItem *option, QWidget *widget) + const QStyleOptionGraphicsItem *option, + QWidget* widget) { Q_UNUSED(widget); Util::drawControlsVertex(painter, QPointF(0,0), (option->state & QStyle::State_Selected)); @@ -119,8 +125,9 @@ public: virtual QPainterPath shape() const; virtual QRectF boundingRect() const; - virtual void paint(QPainter *painter, - const QStyleOptionGraphicsItem *option, QWidget *widget); +protected: + virtual void _doPaint(QPainter *painter, + const QStyleOptionGraphicsItem *option); }; /// Graphics item for colored polygons (eg. quad, triangle). @@ -133,8 +140,9 @@ public: virtual QPainterPath shape() const; virtual QRectF boundingRect() const; - virtual void paint(QPainter *painter, - const QStyleOptionGraphicsItem *option, QWidget *widget); +protected: + virtual void _doPaint(QPainter *painter, + const QStyleOptionGraphicsItem *option); }; /// Abstract class for texture graphics items. @@ -145,8 +153,9 @@ public: virtual ~TextureGraphicsItem() {} - virtual void paint(QPainter *painter, - const QStyleOptionGraphicsItem *option, QWidget *widget); +protected: + virtual void _doPaint(QPainter *painter, + const QStyleOptionGraphicsItem *option); virtual void _doDraw(QPainter* painter, bool selected) = 0; virtual void _doDrawControls(QPainter* painter) { Q_UNUSED(painter); } diff --git a/Mapping.cpp b/Mapping.cpp index e1c6301..75b8f94 100644 --- a/Mapping.cpp +++ b/Mapping.cpp @@ -36,6 +36,9 @@ Mapping::Mapping(Paint::ptr paint, MShape::ptr shape, uid id) // Assign id. _id = id; + + // Default. + _depth = _id; } Mapping::~Mapping() { diff --git a/Mapping.h b/Mapping.h index 8a0d013..5f2dc11 100644 --- a/Mapping.h +++ b/Mapping.h @@ -57,6 +57,7 @@ private: bool _isSolo; bool _isVisible; float _opacity; + int _depth; // depth of the layer protected: /// Constructor. @@ -102,8 +103,7 @@ public: Q_ASSERT(0.0f <= opacity && opacity <= 1.0f); _opacity = opacity; } - void setPaint(Paint::ptr p) { _paint = p; } - void removePaint() { if (_paint) delete _paint.get(); } + void setDepth(int depth) { _depth = depth; } void toggleLocked() { _isLocked = !_isLocked; } void toggleSolo() { _isSolo = !_isSolo; } @@ -113,6 +113,10 @@ public: bool isSolo() const { return _isSolo; } bool isVisible() const { return _isVisible; } float getOpacity() const { return _opacity; } + int getDepth() const { return _depth; } + + void setPaint(Paint::ptr p) { _paint = p; } + void removePaint() { if (_paint) delete _paint.get(); } }; /** diff --git a/MappingManager.cpp b/MappingManager.cpp index 14c8735..b44e5ad 100644 --- a/MappingManager.cpp +++ b/MappingManager.cpp @@ -152,6 +152,7 @@ void MappingManager::reorderMappings(QVector mappingIds) // Both vector needs to have the same size. Q_ASSERT( mappingIds.size() == mappingVector.size() ); mappingVector.clear(); + int depth = 0; for (QVector::iterator it = mappingIds.begin(); it != mappingIds.end(); ++it) { // Uid should be a key of the mappingMap. @@ -159,7 +160,11 @@ void MappingManager::reorderMappings(QVector mappingIds) // Makes sure the uids are not repeated. Q_ASSERT( !mappingVector.contains(mappingMap[*it]) ); // Adds the mapping at the right place in the vector. - mappingVector.push_back( mappingMap[*it] ); + Mapping::ptr mapping = mappingMap[*it]; + mapping->setDepth(depth); + mappingVector.push_back( mapping ); + + depth++; } } From e22075d6ad1fe7c230bcad5ff8586976c0e0b177 Mon Sep 17 00:00:00 2001 From: Tats Date: Sun, 15 Feb 2015 17:46:54 -0500 Subject: [PATCH 17/32] Base working integration of destination and output window (with crosshair working in fullscreen). --- DestinationGLCanvas.cpp | 17 +++++++++++++++-- DestinationGLCanvas.h | 5 ++++- MainWindow.cpp | 36 ++++++++++++++++++++++++------------ MapperGLCanvas.cpp | 9 ++++----- MapperGLCanvas.h | 6 ++---- OutputGLWindow.cpp | 26 +++++++++++++++++++++++--- OutputGLWindow.h | 3 ++- 7 files changed, 74 insertions(+), 28 deletions(-) diff --git a/DestinationGLCanvas.cpp b/DestinationGLCanvas.cpp index a9894e5..f22225a 100644 --- a/DestinationGLCanvas.cpp +++ b/DestinationGLCanvas.cpp @@ -21,8 +21,8 @@ #include "DestinationGLCanvas.h" #include "MainWindow.h" -DestinationGLCanvas::DestinationGLCanvas(MainWindow* mainWindow, QWidget* parent, const QGLWidget * shareWidget) -: MapperGLCanvas(mainWindow, parent, shareWidget), +DestinationGLCanvas::DestinationGLCanvas(MainWindow* mainWindow, QWidget* parent, const QGLWidget* shareWidget, QGraphicsScene* scene) +: MapperGLCanvas(mainWindow, parent, shareWidget, scene), _displayCrosshair(false) { } @@ -35,6 +35,19 @@ MShape* DestinationGLCanvas::getShapeFromMappingId(uid mappingId) return getMainWindow()->getMappingManager().getMappingById(mappingId)->getShape().get(); } +void DestinationGLCanvas::drawForeground(QPainter *painter , const QRectF &rect) { + if (_displayCrosshair) + { + QPointF cursorPosition = mapToScene(cursor().pos());// - rect.topLeft();//(QCursor::pos());///*this->mapFromGlobal(*/QCursor::pos()/*)*/; + if (rect.contains(cursorPosition)) + { + painter->setPen(MM::CONTROL_COLOR); + painter->drawLine(cursorPosition.x(), rect.y(), cursorPosition.x(), rect.height()); + painter->drawLine(rect.x(), cursorPosition.y(), rect.width(), cursorPosition.y()); + } + } +} + //void DestinationGLCanvas::doDraw(QPainter* painter) //{ // glPushMatrix(); diff --git a/DestinationGLCanvas.h b/DestinationGLCanvas.h index 8057480..6074f4d 100644 --- a/DestinationGLCanvas.h +++ b/DestinationGLCanvas.h @@ -31,7 +31,7 @@ class DestinationGLCanvas: public MapperGLCanvas Q_OBJECT public: - DestinationGLCanvas(MainWindow* mainWindow, QWidget* parent = 0, const QGLWidget * shareWidget = 0); + DestinationGLCanvas(MainWindow* mainWindow, QWidget* parent = 0, const QGLWidget* shareWidget = 0, QGraphicsScene* scene = 0); virtual ~DestinationGLCanvas() {} virtual MShape* getShapeFromMappingId(uid mappingId); @@ -40,6 +40,9 @@ public: _displayCrosshair = displayCrosshair; } + // Draws foreground (displays crosshair if needed). + void drawForeground(QPainter *painter , const QRectF &rect); + private: // virtual void doDraw(QPainter* painter); diff --git a/MainWindow.cpp b/MainWindow.cpp index b0962a0..450d344 100644 --- a/MainWindow.cpp +++ b/MainWindow.cpp @@ -981,24 +981,34 @@ void MainWindow::createLayout() destinationCanvas->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); destinationCanvas->setMinimumSize(CANVAS_MINIMUM_WIDTH, CANVAS_MINIMUM_HEIGHT); - outputWindow = new OutputGLWindow(this, this, (QGLWidget*)sourceCanvas->viewport()); + outputWindow = new OutputGLWindow(destinationCanvas); outputWindow->setVisible(true); // Source changed -> change destination - connect(sourceCanvas, SIGNAL(shapeChanged(Shape*)), - destinationCanvas, SLOT(updateCanvas())); +// connect(sourceCanvas, SIGNAL(shapeChanged(MShape*)), +// destinationCanvas, SLOT(updateCanvas())); + + connect(sourceCanvas->scene(), SIGNAL(changed(const QList&)), + destinationCanvas, SLOT(updateCanvas())); // Source changed -> change output window - connect(sourceCanvas, SIGNAL(shapeChanged(Shape*)), +// connect(sourceCanvas, SIGNAL(shapeChanged(MShape*)), +// outputWindow->getCanvas(), SLOT(updateCanvas())); + + connect(sourceCanvas->scene(), SIGNAL(changed(const QList&)), outputWindow->getCanvas(), SLOT(updateCanvas())); // Destination changed -> change output window - connect(destinationCanvas, SIGNAL(shapeChanged(Shape*)), - outputWindow->getCanvas(), SLOT(updateCanvas())); +// connect(destinationCanvas, SIGNAL(shapeChanged(MShape*)), +// outputWindow->getCanvas(), SLOT(updateCanvas())); + + connect(destinationCanvas->scene(), SIGNAL(changed(const QList&)), + outputWindow->getCanvas(), SLOT(updateCanvas())); // Output changed -> change destinatioin - connect(outputWindow->getCanvas(), SIGNAL(shapeChanged(Shape*)), - destinationCanvas, SLOT(updateCanvas())); + // XXX si je decommente cette ligne alors quand je clique sur ajouter media ca gele... +// connect(outputWindow->getCanvas()->scene(), SIGNAL(changed(const QList&)), +// destinationCanvas, SLOT(updateCanvas())); // Create layout. paintSplitter = new QSplitter(Qt::Vertical); @@ -1868,11 +1878,11 @@ void MainWindow::addMappingItem(uid mappingId) connect(mapper.get(), SIGNAL(valueChanged()), this, SLOT(updateCanvases())); - connect(sourceCanvas, SIGNAL(shapeChanged(Shape*)), - mapper.get(), SLOT(updateShape(Shape*))); + connect(sourceCanvas, SIGNAL(shapeChanged(MShape*)), + mapper.get(), SLOT(updateShape(MShape*))); - connect(destinationCanvas, SIGNAL(shapeChanged(Shape*)), - mapper.get(), SLOT(updateShape(Shape*))); + connect(destinationCanvas, SIGNAL(shapeChanged(MShape*)), + mapper.get(), SLOT(updateShape(MShape*))); connect(this, SIGNAL(paintChanged()), mapper.get(), SLOT(updatePaint())); @@ -1895,6 +1905,8 @@ void MainWindow::addMappingItem(uid mappingId) sourceCanvas->scene()->addItem(mapper->getInputGraphicsItem()); if (mapper->getGraphicsItem()) destinationCanvas->scene()->addItem(mapper->getGraphicsItem()); + if (mapper->getGraphicsItem()) + outputWindow->getCanvas()->scene()->addItem(mapper->getGraphicsItem()); // Window was modified. windowModified(); diff --git a/MapperGLCanvas.cpp b/MapperGLCanvas.cpp index 1460a78..bcbd936 100644 --- a/MapperGLCanvas.cpp +++ b/MapperGLCanvas.cpp @@ -22,7 +22,7 @@ #include "MainWindow.h" -MapperGLCanvas::MapperGLCanvas(MainWindow* mainWindow, QWidget* parent, const QGLWidget * shareWidget) +MapperGLCanvas::MapperGLCanvas(MainWindow* mainWindow, QWidget* parent, const QGLWidget * shareWidget, QGraphicsScene* scene) : QGraphicsView(parent), _mainWindow(mainWindow), _mousePressedOnVertex(false), @@ -48,13 +48,13 @@ MapperGLCanvas::MapperGLCanvas(MainWindow* mainWindow, QWidget* parent, const QG // Render with OpenGL. setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers), this, shareWidget)); + setViewportUpdateMode(QGraphicsView::FullViewportUpdate); // TODO: do we need to delete scene (or call new QGraphicsScene(this)?) -// _scene = new QGraphicsScene; - setScene(new QGraphicsScene); + setScene(scene ? scene : new QGraphicsScene); // Black background. - scene()->setBackgroundBrush(Qt::black); + this->scene()->setBackgroundBrush(Qt::black); } //void MapperGLCanvas::initializeGL() @@ -396,7 +396,6 @@ void MapperGLCanvas::glueVertex(MShape *orig, QPointF *p) } } - void MapperGLCanvas::deselectVertices() { _activeVertex = NO_VERTEX; diff --git a/MapperGLCanvas.h b/MapperGLCanvas.h index f56ab6d..d326492 100644 --- a/MapperGLCanvas.h +++ b/MapperGLCanvas.h @@ -44,7 +44,7 @@ class MapperGLCanvas: public QGraphicsView public: /// Constructor. - MapperGLCanvas(MainWindow* mainWindow, QWidget* parent = 0, const QGLWidget* shareWidget = 0); + MapperGLCanvas(MainWindow* mainWindow, QWidget* parent = 0, const QGLWidget* shareWidget = 0, QGraphicsScene* scene = 0); virtual ~MapperGLCanvas() {} /// Returns shape associated with mapping id. @@ -67,8 +67,6 @@ public: /// Returns pointer to main window. MainWindow* getMainWindow() const { return _mainWindow; } -// QGLWidget* getViewport() const { return _viewport; } - /// Returns true iff we should display the controls. bool displayControls() const { return _displayControls; } @@ -117,7 +115,7 @@ private: MainWindow* _mainWindow; // QGraphicsScene* _scene; - +// // QGLWidget* _viewport; // Last point pressed. diff --git a/OutputGLWindow.cpp b/OutputGLWindow.cpp index 0e562df..1f16c89 100644 --- a/OutputGLWindow.cpp +++ b/OutputGLWindow.cpp @@ -21,11 +21,11 @@ #include "MainWindow.h" -OutputGLWindow::OutputGLWindow(MainWindow* mainWindow, QWidget* parent, const QGLWidget * shareWidget) : QDialog(parent) +OutputGLWindow:: OutputGLWindow(const DestinationGLCanvas* canvas_) { resize(MainWindow::OUTPUT_WINDOW_MINIMUM_WIDTH, MainWindow::OUTPUT_WINDOW_MINIMUM_HEIGHT); - canvas = new DestinationGLCanvas(mainWindow, this, shareWidget); + canvas = new DestinationGLCanvas(canvas_->getMainWindow(), this, (const QGLWidget*)canvas_->viewport(), canvas_->scene()); canvas->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); canvas->setMinimumSize(MainWindow::OUTPUT_WINDOW_MINIMUM_WIDTH, MainWindow::OUTPUT_WINDOW_MINIMUM_HEIGHT); @@ -38,9 +38,28 @@ OutputGLWindow::OutputGLWindow(MainWindow* mainWindow, QWidget* parent, const QG _geometry = saveGeometry(); _pointerIsVisible = true; - } +//OutputGLWindow::OutputGLWindow(MainWindow* mainWindow, QWidget* parent, const QGLWidget * shareWidget) : QDialog(parent) +//{ +// resize(MainWindow::OUTPUT_WINDOW_MINIMUM_WIDTH, MainWindow::OUTPUT_WINDOW_MINIMUM_HEIGHT); +// +// canvas = new DestinationGLCanvas(mainWindow, this, shareWidget); +// canvas->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); +// canvas->setMinimumSize(MainWindow::OUTPUT_WINDOW_MINIMUM_WIDTH, MainWindow::OUTPUT_WINDOW_MINIMUM_HEIGHT); +// +// QLayout* layout = new QVBoxLayout; +// layout->setContentsMargins(0,0,0,0); // remove margin +// layout->addWidget(canvas); +// setLayout(layout); +// +// // Save window geometry. +// _geometry = saveGeometry(); +// +// _pointerIsVisible = true; +// +//} + void OutputGLWindow::setCursorVisible(bool visible) { _pointerIsVisible = visible; @@ -52,6 +71,7 @@ void OutputGLWindow::setCursorVisible(bool visible) } else { + this->setCursor(Qt::ArrowCursor); this->setCursor(Qt::BlankCursor); _pointerIsVisible = false; } diff --git a/OutputGLWindow.h b/OutputGLWindow.h index 768df62..642ef46 100644 --- a/OutputGLWindow.h +++ b/OutputGLWindow.h @@ -34,7 +34,8 @@ class OutputGLWindow : public QDialog Q_OBJECT public: - OutputGLWindow(MainWindow* mainWindow, QWidget* parent = 0, const QGLWidget * shareWidget = 0); + OutputGLWindow(const DestinationGLCanvas* canvas_); + //OutputGLWindow(MainWindow* mainWindow, QWidget* parent = 0, const QGLWidget * shareWidget = 0); public slots: void setFullScreen(bool fullScreen); From 36c2c2b539bf989414a42a7f93d4ab4ce15cd8ae Mon Sep 17 00:00:00 2001 From: Tats Date: Fri, 3 Jul 2015 17:20:42 -0600 Subject: [PATCH 18/32] Implemented MainWindow singleton pattern. --- MainWindow.cpp | 8 ++++++++ MainWindow.h | 2 ++ main.cpp | 17 ++++++++++------- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/MainWindow.cpp b/MainWindow.cpp index 450d344..2126a62 100644 --- a/MainWindow.cpp +++ b/MainWindow.cpp @@ -1988,6 +1988,14 @@ void MainWindow::clearWindow() clearProject(); } +MainWindow* MainWindow::instance() { + static MainWindow* inst = 0; + if (!inst) { + inst = new MainWindow; + } + return inst; +} + void MainWindow::updateCanvases() { sourceCanvas->updateCanvas(); diff --git a/MainWindow.h b/MainWindow.h index 6acbb52..af86c6f 100644 --- a/MainWindow.h +++ b/MainWindow.h @@ -213,6 +213,8 @@ public: void removePaintItem(uid paintId); void clearWindow(); + static MainWindow* instance(); + // Returns a short version of filename. static QString strippedName(const QString &fullFileName); diff --git a/main.cpp b/main.cpp index 1790185..af3f05d 100644 --- a/main.cpp +++ b/main.cpp @@ -113,7 +113,7 @@ int main(int argc, char *argv[]) I::sleep(1); // Create window. - MainWindow win; + MainWindow* win = MainWindow::instance(); QFontDatabase db; Q_ASSERT( QFontDatabase::addApplicationFont(":/base-font") != -1); @@ -130,34 +130,37 @@ int main(int argc, char *argv[]) QString projectFileValue = parser.value("file"); if (projectFileValue != "") { - win.loadFile(projectFileValue); + win->loadFile(projectFileValue); } QString oscPortNumberValue = parser.value("osc-port"); if (oscPortNumberValue != "") { - win.setOscPort(oscPortNumberValue); + win->setOscPort(oscPortNumberValue); } #endif // Terminate splash. splash.showMessage(" " + QObject::tr("Done."), Qt::AlignLeft | Qt::AlignTop, MM::WHITE); - splash.finish(&win); + splash.finish(win); splash.raise(); // Launch program. - win.show(); + win->show(); #if USING_QT_5 if (parser.isSet(fullscreenOption)) { qDebug() << "TODO: Running in fullscreen mode"; - win.startFullScreen(); + win->startFullScreen(); } #endif // Start app. - return app.exec(); + int result = app.exec(); + + delete win; + return result; } From f0c4bb1fa946357004a6534255af1cbeb74bdca2 Mon Sep 17 00:00:00 2001 From: Tats Date: Fri, 3 Jul 2015 17:21:11 -0600 Subject: [PATCH 19/32] Allow display of controls only in currently selected mapping. --- Mapper.cpp | 11 ++++++++++- Mapper.h | 6 +----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/Mapper.cpp b/Mapper.cpp index 9c5d3bf..fb46317 100644 --- a/Mapper.cpp +++ b/Mapper.cpp @@ -21,6 +21,15 @@ #include "Mapper.h" #include "MainWindow.h" +void VertexGraphicsItem::paint(QPainter *painter, + const QStyleOptionGraphicsItem *option, + QWidget* widget) +{ + Q_UNUSED(widget); + if (((ShapeGraphicsItem*)parentItem())->getMapping()->getId() == MainWindow::instance()->getCurrentMappingId()) + Util::drawControlsVertex(painter, QPointF(0,0), (option->state & QStyle::State_Selected)); +} + ShapeGraphicsItem::ShapeGraphicsItem(Mapping::ptr mapping, bool output) : _mapping(mapping), _output(output) { @@ -230,7 +239,7 @@ void TextureGraphicsItem::_doPaint(QPainter *painter, // End drawing. _postDraw(painter); -// if (selected) + if (getMapping()->getId() == MainWindow::instance()->getCurrentMappingId()) _doDrawControls(painter); } diff --git a/Mapper.h b/Mapper.h index 0ccff79..732d724 100644 --- a/Mapper.h +++ b/Mapper.h @@ -105,11 +105,7 @@ public: virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, - QWidget* widget) - { - Q_UNUSED(widget); - Util::drawControlsVertex(painter, QPointF(0,0), (option->state & QStyle::State_Selected)); - } + QWidget* widget); protected: int _index; From 9ba817847f4ddcf36e382ae8f14ab0d5aff98d17 Mon Sep 17 00:00:00 2001 From: Tats Date: Fri, 3 Jul 2015 19:01:51 -0600 Subject: [PATCH 20/32] Bugfix: Infinite call loop created by shared scene in destination and outputwindow canvases and the updateCanvas() slot. --- MainWindow.cpp | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/MainWindow.cpp b/MainWindow.cpp index 2126a62..bed1ece 100644 --- a/MainWindow.cpp +++ b/MainWindow.cpp @@ -984,26 +984,13 @@ void MainWindow::createLayout() outputWindow = new OutputGLWindow(destinationCanvas); outputWindow->setVisible(true); - // Source changed -> change destination -// connect(sourceCanvas, SIGNAL(shapeChanged(MShape*)), -// destinationCanvas, SLOT(updateCanvas())); - + // Source scene changed -> change destination. connect(sourceCanvas->scene(), SIGNAL(changed(const QList&)), - destinationCanvas, SLOT(updateCanvas())); - - // Source changed -> change output window -// connect(sourceCanvas, SIGNAL(shapeChanged(MShape*)), -// outputWindow->getCanvas(), SLOT(updateCanvas())); - - connect(sourceCanvas->scene(), SIGNAL(changed(const QList&)), - outputWindow->getCanvas(), SLOT(updateCanvas())); - - // Destination changed -> change output window -// connect(destinationCanvas, SIGNAL(shapeChanged(MShape*)), -// outputWindow->getCanvas(), SLOT(updateCanvas())); + destinationCanvas, SLOT(update())); + // Destination scene changed -> change output window. connect(destinationCanvas->scene(), SIGNAL(changed(const QList&)), - outputWindow->getCanvas(), SLOT(updateCanvas())); + outputWindow->getCanvas(), SLOT(update())); // Output changed -> change destinatioin // XXX si je decommente cette ligne alors quand je clique sur ajouter media ca gele... From 93edb4e41a91bd64b78f2746c4d958e861fd849a Mon Sep 17 00:00:00 2001 From: Tats Date: Fri, 3 Jul 2015 19:04:52 -0600 Subject: [PATCH 21/32] Bug fix: tried to addItem() twice on outputwindow scene. --- MainWindow.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/MainWindow.cpp b/MainWindow.cpp index bed1ece..2c338cf 100644 --- a/MainWindow.cpp +++ b/MainWindow.cpp @@ -1892,8 +1892,6 @@ void MainWindow::addMappingItem(uid mappingId) sourceCanvas->scene()->addItem(mapper->getInputGraphicsItem()); if (mapper->getGraphicsItem()) destinationCanvas->scene()->addItem(mapper->getGraphicsItem()); - if (mapper->getGraphicsItem()) - outputWindow->getCanvas()->scene()->addItem(mapper->getGraphicsItem()); // Window was modified. windowModified(); From 522ecac194a88f286300064681de49a745dee1d3 Mon Sep 17 00:00:00 2001 From: Tats Date: Fri, 3 Jul 2015 21:51:01 -0600 Subject: [PATCH 22/32] Cosmetic change. --- Mapper.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Mapper.cpp b/Mapper.cpp index fb46317..c35d19d 100644 --- a/Mapper.cpp +++ b/Mapper.cpp @@ -21,15 +21,6 @@ #include "Mapper.h" #include "MainWindow.h" -void VertexGraphicsItem::paint(QPainter *painter, - const QStyleOptionGraphicsItem *option, - QWidget* widget) -{ - Q_UNUSED(widget); - if (((ShapeGraphicsItem*)parentItem())->getMapping()->getId() == MainWindow::instance()->getCurrentMappingId()) - Util::drawControlsVertex(painter, QPointF(0,0), (option->state & QStyle::State_Selected)); -} - ShapeGraphicsItem::ShapeGraphicsItem(Mapping::ptr mapping, bool output) : _mapping(mapping), _output(output) { @@ -143,6 +134,15 @@ void ShapeGraphicsItem::_syncVertices() } } +void VertexGraphicsItem::paint(QPainter *painter, + const QStyleOptionGraphicsItem *option, + QWidget* widget) +{ + Q_UNUSED(widget); + if (((ShapeGraphicsItem*)parentItem())->getMapping()->getId() == MainWindow::instance()->getCurrentMappingId()) + Util::drawControlsVertex(painter, QPointF(0,0), (option->state & QStyle::State_Selected)); +} + QPainterPath PolygonColorGraphicsItem::shape() const { QPainterPath path; From 67628915a367c1ea6a42f2c4eaec45f3dc74934c Mon Sep 17 00:00:00 2001 From: Tats Date: Fri, 3 Jul 2015 21:51:38 -0600 Subject: [PATCH 23/32] Handle mapping selection change on pressing mouse on a shape. --- Mapper.cpp | 11 +++++++++++ Mapper.h | 1 + 2 files changed, 12 insertions(+) diff --git a/Mapper.cpp b/Mapper.cpp index c35d19d..d760b59 100644 --- a/Mapper.cpp +++ b/Mapper.cpp @@ -68,6 +68,17 @@ bool ShapeGraphicsItem::sceneEventFilter(QGraphicsItem * watched, QEvent * event } } +void ShapeGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent * event) +{ + QGraphicsItem::mousePressEvent(event); + + // Change mapping to currently selected shape. + if (event->button() == Qt::LeftButton) + { + MainWindow::instance()->setCurrentMapping(_mapping->getId()); + } +} + void ShapeGraphicsItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { QGraphicsItem::mouseMoveEvent(event); diff --git a/Mapper.h b/Mapper.h index 732d724..7f5220d 100644 --- a/Mapper.h +++ b/Mapper.h @@ -74,6 +74,7 @@ public: virtual bool sceneEventFilter(QGraphicsItem * watched, QEvent * event); + void mousePressEvent(QGraphicsSceneMouseEvent *event); void mouseMoveEvent(QGraphicsSceneMouseEvent *event); // virtual QVariant itemChange(GraphicsItemChange change, const QVariant &value); From 082bf2d00967504d2b13fc9f1663ba2bdd24d7a8 Mon Sep 17 00:00:00 2001 From: Tats Date: Fri, 3 Jul 2015 21:51:58 -0600 Subject: [PATCH 24/32] Removed rubberband selection (for now). --- MapperGLCanvas.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/MapperGLCanvas.cpp b/MapperGLCanvas.cpp index bcbd936..3ead4af 100644 --- a/MapperGLCanvas.cpp +++ b/MapperGLCanvas.cpp @@ -33,7 +33,9 @@ MapperGLCanvas::MapperGLCanvas(MainWindow* mainWindow, QWidget* parent, const QG _stickyVertices(true), _zoomLevel(0) { - setDragMode(QGraphicsView::RubberBandDrag); + // For now clicking on the window doesn't do anything. + setDragMode(QGraphicsView::NoDrag); + setRenderHint(QPainter::Antialiasing, true); setRenderHint(QPainter::TextAntialiasing, true); From b206a432a72ac1b37e2a418cb8514c17f62b1fd5 Mon Sep 17 00:00:00 2001 From: Tats Date: Fri, 3 Jul 2015 22:18:07 -0600 Subject: [PATCH 25/32] Prevent default actions on graphics items when mappings not visible. --- Mapper.cpp | 26 +++++++++++++++++++++++--- Mapper.h | 3 +++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/Mapper.cpp b/Mapper.cpp index d760b59..66008d1 100644 --- a/Mapper.cpp +++ b/Mapper.cpp @@ -70,9 +70,10 @@ bool ShapeGraphicsItem::sceneEventFilter(QGraphicsItem * watched, QEvent * event void ShapeGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent * event) { - QGraphicsItem::mousePressEvent(event); + if (!_mapping->isVisible()) + return; - // Change mapping to currently selected shape. + QGraphicsItem::mousePressEvent(event); if (event->button() == Qt::LeftButton) { MainWindow::instance()->setCurrentMapping(_mapping->getId()); @@ -81,6 +82,9 @@ void ShapeGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent * event) void ShapeGraphicsItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { + if (!_mapping->isVisible()) + return; + QGraphicsItem::mouseMoveEvent(event); _syncShape(); } @@ -145,12 +149,28 @@ void ShapeGraphicsItem::_syncVertices() } } +void VertexGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent * event) +{ + ShapeGraphicsItem* shapeParent = static_cast(parentItem()); + if (!shapeParent->getMapping()->isVisible()) + { + // Prevent mouse grabbing. + event->ignore(); + } + else + { + QGraphicsItem::mousePressEvent(event); + } +} + void VertexGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget* widget) { Q_UNUSED(widget); - if (((ShapeGraphicsItem*)parentItem())->getMapping()->getId() == MainWindow::instance()->getCurrentMappingId()) + ShapeGraphicsItem* shapeParent = static_cast(parentItem()); + if (shapeParent->getMapping()->isVisible() && + shapeParent->getMapping()->getId() == MainWindow::instance()->getCurrentMappingId()) Util::drawControlsVertex(painter, QPointF(0,0), (option->state & QStyle::State_Selected)); } diff --git a/Mapper.h b/Mapper.h index 7f5220d..1f42cc7 100644 --- a/Mapper.h +++ b/Mapper.h @@ -104,6 +104,9 @@ public: } virtual ~VertexGraphicsItem() {} + // Prevent mousegrabbing if mapping is invisible. + void mousePressEvent(QGraphicsSceneMouseEvent *event); + virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget* widget); From b9177b941833662a829d11c2196b6e6ca348180f Mon Sep 17 00:00:00 2001 From: Tats Date: Sat, 4 Jul 2015 11:35:48 -0600 Subject: [PATCH 26/32] Fixed a number of problems related to shape selection and display in input and output. --- Mapper.cpp | 91 ++++++++++++++++++++++++++++++++++++++---------------- Mapper.h | 3 ++ 2 files changed, 67 insertions(+), 27 deletions(-) diff --git a/Mapper.cpp b/Mapper.cpp index 66008d1..eeb53bd 100644 --- a/Mapper.cpp +++ b/Mapper.cpp @@ -35,6 +35,8 @@ ShapeGraphicsItem::ShapeGraphicsItem(Mapping::ptr mapping, bool output) _createVertices(); } +bool ShapeGraphicsItem::isMappingCurrent() const { return MainWindow::instance()->getCurrentMappingId() == _mapping->getId(); } + bool ShapeGraphicsItem::sceneEventFilter(QGraphicsItem * watched, QEvent * event) { // Change vertex in model according to moved item. @@ -70,19 +72,29 @@ bool ShapeGraphicsItem::sceneEventFilter(QGraphicsItem * watched, QEvent * event void ShapeGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent * event) { - if (!_mapping->isVisible()) + if (!isMappingVisible()) return; - QGraphicsItem::mousePressEvent(event); - if (event->button() == Qt::LeftButton) + if (isOutput()) { - MainWindow::instance()->setCurrentMapping(_mapping->getId()); + QGraphicsItem::mousePressEvent(event); + if (event->button() == Qt::LeftButton) + { + MainWindow::instance()->setCurrentMapping(_mapping->getId()); + } + } + else + { + if (isMappingCurrent()) + QGraphicsItem::mousePressEvent(event); + else + event->ignore(); // prevent mousegrabbing on non-current mapping } } void ShapeGraphicsItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { - if (!_mapping->isVisible()) + if (!isMappingVisible()) return; QGraphicsItem::mouseMoveEvent(event); @@ -92,12 +104,20 @@ void ShapeGraphicsItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) void ShapeGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { - // Sync depth of figure with that of mapping (for layered output). - setZValue(_mapping->getDepth()); + if (isOutput()) + { + // Sync depth of figure with that of mapping (for layered output). + setZValue(_mapping->getDepth()); - // Paint if visible. - if (_mapping->isVisible()) - _doPaint(painter, option); + // Paint if visible. + if (isMappingVisible()) + _doPaint(painter, option); + } + else + { + if (isMappingVisible()) + _doPaint(painter, option); + } } //QVariant ShapeGraphicsItem::itemChange(GraphicsItemChange change, const QVariant &value) @@ -152,14 +172,28 @@ void ShapeGraphicsItem::_syncVertices() void VertexGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent * event) { ShapeGraphicsItem* shapeParent = static_cast(parentItem()); - if (!shapeParent->getMapping()->isVisible()) + if (!shapeParent->isMappingVisible()) { // Prevent mouse grabbing. event->ignore(); } else { - QGraphicsItem::mousePressEvent(event); + if (shapeParent->isOutput()) + { + QGraphicsItem::mousePressEvent(event); + if (event->button() == Qt::LeftButton) + { + MainWindow::instance()->setCurrentMapping(shapeParent->getMapping()->getId()); + } + } + else + { + if (shapeParent->isMappingCurrent()) + QGraphicsItem::mousePressEvent(event); + else + event->ignore(); // prevent mousegrabbing on non-current mapping + } } } @@ -169,8 +203,8 @@ void VertexGraphicsItem::paint(QPainter *painter, { Q_UNUSED(widget); ShapeGraphicsItem* shapeParent = static_cast(parentItem()); - if (shapeParent->getMapping()->isVisible() && - shapeParent->getMapping()->getId() == MainWindow::instance()->getCurrentMappingId()) + if (shapeParent->isMappingVisible() && + shapeParent->isMappingCurrent()) Util::drawControlsVertex(painter, QPointF(0,0), (option->state & QStyle::State_Selected)); } @@ -270,7 +304,7 @@ void TextureGraphicsItem::_doPaint(QPainter *painter, // End drawing. _postDraw(painter); - if (getMapping()->getId() == MainWindow::instance()->getCurrentMappingId()) + if (isMappingCurrent()) _doDrawControls(painter); } @@ -343,24 +377,27 @@ void TriangleTextureGraphicsItem::_doDraw(QPainter* painter, bool selected) } else { - // FIXME: Does this draw the quad counterclockwise? - glBegin (GL_QUADS); + if (isMappingCurrent()) { - QRectF rect = mapFromScene(_texture->getRect()).boundingRect(); + // FIXME: Does this draw the quad counterclockwise? + glBegin (GL_QUADS); + { + QRectF rect = mapFromScene(_texture->getRect()).boundingRect(); - Util::correctGlTexCoord(0, 0); - glVertex3f (rect.x(), rect.y(), 0); + Util::correctGlTexCoord(0, 0); + glVertex3f (rect.x(), rect.y(), 0); - Util::correctGlTexCoord(1, 0); - glVertex3f (rect.x() + rect.width(), rect.y(), 0); + Util::correctGlTexCoord(1, 0); + glVertex3f (rect.x() + rect.width(), rect.y(), 0); - Util::correctGlTexCoord(1, 1); - glVertex3f (rect.x()+rect.width(), rect.y()+rect.height(), 0); + Util::correctGlTexCoord(1, 1); + glVertex3f (rect.x()+rect.width(), rect.y()+rect.height(), 0); - Util::correctGlTexCoord(0, 1); - glVertex3f (rect.x(), rect.y()+rect.height(), 0); + Util::correctGlTexCoord(0, 1); + glVertex3f (rect.x(), rect.y()+rect.height(), 0); + } + glEnd (); } - glEnd (); } } diff --git a/Mapper.h b/Mapper.h index 1f42cc7..90a723e 100644 --- a/Mapper.h +++ b/Mapper.h @@ -67,6 +67,9 @@ public: bool isOutput() const { return _output; } + bool isMappingCurrent() const; + bool isMappingVisible() const { return _mapping->isVisible(); } + virtual QRectF boundingRect() const = 0; // virtual QPainterPath shape() const; // virtual void paint(QPainter *painter, From 52ebee732fda2c3bd5ca2fe61ae2d46f76591410 Mon Sep 17 00:00:00 2001 From: Tats Date: Mon, 6 Jul 2015 16:56:42 -0600 Subject: [PATCH 27/32] Added a method to reset (clear + recreate) all vertices. --- Mapper.cpp | 14 ++++++++++++++ Mapper.h | 3 +++ 2 files changed, 17 insertions(+) diff --git a/Mapper.cpp b/Mapper.cpp index eeb53bd..5f0da9e 100644 --- a/Mapper.cpp +++ b/Mapper.cpp @@ -131,6 +131,20 @@ void ShapeGraphicsItem::paint(QPainter *painter, // return QGraphicsItem::itemChange(change, value); //} +void ShapeGraphicsItem::resetVertices() { + // Clear vertices. + QList allChildren = children(); + for (QList::iterator it = allChildren.begin(); it!=allChildren.end(); ++it) + { + (*it)->setParentItem(0); + scene()->removeItem(*it); + delete (*it); + } + + // Re-create them. + _createVertices(); +} + void ShapeGraphicsItem::_createVertices() { // rect offset diff --git a/Mapper.h b/Mapper.h index 90a723e..28f4194 100644 --- a/Mapper.h +++ b/Mapper.h @@ -84,6 +84,9 @@ public: virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); +public: + void resetVertices(); + protected: virtual void _createVertices(); virtual void _syncShape(); From a98577de4414d85d57d86343306e582efad9ad7c Mon Sep 17 00:00:00 2001 From: Tats Date: Mon, 6 Jul 2015 16:58:01 -0600 Subject: [PATCH 28/32] Generalization of the texture input drawing. --- Mapper.cpp | 38 ++++++++++++++++++++++++++++++++++---- Mapper.h | 19 +++++++++++-------- 2 files changed, 45 insertions(+), 12 deletions(-) diff --git a/Mapper.cpp b/Mapper.cpp index 5f0da9e..52225b4 100644 --- a/Mapper.cpp +++ b/Mapper.cpp @@ -313,7 +313,10 @@ void TextureGraphicsItem::_doPaint(QPainter *painter, _preDraw(painter); // Perform the actual mapping (done by subclasses). - _doDraw(painter, selected); + if (isOutput()) + _doDrawOutput(painter, selected); + else + _doDrawInput(painter, selected); // End drawing. _postDraw(painter); @@ -322,6 +325,33 @@ void TextureGraphicsItem::_doPaint(QPainter *painter, _doDrawControls(painter); } +void TextureGraphicsItem::_doDrawInput(QPainter* painter, bool selected) +{ + Q_UNUSED(painter); + Q_UNUSED(selected); + if (isMappingCurrent()) + { + // FIXME: Does this draw the quad counterclockwise? + glBegin (GL_QUADS); + { + QRectF rect = mapFromScene(_texture->getRect()).boundingRect(); + + Util::correctGlTexCoord(0, 0); + glVertex3f (rect.x(), rect.y(), 0); + + Util::correctGlTexCoord(1, 0); + glVertex3f (rect.x() + rect.width(), rect.y(), 0); + + Util::correctGlTexCoord(1, 1); + glVertex3f (rect.x()+rect.width(), rect.y()+rect.height(), 0); + + Util::correctGlTexCoord(0, 1); + glVertex3f (rect.x(), rect.y()+rect.height(), 0); + } + glEnd (); + } +} + void TextureGraphicsItem::_preDraw(QPainter* painter) { painter->beginNativePainting(); @@ -374,11 +404,11 @@ QRectF PolygonTextureGraphicsItem::boundingRect() const { return shape().boundingRect(); } -void TriangleTextureGraphicsItem::_doDraw(QPainter* painter, bool selected) +void TriangleTextureGraphicsItem::_doDrawOutput(QPainter* painter, bool selected) { Q_UNUSED(painter); Q_UNUSED(selected); - if (_output) + if (isOutput()) { glBegin(GL_TRIANGLES); { @@ -389,7 +419,7 @@ void TriangleTextureGraphicsItem::_doDraw(QPainter* painter, bool selected) } glEnd(); } - else +} { if (isMappingCurrent()) { diff --git a/Mapper.h b/Mapper.h index 28f4194..0904fb6 100644 --- a/Mapper.h +++ b/Mapper.h @@ -163,7 +163,8 @@ protected: virtual void _doPaint(QPainter *painter, const QStyleOptionGraphicsItem *option); - virtual void _doDraw(QPainter* painter, bool selected) = 0; + virtual void _doDrawOutput(QPainter* painter, bool selected) = 0; + virtual void _doDrawInput(QPainter* painter, bool selected); virtual void _doDrawControls(QPainter* painter) { Q_UNUSED(painter); } void _preDraw(QPainter* painter); void _postDraw(QPainter* painter); @@ -181,6 +182,12 @@ public: PolygonTextureGraphicsItem(Mapping::ptr mapping, bool output=true) : TextureGraphicsItem(mapping, output) {} virtual ~PolygonTextureGraphicsItem(){} + virtual void _doDrawControls(QPainter* painter) { + painter->setPen(MM::SHAPE_STROKE); + Polygon* poly = static_cast(_shape.get()); + Q_ASSERT(poly); + painter->drawPolygon(mapFromScene(poly->toPolygon())); + } virtual QPainterPath shape() const; virtual QRectF boundingRect() const; }; @@ -192,13 +199,9 @@ public: TriangleTextureGraphicsItem(Mapping::ptr mapping, bool output=true) : PolygonTextureGraphicsItem(mapping, output) {} virtual ~TriangleTextureGraphicsItem(){} - virtual void _doDraw(QPainter* painter, bool selected); - virtual void _doDrawControls(QPainter* painter) { - painter->setPen(MM::SHAPE_STROKE); - Polygon* poly = static_cast(_shape.get()); - Q_ASSERT(poly); - painter->drawPolygon(mapFromScene(poly->toPolygon())); - } + virtual void _doDrawOutput(QPainter* painter, bool selected); + +}; }; /** From f23ce800fc1c27b3c4e2ee25a234427ea22228ab Mon Sep 17 00:00:00 2001 From: Tats Date: Mon, 6 Jul 2015 16:58:29 -0600 Subject: [PATCH 29/32] Support for mesh textures. --- Mapper.cpp | 70 +++++++++++++++++++++++++++++++++++++++++------------- Mapper.h | 18 ++++++++++---- 2 files changed, 67 insertions(+), 21 deletions(-) diff --git a/Mapper.cpp b/Mapper.cpp index 52225b4..e9cbbae 100644 --- a/Mapper.cpp +++ b/Mapper.cpp @@ -171,6 +171,10 @@ void ShapeGraphicsItem::_syncShape() QGraphicsItem* child = children.at(i); _shape->setVertex(i, child->scenePos()); } + + // The shape object is the model: it contains the logic to make sure the vertices are ok. + // So here we need to re-sync the vertices (view side) according to the model. + _syncVertices(); } void ShapeGraphicsItem::_syncVertices() @@ -420,29 +424,53 @@ void TriangleTextureGraphicsItem::_doDrawOutput(QPainter* painter, bool selected glEnd(); } } + +void MeshTextureGraphicsItem::_doDrawOutput(QPainter* painter, bool selected) +{ + Q_UNUSED(painter); + Q_UNUSED(selected); + if (isOutput()) { - if (isMappingCurrent()) + std::tr1::shared_ptr outputMesh = std::tr1::static_pointer_cast(_shape); + std::tr1::shared_ptr inputMesh = std::tr1::static_pointer_cast(_inputShape); + QVector > outputQuads = outputMesh->getQuads2d(); + QVector > inputQuads = inputMesh->getQuads2d(); + for (int x = 0; x < outputMesh->nHorizontalQuads(); x++) { - // FIXME: Does this draw the quad counterclockwise? - glBegin (GL_QUADS); + for (int y = 0; y < outputMesh->nVerticalQuads(); y++) { - QRectF rect = mapFromScene(_texture->getRect()).boundingRect(); - - Util::correctGlTexCoord(0, 0); - glVertex3f (rect.x(), rect.y(), 0); - - Util::correctGlTexCoord(1, 0); - glVertex3f (rect.x() + rect.width(), rect.y(), 0); - - Util::correctGlTexCoord(1, 1); - glVertex3f (rect.x()+rect.width(), rect.y()+rect.height(), 0); - - Util::correctGlTexCoord(0, 1); - glVertex3f (rect.x(), rect.y()+rect.height(), 0); + Quad& outputQuad = outputQuads[x][y]; + Quad& inputQuad = inputQuads[x][y]; + glBegin(GL_QUADS); + for (int i = 0; i < outputQuad.nVertices(); i++) + { + Util::setGlTexPoint(*_texture, inputQuad.getVertex(i), mapFromScene(outputQuad.getVertex(i))); + } + glEnd(); } - glEnd (); } } + +} + +void MeshTextureGraphicsItem::_doDrawControls(QPainter* painter) +{ + Mesh* mesh = static_cast(_shape.get()); + Q_ASSERT(mesh); + + // Init colors and stroke. + painter->setPen(MM::SHAPE_INNER_STROKE); + + // Draw inner quads. + QVector quads = mesh->getQuads(); + for (QVector::const_iterator it = quads.begin(); it != quads.end(); ++it) + { + painter->drawPolygon(mapFromScene(it->toPolygon())); + } + + // Draw outer quad. + painter->setPen(MM::SHAPE_STROKE); + painter->drawPolygon(mapFromScene(mesh->toPolygon())); } Mapper::Mapper(Mapping::ptr mapping) @@ -753,6 +781,9 @@ TriangleTextureMapper::TriangleTextureMapper(std::tr1::shared_ptr mapping) : PolygonTextureMapper(mapping) { + _graphicsItem = new MeshTextureGraphicsItem(_mapping, true); + _inputGraphicsItem = new MeshTextureGraphicsItem(_mapping, false); + // Add mesh sub property. Mesh* mesh = (Mesh*)textureMapping->getShape().get(); _meshItem = _variantManager->addProperty(QVariant::Size, QObject::tr("Dimensions")); @@ -776,6 +807,11 @@ void MeshTextureMapper::setValue(QtProperty* property, const QVariant& value) outputMesh->resize(size.width(), size.height()); inputMesh->resize(size.width(), size.height()); + _graphicsItem->resetVertices(); + _inputGraphicsItem->resetVertices(); + + // TODO: here we need to create the graphicsitems + emit valueChanged(); } } diff --git a/Mapper.h b/Mapper.h index 0904fb6..f0608c7 100644 --- a/Mapper.h +++ b/Mapper.h @@ -202,6 +202,16 @@ public: virtual void _doDrawOutput(QPainter* painter, bool selected); }; + +/// Graphics item for textured mesh. +class MeshTextureGraphicsItem : public PolygonTextureGraphicsItem +{ +public: + MeshTextureGraphicsItem(Mapping::ptr mapping, bool output=true) : PolygonTextureGraphicsItem(mapping, output) {} + virtual ~MeshTextureGraphicsItem(){} + + virtual void _doDrawOutput(QPainter* painter, bool selected); + virtual void _doDrawControls(QPainter* painter); }; /** @@ -230,10 +240,10 @@ public: /// Returns a pointer to the properties editor for that mapper. virtual QWidget* getPropertiesEditor(); - virtual QGraphicsItem* getGraphicsItem() { + virtual ShapeGraphicsItem* getGraphicsItem() { return _graphicsItem; } - virtual QGraphicsItem* getInputGraphicsItem() { + virtual ShapeGraphicsItem* getInputGraphicsItem() { return _inputGraphicsItem; } @@ -255,8 +265,8 @@ protected: std::map > _propertyToVertex; - QGraphicsItem* _graphicsItem; - QGraphicsItem* _inputGraphicsItem; + ShapeGraphicsItem* _graphicsItem; + ShapeGraphicsItem* _inputGraphicsItem; // FIXME: use typedefs, member of the class for type names that are too long to type: MShape::ptr outputShape; From 28d353d99a8c407f11fd519892717038ad18e1c4 Mon Sep 17 00:00:00 2001 From: Tats Date: Mon, 6 Jul 2015 16:59:29 -0600 Subject: [PATCH 30/32] Bug fix: meshes did not handle well vertex displacement (adjusted tolerance when constraining a vertex to a polygon). Signed-off-by: Tats --- Shape.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Shape.cpp b/Shape.cpp index 17c67c0..920d716 100644 --- a/Shape.cpp +++ b/Shape.cpp @@ -63,8 +63,8 @@ void Polygon::_constrainVertex(const QPolygonF& polygon, int i, QPointF& v) QLineF& seg = *it; QPointF p1 = seg.p1(); QPointF p2 = seg.p2(); - seg.setP1( p1 + (p1 - p2) * 0.2f); - seg.setP2( p2 + (p2 - p1) * 0.2f); + seg.setP1( p1 + (p1 - p2) * 0.35f); + seg.setP2( p2 + (p2 - p1) * 0.35f); } // For each adjunct segment. From d1b1d00e1090c769826e380cd2c52d4b220bcfdf Mon Sep 17 00:00:00 2001 From: Tats Date: Mon, 6 Jul 2015 17:53:37 -0600 Subject: [PATCH 31/32] Integrated ellipse texture mapping. --- Mapper.cpp | 238 ++++++++++++++++++++++++++--------------------------- Mapper.h | 36 ++++---- 2 files changed, 138 insertions(+), 136 deletions(-) diff --git a/Mapper.cpp b/Mapper.cpp index e9cbbae..6609a89 100644 --- a/Mapper.cpp +++ b/Mapper.cpp @@ -356,6 +356,12 @@ void TextureGraphicsItem::_doDrawInput(QPainter* painter, bool selected) } } +void TextureGraphicsItem::_doDrawControls(QPainter* painter) +{ + painter->setPen(MM::SHAPE_STROKE); + painter->drawPath(shape()); +} + void TextureGraphicsItem::_preDraw(QPainter* painter) { painter->beginNativePainting(); @@ -425,6 +431,14 @@ void TriangleTextureGraphicsItem::_doDrawOutput(QPainter* painter, bool selected } } +void PolygonTextureGraphicsItem::_doDrawControls(QPainter* painter) +{ + painter->setPen(MM::SHAPE_STROKE); + Polygon* poly = static_cast(_shape.get()); + Q_ASSERT(poly); + painter->drawPolygon(mapFromScene(poly->toPolygon())); +} + void MeshTextureGraphicsItem::_doDrawOutput(QPainter* painter, bool selected) { Q_UNUSED(painter); @@ -473,6 +487,109 @@ void MeshTextureGraphicsItem::_doDrawControls(QPainter* painter) painter->drawPolygon(mapFromScene(mesh->toPolygon())); } +QPainterPath EllipseTextureGraphicsItem::shape() const +{ + // Create path for ellipse. + QPainterPath path; + Ellipse* ellipse = static_cast(_shape.get()); + Q_ASSERT(ellipse); + QTransform transform; + transform.translate(ellipse->getCenter().x(), ellipse->getCenter().y()); + transform.rotate(ellipse->getRotation()); + path.addEllipse(QPoint(0,0), ellipse->getHorizontalRadius(), ellipse->getVerticalRadius()); + return mapFromScene(transform.map(path)); +} + +QRectF EllipseTextureGraphicsItem::boundingRect() const +{ + return shape().boundingRect(); +} + +void EllipseTextureGraphicsItem::_doDrawOutput(QPainter* painter, bool selected) +{ + Q_UNUSED(painter); + // Get input and output ellipses. + std::tr1::shared_ptr inputEllipse = std::tr1::static_pointer_cast(_inputShape); + std::tr1::shared_ptr outputEllipse = std::tr1::static_pointer_cast(_shape); + + // Start / end angle. + //const float startAngle = 0; + //const float endAngle = 2*M_PI; + + // + //float angle; + QPointF currentInputPoint; + QPointF prevInputPoint(0, 0); + QPointF currentOutputPoint; + QPointF prevOutputPoint(0, 0); + + // Input ellipse parameters. + const QPointF& inputCenter = inputEllipse->getCenter(); + const QPointF& inputControlCenter = inputEllipse->getVertex(4); + float inputHorizRadius = inputEllipse->getHorizontalRadius(); + float inputVertRadius = inputEllipse->getVerticalRadius(); + float inputRotation = inputEllipse->getRotationRadians(); + + // Output ellipse parameters. + const QPointF& outputCenter = mapFromScene(outputEllipse->getCenter()); + const QPointF& outputControlCenter = mapFromScene(outputEllipse->getVertex(4)); + float outputHorizRadius = outputEllipse->getHorizontalRadius(); + float outputVertRadius = outputEllipse->getVerticalRadius(); + float outputRotation = outputEllipse->getRotationRadians(); + + // Variation in angle at each step of the loop. + const int N_TRIANGLES = 100; + const float ANGLE_STEP = 2*M_PI/N_TRIANGLES; + + float circleAngle = 0; + for (int i=0; i<=N_TRIANGLES; i++, circleAngle += ANGLE_STEP) + { + // Set next (current) points. + _setPointOfEllipseAtAngle(currentInputPoint, inputCenter, inputHorizRadius, inputVertRadius, inputRotation, circleAngle); + _setPointOfEllipseAtAngle(currentOutputPoint, outputCenter, outputHorizRadius, outputVertRadius, outputRotation, circleAngle); + + // We don't draw the first point. + if (i > 0) + { + // Draw triangle. + glBegin(GL_TRIANGLES); + Util::setGlTexPoint(*_texture, inputControlCenter, outputControlCenter); + Util::setGlTexPoint(*_texture, prevInputPoint, prevOutputPoint); + Util::setGlTexPoint(*_texture, currentInputPoint, currentOutputPoint); + glEnd(); + } + + // Save point for next iteration. + prevInputPoint.setX(currentInputPoint.x()); + prevInputPoint.setY(currentInputPoint.y()); + prevOutputPoint.setX(currentOutputPoint.x()); + prevOutputPoint.setY(currentOutputPoint.y()); + } +} + +void EllipseTextureGraphicsItem::_setPointOfEllipseAtAngle(QPointF& point, const QPointF& center, float hRadius, float vRadius, float rotation, float circularAngle) +{ + float xCirc = sin(circularAngle) * hRadius; + float yCirc = cos(circularAngle) * vRadius; + float distance = sqrt( xCirc*xCirc + yCirc*yCirc ); + float angle = atan2( xCirc, yCirc ); + rotation = 2*M_PI-rotation; // rotation needs to be inverted (CW <-> CCW) + point.setX( sin(angle + rotation) * distance + center.x() ); + point.setY( cos(angle + rotation) * distance + center.y() ); +} + +void EllipseTextureGraphicsItem::_doDrawControls(QPainter* painter) +{ + painter->setPen(MM::SHAPE_STROKE); + painter->setBrush(Qt::NoBrush); + + // Just draw the path. + painter->drawPath(shape()); + +// std::tr1::shared_ptr outputEllipse = std::tr1::static_pointer_cast(_shape); +// Util::drawControlsEllipse(painter, selectedVertices, *outputEllipse); +} + Mapper::Mapper(Mapping::ptr mapping) : _mapping(mapping), _graphicsItem(NULL), @@ -819,127 +936,10 @@ void MeshTextureMapper::setValue(QtProperty* property, const QVariant& value) TextureMapper::setValue(property, value); } -void MeshTextureMapper::drawControls(QPainter* painter, const QList* selectedVertices) -{ - std::tr1::shared_ptr outputMesh = std::tr1::static_pointer_cast(outputShape); - Util::drawControlsMesh(painter, selectedVertices, *outputMesh); -} - -void MeshTextureMapper::drawInputControls(QPainter* painter, const QList* selectedVertices) -{ - std::tr1::shared_ptr inputMesh = std::tr1::static_pointer_cast(inputShape); - Util::drawControlsMesh(painter, selectedVertices, *inputMesh); -} - -void MeshTextureMapper::_doDraw(QPainter* painter) -{ - Q_UNUSED(painter); - std::tr1::shared_ptr outputMesh = std::tr1::static_pointer_cast(outputShape); - std::tr1::shared_ptr inputMesh = std::tr1::static_pointer_cast(inputShape); - QVector > outputQuads = outputMesh->getQuads2d(); - QVector > inputQuads = inputMesh->getQuads2d(); - for (int x = 0; x < outputMesh->nHorizontalQuads(); x++) - { - for (int y = 0; y < outputMesh->nVerticalQuads(); y++) - { - Quad& outputQuad = outputQuads[x][y]; - Quad& inputQuad = inputQuads[x][y]; - glBegin(GL_QUADS); - for (int i = 0; i < 4; i++) - { - Util::setGlTexPoint(*texture, inputQuad.getVertex(i), outputQuad.getVertex(i)); - } - glEnd(); - } - } -} - EllipseTextureMapper::EllipseTextureMapper(std::tr1::shared_ptr mapping) : PolygonTextureMapper(mapping) { + _graphicsItem = new EllipseTextureGraphicsItem(_mapping, true); + _inputGraphicsItem = new EllipseTextureGraphicsItem(_mapping, false); } -void EllipseTextureMapper::drawControls(QPainter* painter, const QList* selectedVertices) -{ - std::tr1::shared_ptr outputEllipse = std::tr1::static_pointer_cast(outputShape); - Util::drawControlsEllipse(painter, selectedVertices, *outputEllipse); -} - -void EllipseTextureMapper::drawInputControls(QPainter* painter, const QList* selectedVertices) -{ - std::tr1::shared_ptr inputEllipse = std::tr1::static_pointer_cast(inputShape); - Util::drawControlsEllipse(painter, selectedVertices, *inputEllipse); -} - -void EllipseTextureMapper::_doDraw(QPainter* painter) -{ - Q_UNUSED(painter); - // Get input and output ellipses. - std::tr1::shared_ptr inputEllipse = std::tr1::static_pointer_cast(inputShape); - std::tr1::shared_ptr outputEllipse = std::tr1::static_pointer_cast(outputShape); - - // Start / end angle. - //const float startAngle = 0; - //const float endAngle = 2*M_PI; - - // - //float angle; - QPointF currentInputPoint; - QPointF prevInputPoint(0, 0); - QPointF currentOutputPoint; - QPointF prevOutputPoint(0, 0); - - // Input ellipse parameters. - const QPointF& inputCenter = inputEllipse->getCenter(); - const QPointF& inputControlCenter = inputEllipse->getVertex(4); - float inputHorizRadius = inputEllipse->getHorizontalRadius(); - float inputVertRadius = inputEllipse->getVerticalRadius(); - float inputRotation = inputEllipse->getRotationRadians(); - - // Output ellipse parameters. - const QPointF& outputCenter = outputEllipse->getCenter(); - const QPointF& outputControlCenter = outputEllipse->getVertex(4); - float outputHorizRadius = outputEllipse->getHorizontalRadius(); - float outputVertRadius = outputEllipse->getVerticalRadius(); - float outputRotation = outputEllipse->getRotationRadians(); - - // Variation in angle at each step of the loop. - const int N_TRIANGLES = 100; - const float ANGLE_STEP = 2*M_PI/N_TRIANGLES; - - float circleAngle = 0; - for (int i=0; i<=N_TRIANGLES; i++, circleAngle += ANGLE_STEP) - { - // Set next (current) points. - _setPointOfEllipseAtAngle(currentInputPoint, inputCenter, inputHorizRadius, inputVertRadius, inputRotation, circleAngle); - _setPointOfEllipseAtAngle(currentOutputPoint, outputCenter, outputHorizRadius, outputVertRadius, outputRotation, circleAngle); - - // We don't draw the first point. - if (i > 0) - { - // Draw triangle. - glBegin(GL_TRIANGLES); - Util::setGlTexPoint(*texture, inputControlCenter, outputControlCenter); - Util::setGlTexPoint(*texture, prevInputPoint, prevOutputPoint); - Util::setGlTexPoint(*texture, currentInputPoint, currentOutputPoint); - glEnd(); - } - - // Save point for next iteration. - prevInputPoint.setX(currentInputPoint.x()); - prevInputPoint.setY(currentInputPoint.y()); - prevOutputPoint.setX(currentOutputPoint.x()); - prevOutputPoint.setY(currentOutputPoint.y()); - } -} - -void EllipseTextureMapper::_setPointOfEllipseAtAngle(QPointF& point, const QPointF& center, float hRadius, float vRadius, float rotation, float circularAngle) -{ - float xCirc = sin(circularAngle) * hRadius; - float yCirc = cos(circularAngle) * vRadius; - float distance = sqrt( xCirc*xCirc + yCirc*yCirc ); - float angle = atan2( xCirc, yCirc ); - rotation = 2*M_PI-rotation; // rotation needs to be inverted (CW <-> CCW) - point.setX( sin(angle + rotation) * distance + center.x() ); - point.setY( cos(angle + rotation) * distance + center.y() ); -} diff --git a/Mapper.h b/Mapper.h index f0608c7..d11f43a 100644 --- a/Mapper.h +++ b/Mapper.h @@ -165,7 +165,7 @@ protected: virtual void _doDrawOutput(QPainter* painter, bool selected) = 0; virtual void _doDrawInput(QPainter* painter, bool selected); - virtual void _doDrawControls(QPainter* painter) { Q_UNUSED(painter); } + virtual void _doDrawControls(QPainter* painter); void _preDraw(QPainter* painter); void _postDraw(QPainter* painter); @@ -182,12 +182,8 @@ public: PolygonTextureGraphicsItem(Mapping::ptr mapping, bool output=true) : TextureGraphicsItem(mapping, output) {} virtual ~PolygonTextureGraphicsItem(){} - virtual void _doDrawControls(QPainter* painter) { - painter->setPen(MM::SHAPE_STROKE); - Polygon* poly = static_cast(_shape.get()); - Q_ASSERT(poly); - painter->drawPolygon(mapFromScene(poly->toPolygon())); - } + virtual void _doDrawControls(QPainter* painter); + virtual QPainterPath shape() const; virtual QRectF boundingRect() const; }; @@ -214,6 +210,22 @@ public: virtual void _doDrawControls(QPainter* painter); }; +/// Graphics item for textured mesh. +class EllipseTextureGraphicsItem : public TextureGraphicsItem +{ +public: + EllipseTextureGraphicsItem(Mapping::ptr mapping, bool output=true) : TextureGraphicsItem(mapping, output) {} + virtual ~EllipseTextureGraphicsItem(){} + + virtual QPainterPath shape() const; + virtual QRectF boundingRect() const; + + virtual void _doDrawOutput(QPainter* painter, bool selected); + virtual void _doDrawControls(QPainter* painter); + + static void _setPointOfEllipseAtAngle(QPointF& point, const QPointF& center, float hRadius, float vRadius, float rotation, float circularAngle); +}; + /** * A way to draw on some kind of shape. * @@ -400,15 +412,9 @@ public: MeshTextureMapper(std::tr1::shared_ptr mapping); virtual ~MeshTextureMapper() {} - virtual void drawControls(QPainter* painter, const QList* selectedVertices=0); - virtual void drawInputControls(QPainter* painter, const QList* selectedVertices=0); - public slots: virtual void setValue(QtProperty* property, const QVariant& value); -protected: - virtual void _doDraw(QPainter* painter); - private: QtVariantProperty* _meshItem; }; @@ -420,11 +426,7 @@ public: EllipseTextureMapper(std::tr1::shared_ptr mapping); virtual ~EllipseTextureMapper() {} - virtual void drawControls(QPainter* painter, const QList* selectedVertices=0); - virtual void drawInputControls(QPainter* painter, const QList* selectedVertices=0); - protected: - virtual void _doDraw(QPainter* painter); static void _setPointOfEllipseAtAngle(QPointF& point, const QPointF& center, float hRadius, float vRadius, float rotation, float circularAngle); }; From b6b3863eb2f46a8ccd574e1d24e83da1182c54e1 Mon Sep 17 00:00:00 2001 From: Tats Date: Mon, 6 Jul 2015 17:57:08 -0600 Subject: [PATCH 32/32] Minor documentation added. --- Mapper.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Mapper.h b/Mapper.h index d11f43a..a45783a 100644 --- a/Mapper.h +++ b/Mapper.h @@ -88,8 +88,13 @@ public: void resetVertices(); protected: + // Generates the VertexGraphicsItems that are defining the vertices of that shape. virtual void _createVertices(); + + // Sync MShape from current VertexGraphicsItems. virtual void _syncShape(); + + // Sync VertexGraphicsItems from MShape. virtual void _syncVertices(); virtual void _doPaint(QPainter *painter, const QStyleOptionGraphicsItem *option) = 0;