Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
337 | KeyOz | 1 | /* |
2 | * |
||
3 | * This file is part of QMapControl, |
||
4 | * an open-source cross-platform map widget |
||
5 | * |
||
6 | * Copyright (C) 2007 - 2008 Kai Winter |
||
7 | * |
||
8 | * This program is free software: you can redistribute it and/or modify |
||
9 | * it under the terms of the GNU Lesser General Public License as published by |
||
10 | * the Free Software Foundation, either version 3 of the License, or |
||
11 | * (at your option) any later version. |
||
12 | * |
||
13 | * This program is distributed in the hope that it will be useful, |
||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
16 | * GNU Lesser General Public License for more details. |
||
17 | * |
||
18 | * You should have received a copy of the GNU Lesser General Public License |
||
19 | * along with QMapControl. If not, see <http://www.gnu.org/licenses/>. |
||
20 | * |
||
21 | * Contact e-mail: kaiwinter@gmx.de |
||
22 | * Program URL : http://qmapcontrol.sourceforge.net/ |
||
23 | * |
||
24 | */ |
||
25 | |||
26 | #include "imagemanager.h" |
||
27 | namespace qmapcontrol |
||
28 | { |
||
29 | ImageManager* ImageManager::m_Instance = 0; |
||
30 | ImageManager::ImageManager(QObject* parent) |
||
31 | :QObject(parent), emptyPixmap(QPixmap(1,1)), net(new MapNetwork(this)), doPersistentCaching(false) |
||
32 | { |
||
33 | emptyPixmap.fill(Qt::transparent); |
||
34 | |||
35 | if (QPixmapCache::cacheLimit() <= 20000) |
||
36 | { |
||
37 | QPixmapCache::setCacheLimit(20000); // in kb |
||
38 | } |
||
39 | } |
||
40 | |||
41 | |||
42 | ImageManager::~ImageManager() |
||
43 | { |
||
44 | delete net; |
||
45 | } |
||
46 | |||
47 | QPixmap ImageManager::getImage(const QString& host, const QString& url) |
||
48 | { |
||
49 | //qDebug() << "ImageManager::getImage"; |
||
50 | QPixmap pm; |
||
51 | //pm.fill(Qt::black); |
||
52 | |||
53 | //is image cached (memory) or currently loading? |
||
54 | if (!QPixmapCache::find(url, pm) && !net->imageIsLoading(url)) |
||
55 | // if (!images.contains(url) && !net->imageIsLoading(url)) |
||
56 | { |
||
57 | //image cached (persistent)? |
||
58 | if (doPersistentCaching && tileExist(url)) |
||
59 | { |
||
60 | loadTile(url,pm); |
||
61 | QPixmapCache::insert(url.toAscii().toBase64(), pm); |
||
62 | } |
||
63 | else |
||
64 | { |
||
65 | //load from net, add empty image |
||
66 | net->loadImage(host, url); |
||
67 | //QPixmapCache::insert(url, emptyPixmap); |
||
68 | return emptyPixmap; |
||
69 | } |
||
70 | } |
||
71 | return pm; |
||
72 | } |
||
73 | |||
74 | QPixmap ImageManager::prefetchImage(const QString& host, const QString& url) |
||
75 | { |
||
76 | #ifdef Q_WS_QWS |
||
77 | // on mobile devices we don´t want the display resfreshing when tiles are received which are |
||
78 | // prefetched... This is a performance issue, because mobile devices are very slow in |
||
79 | // repainting the screen |
||
80 | prefetch.append(url); |
||
81 | #endif |
||
82 | return getImage(host, url); |
||
83 | } |
||
84 | |||
85 | void ImageManager::receivedImage(const QPixmap pixmap, const QString& url) |
||
86 | { |
||
87 | //qDebug() << "ImageManager::receivedImage"; |
||
88 | QPixmapCache::insert(url, pixmap); |
||
89 | //images[url] = pixmap; |
||
90 | |||
91 | // needed? |
||
92 | if (doPersistentCaching && !tileExist(url) ) |
||
93 | saveTile(url,pixmap); |
||
94 | |||
95 | //((Layer*)this->parent())->imageReceived(); |
||
96 | |||
97 | if (!prefetch.contains(url)) |
||
98 | { |
||
99 | emit(imageReceived()); |
||
100 | } |
||
101 | else |
||
102 | { |
||
103 | |||
104 | #ifdef Q_WS_QWS |
||
105 | prefetch.remove(prefetch.indexOf(url)); |
||
106 | #endif |
||
107 | } |
||
108 | } |
||
109 | |||
110 | void ImageManager::loadingQueueEmpty() |
||
111 | { |
||
112 | emit(loadingFinished()); |
||
113 | //((Layer*)this->parent())->removeZoomImage(); |
||
114 | //qDebug() << "size of image-map: " << images.size(); |
||
115 | //qDebug() << "size: " << QPixmapCache::cacheLimit(); |
||
116 | } |
||
117 | |||
118 | void ImageManager::abortLoading() |
||
119 | { |
||
120 | net->abortLoading(); |
||
121 | } |
||
122 | void ImageManager::setProxy(QString host, int port) |
||
123 | { |
||
124 | net->setProxy(host, port); |
||
125 | } |
||
126 | |||
127 | void ImageManager::setCacheDir(const QDir& path) |
||
128 | { |
||
129 | doPersistentCaching = true; |
||
130 | cacheDir = path; |
||
131 | if (!cacheDir.exists()) |
||
132 | { |
||
133 | cacheDir.mkpath(cacheDir.absolutePath()); |
||
134 | } |
||
135 | } |
||
136 | |||
137 | bool ImageManager::saveTile(QString tileName,QPixmap tileData) |
||
138 | { |
||
139 | tileName.replace("/","-"); |
||
140 | |||
141 | QFile file(cacheDir.absolutePath() + "/" + tileName.toAscii().toBase64()); |
||
142 | |||
143 | //qDebug() << "writing: " << file.fileName(); |
||
144 | if (!file.open(QIODevice::ReadWrite )){ |
||
145 | qDebug()<<"error reading file"; |
||
146 | return false; |
||
147 | } |
||
148 | QByteArray bytes; |
||
149 | QBuffer buffer(&bytes); |
||
150 | buffer.open(QIODevice::WriteOnly); |
||
151 | tileData.save(&buffer, "PNG"); |
||
152 | |||
153 | file.write(bytes); |
||
154 | file.close(); |
||
155 | return true; |
||
156 | } |
||
157 | bool ImageManager::loadTile(QString tileName,QPixmap &tileData) |
||
158 | { |
||
159 | tileName.replace("/","-"); |
||
160 | QFile file(cacheDir.absolutePath() + "/" + tileName.toAscii().toBase64()); |
||
161 | if (!file.open(QIODevice::ReadOnly )) { |
||
162 | return false; |
||
163 | } |
||
164 | tileData.loadFromData( file.readAll() ); |
||
165 | |||
166 | file.close(); |
||
167 | return true; |
||
168 | } |
||
169 | bool ImageManager::tileExist(QString tileName) |
||
170 | { |
||
171 | tileName.replace("/","-"); |
||
172 | QFile file(cacheDir.absolutePath() + "/" + tileName.toAscii().toBase64()); |
||
173 | if (file.exists()) |
||
174 | return true; |
||
175 | else |
||
176 | return false; |
||
177 | } |
||
178 | } |