Stellarium 0.12.4
StelTextureLoader.hpp
1 /*
2  * Stellarium
3  * Copyright (C) 2012 Ferdinand Majerech
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
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.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18  */
19 
20 #ifndef _STELTEXTURELOADER_HPP_
21 #define _STELTEXTURELOADER_HPP_
22 
23 #include <QApplication>
24 #include <QDebug>
25 #include <QImage>
26 #include <QNetworkAccessManager>
27 #include <QNetworkReply>
28 #include <QObject>
29 #include <QThread>
30 #include <QTimer>
31 
32 #include "StelApp.hpp"
33 #include "StelUtils.hpp"
34 
35 
41 class StelTextureLoader : public QObject
42 {
43  Q_OBJECT
44 
45 public:
49  virtual void abort(){};
50 
51 signals:
53  void error(const QString& errorMsg);
54 
55 protected:
57  StelTextureLoader(QThread* loaderThread)
58  : QObject()
59  , loaderThread(loaderThread)
60  {
61  }
62 
65  {
66  moveToThread(loaderThread);
67  }
68 
69 private:
71  QThread* loaderThread;
72 };
73 
74 
79 {
80  Q_OBJECT
81 
82 public:
88  StelHTTPTextureLoader(const QString& url, int delay, QThread* loaderThread)
89  : StelTextureLoader(loaderThread)
90  , url(url)
91  , networkReply(NULL)
92  {
93  QTimer::singleShot(delay, this, SLOT(start()));
94  }
95 
96  virtual void abort()
97  {
98  Q_ASSERT_X(QThread::currentThread() == QApplication::instance()->thread(),
99  Q_FUNC_INFO,
100  "StelTextureLoader::abort must be called from the main thread");
101  if (networkReply != NULL) {networkReply->abort();}
102  }
103 
104 signals:
106  void finished(QImage);
107 
108 private slots:
110  void start()
111  {
112  QNetworkRequest request = QNetworkRequest(QUrl(url));
113  // Define that preference should be given to cached files (no etag checks)
114  request.setAttribute(QNetworkRequest::CacheLoadControlAttribute,
115  QNetworkRequest::PreferCache);
116  request.setRawHeader("User-Agent", StelUtils::getApplicationName().toAscii());
117  networkReply = StelApp::getInstance().getNetworkAccessManager()->get(request);
118  connect(networkReply, SIGNAL(finished()), this, SLOT(onNetworkReply()));
119 
120  // Move this object outside of the main thread.
122  }
123 
125  void onNetworkReply()
126  {
127  if (networkReply->error() != QNetworkReply::NoError)
128  {
129  emit error(networkReply->errorString());
130  }
131  else
132  {
133  QByteArray data = networkReply->readAll();
134  QImage image = QImage::fromData(data);
135  if (image.isNull())
136  {
137  emit error("Unable to parse image data");
138  } else
139  {
140  emit finished(image);
141  }
142  }
143  networkReply->deleteLater();
144  networkReply = NULL;
145  }
146 
147 private:
149  const QString url;
151  QNetworkReply* networkReply;
152 };
153 
158 {
159  Q_OBJECT
160 
161 public:
167  StelFileTextureLoader(const QString& path, int delay, QThread* loaderThread)
168  : StelTextureLoader(loaderThread)
169  , path(path)
170  {
171  QTimer::singleShot(delay, this, SLOT(start()));
172  }
173 
174 signals:
176  void finished(QImage);
177 
178 private slots:
180  void start()
181  {
182  // At next loop iteration we start to load from the file.
183  QTimer::singleShot(0, this, SLOT(load()));
184  // Move this object outside of the main thread.
186  }
187 
189  void load()
190  {
191  QImage image = QImage(path);
192  if(image.isNull())
193  {
194  emit error("Image " + path + " failed to load");
195  return;
196  }
197  emit finished(image);
198  }
199 
200 private:
202  const QString path;
203 };
204 #endif // _STELTEXTURELOADER_HPP_