Stellarium 0.12.2
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 
28 
30 enum TextureStatus
31 {
38  TextureStatus_Uninitialized,
43  TextureStatus_Loading,
47  TextureStatus_Loaded,
51  TextureStatus_Error
52 };
53 
55 inline QString textureStatusName(const TextureStatus status)
56 {
57  switch(status)
58  {
59  case TextureStatus_Uninitialized: return "TextureStatus_Uninitialized"; break;
60  case TextureStatus_Loaded: return "TextureStatus_Loaded"; break;
61  case TextureStatus_Loading: return "TextureStatus_Loading"; break;
62  case TextureStatus_Error: return "TextureStatus_Error"; break;
63  default: Q_ASSERT_X(false, Q_FUNC_INFO, "Unknown texture status");
64  }
65  // Avoid compiler warnings.
66  return QString();
67 }
68 
70 enum TextureLoadingMode
71 {
75  TextureLoadingMode_Normal,
80  TextureLoadingMode_Asynchronous,
86  TextureLoadingMode_LazyAsynchronous
87 };
88 
94 class StelTextureBackend : public QObject
95 {
96  Q_OBJECT
97 
98 public:
100  virtual ~StelTextureBackend(){};
101 
108  TextureStatus getStatus() const
109  {
110  return status;
111  }
112 
117  const QString& getName() const
118  {
119  return path;
120  }
121 
129  QSize getDimensions() const
130  {
131  invariant();
132  Q_ASSERT_X(status == TextureStatus_Loaded, Q_FUNC_INFO,
133  "Trying to get dimensions of a texture that is not loaded. "
134  "Use StelTextureBackend::getStatus to determine if the texture "
135  "is loaded or not.");
136  return size;
137  }
138 
142  const QString& getErrorMessage() const
143  {
144  invariant();
145  return errorMessage;
146  }
147 
148 protected:
150  const QString path;
151 
156  StelTextureBackend(const QString& path)
157  : QObject()
158  , path(path)
159  , errorMessage(QString())
160  , size(QSize(0, 0))
161  , status(TextureStatus_Uninitialized)
162  {
163  invariant();
164  }
165 
170  {
171  invariant();
172  Q_ASSERT_X(status == TextureStatus_Uninitialized, Q_FUNC_INFO,
173  "Only a texture that has not yet been initialized can start loading");
174  status = TextureStatus_Loading;
175  invariant();
176  }
177 
184  void finishedLoading(const QSize dimensions)
185  {
186  invariant();
187  Q_ASSERT_X(status == TextureStatus_Loading, Q_FUNC_INFO,
188  "Only a texture that has started loading can finish loading");
189  size = dimensions;
190  status = TextureStatus_Loaded;
191  invariant();
192  }
193 
199  void errorOccured(const QString& error)
200  {
201  invariant();
202  if(status != TextureStatus_Loading)
203  {
204  qWarning() << "Unexpected error - texture " << path;
205  qWarning() << "Texture status: " << textureStatusName(status);
206  Q_ASSERT_X(false, Q_FUNC_INFO,
207  "The only time an error can occur with a texture is during loading");
208  }
209  qWarning() << "Error occured during loading of texture " << path <<
210  ": " << error;
211  errorMessage = error;
212  status = TextureStatus_Error;
213  invariant();
214  }
215 
216 private:
218  QString errorMessage;
219 
223  QSize size;
224 
228  TextureStatus status;
229 
231  void invariant() const
232  {
233  Q_ASSERT_X(errorMessage.isEmpty() == (status != TextureStatus_Error),
234  Q_FUNC_INFO,
235  "Error message must be empty when status is not Error and non-empty otherwise");
236  }
237 };
238 
239 #endif // _STELTEXTUREBACKEND_HPP_