mapripper.cpp 4.56 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
/**
******************************************************************************
*
* @file       mapripper.cpp
* @author     The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
* @brief      A class that allows ripping of a selection of the map
* @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 "mapripper.h"
28 29 30

#include <qtimer.h>

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
namespace mapcontrol
{

    MapRipper::MapRipper(internals::Core * core, const internals::RectLatLng & rect):sleep(100),cancel(false),progressForm(0),core(core)
    {
        if(!rect.IsEmpty())
        {
            type=core->GetMapType();
            progressForm=new MapRipForm;
            area=rect;
            zoom=core->Zoom();
            maxzoom=core->MaxZoom();
            points=core->Projection()->GetAreaTileList(area,zoom,0);
            this->start();
            progressForm->show();
            connect(this,SIGNAL(percentageChanged(int)),progressForm,SLOT(SetPercentage(int)));
            connect(this,SIGNAL(numberOfTilesChanged(int,int)),progressForm,SLOT(SetNumberOfTiles(int,int)));
            connect(this,SIGNAL(providerChanged(QString,int)),progressForm,SLOT(SetProvider(QString,int)));
            connect(this,SIGNAL(finished()),this,SLOT(finish()));
            emit numberOfTilesChanged(0,0);
        }
    }
    void MapRipper::finish()
    {
55
        if(zoom<maxzoom)
56
        {
57 58 59 60 61 62 63 64 65
            ++zoom;
            QMessageBox msgBox;
            msgBox.setText(tr("Continue Ripping at zoom level %1? (Continuing automatically after 3s)").arg(zoom));
            // msgBox.setInformativeText("Do you want to save your changes?");
            msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
            msgBox.setDefaultButton(QMessageBox::Yes);
            QTimer::singleShot(3000, &msgBox, SLOT(accept()));
            msgBox.exec();
            int ret  = msgBox.result();
66

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
            if(ret==QMessageBox::Yes || ret == 1)
            {
                points.clear();
                points=core->Projection()->GetAreaTileList(area,zoom,0);
                this->start();
            }
            else
            {
                progressForm->close();
                delete progressForm;
                this->deleteLater();
            }
        }
        else
        {
            progressForm->close();
            delete progressForm;
            this->deleteLater();
        }
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
    }


    void MapRipper::run()
    {
        int countOk = 0;
        bool goodtile=false;
        //  Stuff.Shuffle<Point>(ref list);
        QVector<core::MapType::Types> types = OPMaps::Instance()->GetAllLayersOfType(type);
        int all=points.count();
        for(int i = 0; i < all; i++)
        {
            emit numberOfTilesChanged(all,i+1);
            if(cancel)
                break;

            core::Point p = points[i];
            {
                //qDebug()<<"offline fetching:"<<p.ToString();
                foreach(core::MapType::Types type,types)
                {
                    emit providerChanged(core::MapType::StrByType(type),zoom);
                    QByteArray img = OPMaps::Instance()->GetImageFrom(type, p, zoom);
                    if(img.length()!=0)
                    {
                        goodtile=true;
                        img=NULL;
                    }
                    else
                        goodtile=false;
                }
                if(goodtile)
                {
                    countOk++;
                }
                else
                {
                    i--;
                    QThread::msleep(1000);
                    continue;
                }
            }
            emit percentageChanged((int) ((i+1)*100/all));//, i+1);
            // worker.ReportProgress((int) ((i+1)*100/all), i+1);

            QThread::msleep(sleep);
        }
    }
}