Stellarium 0.12.4
StelTextureBackend.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 _STELTEXTUREBACKEND_HPP_
21 #define _STELTEXTUREBACKEND_HPP_
22 
23 #include <QDebug>
24 #include <QObject>
25 #include <QSize>
26 #include <QString>
27 #include <QDir>
28 
29 
31 enum TextureStatus
32 {
39  TextureStatus_Uninitialized,
44  TextureStatus_Loading,
48  TextureStatus_Loaded,
52  TextureStatus_Error
53 };
54 
56 inline QString textureStatusName(const TextureStatus status)
57 {
58  switch(status)
59  {
60  case TextureStatus_Uninitialized: return "TextureStatus_Uninitialized"; break;
61  case TextureStatus_Loaded: return "TextureStatus_Loaded"; break;
62  case TextureStatus_Loading: return "TextureStatus_Loading"; break;
63  case TextureStatus_Error: return "TextureStatus_Error"; break;
64  default: Q_ASSERT_X(false, Q_FUNC_INFO, "Unknown texture status");
65  }
66  // Avoid compiler warnings.
67  return QString();
68 }
69 
71 enum TextureLoadingMode
72 {
76  TextureLoadingMode_Normal,
81  TextureLoadingMode_Asynchronous,
87  TextureLoadingMode_LazyAsynchronous
88 };
89 
95 class StelTextureBackend : public QObject
96 {
97  Q_OBJECT
98 
99 public:
101  virtual ~StelTextureBackend(){};
102 
109  TextureStatus getStatus() const
110  {
111  return status;
112  }
113 
118  const QString& getName() const
119  {
120  return path;
121  }
122 
130  QSize getDimensions() const
131  {
132  invariant();
133  Q_ASSERT_X(status == TextureStatus_Loaded, Q_FUNC_INFO,
134  "Trying to get dimensions of a texture that is not loaded. "
135  "Use StelTextureBackend::getStatus to determine if the texture "
136  "is loaded or not.");
137  return size;
138  }
139 
143  const QString& getErrorMessage() const
144  {
145  invariant();
146  return errorMessage;
147  }
148 
149 protected:
151  const QString path;
152 
157  StelTextureBackend(const QString& path)
158  : QObject()
159  , path(path)
160  , errorMessage(QString())
161  , size(QSize(0, 0))
162  , status(TextureStatus_Uninitialized)
163  {
164  invariant();
165  }
166 
171  {
172  invariant();
173  Q_ASSERT_X(status == TextureStatus_Uninitialized, Q_FUNC_INFO,
174  "Only a texture that has not yet been initialized can start loading");
175  status = TextureStatus_Loading;
176  invariant();
177  }
178 
185  void finishedLoading(const QSize dimensions)
186  {
187  invariant();
188  Q_ASSERT_X(status == TextureStatus_Loading, Q_FUNC_INFO,
189  "Only a texture that has started loading can finish loading");
190  size = dimensions;
191  status = TextureStatus_Loaded;
192  invariant();
193  }
194 
200  void errorOccured(const QString& error)
201  {
202  invariant();
203  if(status != TextureStatus_Loading)
204  {
205  qWarning() << "Unexpected error - texture " << QDir::toNativeSeparators(path);
206  qWarning() << "Texture status: " << textureStatusName(status);
207  Q_ASSERT_X(false, Q_FUNC_INFO,
208  "The only time an error can occur with a texture is during loading");
209  }
210  qWarning() << "Error occured during loading of texture " << QDir::toNativeSeparators(path) <<
211  ": " << error;
212  errorMessage = error;
213  status = TextureStatus_Error;
214  invariant();
215  }
216 
217 private:
219  QString errorMessage;
220 
224  QSize size;
225 
229  TextureStatus status;
230 
232  void invariant() const
233  {
234  Q_ASSERT_X(errorMessage.isEmpty() == (status != TextureStatus_Error),
235  Q_FUNC_INFO,
236  "Error message must be empty when status is not Error and non-empty otherwise");
237  }
238 };
239 
240 #endif // _STELTEXTUREBACKEND_HPP_