layer.cpp 12.1 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
/*
*
* 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 "layer.h"
namespace qmapcontrol
{
    Layer::Layer(QString layername, MapAdapter* mapadapter, enum LayerType layertype, bool takeevents)
        :visible(true), mylayername(layername), mylayertype(layertype), mapAdapter(mapadapter), takeevents(takeevents), myoffscreenViewport(QRect(0,0,0,0))
    {
32
       draggingGeometry = false;
pixhawk's avatar
pixhawk committed
33 34 35 36 37 38 39 40 41
        //qDebug() << "creating new Layer: " << layername << ", type: " << contents;
        //qDebug() << this->layertype;
    }

    Layer::~Layer()
    {
        delete mapAdapter;
    }

42
    void Layer::setSize(QSize size, QPoint screenmiddle)
pixhawk's avatar
pixhawk committed
43 44
    {
        this->size = size;
45
        this->screenmiddle = screenmiddle;// QPoint(size.width()/2, size.height()/2);
pixhawk's avatar
pixhawk committed
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
        //QMatrix mat;
        //mat.translate(480/2, 640/2);
        //mat.rotate(45);
        //mat.translate(-480/2,-640/2);
        //screenmiddle = mat.map(screenmiddle);
    }

    QString Layer::layername() const
    {
        return mylayername;
    }

    const MapAdapter* Layer::mapadapter() const
    {
        return mapAdapter;
    }

    void Layer::setVisible(bool visible)
    {
        this->visible = visible;
        emit(updateRequest());
    }

    void Layer::addGeometry(Geometry* geom)
    {
        //qDebug() << geom->getName() << ", " << geom->getPoints().at(0)->getWidget();

        geometries.append(geom);
        emit(updateRequest(geom->boundingBox()));
        //a geometry can request a redraw, e.g. when its position has been changed
        connect(geom, SIGNAL(updateRequest(QRectF)),
                this, SIGNAL(updateRequest(QRectF)));
    }
    void Layer::removeGeometry(Geometry* geometry)
    {
        for (int i=0; i<geometries.count(); i++)
        {
            if (geometry == geometries.at(i))
            {
                disconnect(geometry);
                geometries.removeAt(i);
                //delete geometry;
            }
        }
    }

    void Layer::clearGeometries()
    {
        foreach(Geometry *geometry, geometries){
            disconnect(geometry);
        }
        geometries.clear();
    }

100 101
    Geometry* Layer::get_Geometry(int index)
    {
102
        Geometry* geo = NULL;
103 104
        if(geometrySelected)
        {
105
            geo = geometrySelected;
106 107 108 109 110 111 112 113
        }
        else
        {
            for(int i = 0; i <= geometries.size(); i++)
            {
                Geometry *geometry = geometries[i];
                if(geometry->name() == QString::number(index))
                {
114
                  geo = geometry;
115 116 117 118 119 120 121 122 123 124 125 126 127
                }
            }

//           foreach(Geometry *geometry, geometries)
//           {

//               if(geometry->name() == QString::number(index))
//               {
//                   return geometry;
//               }

//           }
        }
128
        return geo;
129 130
    }

pixhawk's avatar
pixhawk committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    bool Layer::isVisible() const
    {
        return visible;
    }
    void Layer::zoomIn() const
    {
        mapAdapter->zoom_in();
    }
    void Layer::zoomOut() const
    {
        mapAdapter->zoom_out();
    }

    void Layer::mouseEvent(const QMouseEvent* evnt, const QPoint mapmiddle_px)
        {
146
            if (takesMouseEvents())
pixhawk's avatar
pixhawk committed
147
            {
148
                if (evnt->button() == Qt::LeftButton && evnt->type() == QEvent::MouseButtonPress)
pixhawk's avatar
pixhawk committed
149
                {
150 151 152 153 154
                    // check for collision
                    QPointF c = mapAdapter->displayToCoordinate(QPoint(evnt->x()-screenmiddle.x()+mapmiddle_px.x(),
                                                                       evnt->y()-screenmiddle.y()+mapmiddle_px.y()));
                    Point* tmppoint = new Point(c.x(), c.y());
                    for (int i=0; i<geometries.count(); i++)
pixhawk's avatar
pixhawk committed
155
                    {
156 157 158 159 160 161 162 163 164
                        if (geometries.at(i)->isVisible() && geometries.at(i)->Touches(tmppoint, mapAdapter))

                            //if (geometries.at(i)->Touches(c, mapAdapter))
                        {

                            emit(geometryClicked(geometries.at(i), QPoint(evnt->x(), evnt->y())));
                            draggingGeometry = true;
                            geometrySelected = geometries.at(i);
                        }
pixhawk's avatar
pixhawk committed
165
                    }
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
                    delete tmppoint;
                }

                if (evnt->type() == QEvent::MouseButtonRelease){
                  QPointF c = mapAdapter->displayToCoordinate(QPoint(evnt->x()-screenmiddle.x()+mapmiddle_px.x(),
                                                                   evnt->y()-screenmiddle.y()+mapmiddle_px.y()));
                    draggingGeometry = false;
                    emit (geometryEndDrag(geometrySelected, c));
                    geometrySelected = 0;
                }

                if ( evnt->type() == QEvent::MouseMove && draggingGeometry){
                    QPointF c = mapAdapter->displayToCoordinate(QPoint(evnt->x()-screenmiddle.x()+mapmiddle_px.x(),
                                                                     evnt->y()-screenmiddle.y()+mapmiddle_px.y()));
                    emit(geometryDragged(geometrySelected, c));
pixhawk's avatar
pixhawk committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 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
                }
            }
        }

    bool Layer::takesMouseEvents() const
    {
        return takeevents;
    }

    void Layer::drawYourImage(QPainter* painter, const QPoint mapmiddle_px) const
    {
        if (mylayertype == MapLayer)
        {
            //qDebug() << ":: " << mapmiddle_px;
            //QMatrix mat;
            //mat.translate(480/2, 640/2);
            //mat.rotate(45);
            //mat.translate(-480/2,-640/2);

            //mapmiddle_px = mat.map(mapmiddle_px);
            //qDebug() << ":: " << mapmiddle_px;
            _draw(painter, mapmiddle_px);
        }

        drawYourGeometries(painter, QPoint(mapmiddle_px.x()-screenmiddle.x(), mapmiddle_px.y()-screenmiddle.y()), myoffscreenViewport);
    }
    void Layer::drawYourGeometries(QPainter* painter, const QPoint mapmiddle_px, QRect viewport) const
    {
        QPoint offset;
        if (mylayertype == MapLayer)
            offset = mapmiddle_px;
        else
            offset = mapmiddle_px-screenmiddle;

        painter->translate(-mapmiddle_px+screenmiddle);
        for (int i=0; i<geometries.count(); i++)
        {
            geometries.at(i)->draw(painter, mapAdapter, viewport, offset);
        }
        painter->translate(mapmiddle_px-screenmiddle);

    }
    void Layer::_draw(QPainter* painter, const QPoint mapmiddle_px) const
    {
        // screen middle rotieren...

        int tilesize = mapAdapter->tilesize();
        int cross_x = int(mapmiddle_px.x())%tilesize; // position on middle tile
        int cross_y = int(mapmiddle_px.y())%tilesize;
        //qDebug() << screenmiddle << " - " << cross_x << ", " << cross_y;

        // calculate how many surrounding tiles have to be drawn to fill the display
        int space_left = screenmiddle.x() - cross_x;
        int tiles_left = space_left/tilesize;
        if (space_left>0)
            tiles_left+=1;

        int space_above = screenmiddle.y() - cross_y;
        int tiles_above = space_above/tilesize;
        if (space_above>0)
            tiles_above+=1;

        int space_right = screenmiddle.x() - (tilesize-cross_x);
        int tiles_right = space_right/tilesize;
        if (space_right>0)
            tiles_right+=1;

        int space_bottom = screenmiddle.y() - (tilesize-cross_y);
        int tiles_bottom = space_bottom/tilesize;
        if (space_bottom>0)
            tiles_bottom+=1;

        //int tiles_displayed = 0;
        int mapmiddle_tile_x = mapmiddle_px.x()/tilesize;
        int mapmiddle_tile_y = mapmiddle_px.y()/tilesize;

	const QPoint from = QPoint((-tiles_left+mapmiddle_tile_x)*tilesize, (-tiles_above+mapmiddle_tile_y)*tilesize);
	const QPoint to = QPoint((tiles_right+mapmiddle_tile_x+1)*tilesize, (tiles_bottom+mapmiddle_tile_y+1)*tilesize);

        myoffscreenViewport = QRect(from, to);

	// for the EmptyMapAdapter no tiles should be loaded and painted.
	if (mapAdapter->host() == "")
	{
	    return;
	}

        if (mapAdapter->isValid(mapmiddle_tile_x, mapmiddle_tile_y, mapAdapter->currentZoom()))
        {
            painter->drawPixmap(-cross_x+size.width(),
                                -cross_y+size.height(),
                                ImageManager::instance()->getImage(mapAdapter->host(), mapAdapter->query(mapmiddle_tile_x, mapmiddle_tile_y, mapAdapter->currentZoom())));
        }

        for (int i=-tiles_left+mapmiddle_tile_x; i<=tiles_right+mapmiddle_tile_x; i++)
        {
            for (int j=-tiles_above+mapmiddle_tile_y; j<=tiles_bottom+mapmiddle_tile_y; j++)
            {
                // check if image is valid
                if (!(i==mapmiddle_tile_x && j==mapmiddle_tile_y))
                    if (mapAdapter->isValid(i, j, mapAdapter->currentZoom()))
                    {

                    painter->drawPixmap(((i-mapmiddle_tile_x)*tilesize)-cross_x+size.width(),
                                        ((j-mapmiddle_tile_y)*tilesize)-cross_y+size.height(),
                                        ImageManager::instance()->getImage(mapAdapter->host(), mapAdapter->query(i, j, mapAdapter->currentZoom())));
                    //if (QCoreApplication::hasPendingEvents())
                    //  QCoreApplication::processEvents();
                }
            }
        }


        // PREFETCHING
295 296 297 298 299 300

        // FIXME Make prefetching a parameter
        int upper = mapmiddle_tile_y-tiles_above-2;
        int right = mapmiddle_tile_x+tiles_right+2;
        int left = mapmiddle_tile_x-tiles_right-2;
        int lower = mapmiddle_tile_y+tiles_bottom+2;
pixhawk's avatar
pixhawk committed
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358

        int j = upper;
        for (int i=left; i<=right; i++)
        {
            if (mapAdapter->isValid(i, j, mapAdapter->currentZoom()))
                ImageManager::instance()->prefetchImage(mapAdapter->host(), mapAdapter->query(i, j, mapAdapter->currentZoom()));
        }
        j = lower;
        for (int i=left; i<=right; i++)
        {
            if (mapAdapter->isValid(i, j, mapAdapter->currentZoom()))
                ImageManager::instance()->prefetchImage(mapAdapter->host(), mapAdapter->query(i, j, mapAdapter->currentZoom()));
        }
        int i = left;
        for (int j=upper+1; j<=lower-1; j++)
        {
            if (mapAdapter->isValid(i, j, mapAdapter->currentZoom()))
                ImageManager::instance()->prefetchImage(mapAdapter->host(), mapAdapter->query(i, j, mapAdapter->currentZoom()));
        }
        i = right;
        for (int j=upper+1; j<=lower-1; j++)
        {
            if (mapAdapter->isValid(i, j, mapAdapter->currentZoom()))
                ImageManager::instance()->prefetchImage(mapAdapter->host(), mapAdapter->query(i, j, mapAdapter->currentZoom()));
        }
    }

    QRect Layer::offscreenViewport() const
    {
        return myoffscreenViewport;
    }

    void Layer::moveWidgets(const QPoint mapmiddle_px) const
    {
        for (int i=0; i<geometries.count(); i++)
        {
            const Geometry* geom = geometries.at(i);
            if (geom->GeometryType == "Point")
            {
                if (((Point*)geom)->widget()!=0)
                {
                    QPoint topleft_relative = QPoint(mapmiddle_px-screenmiddle);
                    ((Point*)geom)->drawWidget(mapAdapter, topleft_relative);
                }
            }
        }
    }
    Layer::LayerType Layer::layertype() const
    {
        return mylayertype;
    }

    void Layer::setMapAdapter(MapAdapter* mapadapter)
    {

        mapAdapter = mapadapter;
    }
}
359 360