From a970bbc66000d79cda7b25e626d0c77fcb519de6 Mon Sep 17 00:00:00 2001 From: Tats Date: Wed, 15 Jul 2015 12:50:24 -0600 Subject: [PATCH] - Manage properly the picking up of vertices and shapes - Fix bug: in source canvas we could change mappings by clicking on shape --- DestinationGLCanvas.cpp | 4 ++-- DestinationGLCanvas.h | 5 +++-- MainWindow.h | 4 ++-- MapperGLCanvas.cpp | 45 ++++++++++++++++++++++++----------------- MapperGLCanvas.h | 5 +++-- SourceGLCanvas.cpp | 4 ++-- SourceGLCanvas.h | 5 +++-- 7 files changed, 41 insertions(+), 31 deletions(-) diff --git a/DestinationGLCanvas.cpp b/DestinationGLCanvas.cpp index 84204c3..71f3740 100644 --- a/DestinationGLCanvas.cpp +++ b/DestinationGLCanvas.cpp @@ -26,7 +26,7 @@ DestinationGLCanvas::DestinationGLCanvas(MainWindow* mainWindow, QWidget* parent { } -MShape* DestinationGLCanvas::getShapeFromMappingId(uid mappingId) +MShape* DestinationGLCanvas::getShapeFromMappingId(uid mappingId) const { if (mappingId == NULL_UID) return NULL; @@ -34,7 +34,7 @@ MShape* DestinationGLCanvas::getShapeFromMappingId(uid mappingId) return getMainWindow()->getMappingManager().getMappingById(mappingId)->getShape().get(); } -ShapeGraphicsItem* DestinationGLCanvas::getShapeGraphicsItemFromMappingId(uid mappingId) +ShapeGraphicsItem* DestinationGLCanvas::getShapeGraphicsItemFromMappingId(uid mappingId) const { if (mappingId == NULL_UID) return NULL; diff --git a/DestinationGLCanvas.h b/DestinationGLCanvas.h index 7cee0c4..ef5b443 100644 --- a/DestinationGLCanvas.h +++ b/DestinationGLCanvas.h @@ -34,8 +34,9 @@ public: DestinationGLCanvas(MainWindow* mainWindow, QWidget* parent = 0, const QGLWidget* shareWidget = 0, QGraphicsScene* scene = 0); virtual ~DestinationGLCanvas() {} - virtual MShape* getShapeFromMappingId(uid mappingId); - virtual ShapeGraphicsItem* getShapeGraphicsItemFromMappingId(uid mappingId); + virtual bool isOutput() const { return true; } + virtual MShape* getShapeFromMappingId(uid mappingId) const; + virtual ShapeGraphicsItem* getShapeGraphicsItemFromMappingId(uid mappingId) const; }; #endif /* DESTINATIONGLCANVAS_H_ */ diff --git a/MainWindow.h b/MainWindow.h index 1555188..1346dc2 100644 --- a/MainWindow.h +++ b/MainWindow.h @@ -388,8 +388,8 @@ private: public: // Accessor/mutators for the view. /////////////////////////////////////////////////////////////////// - MappingManager& getMappingManager() { return *mappingManager; } - Mapper::ptr getMapperByMappingId(uint id) { return mappers[id]; } + MappingManager& getMappingManager() const { return *mappingManager; } + Mapper::ptr getMapperByMappingId(uint id) const { return mappers[id]; } uid getCurrentPaintId() const { return currentPaintId; } uid getCurrentMappingId() const { return currentMappingId; } OutputGLWindow* getOutputWindow() const { return outputWindow; } diff --git a/MapperGLCanvas.cpp b/MapperGLCanvas.cpp index ef58167..8de3ec9 100644 --- a/MapperGLCanvas.cpp +++ b/MapperGLCanvas.cpp @@ -37,6 +37,8 @@ MapperGLCanvas::MapperGLCanvas(MainWindow* mainWindow, QWidget* parent, const QG setRenderHint(QPainter::Antialiasing, true); setRenderHint(QPainter::TextAntialiasing, true); + setResizeAnchor(AnchorViewCenter); + setInteractive(true); //setFrameStyle(Sunken | StyledPanel); // TODO: check this @@ -71,6 +73,7 @@ ShapeGraphicsItem* MapperGLCanvas::getCurrentShapeGraphicsItem() // Draws foreground (displays crosshair if needed). void MapperGLCanvas::drawForeground(QPainter *painter , const QRectF &rect) { + Q_UNUSED(rect); if (_mainWindow->displayControls()) { uid mid = _mainWindow->getCurrentMappingId(); @@ -86,28 +89,25 @@ void MapperGLCanvas::drawForeground(QPainter *painter , const QRectF &rect) // void MapperGLCanvas::mousePressEvent(QMouseEvent* event) { - int i; - int dist; - int minDistance; - bool mousePressedOnSomething = false; _mousePressedPosition = event->pos(); QPointF pos = mapToScene(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) + // Check for vertex selection first. + else if (event->buttons() & Qt::LeftButton) { MShape* shape = getCurrentShape(); if (shape) { - // find the ID of the nearest vertex: (from the selected shape) - for (i = 0; i < shape->nVertices(); i++) + // Note: we compare with the square value for fastest computation of the distance + int minDistance = sq(MM::VERTEX_SELECT_RADIUS); + + // Find the ID of the nearest vertex (from currently selected shape) + for (int i = 0; i < shape->nVertices(); i++) { - dist = distSq(pos, shape->getVertex(i)); // squared distance + int dist = distSq(_mousePressedPosition, mapFromScene(shape->getVertex(i))); // squared distance if (dist < minDistance) { _activeVertex = i; @@ -125,29 +125,36 @@ void MapperGLCanvas::mousePressEvent(QMouseEvent* event) if (mousePressedOnSomething) return; - // Select a shape with a click. - if (event->buttons() & Qt::LeftButton || Qt::RightButton) // Add Right click for context menu + // Check for shape selection. + if (event->buttons() & (Qt::LeftButton | Qt::RightButton)) // Add Right click for context menu { MShape* selectedShape = getCurrentShape(); + + // Possibility of changing shape in output by clicking on it. MappingManager manager = getMainWindow()->getMappingManager(); QVector mappings = manager.getVisibleMappings(); for (QVector::const_iterator it = mappings.end() - 1; it >= mappings.begin(); --it) { MShape *shape = getShapeFromMappingId((*it)->getId()); - // Mouse pressed on a shape. + + // Check if mouse was pressed on that shape. if (shape && shape->includesPoint(pos)) { mousePressedOnSomething = true; + // Deselect vertices. deselectVertices(); - // Change mapping. - if (shape != selectedShape) + + // Change mapping (only available in destination). + if (isOutput() && shape != selectedShape) { + // Change current mapping. getMainWindow()->setCurrentMapping((*it)->getId()); - // Reset orig. + // Reset selected shape to new one. selectedShape = getCurrentShape(); } + break; } } @@ -242,10 +249,10 @@ void MapperGLCanvas::mouseMoveEvent(QMouseEvent* event) // Window translation action else if (event->buttons() & Qt::MiddleButton) { - QPointF diff = pos - mapToScene(lastMousePos); + QPointF diff = event->pos() - lastMousePos; QGraphicsView* view = scene()->views().first(); view->translate(diff.x(), diff.y()); - view->update(); +// view->update(); } lastMousePos = event->pos(); diff --git a/MapperGLCanvas.h b/MapperGLCanvas.h index 212cdff..ab388fd 100644 --- a/MapperGLCanvas.h +++ b/MapperGLCanvas.h @@ -54,8 +54,9 @@ public: virtual ~MapperGLCanvas() {} /// Returns shape associated with mapping id. - virtual MShape* getShapeFromMappingId(uid mappingId) = 0; - virtual ShapeGraphicsItem* getShapeGraphicsItemFromMappingId(uid mappingId) = 0; + virtual bool isOutput() const = 0; + virtual MShape* getShapeFromMappingId(uid mappingId) const = 0; + virtual ShapeGraphicsItem* getShapeGraphicsItemFromMappingId(uid mappingId) const = 0; MShape* getCurrentShape(); ShapeGraphicsItem* getCurrentShapeGraphicsItem(); diff --git a/SourceGLCanvas.cpp b/SourceGLCanvas.cpp index 3118812..7236b7f 100644 --- a/SourceGLCanvas.cpp +++ b/SourceGLCanvas.cpp @@ -28,7 +28,7 @@ SourceGLCanvas::SourceGLCanvas(MainWindow* mainWindow, QWidget* parent) { } -MShape* SourceGLCanvas::getShapeFromMappingId(uid mappingId) +MShape* SourceGLCanvas::getShapeFromMappingId(uid mappingId) const { if (mappingId == NULL_UID) return NULL; @@ -41,7 +41,7 @@ MShape* SourceGLCanvas::getShapeFromMappingId(uid mappingId) } } -ShapeGraphicsItem* SourceGLCanvas::getShapeGraphicsItemFromMappingId(uid mappingId) +ShapeGraphicsItem* SourceGLCanvas::getShapeGraphicsItemFromMappingId(uid mappingId) const { if (mappingId == NULL_UID) return NULL; diff --git a/SourceGLCanvas.h b/SourceGLCanvas.h index 4622060..09c581c 100644 --- a/SourceGLCanvas.h +++ b/SourceGLCanvas.h @@ -35,8 +35,9 @@ public: SourceGLCanvas(MainWindow* mainWindow, QWidget* parent = 0); virtual ~SourceGLCanvas() {} - virtual MShape* getShapeFromMappingId(uid mappingId); - virtual ShapeGraphicsItem* getShapeGraphicsItemFromMappingId(uid mappingId); + virtual bool isOutput() const { return false; } + virtual MShape* getShapeFromMappingId(uid mappingId) const; + virtual ShapeGraphicsItem* getShapeGraphicsItemFromMappingId(uid mappingId) const; private: // virtual void doDraw(QPainter* painter);