1
/***************************************************************************
2
* Copyright (C) 2007 by Kai Winter *
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. *
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. *
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"
24
#include <QCryptographicHash>
26
ImageManager* ImageManager::m_ImageManagerInstance = 0;
28
ImageManager::ImageManager(QObject* parent)
29
:QObject(parent), emptyPixmap(QPixmap(1,1)), net(new MapNetwork(this))
31
emptyPixmap.fill(Qt::transparent);
34
if (QPixmapCache::cacheLimit() <= 20000)
36
QPixmapCache::setCacheLimit(20000); // in kb
39
if (QPixmapCache::cacheLimit() <= 2048)
41
QPixmapCache::setCacheLimit(2048); // in kb
46
ImageManager::~ImageManager()
51
//QPixmap ImageManager::getImage(const QString& host, const QString& url)
52
QPixmap ImageManager::getImage(MapAdapter* anAdapter, int x, int y, int z)
54
// qDebug() << "ImageManager::getImage";
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());
66
/* QPixmap pm(anAdapter->getTileSize(), anAdapter->getTileSize());
68
QPixmap pm(emptyPixmap);
70
// is image in picture cache
71
if (QPixmapCache::find(hash, pm))
75
if (useDiskCache(hash + ".png")) {
76
if (pm.load(cacheDir.absolutePath() + "/" + hash + ".png")) {
77
QPixmapCache::insert(hash, pm);
82
if (M_PREFS->getOfflineMode())
86
if (!net->imageIsLoading(hash))
88
// load from net, add empty image
89
net->loadImage(hash, host, url);
90
emit(imageRequested());
96
//QPixmap ImageManager::prefetchImage(const QString& host, const QString& url)
97
QPixmap ImageManager::prefetchImage(MapAdapter* anAdapter, int x, int y, int z)
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());
104
prefetch.append(hash);
105
return getImage(anAdapter, x, y, z);
108
void ImageManager::receivedImage(const QPixmap& pixmap, const QString& hash)
110
// qDebug() << "ImageManager::receivedImage";
111
if (pixmap.isNull()) {
112
QPixmap pm(256, 256);
114
QPixmapCache::insert(hash, pm);
116
QPixmapCache::insert(hash, pixmap);
118
pixmap.save(cacheDir.absolutePath() + "/" + hash + ".png");
119
QFileInfo info(cacheDir.absolutePath() + "/" + hash + ".png");
120
cacheInfo.append(info);
121
cacheSize += info.size();
127
if (prefetch.contains(hash))
129
prefetch.remove(prefetch.indexOf(hash));
131
emit(imageReceived());
134
void ImageManager::loadingQueueEmpty()
136
emit(loadingFinished());
137
// ((Layer*)this->parent())->removeZoomImage();
138
// qDebug() << "size of image-map: " << images.size();
139
// qDebug() << "size: " << QPixmapCache::cacheLimit();
142
void ImageManager::abortLoading()
147
void ImageManager::setProxy(QString host, int port)
149
net->setProxy(host, port);