AmonRaNet / QGeoView

QGeoView is a Qt / C ++ widget for visualizing geographic data.
GNU Lesser General Public License v3.0
155 stars 60 forks source link

Movable Items #17

Closed JaiPasdeBol closed 2 years ago

JaiPasdeBol commented 2 years ago

Hello,

I am trying to use your library to display movable items. What I mean by movable item is using the mouse to put them somewhere else and to update both geo and projection coordonate. But I don't manage to do that.

I took your demo and try to get the "rectangle" item movable. For that, I create a new flag and add it in the refresh method of QGVDrawItem

void QGVDrawItem::refresh()
{    
    if(isFlag(QGV::ItemFlag::Movable))
        mQGDrawItem->setFlag(QGraphicsItem::ItemIsMovable); etc...
}

Now I can move this item with my mouse that's cool but what I want is to use the  mGeoRect and mProjRect of the new position of the rectangle after being moved. I try this:

 QRectF copy = mProjRect;
    copy.moveCenter(getScenePosItem());
    mGeoRect = getMap()->getProjection()->projToGeo(copy);
    onProjection(getMap());

Where getScenePosItem() return mQGDrawItem->scenePos() But as I do that my object is draw again twice far than the distance that I applied to it with the mouse. I don't know where's my mystake. Can you explain me my mistake or giving me and exemple of a class that do what I want? Thank you.

JPB

AmonRaNet commented 2 years ago

Mouse actions in QGeoView is not trivial question. You should keep in mind QGeoView fully encapsulate QGraphicsView, which includes full interception of most of events - especially mouse, wheel and keyboard. There is several reasons for this: one is historical :), second more critical is expected behavior. QGraphicsView makes a lot of hidden work when we talk about his mouse events (also some keyboard events) and these creates tons of problems in many places, because QGeoView should know every time how map is shown to user (in other words: what values has "camera"). Most simple solution was - lets catch all events and replace them as I want it. It works, but as cost now you need to handle all actions yourself :) As soon you broke this rule (and QGraphicsItem::ItemIsMovable exactly this case) you will have issues, because QGVDrawItem and his encapsulated QGraphicsItem can have different coordinates. Probably it can be fixed by override for QGraphicsItem::itemChange + new virtual methods. But I dont like this solution - first because I want to have more control during move (like run-time accept, reject, object paint, etc), second it requires redesign of QGVMapQGView, because it catches all mouse events and will/can block integrated QGraphicsItem::ItemIsMovable support.

I did home work and have implemented QGeoView-like solution in PR: https://github.com/AmonRaNet/QGeoView/pull/18 Please look to it - maybe is fulfills yours requirement or you can use it as base.

JaiPasdeBol commented 2 years ago

Hi! Thank you very much! It's perfect. Your answer was really fast and it completly fulfills my needs. And adding this with your two commits really help to understand the philosophy of the library. I think this possibility will help a lot of people like me who need to construct small specific GeoData files. Thanks again.

JPB