32 #include <boost/make_shared.hpp> 34 #include <QtAlgorithms> 37 #include <QNetworkAccessManager> 38 #include <QNetworkDiskCache> 47 return left->Priority() > right->Priority();
52 Image::Image(
const QString& uri,
size_t uri_hash, uint64_t priority) :
68 image_ = boost::make_shared<QImage>();
85 network_manager_(this),
86 cache_dir_(cache_dir),
91 network_request_semaphore_(MAXIMUM_NETWORK_REQUESTS)
93 QNetworkDiskCache* disk_cache =
new QNetworkDiskCache(
this);
128 if (
failed_.contains(uri_hash))
138 image_ptr =
new ImagePtr(boost::make_shared<Image>(uri, uri_hash));
140 if (!
cache_.insert(uri_hash, image_ptr))
142 ROS_ERROR(
"FAILED TO CREATE HANDLE: %s", uri.toStdString().c_str());
151 cache_.insert(uri_hash, image_ptr);
157 if (image && !image->GetImage())
159 if (!image->Failed())
167 image->SetPriority(priority +
tick_++);
178 image->SetPriority(priority +
tick_++);
194 QNetworkRequest request;
195 request.setUrl(QUrl(uri));
196 request.setRawHeader(
"User-Agent",
"mapviz-1.0");
197 request.setAttribute(
198 QNetworkRequest::CacheLoadControlAttribute,
199 QNetworkRequest::PreferCache);
200 request.setAttribute(
201 QNetworkRequest::HttpPipeliningAllowedAttribute,
205 connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
211 QString url = reply->url().toString();
220 if (reply->error() == QNetworkReply::NoError)
222 QByteArray
data = reply->readAll();
223 image->InitializeImage();
224 if (!image->GetImage()->loadFromData(data))
240 image->SetLoading(
false);
246 reply->deleteLater();
297 if (!image->Loading() && !image->Failed())
300 image->SetLoading(
true);
303 QString uri = image->Uri();
305 if (uri.startsWith(QString(
"file:///")))
307 image->InitializeImage();
308 QString filepath = uri.replace(QString(
"file:///"), QString(
"/"));
309 if (!image->GetImage()->load(filepath))
317 image->SetLoading(
false);
QMap< size_t, ImagePtr > unprocessed_
static const int MAXIMUM_FAILURES
void NetworkError(QNetworkReply::NetworkError error)
void ProcessReply(QNetworkReply *reply)
boost::shared_ptr< QImage > image_
bool ComparePriority(const ImagePtr left, const ImagePtr right)
QNetworkAccessManager network_manager_
QSemaphore network_request_semaphore_
QCache< size_t, ImagePtr > cache_
Image(const QString &uri, size_t uri_hash, uint64_t priority=0)
ImageCache(const QString &cache_dir, size_t size=4096)
boost::shared_ptr< Image > ImagePtr
ImagePtr GetImage(size_t uri_hash, const QString &uri, int32_t priority=0)
static const int MAXIMUM_NETWORK_REQUESTS
CacheThread(ImageCache *parent)
QMutex unprocessed_mutex_
void RequestImage(QString)
QMap< QString, size_t > uri_to_hash_map_
void ProcessRequest(QString uri)
static const int MAXIMUM_SEQUENTIAL_REQUESTS
CacheThread * cache_thread_