<commit_before>#include "PrivateQGraphicsObject.h"

#include <QtDebug>
#include <QKeyEvent>

#include "guts/Conversions.h"

PrivateQGraphicsObject::PrivateQGraphicsObject(MapGraphicsObject *mgObj,
                                               PrivateQGraphicsInfoSource *infoSource,
                                               QGraphicsItem *parent) :
    QGraphicsObject(parent), _infoSource(infoSource)
{
    this->setMGObj(mgObj);
    this->setZValue(5.0);

    this->setFlag(QGraphicsItem::ItemIsMovable,true);
    this->setFlag(QGraphicsItem::ItemIsSelectable,true);
}

//pure-virtual from QGraphicsItem
QRectF PrivateQGraphicsObject::boundingRect() const
{
    QRectF toRet(-1.0,-1.0,2.0,2.0);
    if (_mgObj.isNull())
    {
        qWarning() << this << "could not get bounding rect as MapGraphicsObject is null";
        return toRet;
    }

    //Get bounding rect in ENU
    QRectF enuRect = _mgObj->boundingRect();

    //Convert from ENU to lat/lon
    QPointF latLonCenter = _mgObj->pos();
    Conversions::Position latLonCenterPos = {latLonCenter,0.0};
    QPointF leftLatLon = Conversions::enu2lla(enuRect.left(),
                                              0.0,
                                              0.0,
                                              latLonCenterPos).lonlat;
    QPointF upLatLon = Conversions::enu2lla(0.0,
                                            enuRect.top(),
                                            0.0,
                                            latLonCenterPos).lonlat;

    qreal lonWidth = 2.0*(latLonCenter.x() - leftLatLon.x());
    qreal latHeight = 2.0*(upLatLon.y() - latLonCenter.y());

    //Once we've got the rect in lat/lon, we should convert it to scene pixels
    QRectF latLonRect(leftLatLon.x(),upLatLon.y(),lonWidth,latHeight);

    //We need our tile source to do the conversion
    QSharedPointer<MapTileSource> tileSource = _infoSource->tileSource();
    if (tileSource.isNull())
    {
        qWarning() << this << "can't do bounding box conversion, null tile source.";
        return toRet;
    }

    int zoomLevel = _infoSource->zoomLevel();
    QPointF topLeft = tileSource->ll2qgs(latLonRect.topLeft(),zoomLevel);
    QPointF bottomRight = tileSource->ll2qgs(latLonRect.bottomRight(),zoomLevel);

    toRet = QRectF(topLeft,bottomRight);
    toRet.moveCenter(QPointF(0,0));
    return toRet;
}

//pure-virtual from QGraphicsItem
void PrivateQGraphicsObject::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    if (_mgObj.isNull())
    {
        qWarning() << this << "could not paint as our MapGraphicsObject is null";
        return;
    }

    //Transform painter coordinates to ENU and then have the MapGraphicsObject do its thing
    QRectF enuRect = _mgObj->boundingRect();
    qreal desiredWidthMeters = enuRect.width();
    qreal desiredHeightMeters = enuRect.height();
    QRectF pixelRect = this->boundingRect();
    qreal widthPixels = pixelRect.width();
    qreal heightPixels = pixelRect.height();

    qreal scaleX = widthPixels / desiredWidthMeters;
    qreal scaleY = heightPixels / desiredHeightMeters;

    painter->scale(scaleX,scaleY);

    if (this->isSelected())
        painter->fillRect(enuRect,Qt::red);

    _mgObj->paint(painter,option,widget);
}

//override from QGraphicsItem
void PrivateQGraphicsObject::setSelected(bool selected)
{
    qDebug() << "Selected:" << selected;
    QGraphicsItem::setSelected(selected);

    //Tell the MapGraphicsObject that we represent about it
    _mgObj->setSelected(this->isSelected());
}

//protected
void PrivateQGraphicsObject::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{
    if (_mgObj.isNull())
        return;

    _mgObj->contextMenuEvent(event);

    if (!event->isAccepted())
        QGraphicsObject::contextMenuEvent(event);
}

//protected
QVariant PrivateQGraphicsObject::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
{
    if (_mgObj.isNull())
        return value;

    return _mgObj->itemChange(change,value);
}

