From acca6059f67a221eada95a7e6e5adfc78c71b00f Mon Sep 17 00:00:00 2001 From: Tats Date: Mon, 13 Jul 2015 13:25:18 -0600 Subject: [PATCH] Implemented invariant control points and lines wrt zoom level. --- MainWindow.h | 3 ++ Mapper.cpp | 83 ++++++++++++++++++++++++++++------------------ Mapper.h | 15 ++++++--- MapperGLCanvas.cpp | 6 ++++ MapperGLCanvas.h | 4 +++ 5 files changed, 74 insertions(+), 37 deletions(-) diff --git a/MainWindow.h b/MainWindow.h index 284dbbf..1555188 100644 --- a/MainWindow.h +++ b/MainWindow.h @@ -400,6 +400,9 @@ public: void removeCurrentPaint(); void removeCurrentMapping(); + MapperGLCanvas* getSourceCanvas() const { return sourceCanvas; } + MapperGLCanvas* getDestinationCanvas() const { return destinationCanvas; } + /// Returns true iff we should display the controls. bool displayControls() const { return _displayControls; } diff --git a/Mapper.cpp b/Mapper.cpp index fa8dbe7..80d7957 100644 --- a/Mapper.cpp +++ b/Mapper.cpp @@ -35,6 +35,12 @@ ShapeGraphicsItem::ShapeGraphicsItem(Mapping::ptr mapping, bool output) _createVertices(); } +MapperGLCanvas* ShapeGraphicsItem::getCanvas() const +{ + MainWindow* win = MainWindow::instance(); + return isOutput() ? win->getDestinationCanvas() : win->getSourceCanvas(); +} + bool ShapeGraphicsItem::isMappingCurrent() const { return MainWindow::instance()->getCurrentMappingId() == _mapping->getId(); } bool ShapeGraphicsItem::sceneEventFilter(QGraphicsItem * watched, QEvent * event) @@ -124,9 +130,23 @@ void ShapeGraphicsItem::paint(QPainter *painter, _prePaint(painter, option); _doPaint(painter, option); _postPaint(painter, option); + + if (MainWindow::instance()->displayControls() && isMappingCurrent()) + { + _doPaintControls(painter, option); + } } } +void ShapeGraphicsItem::_doPaintControls(QPainter *painter, const QStyleOptionGraphicsItem *option) +{ + Q_UNUSED(option); + painter->setPen(_getRescaledShapeStroke()); + painter->setBrush(Qt::NoBrush); + painter->drawPath(shape()); +} + + //QVariant ShapeGraphicsItem::itemChange(GraphicsItemChange change, const QVariant &value) //{ // if (change == ItemPositionChange) @@ -214,6 +234,11 @@ void ShapeGraphicsItem::_glueVertex(QPointF* p) } } +QPen ShapeGraphicsItem::_getRescaledShapeStroke(bool innerStroke) +{ + return QPen(QBrush(MM::CONTROL_COLOR), (innerStroke ? MM::SHAPE_INNER_STROKE_WIDTH : MM::SHAPE_STROKE_WIDTH) / getCanvas()->getZoomFactor()); +} + void VertexGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent * event) { ShapeGraphicsItem* shapeParent = static_cast(parentItem()); @@ -250,9 +275,15 @@ void VertexGraphicsItem::paint(QPainter *painter, if (MainWindow::instance()->displayControls()) { ShapeGraphicsItem* shapeParent = static_cast(parentItem()); + if (shapeParent->isOutput()) if (shapeParent->isMappingVisible() && shapeParent->isMappingCurrent()) - Util::drawControlsVertex(painter, QPointF(0,0), (option->state & QStyle::State_Selected)); + { + qreal zoomFactor = 1.0 / shapeParent->getCanvas()->getZoomFactor(); + resetMatrix(); + scale(zoomFactor, zoomFactor); + Util::drawControlsVertex(painter, QPointF(0,0), (option->state & QStyle::State_Selected), MM::VERTEX_SELECT_RADIUS); + } } } @@ -262,12 +293,7 @@ void ColorGraphicsItem::_prePaint(QPainter *painter, Color* color = static_cast(_mapping->getPaint().get()); Q_ASSERT(color); - // Setup pen and brush. - if (MainWindow::instance()->displayControls() && - (option->state & QStyle::State_Selected)) - painter->setPen(MM::SHAPE_STROKE); // if display controls then draw appropriate stroke around - else - painter->setPen(Qt::NoPen); + painter->setPen(Qt::NoPen); // Set brush. QColor col = color->getColor(); @@ -293,6 +319,15 @@ void PolygonColorGraphicsItem::_doPaint(QPainter *painter, painter->drawPolygon(mapFromScene(poly->toPolygon())); } +void PolygonColorGraphicsItem::_doPaintControls(QPainter* painter, const QStyleOptionGraphicsItem *option) +{ + Q_UNUSED(option); + painter->setPen(_getRescaledShapeStroke()); + Polygon* poly = static_cast(_shape.get()); + Q_ASSERT(poly); + painter->drawPolygon(mapFromScene(poly->toPolygon())); +} + QPainterPath EllipseColorGraphicsItem::shape() const { // Create path for ellipse. @@ -364,12 +399,6 @@ void TextureGraphicsItem::_doDrawInput(QPainter* painter) } } -void TextureGraphicsItem::_doDrawControls(QPainter* painter) -{ - painter->setPen(MM::SHAPE_STROKE); - painter->drawPath(shape()); -} - void TextureGraphicsItem::_prePaint(QPainter* painter, const QStyleOptionGraphicsItem *option) { @@ -418,9 +447,6 @@ void TextureGraphicsItem::_postPaint(QPainter* painter, glDisable(GL_TEXTURE_2D); painter->endNativePainting(); - - if (MainWindow::instance()->displayControls() && isMappingCurrent()) - _doDrawControls(painter); } QPainterPath PolygonTextureGraphicsItem::shape() const @@ -452,9 +478,10 @@ void TriangleTextureGraphicsItem::_doDrawOutput(QPainter* painter) } } -void PolygonTextureGraphicsItem::_doDrawControls(QPainter* painter) +void PolygonTextureGraphicsItem::_doPaintControls(QPainter* painter, const QStyleOptionGraphicsItem *option) { - painter->setPen(MM::SHAPE_STROKE); + Q_UNUSED(option); + painter->setPen(_getRescaledShapeStroke()); Polygon* poly = static_cast(_shape.get()); Q_ASSERT(poly); painter->drawPolygon(mapFromScene(poly->toPolygon())); @@ -482,13 +509,15 @@ void MeshTextureGraphicsItem::_doDrawOutput(QPainter* painter) } -void MeshTextureGraphicsItem::_doDrawControls(QPainter* painter) +void MeshTextureGraphicsItem::_doPaintControls(QPainter* painter, const QStyleOptionGraphicsItem *option) { + Q_UNUSED(option); + Mesh* mesh = static_cast(_shape.get()); Q_ASSERT(mesh); // Init colors and stroke. - painter->setPen(MM::SHAPE_INNER_STROKE); + painter->setPen(_getRescaledShapeStroke(true)); // Draw inner quads. QVector quads = mesh->getQuads(); @@ -498,7 +527,7 @@ void MeshTextureGraphicsItem::_doDrawControls(QPainter* painter) } // Draw outer quad. - painter->setPen(MM::SHAPE_STROKE); + painter->setPen(_getRescaledShapeStroke()); painter->drawPolygon(mapFromScene(mesh->toPolygon())); } @@ -667,18 +696,6 @@ void EllipseTextureGraphicsItem::_setPointOfEllipseAtAngle(QPointF& point, const 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), diff --git a/Mapper.h b/Mapper.h index a835050..afcfb55 100644 --- a/Mapper.h +++ b/Mapper.h @@ -38,6 +38,7 @@ #include "Shape.h" #include "Paint.h" #include "Mapping.h" +#include "MapperGLCanvas.h" #include "Util.h" @@ -66,6 +67,7 @@ public: Mapping::ptr getMapping() const { return _mapping; } bool isOutput() const { return _output; } + MapperGLCanvas* getCanvas() const; bool isMappingCurrent() const; bool isMappingVisible() const { return _mapping->isVisible(); } @@ -103,9 +105,15 @@ protected: virtual void _postPaint(QPainter *painter, const QStyleOptionGraphicsItem *option) { Q_UNUSED(painter); Q_UNUSED(option); } + virtual void _doPaintControls(QPainter *painter, const QStyleOptionGraphicsItem *option); + // TODO: Perhaps the sticky-sensitivity should be configurable through GUI void _glueVertex(QPointF* p); + // Utility function: returns a stroke with rescaled width such that the stroke appears + // invariant to the zoom level (to be used in _doPaintControls() method). + QPen _getRescaledShapeStroke(bool innerStroke=false); + Mapping::ptr _mapping; MShape::ptr _shape; bool _output; @@ -156,6 +164,7 @@ public: protected: virtual void _doPaint(QPainter *painter, const QStyleOptionGraphicsItem *option); + void _doPaintControls(QPainter* painter, const QStyleOptionGraphicsItem *option); }; /// Graphics item for colored polygons (eg. quad, triangle). @@ -187,7 +196,6 @@ protected: virtual void _doDrawOutput(QPainter* painter) = 0; virtual void _doDrawInput(QPainter* painter); - virtual void _doDrawControls(QPainter* painter); protected: std::tr1::shared_ptr _textureMapping; @@ -202,7 +210,7 @@ public: PolygonTextureGraphicsItem(Mapping::ptr mapping, bool output=true) : TextureGraphicsItem(mapping, output) {} virtual ~PolygonTextureGraphicsItem(){} - virtual void _doDrawControls(QPainter* painter); + virtual void _doPaintControls(QPainter* painter, const QStyleOptionGraphicsItem *option); virtual QPainterPath shape() const; virtual QRectF boundingRect() const; @@ -226,8 +234,8 @@ public: MeshTextureGraphicsItem(Mapping::ptr mapping, bool output=true) : PolygonTextureGraphicsItem(mapping, output) {} virtual ~MeshTextureGraphicsItem(){} + virtual void _doPaintControls(QPainter* painter, const QStyleOptionGraphicsItem *option); virtual void _doDrawOutput(QPainter* painter); - virtual void _doDrawControls(QPainter* painter); private: // Draws quad recursively using the technique described in Oliveira, M. "Correcting Texture Mapping Errors Introduced by Graphics Hardware" @@ -246,7 +254,6 @@ public: virtual QRectF boundingRect() const; virtual void _doDrawOutput(QPainter* painter); - virtual void _doDrawControls(QPainter* painter); static void _setPointOfEllipseAtAngle(QPointF& point, const QPointF& center, float hRadius, float vRadius, float rotation, float circularAngle); }; diff --git a/MapperGLCanvas.cpp b/MapperGLCanvas.cpp index d929409..5c4ff58 100644 --- a/MapperGLCanvas.cpp +++ b/MapperGLCanvas.cpp @@ -378,9 +378,15 @@ void MapperGLCanvas::wheelEvent(QWheelEvent *event) zoomFactor = qMax(zoomFactor, MM::ZOOM_MIN); } + // Re-bound zoom (for consistency). + zoomFactor = getZoomFactor(); + + // Apply zoom to view. QGraphicsView* view = scene()->views().first(); view->resetMatrix(); view->scale(zoomFactor, zoomFactor); view->update(); + + // Accept wheel scrolling event. event->accept(); } diff --git a/MapperGLCanvas.h b/MapperGLCanvas.h index 8cc7c97..d1571ee 100644 --- a/MapperGLCanvas.h +++ b/MapperGLCanvas.h @@ -27,6 +27,8 @@ #include #include +#include + #include #include "MM.h" @@ -72,6 +74,8 @@ public: /// Set the currently active vertex void setActiveVertexIndex(int activeVertex) { _activeVertex = activeVertex; } + qreal getZoomFactor() const { return qBound(qPow(MM::ZOOM_FACTOR, _zoomLevel), MM::ZOOM_MIN, MM::ZOOM_MAX); } + protected: // void initializeGL(); // void resizeGL(int width, int height);