linestring.cpp 4.37 KB
Newer Older
pixhawk's avatar
pixhawk committed
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 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 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 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
/*
*
* This file is part of QMapControl,
* an open-source cross-platform map widget
*
* Copyright (C) 2007 - 2008 Kai Winter
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with QMapControl. If not, see <http://www.gnu.org/licenses/>.
*
* Contact e-mail: kaiwinter@gmx.de
* Program URL   : http://qmapcontrol.sourceforge.net/
*
*/

#include "linestring.h"
namespace qmapcontrol
{
    LineString::LineString()
        : Curve()
    {
        GeometryType = "LineString";
    }

    LineString::LineString(QList<Point*> const points, QString name, QPen* pen)
        :Curve(name)
    {
        mypen = pen;
        LineString();
        setPoints(points);
    }

    LineString::~LineString()
    {
    }

    // Geometry LineString::Clone(){}

    // Point LineString::EndPoint(){}
    // Point LineString::StartPoint(){}
    // Point LineString::Value(){}


    void LineString::addPoint(Point* point)
    {
        vertices.append(point);
    }

    QList<Point*> LineString::points()
    {
        return vertices;
    }

    void LineString::setPoints(QList<Point*> points)
    {
        for (int i=0; i<points.size(); i++)
        {
            points.at(i)->setParentGeometry(this);
        }
        vertices = points;
    }

    void LineString::draw(QPainter* painter, const MapAdapter* mapadapter, const QRect &screensize, const QPoint offset)
    {
        if (!visible)
            return;

        QPolygon p = QPolygon();

        QPointF c;
        for (int i=0; i<vertices.size(); i++)
        {
            c = vertices[i]->coordinate();
            p.append(mapadapter->coordinateToDisplay(c));
        }
        if (mypen != 0)
        {
            painter->save();
            painter->setPen(*mypen);
        }
        painter->drawPolyline(p);
        if (mypen != 0)
        {
            painter->restore();
        }
        for (int i=0; i<vertices.size(); i++)
        {
            vertices[i]->draw(painter, mapadapter, screensize, offset);
        }
    }

    int LineString::numberOfPoints() const
    {
        return vertices.count();
    }
    bool LineString::Touches(Point* geom, const MapAdapter* mapadapter)
    {
        // qDebug() << "LineString::Touches Point";
        touchedPoints.clear();
        bool touches = false;
        for (int i=0; i<vertices.count(); i++)
        {
            // use implementation from Point
            if (vertices.at(i)->Touches(geom, mapadapter))
            {
                touchedPoints.append(vertices.at(i));

                touches = true;
            }
        }
        if (touches)
        {
            emit(geometryClicked(this, QPoint(0,0)));
        }
        return touches;
    }
    bool LineString::Touches(Geometry* /*geom*/, const MapAdapter* /*mapadapter*/)
    {
        // qDebug() << "LineString::Touches Geom";
        touchedPoints.clear();

        return false;
    }

    QList<Geometry*> LineString::clickedPoints()
    {
        return touchedPoints;
    }
    bool LineString::hasPoints() const
    {
        return vertices.size() > 0 ? true : false;
    }
    bool LineString::hasClickedPoints() const
    {
        return touchedPoints.size() > 0 ? true : false;
    }

    QRectF LineString::boundingBox()
    {
        qreal minlon=180;
        qreal maxlon=-180;
        qreal minlat=90;
        qreal maxlat=-90;
        for (int i=0; i<vertices.size(); i++)
        {
            Point* tmp = vertices.at(i);
            if (tmp->longitude() < minlon) minlon = tmp->longitude();
            if (tmp->longitude() > maxlon) maxlon = tmp->longitude();
            if (tmp->latitude() < minlat) minlat = tmp->latitude();
            if (tmp->latitude() > maxlat) maxlat = tmp->latitude();
        }
        QPointF min = QPointF(minlon, minlat);
        QPointF max = QPointF(maxlon, maxlat);
        QPointF dist = max - min;
        QSizeF si = QSizeF(dist.x(), dist.y());

        return QRectF(min, si);

    }
}