From b95fd4cbf9813dc902859029990224df265c6a59 Mon Sep 17 00:00:00 2001 From: Mariano Lizarraga Date: Thu, 26 Aug 2010 11:32:20 -0500 Subject: [PATCH] Dragging of waypoints now working in the map control --- lib/QMapControl/src/layer.cpp | 45 ++++++++++++++------- lib/QMapControl/src/layer.h | 23 +++++++++++ lib/QMapControl/src/mapcontrol.cpp | 2 + src/ui/MapWidget.cc | 64 ++++++++++++++++++++++-------- src/ui/MapWidget.h | 7 +++- 5 files changed, 109 insertions(+), 32 deletions(-) diff --git a/lib/QMapControl/src/layer.cpp b/lib/QMapControl/src/layer.cpp index 65d0f3e21..6083b3bec 100644 --- a/lib/QMapControl/src/layer.cpp +++ b/lib/QMapControl/src/layer.cpp @@ -110,28 +110,45 @@ namespace qmapcontrol } void Layer::mouseEvent(const QMouseEvent* evnt, const QPoint mapmiddle_px) - { - if (takesMouseEvents()) { - if (evnt->button() == Qt::LeftButton && evnt->type() == QEvent::MouseButtonPress) + if (takesMouseEvents()) { - // check for collision - QPointF c = mapAdapter->displayToCoordinate(QPoint(evnt->x()-screenmiddle.x()+mapmiddle_px.x(), - evnt->y()-screenmiddle.y()+mapmiddle_px.y())); - Point* tmppoint = new Point(c.x(), c.y()); - for (int i=0; ibutton() == Qt::LeftButton && evnt->type() == QEvent::MouseButtonPress) { - if (geometries.at(i)->isVisible() && geometries.at(i)->Touches(tmppoint, mapAdapter)) - - //if (geometries.at(i)->Touches(c, mapAdapter)) + // check for collision + QPointF c = mapAdapter->displayToCoordinate(QPoint(evnt->x()-screenmiddle.x()+mapmiddle_px.x(), + evnt->y()-screenmiddle.y()+mapmiddle_px.y())); + Point* tmppoint = new Point(c.x(), c.y()); + for (int i=0; ix(), evnt->y()))); + if (geometries.at(i)->isVisible() && geometries.at(i)->Touches(tmppoint, mapAdapter)) + + //if (geometries.at(i)->Touches(c, mapAdapter)) + { + + emit(geometryClicked(geometries.at(i), QPoint(evnt->x(), evnt->y()))); + draggingGeometry = true; + geometrySelected = geometries.at(i); + } } + delete tmppoint; + } + + if (evnt->type() == QEvent::MouseButtonRelease){ + QPointF c = mapAdapter->displayToCoordinate(QPoint(evnt->x()-screenmiddle.x()+mapmiddle_px.x(), + evnt->y()-screenmiddle.y()+mapmiddle_px.y())); + draggingGeometry = false; + emit (geometryEndDrag(geometrySelected, c)); + geometrySelected = 0; + } + + if ( evnt->type() == QEvent::MouseMove && draggingGeometry){ + QPointF c = mapAdapter->displayToCoordinate(QPoint(evnt->x()-screenmiddle.x()+mapmiddle_px.x(), + evnt->y()-screenmiddle.y()+mapmiddle_px.y())); + emit(geometryDragged(geometrySelected, c)); } - delete tmppoint; } } - } bool Layer::takesMouseEvents() const { diff --git a/lib/QMapControl/src/layer.h b/lib/QMapControl/src/layer.h index 14debeaac..9d677916b 100644 --- a/lib/QMapControl/src/layer.h +++ b/lib/QMapControl/src/layer.h @@ -63,6 +63,7 @@ namespace qmapcontrol public: friend class LayerManager; + //! sets the type of a layer, see Layer class doc for further information enum LayerType { @@ -157,6 +158,9 @@ namespace qmapcontrol bool takeevents; mutable QRect myoffscreenViewport; + Geometry* geometrySelected; + bool draggingGeometry; + signals: //! This signal is emitted when a Geometry is clicked /*! @@ -167,6 +171,25 @@ namespace qmapcontrol */ void geometryClicked(Geometry* geometry, QPoint point); + //! This signal is emitted while a Geometry is being dragged + /*! + * A Geometry is clickable, if the containing layer is clickable. + * The layer emits a signal as it is dragged + * @param geometry The selected Geometry + * @param coordinate The new coordinate (in world coordinates) + */ + void geometryDragged(Geometry* geometrySelected, QPointF coordinate); + + + //! This signal is emitted when a User releases the button after selecting a Geometry + /*! + * A Geometry is clickable, if the containing layer is clickable. + * The layer emits a signal when it is released + * @param geometry The selected Geometry + * @param coordinate The new coordinate (in world coordinates) + */ + void geometryEndDrag(Geometry* geometrySelected, QPointF coordinate); + void updateRequest(QRectF rect); void updateRequest(); diff --git a/lib/QMapControl/src/mapcontrol.cpp b/lib/QMapControl/src/mapcontrol.cpp index 57df98b7d..7d53b7fc5 100644 --- a/lib/QMapControl/src/mapcontrol.cpp +++ b/lib/QMapControl/src/mapcontrol.cpp @@ -255,6 +255,7 @@ namespace qmapcontrol void MapControl::mouseReleaseEvent(QMouseEvent* evnt) { + layermanager->mouseEvent(evnt); mousepressed = false; if (mymousemode == Dragging) { @@ -271,6 +272,7 @@ namespace qmapcontrol void MapControl::mouseMoveEvent(QMouseEvent* evnt) { + layermanager->mouseEvent(evnt); emit(mouseEvent(evnt)); diff --git a/src/ui/MapWidget.cc b/src/ui/MapWidget.cc index 600f4dd62..4811d9557 100644 --- a/src/ui/MapWidget.cc +++ b/src/ui/MapWidget.cc @@ -190,11 +190,16 @@ MapWidget::MapWidget(QWidget *parent) : this, SLOT(captureMapClick(const QMouseEvent*, const QPointF))); connect(createPath, SIGNAL(clicked(bool)), - this, SLOT(createPathButtonClicked())); + this, SLOT(createPathButtonClicked(bool))); connect(geomLayer, SIGNAL(geometryClicked(Geometry*,QPoint)), this, SLOT(captureGeometryClick(Geometry*, QPoint))); + connect(geomLayer, SIGNAL(geometryDragged(Geometry*, QPointF)), + this, SLOT(captureGeometryDrag(Geometry*, QPointF))); + + connect(geomLayer, SIGNAL(geometryEndDrag(Geometry*, QPointF)), + this, SLOT(captureGeometryEndDrag(Geometry*, QPointF))); // Configure the WP Path's pen pointPen = new QPen(QColor(0, 255,0)); @@ -290,12 +295,15 @@ void MapWidget::mapproviderSelected(QAction* action) } -void MapWidget::createPathButtonClicked() +void MapWidget::createPathButtonClicked(bool checked) { + Q_UNUSED(checked); + if (createPath->isChecked()) { // change the cursor shape this->setCursor(Qt::PointingHandCursor); + mc->setMouseMode(qmapcontrol::MapControl::None); // Clear the previous WP track // TODO: Move this to an actual clear track button and add a warning dialog @@ -303,8 +311,12 @@ void MapWidget::createPathButtonClicked() wps.clear(); path->setPoints(wps); mc->layer("Waypoints")->addGeometry(path); + wpIndex.clear(); + + } else { this->setCursor(Qt::ArrowCursor); + mc->setMouseMode(qmapcontrol::MapControl::Panning); } } @@ -313,37 +325,55 @@ void MapWidget::createPathButtonClicked() void MapWidget::captureMapClick(const QMouseEvent* event, const QPointF coordinate){ if (QEvent::MouseButtonRelease == event->type() && createPath->isChecked()){ - // Create waypoint name QString str; - str = QString("WP%1").arg(path->numberOfPoints()+1); - - qDebug()<< "Waypoint " << str; - qDebug()<< "Lat: " << coordinate.y(); - qDebug()<< "Lon: " << coordinate.x(); + str = QString("WP%1").arg(path->numberOfPoints()); // create the WP and set everything in the LineString to display the path - mc->layer("Waypoints")->addGeometry(new CirclePoint(coordinate.x(), coordinate.y(), 10, str)); - wps.append(new Point(coordinate.x(), coordinate.y(),str)); - path->addPoint(new Point(coordinate.x(), coordinate.y(),str)); + CirclePoint* tempCirclePoint = new CirclePoint(coordinate.x(), coordinate.y(), 10, str); + mc->layer("Waypoints")->addGeometry(tempCirclePoint); + + Point* tempPoint = new Point(coordinate.x(), coordinate.y(),str); + wps.append(tempPoint); + path->addPoint(tempPoint); + + wpIndex.insert(str,tempPoint); + // Refresh the screen mc->updateRequestNew(); } } void MapWidget::captureGeometryClick(Geometry* geom, QPoint point){ + Q_UNUSED(geom); + Q_UNUSED(point); + + mc->setMouseMode(qmapcontrol::MapControl::None); + - qDebug() << geom->name(); - qDebug() << geom->GeometryType; - qDebug() << point; } void MapWidget::captureGeometryDrag(Geometry* geom, QPointF coordinate){ + Q_UNUSED(coordinate); + + Point* point2Find; + point2Find = wpIndex[geom->name()]; + point2Find->setCoordinate(coordinate); + + point2Find = dynamic_cast (geom); + point2Find->setCoordinate(coordinate); + + // Refresh the screen + mc->updateRequestNew(); +} + +void MapWidget::captureGeometryEndDrag(Geometry* geom, QPointF coordinate){ + mc->setMouseMode(qmapcontrol::MapControl::Panning); - qDebug() << geom->name(); - qDebug() << geom->GeometryType; - qDebug() << coordinate; +// qDebug() << geom->name(); +// qDebug() << geom->GeometryType; +// qDebug() << point; } MapWidget::~MapWidget() diff --git a/src/ui/MapWidget.h b/src/ui/MapWidget.h index 71624997e..17929329c 100644 --- a/src/ui/MapWidget.h +++ b/src/ui/MapWidget.h @@ -102,14 +102,19 @@ protected: protected slots: void captureMapClick (const QMouseEvent* event, const QPointF coordinate); - void createPathButtonClicked(); + void createPathButtonClicked(bool checked); void captureGeometryClick(Geometry*, QPoint); void mapproviderSelected(QAction* action); void captureGeometryDrag(Geometry* geom, QPointF coordinate); + void captureGeometryEndDrag(Geometry* geom, QPointF coordinate); + + signals: + void movePoint(QPointF newCoord); private: Ui::MapWidget *m_ui; QList wps; + QHash wpIndex; LineString* path; QPen* pointPen; }; -- 2.22.0