Commit 9adfc8dc authored by Lionel Heng's avatar Lionel Heng

Disabled culling of imagery tiles.

parent ee74c2c3
...@@ -42,13 +42,13 @@ const double WGS84_ECCSQ = 0.00669437999013; ...@@ -42,13 +42,13 @@ const double WGS84_ECCSQ = 0.00669437999013;
const int MAX_ZOOM_LEVEL = 20; const int MAX_ZOOM_LEVEL = 20;
Imagery::Imagery() Imagery::Imagery()
: mTextureCache(new TextureCache(500)) : mTextureCache(new TextureCache(1000))
, mImageryType(Imagery::BLANK_MAP) , mImageryType(Imagery::BLANK_MAP)
, mXOffset(0.0) , mXOffset(0.0)
, mYOffset(0.0) , mYOffset(0.0)
, mZOffset(0.0) , mZOffset(0.0)
{ {
setCullingActive(false);
} }
Imagery::Type Imagery::Type
......
...@@ -2053,9 +2053,9 @@ Pixhawk3DWidget::updateImagery(double originX, double originY, ...@@ -2053,9 +2053,9 @@ Pixhawk3DWidget::updateImagery(double originX, double originY,
} }
double viewingRadius = m3DWidget->cameraManipulator()->getDistance() * 10.0; double viewingRadius = m3DWidget->cameraManipulator()->getDistance() * 10.0;
if (viewingRadius < 100.0) if (viewingRadius < 200.0)
{ {
viewingRadius = 100.0; viewingRadius = 200.0;
} }
double minResolution = 0.25; double minResolution = 0.25;
......
...@@ -25,7 +25,7 @@ This file is part of the QGROUNDCONTROL project ...@@ -25,7 +25,7 @@ This file is part of the QGROUNDCONTROL project
* @file * @file
* @brief Definition of the class Texture. * @brief Definition of the class Texture.
* *
* @author Lionel Heng <hengli@student.ethz.ch> * @author Lionel Heng <hengli@inf.ethz.ch>
* *
*/ */
...@@ -33,77 +33,86 @@ This file is part of the QGROUNDCONTROL project ...@@ -33,77 +33,86 @@ This file is part of the QGROUNDCONTROL project
#include "Texture.h" #include "Texture.h"
Texture::Texture(unsigned int _id) Texture::Texture(quint64 id)
: id(_id) : mId(id)
, texture2D(new osg::Texture2D) , mTexture2D(new osg::Texture2D)
, geometry(new osg::Geometry) , mGeometry(new osg::Geometry)
{ {
texture2D->setFilter(osg::Texture::MIN_FILTER, osg::Texture::NEAREST); mTexture2D->setFilter(osg::Texture::MIN_FILTER, osg::Texture::NEAREST);
texture2D->setFilter(osg::Texture::MAG_FILTER, osg::Texture::NEAREST); mTexture2D->setFilter(osg::Texture::MAG_FILTER, osg::Texture::NEAREST);
texture2D->setDataVariance(osg::Object::DYNAMIC); mTexture2D->setDataVariance(osg::Object::DYNAMIC);
texture2D->setResizeNonPowerOfTwoHint(false); mTexture2D->setResizeNonPowerOfTwoHint(false);
osg::ref_ptr<osg::Image> image = new osg::Image; osg::ref_ptr<osg::Image> image = new osg::Image;
texture2D->setImage(image); mTexture2D->setImage(image);
osg::ref_ptr<osg::Vec3dArray> vertices(new osg::Vec3dArray(4)); osg::ref_ptr<osg::Vec3dArray> vertices(new osg::Vec3dArray(4));
geometry->setVertexArray(vertices); mGeometry->setVertexArray(vertices);
osg::ref_ptr<osg::Vec2Array> textureCoords = new osg::Vec2Array; osg::ref_ptr<osg::Vec2Array> textureCoords = new osg::Vec2Array;
textureCoords->push_back(osg::Vec2(0.0f, 1.0f)); textureCoords->push_back(osg::Vec2(0.0f, 1.0f));
textureCoords->push_back(osg::Vec2(1.0f, 1.0f)); textureCoords->push_back(osg::Vec2(1.0f, 1.0f));
textureCoords->push_back(osg::Vec2(1.0f, 0.0f)); textureCoords->push_back(osg::Vec2(1.0f, 0.0f));
textureCoords->push_back(osg::Vec2(0.0f, 0.0f)); textureCoords->push_back(osg::Vec2(0.0f, 0.0f));
geometry->setTexCoordArray(0, textureCoords); mGeometry->setTexCoordArray(0, textureCoords);
geometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES, mGeometry->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES,
0, 4)); 0, 4));
osg::ref_ptr<osg::Vec4Array> colors(new osg::Vec4Array); osg::ref_ptr<osg::Vec4Array> colors(new osg::Vec4Array);
colors->push_back(osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f)); colors->push_back(osg::Vec4(0.0f, 0.0f, 1.0f, 1.0f));
geometry->setColorArray(colors); mGeometry->setColorArray(colors);
geometry->setColorBinding(osg::Geometry::BIND_OVERALL); mGeometry->setColorBinding(osg::Geometry::BIND_OVERALL);
geometry->setUseDisplayList(false); mGeometry->setUseDisplayList(false);
osg::ref_ptr<osg::LineWidth> linewidth(new osg::LineWidth); osg::ref_ptr<osg::LineWidth> linewidth(new osg::LineWidth);
linewidth->setWidth(2.0f); linewidth->setWidth(2.0f);
geometry->getOrCreateStateSet()-> mGeometry->getOrCreateStateSet()->
setAttributeAndModes(linewidth, osg::StateAttribute::ON); setAttributeAndModes(linewidth, osg::StateAttribute::ON);
geometry->getOrCreateStateSet()-> mGeometry->getOrCreateStateSet()->
setMode(GL_LIGHTING, osg::StateAttribute::OFF); setMode(GL_LIGHTING, osg::StateAttribute::OFF);
} }
const QString& const QString&
Texture::getSourceURL(void) const Texture::getSourceURL(void) const
{ {
return sourceURL; return mSourceURL;
}
void
Texture::setId(quint64 id)
{
mId = id;
} }
void void
Texture::sync(const WebImagePtr& image) Texture::sync(const WebImagePtr& image)
{ {
state = static_cast<State>(image->getState()); mState = static_cast<State>(image->getState());
if (image->getState() != WebImage::UNINITIALIZED && if (image->getState() != WebImage::UNINITIALIZED &&
sourceURL != image->getSourceURL()) { mSourceURL != image->getSourceURL())
sourceURL = image->getSourceURL(); {
mSourceURL = image->getSourceURL();
} }
if (image->getState() == WebImage::READY && image->getSyncFlag()) { if (image->getState() == WebImage::READY && image->getSyncFlag())
{
image->setSyncFlag(false); image->setSyncFlag(false);
if (texture2D->getImage() != NULL) { if (mTexture2D->getImage() != NULL)
texture2D->getImage()->setImage(image->getWidth(), {
image->getHeight(), mTexture2D->getImage()->setImage(image->getWidth(),
1, image->getHeight(),
GL_RGBA, 1,
GL_RGBA, GL_RGBA,
GL_UNSIGNED_BYTE, GL_RGBA,
image->getImageData(), GL_UNSIGNED_BYTE,
osg::Image::NO_DELETE); image->getImageData(),
texture2D->getImage()->dirty(); osg::Image::NO_DELETE);
mTexture2D->getImage()->dirty();
} }
} }
} }
...@@ -123,40 +132,44 @@ Texture::draw(double x1, double y1, double x2, double y2, ...@@ -123,40 +132,44 @@ Texture::draw(double x1, double y1, double x2, double y2,
bool smoothInterpolation) const bool smoothInterpolation) const
{ {
osg::Vec3dArray* vertices = osg::Vec3dArray* vertices =
static_cast<osg::Vec3dArray*>(geometry->getVertexArray()); static_cast<osg::Vec3dArray*>(mGeometry->getVertexArray());
(*vertices)[0].set(x1, y1, z); (*vertices)[0].set(x1, y1, z);
(*vertices)[1].set(x2, y2, z); (*vertices)[1].set(x2, y2, z);
(*vertices)[2].set(x3, y3, z); (*vertices)[2].set(x3, y3, z);
(*vertices)[3].set(x4, y4, z); (*vertices)[3].set(x4, y4, z);
osg::DrawArrays* drawarrays = osg::DrawArrays* drawarrays =
static_cast<osg::DrawArrays*>(geometry->getPrimitiveSet(0)); static_cast<osg::DrawArrays*>(mGeometry->getPrimitiveSet(0));
osg::Vec4Array* colors = osg::Vec4Array* colors =
static_cast<osg::Vec4Array*>(geometry->getColorArray()); static_cast<osg::Vec4Array*>(mGeometry->getColorArray());
if (state == REQUESTED) { if (mState == REQUESTED)
{
drawarrays->set(osg::PrimitiveSet::LINE_LOOP, 0, 4); drawarrays->set(osg::PrimitiveSet::LINE_LOOP, 0, 4);
(*colors)[0].set(0.0f, 0.0f, 1.0f, 1.0f); (*colors)[0].set(0.0f, 0.0f, 1.0f, 1.0f);
geometry->getOrCreateStateSet()-> mGeometry->getOrCreateStateSet()->
setTextureAttributeAndModes(0, texture2D, osg::StateAttribute::OFF); setTextureAttributeAndModes(0, mTexture2D, osg::StateAttribute::OFF);
return geometry; return mGeometry;
} }
if (smoothInterpolation) { if (smoothInterpolation)
texture2D->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR); {
texture2D->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR); mTexture2D->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);
} else { mTexture2D->setFilter(osg::Texture::MAG_FILTER, osg::Texture::LINEAR);
texture2D->setFilter(osg::Texture::MIN_FILTER, osg::Texture::NEAREST); }
texture2D->setFilter(osg::Texture::MAG_FILTER, osg::Texture::NEAREST); else
{
mTexture2D->setFilter(osg::Texture::MIN_FILTER, osg::Texture::NEAREST);
mTexture2D->setFilter(osg::Texture::MAG_FILTER, osg::Texture::NEAREST);
} }
drawarrays->set(osg::PrimitiveSet::POLYGON, 0, 4); drawarrays->set(osg::PrimitiveSet::POLYGON, 0, 4);
(*colors)[0].set(1.0f, 1.0f, 1.0f, 1.0f); (*colors)[0].set(1.0f, 1.0f, 1.0f, 1.0f);
geometry->getOrCreateStateSet()-> mGeometry->getOrCreateStateSet()->
setTextureAttributeAndModes(0, texture2D, osg::StateAttribute::ON); setTextureAttributeAndModes(0, mTexture2D, osg::StateAttribute::ON);
return geometry; return mGeometry;
} }
...@@ -25,7 +25,7 @@ This file is part of the QGROUNDCONTROL project ...@@ -25,7 +25,7 @@ This file is part of the QGROUNDCONTROL project
* @file * @file
* @brief Definition of the class Texture. * @brief Definition of the class Texture.
* *
* @author Lionel Heng <hengli@student.ethz.ch> * @author Lionel Heng <hengli@inf.ethz.ch>
* *
*/ */
...@@ -43,11 +43,11 @@ This file is part of the QGROUNDCONTROL project ...@@ -43,11 +43,11 @@ This file is part of the QGROUNDCONTROL project
class Texture class Texture
{ {
public: public:
explicit Texture(unsigned int _id); explicit Texture(quint64 id);
const QString& getSourceURL(void) const; const QString& getSourceURL(void) const;
void setId(unsigned int _id); void setId(quint64 id);
void sync(const WebImagePtr& image); void sync(const WebImagePtr& image);
...@@ -60,17 +60,18 @@ public: ...@@ -60,17 +60,18 @@ public:
bool smoothInterpolation) const; bool smoothInterpolation) const;
private: private:
enum State { enum State
{
UNINITIALIZED = 0, UNINITIALIZED = 0,
REQUESTED = 1, REQUESTED = 1,
READY = 2 READY = 2
}; };
State state; State mState;
QString sourceURL; QString mSourceURL;
unsigned int id; quint64 mId;
osg::ref_ptr<osg::Texture2D> texture2D; osg::ref_ptr<osg::Texture2D> mTexture2D;
osg::ref_ptr<osg::Geometry> geometry; osg::ref_ptr<osg::Geometry> mGeometry;
}; };
typedef QSharedPointer<Texture> TexturePtr; typedef QSharedPointer<Texture> TexturePtr;
......
...@@ -25,34 +25,37 @@ This file is part of the QGROUNDCONTROL project ...@@ -25,34 +25,37 @@ This file is part of the QGROUNDCONTROL project
* @file * @file
* @brief Definition of the class TextureCache. * @brief Definition of the class TextureCache.
* *
* @author Lionel Heng <hengli@student.ethz.ch> * @author Lionel Heng <hengli@inf.ethz.ch>
* *
*/ */
#include "TextureCache.h" #include "TextureCache.h"
TextureCache::TextureCache(uint32_t _cacheSize) TextureCache::TextureCache(int cacheSize)
: cacheSize(_cacheSize) : mCacheSize(cacheSize)
, imageCache(new WebImageCache(0, cacheSize)) , mImageCache(new WebImageCache(0, cacheSize))
{ {
for (uint32_t i = 0; i < cacheSize; ++i) { for (int i = 0; i < mCacheSize; ++i)
{
TexturePtr t(new Texture(i)); TexturePtr t(new Texture(i));
textures.push_back(t); mTextures.push_back(t);
} }
} }
TexturePtr TexturePtr
TextureCache::get(const QString& tileURL) TextureCache::get(const QString& tileURL)
{ {
QPair<TexturePtr, int32_t> p1 = lookup(tileURL); QPair<TexturePtr, int> p1 = lookup(tileURL);
if (!p1.first.isNull()) { if (!p1.first.isNull())
{
return p1.first; return p1.first;
} }
QPair<WebImagePtr, int32_t> p2 = imageCache->lookup(tileURL); QPair<WebImagePtr, int> p2 = mImageCache->lookup(tileURL);
if (!p2.first.isNull()) { if (!p2.first.isNull())
textures[p2.second]->sync(p2.first); {
mTextures[p2.second]->sync(p2.first);
p1 = lookup(tileURL); p1 = lookup(tileURL);
return p1.first; return p1.first;
...@@ -64,19 +67,23 @@ TextureCache::get(const QString& tileURL) ...@@ -64,19 +67,23 @@ TextureCache::get(const QString& tileURL)
void void
TextureCache::sync(void) TextureCache::sync(void)
{ {
if (requireSync()) { if (requireSync())
for (int32_t i = 0; i < textures.size(); ++i) { {
textures[i]->sync(imageCache->at(i)); for (int i = 0; i < mTextures.size(); ++i)
{
mTextures[i]->sync(mImageCache->at(i));
} }
} }
} }
QPair<TexturePtr, int32_t> QPair<TexturePtr, int>
TextureCache::lookup(const QString& tileURL) TextureCache::lookup(const QString& tileURL)
{ {
for (int32_t i = 0; i < textures.size(); ++i) { for (int i = 0; i < mTextures.size(); ++i)
if (textures[i]->getSourceURL() == tileURL) { {
return qMakePair(textures[i], i); if (mTextures[i]->getSourceURL() == tileURL)
{
return qMakePair(mTextures[i], i);
} }
} }
...@@ -86,8 +93,10 @@ TextureCache::lookup(const QString& tileURL) ...@@ -86,8 +93,10 @@ TextureCache::lookup(const QString& tileURL)
bool bool
TextureCache::requireSync(void) const TextureCache::requireSync(void) const
{ {
for (uint32_t i = 0; i < cacheSize; ++i) { for (int i = 0; i < mCacheSize; ++i)
if (imageCache->at(i)->getSyncFlag()) { {
if (mImageCache->at(i)->getSyncFlag())
{
return true; return true;
} }
} }
......
...@@ -25,7 +25,7 @@ This file is part of the QGROUNDCONTROL project ...@@ -25,7 +25,7 @@ This file is part of the QGROUNDCONTROL project
* @file * @file
* @brief Definition of the class TextureCache. * @brief Definition of the class TextureCache.
* *
* @author Lionel Heng <hengli@student.ethz.ch> * @author Lionel Heng <hengli@inf.ethz.ch>
* *
*/ */
...@@ -40,7 +40,7 @@ This file is part of the QGROUNDCONTROL project ...@@ -40,7 +40,7 @@ This file is part of the QGROUNDCONTROL project
class TextureCache class TextureCache
{ {
public: public:
explicit TextureCache(uint32_t cacheSize); explicit TextureCache(int cacheSize);
TexturePtr get(const QString& tileURL); TexturePtr get(const QString& tileURL);
...@@ -51,10 +51,10 @@ private: ...@@ -51,10 +51,10 @@ private:
bool requireSync(void) const; bool requireSync(void) const;
uint32_t cacheSize; int mCacheSize;
QVector<TexturePtr> textures; QVector<TexturePtr> mTextures;
QScopedPointer<WebImageCache> imageCache; QScopedPointer<WebImageCache> mImageCache;
}; };
#endif // TEXTURECACHE_H #endif // TEXTURECACHE_H
...@@ -35,11 +35,11 @@ This file is part of the QGROUNDCONTROL project ...@@ -35,11 +35,11 @@ This file is part of the QGROUNDCONTROL project
#include <QGLWidget> #include <QGLWidget>
WebImage::WebImage() WebImage::WebImage()
: state(WebImage::UNINITIALIZED) : mState(WebImage::UNINITIALIZED)
, sourceURL("") , mSourceURL("")
, image(0) , mImage(0)
, lastReference(0) , mLastReference(0)
, syncFlag(false) , mSyncFlag(false)
{ {
} }
...@@ -47,54 +47,58 @@ WebImage::WebImage() ...@@ -47,54 +47,58 @@ WebImage::WebImage()
void void
WebImage::clear(void) WebImage::clear(void)
{ {
image.reset(); mImage.reset();
sourceURL.clear(); mSourceURL.clear();
state = WebImage::UNINITIALIZED; mState = WebImage::UNINITIALIZED;
lastReference = 0; mLastReference = 0;
} }
WebImage::State WebImage::State
WebImage::getState(void) const WebImage::getState(void) const
{ {
return state; return mState;
} }
void void
WebImage::setState(State state) WebImage::setState(State state)
{ {
this->state = state; mState = state;
} }
const QString& const QString&
WebImage::getSourceURL(void) const WebImage::getSourceURL(void) const
{ {
return sourceURL; return mSourceURL;
} }
void void
WebImage::setSourceURL(const QString& url) WebImage::setSourceURL(const QString& url)
{ {
sourceURL = url; mSourceURL = url;
} }
uchar* uchar*
WebImage::getImageData(void) const WebImage::getImageData(void) const
{ {
return image->scanLine(0); return mImage->scanLine(0);
} }
bool bool
WebImage::setData(const QByteArray& data) WebImage::setData(const QByteArray& data)
{ {
QImage tempImage; QImage tempImage;
if (tempImage.loadFromData(data)) { if (tempImage.loadFromData(data))
if (image.isNull()) { {
image.reset(new QImage); if (mImage.isNull())
{
mImage.reset(new QImage);
} }
*image = QGLWidget::convertToGLFormat(tempImage); *mImage = QGLWidget::convertToGLFormat(tempImage);
return true; return true;
} else { }
else
{
return false; return false;
} }
} }
...@@ -103,14 +107,18 @@ bool ...@@ -103,14 +107,18 @@ bool
WebImage::setData(const QString& filename) WebImage::setData(const QString& filename)
{ {
QImage tempImage; QImage tempImage;
if (tempImage.load(filename)) { if (tempImage.load(filename))
if (image.isNull()) { {
image.reset(new QImage); if (mImage.isNull())
{
mImage.reset(new QImage);
} }
*image = QGLWidget::convertToGLFormat(tempImage); *mImage = QGLWidget::convertToGLFormat(tempImage);
return true; return true;
} else { }
else
{
return false; return false;
} }
} }
...@@ -118,41 +126,41 @@ WebImage::setData(const QString& filename) ...@@ -118,41 +126,41 @@ WebImage::setData(const QString& filename)
int int
WebImage::getWidth(void) const WebImage::getWidth(void) const
{ {
return image->width(); return mImage->width();
} }
int int
WebImage::getHeight(void) const WebImage::getHeight(void) const
{ {
return image->height(); return mImage->height();
} }
int int
WebImage::getByteCount(void) const WebImage::getByteCount(void) const
{ {
return image->byteCount(); return mImage->byteCount();
} }
ulong quint64
WebImage::getLastReference(void) const WebImage::getLastReference(void) const
{ {
return lastReference; return mLastReference;
} }
void void
WebImage::setLastReference(ulong value) WebImage::setLastReference(quint64 value)
{ {
lastReference = value; mLastReference = value;
} }
bool bool
WebImage::getSyncFlag(void) const WebImage::getSyncFlag(void) const
{ {
return syncFlag; return mSyncFlag;
} }
void void
WebImage::setSyncFlag(bool onoff) WebImage::setSyncFlag(bool onoff)
{ {
syncFlag = onoff; mSyncFlag = onoff;
} }
...@@ -64,18 +64,18 @@ public: ...@@ -64,18 +64,18 @@ public:
int getHeight(void) const; int getHeight(void) const;
int getByteCount(void) const; int getByteCount(void) const;
ulong getLastReference(void) const; quint64 getLastReference(void) const;
void setLastReference(ulong value); void setLastReference(quint64 value);
bool getSyncFlag(void) const; bool getSyncFlag(void) const;
void setSyncFlag(bool onoff); void setSyncFlag(bool onoff);
private: private:
State state; State mState;
QString sourceURL; QString mSourceURL;
QScopedPointer<QImage> image; QScopedPointer<QImage> mImage;
ulong lastReference; quint64 mLastReference;
bool syncFlag; bool mSyncFlag;
}; };
typedef QSharedPointer<WebImage> WebImagePtr; typedef QSharedPointer<WebImage> WebImagePtr;
......
...@@ -25,102 +25,129 @@ This file is part of the QGROUNDCONTROL project ...@@ -25,102 +25,129 @@ This file is part of the QGROUNDCONTROL project
* @file * @file
* @brief Definition of the class WebImageCache. * @brief Definition of the class WebImageCache.
* *
* @author Lionel Heng <hengli@student.ethz.ch> * @author Lionel Heng <hengli@inf.ethz.ch>
* *
*/ */
#include "WebImageCache.h" #include "WebImageCache.h"
#include <cstdio>
#include <QNetworkReply> #include <QNetworkReply>
#include <QPixmap> #include <QPixmap>
WebImageCache::WebImageCache(QObject* parent, uint32_t _cacheSize) WebImageCache::WebImageCache(QObject* parent, int cacheSize)
: QObject(parent) : QObject(parent)
, cacheSize(_cacheSize) , mCacheSize(cacheSize)
, currentReference(0) , mCurrentReference(0)
, networkManager(new QNetworkAccessManager) , mNetworkManager(new QNetworkAccessManager)
{ {
for (uint32_t i = 0; i < cacheSize; ++i) { for (int i = 0; i < mCacheSize; ++i)
{
WebImagePtr image(new WebImage); WebImagePtr image(new WebImage);
webImages.push_back(image); mWebImages.push_back(image);
} }
connect(networkManager.data(), SIGNAL(finished(QNetworkReply*)), connect(mNetworkManager.data(), SIGNAL(finished(QNetworkReply*)),
this, SLOT(downloadFinished(QNetworkReply*))); this, SLOT(downloadFinished(QNetworkReply*)));
} }
QPair<WebImagePtr, int32_t> QPair<WebImagePtr, int>
WebImageCache::lookup(const QString& url) WebImageCache::lookup(const QString& url)
{ {
QPair<WebImagePtr, int32_t> cacheEntry; QPair<WebImagePtr, int> cacheEntry;
for (int32_t i = 0; i < webImages.size(); ++i) { for (int i = 0; i < mWebImages.size(); ++i)
if (webImages[i]->getState() != WebImage::UNINITIALIZED && {
webImages[i]->getSourceURL() == url) { WebImagePtr& image = mWebImages[i];
cacheEntry.first = webImages[i];
if (image->getState() != WebImage::UNINITIALIZED &&
image->getSourceURL() == url)
{
cacheEntry.first = image;
cacheEntry.second = i; cacheEntry.second = i;
break; break;
} }
} }
if (cacheEntry.first.isNull()) { if (cacheEntry.first.isNull())
for (int32_t i = 0; i < webImages.size(); ++i) { {
for (int i = 0; i < mWebImages.size(); ++i)
{
WebImagePtr& image = mWebImages[i];
// get uninitialized image // get uninitialized image
if (webImages[i]->getState() == WebImage::UNINITIALIZED) { if (image->getState() == WebImage::UNINITIALIZED)
cacheEntry.first = webImages[i]; {
cacheEntry.first = image;
cacheEntry.second = i; cacheEntry.second = i;
break; break;
} }
// get oldest image // get oldest image
else if (webImages[i]->getState() == WebImage::READY && else if (image->getState() == WebImage::READY &&
(cacheEntry.first.isNull() || (cacheEntry.first.isNull() ||
webImages[i]->getLastReference() < image->getLastReference() <
cacheEntry.first->getLastReference())) { cacheEntry.first->getLastReference()))
cacheEntry.first = webImages[i]; {
cacheEntry.first = image;
cacheEntry.second = i; cacheEntry.second = i;
} }
} }
if (cacheEntry.first.isNull()) { if (cacheEntry.first.isNull())
return qMakePair(WebImagePtr(), -1); {
} else { return qMakePair(WebImagePtr(), -1);
if (cacheEntry.first->getState() == WebImage::READY) { }
else
{
if (cacheEntry.first->getState() == WebImage::READY)
{
cacheEntry.first->clear(); cacheEntry.first->clear();
} }
cacheEntry.first->setSourceURL(url); cacheEntry.first->setSourceURL(url);
cacheEntry.first->setLastReference(currentReference); cacheEntry.first->setLastReference(mCurrentReference);
++currentReference; ++mCurrentReference;
cacheEntry.first->setState(WebImage::REQUESTED); cacheEntry.first->setState(WebImage::REQUESTED);
if (url.left(4).compare("http") == 0) { if (url.left(4).compare("http") == 0)
networkManager->get(QNetworkRequest(QUrl(url))); {
} else { mNetworkManager->get(QNetworkRequest(QUrl(url)));
if (cacheEntry.first->setData(url)) { }
else
{
if (cacheEntry.first->setData(url))
{
cacheEntry.first->setSyncFlag(true); cacheEntry.first->setSyncFlag(true);
cacheEntry.first->setState(WebImage::READY); cacheEntry.first->setState(WebImage::READY);
} else { }
else
{
cacheEntry.first->setState(WebImage::UNINITIALIZED); cacheEntry.first->setState(WebImage::UNINITIALIZED);
} }
} }
return cacheEntry; return cacheEntry;
} }
} else { }
if (cacheEntry.first->getState() == WebImage::READY) { else
cacheEntry.first->setLastReference(currentReference); {
++currentReference; if (cacheEntry.first->getState() == WebImage::READY)
{
cacheEntry.first->setLastReference(mCurrentReference);
++mCurrentReference;
return cacheEntry; return cacheEntry;
} else { }
else
{
return qMakePair(WebImagePtr(), -1); return qMakePair(WebImagePtr(), -1);
} }
} }
} }
WebImagePtr WebImagePtr
WebImageCache::at(int32_t index) const WebImageCache::at(int index) const
{ {
return webImages[index]; return mWebImages[index];
} }
void void
...@@ -128,17 +155,21 @@ WebImageCache::downloadFinished(QNetworkReply* reply) ...@@ -128,17 +155,21 @@ WebImageCache::downloadFinished(QNetworkReply* reply)
{ {
reply->deleteLater(); reply->deleteLater();
if (reply->error() != QNetworkReply::NoError) { if (reply->error() != QNetworkReply::NoError)
{
return; return;
} }
QVariant attribute = reply->attribute(QNetworkRequest::RedirectionTargetAttribute); QVariant attribute = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
if (attribute.isValid()) { if (attribute.isValid())
{
return; return;
} }
WebImagePtr image; WebImagePtr image;
foreach(image, webImages) { foreach(image, mWebImages)
if (reply->url().toString() == image->getSourceURL()) { {
if (reply->url().toString() == image->getSourceURL())
{
image->setData(reply->readAll()); image->setData(reply->readAll());
image->setSyncFlag(true); image->setSyncFlag(true);
image->setState(WebImage::READY); image->setState(WebImage::READY);
......
...@@ -25,7 +25,7 @@ This file is part of the QGROUNDCONTROL project ...@@ -25,7 +25,7 @@ This file is part of the QGROUNDCONTROL project
* @file * @file
* @brief Definition of the class WebImageCache. * @brief Definition of the class WebImageCache.
* *
* @author Lionel Heng <hengli@student.ethz.ch> * @author Lionel Heng <hengli@inf.ethz.ch>
* *
*/ */
...@@ -43,22 +43,22 @@ class WebImageCache : public QObject ...@@ -43,22 +43,22 @@ class WebImageCache : public QObject
Q_OBJECT Q_OBJECT
public: public:
WebImageCache(QObject* parent, uint32_t cacheSize); WebImageCache(QObject* parent, int cacheSize);
QPair<WebImagePtr, int32_t> lookup(const QString& url); QPair<WebImagePtr, int> lookup(const QString& url);
WebImagePtr at(int32_t index) const; WebImagePtr at(int index) const;
private Q_SLOTS: private Q_SLOTS:
void downloadFinished(QNetworkReply* reply); void downloadFinished(QNetworkReply* reply);
private: private:
uint32_t cacheSize; int mCacheSize;
QVector<WebImagePtr> webImages; QVector<WebImagePtr> mWebImages;
uint64_t currentReference; quint64 mCurrentReference;
QScopedPointer<QNetworkAccessManager> networkManager; QScopedPointer<QNetworkAccessManager> mNetworkManager;
}; };
#endif // WEBIMAGECACHE_H #endif // WEBIMAGECACHE_H
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment