waypointitem.cpp 10.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/**
******************************************************************************
*
* @file       waypointitem.cpp
* @author     The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
* @brief      A graphicsItem representing a WayPoint
* @see        The GNU Public License (GPL) Version 3
* @defgroup   OPMapWidget
* @{
*
*****************************************************************************/
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "waypointitem.h"
namespace mapcontrol
{
lm's avatar
lm committed
30 31
    WayPointItem::WayPointItem(const internals::PointLatLng &coord,double const& altitude, MapGraphicItem *map) :
        map(map),
32
        autoreachedEnabled(true),
lm's avatar
lm committed
33 34 35 36 37 38
        coord(coord),
        reached(false),
        description(""),
        shownumber(true),
        isDragging(false),
        altitude(altitude),
39
        heading(0)
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
    {
        text=0;
        numberI=0;
        picture.load(QString::fromUtf8(":/markers/images/marker.png"));
        number=WayPointItem::snumber;
        ++WayPointItem::snumber;
        this->setFlag(QGraphicsItem::ItemIsMovable,true);
        this->setFlag(QGraphicsItem::ItemIgnoresTransformations,true);
        this->setFlag(QGraphicsItem::ItemIsSelectable,true);
       // transf.translate(picture.width()/2,picture.height());
       // this->setTransform(transf);
        SetShowNumber(shownumber);
        RefreshToolTip();
        RefreshPos();
    }
lm's avatar
lm committed
55
    WayPointItem::WayPointItem(const internals::PointLatLng &coord,double const& altitude, const QString &description, MapGraphicItem *map) :
56
        map(map),
lm's avatar
lm committed
57 58 59 60 61 62 63
        coord(coord),
        reached(false),
        description(description),
        shownumber(true),
        isDragging(false),
        altitude(altitude),
        heading(0)
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    {
        text=0;
        numberI=0;
        picture.load(QString::fromUtf8(":/markers/images/marker.png"));
        number=WayPointItem::snumber;
        ++WayPointItem::snumber;
        this->setFlag(QGraphicsItem::ItemIsMovable,true);
        this->setFlag(QGraphicsItem::ItemIgnoresTransformations,true);
        this->setFlag(QGraphicsItem::ItemIsSelectable,true);
       //transf.translate(picture.width()/2,picture.height());
       // this->setTransform(transf);
        SetShowNumber(shownumber);
        RefreshToolTip();
        RefreshPos();
    }

    QRectF WayPointItem::boundingRect() const
    {
        return QRectF(-picture.width()/2,-picture.height(),picture.width(),picture.height());
    }

