~ubuntu-branches/ubuntu/saucy/merkaartor/saucy

« back to all changes in this revision

Viewing changes to QMapControl/imagemanager.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Bernd Zeimetz
  • Date: 2009-09-13 00:52:12 UTC
  • mto: (1.2.7 upstream) (0.1.3 upstream) (3.1.7 sid)
  • mto: This revision was merged to the branch mainline in revision 10.
  • Revision ID: james.westby@ubuntu.com-20090913005212-pjecal8zxm07x0fj
ImportĀ upstreamĀ versionĀ 0.14+svnfixes~20090912

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
 *   Copyright (C) 2007 by Kai Winter   *
3
 
 *   kaiwinter@gmx.de   *
4
 
 *                                                                         *
5
 
 *   This program is free software; you can redistribute it and/or modify  *
6
 
 *   it under the terms of the GNU General Public License as published by  *
7
 
 *   the Free Software Foundation; either version 2 of the License, or     *
8
 
 *   (at your option) any later version.                                   *
9
 
 *                                                                         *
10
 
 *   This program is distributed in the hope that it will be useful,       *
11
 
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12
 
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13
 
 *   GNU General Public License for more details.                          *
14
 
 *                                                                         *
15
 
 *   You should have received a copy of the GNU General Public License     *
16
 
 *   along with this program; if not, write to the                         *
17
 
 *   Free Software Foundation, Inc.,                                       *
18
 
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19
 
 ***************************************************************************/
20
 
#include "imagemanager.h"
21
 
#include "Preferences/MerkaartorPreferences.h"
22
 
 
23
 
#include <QDateTime>
24
 
#include <QCryptographicHash>
25
 
 
26
 
ImageManager* ImageManager::m_ImageManagerInstance = 0;
27
 
 
28
 
ImageManager::ImageManager(QObject* parent)
29
 
        :QObject(parent), emptyPixmap(QPixmap(1,1)), net(new MapNetwork(this))
30
 
{
31
 
        emptyPixmap.fill(Qt::transparent);
32
 
 
33
 
#ifndef _MOBILE
34
 
        if (QPixmapCache::cacheLimit() <= 20000)
35
 
        {
36
 
                QPixmapCache::setCacheLimit(20000);     // in kb
37
 
        }
38
 
#else
39
 
        if (QPixmapCache::cacheLimit() <= 2048)
40
 
        {
41
 
                QPixmapCache::setCacheLimit(2048);      // in kb
42
 
        }
43
 
#endif
44
 
}
45
 
 
46
 
ImageManager::~ImageManager()
47
 
{
48
 
        delete net;
49
 
}
50
 
 
51
 
//QPixmap ImageManager::getImage(const QString& host, const QString& url)
52
 
QPixmap ImageManager::getImage(MapAdapter* anAdapter, int x, int y, int z)
53
 
{
54
 
//      qDebug() << "ImageManager::getImage";
55
 
        
56
 
        QString host = anAdapter->getHost();
57
 
        QString url = anAdapter->getQuery(x, y, z);
58
 
        QString strHash = QString("%1%2").arg(anAdapter->getName()).arg(url);
59
 
        QString hash = QString(strHash.toAscii().toBase64());
60
 
        if (hash.size() > 255) {
61
 
                QCryptographicHash crypt(QCryptographicHash::Md5);
62
 
                crypt.addData(hash.toLatin1());
63
 
                hash = QString(crypt.result().toHex());
64
 
        }
65
 
 
66
 
/*      QPixmap pm(anAdapter->getTileSize(), anAdapter->getTileSize());
67
 
        pm.fill(Qt::black);*/
68
 
        QPixmap pm(emptyPixmap);
69
 
        
70
 
        // is image in picture cache
71
 
        if (QPixmapCache::find(hash, pm))
72
 
                return pm;
73
 
 
74
 
        // disk cache?
75
 
    if (useDiskCache(hash + ".png")) {
76
 
                if (pm.load(cacheDir.absolutePath() + "/" + hash + ".png")) {
77
 
                        QPixmapCache::insert(hash, pm);
78
 
                        return pm;
79
 
                }
80
 
        }
81
 
 
82
 
        if (M_PREFS->getOfflineMode())
83
 
                return pm;
84
 
        
85
 
        // currently loading?
86
 
        if (!net->imageIsLoading(hash))
87
 
        {
88
 
                // load from net, add empty image
89
 
                net->loadImage(hash, host, url);
90
 
                emit(imageRequested());
91
 
                return emptyPixmap;
92
 
        }
93
 
        return pm;
94
 
}
95
 
 
96
 
//QPixmap ImageManager::prefetchImage(const QString& host, const QString& url)
97
 
QPixmap ImageManager::prefetchImage(MapAdapter* anAdapter, int x, int y, int z)
98
 
{
99
 
        QString host = anAdapter->getHost();
100
 
        QString url = anAdapter->getQuery(x, y, z);
101
 
        QString strHash = QString("%1%2").arg(anAdapter->getName()).arg(url);
102
 
        QString hash = QString(strHash.toAscii().toBase64());
103
 
 
104
 
        prefetch.append(hash);
105
 
        return getImage(anAdapter, x, y, z);
106
 
}
107
 
 
108
 
void ImageManager::receivedImage(const QPixmap& pixmap, const QString& hash)
109
 
{
110
 
//      qDebug() << "ImageManager::receivedImage";
111
 
        if (pixmap.isNull()) {
112
 
                QPixmap pm(256, 256);
113
 
                pm.fill(Qt::gray);
114
 
                QPixmapCache::insert(hash, pm);
115
 
        } else {
116
 
                QPixmapCache::insert(hash, pixmap);
117
 
                if (cacheMaxSize) {
118
 
                        pixmap.save(cacheDir.absolutePath() + "/" + hash + ".png");
119
 
                        QFileInfo info(cacheDir.absolutePath() + "/" + hash + ".png");
120
 
                        cacheInfo.append(info);
121
 
                        cacheSize += info.size();
122
 
 
123
 
                        adaptCache();
124
 
                }
125
 
        }
126
 
 
127
 
        if (prefetch.contains(hash))
128
 
        {
129
 
                prefetch.remove(prefetch.indexOf(hash));
130
 
        }
131
 
        emit(imageReceived());
132
 
}
133
 
 
134
 
void ImageManager::loadingQueueEmpty()
135
 
{
136
 
        emit(loadingFinished());
137
 
//      ((Layer*)this->parent())->removeZoomImage();
138
 
//      qDebug() << "size of image-map: " << images.size();
139
 
//      qDebug() << "size: " << QPixmapCache::cacheLimit();
140
 
}
141
 
 
142
 
void ImageManager::abortLoading()
143
 
{
144
 
        net->abortLoading();
145
 
        loadingQueueEmpty();
146
 
}
147
 
void ImageManager::setProxy(QString host, int port)
148
 
{
149
 
        net->setProxy(host, port);
150
 
}
151