You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
5.1 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "imagemanager.h"
  26. #include "mapnetwork.h"
  27. #include <QtCore/QCryptographicHash>
  28. #include <QtGui/QPainter>
  29. #include <QtCore/QDateTime>
  30. static const int kDefaultTimeoutDelaySecs = 30;
  31. static const int kDefaultPixmapCacheSizeKB = 20000;
  32. ImageManager::ImageManager(MapControl* ptr) :
  33. emptyPixmap(QPixmap(1,1)),
  34. loadingPixmap(QPixmap(256,256)),
  35. net(new MapNetwork(this)),
  36. diskCache( new QNetworkDiskCache(NULL))
  37. {
  38. m_pMapControl = ptr;
  39. emptyPixmap.fill(Qt::transparent);
  40. //initialize loading image
  41. loadingPixmap.fill( Qt::transparent );
  42. QPainter paint(&loadingPixmap);
  43. QBrush brush( Qt::lightGray, Qt::Dense5Pattern );
  44. paint.fillRect(loadingPixmap.rect(), brush );
  45. paint.end();
  46. if (QPixmapCache::cacheLimit() <= kDefaultPixmapCacheSizeKB)
  47. {
  48. QPixmapCache::setCacheLimit(kDefaultPixmapCacheSizeKB);
  49. }
  50. }
  51. ImageManager::~ImageManager()
  52. {
  53. delete net;
  54. net = 0;
  55. }
  56. QPixmap ImageManager::getImage(const QString& host, const QString& url){
  57. //qDebug() << "ImageManager::getImage";
  58. QPixmap pm;
  59. if( net->imageIsLoading(url) ){
  60. //currently loading an image
  61. return loadingPixmap;
  62. } else if ( QPixmapCache::find(url, &pm) ) {
  63. //image found in cache, use this version
  64. return pm;
  65. } else if ( QPixmapCache::find(url, &pm) && !pm.isNull() ){
  66. //is image cached (memory)
  67. //we had a valid copy cached in memory (not disk) so return this
  68. return pm;
  69. } else if ( failedFetches.contains(url) && failedFetches[url].secsTo(QDateTime::currentDateTime()) < kDefaultTimeoutDelaySecs ){
  70. //prevents spamming public servers when requests fail to return an image or server returns error code (busy/ivalid useragent etc)
  71. qDebug() << "Ignored: " << url << " - last request failed less than 30 seconds ago";
  72. } else {
  73. //load from net, add empty image
  74. net->loadImage(host, url);
  75. };
  76. return emptyPixmap;
  77. };
  78. QPixmap ImageManager::prefetchImage(const QString& host, const QString& url)
  79. {
  80. // TODO See if this actually helps on the N900 & Symbian Phones
  81. #if defined Q_WS_QWS || defined Q_WS_MAEMO_5 || defined Q_WS_S60
  82. // on mobile devices we don´t want the display refreshing when tiles are received which are
  83. // prefetched... This is a performance issue, because mobile devices are very slow in
  84. // repainting the screen
  85. prefetch.append(url);
  86. #endif
  87. return getImage(host, url);
  88. }
  89. void ImageManager::receivedImage(const QPixmap pixmap, const QString& url){
  90. //qDebug() << "ImageManager::receivedImage";
  91. QPixmapCache::insert(url, pixmap);
  92. //remove from failed list (if exists) as it has now come good
  93. if (failedFetches.contains(url)){
  94. failedFetches.remove(url);
  95. }
  96. if (!prefetch.contains(url)){
  97. m_pMapControl->updateRequestNew();
  98. }else{
  99. #if defined Q_WS_QWS || defined Q_WS_MAEMO_5 || defined Q_WS_S60
  100. prefetch.remove(prefetch.indexOf(url));
  101. #endif
  102. };
  103. };
  104. void ImageManager::loadingQueueEmpty(){
  105. m_pMapControl->loadingFinished();
  106. };
  107. void ImageManager::abortLoading(){
  108. net->abortLoading();
  109. };
  110. void ImageManager::setProxy(QString host, int port, const QString username, const QString password){
  111. net->setProxy(host, port, username, password);
  112. };
  113. void ImageManager::setCacheDir(const QDir& path, const int qDiskSizeMB ){
  114. if ( !path.absolutePath().isEmpty() ){
  115. QDir cacheDir = path;
  116. if (!cacheDir.exists()){
  117. cacheDir.mkpath( cacheDir.absolutePath() );
  118. };
  119. diskCache->setCacheDirectory( cacheDir.absolutePath() );
  120. diskCache->setMaximumCacheSize( qDiskSizeMB *1024*1024 ); //Megabytes to bytes
  121. net->setDiskCache(diskCache);
  122. }else{
  123. net->setDiskCache(0);
  124. };
  125. };
  126. int ImageManager::loadQueueSize() const
  127. {
  128. return net->loadQueueSize();
  129. }
  130. void ImageManager::fetchFailed(const QString &url){
  131. qDebug() << "ImageManager::fetchFailed" << url;
  132. //store current time for this failed image to prevent loading it again until
  133. failedFetches.insert(url, QDateTime::currentDateTime());
  134. };
  135. void ImageManager::imageReceived(){
  136. qDebug() << "ImageManager::imageReceived";
  137. };
  138. void ImageManager::loadingFinished(){
  139. qDebug() << "ImageManager::loadingFinished";
  140. };