    void WayPointItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
        Q_UNUSED(option);
        Q_UNUSED(widget);
        painter->drawPixmap(-picture.width()/2,-picture.height(),picture);
        if(this->isSelected())
        {
            painter->drawRect(QRectF(-picture.width()/2,-picture.height(),picture.width()-1,picture.height()-1));
        }
    }
    void WayPointItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
    {
        if(event->button()==Qt::LeftButton)
        {
	    text=new QGraphicsSimpleTextItem(this);
            textBG=new QGraphicsRectItem(this);

//	    textBG->setBrush(Qt::white);
//	    textBG->setOpacity(0.5);

	    textBG->setBrush(QColor(255, 255, 255, 128));

	    text->setPen(QPen(Qt::red));
	    text->setPos(10,-picture.height());
	    textBG->setPos(10,-picture.height());
	    text->setZValue(3);
	    RefreshToolTip();
	    isDragging=true;
	}
        QGraphicsItem::mousePressEvent(event);
    }
    void WayPointItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
    {
        if(event->button()==Qt::LeftButton)
        {
            delete text;
            delete textBG;
            coord=map->FromLocalToLatLng(this->pos().x(),this->pos().y());
123
            QString coord_str = " " + QString::number(coord.Lat(), 'f', 6) + "   " + QString::number(coord.Lng(), 'f', 6);
LM's avatar
LM committed
124
            // qDebug() << "WP MOVE:" << coord_str << __FILE__ << __LINE__;
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
            isDragging=false;
            RefreshToolTip();

            emit WPValuesChanged(this);
        }
        QGraphicsItem::mouseReleaseEvent(event);
    }
    void WayPointItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
    {

        if(isDragging)
        {
            coord=map->FromLocalToLatLng(this->pos().x(),this->pos().y());
            QString coord_str = " " + QString::number(coord.Lat(), 'f', 6) + "   " + QString::number(coord.Lng(), 'f', 6);
            text->setText(coord_str);
LM's avatar
LM committed
140
            // qDebug() << "WP DRAG:" << coord_str << __FILE__ << __LINE__;
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
            textBG->setRect(text->boundingRect());

            emit WPValuesChanged(this);
        }
            QGraphicsItem::mouseMoveEvent(event);
    }
    void WayPointItem::SetAltitude(const double &value)
    {
        altitude=value;
        RefreshToolTip();

        emit WPValuesChanged(this);
        this->update();
    }
    void WayPointItem::SetHeading(const float &value)
    {
        heading=value;
        RefreshToolTip();

        emit WPValuesChanged(this);
        this->update();
    }
    void WayPointItem::SetCoord(const internals::PointLatLng &value)
    {
        coord=value;
        emit WPValuesChanged(this);
        RefreshPos();
        RefreshToolTip();
        this->update();
    }
    void WayPointItem::SetDescription(const QString &value)
    {
        description=value;
        RefreshToolTip();
        emit WPValuesChanged(this);
        this->update();
    }
    void WayPointItem::SetNumber(const int &value)
    {
        emit WPNumberChanged(number,value,this);
        number=value;
        RefreshToolTip();
        numberI->setText(QString::number(number));
        numberIBG->setRect(numberI->boundingRect().adjusted(-2,0,1,0));
        this->update();
    }
    void WayPointItem::SetReached(const bool &value)
    {
lm's avatar
lm committed
189 190 191 192 193 194 195 196 197 198
        if (autoreachedEnabled)
        {
            reached=value;
            emit WPValuesChanged(this);
            if(value)
                picture.load(QString::fromUtf8(":/markers/images/bigMarkerGreen.png"));
            else
                picture.load(QString::fromUtf8(":/markers/images/marker.png"));
            this->update();
        }
199 200 201
    }
    void WayPointItem::SetShowNumber(const bool &value)
    {
lm's avatar
lm committed
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
//        shownumber=value;
//        if((numberI==0) && value)
//        {
//            numberI=new QGraphicsSimpleTextItem(this);
//            numberIBG=new QGraphicsRectItem(this);
//            numberIBG->setBrush(Qt::white);
//            numberIBG->setOpacity(0.5);
//            numberI->setZValue(3);
//            numberI->setPen(QPen(Qt::blue));
//            numberI->setPos(0,-13-picture.height());
//            numberIBG->setPos(0,-13-picture.height());
//            numberI->setText(QString::number(number));
//            numberIBG->setRect(numberI->boundingRect().adjusted(-2,0,1,0));
//        }
//        else if (!value && numberI)
//        {
//            delete numberI;
//            delete numberIBG;
//        }
//        this->update();



225 226 227 228 229
        shownumber=value;
        if((numberI==0) && value)
        {
            numberI=new QGraphicsSimpleTextItem(this);
            numberIBG=new QGraphicsRectItem(this);
lm's avatar
lm committed
230
            numberIBG->setBrush(Qt::black);
231 232
            numberIBG->setOpacity(0.5);
            numberI->setZValue(3);
lm's avatar
lm committed
233 234 235
            numberI->setPen(QPen(Qt::white));
            numberI->setPos(18,-picture.height()/2-2);
            numberIBG->setPos(18,-picture.height()/2-2);
236 237 238 239 240 241 242 243 244
            numberI->setText(QString::number(number));
            numberIBG->setRect(numberI->boundingRect().adjusted(-2,0,1,0));
        }
        else if (!value && numberI)
        {
            delete numberI;
            delete numberIBG;
        }
        this->update();
lm's avatar
lm committed
245 246 247



248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
    }
    void WayPointItem::WPDeleted(const int &onumber)
    {
        if(number>onumber) --number;
        numberI->setText(QString::number(number));
        numberIBG->setRect(numberI->boundingRect().adjusted(-2,0,1,0));
        RefreshToolTip();
        this->update();
    }
    void WayPointItem::WPInserted(const int &onumber, WayPointItem *waypoint)
    {
        if(waypoint!=this)
        {
            if(onumber<=number) ++number;
            numberI->setText(QString::number(number));
            RefreshToolTip();
            this->update();
        }
    }
    void WayPointItem::WPRenumbered(const int &oldnumber, const int &newnumber, WayPointItem *waypoint)
    {
        if (waypoint!=this)
        {
            if(((oldnumber>number) && (newnumber<=number)))
            {
                ++number;
                numberI->setText(QString::number(number));
                numberIBG->setRect(numberI->boundingRect().adjusted(-2,0,1,0));
                RefreshToolTip();
            }
            else if (((oldnumber<number) && (newnumber>number)))
            {
                --number;
                numberI->setText(QString::number(number));
                numberIBG->setRect(numberI->boundingRect().adjusted(-2,0,1,0));
                RefreshToolTip();
            }
            else if (newnumber==number)
            {
                ++number;
                numberI->setText(QString::number(number));
                RefreshToolTip();
            }
            this->update();
        }
    }
    int WayPointItem::type() const
    {
        // Enable the use of qgraphicsitem_cast with this item.
        return Type;
    }

    WayPointItem::~WayPointItem()
    {
        --WayPointItem::snumber;
    }
    void WayPointItem::RefreshPos()
    {
        core::Point point=map->FromLatLngToLocal(coord);
        this->setPos(point.X(),point.Y());
    }
    void WayPointItem::RefreshToolTip()
    {
Lorenz Meier's avatar
Lorenz Meier committed
311
        QString coord_str = QString::number(coord.Lat(), 'f', 7) + "   " + QString::number(coord.Lng(), 'f', 7);
312 313 314 315 316
        setToolTip(QString("WayPoint Number: %1\nDescription: %2\nCoordinate: %4\nAltitude: %5 m (MSL)\nHeading: %6 deg").arg(QString::number(WayPointItem::number)).arg(description).arg(coord_str).arg(QString::number(altitude)).arg(QString::number(heading)));
    }

    int WayPointItem::snumber=0;
}