#include "maptooldrawline.h"
#include <QDebug>

#include "mapline.h"
#include "mapcanvas.h"
#include "maplayer.h"
#pragma execution_character_set("utf-8")

void LAMPMainWidget::MapToolDrawline::execute(QMouseEvent *event)
{
    QPointF scene_point=this->mMapCanvas->mapToScene(event->pos());
    QPointF mkt_point=scene_point*this->mMapCanvas->currentLayer()->resolution();
    if(event->type()==QEvent::MouseMove&&isDrawing){
        this->current_line->lastMktpoint()=mkt_point;
    }

    if( event->type() != QEvent::MouseButtonPress) return;
    if(event->button()==Qt::LeftButton){
        if(!isDrawing){
            qDebug()<<"begin drawline";
            isDrawing=true;
            this->current_line=new Mapline(this->mMapCanvas->currentLayer());
            this->mMapCanvas->scene()->addItem(current_line);
            this->current_line->append(mkt_point);
        }
        this->current_line->append(mkt_point);
    }
    if(event->button()==Qt::RightButton){
        isDrawing=false;
        qDebug()<<"end drawline";
    }
}

void LAMPMainWidget::MapToolDrawline::setup()
{
    mMapCanvas->setCursor(Qt::CrossCursor);
}

void LAMPMainWidget::MapToolDrawline::deSetup()
{
    mMapCanvas->setCursor(Qt::ArrowCursor);
}

QString LAMPMainWidget::MapToolDrawline::id()
{
    return QString("drawline_tool");
}
