tilematrix.cpp 4.2 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 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
/**
******************************************************************************
*
* @file       tilematrix.cpp
* @author     The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
* @brief      
* @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 "tilematrix.h"

 
namespace internals {
TileMatrix::TileMatrix()
{
}
void TileMatrix::Clear()
{
    mutex.lock();
    foreach(Tile* t,matrix.values())
    {
        delete t;
        t=0;
    }
    matrix.clear();
    mutex.unlock();
}
//void TileMatrix::RebuildToUpperZoom()
//{
//    mutex.lock();
//    QList<Tile*> newtiles;
//    foreach(Tile* t,matrix.values())
//    {
//       Point point=Point(t->GetPos().X()*2,t->GetPos().Y()*2);
//       Tile* tile1=new Tile(t->GetZoom()+1,point);
//       Tile* tile2=new Tile(t->GetZoom()+1,Point(point.X()+1,point.Y()+0));
//       Tile* tile3=new Tile(t->GetZoom()+1,Point(point.X()+0,point.Y()+1));
//       Tile* tile4=new Tile(t->GetZoom()+1,Point(point.X()+1,point.Y()+1));
//
//       foreach(QByteArray arr, t->Overlays)
//        {
//           QImage ima=QImage::fromData(arr);
//           QImage ima1=ima.copy(0,0,ima.width()/2,ima.height()/2);
//           QImage ima2=ima.copy(ima.width()/2,0,ima.width()/2,ima.height()/2);
//           QImage ima3=ima.copy(0,ima.height()/2,ima.width()/2,ima.height()/2);
//           QImage ima4=ima.copy(ima.width()/2,ima.height()/2,ima.width()/2,ima.height()/2);
//           QByteArray ba;
//           QBuffer buf(&ba);
//           ima1.scaled(QSize(ima.width(),ima.height())).save(&buf,"PNG");
//           tile1->Overlays.append(ba);
//           QByteArray ba1;
//           QBuffer buf1(&ba1);
//           ima2.scaled(QSize(ima.width(),ima.height())).save(&buf1,"PNG");
//           tile2->Overlays.append(ba1);
//           QByteArray ba2;
//           QBuffer buf2(&ba2);
//           ima3.scaled(QSize(ima.width(),ima.height())).save(&buf2,"PNG");
//           tile3->Overlays.append(ba2);
//           QByteArray ba3;
//           QBuffer buf3(&ba3);
//           ima4.scaled(QSize(ima.width(),ima.height())).save(&buf3,"PNG");
//           tile4->Overlays.append(ba3);
//           newtiles.append(tile1);
//           newtiles.append(tile2);
//           newtiles.append(tile3);
//           newtiles.append(tile4);
//        }
//    }
//    foreach(Tile* t,matrix.values())
//    {
//        delete t;
//        t=0;
//    }
//    matrix.clear();
//    foreach(Tile* t,newtiles)
//    {
//        matrix.insert(t->GetPos(),t);
//    }
//
//    mutex.unlock();
//}

void TileMatrix::ClearPointsNotIn(QList<Point>list)
{
    removals.clear();
    mutex.lock();
    foreach(Point p, matrix.keys())
    {
        if(!list.contains(p))
        {
            removals.append(p);
        }
    }
    mutex.unlock();
    foreach(Point p,removals)
    {
        Tile* t=TileAt(p);
        if(t!=0)
        {
            mutex.lock();
            delete t;
            t=0;
            matrix.remove(p);
            mutex.unlock();
        }

    }
    removals.clear();
}
Tile* TileMatrix::TileAt(const Point &p)
{

#ifdef DEBUG_TILEMATRIX
    qDebug()<<"TileMatrix:TileAt:"<<p.ToString();
#endif //DEBUG_TILEMATRIX
    Tile* ret;
    mutex.lock();
    ret=matrix.value(p,0);
    mutex.unlock();
    return ret;
}
void TileMatrix::SetTileAt(const Point &p, Tile* tile)
{
    mutex.lock();
    Tile* t=matrix.value(p,0);
    if(t!=0)
        delete t;
    matrix.insert(p,tile);
    mutex.unlock();
}
}