Stellarium 0.12.0
Home · All Namespaces · All Classes · Functions · Coding Style · Scripting · Plugins · Renderer · File Structure

StelTextureLoader.hpp

00001 /*
00002  * Stellarium
00003  * Copyright (C) 2012 Ferdinand Majerech
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * as published by the Free Software Foundation; either version 2
00008  * of the License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA  02110-1335, USA.
00018  */
00019 
00020 #ifndef _STELTEXTURELOADER_HPP_
00021 #define _STELTEXTURELOADER_HPP_
00022 
00023 #include <QApplication>
00024 #include <QDebug>
00025 #include <QImage>
00026 #include <QNetworkAccessManager>
00027 #include <QNetworkReply>
00028 #include <QObject>
00029 #include <QThread>
00030 #include <QTimer>
00031 
00032 #include "StelApp.hpp"
00033 #include "StelUtils.hpp"
00034 
00035 
00041 class StelTextureLoader : public QObject
00042 {
00043     Q_OBJECT
00044 
00045 public:
00049     virtual void abort(){};
00050 
00051 signals:
00053     void error(const QString& errorMsg);
00054 
00055 protected:
00057     StelTextureLoader(QThread* loaderThread)
00058         : QObject()
00059         , loaderThread(loaderThread)
00060     {
00061     }
00062 
00064     void moveToLoaderThread()
00065     {
00066         moveToThread(loaderThread);
00067     }
00068 
00069 private:
00071     QThread* loaderThread;
00072 };
00073 
00074 
00078 class StelHTTPTextureLoader : public StelTextureLoader
00079 {
00080     Q_OBJECT
00081 
00082 public:
00088     StelHTTPTextureLoader(const QString& url, int delay, QThread* loaderThread)
00089         : StelTextureLoader(loaderThread)
00090         , url(url)
00091         , networkReply(NULL)
00092     {
00093         QTimer::singleShot(delay, this, SLOT(start()));
00094     }
00095 
00096     virtual void abort()
00097     {
00098         Q_ASSERT_X(QThread::currentThread() == QApplication::instance()->thread(),
00099                    Q_FUNC_INFO,
00100                    "StelTextureLoader::abort must be called from the main thread");
00101         if (networkReply != NULL) {networkReply->abort();}
00102     }
00103 
00104 signals:
00106     void finished(QImage);
00107 
00108 private slots:
00110     void start()
00111     {
00112         QNetworkRequest request = QNetworkRequest(QUrl(url));
00113         // Define that preference should be given to cached files (no etag checks)
00114         request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, 
00115                              QNetworkRequest::PreferCache);
00116         request.setRawHeader("User-Agent", StelUtils::getApplicationName().toAscii());
00117         networkReply = StelApp::getInstance().getNetworkAccessManager()->get(request);
00118         connect(networkReply, SIGNAL(finished()), this, SLOT(onNetworkReply()));
00119 
00120         // Move this object outside of the main thread.
00121         moveToLoaderThread();
00122     }
00123     
00125     void onNetworkReply()
00126     {
00127         if (networkReply->error() != QNetworkReply::NoError) 
00128         {
00129             emit error(networkReply->errorString());
00130         } 
00131         else 
00132         {
00133             QByteArray data = networkReply->readAll();
00134             QImage image = QImage::fromData(data);
00135             if (image.isNull()) 
00136             {
00137                 emit error("Unable to parse image data");
00138             } else 
00139             {
00140                 emit finished(image);
00141             }
00142         }
00143         networkReply->deleteLater();
00144         networkReply = NULL; 
00145     }
00146 
00147 private:
00149     const QString url;
00151     QNetworkReply* networkReply;
00152 };
00153 
00157 class StelFileTextureLoader : public StelTextureLoader
00158 {
00159     Q_OBJECT
00160 
00161 public:
00167     StelFileTextureLoader(const QString& path, int delay, QThread* loaderThread)
00168         : StelTextureLoader(loaderThread)
00169         , path(path)
00170     {
00171         QTimer::singleShot(delay, this, SLOT(start()));
00172     }
00173 
00174 signals:
00176     void finished(QImage);
00177 
00178 private slots:
00180     void start()
00181     {
00182         // At next loop iteration we start to load from the file.
00183         QTimer::singleShot(0, this, SLOT(load()));
00184         // Move this object outside of the main thread.
00185         moveToLoaderThread();
00186     }
00187 
00189     void load()
00190     {
00191         QImage image = QImage(path);
00192         if(image.isNull())
00193         {
00194             emit error("Image " + path + " failed to load");
00195             return;
00196         }
00197         emit finished(image);
00198     }
00199 
00200 private:
00202     const QString path;
00203 };
00204 #endif // _STELTEXTURELOADER_HPP_
Generated on Thu Jan 31 14:05:40 2013 for Stellarium by  doxygen 1.6.3