//protected
void PrivateQGraphicsObject::keyPressEvent(QKeyEvent *event)
{
    if (_mgObj.isNull())<commit_msg>Moving objects with the mouse should work now<commit_after>#include "PrivateQGraphicsObject.h"

#include <QtDebug>
#include <QKeyEvent>

#include "guts/Conversions.h"

PrivateQGraphicsObject::PrivateQGraphicsObject(MapGraphicsObject *mgObj,
                                               PrivateQGraphicsInfoSource *infoSource,
                                               QGraphicsItem *parent) :
    QGraphicsObject(parent), _infoSource(infoSource)
{
    this->setMGObj(mgObj);
    this->setZValue(5.0);

    this->setFlag(QGraphicsItem::ItemIsMovable,true);
    this->setFlag(QGraphicsItem::ItemIsSelectable,true);
    this->setFlag(QGraphicsItem::ItemSendsScenePositionChanges,true);
}

//pure-virtual from QGraphicsItem
QRectF PrivateQGraphicsObject::boundingRect() const
{
    QRectF toRet(-1.0,-1.0,2.0,2.0);
    if (_mgObj.isNull())
    {
        qWarning() << this << "could not get bounding rect as MapGraphicsObject is null";
        return toRet;
    }

    //Get bounding rect in ENU
    QRectF enuRect = _mgObj->boundingRect();

    //Convert from ENU to lat/lon
    QPointF latLonCenter = _mgObj->pos();
    Conversions::Position latLonCenterPos = {latLonCenter,0.0};
    QPointF leftLatLon = Conversions::enu2lla(enuRect.left(),
                                              0.0,
                                              0.0,
                                              latLonCenterPos).lonlat;
    QPointF upLatLon = Conversions::enu2lla(0.0,
                                            enuRect.top(),
                                            0.0,
                                            latLonCenterPos).lonlat;

    qreal lonWidth = 2.0*(latLonCenter.x() - leftLatLon.x());
    qreal latHeight = 2.0*(upLatLon.y() - latLonCenter.y());

    //Once we've got the rect in lat/lon, we should convert it to scene pixels
    QRectF latLonRect(leftLatLon.x(),upLatLon.y(),lonWidth,latHeight);

    //We need our tile source to do the conversion
    QSharedPointer<MapTileSource> tileSource = _infoSource->tileSource();
    if (tileSource.isNull())
    {
        qWarning() << this << "can't do bounding box conversion, null tile source.";
        return toRet;
    }

    int zoomLevel = _infoSource->zoomLevel();
    QPointF topLeft = tileSource->ll2qgs(latLonRect.topLeft(),zoomLevel);
    QPointF bottomRight = tileSource->ll2qgs(latLonRect.bottomRight(),zoomLevel);

    toRet = QRectF(topLeft,bottomRight);
    toRet.moveCenter(QPointF(0,0));
    return toRet;
}

//pure-virtual from QGraphicsItem
void PrivateQGraphicsObject::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    if (_mgObj.isNull())
    {
        qWarning() << this << "could not paint as our MapGraphicsObject is null";
        return;
    }

    //Transform painter coordinates to ENU and then have the MapGraphicsObject do its thing
    QRectF enuRect = _mgObj->boundingRect();
    qreal desiredWidthMeters = enuRect.width();
    qreal desiredHeightMeters = enuRect.height();
    QRectF pixelRect = this->boundingRect();
    qreal widthPixels = pixelRect.width();
    qreal heightPixels = pixelRect.height();

    qreal scaleX = widthPixels / desiredWidthMeters;
    qreal scaleY = heightPixels / desiredHeightMeters;

    painter->scale(scaleX,scaleY);

    if (this->isSelected())
        painter->fillRect(enuRect,Qt::red);

    _mgObj->paint(painter,option,widget);
}

//override from QGraphicsItem
void PrivateQGraphicsObject::setSelected(bool selected)
{
    QGraphicsItem::setSelected(selected);

    //Tell the MapGraphicsObject that we represent about it
    _mgObj->setSelected(this->isSelected());
}

//protected
void PrivateQGraphicsObject::contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
{
    if (_mgObj.isNull())
        return;

    _mgObj->contextMenuEvent(event);

    if (!event->isAccepted())
        QGraphicsObject::contextMenuEvent(event);
}

//protected
QVariant PrivateQGraphicsObject::itemChange(QGraphicsItem::GraphicsItemChange change, const QVariant &value)
{
    if (_mgObj.isNull())
        return value;

    //When the position changes as a result of user action we need to update the MapGraphicsObject's geo pos
    if (change == QGraphicsItem::ItemPositionChange)
    {
        QPointF scenePos = value.toPointF();
        QSharedPointer<MapTileSource> tileSource = _infoSource->tileSource();
        if (!tileSource.isNull())
        {
            QPointF geoPos = tileSource->qgs2ll(scenePos,_infoSource->zoomLevel());

            //Hackz
            this->setFlag(QGraphicsItem::ItemSendsScenePositionChanges,false);
            _mgObj->setPos(geoPos);
            this->setFlag(QGraphicsItem::ItemSendsScenePositionChanges,true);
        }

    }

    return _mgObj->itemChange(change,value);
}

//protected
void PrivateQGraphicsObject::keyPressEvent(QKeyEvent *event)
{
    if (_mgObj.isNull())
{'fraction_non_alphanumeric': 0.1104728236853734, 'fraction_numerical': 0.0067388422448077774, 'mean_word_length': 2.8230574324324325, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 32, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2000721', 'n_tokens_mistral': 2704, 'n_tokens_neox': 2634, 'n_words': 550}