From 07e1b26436fc4750b519d322d3544f5ea30a44e0 Mon Sep 17 00:00:00 2001 From: Tats Date: Thu, 23 Oct 2014 13:40:01 +0000 Subject: [PATCH] Allows individual selection of a vertex to be shown on screen. --- DestinationGLCanvas.cpp | 9 +++++- MM.cpp | 1 + MM.h | 1 + Mapper.cpp | 36 +++++++++++------------ Mapper.h | 30 +++++++++++--------- MapperGLCanvas.cpp | 63 +++++++++++++++++++++++++++-------------- MapperGLCanvas.h | 13 +++++++-- SourceGLCanvas.cpp | 10 +++++-- Util.cpp | 36 ++++++++++++----------- Util.h | 13 +++++---- 10 files changed, 133 insertions(+), 79 deletions(-) diff --git a/DestinationGLCanvas.cpp b/DestinationGLCanvas.cpp index 4fa00b4..1116d5f 100644 --- a/DestinationGLCanvas.cpp +++ b/DestinationGLCanvas.cpp @@ -54,7 +54,14 @@ void DestinationGLCanvas::doDraw(QPainter* painter) getCurrentShape() != NULL) { painter->save(); - getMainWindow()->getMapperByMappingId(getMainWindow()->getCurrentMappingId())->drawControls(painter); + 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(); } diff --git a/MM.cpp b/MM.cpp index 7ce372d..a93077c 100644 --- a/MM.cpp +++ b/MM.cpp @@ -34,6 +34,7 @@ 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 QPen MM::SHAPE_STROKE(QBrush(CONTROL_COLOR), SHAPE_STROKE_WIDTH); const QPen MM::SHAPE_INNER_STROKE(QBrush(CONTROL_COLOR), SHAPE_INNER_STROKE_WIDTH); diff --git a/MM.h b/MM.h index fade564..0970c5f 100644 --- a/MM.h +++ b/MM.h @@ -51,6 +51,7 @@ public: static const QColor CONTROL_COLOR; 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; diff --git a/Mapper.cpp b/Mapper.cpp index 63cd193..8713fd9 100644 --- a/Mapper.cpp +++ b/Mapper.cpp @@ -136,10 +136,10 @@ void PolygonColorMapper::draw(QPainter* painter) painter->drawPolygon(poly->toPolygon()); } -void PolygonColorMapper::drawControls(QPainter* painter) +void PolygonColorMapper::drawControls(QPainter* painter, const QList* selectedVertices) { Polygon* poly = (Polygon*)_mapping->getShape().get(); - Util::drawControlsPolygon(painter, *poly); + Util::drawControlsPolygon(painter, selectedVertices, *poly); } MeshColorMapper::MeshColorMapper(Mapping::ptr mapping) @@ -168,10 +168,10 @@ void MeshColorMapper::draw(QPainter* painter) } } -void MeshColorMapper::drawControls(QPainter* painter) +void MeshColorMapper::drawControls(QPainter* painter, const QList* selectedVertices) { std::tr1::shared_ptr outputMesh = std::tr1::static_pointer_cast(outputShape); - Util::drawControlsMesh(painter, *outputMesh); + Util::drawControlsMesh(painter, selectedVertices, *outputMesh); } void MeshColorMapper::setValue(QtProperty* property, const QVariant& value) @@ -215,10 +215,10 @@ void EllipseColorMapper::draw(QPainter* painter) painter->restore(); // restore saved painter state } -void EllipseColorMapper::drawControls(QPainter* painter) +void EllipseColorMapper::drawControls(QPainter* painter, const QList* selectedVertices) { std::tr1::shared_ptr outputEllipse = std::tr1::static_pointer_cast(outputShape); - Util::drawControlsEllipse(painter, *outputEllipse); + Util::drawControlsEllipse(painter, selectedVertices, *outputEllipse); } @@ -348,16 +348,16 @@ void TextureMapper::_postDraw(QPainter* painter) painter->endNativePainting(); } -void PolygonTextureMapper::drawControls(QPainter* painter) +void PolygonTextureMapper::drawControls(QPainter* painter, const QList* selectedVertices) { std::tr1::shared_ptr outputPoly = std::tr1::static_pointer_cast(outputShape); - Util::drawControlsPolygon(painter, *outputPoly); + Util::drawControlsPolygon(painter, selectedVertices, *outputPoly); } -void PolygonTextureMapper::drawInputControls(QPainter* painter) +void PolygonTextureMapper::drawInputControls(QPainter* painter, const QList* selectedVertices) { std::tr1::shared_ptr inputPoly = std::tr1::static_pointer_cast(inputShape); - Util::drawControlsPolygon(painter, *inputPoly); + Util::drawControlsPolygon(painter, selectedVertices, *inputPoly); } TriangleTextureMapper::TriangleTextureMapper(std::tr1::shared_ptr mapping) @@ -411,16 +411,16 @@ void MeshTextureMapper::setValue(QtProperty* property, const QVariant& value) TextureMapper::setValue(property, value); } -void MeshTextureMapper::drawControls(QPainter* painter) +void MeshTextureMapper::drawControls(QPainter* painter, const QList* selectedVertices) { std::tr1::shared_ptr outputMesh = std::tr1::static_pointer_cast(outputShape); - Util::drawControlsMesh(painter, *outputMesh); + Util::drawControlsMesh(painter, selectedVertices, *outputMesh); } -void MeshTextureMapper::drawInputControls(QPainter* painter) +void MeshTextureMapper::drawInputControls(QPainter* painter, const QList* selectedVertices) { std::tr1::shared_ptr inputMesh = std::tr1::static_pointer_cast(inputShape); - Util::drawControlsMesh(painter, *inputMesh); + Util::drawControlsMesh(painter, selectedVertices, *inputMesh); } void MeshTextureMapper::_doDraw(QPainter* painter) @@ -451,16 +451,16 @@ EllipseTextureMapper::EllipseTextureMapper(std::tr1::shared_ptr { } -void EllipseTextureMapper::drawControls(QPainter* painter) +void EllipseTextureMapper::drawControls(QPainter* painter, const QList* selectedVertices) { std::tr1::shared_ptr outputEllipse = std::tr1::static_pointer_cast(outputShape); - Util::drawControlsEllipse(painter, *outputEllipse); + Util::drawControlsEllipse(painter, selectedVertices, *outputEllipse); } -void EllipseTextureMapper::drawInputControls(QPainter* painter) +void EllipseTextureMapper::drawInputControls(QPainter* painter, const QList* selectedVertices) { std::tr1::shared_ptr inputEllipse = std::tr1::static_pointer_cast(inputShape); - Util::drawControlsEllipse(painter, *inputEllipse); + Util::drawControlsEllipse(painter, selectedVertices, *inputEllipse); } void EllipseTextureMapper::_doDraw(QPainter* painter) diff --git a/Mapper.h b/Mapper.h index d6aa978..e25de47 100644 --- a/Mapper.h +++ b/Mapper.h @@ -80,13 +80,17 @@ public: virtual void draw(QPainter* painter) = 0; /// Draws the output controls. - virtual void drawControls(QPainter* painter) = 0; + 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) { Q_UNUSED(painter); } + virtual void drawInputControls(QPainter* painter, const QList* selectedVertices=0) + { + Q_UNUSED(painter); + Q_UNUSED(selectedVertices); + } public slots: virtual void setValue(QtProperty* property, const QVariant& value); @@ -140,7 +144,7 @@ public: virtual ~PolygonColorMapper() {} virtual void draw(QPainter* painter); - virtual void drawControls(QPainter* painter); + virtual void drawControls(QPainter* painter, const QList* selectedVertices=0); }; class MeshColorMapper : public ColorMapper @@ -152,7 +156,7 @@ public: virtual ~MeshColorMapper() {} virtual void draw(QPainter* painter); - virtual void drawControls(QPainter* painter); + virtual void drawControls(QPainter* painter, const QList* selectedVertices=0); public slots: virtual void setValue(QtProperty* property, const QVariant& value); @@ -170,7 +174,7 @@ public: virtual ~EllipseColorMapper() {} virtual void draw(QPainter* painter); - virtual void drawControls(QPainter* painter); + virtual void drawControls(QPainter* painter, const QList* selectedVertices=0); }; /** @@ -191,8 +195,8 @@ public: virtual void draw(QPainter* painter); virtual void drawInput(QPainter* painter); - virtual void drawControls(QPainter* painter) = 0; - virtual void drawInputControls(QPainter* painter) = 0; + 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(Shape* shape); @@ -221,8 +225,8 @@ public: PolygonTextureMapper(std::tr1::shared_ptr mapping) : TextureMapper(mapping) {} virtual ~PolygonTextureMapper() {} - virtual void drawControls(QPainter* painter); - virtual void drawInputControls(QPainter* painter); + virtual void drawControls(QPainter* painter, const QList* selectedVertices=0); + virtual void drawInputControls(QPainter* painter, const QList* selectedVertices=0); }; class TriangleTextureMapper : public PolygonTextureMapper @@ -245,8 +249,8 @@ public: MeshTextureMapper(std::tr1::shared_ptr mapping); virtual ~MeshTextureMapper() {} - virtual void drawControls(QPainter* painter); - virtual void drawInputControls(QPainter* painter); + 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); @@ -265,8 +269,8 @@ public: EllipseTextureMapper(std::tr1::shared_ptr mapping); virtual ~EllipseTextureMapper() {} - virtual void drawControls(QPainter* painter); - virtual void drawInputControls(QPainter* painter); + virtual void drawControls(QPainter* painter, const QList* selectedVertices=0); + virtual void drawInputControls(QPainter* painter, const QList* selectedVertices=0); protected: virtual void _doDraw(QPainter* painter); diff --git a/MapperGLCanvas.cpp b/MapperGLCanvas.cpp index 626f7fa..893fd88 100644 --- a/MapperGLCanvas.cpp +++ b/MapperGLCanvas.cpp @@ -25,7 +25,7 @@ MapperGLCanvas::MapperGLCanvas(MainWindow* mainWindow, QWidget* parent, const QGLWidget * shareWidget) : QGLWidget(QGLFormat(QGL::SampleBuffers), parent, shareWidget), _mainWindow(mainWindow), - _mousePressedToDragVertex(false), + _mousePressedOnVertex(false), _activeVertex(NO_VERTEX), _shapeGrabbed(false), // comment out? _shapeFirstGrab(false), // comment out? @@ -103,7 +103,11 @@ void MapperGLCanvas::mousePressEvent(QMouseEvent* event) int i; int dist; int minDistance; - const QPointF& mousePos = event->posF(); + + 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; @@ -116,31 +120,37 @@ void MapperGLCanvas::mousePressEvent(QMouseEvent* event) // find the ID of the nearest vertex: (from the selected shape) for (i = 0; i < shape->nVertices(); i++) { - dist = distSq(mousePos, shape->getVertex(i)); // squared distance + dist = distSq(_mousePressedPosition, shape->getVertex(i)); // squared distance if (dist < minDistance) { _activeVertex = i; minDistance = dist; - _mousePressedToDragVertex = true; + _mousePressedOnVertex = true; + mousePressedOnSomething = true; } } - if (_mousePressedToDragVertex) { - return; - } } } + if (mousePressedOnSomething) + return; + + // Select a shape with a click. if (event->buttons() & Qt::LeftButton) { - // Select a shape with a click 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()); - if (shape && shape->includesPoint(mousePos)) + // Mouse pressed on a shape. + if (shape && shape->includesPoint(_mousePressedPosition)) { + mousePressedOnSomething = true; + // Deselect vertices. + deselectVertices(); + // Change mapping. if (shape != orig) { getMainWindow()->setCurrentMapping((*it)->getId()); @@ -148,31 +158,36 @@ void MapperGLCanvas::mousePressEvent(QMouseEvent* event) break; } } - } - // Drag the currently selected shape - if (event->buttons() & Qt::LeftButton) - { - Shape* shape = getCurrentShape(); - if (shape && shape->includesPoint(mousePos)) + // 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); - // std::cout << "Mouse Release event " << std::endl; - _mousePressedToDragVertex = false; +// // Click on vertex ==> select the vertex. +// if ((event->buttons() & Qt::LeftButton) && _mousePressedOnVertex) +// { +// } + _mousePressedOnVertex = false; _shapeGrabbed = false; } void MapperGLCanvas::mouseMoveEvent(QMouseEvent* event) { - if (_mousePressedToDragVertex) + if (_mousePressedOnVertex) { // std::cout << "Move event " << std::endl; Shape* shape = getCurrentShape(); @@ -200,7 +215,7 @@ void MapperGLCanvas::mouseMoveEvent(QMouseEvent* event) if (shape) { if (!_shapeFirstGrab) - { + { shape->translate(event->x() - prevMousePosition.x(), event->y() - prevMousePosition.y()); update(); emit shapeChanged(getCurrentShape()); @@ -311,10 +326,16 @@ void MapperGLCanvas::glueVertex(Shape *orig, QPointF *p) } } -void MapperGLCanvas::deselectAll() + +void MapperGLCanvas::deselectVertices() { _activeVertex = NO_VERTEX; + _mousePressedOnVertex = false; +} + +void MapperGLCanvas::deselectAll() +{ + deselectVertices(); _shapeGrabbed = false; _shapeFirstGrab = false; - _mousePressedToDragVertex = false; } diff --git a/MapperGLCanvas.h b/MapperGLCanvas.h index 8ca39db..360022d 100644 --- a/MapperGLCanvas.h +++ b/MapperGLCanvas.h @@ -53,6 +53,8 @@ public: MainWindow* getMainWindow() const { return _mainWindow; } bool displayControls() const { return _displayControls; } bool stickyVertices() const { return _stickyVertices; } + bool hasActiveVertex() const { return _activeVertex != NO_VERTEX; } + int getActiveVertexIndex() const { return _activeVertex; } protected: void initializeGL(); @@ -65,14 +67,20 @@ protected: void mouseReleaseEvent(QMouseEvent* event); void paintEvent(QPaintEvent* event); -private: +protected: void draw(QPainter* painter); void enterDraw(QPainter* painter); virtual void doDraw(QPainter* painter) = 0; void exitDraw(QPainter* painter); +private: MainWindow* _mainWindow; - bool _mousePressedToDragVertex; + + // Last point pressed. + QPoint _mousePressedPosition; + + // Mouse currently pressed inside a vertex. + bool _mousePressedOnVertex; int _activeVertex; bool _shapeGrabbed; bool _shapeFirstGrab; @@ -87,6 +95,7 @@ public slots: void updateCanvas(); void enableDisplayControls(bool display); void enableStickyVertices(bool display); + void deselectVertices(); void deselectAll(); public: diff --git a/SourceGLCanvas.cpp b/SourceGLCanvas.cpp index 0dbab27..5a2d127 100644 --- a/SourceGLCanvas.cpp +++ b/SourceGLCanvas.cpp @@ -47,14 +47,20 @@ void SourceGLCanvas::doDraw(QPainter* painter) if (getMainWindow()->hasCurrentMapping()) { uint mappingId = getMainWindow()->getCurrentMappingId(); - Mapper::ptr mapper = getMainWindow()->getMapperByMappingId(mappingId); + const Mapper::ptr& mapper = getMainWindow()->getMapperByMappingId(mappingId); painter->save(); mapper->drawInput(painter); painter->restore(); if (displayControls()) { painter->save(); - mapper->drawInputControls(painter); + if (hasActiveVertex()) { + QList selectedVertices; + selectedVertices.append(getActiveVertexIndex()); + mapper->drawInputControls(painter, &selectedVertices); + } + else + mapper->drawInputControls(painter); painter->restore(); } } diff --git a/Util.cpp b/Util.cpp index 9df2b35..c459b77 100644 --- a/Util.cpp +++ b/Util.cpp @@ -150,16 +150,10 @@ Ellipse* createEllipseForColor(int frameWidth, int frameHeight) ); } -void drawControlsVertices(QPainter* painter, const Shape& shape) -{ - for (int i=0; isetBrush(MM::VERTEX_BACKGROUND); + painter->setBrush(selected ? MM::VERTEX_SELECTED_BACKGROUND : MM::VERTEX_BACKGROUND); painter->setPen(QPen(MM::CONTROL_COLOR, strokeWidth)); // Draw ellipse. @@ -171,7 +165,17 @@ void drawControlsVertex(QPainter* painter, const QPointF& vertex, qreal radius, painter->drawLine( vertex + QPointF(offset, -offset), vertex + QPointF(-offset, offset) ); } -void drawControlsEllipse(QPainter* painter, const Ellipse& ellipse) +void drawControlsVertices(QPainter* painter, const QList* selectedVertices, const Shape& shape) +{ + if (!selectedVertices) + for (int i=0; icontains(i)); +} + +void drawControlsEllipse(QPainter* painter, const QList* selectedVertices, const Ellipse& ellipse) { // Init colors and stroke. painter->setPen(MM::SHAPE_STROKE); @@ -191,10 +195,10 @@ void drawControlsEllipse(QPainter* painter, const Ellipse& ellipse) painter->restore(); // restore saved painter state // Draw control points. - drawControlsVertices(painter, ellipse); + drawControlsVertices(painter, selectedVertices, ellipse); } -void drawControlsQuad(QPainter* painter, const Quad& quad) +void drawControlsQuad(QPainter* painter, const QList* selectedVertices, const Quad& quad) { // Init colors and stroke. painter->setPen(MM::SHAPE_STROKE); @@ -203,10 +207,10 @@ void drawControlsQuad(QPainter* painter, const Quad& quad) painter->drawPolygon(quad.toPolygon()); // Draw control points. - drawControlsVertices(painter, quad); + drawControlsVertices(painter, selectedVertices, quad); } -void drawControlsMesh(QPainter* painter, const Mesh& mesh) +void drawControlsMesh(QPainter* painter, const QList* selectedVertices, const Mesh& mesh) { // Init colors and stroke. painter->setPen(MM::SHAPE_INNER_STROKE); @@ -223,10 +227,10 @@ void drawControlsMesh(QPainter* painter, const Mesh& mesh) painter->drawPolygon(mesh.toPolygon()); // Draw control points. - drawControlsVertices(painter, mesh); + drawControlsVertices(painter, selectedVertices, mesh); } -void drawControlsPolygon(QPainter* painter, const Polygon& polygon) +void drawControlsPolygon(QPainter* painter, const QList* selectedVertices, const Polygon& polygon) { // Init colors and stroke. painter->setPen(MM::SHAPE_STROKE); @@ -235,7 +239,7 @@ void drawControlsPolygon(QPainter* painter, const Polygon& polygon) painter->drawPolygon(polygon.toPolygon()); // Draw control points. - drawControlsVertices(painter, polygon); + drawControlsVertices(painter, selectedVertices, polygon); } bool fileExists(const QString& filename) diff --git a/Util.h b/Util.h index 7f98219..d63ade9 100644 --- a/Util.h +++ b/Util.h @@ -57,12 +57,13 @@ Quad* createQuadForColor(int frameWidth, int frameHeight); Triangle* createTriangleForColor(int frameWidth, int frameHeight); Ellipse* createEllipseForColor(int frameWidth, int frameHeight); -void drawControlsVertices(QPainter* painter, const Shape& shape); -void drawControlsVertex(QPainter* painter, const QPointF& vertex, qreal radius = MM::VERTEX_SELECT_RADIUS, qreal strokeWidth = MM::VERTEX_SELECT_STROKE_WIDTH); -void drawControlsEllipse(QPainter* painter, const Ellipse& ellipse); -void drawControlsQuad(QPainter* painter, const Quad& quad); -void drawControlsMesh(QPainter* painter, const Mesh& mesh); -void drawControlsPolygon(QPainter* painter, const Polygon& polygon); +void drawControlsVertex(QPainter* painter, const QPointF& vertex, bool selected, qreal radius = MM::VERTEX_SELECT_RADIUS, qreal strokeWidth = MM::VERTEX_SELECT_STROKE_WIDTH); + +void drawControlsVertices(QPainter* painter, const QList* selectedVertices, const Shape& 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); +void drawControlsPolygon(QPainter* painter, const QList* selectedVertices, const Polygon& polygon); /** * Checks if a file exists or not.